insn-eval.c 38 KB

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