insn.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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) && !inat_is_group(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. if (insn_is_avx(insn) && !inat_accept_vex(insn->attr))
  231. insn->attr = 0; /* This is bad */
  232. }
  233. }
  234. if (insn->x86_64 && inat_is_force64(insn->attr))
  235. insn->opnd_bytes = 8;
  236. modrm->got = 1;
  237. err_out:
  238. return;
  239. }
  240. /**
  241. * insn_rip_relative() - Does instruction use RIP-relative addressing mode?
  242. * @insn: &struct insn containing instruction
  243. *
  244. * If necessary, first collects the instruction up to and including the
  245. * ModRM byte. No effect if @insn->x86_64 is 0.
  246. */
  247. int insn_rip_relative(struct insn *insn)
  248. {
  249. struct insn_field *modrm = &insn->modrm;
  250. if (!insn->x86_64)
  251. return 0;
  252. if (!modrm->got)
  253. insn_get_modrm(insn);
  254. /*
  255. * For rip-relative instructions, the mod field (top 2 bits)
  256. * is zero and the r/m field (bottom 3 bits) is 0x5.
  257. */
  258. return (modrm->nbytes && (modrm->value & 0xc7) == 0x5);
  259. }
  260. /**
  261. * insn_get_sib() - Get the SIB byte of instruction
  262. * @insn: &struct insn containing instruction
  263. *
  264. * If necessary, first collects the instruction up to and including the
  265. * ModRM byte.
  266. */
  267. void insn_get_sib(struct insn *insn)
  268. {
  269. insn_byte_t modrm;
  270. if (insn->sib.got)
  271. return;
  272. if (!insn->modrm.got)
  273. insn_get_modrm(insn);
  274. if (insn->modrm.nbytes) {
  275. modrm = (insn_byte_t)insn->modrm.value;
  276. if (insn->addr_bytes != 2 &&
  277. X86_MODRM_MOD(modrm) != 3 && X86_MODRM_RM(modrm) == 4) {
  278. insn->sib.value = get_next(insn_byte_t, insn);
  279. insn->sib.nbytes = 1;
  280. }
  281. }
  282. insn->sib.got = 1;
  283. err_out:
  284. return;
  285. }
  286. /**
  287. * insn_get_displacement() - Get the displacement of instruction
  288. * @insn: &struct insn containing instruction
  289. *
  290. * If necessary, first collects the instruction up to and including the
  291. * SIB byte.
  292. * Displacement value is sign-expanded.
  293. */
  294. void insn_get_displacement(struct insn *insn)
  295. {
  296. insn_byte_t mod, rm, base;
  297. if (insn->displacement.got)
  298. return;
  299. if (!insn->sib.got)
  300. insn_get_sib(insn);
  301. if (insn->modrm.nbytes) {
  302. /*
  303. * Interpreting the modrm byte:
  304. * mod = 00 - no displacement fields (exceptions below)
  305. * mod = 01 - 1-byte displacement field
  306. * mod = 10 - displacement field is 4 bytes, or 2 bytes if
  307. * address size = 2 (0x67 prefix in 32-bit mode)
  308. * mod = 11 - no memory operand
  309. *
  310. * If address size = 2...
  311. * mod = 00, r/m = 110 - displacement field is 2 bytes
  312. *
  313. * If address size != 2...
  314. * mod != 11, r/m = 100 - SIB byte exists
  315. * mod = 00, SIB base = 101 - displacement field is 4 bytes
  316. * mod = 00, r/m = 101 - rip-relative addressing, displacement
  317. * field is 4 bytes
  318. */
  319. mod = X86_MODRM_MOD(insn->modrm.value);
  320. rm = X86_MODRM_RM(insn->modrm.value);
  321. base = X86_SIB_BASE(insn->sib.value);
  322. if (mod == 3)
  323. goto out;
  324. if (mod == 1) {
  325. insn->displacement.value = get_next(char, insn);
  326. insn->displacement.nbytes = 1;
  327. } else if (insn->addr_bytes == 2) {
  328. if ((mod == 0 && rm == 6) || mod == 2) {
  329. insn->displacement.value =
  330. get_next(short, insn);
  331. insn->displacement.nbytes = 2;
  332. }
  333. } else {
  334. if ((mod == 0 && rm == 5) || mod == 2 ||
  335. (mod == 0 && base == 5)) {
  336. insn->displacement.value = get_next(int, insn);
  337. insn->displacement.nbytes = 4;
  338. }
  339. }
  340. }
  341. out:
  342. insn->displacement.got = 1;
  343. err_out:
  344. return;
  345. }
  346. /* Decode moffset16/32/64 */
  347. static void __get_moffset(struct insn *insn)
  348. {
  349. switch (insn->addr_bytes) {
  350. case 2:
  351. insn->moffset1.value = get_next(short, insn);
  352. insn->moffset1.nbytes = 2;
  353. break;
  354. case 4:
  355. insn->moffset1.value = get_next(int, insn);
  356. insn->moffset1.nbytes = 4;
  357. break;
  358. case 8:
  359. insn->moffset1.value = get_next(int, insn);
  360. insn->moffset1.nbytes = 4;
  361. insn->moffset2.value = get_next(int, insn);
  362. insn->moffset2.nbytes = 4;
  363. break;
  364. }
  365. insn->moffset1.got = insn->moffset2.got = 1;
  366. err_out:
  367. return;
  368. }
  369. /* Decode imm v32(Iz) */
  370. static void __get_immv32(struct insn *insn)
  371. {
  372. switch (insn->opnd_bytes) {
  373. case 2:
  374. insn->immediate.value = get_next(short, insn);
  375. insn->immediate.nbytes = 2;
  376. break;
  377. case 4:
  378. case 8:
  379. insn->immediate.value = get_next(int, insn);
  380. insn->immediate.nbytes = 4;
  381. break;
  382. }
  383. err_out:
  384. return;
  385. }
  386. /* Decode imm v64(Iv/Ov) */
  387. static void __get_immv(struct insn *insn)
  388. {
  389. switch (insn->opnd_bytes) {
  390. case 2:
  391. insn->immediate1.value = get_next(short, insn);
  392. insn->immediate1.nbytes = 2;
  393. break;
  394. case 4:
  395. insn->immediate1.value = get_next(int, insn);
  396. insn->immediate1.nbytes = 4;
  397. break;
  398. case 8:
  399. insn->immediate1.value = get_next(int, insn);
  400. insn->immediate1.nbytes = 4;
  401. insn->immediate2.value = get_next(int, insn);
  402. insn->immediate2.nbytes = 4;
  403. break;
  404. }
  405. insn->immediate1.got = insn->immediate2.got = 1;
  406. err_out:
  407. return;
  408. }
  409. /* Decode ptr16:16/32(Ap) */
  410. static void __get_immptr(struct insn *insn)
  411. {
  412. switch (insn->opnd_bytes) {
  413. case 2:
  414. insn->immediate1.value = get_next(short, insn);
  415. insn->immediate1.nbytes = 2;
  416. break;
  417. case 4:
  418. insn->immediate1.value = get_next(int, insn);
  419. insn->immediate1.nbytes = 4;
  420. break;
  421. case 8:
  422. /* ptr16:64 is not exist (no segment) */
  423. return;
  424. }
  425. insn->immediate2.value = get_next(unsigned short, insn);
  426. insn->immediate2.nbytes = 2;
  427. insn->immediate1.got = insn->immediate2.got = 1;
  428. err_out:
  429. return;
  430. }
  431. /**
  432. * insn_get_immediate() - Get the immediates of instruction
  433. * @insn: &struct insn containing instruction
  434. *
  435. * If necessary, first collects the instruction up to and including the
  436. * displacement bytes.
  437. * Basically, most of immediates are sign-expanded. Unsigned-value can be
  438. * get by bit masking with ((1 << (nbytes * 8)) - 1)
  439. */
  440. void insn_get_immediate(struct insn *insn)
  441. {
  442. if (insn->immediate.got)
  443. return;
  444. if (!insn->displacement.got)
  445. insn_get_displacement(insn);
  446. if (inat_has_moffset(insn->attr)) {
  447. __get_moffset(insn);
  448. goto done;
  449. }
  450. if (!inat_has_immediate(insn->attr))
  451. /* no immediates */
  452. goto done;
  453. switch (inat_immediate_size(insn->attr)) {
  454. case INAT_IMM_BYTE:
  455. insn->immediate.value = get_next(char, insn);
  456. insn->immediate.nbytes = 1;
  457. break;
  458. case INAT_IMM_WORD:
  459. insn->immediate.value = get_next(short, insn);
  460. insn->immediate.nbytes = 2;
  461. break;
  462. case INAT_IMM_DWORD:
  463. insn->immediate.value = get_next(int, insn);
  464. insn->immediate.nbytes = 4;
  465. break;
  466. case INAT_IMM_QWORD:
  467. insn->immediate1.value = get_next(int, insn);
  468. insn->immediate1.nbytes = 4;
  469. insn->immediate2.value = get_next(int, insn);
  470. insn->immediate2.nbytes = 4;
  471. break;
  472. case INAT_IMM_PTR:
  473. __get_immptr(insn);
  474. break;
  475. case INAT_IMM_VWORD32:
  476. __get_immv32(insn);
  477. break;
  478. case INAT_IMM_VWORD:
  479. __get_immv(insn);
  480. break;
  481. default:
  482. break;
  483. }
  484. if (inat_has_second_immediate(insn->attr)) {
  485. insn->immediate2.value = get_next(char, insn);
  486. insn->immediate2.nbytes = 1;
  487. }
  488. done:
  489. insn->immediate.got = 1;
  490. err_out:
  491. return;
  492. }
  493. /**
  494. * insn_get_length() - Get the length of instruction
  495. * @insn: &struct insn containing instruction
  496. *
  497. * If necessary, first collects the instruction up to and including the
  498. * immediates bytes.
  499. */
  500. void insn_get_length(struct insn *insn)
  501. {
  502. if (insn->length)
  503. return;
  504. if (!insn->immediate.got)
  505. insn_get_immediate(insn);
  506. insn->length = (unsigned char)((unsigned long)insn->next_byte
  507. - (unsigned long)insn->kaddr);
  508. }