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