insn.c 14 KB

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