insn.c 14 KB

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