insn.c 14 KB

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