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