insn-eval.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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_byte_t p;
  68. insn_get_prefixes(insn);
  69. /* Look for any segment override prefixes. */
  70. for_each_insn_prefix(insn, i, p) {
  71. insn_attr_t attr;
  72. attr = inat_get_opcode_attribute(p);
  73. switch (attr) {
  74. case INAT_MAKE_PREFIX(INAT_PFX_CS):
  75. idx = INAT_SEG_REG_CS;
  76. num_overrides++;
  77. break;
  78. case INAT_MAKE_PREFIX(INAT_PFX_SS):
  79. idx = INAT_SEG_REG_SS;
  80. num_overrides++;
  81. break;
  82. case INAT_MAKE_PREFIX(INAT_PFX_DS):
  83. idx = INAT_SEG_REG_DS;
  84. num_overrides++;
  85. break;
  86. case INAT_MAKE_PREFIX(INAT_PFX_ES):
  87. idx = INAT_SEG_REG_ES;
  88. num_overrides++;
  89. break;
  90. case INAT_MAKE_PREFIX(INAT_PFX_FS):
  91. idx = INAT_SEG_REG_FS;
  92. num_overrides++;
  93. break;
  94. case INAT_MAKE_PREFIX(INAT_PFX_GS):
  95. idx = INAT_SEG_REG_GS;
  96. num_overrides++;
  97. break;
  98. /* No default action needed. */
  99. }
  100. }
  101. /* More than one segment override prefix leads to undefined behavior. */
  102. if (num_overrides > 1)
  103. return -EINVAL;
  104. return idx;
  105. }
  106. /**
  107. * check_seg_overrides() - check if segment override prefixes are allowed
  108. * @insn: Valid instruction with segment override prefixes
  109. * @regoff: Operand offset, in pt_regs, for which the check is performed
  110. *
  111. * For a particular register used in register-indirect addressing, determine if
  112. * segment override prefixes can be used. Specifically, no overrides are allowed
  113. * for rDI if used with a string instruction.
  114. *
  115. * Returns:
  116. *
  117. * True if segment override prefixes can be used with the register indicated
  118. * in @regoff. False if otherwise.
  119. */
  120. static bool check_seg_overrides(struct insn *insn, int regoff)
  121. {
  122. if (regoff == offsetof(struct pt_regs, di) && is_string_insn(insn))
  123. return false;
  124. return true;
  125. }
  126. /**
  127. * resolve_default_seg() - resolve default segment register index for an operand
  128. * @insn: Instruction with opcode and address size. Must be valid.
  129. * @regs: Register values as seen when entering kernel mode
  130. * @off: Operand offset, in pt_regs, for which resolution is needed
  131. *
  132. * Resolve the default segment register index associated with the instruction
  133. * operand register indicated by @off. Such index is resolved based on defaults
  134. * described in the Intel Software Development Manual.
  135. *
  136. * Returns:
  137. *
  138. * If in protected mode, a constant identifying the segment register to use,
  139. * among CS, SS, ES or DS. If in long mode, INAT_SEG_REG_IGNORE.
  140. *
  141. * -EINVAL in case of error.
  142. */
  143. static int resolve_default_seg(struct insn *insn, struct pt_regs *regs, int off)
  144. {
  145. if (user_64bit_mode(regs))
  146. return INAT_SEG_REG_IGNORE;
  147. /*
  148. * Resolve the default segment register as described in Section 3.7.4
  149. * of the Intel Software Development Manual Vol. 1:
  150. *
  151. * + DS for all references involving r[ABCD]X, and rSI.
  152. * + If used in a string instruction, ES for rDI. Otherwise, DS.
  153. * + AX, CX and DX are not valid register operands in 16-bit address
  154. * encodings but are valid for 32-bit and 64-bit encodings.
  155. * + -EDOM is reserved to identify for cases in which no register
  156. * is used (i.e., displacement-only addressing). Use DS.
  157. * + SS for rSP or rBP.
  158. * + CS for rIP.
  159. */
  160. switch (off) {
  161. case offsetof(struct pt_regs, ax):
  162. case offsetof(struct pt_regs, cx):
  163. case offsetof(struct pt_regs, dx):
  164. /* Need insn to verify address size. */
  165. if (insn->addr_bytes == 2)
  166. return -EINVAL;
  167. case -EDOM:
  168. case offsetof(struct pt_regs, bx):
  169. case offsetof(struct pt_regs, si):
  170. return INAT_SEG_REG_DS;
  171. case offsetof(struct pt_regs, di):
  172. if (is_string_insn(insn))
  173. return INAT_SEG_REG_ES;
  174. return INAT_SEG_REG_DS;
  175. case offsetof(struct pt_regs, bp):
  176. case offsetof(struct pt_regs, sp):
  177. return INAT_SEG_REG_SS;
  178. case offsetof(struct pt_regs, ip):
  179. return INAT_SEG_REG_CS;
  180. default:
  181. return -EINVAL;
  182. }
  183. }
  184. /**
  185. * resolve_seg_reg() - obtain segment register index
  186. * @insn: Instruction with operands
  187. * @regs: Register values as seen when entering kernel mode
  188. * @regoff: Operand offset, in pt_regs, used to deterimine segment register
  189. *
  190. * Determine the segment register associated with the operands and, if
  191. * applicable, prefixes and the instruction pointed by @insn.
  192. *
  193. * The segment register associated to an operand used in register-indirect
  194. * addressing depends on:
  195. *
  196. * a) Whether running in long mode (in such a case segments are ignored, except
  197. * if FS or GS are used).
  198. *
  199. * b) Whether segment override prefixes can be used. Certain instructions and
  200. * registers do not allow override prefixes.
  201. *
  202. * c) Whether segment overrides prefixes are found in the instruction prefixes.
  203. *
  204. * d) If there are not segment override prefixes or they cannot be used, the
  205. * default segment register associated with the operand register is used.
  206. *
  207. * The function checks first if segment override prefixes can be used with the
  208. * operand indicated by @regoff. If allowed, obtain such overridden segment
  209. * register index. Lastly, if not prefixes were found or cannot be used, resolve
  210. * the segment register index to use based on the defaults described in the
  211. * Intel documentation. In long mode, all segment register indexes will be
  212. * ignored, except if overrides were found for FS or GS. All these operations
  213. * are done using helper functions.
  214. *
  215. * The operand register, @regoff, is represented as the offset from the base of
  216. * pt_regs.
  217. *
  218. * As stated, the main use of this function is to determine the segment register
  219. * index based on the instruction, its operands and prefixes. Hence, @insn
  220. * must be valid. However, if @regoff indicates rIP, we don't need to inspect
  221. * @insn at all as in this case CS is used in all cases. This case is checked
  222. * before proceeding further.
  223. *
  224. * Please note that this function does not return the value in the segment
  225. * register (i.e., the segment selector) but our defined index. The segment
  226. * selector needs to be obtained using get_segment_selector() and passing the
  227. * segment register index resolved by this function.
  228. *
  229. * Returns:
  230. *
  231. * An index identifying the segment register to use, among CS, SS, DS,
  232. * ES, FS, or GS. INAT_SEG_REG_IGNORE is returned if running in long mode.
  233. *
  234. * -EINVAL in case of error.
  235. */
  236. static int resolve_seg_reg(struct insn *insn, struct pt_regs *regs, int regoff)
  237. {
  238. int idx;
  239. /*
  240. * In the unlikely event of having to resolve the segment register
  241. * index for rIP, do it first. Segment override prefixes should not
  242. * be used. Hence, it is not necessary to inspect the instruction,
  243. * which may be invalid at this point.
  244. */
  245. if (regoff == offsetof(struct pt_regs, ip)) {
  246. if (user_64bit_mode(regs))
  247. return INAT_SEG_REG_IGNORE;
  248. else
  249. return INAT_SEG_REG_CS;
  250. }
  251. if (!insn)
  252. return -EINVAL;
  253. if (!check_seg_overrides(insn, regoff))
  254. return resolve_default_seg(insn, regs, regoff);
  255. idx = get_seg_reg_override_idx(insn);
  256. if (idx < 0)
  257. return idx;
  258. if (idx == INAT_SEG_REG_DEFAULT)
  259. return resolve_default_seg(insn, regs, regoff);
  260. /*
  261. * In long mode, segment override prefixes are ignored, except for
  262. * overrides for FS and GS.
  263. */
  264. if (user_64bit_mode(regs)) {
  265. if (idx != INAT_SEG_REG_FS &&
  266. idx != INAT_SEG_REG_GS)
  267. idx = INAT_SEG_REG_IGNORE;
  268. }
  269. return idx;
  270. }
  271. /**
  272. * get_segment_selector() - obtain segment selector
  273. * @regs: Register values as seen when entering kernel mode
  274. * @seg_reg_idx: Segment register index to use
  275. *
  276. * Obtain the segment selector from any of the CS, SS, DS, ES, FS, GS segment
  277. * registers. In CONFIG_X86_32, the segment is obtained from either pt_regs or
  278. * kernel_vm86_regs as applicable. In CONFIG_X86_64, CS and SS are obtained
  279. * from pt_regs. DS, ES, FS and GS are obtained by reading the actual CPU
  280. * registers. This done for only for completeness as in CONFIG_X86_64 segment
  281. * registers are ignored.
  282. *
  283. * Returns:
  284. *
  285. * Value of the segment selector, including null when running in
  286. * long mode.
  287. *
  288. * -EINVAL on error.
  289. */
  290. static short get_segment_selector(struct pt_regs *regs, int seg_reg_idx)
  291. {
  292. #ifdef CONFIG_X86_64
  293. unsigned short sel;
  294. switch (seg_reg_idx) {
  295. case INAT_SEG_REG_IGNORE:
  296. return 0;
  297. case INAT_SEG_REG_CS:
  298. return (unsigned short)(regs->cs & 0xffff);
  299. case INAT_SEG_REG_SS:
  300. return (unsigned short)(regs->ss & 0xffff);
  301. case INAT_SEG_REG_DS:
  302. savesegment(ds, sel);
  303. return sel;
  304. case INAT_SEG_REG_ES:
  305. savesegment(es, sel);
  306. return sel;
  307. case INAT_SEG_REG_FS:
  308. savesegment(fs, sel);
  309. return sel;
  310. case INAT_SEG_REG_GS:
  311. savesegment(gs, sel);
  312. return sel;
  313. default:
  314. return -EINVAL;
  315. }
  316. #else /* CONFIG_X86_32 */
  317. struct kernel_vm86_regs *vm86regs = (struct kernel_vm86_regs *)regs;
  318. if (v8086_mode(regs)) {
  319. switch (seg_reg_idx) {
  320. case INAT_SEG_REG_CS:
  321. return (unsigned short)(regs->cs & 0xffff);
  322. case INAT_SEG_REG_SS:
  323. return (unsigned short)(regs->ss & 0xffff);
  324. case INAT_SEG_REG_DS:
  325. return vm86regs->ds;
  326. case INAT_SEG_REG_ES:
  327. return vm86regs->es;
  328. case INAT_SEG_REG_FS:
  329. return vm86regs->fs;
  330. case INAT_SEG_REG_GS:
  331. return vm86regs->gs;
  332. case INAT_SEG_REG_IGNORE:
  333. /* fall through */
  334. default:
  335. return -EINVAL;
  336. }
  337. }
  338. switch (seg_reg_idx) {
  339. case INAT_SEG_REG_CS:
  340. return (unsigned short)(regs->cs & 0xffff);
  341. case INAT_SEG_REG_SS:
  342. return (unsigned short)(regs->ss & 0xffff);
  343. case INAT_SEG_REG_DS:
  344. return (unsigned short)(regs->ds & 0xffff);
  345. case INAT_SEG_REG_ES:
  346. return (unsigned short)(regs->es & 0xffff);
  347. case INAT_SEG_REG_FS:
  348. return (unsigned short)(regs->fs & 0xffff);
  349. case INAT_SEG_REG_GS:
  350. /*
  351. * GS may or may not be in regs as per CONFIG_X86_32_LAZY_GS.
  352. * The macro below takes care of both cases.
  353. */
  354. return get_user_gs(regs);
  355. case INAT_SEG_REG_IGNORE:
  356. /* fall through */
  357. default:
  358. return -EINVAL;
  359. }
  360. #endif /* CONFIG_X86_64 */
  361. }
  362. static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
  363. enum reg_type type)
  364. {
  365. int regno = 0;
  366. static const int regoff[] = {
  367. offsetof(struct pt_regs, ax),
  368. offsetof(struct pt_regs, cx),
  369. offsetof(struct pt_regs, dx),
  370. offsetof(struct pt_regs, bx),
  371. offsetof(struct pt_regs, sp),
  372. offsetof(struct pt_regs, bp),
  373. offsetof(struct pt_regs, si),
  374. offsetof(struct pt_regs, di),
  375. #ifdef CONFIG_X86_64
  376. offsetof(struct pt_regs, r8),
  377. offsetof(struct pt_regs, r9),
  378. offsetof(struct pt_regs, r10),
  379. offsetof(struct pt_regs, r11),
  380. offsetof(struct pt_regs, r12),
  381. offsetof(struct pt_regs, r13),
  382. offsetof(struct pt_regs, r14),
  383. offsetof(struct pt_regs, r15),
  384. #endif
  385. };
  386. int nr_registers = ARRAY_SIZE(regoff);
  387. /*
  388. * Don't possibly decode a 32-bit instructions as
  389. * reading a 64-bit-only register.
  390. */
  391. if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
  392. nr_registers -= 8;
  393. switch (type) {
  394. case REG_TYPE_RM:
  395. regno = X86_MODRM_RM(insn->modrm.value);
  396. /*
  397. * ModRM.mod == 0 and ModRM.rm == 5 means a 32-bit displacement
  398. * follows the ModRM byte.
  399. */
  400. if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
  401. return -EDOM;
  402. if (X86_REX_B(insn->rex_prefix.value))
  403. regno += 8;
  404. break;
  405. case REG_TYPE_INDEX:
  406. regno = X86_SIB_INDEX(insn->sib.value);
  407. if (X86_REX_X(insn->rex_prefix.value))
  408. regno += 8;
  409. /*
  410. * If ModRM.mod != 3 and SIB.index = 4 the scale*index
  411. * portion of the address computation is null. This is
  412. * true only if REX.X is 0. In such a case, the SIB index
  413. * is used in the address computation.
  414. */
  415. if (X86_MODRM_MOD(insn->modrm.value) != 3 && regno == 4)
  416. return -EDOM;
  417. break;
  418. case REG_TYPE_BASE:
  419. regno = X86_SIB_BASE(insn->sib.value);
  420. /*
  421. * If ModRM.mod is 0 and SIB.base == 5, the base of the
  422. * register-indirect addressing is 0. In this case, a
  423. * 32-bit displacement follows the SIB byte.
  424. */
  425. if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
  426. return -EDOM;
  427. if (X86_REX_B(insn->rex_prefix.value))
  428. regno += 8;
  429. break;
  430. default:
  431. pr_err_ratelimited("invalid register type: %d\n", type);
  432. return -EINVAL;
  433. }
  434. if (regno >= nr_registers) {
  435. WARN_ONCE(1, "decoded an instruction with an invalid register");
  436. return -EINVAL;
  437. }
  438. return regoff[regno];
  439. }
  440. /**
  441. * get_reg_offset_16() - Obtain offset of register indicated by instruction
  442. * @insn: Instruction containing ModRM byte
  443. * @regs: Register values as seen when entering kernel mode
  444. * @offs1: Offset of the first operand register
  445. * @offs2: Offset of the second opeand register, if applicable
  446. *
  447. * Obtain the offset, in pt_regs, of the registers indicated by the ModRM byte
  448. * in @insn. This function is to be used with 16-bit address encodings. The
  449. * @offs1 and @offs2 will be written with the offset of the two registers
  450. * indicated by the instruction. In cases where any of the registers is not
  451. * referenced by the instruction, the value will be set to -EDOM.
  452. *
  453. * Returns:
  454. *
  455. * 0 on success, -EINVAL on error.
  456. */
  457. static int get_reg_offset_16(struct insn *insn, struct pt_regs *regs,
  458. int *offs1, int *offs2)
  459. {
  460. /*
  461. * 16-bit addressing can use one or two registers. Specifics of
  462. * encodings are given in Table 2-1. "16-Bit Addressing Forms with the
  463. * ModR/M Byte" of the Intel Software Development Manual.
  464. */
  465. static const int regoff1[] = {
  466. offsetof(struct pt_regs, bx),
  467. offsetof(struct pt_regs, bx),
  468. offsetof(struct pt_regs, bp),
  469. offsetof(struct pt_regs, bp),
  470. offsetof(struct pt_regs, si),
  471. offsetof(struct pt_regs, di),
  472. offsetof(struct pt_regs, bp),
  473. offsetof(struct pt_regs, bx),
  474. };
  475. static const int regoff2[] = {
  476. offsetof(struct pt_regs, si),
  477. offsetof(struct pt_regs, di),
  478. offsetof(struct pt_regs, si),
  479. offsetof(struct pt_regs, di),
  480. -EDOM,
  481. -EDOM,
  482. -EDOM,
  483. -EDOM,
  484. };
  485. if (!offs1 || !offs2)
  486. return -EINVAL;
  487. /* Operand is a register, use the generic function. */
  488. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  489. *offs1 = insn_get_modrm_rm_off(insn, regs);
  490. *offs2 = -EDOM;
  491. return 0;
  492. }
  493. *offs1 = regoff1[X86_MODRM_RM(insn->modrm.value)];
  494. *offs2 = regoff2[X86_MODRM_RM(insn->modrm.value)];
  495. /*
  496. * If ModRM.mod is 0 and ModRM.rm is 110b, then we use displacement-
  497. * only addressing. This means that no registers are involved in
  498. * computing the effective address. Thus, ensure that the first
  499. * register offset is invalild. The second register offset is already
  500. * invalid under the aforementioned conditions.
  501. */
  502. if ((X86_MODRM_MOD(insn->modrm.value) == 0) &&
  503. (X86_MODRM_RM(insn->modrm.value) == 6))
  504. *offs1 = -EDOM;
  505. return 0;
  506. }
  507. /**
  508. * get_desc() - Obtain contents of a segment descriptor
  509. * @out: Segment descriptor contents on success
  510. * @sel: Segment selector
  511. *
  512. * Given a segment selector, obtain a pointer to the segment descriptor.
  513. * Both global and local descriptor tables are supported.
  514. *
  515. * Returns:
  516. *
  517. * True on success, false on failure.
  518. *
  519. * NULL on error.
  520. */
  521. static bool get_desc(struct desc_struct *out, unsigned short sel)
  522. {
  523. struct desc_ptr gdt_desc = {0, 0};
  524. unsigned long desc_base;
  525. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  526. if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  527. bool success = false;
  528. struct ldt_struct *ldt;
  529. /* Bits [15:3] contain the index of the desired entry. */
  530. sel >>= 3;
  531. mutex_lock(&current->active_mm->context.lock);
  532. ldt = current->active_mm->context.ldt;
  533. if (ldt && sel < ldt->nr_entries) {
  534. *out = ldt->entries[sel];
  535. success = true;
  536. }
  537. mutex_unlock(&current->active_mm->context.lock);
  538. return success;
  539. }
  540. #endif
  541. native_store_gdt(&gdt_desc);
  542. /*
  543. * Segment descriptors have a size of 8 bytes. Thus, the index is
  544. * multiplied by 8 to obtain the memory offset of the desired descriptor
  545. * from the base of the GDT. As bits [15:3] of the segment selector
  546. * contain the index, it can be regarded as multiplied by 8 already.
  547. * All that remains is to clear bits [2:0].
  548. */
  549. desc_base = sel & ~(SEGMENT_RPL_MASK | SEGMENT_TI_MASK);
  550. if (desc_base > gdt_desc.size)
  551. return false;
  552. *out = *(struct desc_struct *)(gdt_desc.address + desc_base);
  553. return true;
  554. }
  555. /**
  556. * insn_get_seg_base() - Obtain base address of segment descriptor.
  557. * @regs: Register values as seen when entering kernel mode
  558. * @seg_reg_idx: Index of the segment register pointing to seg descriptor
  559. *
  560. * Obtain the base address of the segment as indicated by the segment descriptor
  561. * pointed by the segment selector. The segment selector is obtained from the
  562. * input segment register index @seg_reg_idx.
  563. *
  564. * Returns:
  565. *
  566. * In protected mode, base address of the segment. Zero in long mode,
  567. * except when FS or GS are used. In virtual-8086 mode, the segment
  568. * selector shifted 4 bits to the right.
  569. *
  570. * -1L in case of error.
  571. */
  572. unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
  573. {
  574. struct desc_struct desc;
  575. short sel;
  576. sel = get_segment_selector(regs, seg_reg_idx);
  577. if (sel < 0)
  578. return -1L;
  579. if (v8086_mode(regs))
  580. /*
  581. * Base is simply the segment selector shifted 4
  582. * bits to the right.
  583. */
  584. return (unsigned long)(sel << 4);
  585. if (user_64bit_mode(regs)) {
  586. /*
  587. * Only FS or GS will have a base address, the rest of
  588. * the segments' bases are forced to 0.
  589. */
  590. unsigned long base;
  591. if (seg_reg_idx == INAT_SEG_REG_FS)
  592. rdmsrl(MSR_FS_BASE, base);
  593. else if (seg_reg_idx == INAT_SEG_REG_GS)
  594. /*
  595. * swapgs was called at the kernel entry point. Thus,
  596. * MSR_KERNEL_GS_BASE will have the user-space GS base.
  597. */
  598. rdmsrl(MSR_KERNEL_GS_BASE, base);
  599. else
  600. base = 0;
  601. return base;
  602. }
  603. /* In protected mode the segment selector cannot be null. */
  604. if (!sel)
  605. return -1L;
  606. if (!get_desc(&desc, sel))
  607. return -1L;
  608. return get_desc_base(&desc);
  609. }
  610. /**
  611. * get_seg_limit() - Obtain the limit of a segment descriptor
  612. * @regs: Register values as seen when entering kernel mode
  613. * @seg_reg_idx: Index of the segment register pointing to seg descriptor
  614. *
  615. * Obtain the limit of the segment as indicated by the segment descriptor
  616. * pointed by the segment selector. The segment selector is obtained from the
  617. * input segment register index @seg_reg_idx.
  618. *
  619. * Returns:
  620. *
  621. * In protected mode, the limit of the segment descriptor in bytes.
  622. * In long mode and virtual-8086 mode, segment limits are not enforced. Thus,
  623. * limit is returned as -1L to imply a limit-less segment.
  624. *
  625. * Zero is returned on error.
  626. */
  627. static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx)
  628. {
  629. struct desc_struct desc;
  630. unsigned long limit;
  631. short sel;
  632. sel = get_segment_selector(regs, seg_reg_idx);
  633. if (sel < 0)
  634. return 0;
  635. if (user_64bit_mode(regs) || v8086_mode(regs))
  636. return -1L;
  637. if (!sel)
  638. return 0;
  639. if (!get_desc(&desc, sel))
  640. return 0;
  641. /*
  642. * If the granularity bit is set, the limit is given in multiples
  643. * of 4096. This also means that the 12 least significant bits are
  644. * not tested when checking the segment limits. In practice,
  645. * this means that the segment ends in (limit << 12) + 0xfff.
  646. */
  647. limit = get_desc_limit(&desc);
  648. if (desc.g)
  649. limit = (limit << 12) + 0xfff;
  650. return limit;
  651. }
  652. /**
  653. * insn_get_code_seg_params() - Obtain code segment parameters
  654. * @regs: Structure with register values as seen when entering kernel mode
  655. *
  656. * Obtain address and operand sizes of the code segment. It is obtained from the
  657. * selector contained in the CS register in regs. In protected mode, the default
  658. * address is determined by inspecting the L and D bits of the segment
  659. * descriptor. In virtual-8086 mode, the default is always two bytes for both
  660. * address and operand sizes.
  661. *
  662. * Returns:
  663. *
  664. * An int containing ORed-in default parameters on success.
  665. *
  666. * -EINVAL on error.
  667. */
  668. int insn_get_code_seg_params(struct pt_regs *regs)
  669. {
  670. struct desc_struct desc;
  671. short sel;
  672. if (v8086_mode(regs))
  673. /* Address and operand size are both 16-bit. */
  674. return INSN_CODE_SEG_PARAMS(2, 2);
  675. sel = get_segment_selector(regs, INAT_SEG_REG_CS);
  676. if (sel < 0)
  677. return sel;
  678. if (!get_desc(&desc, sel))
  679. return -EINVAL;
  680. /*
  681. * The most significant byte of the Type field of the segment descriptor
  682. * determines whether a segment contains data or code. If this is a data
  683. * segment, return error.
  684. */
  685. if (!(desc.type & BIT(3)))
  686. return -EINVAL;
  687. switch ((desc.l << 1) | desc.d) {
  688. case 0: /*
  689. * Legacy mode. CS.L=0, CS.D=0. Address and operand size are
  690. * both 16-bit.
  691. */
  692. return INSN_CODE_SEG_PARAMS(2, 2);
  693. case 1: /*
  694. * Legacy mode. CS.L=0, CS.D=1. Address and operand size are
  695. * both 32-bit.
  696. */
  697. return INSN_CODE_SEG_PARAMS(4, 4);
  698. case 2: /*
  699. * IA-32e 64-bit mode. CS.L=1, CS.D=0. Address size is 64-bit;
  700. * operand size is 32-bit.
  701. */
  702. return INSN_CODE_SEG_PARAMS(4, 8);
  703. case 3: /* Invalid setting. CS.L=1, CS.D=1 */
  704. /* fall through */
  705. default:
  706. return -EINVAL;
  707. }
  708. }
  709. /**
  710. * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte
  711. * @insn: Instruction containing the ModRM byte
  712. * @regs: Register values as seen when entering kernel mode
  713. *
  714. * Returns:
  715. *
  716. * The register indicated by the r/m part of the ModRM byte. The
  717. * register is obtained as an offset from the base of pt_regs. In specific
  718. * cases, the returned value can be -EDOM to indicate that the particular value
  719. * of ModRM does not refer to a register and shall be ignored.
  720. */
  721. int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)
  722. {
  723. return get_reg_offset(insn, regs, REG_TYPE_RM);
  724. }
  725. /**
  726. * get_seg_base_limit() - obtain base address and limit of a segment
  727. * @insn: Instruction. Must be valid.
  728. * @regs: Register values as seen when entering kernel mode
  729. * @regoff: Operand offset, in pt_regs, used to resolve segment descriptor
  730. * @base: Obtained segment base
  731. * @limit: Obtained segment limit
  732. *
  733. * Obtain the base address and limit of the segment associated with the operand
  734. * @regoff and, if any or allowed, override prefixes in @insn. This function is
  735. * different from insn_get_seg_base() as the latter does not resolve the segment
  736. * associated with the instruction operand. If a limit is not needed (e.g.,
  737. * when running in long mode), @limit can be NULL.
  738. *
  739. * Returns:
  740. *
  741. * 0 on success. @base and @limit will contain the base address and of the
  742. * resolved segment, respectively.
  743. *
  744. * -EINVAL on error.
  745. */
  746. static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
  747. int regoff, unsigned long *base,
  748. unsigned long *limit)
  749. {
  750. int seg_reg_idx;
  751. if (!base)
  752. return -EINVAL;
  753. seg_reg_idx = resolve_seg_reg(insn, regs, regoff);
  754. if (seg_reg_idx < 0)
  755. return seg_reg_idx;
  756. *base = insn_get_seg_base(regs, seg_reg_idx);
  757. if (*base == -1L)
  758. return -EINVAL;
  759. if (!limit)
  760. return 0;
  761. *limit = get_seg_limit(regs, seg_reg_idx);
  762. if (!(*limit))
  763. return -EINVAL;
  764. return 0;
  765. }
  766. /**
  767. * get_eff_addr_reg() - Obtain effective address from register operand
  768. * @insn: Instruction. Must be valid.
  769. * @regs: Register values as seen when entering kernel mode
  770. * @regoff: Obtained operand offset, in pt_regs, with the effective address
  771. * @eff_addr: Obtained effective address
  772. *
  773. * Obtain the effective address stored in the register operand as indicated by
  774. * the ModRM byte. This function is to be used only with register addressing
  775. * (i.e., ModRM.mod is 3). The effective address is saved in @eff_addr. The
  776. * register operand, as an offset from the base of pt_regs, is saved in @regoff;
  777. * such offset can then be used to resolve the segment associated with the
  778. * operand. This function can be used with any of the supported address sizes
  779. * in x86.
  780. *
  781. * Returns:
  782. *
  783. * 0 on success. @eff_addr will have the effective address stored in the
  784. * operand indicated by ModRM. @regoff will have such operand as an offset from
  785. * the base of pt_regs.
  786. *
  787. * -EINVAL on error.
  788. */
  789. static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
  790. int *regoff, long *eff_addr)
  791. {
  792. insn_get_modrm(insn);
  793. if (!insn->modrm.nbytes)
  794. return -EINVAL;
  795. if (X86_MODRM_MOD(insn->modrm.value) != 3)
  796. return -EINVAL;
  797. *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
  798. if (*regoff < 0)
  799. return -EINVAL;
  800. /* Ignore bytes that are outside the address size. */
  801. if (insn->addr_bytes == 2)
  802. *eff_addr = regs_get_register(regs, *regoff) & 0xffff;
  803. else if (insn->addr_bytes == 4)
  804. *eff_addr = regs_get_register(regs, *regoff) & 0xffffffff;
  805. else /* 64-bit address */
  806. *eff_addr = regs_get_register(regs, *regoff);
  807. return 0;
  808. }
  809. /**
  810. * get_eff_addr_modrm() - Obtain referenced effective address via ModRM
  811. * @insn: Instruction. Must be valid.
  812. * @regs: Register values as seen when entering kernel mode
  813. * @regoff: Obtained operand offset, in pt_regs, associated with segment
  814. * @eff_addr: Obtained effective address
  815. *
  816. * Obtain the effective address referenced by the ModRM byte of @insn. After
  817. * identifying the registers involved in the register-indirect memory reference,
  818. * its value is obtained from the operands in @regs. The computed address is
  819. * stored @eff_addr. Also, the register operand that indicates the associated
  820. * segment is stored in @regoff, this parameter can later be used to determine
  821. * such segment.
  822. *
  823. * Returns:
  824. *
  825. * 0 on success. @eff_addr will have the referenced effective address. @regoff
  826. * will have a register, as an offset from the base of pt_regs, that can be used
  827. * to resolve the associated segment.
  828. *
  829. * -EINVAL on error.
  830. */
  831. static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
  832. int *regoff, long *eff_addr)
  833. {
  834. long tmp;
  835. if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
  836. return -EINVAL;
  837. insn_get_modrm(insn);
  838. if (!insn->modrm.nbytes)
  839. return -EINVAL;
  840. if (X86_MODRM_MOD(insn->modrm.value) > 2)
  841. return -EINVAL;
  842. *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
  843. /*
  844. * -EDOM means that we must ignore the address_offset. In such a case,
  845. * in 64-bit mode the effective address relative to the rIP of the
  846. * following instruction.
  847. */
  848. if (*regoff == -EDOM) {
  849. if (user_64bit_mode(regs))
  850. tmp = regs->ip + insn->length;
  851. else
  852. tmp = 0;
  853. } else if (*regoff < 0) {
  854. return -EINVAL;
  855. } else {
  856. tmp = regs_get_register(regs, *regoff);
  857. }
  858. if (insn->addr_bytes == 4) {
  859. int addr32 = (int)(tmp & 0xffffffff) + insn->displacement.value;
  860. *eff_addr = addr32 & 0xffffffff;
  861. } else {
  862. *eff_addr = tmp + insn->displacement.value;
  863. }
  864. return 0;
  865. }
  866. /**
  867. * get_eff_addr_modrm_16() - Obtain referenced effective address via ModRM
  868. * @insn: Instruction. Must be valid.
  869. * @regs: Register values as seen when entering kernel mode
  870. * @regoff: Obtained operand offset, in pt_regs, associated with segment
  871. * @eff_addr: Obtained effective address
  872. *
  873. * Obtain the 16-bit effective address referenced by the ModRM byte of @insn.
  874. * After identifying the registers involved in the register-indirect memory
  875. * reference, its value is obtained from the operands in @regs. The computed
  876. * address is stored @eff_addr. Also, the register operand that indicates
  877. * the associated segment is stored in @regoff, this parameter can later be used
  878. * to determine such segment.
  879. *
  880. * Returns:
  881. *
  882. * 0 on success. @eff_addr will have the referenced effective address. @regoff
  883. * will have a register, as an offset from the base of pt_regs, that can be used
  884. * to resolve the associated segment.
  885. *
  886. * -EINVAL on error.
  887. */
  888. static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
  889. int *regoff, short *eff_addr)
  890. {
  891. int addr_offset1, addr_offset2, ret;
  892. short addr1 = 0, addr2 = 0, displacement;
  893. if (insn->addr_bytes != 2)
  894. return -EINVAL;
  895. insn_get_modrm(insn);
  896. if (!insn->modrm.nbytes)
  897. return -EINVAL;
  898. if (X86_MODRM_MOD(insn->modrm.value) > 2)
  899. return -EINVAL;
  900. ret = get_reg_offset_16(insn, regs, &addr_offset1, &addr_offset2);
  901. if (ret < 0)
  902. return -EINVAL;
  903. /*
  904. * Don't fail on invalid offset values. They might be invalid because
  905. * they cannot be used for this particular value of ModRM. Instead, use
  906. * them in the computation only if they contain a valid value.
  907. */
  908. if (addr_offset1 != -EDOM)
  909. addr1 = regs_get_register(regs, addr_offset1) & 0xffff;
  910. if (addr_offset2 != -EDOM)
  911. addr2 = regs_get_register(regs, addr_offset2) & 0xffff;
  912. displacement = insn->displacement.value & 0xffff;
  913. *eff_addr = addr1 + addr2 + displacement;
  914. /*
  915. * The first operand register could indicate to use of either SS or DS
  916. * registers to obtain the segment selector. The second operand
  917. * register can only indicate the use of DS. Thus, the first operand
  918. * will be used to obtain the segment selector.
  919. */
  920. *regoff = addr_offset1;
  921. return 0;
  922. }
  923. /**
  924. * get_eff_addr_sib() - Obtain referenced effective address via SIB
  925. * @insn: Instruction. Must be valid.
  926. * @regs: Register values as seen when entering kernel mode
  927. * @regoff: Obtained operand offset, in pt_regs, associated with segment
  928. * @eff_addr: Obtained effective address
  929. *
  930. * Obtain the effective address referenced by the SIB byte of @insn. After
  931. * identifying the registers involved in the indexed, register-indirect memory
  932. * reference, its value is obtained from the operands in @regs. The computed
  933. * address is stored @eff_addr. Also, the register operand that indicates the
  934. * associated segment is stored in @regoff, this parameter can later be used to
  935. * determine such segment.
  936. *
  937. * Returns:
  938. *
  939. * 0 on success. @eff_addr will have the referenced effective address.
  940. * @base_offset will have a register, as an offset from the base of pt_regs,
  941. * that can be used to resolve the associated segment.
  942. *
  943. * -EINVAL on error.
  944. */
  945. static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
  946. int *base_offset, long *eff_addr)
  947. {
  948. long base, indx;
  949. int indx_offset;
  950. if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
  951. return -EINVAL;
  952. insn_get_modrm(insn);
  953. if (!insn->modrm.nbytes)
  954. return -EINVAL;
  955. if (X86_MODRM_MOD(insn->modrm.value) > 2)
  956. return -EINVAL;
  957. insn_get_sib(insn);
  958. if (!insn->sib.nbytes)
  959. return -EINVAL;
  960. *base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
  961. indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
  962. /*
  963. * Negative values in the base and index offset means an error when
  964. * decoding the SIB byte. Except -EDOM, which means that the registers
  965. * should not be used in the address computation.
  966. */
  967. if (*base_offset == -EDOM)
  968. base = 0;
  969. else if (*base_offset < 0)
  970. return -EINVAL;
  971. else
  972. base = regs_get_register(regs, *base_offset);
  973. if (indx_offset == -EDOM)
  974. indx = 0;
  975. else if (indx_offset < 0)
  976. return -EINVAL;
  977. else
  978. indx = regs_get_register(regs, indx_offset);
  979. if (insn->addr_bytes == 4) {
  980. int addr32, base32, idx32;
  981. base32 = base & 0xffffffff;
  982. idx32 = indx & 0xffffffff;
  983. addr32 = base32 + idx32 * (1 << X86_SIB_SCALE(insn->sib.value));
  984. addr32 += insn->displacement.value;
  985. *eff_addr = addr32 & 0xffffffff;
  986. } else {
  987. *eff_addr = base + indx * (1 << X86_SIB_SCALE(insn->sib.value));
  988. *eff_addr += insn->displacement.value;
  989. }
  990. return 0;
  991. }
  992. /**
  993. * get_addr_ref_16() - Obtain the 16-bit address referred by instruction
  994. * @insn: Instruction containing ModRM byte and displacement
  995. * @regs: Register values as seen when entering kernel mode
  996. *
  997. * This function is to be used with 16-bit address encodings. Obtain the memory
  998. * address referred by the instruction's ModRM and displacement bytes. Also, the
  999. * segment used as base is determined by either any segment override prefixes in
  1000. * @insn or the default segment of the registers involved in the address
  1001. * computation. In protected mode, segment limits are enforced.
  1002. *
  1003. * Returns:
  1004. *
  1005. * Linear address referenced by the instruction operands on success.
  1006. *
  1007. * -1L on error.
  1008. */
  1009. static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
  1010. {
  1011. unsigned long linear_addr = -1L, seg_base, seg_limit;
  1012. int ret, regoff;
  1013. short eff_addr;
  1014. long tmp;
  1015. insn_get_modrm(insn);
  1016. insn_get_displacement(insn);
  1017. if (insn->addr_bytes != 2)
  1018. goto out;
  1019. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  1020. ret = get_eff_addr_reg(insn, regs, &regoff, &tmp);
  1021. if (ret)
  1022. goto out;
  1023. eff_addr = tmp;
  1024. } else {
  1025. ret = get_eff_addr_modrm_16(insn, regs, &regoff, &eff_addr);
  1026. if (ret)
  1027. goto out;
  1028. }
  1029. ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
  1030. if (ret)
  1031. goto out;
  1032. /*
  1033. * Before computing the linear address, make sure the effective address
  1034. * is within the limits of the segment. In virtual-8086 mode, segment
  1035. * limits are not enforced. In such a case, the segment limit is -1L to
  1036. * reflect this fact.
  1037. */
  1038. if ((unsigned long)(eff_addr & 0xffff) > seg_limit)
  1039. goto out;
  1040. linear_addr = (unsigned long)(eff_addr & 0xffff) + seg_base;
  1041. /* Limit linear address to 20 bits */
  1042. if (v8086_mode(regs))
  1043. linear_addr &= 0xfffff;
  1044. out:
  1045. return (void __user *)linear_addr;
  1046. }
  1047. /**
  1048. * get_addr_ref_32() - Obtain a 32-bit linear address
  1049. * @insn: Instruction with ModRM, SIB bytes and displacement
  1050. * @regs: Register values as seen when entering kernel mode
  1051. *
  1052. * This function is to be used with 32-bit address encodings to obtain the
  1053. * linear memory address referred by the instruction's ModRM, SIB,
  1054. * displacement bytes and segment base address, as applicable. If in protected
  1055. * mode, segment limits are enforced.
  1056. *
  1057. * Returns:
  1058. *
  1059. * Linear address referenced by instruction and registers on success.
  1060. *
  1061. * -1L on error.
  1062. */
  1063. static void __user *get_addr_ref_32(struct insn *insn, struct pt_regs *regs)
  1064. {
  1065. unsigned long linear_addr = -1L, seg_base, seg_limit;
  1066. int eff_addr, regoff;
  1067. long tmp;
  1068. int ret;
  1069. if (insn->addr_bytes != 4)
  1070. goto out;
  1071. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  1072. ret = get_eff_addr_reg(insn, regs, &regoff, &tmp);
  1073. if (ret)
  1074. goto out;
  1075. eff_addr = tmp;
  1076. } else {
  1077. if (insn->sib.nbytes) {
  1078. ret = get_eff_addr_sib(insn, regs, &regoff, &tmp);
  1079. if (ret)
  1080. goto out;
  1081. eff_addr = tmp;
  1082. } else {
  1083. ret = get_eff_addr_modrm(insn, regs, &regoff, &tmp);
  1084. if (ret)
  1085. goto out;
  1086. eff_addr = tmp;
  1087. }
  1088. }
  1089. ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
  1090. if (ret)
  1091. goto out;
  1092. /*
  1093. * In protected mode, before computing the linear address, make sure
  1094. * the effective address is within the limits of the segment.
  1095. * 32-bit addresses can be used in long and virtual-8086 modes if an
  1096. * address override prefix is used. In such cases, segment limits are
  1097. * not enforced. When in virtual-8086 mode, the segment limit is -1L
  1098. * to reflect this situation.
  1099. *
  1100. * After computed, the effective address is treated as an unsigned
  1101. * quantity.
  1102. */
  1103. if (!user_64bit_mode(regs) && ((unsigned int)eff_addr > seg_limit))
  1104. goto out;
  1105. /*
  1106. * Even though 32-bit address encodings are allowed in virtual-8086
  1107. * mode, the address range is still limited to [0x-0xffff].
  1108. */
  1109. if (v8086_mode(regs) && (eff_addr & ~0xffff))
  1110. goto out;
  1111. /*
  1112. * Data type long could be 64 bits in size. Ensure that our 32-bit
  1113. * effective address is not sign-extended when computing the linear
  1114. * address.
  1115. */
  1116. linear_addr = (unsigned long)(eff_addr & 0xffffffff) + seg_base;
  1117. /* Limit linear address to 20 bits */
  1118. if (v8086_mode(regs))
  1119. linear_addr &= 0xfffff;
  1120. out:
  1121. return (void __user *)linear_addr;
  1122. }
  1123. /**
  1124. * get_addr_ref_64() - Obtain a 64-bit linear address
  1125. * @insn: Instruction struct with ModRM and SIB bytes and displacement
  1126. * @regs: Structure with register values as seen when entering kernel mode
  1127. *
  1128. * This function is to be used with 64-bit address encodings to obtain the
  1129. * linear memory address referred by the instruction's ModRM, SIB,
  1130. * displacement bytes and segment base address, as applicable.
  1131. *
  1132. * Returns:
  1133. *
  1134. * Linear address referenced by instruction and registers on success.
  1135. *
  1136. * -1L on error.
  1137. */
  1138. #ifndef CONFIG_X86_64
  1139. static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
  1140. {
  1141. return (void __user *)-1L;
  1142. }
  1143. #else
  1144. static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
  1145. {
  1146. unsigned long linear_addr = -1L, seg_base;
  1147. int regoff, ret;
  1148. long eff_addr;
  1149. if (insn->addr_bytes != 8)
  1150. goto out;
  1151. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  1152. ret = get_eff_addr_reg(insn, regs, &regoff, &eff_addr);
  1153. if (ret)
  1154. goto out;
  1155. } else {
  1156. if (insn->sib.nbytes) {
  1157. ret = get_eff_addr_sib(insn, regs, &regoff, &eff_addr);
  1158. if (ret)
  1159. goto out;
  1160. } else {
  1161. ret = get_eff_addr_modrm(insn, regs, &regoff, &eff_addr);
  1162. if (ret)
  1163. goto out;
  1164. }
  1165. }
  1166. ret = get_seg_base_limit(insn, regs, regoff, &seg_base, NULL);
  1167. if (ret)
  1168. goto out;
  1169. linear_addr = (unsigned long)eff_addr + seg_base;
  1170. out:
  1171. return (void __user *)linear_addr;
  1172. }
  1173. #endif /* CONFIG_X86_64 */
  1174. /**
  1175. * insn_get_addr_ref() - Obtain the linear address referred by instruction
  1176. * @insn: Instruction structure containing ModRM byte and displacement
  1177. * @regs: Structure with register values as seen when entering kernel mode
  1178. *
  1179. * Obtain the linear address referred by the instruction's ModRM, SIB and
  1180. * displacement bytes, and segment base, as applicable. In protected mode,
  1181. * segment limits are enforced.
  1182. *
  1183. * Returns:
  1184. *
  1185. * Linear address referenced by instruction and registers on success.
  1186. *
  1187. * -1L on error.
  1188. */
  1189. void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
  1190. {
  1191. if (!insn || !regs)
  1192. return (void __user *)-1L;
  1193. switch (insn->addr_bytes) {
  1194. case 2:
  1195. return get_addr_ref_16(insn, regs);
  1196. case 4:
  1197. return get_addr_ref_32(insn, regs);
  1198. case 8:
  1199. return get_addr_ref_64(insn, regs);
  1200. default:
  1201. return (void __user *)-1L;
  1202. }
  1203. }