insn.c 13 KB

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