insn-eval.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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. /*
  396. * ModRM.mod == 0 and ModRM.rm == 5 means a 32-bit displacement
  397. * follows the ModRM byte.
  398. */
  399. if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
  400. return -EDOM;
  401. if (X86_REX_B(insn->rex_prefix.value))
  402. regno += 8;
  403. break;
  404. case REG_TYPE_INDEX:
  405. regno = X86_SIB_INDEX(insn->sib.value);
  406. if (X86_REX_X(insn->rex_prefix.value))
  407. regno += 8;
  408. /*
  409. * If ModRM.mod != 3 and SIB.index = 4 the scale*index
  410. * portion of the address computation is null. This is
  411. * true only if REX.X is 0. In such a case, the SIB index
  412. * is used in the address computation.
  413. */
  414. if (X86_MODRM_MOD(insn->modrm.value) != 3 && regno == 4)
  415. return -EDOM;
  416. break;
  417. case REG_TYPE_BASE:
  418. regno = X86_SIB_BASE(insn->sib.value);
  419. /*
  420. * If ModRM.mod is 0 and SIB.base == 5, the base of the
  421. * register-indirect addressing is 0. In this case, a
  422. * 32-bit displacement follows the SIB byte.
  423. */
  424. if (!X86_MODRM_MOD(insn->modrm.value) && regno == 5)
  425. return -EDOM;
  426. if (X86_REX_B(insn->rex_prefix.value))
  427. regno += 8;
  428. break;
  429. default:
  430. pr_err_ratelimited("invalid register type: %d\n", type);
  431. return -EINVAL;
  432. }
  433. if (regno >= nr_registers) {
  434. WARN_ONCE(1, "decoded an instruction with an invalid register");
  435. return -EINVAL;
  436. }
  437. return regoff[regno];
  438. }
  439. /**
  440. * get_reg_offset_16() - Obtain offset of register indicated by instruction
  441. * @insn: Instruction containing ModRM byte
  442. * @regs: Register values as seen when entering kernel mode
  443. * @offs1: Offset of the first operand register
  444. * @offs2: Offset of the second opeand register, if applicable
  445. *
  446. * Obtain the offset, in pt_regs, of the registers indicated by the ModRM byte
  447. * in @insn. This function is to be used with 16-bit address encodings. The
  448. * @offs1 and @offs2 will be written with the offset of the two registers
  449. * indicated by the instruction. In cases where any of the registers is not
  450. * referenced by the instruction, the value will be set to -EDOM.
  451. *
  452. * Returns:
  453. *
  454. * 0 on success, -EINVAL on error.
  455. */
  456. static int get_reg_offset_16(struct insn *insn, struct pt_regs *regs,
  457. int *offs1, int *offs2)
  458. {
  459. /*
  460. * 16-bit addressing can use one or two registers. Specifics of
  461. * encodings are given in Table 2-1. "16-Bit Addressing Forms with the
  462. * ModR/M Byte" of the Intel Software Development Manual.
  463. */
  464. static const int regoff1[] = {
  465. offsetof(struct pt_regs, bx),
  466. offsetof(struct pt_regs, bx),
  467. offsetof(struct pt_regs, bp),
  468. offsetof(struct pt_regs, bp),
  469. offsetof(struct pt_regs, si),
  470. offsetof(struct pt_regs, di),
  471. offsetof(struct pt_regs, bp),
  472. offsetof(struct pt_regs, bx),
  473. };
  474. static const int regoff2[] = {
  475. offsetof(struct pt_regs, si),
  476. offsetof(struct pt_regs, di),
  477. offsetof(struct pt_regs, si),
  478. offsetof(struct pt_regs, di),
  479. -EDOM,
  480. -EDOM,
  481. -EDOM,
  482. -EDOM,
  483. };
  484. if (!offs1 || !offs2)
  485. return -EINVAL;
  486. /* Operand is a register, use the generic function. */
  487. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  488. *offs1 = insn_get_modrm_rm_off(insn, regs);
  489. *offs2 = -EDOM;
  490. return 0;
  491. }
  492. *offs1 = regoff1[X86_MODRM_RM(insn->modrm.value)];
  493. *offs2 = regoff2[X86_MODRM_RM(insn->modrm.value)];
  494. /*
  495. * If ModRM.mod is 0 and ModRM.rm is 110b, then we use displacement-
  496. * only addressing. This means that no registers are involved in
  497. * computing the effective address. Thus, ensure that the first
  498. * register offset is invalild. The second register offset is already
  499. * invalid under the aforementioned conditions.
  500. */
  501. if ((X86_MODRM_MOD(insn->modrm.value) == 0) &&
  502. (X86_MODRM_RM(insn->modrm.value) == 6))
  503. *offs1 = -EDOM;
  504. return 0;
  505. }
  506. /**
  507. * get_desc() - Obtain contents of a segment descriptor
  508. * @out: Segment descriptor contents on success
  509. * @sel: Segment selector
  510. *
  511. * Given a segment selector, obtain a pointer to the segment descriptor.
  512. * Both global and local descriptor tables are supported.
  513. *
  514. * Returns:
  515. *
  516. * True on success, false on failure.
  517. *
  518. * NULL on error.
  519. */
  520. static bool get_desc(struct desc_struct *out, unsigned short sel)
  521. {
  522. struct desc_ptr gdt_desc = {0, 0};
  523. unsigned long desc_base;
  524. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  525. if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  526. bool success = false;
  527. struct ldt_struct *ldt;
  528. /* Bits [15:3] contain the index of the desired entry. */
  529. sel >>= 3;
  530. mutex_lock(&current->active_mm->context.lock);
  531. ldt = current->active_mm->context.ldt;
  532. if (ldt && sel < ldt->nr_entries) {
  533. *out = ldt->entries[sel];
  534. success = true;
  535. }
  536. mutex_unlock(&current->active_mm->context.lock);
  537. return success;
  538. }
  539. #endif
  540. native_store_gdt(&gdt_desc);
  541. /*
  542. * Segment descriptors have a size of 8 bytes. Thus, the index is
  543. * multiplied by 8 to obtain the memory offset of the desired descriptor
  544. * from the base of the GDT. As bits [15:3] of the segment selector
  545. * contain the index, it can be regarded as multiplied by 8 already.
  546. * All that remains is to clear bits [2:0].
  547. */
  548. desc_base = sel & ~(SEGMENT_RPL_MASK | SEGMENT_TI_MASK);
  549. if (desc_base > gdt_desc.size)
  550. return false;
  551. *out = *(struct desc_struct *)(gdt_desc.address + desc_base);
  552. return true;
  553. }
  554. /**
  555. * insn_get_seg_base() - Obtain base address of segment descriptor.
  556. * @regs: Register values as seen when entering kernel mode
  557. * @seg_reg_idx: Index of the segment register pointing to seg descriptor
  558. *
  559. * Obtain the base address of the segment as indicated by the segment descriptor
  560. * pointed by the segment selector. The segment selector is obtained from the
  561. * input segment register index @seg_reg_idx.
  562. *
  563. * Returns:
  564. *
  565. * In protected mode, base address of the segment. Zero in long mode,
  566. * except when FS or GS are used. In virtual-8086 mode, the segment
  567. * selector shifted 4 bits to the right.
  568. *
  569. * -1L in case of error.
  570. */
  571. unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
  572. {
  573. struct desc_struct desc;
  574. short sel;
  575. sel = get_segment_selector(regs, seg_reg_idx);
  576. if (sel < 0)
  577. return -1L;
  578. if (v8086_mode(regs))
  579. /*
  580. * Base is simply the segment selector shifted 4
  581. * bits to the right.
  582. */
  583. return (unsigned long)(sel << 4);
  584. if (user_64bit_mode(regs)) {
  585. /*
  586. * Only FS or GS will have a base address, the rest of
  587. * the segments' bases are forced to 0.
  588. */
  589. unsigned long base;
  590. if (seg_reg_idx == INAT_SEG_REG_FS)
  591. rdmsrl(MSR_FS_BASE, base);
  592. else if (seg_reg_idx == INAT_SEG_REG_GS)
  593. /*
  594. * swapgs was called at the kernel entry point. Thus,
  595. * MSR_KERNEL_GS_BASE will have the user-space GS base.
  596. */
  597. rdmsrl(MSR_KERNEL_GS_BASE, base);
  598. else
  599. base = 0;
  600. return base;
  601. }
  602. /* In protected mode the segment selector cannot be null. */
  603. if (!sel)
  604. return -1L;
  605. if (!get_desc(&desc, sel))
  606. return -1L;
  607. return get_desc_base(&desc);
  608. }
  609. /**
  610. * get_seg_limit() - Obtain the limit of a segment descriptor
  611. * @regs: Register values as seen when entering kernel mode
  612. * @seg_reg_idx: Index of the segment register pointing to seg descriptor
  613. *
  614. * Obtain the limit of the segment as indicated by the segment descriptor
  615. * pointed by the segment selector. The segment selector is obtained from the
  616. * input segment register index @seg_reg_idx.
  617. *
  618. * Returns:
  619. *
  620. * In protected mode, the limit of the segment descriptor in bytes.
  621. * In long mode and virtual-8086 mode, segment limits are not enforced. Thus,
  622. * limit is returned as -1L to imply a limit-less segment.
  623. *
  624. * Zero is returned on error.
  625. */
  626. static unsigned long get_seg_limit(struct pt_regs *regs, int seg_reg_idx)
  627. {
  628. struct desc_struct desc;
  629. unsigned long limit;
  630. short sel;
  631. sel = get_segment_selector(regs, seg_reg_idx);
  632. if (sel < 0)
  633. return 0;
  634. if (user_64bit_mode(regs) || v8086_mode(regs))
  635. return -1L;
  636. if (!sel)
  637. return 0;
  638. if (!get_desc(&desc, sel))
  639. return 0;
  640. /*
  641. * If the granularity bit is set, the limit is given in multiples
  642. * of 4096. This also means that the 12 least significant bits are
  643. * not tested when checking the segment limits. In practice,
  644. * this means that the segment ends in (limit << 12) + 0xfff.
  645. */
  646. limit = get_desc_limit(&desc);
  647. if (desc.g)
  648. limit = (limit << 12) + 0xfff;
  649. return limit;
  650. }
  651. /**
  652. * insn_get_code_seg_params() - Obtain code segment parameters
  653. * @regs: Structure with register values as seen when entering kernel mode
  654. *
  655. * Obtain address and operand sizes of the code segment. It is obtained from the
  656. * selector contained in the CS register in regs. In protected mode, the default
  657. * address is determined by inspecting the L and D bits of the segment
  658. * descriptor. In virtual-8086 mode, the default is always two bytes for both
  659. * address and operand sizes.
  660. *
  661. * Returns:
  662. *
  663. * An int containing ORed-in default parameters on success.
  664. *
  665. * -EINVAL on error.
  666. */
  667. int insn_get_code_seg_params(struct pt_regs *regs)
  668. {
  669. struct desc_struct desc;
  670. short sel;
  671. if (v8086_mode(regs))
  672. /* Address and operand size are both 16-bit. */
  673. return INSN_CODE_SEG_PARAMS(2, 2);
  674. sel = get_segment_selector(regs, INAT_SEG_REG_CS);
  675. if (sel < 0)
  676. return sel;
  677. if (!get_desc(&desc, sel))
  678. return -EINVAL;
  679. /*
  680. * The most significant byte of the Type field of the segment descriptor
  681. * determines whether a segment contains data or code. If this is a data
  682. * segment, return error.
  683. */
  684. if (!(desc.type & BIT(3)))
  685. return -EINVAL;
  686. switch ((desc.l << 1) | desc.d) {
  687. case 0: /*
  688. * Legacy mode. CS.L=0, CS.D=0. Address and operand size are
  689. * both 16-bit.
  690. */
  691. return INSN_CODE_SEG_PARAMS(2, 2);
  692. case 1: /*
  693. * Legacy mode. CS.L=0, CS.D=1. Address and operand size are
  694. * both 32-bit.
  695. */
  696. return INSN_CODE_SEG_PARAMS(4, 4);
  697. case 2: /*
  698. * IA-32e 64-bit mode. CS.L=1, CS.D=0. Address size is 64-bit;
  699. * operand size is 32-bit.
  700. */
  701. return INSN_CODE_SEG_PARAMS(4, 8);
  702. case 3: /* Invalid setting. CS.L=1, CS.D=1 */
  703. /* fall through */
  704. default:
  705. return -EINVAL;
  706. }
  707. }
  708. /**
  709. * insn_get_modrm_rm_off() - Obtain register in r/m part of the ModRM byte
  710. * @insn: Instruction containing the ModRM byte
  711. * @regs: Register values as seen when entering kernel mode
  712. *
  713. * Returns:
  714. *
  715. * The register indicated by the r/m part of the ModRM byte. The
  716. * register is obtained as an offset from the base of pt_regs. In specific
  717. * cases, the returned value can be -EDOM to indicate that the particular value
  718. * of ModRM does not refer to a register and shall be ignored.
  719. */
  720. int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)
  721. {
  722. return get_reg_offset(insn, regs, REG_TYPE_RM);
  723. }
  724. /**
  725. * get_seg_base_limit() - obtain base address and limit of a segment
  726. * @insn: Instruction. Must be valid.
  727. * @regs: Register values as seen when entering kernel mode
  728. * @regoff: Operand offset, in pt_regs, used to resolve segment descriptor
  729. * @base: Obtained segment base
  730. * @limit: Obtained segment limit
  731. *
  732. * Obtain the base address and limit of the segment associated with the operand
  733. * @regoff and, if any or allowed, override prefixes in @insn. This function is
  734. * different from insn_get_seg_base() as the latter does not resolve the segment
  735. * associated with the instruction operand. If a limit is not needed (e.g.,
  736. * when running in long mode), @limit can be NULL.
  737. *
  738. * Returns:
  739. *
  740. * 0 on success. @base and @limit will contain the base address and of the
  741. * resolved segment, respectively.
  742. *
  743. * -EINVAL on error.
  744. */
  745. static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
  746. int regoff, unsigned long *base,
  747. unsigned long *limit)
  748. {
  749. int seg_reg_idx;
  750. if (!base)
  751. return -EINVAL;
  752. seg_reg_idx = resolve_seg_reg(insn, regs, regoff);
  753. if (seg_reg_idx < 0)
  754. return seg_reg_idx;
  755. *base = insn_get_seg_base(regs, seg_reg_idx);
  756. if (*base == -1L)
  757. return -EINVAL;
  758. if (!limit)
  759. return 0;
  760. *limit = get_seg_limit(regs, seg_reg_idx);
  761. if (!(*limit))
  762. return -EINVAL;
  763. return 0;
  764. }
  765. /**
  766. * get_eff_addr_reg() - Obtain effective address from register operand
  767. * @insn: Instruction. Must be valid.
  768. * @regs: Register values as seen when entering kernel mode
  769. * @regoff: Obtained operand offset, in pt_regs, with the effective address
  770. * @eff_addr: Obtained effective address
  771. *
  772. * Obtain the effective address stored in the register operand as indicated by
  773. * the ModRM byte. This function is to be used only with register addressing
  774. * (i.e., ModRM.mod is 3). The effective address is saved in @eff_addr. The
  775. * register operand, as an offset from the base of pt_regs, is saved in @regoff;
  776. * such offset can then be used to resolve the segment associated with the
  777. * operand. This function can be used with any of the supported address sizes
  778. * in x86.
  779. *
  780. * Returns:
  781. *
  782. * 0 on success. @eff_addr will have the effective address stored in the
  783. * operand indicated by ModRM. @regoff will have such operand as an offset from
  784. * the base of pt_regs.
  785. *
  786. * -EINVAL on error.
  787. */
  788. static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
  789. int *regoff, long *eff_addr)
  790. {
  791. insn_get_modrm(insn);
  792. if (!insn->modrm.nbytes)
  793. return -EINVAL;
  794. if (X86_MODRM_MOD(insn->modrm.value) != 3)
  795. return -EINVAL;
  796. *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
  797. if (*regoff < 0)
  798. return -EINVAL;
  799. /* Ignore bytes that are outside the address size. */
  800. if (insn->addr_bytes == 2)
  801. *eff_addr = regs_get_register(regs, *regoff) & 0xffff;
  802. else if (insn->addr_bytes == 4)
  803. *eff_addr = regs_get_register(regs, *regoff) & 0xffffffff;
  804. else /* 64-bit address */
  805. *eff_addr = regs_get_register(regs, *regoff);
  806. return 0;
  807. }
  808. /**
  809. * get_eff_addr_modrm() - Obtain referenced effective address via ModRM
  810. * @insn: Instruction. Must be valid.
  811. * @regs: Register values as seen when entering kernel mode
  812. * @regoff: Obtained operand offset, in pt_regs, associated with segment
  813. * @eff_addr: Obtained effective address
  814. *
  815. * Obtain the effective address referenced by the ModRM byte of @insn. After
  816. * identifying the registers involved in the register-indirect memory reference,
  817. * its value is obtained from the operands in @regs. The computed address is
  818. * stored @eff_addr. Also, the register operand that indicates the associated
  819. * segment is stored in @regoff, this parameter can later be used to determine
  820. * such segment.
  821. *
  822. * Returns:
  823. *
  824. * 0 on success. @eff_addr will have the referenced effective address. @regoff
  825. * will have a register, as an offset from the base of pt_regs, that can be used
  826. * to resolve the associated segment.
  827. *
  828. * -EINVAL on error.
  829. */
  830. static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
  831. int *regoff, long *eff_addr)
  832. {
  833. long tmp;
  834. if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
  835. return -EINVAL;
  836. insn_get_modrm(insn);
  837. if (!insn->modrm.nbytes)
  838. return -EINVAL;
  839. if (X86_MODRM_MOD(insn->modrm.value) > 2)
  840. return -EINVAL;
  841. *regoff = get_reg_offset(insn, regs, REG_TYPE_RM);
  842. /*
  843. * -EDOM means that we must ignore the address_offset. In such a case,
  844. * in 64-bit mode the effective address relative to the rIP of the
  845. * following instruction.
  846. */
  847. if (*regoff == -EDOM) {
  848. if (user_64bit_mode(regs))
  849. tmp = regs->ip + insn->length;
  850. else
  851. tmp = 0;
  852. } else if (*regoff < 0) {
  853. return -EINVAL;
  854. } else {
  855. tmp = regs_get_register(regs, *regoff);
  856. }
  857. if (insn->addr_bytes == 4) {
  858. int addr32 = (int)(tmp & 0xffffffff) + insn->displacement.value;
  859. *eff_addr = addr32 & 0xffffffff;
  860. } else {
  861. *eff_addr = tmp + insn->displacement.value;
  862. }
  863. return 0;
  864. }
  865. /**
  866. * get_eff_addr_modrm_16() - Obtain referenced effective address via ModRM
  867. * @insn: Instruction. Must be valid.
  868. * @regs: Register values as seen when entering kernel mode
  869. * @regoff: Obtained operand offset, in pt_regs, associated with segment
  870. * @eff_addr: Obtained effective address
  871. *
  872. * Obtain the 16-bit effective address referenced by the ModRM byte of @insn.
  873. * After identifying the registers involved in the register-indirect memory
  874. * reference, its value is obtained from the operands in @regs. The computed
  875. * address is stored @eff_addr. Also, the register operand that indicates
  876. * the associated segment is stored in @regoff, this parameter can later be used
  877. * to determine such segment.
  878. *
  879. * Returns:
  880. *
  881. * 0 on success. @eff_addr will have the referenced effective address. @regoff
  882. * will have a register, as an offset from the base of pt_regs, that can be used
  883. * to resolve the associated segment.
  884. *
  885. * -EINVAL on error.
  886. */
  887. static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
  888. int *regoff, short *eff_addr)
  889. {
  890. int addr_offset1, addr_offset2, ret;
  891. short addr1 = 0, addr2 = 0, displacement;
  892. if (insn->addr_bytes != 2)
  893. return -EINVAL;
  894. insn_get_modrm(insn);
  895. if (!insn->modrm.nbytes)
  896. return -EINVAL;
  897. if (X86_MODRM_MOD(insn->modrm.value) > 2)
  898. return -EINVAL;
  899. ret = get_reg_offset_16(insn, regs, &addr_offset1, &addr_offset2);
  900. if (ret < 0)
  901. return -EINVAL;
  902. /*
  903. * Don't fail on invalid offset values. They might be invalid because
  904. * they cannot be used for this particular value of ModRM. Instead, use
  905. * them in the computation only if they contain a valid value.
  906. */
  907. if (addr_offset1 != -EDOM)
  908. addr1 = regs_get_register(regs, addr_offset1) & 0xffff;
  909. if (addr_offset2 != -EDOM)
  910. addr2 = regs_get_register(regs, addr_offset2) & 0xffff;
  911. displacement = insn->displacement.value & 0xffff;
  912. *eff_addr = addr1 + addr2 + displacement;
  913. /*
  914. * The first operand register could indicate to use of either SS or DS
  915. * registers to obtain the segment selector. The second operand
  916. * register can only indicate the use of DS. Thus, the first operand
  917. * will be used to obtain the segment selector.
  918. */
  919. *regoff = addr_offset1;
  920. return 0;
  921. }
  922. /**
  923. * get_eff_addr_sib() - Obtain referenced effective address via SIB
  924. * @insn: Instruction. Must be valid.
  925. * @regs: Register values as seen when entering kernel mode
  926. * @regoff: Obtained operand offset, in pt_regs, associated with segment
  927. * @eff_addr: Obtained effective address
  928. *
  929. * Obtain the effective address referenced by the SIB byte of @insn. After
  930. * identifying the registers involved in the indexed, register-indirect memory
  931. * reference, its value is obtained from the operands in @regs. The computed
  932. * address is stored @eff_addr. Also, the register operand that indicates the
  933. * associated segment is stored in @regoff, this parameter can later be used to
  934. * determine such segment.
  935. *
  936. * Returns:
  937. *
  938. * 0 on success. @eff_addr will have the referenced effective address.
  939. * @base_offset will have a register, as an offset from the base of pt_regs,
  940. * that can be used to resolve the associated segment.
  941. *
  942. * -EINVAL on error.
  943. */
  944. static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
  945. int *base_offset, long *eff_addr)
  946. {
  947. long base, indx;
  948. int indx_offset;
  949. if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
  950. return -EINVAL;
  951. insn_get_modrm(insn);
  952. if (!insn->modrm.nbytes)
  953. return -EINVAL;
  954. if (X86_MODRM_MOD(insn->modrm.value) > 2)
  955. return -EINVAL;
  956. insn_get_sib(insn);
  957. if (!insn->sib.nbytes)
  958. return -EINVAL;
  959. *base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
  960. indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
  961. /*
  962. * Negative values in the base and index offset means an error when
  963. * decoding the SIB byte. Except -EDOM, which means that the registers
  964. * should not be used in the address computation.
  965. */
  966. if (*base_offset == -EDOM)
  967. base = 0;
  968. else if (*base_offset < 0)
  969. return -EINVAL;
  970. else
  971. base = regs_get_register(regs, *base_offset);
  972. if (indx_offset == -EDOM)
  973. indx = 0;
  974. else if (indx_offset < 0)
  975. return -EINVAL;
  976. else
  977. indx = regs_get_register(regs, indx_offset);
  978. if (insn->addr_bytes == 4) {
  979. int addr32, base32, idx32;
  980. base32 = base & 0xffffffff;
  981. idx32 = indx & 0xffffffff;
  982. addr32 = base32 + idx32 * (1 << X86_SIB_SCALE(insn->sib.value));
  983. addr32 += insn->displacement.value;
  984. *eff_addr = addr32 & 0xffffffff;
  985. } else {
  986. *eff_addr = base + indx * (1 << X86_SIB_SCALE(insn->sib.value));
  987. *eff_addr += insn->displacement.value;
  988. }
  989. return 0;
  990. }
  991. /**
  992. * get_addr_ref_16() - Obtain the 16-bit address referred by instruction
  993. * @insn: Instruction containing ModRM byte and displacement
  994. * @regs: Register values as seen when entering kernel mode
  995. *
  996. * This function is to be used with 16-bit address encodings. Obtain the memory
  997. * address referred by the instruction's ModRM and displacement bytes. Also, the
  998. * segment used as base is determined by either any segment override prefixes in
  999. * @insn or the default segment of the registers involved in the address
  1000. * computation. In protected mode, segment limits are enforced.
  1001. *
  1002. * Returns:
  1003. *
  1004. * Linear address referenced by the instruction operands on success.
  1005. *
  1006. * -1L on error.
  1007. */
  1008. static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
  1009. {
  1010. unsigned long linear_addr = -1L, seg_base, seg_limit;
  1011. int ret, regoff;
  1012. short eff_addr;
  1013. long tmp;
  1014. insn_get_modrm(insn);
  1015. insn_get_displacement(insn);
  1016. if (insn->addr_bytes != 2)
  1017. goto out;
  1018. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  1019. ret = get_eff_addr_reg(insn, regs, &regoff, &tmp);
  1020. if (ret)
  1021. goto out;
  1022. eff_addr = tmp;
  1023. } else {
  1024. ret = get_eff_addr_modrm_16(insn, regs, &regoff, &eff_addr);
  1025. if (ret)
  1026. goto out;
  1027. }
  1028. ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
  1029. if (ret)
  1030. goto out;
  1031. /*
  1032. * Before computing the linear address, make sure the effective address
  1033. * is within the limits of the segment. In virtual-8086 mode, segment
  1034. * limits are not enforced. In such a case, the segment limit is -1L to
  1035. * reflect this fact.
  1036. */
  1037. if ((unsigned long)(eff_addr & 0xffff) > seg_limit)
  1038. goto out;
  1039. linear_addr = (unsigned long)(eff_addr & 0xffff) + seg_base;
  1040. /* Limit linear address to 20 bits */
  1041. if (v8086_mode(regs))
  1042. linear_addr &= 0xfffff;
  1043. out:
  1044. return (void __user *)linear_addr;
  1045. }
  1046. /**
  1047. * get_addr_ref_32() - Obtain a 32-bit linear address
  1048. * @insn: Instruction with ModRM, SIB bytes and displacement
  1049. * @regs: Register values as seen when entering kernel mode
  1050. *
  1051. * This function is to be used with 32-bit address encodings to obtain the
  1052. * linear memory address referred by the instruction's ModRM, SIB,
  1053. * displacement bytes and segment base address, as applicable. If in protected
  1054. * mode, segment limits are enforced.
  1055. *
  1056. * Returns:
  1057. *
  1058. * Linear address referenced by instruction and registers on success.
  1059. *
  1060. * -1L on error.
  1061. */
  1062. static void __user *get_addr_ref_32(struct insn *insn, struct pt_regs *regs)
  1063. {
  1064. unsigned long linear_addr = -1L, seg_base, seg_limit;
  1065. int eff_addr, regoff;
  1066. long tmp;
  1067. int ret;
  1068. if (insn->addr_bytes != 4)
  1069. goto out;
  1070. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  1071. ret = get_eff_addr_reg(insn, regs, &regoff, &tmp);
  1072. if (ret)
  1073. goto out;
  1074. eff_addr = tmp;
  1075. } else {
  1076. if (insn->sib.nbytes) {
  1077. ret = get_eff_addr_sib(insn, regs, &regoff, &tmp);
  1078. if (ret)
  1079. goto out;
  1080. eff_addr = tmp;
  1081. } else {
  1082. ret = get_eff_addr_modrm(insn, regs, &regoff, &tmp);
  1083. if (ret)
  1084. goto out;
  1085. eff_addr = tmp;
  1086. }
  1087. }
  1088. ret = get_seg_base_limit(insn, regs, regoff, &seg_base, &seg_limit);
  1089. if (ret)
  1090. goto out;
  1091. /*
  1092. * In protected mode, before computing the linear address, make sure
  1093. * the effective address is within the limits of the segment.
  1094. * 32-bit addresses can be used in long and virtual-8086 modes if an
  1095. * address override prefix is used. In such cases, segment limits are
  1096. * not enforced. When in virtual-8086 mode, the segment limit is -1L
  1097. * to reflect this situation.
  1098. *
  1099. * After computed, the effective address is treated as an unsigned
  1100. * quantity.
  1101. */
  1102. if (!user_64bit_mode(regs) && ((unsigned int)eff_addr > seg_limit))
  1103. goto out;
  1104. /*
  1105. * Even though 32-bit address encodings are allowed in virtual-8086
  1106. * mode, the address range is still limited to [0x-0xffff].
  1107. */
  1108. if (v8086_mode(regs) && (eff_addr & ~0xffff))
  1109. goto out;
  1110. /*
  1111. * Data type long could be 64 bits in size. Ensure that our 32-bit
  1112. * effective address is not sign-extended when computing the linear
  1113. * address.
  1114. */
  1115. linear_addr = (unsigned long)(eff_addr & 0xffffffff) + seg_base;
  1116. /* Limit linear address to 20 bits */
  1117. if (v8086_mode(regs))
  1118. linear_addr &= 0xfffff;
  1119. out:
  1120. return (void __user *)linear_addr;
  1121. }
  1122. /**
  1123. * get_addr_ref_64() - Obtain a 64-bit linear address
  1124. * @insn: Instruction struct with ModRM and SIB bytes and displacement
  1125. * @regs: Structure with register values as seen when entering kernel mode
  1126. *
  1127. * This function is to be used with 64-bit address encodings to obtain the
  1128. * linear memory address referred by the instruction's ModRM, SIB,
  1129. * displacement bytes and segment base address, as applicable.
  1130. *
  1131. * Returns:
  1132. *
  1133. * Linear address referenced by instruction and registers on success.
  1134. *
  1135. * -1L on error.
  1136. */
  1137. #ifndef CONFIG_X86_64
  1138. static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
  1139. {
  1140. return (void __user *)-1L;
  1141. }
  1142. #else
  1143. static void __user *get_addr_ref_64(struct insn *insn, struct pt_regs *regs)
  1144. {
  1145. unsigned long linear_addr = -1L, seg_base;
  1146. int regoff, ret;
  1147. long eff_addr;
  1148. if (insn->addr_bytes != 8)
  1149. goto out;
  1150. if (X86_MODRM_MOD(insn->modrm.value) == 3) {
  1151. ret = get_eff_addr_reg(insn, regs, &regoff, &eff_addr);
  1152. if (ret)
  1153. goto out;
  1154. } else {
  1155. if (insn->sib.nbytes) {
  1156. ret = get_eff_addr_sib(insn, regs, &regoff, &eff_addr);
  1157. if (ret)
  1158. goto out;
  1159. } else {
  1160. ret = get_eff_addr_modrm(insn, regs, &regoff, &eff_addr);
  1161. if (ret)
  1162. goto out;
  1163. }
  1164. }
  1165. ret = get_seg_base_limit(insn, regs, regoff, &seg_base, NULL);
  1166. if (ret)
  1167. goto out;
  1168. linear_addr = (unsigned long)eff_addr + seg_base;
  1169. out:
  1170. return (void __user *)linear_addr;
  1171. }
  1172. #endif /* CONFIG_X86_64 */
  1173. /**
  1174. * insn_get_addr_ref() - Obtain the linear address referred by instruction
  1175. * @insn: Instruction structure containing ModRM byte and displacement
  1176. * @regs: Structure with register values as seen when entering kernel mode
  1177. *
  1178. * Obtain the linear address referred by the instruction's ModRM, SIB and
  1179. * displacement bytes, and segment base, as applicable. In protected mode,
  1180. * segment limits are enforced.
  1181. *
  1182. * Returns:
  1183. *
  1184. * Linear address referenced by instruction and registers on success.
  1185. *
  1186. * -1L on error.
  1187. */
  1188. void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)
  1189. {
  1190. if (!insn || !regs)
  1191. return (void __user *)-1L;
  1192. switch (insn->addr_bytes) {
  1193. case 2:
  1194. return get_addr_ref_16(insn, regs);
  1195. case 4:
  1196. return get_addr_ref_32(insn, regs);
  1197. case 8:
  1198. return get_addr_ref_64(insn, regs);
  1199. default:
  1200. return (void __user *)-1L;
  1201. }
  1202. }