insn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * x86 instruction analysis
  4. *
  5. * Copyright (C) IBM Corporation, 2002, 2004, 2009
  6. */
  7. #include <linux/kernel.h>
  8. #ifdef __KERNEL__
  9. #include <linux/string.h>
  10. #else
  11. #include <string.h>
  12. #endif
  13. #include <asm/inat.h> /*__ignore_sync_check__ */
  14. #include <asm/insn.h> /* __ignore_sync_check__ */
  15. #include <asm/unaligned.h> /* __ignore_sync_check__ */
  16. #include <linux/errno.h>
  17. #include <linux/kconfig.h>
  18. #include <asm/emulate_prefix.h> /* __ignore_sync_check__ */
  19. #define leXX_to_cpu(t, r) \
  20. ({ \
  21. __typeof__(t) v; \
  22. switch (sizeof(t)) { \
  23. case 4: v = le32_to_cpu(r); break; \
  24. case 2: v = le16_to_cpu(r); break; \
  25. case 1: v = r; break; \
  26. default: \
  27. BUILD_BUG(); break; \
  28. } \
  29. v; \
  30. })
  31. /* Verify next sizeof(t) bytes can be on the same instruction */
  32. #define validate_next(t, insn, n) \
  33. ((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
  34. #define __get_next(t, insn) \
  35. ({ t r = get_unaligned((t *)(insn)->next_byte); (insn)->next_byte += sizeof(t); leXX_to_cpu(t, r); })
  36. #define __peek_nbyte_next(t, insn, n) \
  37. ({ t r = get_unaligned((t *)(insn)->next_byte + n); leXX_to_cpu(t, r); })
  38. #define get_next(t, insn) \
  39. ({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); })
  40. #define peek_nbyte_next(t, insn, n) \
  41. ({ if (unlikely(!validate_next(t, insn, n))) goto err_out; __peek_nbyte_next(t, insn, n); })
  42. #define peek_next(t, insn) peek_nbyte_next(t, insn, 0)
  43. /**
  44. * insn_init() - initialize struct insn
  45. * @insn: &struct insn to be initialized
  46. * @kaddr: address (in kernel memory) of instruction (or copy thereof)
  47. * @buf_len: length of the insn buffer at @kaddr
  48. * @x86_64: !0 for 64-bit kernel or 64-bit app
  49. */
  50. void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
  51. {
  52. /*
  53. * Instructions longer than MAX_INSN_SIZE (15 bytes) are invalid
  54. * even if the input buffer is long enough to hold them.
  55. */
  56. if (buf_len > MAX_INSN_SIZE)
  57. buf_len = MAX_INSN_SIZE;
  58. memset(insn, 0, sizeof(*insn));
  59. insn->kaddr = kaddr;
  60. insn->end_kaddr = kaddr + buf_len;
  61. insn->next_byte = kaddr;
  62. insn->x86_64 = x86_64 ? 1 : 0;
  63. insn->opnd_bytes = 4;
  64. if (x86_64)
  65. insn->addr_bytes = 8;
  66. else
  67. insn->addr_bytes = 4;
  68. }
  69. static const insn_byte_t xen_prefix[] = { __XEN_EMULATE_PREFIX };
  70. static const insn_byte_t kvm_prefix[] = { __KVM_EMULATE_PREFIX };
  71. static int __insn_get_emulate_prefix(struct insn *insn,
  72. const insn_byte_t *prefix, size_t len)
  73. {
  74. size_t i;
  75. for (i = 0; i < len; i++) {
  76. if (peek_nbyte_next(insn_byte_t, insn, i) != prefix[i])
  77. goto err_out;
  78. }
  79. insn->emulate_prefix_size = len;
  80. insn->next_byte += len;
  81. return 1;
  82. err_out:
  83. return 0;
  84. }
  85. static void insn_get_emulate_prefix(struct insn *insn)
  86. {
  87. if (__insn_get_emulate_prefix(insn, xen_prefix, sizeof(xen_prefix)))
  88. return;
  89. __insn_get_emulate_prefix(insn, kvm_prefix, sizeof(kvm_prefix));
  90. }
  91. /**
  92. * insn_get_prefixes - scan x86 instruction prefix bytes
  93. * @insn: &struct insn containing instruction
  94. *
  95. * Populates the @insn->prefixes bitmap, and updates @insn->next_byte
  96. * to point to the (first) opcode. No effect if @insn->prefixes.got
  97. * is already set.
  98. *
  99. * * Returns:
  100. * 0: on success
  101. * < 0: on error
  102. */
  103. int insn_get_prefixes(struct insn *insn)
  104. {
  105. struct insn_field *prefixes = &insn->prefixes;
  106. insn_attr_t attr;
  107. insn_byte_t b, lb;
  108. int i, nb;
  109. if (prefixes->got)
  110. return 0;
  111. insn_get_emulate_prefix(insn);
  112. nb = 0;
  113. lb = 0;
  114. b = peek_next(insn_byte_t, insn);
  115. attr = inat_get_opcode_attribute(b);
  116. while (inat_is_legacy_prefix(attr)) {
  117. /* Skip if same prefix */
  118. for (i = 0; i < nb; i++)
  119. if (prefixes->bytes[i] == b)
  120. goto found;
  121. if (nb == 4)
  122. /* Invalid instruction */
  123. break;
  124. prefixes->bytes[nb++] = b;
  125. if (inat_is_address_size_prefix(attr)) {
  126. /* address size switches 2/4 or 4/8 */
  127. if (insn->x86_64)
  128. insn->addr_bytes ^= 12;
  129. else
  130. insn->addr_bytes ^= 6;
  131. } else if (inat_is_operand_size_prefix(attr)) {
  132. /* oprand size switches 2/4 */
  133. insn->opnd_bytes ^= 6;
  134. }
  135. found:
  136. prefixes->nbytes++;
  137. insn->next_byte++;
  138. lb = b;
  139. b = peek_next(insn_byte_t, insn);
  140. attr = inat_get_opcode_attribute(b);
  141. }
  142. /* Set the last prefix */
  143. if (lb && lb != insn->prefixes.bytes[3]) {
  144. if (unlikely(insn->prefixes.bytes[3])) {
  145. /* Swap the last prefix */
  146. b = insn->prefixes.bytes[3];
  147. for (i = 0; i < nb; i++)
  148. if (prefixes->bytes[i] == lb)
  149. insn_set_byte(prefixes, i, b);
  150. }
  151. insn_set_byte(&insn->prefixes, 3, lb);
  152. }
  153. /* Decode REX prefix */
  154. if (insn->x86_64) {
  155. b = peek_next(insn_byte_t, insn);
  156. attr = inat_get_opcode_attribute(b);
  157. if (inat_is_rex_prefix(attr)) {
  158. insn_field_set(&insn->rex_prefix, b, 1);
  159. insn->next_byte++;
  160. if (X86_REX_W(b))
  161. /* REX.W overrides opnd_size */
  162. insn->opnd_bytes = 8;
  163. }
  164. }
  165. insn->rex_prefix.got = 1;
  166. /* Decode VEX prefix */
  167. b = peek_next(insn_byte_t, insn);
  168. attr = inat_get_opcode_attribute(b);
  169. if (inat_is_vex_prefix(attr)) {
  170. insn_byte_t b2 = peek_nbyte_next(insn_byte_t, insn, 1);
  171. if (!insn->x86_64) {
  172. /*
  173. * In 32-bits mode, if the [7:6] bits (mod bits of
  174. * ModRM) on the second byte are not 11b, it is
  175. * LDS or LES or BOUND.
  176. */
  177. if (X86_MODRM_MOD(b2) != 3)
  178. goto vex_end;
  179. }
  180. insn_set_byte(&insn->vex_prefix, 0, b);
  181. insn_set_byte(&insn->vex_prefix, 1, b2);
  182. if (inat_is_evex_prefix(attr)) {
  183. b2 = peek_nbyte_next(insn_byte_t, insn, 2);
  184. insn_set_byte(&insn->vex_prefix, 2, b2);
  185. b2 = peek_nbyte_next(insn_byte_t, insn, 3);
  186. insn_set_byte(&insn->vex_prefix, 3, b2);
  187. insn->vex_prefix.nbytes = 4;
  188. insn->next_byte += 4;
  189. if (insn->x86_64 && X86_VEX_W(b2))
  190. /* VEX.W overrides opnd_size */
  191. insn->opnd_bytes = 8;
  192. } else if (inat_is_vex3_prefix(attr)) {
  193. b2 = peek_nbyte_next(insn_byte_t, insn, 2);
  194. insn_set_byte(&insn->vex_prefix, 2, b2);
  195. insn->vex_prefix.nbytes = 3;
  196. insn->next_byte += 3;
  197. if (insn->x86_64 && X86_VEX_W(b2))
  198. /* VEX.W overrides opnd_size */
  199. insn->opnd_bytes = 8;
  200. } else {
  201. /*
  202. * For VEX2, fake VEX3-like byte#2.
  203. * Makes it easier to decode vex.W, vex.vvvv,
  204. * vex.L and vex.pp. Masking with 0x7f sets vex.W == 0.
  205. */
  206. insn_set_byte(&insn->vex_prefix, 2, b2 & 0x7f);
  207. insn->vex_prefix.nbytes = 2;
  208. insn->next_byte += 2;
  209. }
  210. }
  211. vex_end:
  212. insn->vex_prefix.got = 1;
  213. prefixes->got = 1;
  214. return 0;
  215. err_out:
  216. return -ENODATA;
  217. }
  218. /**
  219. * insn_get_opcode - collect opcode(s)
  220. * @insn: &struct insn containing instruction
  221. *
  222. * Populates @insn->opcode, updates @insn->next_byte to point past the
  223. * opcode byte(s), and set @insn->attr (except for groups).
  224. * If necessary, first collects any preceding (prefix) bytes.
  225. * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got
  226. * is already 1.
  227. *
  228. * Returns:
  229. * 0: on success
  230. * < 0: on error
  231. */
  232. int insn_get_opcode(struct insn *insn)
  233. {
  234. struct insn_field *opcode = &insn->opcode;
  235. int pfx_id, ret;
  236. insn_byte_t op;
  237. if (opcode->got)
  238. return 0;
  239. if (!insn->prefixes.got) {
  240. ret = insn_get_prefixes(insn);
  241. if (ret)
  242. return ret;
  243. }
  244. /* Get first opcode */
  245. op = get_next(insn_byte_t, insn);
  246. insn_set_byte(opcode, 0, op);
  247. opcode->nbytes = 1;
  248. /* Check if there is VEX prefix or not */
  249. if (insn_is_avx(insn)) {
  250. insn_byte_t m, p;
  251. m = insn_vex_m_bits(insn);
  252. p = insn_vex_p_bits(insn);
  253. insn->attr = inat_get_avx_attribute(op, m, p);
  254. if ((inat_must_evex(insn->attr) && !insn_is_evex(insn)) ||
  255. (!inat_accept_vex(insn->attr) &&
  256. !inat_is_group(insn->attr))) {
  257. /* This instruction is bad */
  258. insn->attr = 0;
  259. return -EINVAL;
  260. }
  261. /* VEX has only 1 byte for opcode */
  262. goto end;
  263. }
  264. insn->attr = inat_get_opcode_attribute(op);
  265. while (inat_is_escape(insn->attr)) {
  266. /* Get escaped opcode */
  267. op = get_next(insn_byte_t, insn);
  268. opcode->bytes[opcode->nbytes++] = op;
  269. pfx_id = insn_last_prefix_id(insn);
  270. insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr);
  271. }
  272. if (inat_must_vex(insn->attr)) {
  273. /* This instruction is bad */
  274. insn->attr = 0;
  275. return -EINVAL;
  276. }
  277. end:
  278. opcode->got = 1;
  279. return 0;
  280. err_out:
  281. return -ENODATA;
  282. }
  283. /**
  284. * insn_get_modrm - collect ModRM byte, if any
  285. * @insn: &struct insn containing instruction
  286. *
  287. * Populates @insn->modrm and updates @insn->next_byte to point past the
  288. * ModRM byte, if any. If necessary, first collects the preceding bytes
  289. * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1.
  290. *
  291. * Returns:
  292. * 0: on success
  293. * < 0: on error
  294. */
  295. int insn_get_modrm(struct insn *insn)
  296. {
  297. struct insn_field *modrm = &insn->modrm;
  298. insn_byte_t pfx_id, mod;
  299. int ret;
  300. if (modrm->got)
  301. return 0;
  302. if (!insn->opcode.got) {
  303. ret = insn_get_opcode(insn);
  304. if (ret)
  305. return ret;
  306. }
  307. if (inat_has_modrm(insn->attr)) {
  308. mod = get_next(insn_byte_t, insn);
  309. insn_field_set(modrm, mod, 1);
  310. if (inat_is_group(insn->attr)) {
  311. pfx_id = insn_last_prefix_id(insn);
  312. insn->attr = inat_get_group_attribute(mod, pfx_id,
  313. insn->attr);
  314. if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) {
  315. /* Bad insn */
  316. insn->attr = 0;
  317. return -EINVAL;
  318. }
  319. }
  320. }
  321. if (insn->x86_64 && inat_is_force64(insn->attr))
  322. insn->opnd_bytes = 8;
  323. modrm->got = 1;
  324. return 0;
  325. err_out:
  326. return -ENODATA;
  327. }
  328. /**
  329. * insn_rip_relative() - Does instruction use RIP-relative addressing mode?
  330. * @insn: &struct insn containing instruction
  331. *
  332. * If necessary, first collects the instruction up to and including the
  333. * ModRM byte. No effect if @insn->x86_64 is 0.
  334. */
  335. int insn_rip_relative(struct insn *insn)
  336. {
  337. struct insn_field *modrm = &insn->modrm;
  338. int ret;
  339. if (!insn->x86_64)
  340. return 0;
  341. if (!modrm->got) {
  342. ret = insn_get_modrm(insn);
  343. if (ret)
  344. return 0;
  345. }
  346. /*
  347. * For rip-relative instructions, the mod field (top 2 bits)
  348. * is zero and the r/m field (bottom 3 bits) is 0x5.
  349. */
  350. return (modrm->nbytes && (modrm->bytes[0] & 0xc7) == 0x5);
  351. }
  352. /**
  353. * insn_get_sib() - Get the SIB byte of instruction
  354. * @insn: &struct insn containing instruction
  355. *
  356. * If necessary, first collects the instruction up to and including the
  357. * ModRM byte.
  358. *
  359. * Returns:
  360. * 0: if decoding succeeded
  361. * < 0: otherwise.
  362. */
  363. int insn_get_sib(struct insn *insn)
  364. {
  365. insn_byte_t modrm;
  366. int ret;
  367. if (insn->sib.got)
  368. return 0;
  369. if (!insn->modrm.got) {
  370. ret = insn_get_modrm(insn);
  371. if (ret)
  372. return ret;
  373. }
  374. if (insn->modrm.nbytes) {
  375. modrm = insn->modrm.bytes[0];
  376. if (insn->addr_bytes != 2 &&
  377. X86_MODRM_MOD(modrm) != 3 && X86_MODRM_RM(modrm) == 4) {
  378. insn_field_set(&insn->sib,
  379. get_next(insn_byte_t, insn), 1);
  380. }
  381. }
  382. insn->sib.got = 1;
  383. return 0;
  384. err_out:
  385. return -ENODATA;
  386. }
  387. /**
  388. * insn_get_displacement() - Get the displacement of instruction
  389. * @insn: &struct insn containing instruction
  390. *
  391. * If necessary, first collects the instruction up to and including the
  392. * SIB byte.
  393. * Displacement value is sign-expanded.
  394. *
  395. * * Returns:
  396. * 0: if decoding succeeded
  397. * < 0: otherwise.
  398. */
  399. int insn_get_displacement(struct insn *insn)
  400. {
  401. insn_byte_t mod, rm, base;
  402. int ret;
  403. if (insn->displacement.got)
  404. return 0;
  405. if (!insn->sib.got) {
  406. ret = insn_get_sib(insn);
  407. if (ret)
  408. return ret;
  409. }
  410. if (insn->modrm.nbytes) {
  411. /*
  412. * Interpreting the modrm byte:
  413. * mod = 00 - no displacement fields (exceptions below)
  414. * mod = 01 - 1-byte displacement field
  415. * mod = 10 - displacement field is 4 bytes, or 2 bytes if
  416. * address size = 2 (0x67 prefix in 32-bit mode)
  417. * mod = 11 - no memory operand
  418. *
  419. * If address size = 2...
  420. * mod = 00, r/m = 110 - displacement field is 2 bytes
  421. *
  422. * If address size != 2...
  423. * mod != 11, r/m = 100 - SIB byte exists
  424. * mod = 00, SIB base = 101 - displacement field is 4 bytes
  425. * mod = 00, r/m = 101 - rip-relative addressing, displacement
  426. * field is 4 bytes
  427. */
  428. mod = X86_MODRM_MOD(insn->modrm.value);
  429. rm = X86_MODRM_RM(insn->modrm.value);
  430. base = X86_SIB_BASE(insn->sib.value);
  431. if (mod == 3)
  432. goto out;
  433. if (mod == 1) {
  434. insn_field_set(&insn->displacement,
  435. get_next(signed char, insn), 1);
  436. } else if (insn->addr_bytes == 2) {
  437. if ((mod == 0 && rm == 6) || mod == 2) {
  438. insn_field_set(&insn->displacement,
  439. get_next(short, insn), 2);
  440. }
  441. } else {
  442. if ((mod == 0 && rm == 5) || mod == 2 ||
  443. (mod == 0 && base == 5)) {
  444. insn_field_set(&insn->displacement,
  445. get_next(int, insn), 4);
  446. }
  447. }
  448. }
  449. out:
  450. insn->displacement.got = 1;
  451. return 0;
  452. err_out:
  453. return -ENODATA;
  454. }
  455. /* Decode moffset16/32/64. Return 0 if failed */
  456. static int __get_moffset(struct insn *insn)
  457. {
  458. switch (insn->addr_bytes) {
  459. case 2:
  460. insn_field_set(&insn->moffset1, get_next(short, insn), 2);
  461. break;
  462. case 4:
  463. insn_field_set(&insn->moffset1, get_next(int, insn), 4);
  464. break;
  465. case 8:
  466. insn_field_set(&insn->moffset1, get_next(int, insn), 4);
  467. insn_field_set(&insn->moffset2, get_next(int, insn), 4);
  468. break;
  469. default: /* opnd_bytes must be modified manually */
  470. goto err_out;
  471. }
  472. insn->moffset1.got = insn->moffset2.got = 1;
  473. return 1;
  474. err_out:
  475. return 0;
  476. }
  477. /* Decode imm v32(Iz). Return 0 if failed */
  478. static int __get_immv32(struct insn *insn)
  479. {
  480. switch (insn->opnd_bytes) {
  481. case 2:
  482. insn_field_set(&insn->immediate, get_next(short, insn), 2);
  483. break;
  484. case 4:
  485. case 8:
  486. insn_field_set(&insn->immediate, get_next(int, insn), 4);
  487. break;
  488. default: /* opnd_bytes must be modified manually */
  489. goto err_out;
  490. }
  491. return 1;
  492. err_out:
  493. return 0;
  494. }
  495. /* Decode imm v64(Iv/Ov), Return 0 if failed */
  496. static int __get_immv(struct insn *insn)
  497. {
  498. switch (insn->opnd_bytes) {
  499. case 2:
  500. insn_field_set(&insn->immediate1, get_next(short, insn), 2);
  501. break;
  502. case 4:
  503. insn_field_set(&insn->immediate1, get_next(int, insn), 4);
  504. insn->immediate1.nbytes = 4;
  505. break;
  506. case 8:
  507. insn_field_set(&insn->immediate1, get_next(int, insn), 4);
  508. insn_field_set(&insn->immediate2, get_next(int, insn), 4);
  509. break;
  510. default: /* opnd_bytes must be modified manually */
  511. goto err_out;
  512. }
  513. insn->immediate1.got = insn->immediate2.got = 1;
  514. return 1;
  515. err_out:
  516. return 0;
  517. }
  518. /* Decode ptr16:16/32(Ap) */
  519. static int __get_immptr(struct insn *insn)
  520. {
  521. switch (insn->opnd_bytes) {
  522. case 2:
  523. insn_field_set(&insn->immediate1, get_next(short, insn), 2);
  524. break;
  525. case 4:
  526. insn_field_set(&insn->immediate1, get_next(int, insn), 4);
  527. break;
  528. case 8:
  529. /* ptr16:64 is not exist (no segment) */
  530. return 0;
  531. default: /* opnd_bytes must be modified manually */
  532. goto err_out;
  533. }
  534. insn_field_set(&insn->immediate2, get_next(unsigned short, insn), 2);
  535. insn->immediate1.got = insn->immediate2.got = 1;
  536. return 1;
  537. err_out:
  538. return 0;
  539. }
  540. /**
  541. * insn_get_immediate() - Get the immediate in an instruction
  542. * @insn: &struct insn containing instruction
  543. *
  544. * If necessary, first collects the instruction up to and including the
  545. * displacement bytes.
  546. * Basically, most of immediates are sign-expanded. Unsigned-value can be
  547. * computed by bit masking with ((1 << (nbytes * 8)) - 1)
  548. *
  549. * Returns:
  550. * 0: on success
  551. * < 0: on error
  552. */
  553. int insn_get_immediate(struct insn *insn)
  554. {
  555. int ret;
  556. if (insn->immediate.got)
  557. return 0;
  558. if (!insn->displacement.got) {
  559. ret = insn_get_displacement(insn);
  560. if (ret)
  561. return ret;
  562. }
  563. if (inat_has_moffset(insn->attr)) {
  564. if (!__get_moffset(insn))
  565. goto err_out;
  566. goto done;
  567. }
  568. if (!inat_has_immediate(insn->attr))
  569. /* no immediates */
  570. goto done;
  571. switch (inat_immediate_size(insn->attr)) {
  572. case INAT_IMM_BYTE:
  573. insn_field_set(&insn->immediate, get_next(signed char, insn), 1);
  574. break;
  575. case INAT_IMM_WORD:
  576. insn_field_set(&insn->immediate, get_next(short, insn), 2);
  577. break;
  578. case INAT_IMM_DWORD:
  579. insn_field_set(&insn->immediate, get_next(int, insn), 4);
  580. break;
  581. case INAT_IMM_QWORD:
  582. insn_field_set(&insn->immediate1, get_next(int, insn), 4);
  583. insn_field_set(&insn->immediate2, get_next(int, insn), 4);
  584. break;
  585. case INAT_IMM_PTR:
  586. if (!__get_immptr(insn))
  587. goto err_out;
  588. break;
  589. case INAT_IMM_VWORD32:
  590. if (!__get_immv32(insn))
  591. goto err_out;
  592. break;
  593. case INAT_IMM_VWORD:
  594. if (!__get_immv(insn))
  595. goto err_out;
  596. break;
  597. default:
  598. /* Here, insn must have an immediate, but failed */
  599. goto err_out;
  600. }
  601. if (inat_has_second_immediate(insn->attr)) {
  602. insn_field_set(&insn->immediate2, get_next(signed char, insn), 1);
  603. }
  604. done:
  605. insn->immediate.got = 1;
  606. return 0;
  607. err_out:
  608. return -ENODATA;
  609. }
  610. /**
  611. * insn_get_length() - Get the length of instruction
  612. * @insn: &struct insn containing instruction
  613. *
  614. * If necessary, first collects the instruction up to and including the
  615. * immediates bytes.
  616. *
  617. * Returns:
  618. * - 0 on success
  619. * - < 0 on error
  620. */
  621. int insn_get_length(struct insn *insn)
  622. {
  623. int ret;
  624. if (insn->length)
  625. return 0;
  626. if (!insn->immediate.got) {
  627. ret = insn_get_immediate(insn);
  628. if (ret)
  629. return ret;
  630. }
  631. insn->length = (unsigned char)((unsigned long)insn->next_byte
  632. - (unsigned long)insn->kaddr);
  633. return 0;
  634. }
  635. /* Ensure this instruction is decoded completely */
  636. static inline int insn_complete(struct insn *insn)
  637. {
  638. return insn->opcode.got && insn->modrm.got && insn->sib.got &&
  639. insn->displacement.got && insn->immediate.got;
  640. }
  641. /**
  642. * insn_decode() - Decode an x86 instruction
  643. * @insn: &struct insn to be initialized
  644. * @kaddr: address (in kernel memory) of instruction (or copy thereof)
  645. * @buf_len: length of the insn buffer at @kaddr
  646. * @m: insn mode, see enum insn_mode
  647. *
  648. * Returns:
  649. * 0: if decoding succeeded
  650. * < 0: otherwise.
  651. */
  652. int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m)
  653. {
  654. int ret;
  655. /* #define INSN_MODE_KERN -1 __ignore_sync_check__ mode is only valid in the kernel */
  656. if (m == INSN_MODE_KERN)
  657. insn_init(insn, kaddr, buf_len, IS_ENABLED(CONFIG_X86_64));
  658. else
  659. insn_init(insn, kaddr, buf_len, m == INSN_MODE_64);
  660. ret = insn_get_length(insn);
  661. if (ret)
  662. return ret;
  663. if (insn_complete(insn))
  664. return 0;
  665. return -EINVAL;
  666. }