insn-eval.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * Utility functions for x86 operand and address decoding
  3. *
  4. * Copyright (C) Intel Corporation 2017
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <linux/ratelimit.h>
  9. #include <linux/mmu_context.h>
  10. #include <asm/desc_defs.h>
  11. #include <asm/desc.h>
  12. #include <asm/inat.h>
  13. #include <asm/insn.h>
  14. #include <asm/insn-eval.h>
  15. #include <asm/ldt.h>
  16. #include <asm/vm86.h>
  17. #undef pr_fmt
  18. #define pr_fmt(fmt) "insn: " fmt
  19. enum reg_type {
  20. REG_TYPE_RM = 0,
  21. REG_TYPE_INDEX,
  22. REG_TYPE_BASE,
  23. };
  24. /**
  25. * is_string_insn() - Determine if instruction is a string instruction
  26. * @insn: Instruction containing the opcode to inspect
  27. *
  28. * Returns:
  29. *
  30. * true if the instruction, determined by the opcode, is any of the
  31. * string instructions as defined in the Intel Software Development manual.
  32. * False otherwise.
  33. */
  34. static bool is_string_insn(struct insn *insn)
  35. {
  36. insn_get_opcode(insn);
  37. /* All string instructions have a 1-byte opcode. */
  38. if (insn->opcode.nbytes != 1)
  39. return false;
  40. switch (insn->opcode.bytes[0]) {
  41. case 0x6c ... 0x6f: /* INS, OUTS */
  42. case 0xa4 ... 0xa7: /* MOVS, CMPS */
  43. case 0xaa ... 0xaf: /* STOS, LODS, SCAS */
  44. return true;
  45. default:
  46. return false;
  47. }
  48. }
  49. /**
  50. * get_seg_reg_override_idx() - obtain segment register override index
  51. * @insn: Valid instruction with segment override prefixes
  52. *
  53. * Inspect the instruction prefixes in @insn and find segment overrides, if any.
  54. *
  55. * Returns:
  56. *
  57. * A constant identifying the segment register to use, among CS, SS, DS,
  58. * ES, FS, or GS. INAT_SEG_REG_DEFAULT is returned if no segment override
  59. * prefixes were found.
  60. *
  61. * -EINVAL in case of error.
  62. */
  63. static int get_seg_reg_override_idx(struct insn *insn)
  64. {
  65. int idx = INAT_SEG_REG_DEFAULT;
  66. int num_overrides = 0, i;
  67. insn_get_prefixes(insn);
  68. /* Look for any segment override prefixes. */
  69. for (i = 0; i < insn->prefixes.nbytes; i++) {
  70. insn_attr_t attr;
  71. attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
  72. switch (attr) {
  73. case INAT_MAKE_PREFIX(INAT_PFX_CS):
  74. idx = INAT_SEG_REG_CS;
  75. num_overrides++;
  76. break;
  77. case INAT_MAKE_PREFIX(INAT_PFX_SS):
  78. idx = INAT_SEG_REG_SS;
  79. num_overrides++;
  80. break;
  81. case INAT_MAKE_PREFIX(INAT_PFX_DS):
  82. idx = INAT_SEG_REG_DS;
  83. num_overrides++;
  84. break;
  85. case INAT_MAKE_PREFIX(INAT_PFX_ES):
  86. idx = INAT_SEG_REG_ES;
  87. num_overrides++;
  88. break;
  89. case INAT_MAKE_PREFIX(INAT_PFX_FS):
  90. idx = INAT_SEG_REG_FS;
  91. num_overrides++;
  92. break;
  93. case INAT_MAKE_PREFIX(INAT_PFX_GS):
  94. idx = INAT_SEG_REG_GS;
  95. num_overrides++;
  96. break;
  97. /* No default action needed. */
  98. }
  99. }
  100. /* More than one segment override prefix leads to undefined behavior. */
  101. if (num_overrides > 1)
  102. return -EINVAL;
  103. return idx;
  104. }
  105. /**
  106. * check_seg_overrides() - check if segment override prefixes are allowed
  107. * @insn: Valid instruction with segment override prefixes
  108. * @regoff: Operand offset, in pt_regs, for which the check is performed
  109. *
  110. * For a particular register used in register-indirect addressing, determine if
  111. * segment override prefixes can be used. Specifically, no overrides are allowed
  112. * for rDI if used with a string instruction.
  113. *
  114. * Returns:
  115. *
  116. * True if segment override prefixes can be used with the register indicated
  117. * in @regoff. False if otherwise.
  118. */
  119. static bool check_seg_overrides(struct insn *insn, int regoff)
  120. {
  121. if (regoff == offsetof(struct pt_regs, di) && is_string_insn(insn))
  122. return false;
  123. return true;
  124. }
  125. /**
  126. * resolve_default_seg() - resolve default segment register index for an operand
  127. * @insn: Instruction with opcode and address size. Must be valid.
  128. * @regs: Register values as seen when entering kernel mode
  129. * @off: Operand offset, in pt_regs, for which resolution is needed
  130. *
  131. * Resolve the default segment register index associated with the instruction
  132. * operand register indicated by @off. Such index is resolved based on defaults
  133. * described in the Intel Software Development Manual.
  134. *
  135. * Returns:
  136. *
  137. * If in protected mode, a constant identifying the segment register to use,
  138. * among CS, SS, ES or DS. If in long mode, INAT_SEG_REG_IGNORE.
  139. *
  140. * -EINVAL in case of error.
  141. */
  142. static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
  143. {
  144. if (user_64bit_mode(regs))
  145. return INAT_SEG_REG_IGNORE;
  146. /*
  147. * Resolve the default segment register as described in Section 3.7.4
  148. * of the Intel Software Development Manual Vol. 1:
  149. *
  150. * + DS for all references involving r[ABCD]X, and rSI.
  151. * + If used in a string instruction, ES for rDI. Otherwise, DS.
  152. * + AX, CX and DX are not valid register operands in 16-bit address
  153. * encodings but are valid for 32-bit and 64-bit encodings.
  154. * + -EDOM is reserved to identify for cases in which no register
  155. * is used (i.e., displacement-only addressing). Use DS.
  156. * + SS for rSP or rBP.
  157. * + CS for rIP.
  158. */
  159. switch (off) {
  160. case offsetof(struct pt_regs, ax):
  161. case offsetof(struct pt_regs, cx):
  162. case offsetof(struct pt_regs, dx):
  163. /* Need insn to verify address size. */
  164. if (insn->addr_bytes == 2)
  165. return -EINVAL;
  166. case -EDOM:
  167. case offsetof(struct pt_regs, bx):
  168. case offsetof(struct pt_regs, si):
  169. return INAT_SEG_REG_DS;
  170. case offsetof(struct pt_regs, di):
  171. if (is_string_insn(insn))
  172. return INAT_SEG_REG_ES;
  173. return INAT_SEG_REG_DS;
  174. case offsetof(struct pt_regs, bp):
  175. case offsetof(struct pt_regs, sp):
  176. return INAT_SEG_REG_SS;
  177. case offsetof(struct pt_regs, ip):
  178. return INAT_SEG_REG_CS;
  179. default:
  180. return -EINVAL;
  181. }
  182. }
  183. /**
  184. * resolve_seg_reg() - obtain segment register index
  185. * @insn: Instruction with operands
  186. * @regs: Register values as seen when entering kernel mode
  187. * @regoff: Operand offset, in pt_regs, used to deterimine segment register
  188. *
  189. * Determine the segment register associated with the operands and, if
  190. * applicable, prefixes and the instruction pointed by @insn.
  191. *
  192. * The segment register associated to an operand used in register-indirect
  193. * addressing depends on:
  194. *
  195. * a) Whether running in long mode (in such a case segments are ignored, except
  196. * if FS or GS are used).
  197. *
  198. * b) Whether segment override prefixes can be used. Certain instructions and
  199. * registers do not allow override prefixes.
  200. *
  201. * c) Whether segment overrides prefixes are found in the instruction prefixes.
  202. *
  203. * d) If there are not segment override prefixes or they cannot be used, the
  204. * default segment register associated with the operand register is used.
  205. *
  206. * The function checks first if segment override prefixes can be used with the
  207. * operand indicated by @regoff. If allowed, obtain such overridden segment
  208. * register index. Lastly, if not prefixes were found or cannot be used, resolve
  209. * the segment register index to use based on the defaults described in the
  210. * Intel documentation. In long mode, all segment register indexes will be
  211. * ignored, except if overrides were found for FS or GS. All these operations
  212. * are done using helper functions.
  213. *
  214. * The operand register, @regoff, is represented as the offset from the base of
  215. * pt_regs.
  216. *
  217. * As stated, the main use of this function is to determine the segment register
  218. * index based on the instruction, its operands and prefixes. Hence, @insn
  219. * must be valid. However, if @regoff indicates rIP, we don't need to inspect
  220. * @insn at all as in this case CS is used in all cases. This case is checked
  221. * before proceeding further.
  222. *
  223. * Please note that this function does not return the value in the segment
  224. * register (i.e., the segment selector) but our defined index. The segment
  225. * selector needs to be obtained using get_segment_selector() and passing the
  226. * segment register index resolved by this function.
  227. *
  228. * Returns:
  229. *
  230. * An index identifying the segment register to use, among CS, SS, DS,
  231. * ES, FS, or GS. INAT_SEG_REG_IGNORE is returned if running in long mode.
  232. *
  233. * -EINVAL in case of error.
  234. */
  235. static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff)
  236. {
  237. int idx;
  238. /*
  239. * In the unlikely event of having to resolve the segment register
  240. * index for rIP, do it first. Segment override prefixes should not
  241. * be used. Hence, it is not necessary to inspect the instruction,
  242. * which may be invalid at this point.
  243. */
  244. if (regoff == offsetof(struct pt_regs, ip)) {
  245. if (user_64bit_mode(regs))
  246. return INAT_SEG_REG_IGNORE;
  247. else
  248. return INAT_SEG_REG_CS;
  249. }
  250. if (!insn)
  251. return -EINVAL;
  252. if (!check_seg_overrides(insn, regoff))
  253. return resolve_default_seg(insn, regs, regoff);
  254. idx = get_seg_reg_override_idx(insn);
  255. if (idx < 0)
  256. return idx;
  257. if (idx == INAT_SEG_REG_DEFAULT)
  258. return resolve_default_seg(insn, regs, regoff);
  259. /*
  260. * In long mode, segment override prefixes are ignored, except for
  261. * overrides for FS and GS.
  262. */
  263. if (user_64bit_mode(regs)) {
  264. if (idx != INAT_SEG_REG_FS &&
  265. idx != INAT_SEG_REG_GS)
  266. idx = INAT_SEG_REG_IGNORE;
  267. }
  268. return idx;
  269. }
  270. /**
  271. * get_segment_selector() - obtain segment selector
  272. * @regs: Register values as seen when entering kernel mode
  273. * @seg_reg_idx: Segment register index to use
  274. *
  275. * Obtain the segment selector from any of the CS, SS, DS, ES, FS, GS segment
  276. * registers. In CONFIG_X86_32, the segment is obtained from either pt_regs or
  277. * kernel_vm86_regs as applicable. In CONFIG_X86_64, CS and SS are obtained
  278. * from pt_regs. DS, ES, FS and GS are obtained by reading the actual CPU
  279. * registers. This done for only for completeness as in CONFIG_X86_64 segment
  280. * registers are ignored.
  281. *
  282. * Returns:
  283. *
  284. * Value of the segment selector, including null when running in
  285. * long mode.
  286. *
  287. * -EINVAL on error.
  288. */
  289. static short get_segment_selector(struct pt_regs *regs, int seg_reg_idx)
  290. {
  291. #ifdef CONFIG_X86_64
  292. unsigned short sel;
  293. switch (seg_reg_idx) {
  294. case INAT_SEG_REG_IGNORE:
  295. return 0;
  296. case INAT_SEG_REG_CS:
  297. return (unsigned short)(regs->cs & 0xffff);
  298. case INAT_SEG_REG_SS:
  299. return (unsigned short)(regs->ss & 0xffff);
  300. case INAT_SEG_REG_DS:
  301. savesegment(ds, sel);
  302. return sel;
  303. case INAT_SEG_REG_ES:
  304. savesegment(es, sel);
  305. return sel;
  306. case INAT_SEG_REG_FS:
  307. savesegment(fs, sel);
  308. return sel;
  309. case INAT_SEG_REG_GS:
  310. savesegment(gs, sel);
  311. return sel;
  312. default:
  313. return -EINVAL;
  314. }
  315. #else /* CONFIG_X86_32 */
  316. struct kernel_vm86_regs *vm86regs = (struct kernel_vm86_regs *)regs;
  317. if (v8086_mode(regs)) {
  318. switch (seg_reg_idx) {
  319. case INAT_SEG_REG_CS:
  320. return (unsigned short)(regs->cs & 0xffff);
  321. case INAT_SEG_REG_SS:
  322. return (unsigned short)(regs->ss & 0xffff);
  323. case INAT_SEG_REG_DS:
  324. return vm86regs->ds;
  325. case INAT_SEG_REG_ES:
  326. return vm86regs->es;
  327. case INAT_SEG_REG_FS:
  328. return vm86regs->fs;
  329. case INAT_SEG_REG_GS:
  330. return vm86regs->gs;
  331. case INAT_SEG_REG_IGNORE:
  332. /* fall through */
  333. default:
  334. return -EINVAL;
  335. }
  336. }
  337. switch (seg_reg_idx) {
  338. case INAT_SEG_REG_CS:
  339. return (unsigned short)(regs->cs & 0xffff);
  340. case INAT_SEG_REG_SS:
  341. return (unsigned short)(regs->ss & 0xffff);
  342. case INAT_SEG_REG_DS:
  343. return (unsigned short)(regs->ds & 0xffff);
  344. case INAT_SEG_REG_ES:
  345. return (unsigned short)(regs->es & 0xffff);
  346. case INAT_SEG_REG_FS:
  347. return (unsigned short)(regs->fs & 0xffff);
  348. case INAT_SEG_REG_GS:
  349. /*
  350. * GS may or may not be in regs as per CONFIG_X86_32_LAZY_GS.
  351. * The macro below takes care of both cases.
  352. */
  353. return get_user_gs(regs);
  354. case INAT_SEG_REG_IGNORE:
  355. /* fall through */
  356. default:
  357. return -EINVAL;
  358. }
  359. #endif /* CONFIG_X86_64 */
  360. }
  361. static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
  362. enum reg_type type)
  363. {
  364. int regno = 0;
  365. static const int regoff[] = {
  366. offsetof(struct pt_regs, ax),
  367. offsetof(struct pt_regs, cx),
  368. offsetof(struct pt_regs, dx),
  369. offsetof(struct pt_regs, bx),
  370. offsetof(struct pt_regs, sp),
  371. offsetof(struct pt_regs, bp),
  372. offsetof(struct pt_regs, si),
  373. offsetof(struct pt_regs, di),
  374. #ifdef CONFIG_X86_64
  375. offsetof(struct pt_regs, r8),
  376. offsetof(struct pt_regs, r9),
  377. offsetof(struct pt_regs, r10),
  378. offsetof(struct pt_regs, r11),
  379. offsetof(struct pt_regs, r12),
  380. offsetof(struct pt_regs, r13),
  381. offsetof(struct pt_regs, r14),
  382. offsetof(struct pt_regs, r15),
  383. #endif
  384. };
  385. int nr_registers = ARRAY_SIZE(regoff);
  386. /*
  387. * Don't possibly decode a 32-bit instructions as
  388. * reading a 64-bit-only register.
  389. */
  390. if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
  391. nr_registers -= 8;
  392. switch (type) {
  393. case REG_TYPE_RM:
  394. regno = X86_MODRM_RM(insn->modrm.value);
  395. if (X86_REX_B(insn->rex_prefix.value))
  396. regno += 8;
  397. break;
  398. case REG_TYPE_INDEX:
  399. regno = X86_SIB_INDEX(insn->sib.value);
  400. if (X86_REX_X(insn->rex_prefix.value))
  401. regno += 8;
  402. /*
  403. * If ModRM.mod != 3 and SIB.index = 4 the scale*index
  404. * portion of the address computation is null. This is
  405. * true only if REX.X is 0. In such a case, the SIB index
  406. * is used in the address computation.
  407. */
  408. if (X86_MODRM_MOD(insn->modrm.value) != 3 && regno == 4)
  409. return -EDOM;
  410. break;
  411. case REG_TYPE_BASE:
  412. regno = X86_SIB_BASE(insn->sib.value);
  413. /*
  414. * If ModRM.mod is 0 and SIB.base == 5, the base of the
  415. * register-indirect addressing is 0. In this case, a
  416. * 32-bit displacement follows the SIB byte.
  417. */
  418. if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
  419. return -EDOM;
  420. if (X86_REX_B(insn->rex_prefix.value))
  421. regno += 8;
  422. break;
  423. default:
  424. pr_err_ratelimited("invalid register type: %d\n", type);
  425. return -EINVAL;
  426. }
  427. if (regno >= nr_registers) {
  428. WARN_ONCE(1, "decoded an instruction with an invalid register");
  429. return -EINVAL;
  430. }
  431. return regoff[regno];
  432. }
  433. /**
  434. * get_desc() - Obtain pointer to a segment descriptor
  435. * @sel: Segment selector
  436. *
  437. * Given a segment selector, obtain a pointer to the segment descriptor.
  438. * Both global and local descriptor tables are supported.
  439. *
  440. * Returns:
  441. *
  442. * Pointer to segment descriptor on success.
  443. *
  444. * NULL on error.
  445. */
  446. static struct desc_struct *get_desc(unsigned short sel)
  447. {
  448. struct desc_ptr gdt_desc = {0, 0};
  449. unsigned long desc_base;
  450. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  451. if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  452. struct desc_struct *desc = NULL;
  453. struct ldt_struct *ldt;
  454. /* Bits [15:3] contain the index of the desired entry. */
  455. sel >>= 3;
  456. mutex_lock(&current->active_mm->context.lock);
  457. ldt = current->active_mm->context.ldt;
  458. if (ldt && sel < ldt->nr_entries)
  459. desc = &ldt->entries[sel];
  460. mutex_unlock(&current->active_mm->context.lock);
  461. return desc;
  462. }
  463. #endif
  464. native_store_gdt(&gdt_desc);
  465. /*
  466. * Segment descriptors have a size of 8 bytes. Thus, the index is
  467. * multiplied by 8 to obtain the memory offset of the desired descriptor
  468. * from the base of the GDT. As bits [15:3] of the segment selector
  469. * contain the index, it can be regarded as multiplied by 8 already.
  470. * All that remains is to clear bits [2:0].
  471. */
  472. desc_base = sel & ~(SEGMENT_RPL_MASK | SEGMENT_TI_MASK);
  473. if (desc_base > gdt_desc.size)
  474. return NULL;
  475. return (struct desc_struct *)(gdt_desc.address + desc_base);
  476. }
  477. /**
  478. * insn_get_seg_base() - Obtain base address of segment descriptor.
  479. * @regs: Register values as seen when entering kernel mode
  480. * @seg_reg_idx: Index of the segment register pointing to seg descriptor
  481. *
  482. * Obtain the base address of the segment as indicated by the segment descriptor
  483. * pointed by the segment selector. The segment selector is obtained from the
  484. * input segment register index @seg_reg_idx.
  485. *
  486. * Returns:
  487. *
  488. * In protected mode, base address of the segment. Zero in long mode,
  489. * except when FS or GS are used. In virtual-8086 mode, the segment
  490. * selector shifted 4 bits to the right.
  491. *
  492. * -1L in case of error.
  493. */
  494. unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
  495. {
  496. struct desc_struct *desc;
  497. short sel;
  498. sel = get_segment_selector(regs, seg_reg_idx);
  499. if (sel < 0)
  500. return -1L;
  501. if (v8086_mode(regs))
  502. /*
  503. * Base is simply the segment selector shifted 4
  504. * bits to the right.
  505. */
  506. return (unsigned long)(sel << 4);
  507. if (user_64bit_mode(regs)) {
  508. /*
  509. * Only FS or GS will have a base address, the rest of
  510. * the segments' bases are forced to 0.
  511. */
  512. unsigned long base;
  513. if (seg_reg_idx == INAT_SEG_REG_FS)
  514. rdmsrl(MSR_FS_BASE, base);
  515. else if (seg_reg_idx == INAT_SEG_REG_GS)
  516. /*
  517. * swapgs was called at the kernel entry point. Thus,
  518. * MSR_KERNEL_GS_BASE will have the user-space GS base.
  519. */
  520. rdmsrl(MSR_KERNEL_GS_BASE, base);
  521. else
  522. base = 0;
  523. return base;
  524. }
  525. /* In protected mode the segment selector cannot be null. */
  526. if (!sel)
  527. return -1L;
  528. desc = get_desc(sel);
  529. if (!desc)
  530. return -1L;
  531. return get_desc_base(desc);
  532. }
  533. /**
  534. * get_seg_limit() - Obtain the limit of a segment descriptor
  535. * @regs: Register values as seen when entering kernel mode
  536. * @seg_reg_idx: Index of the segment register pointing to seg descriptor
  537. *
  538. * Obtain the limit of the segment as indicated by the segment descriptor
  539. * pointed by the segment selector. The segment selector is obtained from the
  540. * input segment register index @seg_reg_idx.
  541. *
  542. * Returns:
  543. *
  544. * In protected mode, the limit of the segment descriptor in bytes.
  545. * In long mode and virtual-8086 mode, segment limits are not enforced. Thus,
  546. * limit is returned as -1L to imply a limit-less segment.
  547. *
  548. * Zero is returned on error.
  549. */
  550. static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx)
  551. {
  552. struct desc_struct *desc;
  553. unsigned long limit;
  554. short sel;
  555. sel = get_segment_selector(regs, seg_reg_idx);
  556. if (sel < 0)
  557. return 0;
  558. if (user_64bit_mode(regs) || v8086_mode(regs))
  559. return -1L;
  560. if (!sel)
  561. return 0;
  562. desc = get_desc(sel);
  563. if (!desc)
  564. return 0;
  565. /*
  566. * If the granularity bit is set, the limit is given in multiples
  567. * of 4096. This also means that the 12 least significant bits are
  568. * not tested when checking the segment limits. In practice,
  569. * this means that the segment ends in (limit << 12) + 0xfff.
  570. */
  571. limit = get_desc_limit(desc);
  572. if (desc->g)
  573. limit = (limit << 12) + 0xfff;
  574. return limit;
  575. }
  576. /**
  577. * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte
  578. * @insn: Instruction containing the ModRM byte
  579. * @regs: Register values as seen when entering kernel mode
  580. *
  581. * Returns:
  582. *
  583. * The register indicated by the r/m part of the ModRM byte. The
  584. * register is obtained as an offset from the base of pt_regs. In specific
  585. * cases, the returned value can be -EDOM to indicate that the particular value
  586. * of ModRM does not refer to a register and shall be ignored.
  587. */
  588. int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)
  589. {
  590. return get_reg_offset(insn, regs, REG_TYPE_RM);
  591. }
  592. /*
  593. * return the address being referenced be instruction
  594. * for rm=3 returning the content of the rm reg
  595. * for rm!=3 calculates the address using SIB and Disp
  596. */
  597. void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
  598. {
  599. int addr_offset, base_offset, indx_offset;
  600. unsigned long linear_addr = -1L;
  601. long eff_addr, base, indx;
  602. insn_byte_t sib;
  603. insn_get_modrm(insn);
  604. insn_get_sib(insn);
  605. sib = insn->sib.value;
  606. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  607. addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
  608. if (addr_offset < 0)
  609. goto out;
  610. eff_addr = regs_get_register(regs, addr_offset);
  611. } else {
  612. if (insn->sib.nbytes) {
  613. /*
  614. * Negative values in the base and index offset means
  615. * an error when decoding the SIB byte. Except -EDOM,
  616. * which means that the registers should not be used
  617. * in the address computation.
  618. */
  619. base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
  620. if (base_offset == -EDOM)
  621. base = 0;
  622. else if (base_offset < 0)
  623. goto out;
  624. else
  625. base = regs_get_register(regs, base_offset);
  626. indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
  627. if (indx_offset == -EDOM)
  628. indx = 0;
  629. else if (indx_offset < 0)
  630. goto out;
  631. else
  632. indx = regs_get_register(regs, indx_offset);
  633. eff_addr = base + indx * (1 << X86_SIB_SCALE(sib));
  634. } else {
  635. addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
  636. if (addr_offset < 0)
  637. goto out;
  638. eff_addr = regs_get_register(regs, addr_offset);
  639. }
  640. eff_addr += insn->displacement.value;
  641. }
  642. linear_addr = (unsigned long)eff_addr;
  643. out:
  644. return (void __user *)linear_addr;
  645. }