insn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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.
  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_vex3_prefix(attr)) {
  152. b2 = peek_nbyte_next(insn_byte_t, insn, 2);
  153. insn->vex_prefix.bytes[2] = b2;
  154. insn->vex_prefix.nbytes = 3;
  155. insn->next_byte += 3;
  156. if (insn->x86_64 && X86_VEX_W(b2))
  157. /* VEX.W overrides opnd_size */
  158. insn->opnd_bytes = 8;
  159. } else {
  160. /*
  161. * For VEX2, fake VEX3-like byte#2.
  162. * Makes it easier to decode vex.W, vex.vvvv,
  163. * vex.L and vex.pp. Masking with 0x7f sets vex.W == 0.
  164. */
  165. insn->vex_prefix.bytes[2] = b2 & 0x7f;
  166. insn->vex_prefix.nbytes = 2;
  167. insn->next_byte += 2;
  168. }
  169. }
  170. vex_end:
  171. insn->vex_prefix.got = 1;
  172. prefixes->got = 1;
  173. err_out:
  174. return;
  175. }
  176. /**
  177. * insn_get_opcode - collect opcode(s)
  178. * @insn: &struct insn containing instruction
  179. *
  180. * Populates @insn->opcode, updates @insn->next_byte to point past the
  181. * opcode byte(s), and set @insn->attr (except for groups).
  182. * If necessary, first collects any preceding (prefix) bytes.
  183. * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got
  184. * is already 1.
  185. */
  186. void insn_get_opcode(struct insn *insn)
  187. {
  188. struct insn_field *opcode = &insn->opcode;
  189. insn_byte_t op;
  190. int pfx_id;
  191. if (opcode->got)
  192. return;
  193. if (!insn->prefixes.got)
  194. insn_get_prefixes(insn);
  195. /* Get first opcode */
  196. op = get_next(insn_byte_t, insn);
  197. opcode->bytes[0] = op;
  198. opcode->nbytes = 1;
  199. /* Check if there is VEX prefix or not */
  200. if (insn_is_avx(insn)) {
  201. insn_byte_t m, p;
  202. m = insn_vex_m_bits(insn);
  203. p = insn_vex_p_bits(insn);
  204. insn->attr = inat_get_avx_attribute(op, m, p);
  205. if (!inat_accept_vex(insn->attr) && !inat_is_group(insn->attr))
  206. insn->attr = 0; /* This instruction is bad */
  207. goto end; /* VEX has only 1 byte for opcode */
  208. }
  209. insn->attr = inat_get_opcode_attribute(op);
  210. while (inat_is_escape(insn->attr)) {
  211. /* Get escaped opcode */
  212. op = get_next(insn_byte_t, insn);
  213. opcode->bytes[opcode->nbytes++] = op;
  214. pfx_id = insn_last_prefix_id(insn);
  215. insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr);
  216. }
  217. if (inat_must_vex(insn->attr))
  218. insn->attr = 0; /* This instruction is bad */
  219. end:
  220. opcode->got = 1;
  221. err_out:
  222. return;
  223. }
  224. /**
  225. * insn_get_modrm - collect ModRM byte, if any
  226. * @insn: &struct insn containing instruction
  227. *
  228. * Populates @insn->modrm and updates @insn->next_byte to point past the
  229. * ModRM byte, if any. If necessary, first collects the preceding bytes
  230. * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1.
  231. */
  232. void insn_get_modrm(struct insn *insn)
  233. {
  234. struct insn_field *modrm = &insn->modrm;
  235. insn_byte_t pfx_id, mod;
  236. if (modrm->got)
  237. return;
  238. if (!insn->opcode.got)
  239. insn_get_opcode(insn);
  240. if (inat_has_modrm(insn->attr)) {
  241. mod = get_next(insn_byte_t, insn);
  242. modrm->value = mod;
  243. modrm->nbytes = 1;
  244. if (inat_is_group(insn->attr)) {
  245. pfx_id = insn_last_prefix_id(insn);
  246. insn->attr = inat_get_group_attribute(mod, pfx_id,
  247. insn->attr);
  248. if (insn_is_avx(insn) && !inat_accept_vex(insn->attr))
  249. insn->attr = 0; /* This is bad */
  250. }
  251. }
  252. if (insn->x86_64 && inat_is_force64(insn->attr))
  253. insn->opnd_bytes = 8;
  254. modrm->got = 1;
  255. err_out:
  256. return;
  257. }
  258. /**
  259. * insn_rip_relative() - Does instruction use RIP-relative addressing mode?
  260. * @insn: &struct insn containing instruction
  261. *
  262. * If necessary, first collects the instruction up to and including the
  263. * ModRM byte. No effect if @insn->x86_64 is 0.
  264. */
  265. int insn_rip_relative(struct insn *insn)
  266. {
  267. struct insn_field *modrm = &insn->modrm;
  268. if (!insn->x86_64)
  269. return 0;
  270. if (!modrm->got)
  271. insn_get_modrm(insn);
  272. /*
  273. * For rip-relative instructions, the mod field (top 2 bits)
  274. * is zero and the r/m field (bottom 3 bits) is 0x5.
  275. */
  276. return (modrm->nbytes && (modrm->value & 0xc7) == 0x5);
  277. }
  278. /**
  279. * insn_get_sib() - Get the SIB byte of instruction
  280. * @insn: &struct insn containing instruction
  281. *
  282. * If necessary, first collects the instruction up to and including the
  283. * ModRM byte.
  284. */
  285. void insn_get_sib(struct insn *insn)
  286. {
  287. insn_byte_t modrm;
  288. if (insn->sib.got)
  289. return;
  290. if (!insn->modrm.got)
  291. insn_get_modrm(insn);
  292. if (insn->modrm.nbytes) {
  293. modrm = (insn_byte_t)insn->modrm.value;
  294. if (insn->addr_bytes != 2 &&
  295. X86_MODRM_MOD(modrm) != 3 && X86_MODRM_RM(modrm) == 4) {
  296. insn->sib.value = get_next(insn_byte_t, insn);
  297. insn->sib.nbytes = 1;
  298. }
  299. }
  300. insn->sib.got = 1;
  301. err_out:
  302. return;
  303. }
  304. /**
  305. * insn_get_displacement() - Get the displacement of instruction
  306. * @insn: &struct insn containing instruction
  307. *
  308. * If necessary, first collects the instruction up to and including the
  309. * SIB byte.
  310. * Displacement value is sign-expanded.
  311. */
  312. void insn_get_displacement(struct insn *insn)
  313. {
  314. insn_byte_t mod, rm, base;
  315. if (insn->displacement.got)
  316. return;
  317. if (!insn->sib.got)
  318. insn_get_sib(insn);
  319. if (insn->modrm.nbytes) {
  320. /*
  321. * Interpreting the modrm byte:
  322. * mod = 00 - no displacement fields (exceptions below)
  323. * mod = 01 - 1-byte displacement field
  324. * mod = 10 - displacement field is 4 bytes, or 2 bytes if
  325. * address size = 2 (0x67 prefix in 32-bit mode)
  326. * mod = 11 - no memory operand
  327. *
  328. * If address size = 2...
  329. * mod = 00, r/m = 110 - displacement field is 2 bytes
  330. *
  331. * If address size != 2...
  332. * mod != 11, r/m = 100 - SIB byte exists
  333. * mod = 00, SIB base = 101 - displacement field is 4 bytes
  334. * mod = 00, r/m = 101 - rip-relative addressing, displacement
  335. * field is 4 bytes
  336. */
  337. mod = X86_MODRM_MOD(insn->modrm.value);
  338. rm = X86_MODRM_RM(insn->modrm.value);
  339. base = X86_SIB_BASE(insn->sib.value);
  340. if (mod == 3)
  341. goto out;
  342. if (mod == 1) {
  343. insn->displacement.value = get_next(char, insn);
  344. insn->displacement.nbytes = 1;
  345. } else if (insn->addr_bytes == 2) {
  346. if ((mod == 0 && rm == 6) || mod == 2) {
  347. insn->displacement.value =
  348. get_next(short, insn);
  349. insn->displacement.nbytes = 2;
  350. }
  351. } else {
  352. if ((mod == 0 && rm == 5) || mod == 2 ||
  353. (mod == 0 && base == 5)) {
  354. insn->displacement.value = get_next(int, insn);
  355. insn->displacement.nbytes = 4;
  356. }
  357. }
  358. }
  359. out:
  360. insn->displacement.got = 1;
  361. err_out:
  362. return;
  363. }
  364. /* Decode moffset16/32/64. Return 0 if failed */
  365. static int __get_moffset(struct insn *insn)
  366. {
  367. switch (insn->addr_bytes) {
  368. case 2:
  369. insn->moffset1.value = get_next(short, insn);
  370. insn->moffset1.nbytes = 2;
  371. break;
  372. case 4:
  373. insn->moffset1.value = get_next(int, insn);
  374. insn->moffset1.nbytes = 4;
  375. break;
  376. case 8:
  377. insn->moffset1.value = get_next(int, insn);
  378. insn->moffset1.nbytes = 4;
  379. insn->moffset2.value = get_next(int, insn);
  380. insn->moffset2.nbytes = 4;
  381. break;
  382. default: /* opnd_bytes must be modified manually */
  383. goto err_out;
  384. }
  385. insn->moffset1.got = insn->moffset2.got = 1;
  386. return 1;
  387. err_out:
  388. return 0;
  389. }
  390. /* Decode imm v32(Iz). Return 0 if failed */
  391. static int __get_immv32(struct insn *insn)
  392. {
  393. switch (insn->opnd_bytes) {
  394. case 2:
  395. insn->immediate.value = get_next(short, insn);
  396. insn->immediate.nbytes = 2;
  397. break;
  398. case 4:
  399. case 8:
  400. insn->immediate.value = get_next(int, insn);
  401. insn->immediate.nbytes = 4;
  402. break;
  403. default: /* opnd_bytes must be modified manually */
  404. goto err_out;
  405. }
  406. return 1;
  407. err_out:
  408. return 0;
  409. }
  410. /* Decode imm v64(Iv/Ov), Return 0 if failed */
  411. static int __get_immv(struct insn *insn)
  412. {
  413. switch (insn->opnd_bytes) {
  414. case 2:
  415. insn->immediate1.value = get_next(short, insn);
  416. insn->immediate1.nbytes = 2;
  417. break;
  418. case 4:
  419. insn->immediate1.value = get_next(int, insn);
  420. insn->immediate1.nbytes = 4;
  421. break;
  422. case 8:
  423. insn->immediate1.value = get_next(int, insn);
  424. insn->immediate1.nbytes = 4;
  425. insn->immediate2.value = get_next(int, insn);
  426. insn->immediate2.nbytes = 4;
  427. break;
  428. default: /* opnd_bytes must be modified manually */
  429. goto err_out;
  430. }
  431. insn->immediate1.got = insn->immediate2.got = 1;
  432. return 1;
  433. err_out:
  434. return 0;
  435. }
  436. /* Decode ptr16:16/32(Ap) */
  437. static int __get_immptr(struct insn *insn)
  438. {
  439. switch (insn->opnd_bytes) {
  440. case 2:
  441. insn->immediate1.value = get_next(short, insn);
  442. insn->immediate1.nbytes = 2;
  443. break;
  444. case 4:
  445. insn->immediate1.value = get_next(int, insn);
  446. insn->immediate1.nbytes = 4;
  447. break;
  448. case 8:
  449. /* ptr16:64 is not exist (no segment) */
  450. return 0;
  451. default: /* opnd_bytes must be modified manually */
  452. goto err_out;
  453. }
  454. insn->immediate2.value = get_next(unsigned short, insn);
  455. insn->immediate2.nbytes = 2;
  456. insn->immediate1.got = insn->immediate2.got = 1;
  457. return 1;
  458. err_out:
  459. return 0;
  460. }
  461. /**
  462. * insn_get_immediate() - Get the immediates of instruction
  463. * @insn: &struct insn containing instruction
  464. *
  465. * If necessary, first collects the instruction up to and including the
  466. * displacement bytes.
  467. * Basically, most of immediates are sign-expanded. Unsigned-value can be
  468. * get by bit masking with ((1 << (nbytes * 8)) - 1)
  469. */
  470. void insn_get_immediate(struct insn *insn)
  471. {
  472. if (insn->immediate.got)
  473. return;
  474. if (!insn->displacement.got)
  475. insn_get_displacement(insn);
  476. if (inat_has_moffset(insn->attr)) {
  477. if (!__get_moffset(insn))
  478. goto err_out;
  479. goto done;
  480. }
  481. if (!inat_has_immediate(insn->attr))
  482. /* no immediates */
  483. goto done;
  484. switch (inat_immediate_size(insn->attr)) {
  485. case INAT_IMM_BYTE:
  486. insn->immediate.value = get_next(char, insn);
  487. insn->immediate.nbytes = 1;
  488. break;
  489. case INAT_IMM_WORD:
  490. insn->immediate.value = get_next(short, insn);
  491. insn->immediate.nbytes = 2;
  492. break;
  493. case INAT_IMM_DWORD:
  494. insn->immediate.value = get_next(int, insn);
  495. insn->immediate.nbytes = 4;
  496. break;
  497. case INAT_IMM_QWORD:
  498. insn->immediate1.value = get_next(int, insn);
  499. insn->immediate1.nbytes = 4;
  500. insn->immediate2.value = get_next(int, insn);
  501. insn->immediate2.nbytes = 4;
  502. break;
  503. case INAT_IMM_PTR:
  504. if (!__get_immptr(insn))
  505. goto err_out;
  506. break;
  507. case INAT_IMM_VWORD32:
  508. if (!__get_immv32(insn))
  509. goto err_out;
  510. break;
  511. case INAT_IMM_VWORD:
  512. if (!__get_immv(insn))
  513. goto err_out;
  514. break;
  515. default:
  516. /* Here, insn must have an immediate, but failed */
  517. goto err_out;
  518. }
  519. if (inat_has_second_immediate(insn->attr)) {
  520. insn->immediate2.value = get_next(char, insn);
  521. insn->immediate2.nbytes = 1;
  522. }
  523. done:
  524. insn->immediate.got = 1;
  525. err_out:
  526. return;
  527. }
  528. /**
  529. * insn_get_length() - Get the length of instruction
  530. * @insn: &struct insn containing instruction
  531. *
  532. * If necessary, first collects the instruction up to and including the
  533. * immediates bytes.
  534. */
  535. void insn_get_length(struct insn *insn)
  536. {
  537. if (insn->length)
  538. return;
  539. if (!insn->immediate.got)
  540. insn_get_immediate(insn);
  541. insn->length = (unsigned char)((unsigned long)insn->next_byte
  542. - (unsigned long)insn->kaddr);
  543. }