insn.c 14 KB

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