• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3  * emulate.c
4  *
5  * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
6  *
7  * Copyright (c) 2005 Keir Fraser
8  *
9  * Linux coding style, mod r/m decoder, segment base fixes, real-mode
10  * privileged instructions:
11  *
12  * Copyright (C) 2006 Qumranet
13  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
14  *
15  *   Avi Kivity <avi@qumranet.com>
16  *   Yaniv Kamay <yaniv@qumranet.com>
17  *
18  * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
19  */
20 
21 #include <linux/kvm_host.h>
22 #include "kvm_cache_regs.h"
23 #include "kvm_emulate.h"
24 #include <linux/stringify.h>
25 #include <asm/debugreg.h>
26 #include <asm/nospec-branch.h>
27 
28 #include "x86.h"
29 #include "tss.h"
30 #include "mmu.h"
31 #include "pmu.h"
32 
33 /*
34  * Operand types
35  */
36 #define OpNone             0ull
37 #define OpImplicit         1ull  /* No generic decode */
38 #define OpReg              2ull  /* Register */
39 #define OpMem              3ull  /* Memory */
40 #define OpAcc              4ull  /* Accumulator: AL/AX/EAX/RAX */
41 #define OpDI               5ull  /* ES:DI/EDI/RDI */
42 #define OpMem64            6ull  /* Memory, 64-bit */
43 #define OpImmUByte         7ull  /* Zero-extended 8-bit immediate */
44 #define OpDX               8ull  /* DX register */
45 #define OpCL               9ull  /* CL register (for shifts) */
46 #define OpImmByte         10ull  /* 8-bit sign extended immediate */
47 #define OpOne             11ull  /* Implied 1 */
48 #define OpImm             12ull  /* Sign extended up to 32-bit immediate */
49 #define OpMem16           13ull  /* Memory operand (16-bit). */
50 #define OpMem32           14ull  /* Memory operand (32-bit). */
51 #define OpImmU            15ull  /* Immediate operand, zero extended */
52 #define OpSI              16ull  /* SI/ESI/RSI */
53 #define OpImmFAddr        17ull  /* Immediate far address */
54 #define OpMemFAddr        18ull  /* Far address in memory */
55 #define OpImmU16          19ull  /* Immediate operand, 16 bits, zero extended */
56 #define OpES              20ull  /* ES */
57 #define OpCS              21ull  /* CS */
58 #define OpSS              22ull  /* SS */
59 #define OpDS              23ull  /* DS */
60 #define OpFS              24ull  /* FS */
61 #define OpGS              25ull  /* GS */
62 #define OpMem8            26ull  /* 8-bit zero extended memory operand */
63 #define OpImm64           27ull  /* Sign extended 16/32/64-bit immediate */
64 #define OpXLat            28ull  /* memory at BX/EBX/RBX + zero-extended AL */
65 #define OpAccLo           29ull  /* Low part of extended acc (AX/AX/EAX/RAX) */
66 #define OpAccHi           30ull  /* High part of extended acc (-/DX/EDX/RDX) */
67 
68 #define OpBits             5  /* Width of operand field */
69 #define OpMask             ((1ull << OpBits) - 1)
70 
71 /*
72  * Opcode effective-address decode tables.
73  * Note that we only emulate instructions that have at least one memory
74  * operand (excluding implicit stack references). We assume that stack
75  * references and instruction fetches will never occur in special memory
76  * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
77  * not be handled.
78  */
79 
80 /* Operand sizes: 8-bit operands or specified/overridden size. */
81 #define ByteOp      (1<<0)	/* 8-bit operands. */
82 /* Destination operand type. */
83 #define DstShift    1
84 #define ImplicitOps (OpImplicit << DstShift)
85 #define DstReg      (OpReg << DstShift)
86 #define DstMem      (OpMem << DstShift)
87 #define DstAcc      (OpAcc << DstShift)
88 #define DstDI       (OpDI << DstShift)
89 #define DstMem64    (OpMem64 << DstShift)
90 #define DstMem16    (OpMem16 << DstShift)
91 #define DstImmUByte (OpImmUByte << DstShift)
92 #define DstDX       (OpDX << DstShift)
93 #define DstAccLo    (OpAccLo << DstShift)
94 #define DstMask     (OpMask << DstShift)
95 /* Source operand type. */
96 #define SrcShift    6
97 #define SrcNone     (OpNone << SrcShift)
98 #define SrcReg      (OpReg << SrcShift)
99 #define SrcMem      (OpMem << SrcShift)
100 #define SrcMem16    (OpMem16 << SrcShift)
101 #define SrcMem32    (OpMem32 << SrcShift)
102 #define SrcImm      (OpImm << SrcShift)
103 #define SrcImmByte  (OpImmByte << SrcShift)
104 #define SrcOne      (OpOne << SrcShift)
105 #define SrcImmUByte (OpImmUByte << SrcShift)
106 #define SrcImmU     (OpImmU << SrcShift)
107 #define SrcSI       (OpSI << SrcShift)
108 #define SrcXLat     (OpXLat << SrcShift)
109 #define SrcImmFAddr (OpImmFAddr << SrcShift)
110 #define SrcMemFAddr (OpMemFAddr << SrcShift)
111 #define SrcAcc      (OpAcc << SrcShift)
112 #define SrcImmU16   (OpImmU16 << SrcShift)
113 #define SrcImm64    (OpImm64 << SrcShift)
114 #define SrcDX       (OpDX << SrcShift)
115 #define SrcMem8     (OpMem8 << SrcShift)
116 #define SrcAccHi    (OpAccHi << SrcShift)
117 #define SrcMask     (OpMask << SrcShift)
118 #define BitOp       (1<<11)
119 #define MemAbs      (1<<12)      /* Memory operand is absolute displacement */
120 #define String      (1<<13)     /* String instruction (rep capable) */
121 #define Stack       (1<<14)     /* Stack instruction (push/pop) */
122 #define GroupMask   (7<<15)     /* Opcode uses one of the group mechanisms */
123 #define Group       (1<<15)     /* Bits 3:5 of modrm byte extend opcode */
124 #define GroupDual   (2<<15)     /* Alternate decoding of mod == 3 */
125 #define Prefix      (3<<15)     /* Instruction varies with 66/f2/f3 prefix */
126 #define RMExt       (4<<15)     /* Opcode extension in ModRM r/m if mod == 3 */
127 #define Escape      (5<<15)     /* Escape to coprocessor instruction */
128 #define InstrDual   (6<<15)     /* Alternate instruction decoding of mod == 3 */
129 #define ModeDual    (7<<15)     /* Different instruction for 32/64 bit */
130 #define Sse         (1<<18)     /* SSE Vector instruction */
131 /* Generic ModRM decode. */
132 #define ModRM       (1<<19)
133 /* Destination is only written; never read. */
134 #define Mov         (1<<20)
135 /* Misc flags */
136 #define Prot        (1<<21) /* instruction generates #UD if not in prot-mode */
137 #define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
138 #define NoAccess    (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
139 #define Op3264      (1<<24) /* Operand is 64b in long mode, 32b otherwise */
140 #define Undefined   (1<<25) /* No Such Instruction */
141 #define Lock        (1<<26) /* lock prefix is allowed for the instruction */
142 #define Priv        (1<<27) /* instruction generates #GP if current CPL != 0 */
143 #define No64	    (1<<28)
144 #define PageTable   (1 << 29)   /* instruction used to write page table */
145 #define NotImpl     (1 << 30)   /* instruction is not implemented */
146 /* Source 2 operand type */
147 #define Src2Shift   (31)
148 #define Src2None    (OpNone << Src2Shift)
149 #define Src2Mem     (OpMem << Src2Shift)
150 #define Src2CL      (OpCL << Src2Shift)
151 #define Src2ImmByte (OpImmByte << Src2Shift)
152 #define Src2One     (OpOne << Src2Shift)
153 #define Src2Imm     (OpImm << Src2Shift)
154 #define Src2ES      (OpES << Src2Shift)
155 #define Src2CS      (OpCS << Src2Shift)
156 #define Src2SS      (OpSS << Src2Shift)
157 #define Src2DS      (OpDS << Src2Shift)
158 #define Src2FS      (OpFS << Src2Shift)
159 #define Src2GS      (OpGS << Src2Shift)
160 #define Src2Mask    (OpMask << Src2Shift)
161 #define Mmx         ((u64)1 << 40)  /* MMX Vector instruction */
162 #define AlignMask   ((u64)7 << 41)
163 #define Aligned     ((u64)1 << 41)  /* Explicitly aligned (e.g. MOVDQA) */
164 #define Unaligned   ((u64)2 << 41)  /* Explicitly unaligned (e.g. MOVDQU) */
165 #define Avx         ((u64)3 << 41)  /* Advanced Vector Extensions */
166 #define Aligned16   ((u64)4 << 41)  /* Aligned to 16 byte boundary (e.g. FXSAVE) */
167 #define Fastop      ((u64)1 << 44)  /* Use opcode::u.fastop */
168 #define NoWrite     ((u64)1 << 45)  /* No writeback */
169 #define SrcWrite    ((u64)1 << 46)  /* Write back src operand */
170 #define NoMod	    ((u64)1 << 47)  /* Mod field is ignored */
171 #define Intercept   ((u64)1 << 48)  /* Has valid intercept field */
172 #define CheckPerm   ((u64)1 << 49)  /* Has valid check_perm field */
173 #define PrivUD      ((u64)1 << 51)  /* #UD instead of #GP on CPL > 0 */
174 #define NearBranch  ((u64)1 << 52)  /* Near branches */
175 #define No16	    ((u64)1 << 53)  /* No 16 bit operand */
176 #define IncSP       ((u64)1 << 54)  /* SP is incremented before ModRM calc */
177 #define TwoMemOp    ((u64)1 << 55)  /* Instruction has two memory operand */
178 
179 #define DstXacc     (DstAccLo | SrcAccHi | SrcWrite)
180 
181 #define X2(x...) x, x
182 #define X3(x...) X2(x), x
183 #define X4(x...) X2(x), X2(x)
184 #define X5(x...) X4(x), x
185 #define X6(x...) X4(x), X2(x)
186 #define X7(x...) X4(x), X3(x)
187 #define X8(x...) X4(x), X4(x)
188 #define X16(x...) X8(x), X8(x)
189 
190 struct opcode {
191 	u64 flags : 56;
192 	u64 intercept : 8;
193 	union {
194 		int (*execute)(struct x86_emulate_ctxt *ctxt);
195 		const struct opcode *group;
196 		const struct group_dual *gdual;
197 		const struct gprefix *gprefix;
198 		const struct escape *esc;
199 		const struct instr_dual *idual;
200 		const struct mode_dual *mdual;
201 		void (*fastop)(struct fastop *fake);
202 	} u;
203 	int (*check_perm)(struct x86_emulate_ctxt *ctxt);
204 };
205 
206 struct group_dual {
207 	struct opcode mod012[8];
208 	struct opcode mod3[8];
209 };
210 
211 struct gprefix {
212 	struct opcode pfx_no;
213 	struct opcode pfx_66;
214 	struct opcode pfx_f2;
215 	struct opcode pfx_f3;
216 };
217 
218 struct escape {
219 	struct opcode op[8];
220 	struct opcode high[64];
221 };
222 
223 struct instr_dual {
224 	struct opcode mod012;
225 	struct opcode mod3;
226 };
227 
228 struct mode_dual {
229 	struct opcode mode32;
230 	struct opcode mode64;
231 };
232 
233 #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
234 
235 enum x86_transfer_type {
236 	X86_TRANSFER_NONE,
237 	X86_TRANSFER_CALL_JMP,
238 	X86_TRANSFER_RET,
239 	X86_TRANSFER_TASK_SWITCH,
240 };
241 
reg_read(struct x86_emulate_ctxt * ctxt,unsigned nr)242 static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
243 {
244 	if (!(ctxt->regs_valid & (1 << nr))) {
245 		ctxt->regs_valid |= 1 << nr;
246 		ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
247 	}
248 	return ctxt->_regs[nr];
249 }
250 
reg_write(struct x86_emulate_ctxt * ctxt,unsigned nr)251 static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
252 {
253 	ctxt->regs_valid |= 1 << nr;
254 	ctxt->regs_dirty |= 1 << nr;
255 	return &ctxt->_regs[nr];
256 }
257 
reg_rmw(struct x86_emulate_ctxt * ctxt,unsigned nr)258 static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
259 {
260 	reg_read(ctxt, nr);
261 	return reg_write(ctxt, nr);
262 }
263 
writeback_registers(struct x86_emulate_ctxt * ctxt)264 static void writeback_registers(struct x86_emulate_ctxt *ctxt)
265 {
266 	unsigned reg;
267 
268 	for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
269 		ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
270 }
271 
invalidate_registers(struct x86_emulate_ctxt * ctxt)272 static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
273 {
274 	ctxt->regs_dirty = 0;
275 	ctxt->regs_valid = 0;
276 }
277 
278 /*
279  * These EFLAGS bits are restored from saved value during emulation, and
280  * any changes are written back to the saved value after emulation.
281  */
282 #define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
283 		     X86_EFLAGS_PF|X86_EFLAGS_CF)
284 
285 #ifdef CONFIG_X86_64
286 #define ON64(x) x
287 #else
288 #define ON64(x)
289 #endif
290 
291 /*
292  * fastop functions have a special calling convention:
293  *
294  * dst:    rax        (in/out)
295  * src:    rdx        (in/out)
296  * src2:   rcx        (in)
297  * flags:  rflags     (in/out)
298  * ex:     rsi        (in:fastop pointer, out:zero if exception)
299  *
300  * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
301  * different operand sizes can be reached by calculation, rather than a jump
302  * table (which would be bigger than the code).
303  *
304  * The 16 byte alignment, considering 5 bytes for the RET thunk, 3 for ENDBR
305  * and 1 for the straight line speculation INT3, leaves 7 bytes for the
306  * body of the function.  Currently none is larger than 4.
307  */
308 static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop);
309 
310 #define FASTOP_SIZE	16
311 
312 #define __FOP_FUNC(name) \
313 	".align " __stringify(FASTOP_SIZE) " \n\t" \
314 	".type " name ", @function \n\t" \
315 	name ":\n\t"
316 
317 #define FOP_FUNC(name) \
318 	__FOP_FUNC(#name)
319 
320 #define __FOP_RET(name) \
321 	ASM_RET \
322 	".size " name ", .-" name "\n\t"
323 
324 #define FOP_RET(name) \
325 	__FOP_RET(#name)
326 
327 #define __FOP_START(op, align) \
328 	extern void em_##op(struct fastop *fake); \
329 	asm(".pushsection .text, \"ax\" \n\t" \
330 	    ".global em_" #op " \n\t" \
331 	    ".align " __stringify(align) " \n\t" \
332 	    "em_" #op ":\n\t"
333 
334 #define FOP_START(op) __FOP_START(op, FASTOP_SIZE)
335 
336 #define FOP_END \
337 	    ".popsection")
338 
339 #define __FOPNOP(name) \
340 	__FOP_FUNC(name) \
341 	__FOP_RET(name)
342 
343 #define FOPNOP() \
344 	__FOPNOP(__stringify(__UNIQUE_ID(nop)))
345 
346 #define FOP1E(op,  dst) \
347 	__FOP_FUNC(#op "_" #dst) \
348 	"10: " #op " %" #dst " \n\t" \
349 	__FOP_RET(#op "_" #dst)
350 
351 #define FOP1EEX(op,  dst) \
352 	FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
353 
354 #define FASTOP1(op) \
355 	FOP_START(op) \
356 	FOP1E(op##b, al) \
357 	FOP1E(op##w, ax) \
358 	FOP1E(op##l, eax) \
359 	ON64(FOP1E(op##q, rax))	\
360 	FOP_END
361 
362 /* 1-operand, using src2 (for MUL/DIV r/m) */
363 #define FASTOP1SRC2(op, name) \
364 	FOP_START(name) \
365 	FOP1E(op, cl) \
366 	FOP1E(op, cx) \
367 	FOP1E(op, ecx) \
368 	ON64(FOP1E(op, rcx)) \
369 	FOP_END
370 
371 /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
372 #define FASTOP1SRC2EX(op, name) \
373 	FOP_START(name) \
374 	FOP1EEX(op, cl) \
375 	FOP1EEX(op, cx) \
376 	FOP1EEX(op, ecx) \
377 	ON64(FOP1EEX(op, rcx)) \
378 	FOP_END
379 
380 #define FOP2E(op,  dst, src)	   \
381 	__FOP_FUNC(#op "_" #dst "_" #src) \
382 	#op " %" #src ", %" #dst " \n\t" \
383 	__FOP_RET(#op "_" #dst "_" #src)
384 
385 #define FASTOP2(op) \
386 	FOP_START(op) \
387 	FOP2E(op##b, al, dl) \
388 	FOP2E(op##w, ax, dx) \
389 	FOP2E(op##l, eax, edx) \
390 	ON64(FOP2E(op##q, rax, rdx)) \
391 	FOP_END
392 
393 /* 2 operand, word only */
394 #define FASTOP2W(op) \
395 	FOP_START(op) \
396 	FOPNOP() \
397 	FOP2E(op##w, ax, dx) \
398 	FOP2E(op##l, eax, edx) \
399 	ON64(FOP2E(op##q, rax, rdx)) \
400 	FOP_END
401 
402 /* 2 operand, src is CL */
403 #define FASTOP2CL(op) \
404 	FOP_START(op) \
405 	FOP2E(op##b, al, cl) \
406 	FOP2E(op##w, ax, cl) \
407 	FOP2E(op##l, eax, cl) \
408 	ON64(FOP2E(op##q, rax, cl)) \
409 	FOP_END
410 
411 /* 2 operand, src and dest are reversed */
412 #define FASTOP2R(op, name) \
413 	FOP_START(name) \
414 	FOP2E(op##b, dl, al) \
415 	FOP2E(op##w, dx, ax) \
416 	FOP2E(op##l, edx, eax) \
417 	ON64(FOP2E(op##q, rdx, rax)) \
418 	FOP_END
419 
420 #define FOP3E(op,  dst, src, src2) \
421 	__FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
422 	#op " %" #src2 ", %" #src ", %" #dst " \n\t"\
423 	__FOP_RET(#op "_" #dst "_" #src "_" #src2)
424 
425 /* 3-operand, word-only, src2=cl */
426 #define FASTOP3WCL(op) \
427 	FOP_START(op) \
428 	FOPNOP() \
429 	FOP3E(op##w, ax, dx, cl) \
430 	FOP3E(op##l, eax, edx, cl) \
431 	ON64(FOP3E(op##q, rax, rdx, cl)) \
432 	FOP_END
433 
434 /* Special case for SETcc - 1 instruction per cc */
435 
436 /*
437  * Depending on .config the SETcc functions look like:
438  *
439  * SETcc %al			[3 bytes]
440  * RET | JMP __x86_return_thunk	[1,5 bytes; CONFIG_RETHUNK]
441  * INT3				[1 byte; CONFIG_SLS]
442  */
443 #define SETCC_ALIGN	16
444 
445 #define FOP_SETCC(op) \
446 	".align " __stringify(SETCC_ALIGN) " \n\t" \
447 	".type " #op ", @function \n\t" \
448 	#op ": \n\t" \
449 	#op " %al \n\t" \
450 	__FOP_RET(#op) \
451 	".skip " __stringify(SETCC_ALIGN) " - (.-" #op "), 0xcc \n\t"
452 
453 asm(".pushsection .fixup, \"ax\"\n"
454     "kvm_fastop_exception: xor %esi, %esi; " ASM_RET
455     ".popsection");
456 
457 __FOP_START(setcc, SETCC_ALIGN)
458 FOP_SETCC(seto)
459 FOP_SETCC(setno)
460 FOP_SETCC(setc)
461 FOP_SETCC(setnc)
462 FOP_SETCC(setz)
463 FOP_SETCC(setnz)
464 FOP_SETCC(setbe)
465 FOP_SETCC(setnbe)
466 FOP_SETCC(sets)
467 FOP_SETCC(setns)
468 FOP_SETCC(setp)
469 FOP_SETCC(setnp)
470 FOP_SETCC(setl)
471 FOP_SETCC(setnl)
472 FOP_SETCC(setle)
473 FOP_SETCC(setnle)
474 FOP_END;
475 
476 FOP_START(salc)
477 FOP_FUNC(salc)
478 "pushf; sbb %al, %al; popf \n\t"
479 FOP_RET(salc)
480 FOP_END;
481 
482 /*
483  * XXX: inoutclob user must know where the argument is being expanded.
484  *      Relying on CONFIG_CC_HAS_ASM_GOTO would allow us to remove _fault.
485  */
486 #define asm_safe(insn, inoutclob...) \
487 ({ \
488 	int _fault = 0; \
489  \
490 	asm volatile("1:" insn "\n" \
491 	             "2:\n" \
492 	             ".pushsection .fixup, \"ax\"\n" \
493 	             "3: movl $1, %[_fault]\n" \
494 	             "   jmp  2b\n" \
495 	             ".popsection\n" \
496 	             _ASM_EXTABLE(1b, 3b) \
497 	             : [_fault] "+qm"(_fault) inoutclob ); \
498  \
499 	_fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
500 })
501 
emulator_check_intercept(struct x86_emulate_ctxt * ctxt,enum x86_intercept intercept,enum x86_intercept_stage stage)502 static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
503 				    enum x86_intercept intercept,
504 				    enum x86_intercept_stage stage)
505 {
506 	struct x86_instruction_info info = {
507 		.intercept  = intercept,
508 		.rep_prefix = ctxt->rep_prefix,
509 		.modrm_mod  = ctxt->modrm_mod,
510 		.modrm_reg  = ctxt->modrm_reg,
511 		.modrm_rm   = ctxt->modrm_rm,
512 		.src_val    = ctxt->src.val64,
513 		.dst_val    = ctxt->dst.val64,
514 		.src_bytes  = ctxt->src.bytes,
515 		.dst_bytes  = ctxt->dst.bytes,
516 		.ad_bytes   = ctxt->ad_bytes,
517 		.next_rip   = ctxt->eip,
518 	};
519 
520 	return ctxt->ops->intercept(ctxt, &info, stage);
521 }
522 
assign_masked(ulong * dest,ulong src,ulong mask)523 static void assign_masked(ulong *dest, ulong src, ulong mask)
524 {
525 	*dest = (*dest & ~mask) | (src & mask);
526 }
527 
assign_register(unsigned long * reg,u64 val,int bytes)528 static void assign_register(unsigned long *reg, u64 val, int bytes)
529 {
530 	/* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
531 	switch (bytes) {
532 	case 1:
533 		*(u8 *)reg = (u8)val;
534 		break;
535 	case 2:
536 		*(u16 *)reg = (u16)val;
537 		break;
538 	case 4:
539 		*reg = (u32)val;
540 		break;	/* 64b: zero-extend */
541 	case 8:
542 		*reg = val;
543 		break;
544 	}
545 }
546 
ad_mask(struct x86_emulate_ctxt * ctxt)547 static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
548 {
549 	return (1UL << (ctxt->ad_bytes << 3)) - 1;
550 }
551 
stack_mask(struct x86_emulate_ctxt * ctxt)552 static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
553 {
554 	u16 sel;
555 	struct desc_struct ss;
556 
557 	if (ctxt->mode == X86EMUL_MODE_PROT64)
558 		return ~0UL;
559 	ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
560 	return ~0U >> ((ss.d ^ 1) * 16);  /* d=0: 0xffff; d=1: 0xffffffff */
561 }
562 
stack_size(struct x86_emulate_ctxt * ctxt)563 static int stack_size(struct x86_emulate_ctxt *ctxt)
564 {
565 	return (__fls(stack_mask(ctxt)) + 1) >> 3;
566 }
567 
568 /* Access/update address held in a register, based on addressing mode. */
569 static inline unsigned long
address_mask(struct x86_emulate_ctxt * ctxt,unsigned long reg)570 address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
571 {
572 	if (ctxt->ad_bytes == sizeof(unsigned long))
573 		return reg;
574 	else
575 		return reg & ad_mask(ctxt);
576 }
577 
578 static inline unsigned long
register_address(struct x86_emulate_ctxt * ctxt,int reg)579 register_address(struct x86_emulate_ctxt *ctxt, int reg)
580 {
581 	return address_mask(ctxt, reg_read(ctxt, reg));
582 }
583 
masked_increment(ulong * reg,ulong mask,int inc)584 static void masked_increment(ulong *reg, ulong mask, int inc)
585 {
586 	assign_masked(reg, *reg + inc, mask);
587 }
588 
589 static inline void
register_address_increment(struct x86_emulate_ctxt * ctxt,int reg,int inc)590 register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
591 {
592 	ulong *preg = reg_rmw(ctxt, reg);
593 
594 	assign_register(preg, *preg + inc, ctxt->ad_bytes);
595 }
596 
rsp_increment(struct x86_emulate_ctxt * ctxt,int inc)597 static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
598 {
599 	masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
600 }
601 
desc_limit_scaled(struct desc_struct * desc)602 static u32 desc_limit_scaled(struct desc_struct *desc)
603 {
604 	u32 limit = get_desc_limit(desc);
605 
606 	return desc->g ? (limit << 12) | 0xfff : limit;
607 }
608 
seg_base(struct x86_emulate_ctxt * ctxt,int seg)609 static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
610 {
611 	if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
612 		return 0;
613 
614 	return ctxt->ops->get_cached_segment_base(ctxt, seg);
615 }
616 
emulate_exception(struct x86_emulate_ctxt * ctxt,int vec,u32 error,bool valid)617 static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
618 			     u32 error, bool valid)
619 {
620 	WARN_ON(vec > 0x1f);
621 	ctxt->exception.vector = vec;
622 	ctxt->exception.error_code = error;
623 	ctxt->exception.error_code_valid = valid;
624 	return X86EMUL_PROPAGATE_FAULT;
625 }
626 
emulate_db(struct x86_emulate_ctxt * ctxt)627 static int emulate_db(struct x86_emulate_ctxt *ctxt)
628 {
629 	return emulate_exception(ctxt, DB_VECTOR, 0, false);
630 }
631 
emulate_gp(struct x86_emulate_ctxt * ctxt,int err)632 static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
633 {
634 	return emulate_exception(ctxt, GP_VECTOR, err, true);
635 }
636 
emulate_ss(struct x86_emulate_ctxt * ctxt,int err)637 static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
638 {
639 	return emulate_exception(ctxt, SS_VECTOR, err, true);
640 }
641 
emulate_ud(struct x86_emulate_ctxt * ctxt)642 static int emulate_ud(struct x86_emulate_ctxt *ctxt)
643 {
644 	return emulate_exception(ctxt, UD_VECTOR, 0, false);
645 }
646 
emulate_ts(struct x86_emulate_ctxt * ctxt,int err)647 static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
648 {
649 	return emulate_exception(ctxt, TS_VECTOR, err, true);
650 }
651 
emulate_de(struct x86_emulate_ctxt * ctxt)652 static int emulate_de(struct x86_emulate_ctxt *ctxt)
653 {
654 	return emulate_exception(ctxt, DE_VECTOR, 0, false);
655 }
656 
emulate_nm(struct x86_emulate_ctxt * ctxt)657 static int emulate_nm(struct x86_emulate_ctxt *ctxt)
658 {
659 	return emulate_exception(ctxt, NM_VECTOR, 0, false);
660 }
661 
get_segment_selector(struct x86_emulate_ctxt * ctxt,unsigned seg)662 static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
663 {
664 	u16 selector;
665 	struct desc_struct desc;
666 
667 	ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
668 	return selector;
669 }
670 
set_segment_selector(struct x86_emulate_ctxt * ctxt,u16 selector,unsigned seg)671 static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
672 				 unsigned seg)
673 {
674 	u16 dummy;
675 	u32 base3;
676 	struct desc_struct desc;
677 
678 	ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
679 	ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
680 }
681 
ctxt_virt_addr_bits(struct x86_emulate_ctxt * ctxt)682 static inline u8 ctxt_virt_addr_bits(struct x86_emulate_ctxt *ctxt)
683 {
684 	return (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_LA57) ? 57 : 48;
685 }
686 
emul_is_noncanonical_address(u64 la,struct x86_emulate_ctxt * ctxt)687 static inline bool emul_is_noncanonical_address(u64 la,
688 						struct x86_emulate_ctxt *ctxt)
689 {
690 	return !__is_canonical_address(la, ctxt_virt_addr_bits(ctxt));
691 }
692 
693 /*
694  * x86 defines three classes of vector instructions: explicitly
695  * aligned, explicitly unaligned, and the rest, which change behaviour
696  * depending on whether they're AVX encoded or not.
697  *
698  * Also included is CMPXCHG16B which is not a vector instruction, yet it is
699  * subject to the same check.  FXSAVE and FXRSTOR are checked here too as their
700  * 512 bytes of data must be aligned to a 16 byte boundary.
701  */
insn_alignment(struct x86_emulate_ctxt * ctxt,unsigned size)702 static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
703 {
704 	u64 alignment = ctxt->d & AlignMask;
705 
706 	if (likely(size < 16))
707 		return 1;
708 
709 	switch (alignment) {
710 	case Unaligned:
711 	case Avx:
712 		return 1;
713 	case Aligned16:
714 		return 16;
715 	case Aligned:
716 	default:
717 		return size;
718 	}
719 }
720 
__linearize(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,unsigned * max_size,unsigned size,bool write,bool fetch,enum x86emul_mode mode,ulong * linear)721 static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
722 				       struct segmented_address addr,
723 				       unsigned *max_size, unsigned size,
724 				       bool write, bool fetch,
725 				       enum x86emul_mode mode, ulong *linear)
726 {
727 	struct desc_struct desc;
728 	bool usable;
729 	ulong la;
730 	u32 lim;
731 	u16 sel;
732 	u8  va_bits;
733 
734 	la = seg_base(ctxt, addr.seg) + addr.ea;
735 	*max_size = 0;
736 	switch (mode) {
737 	case X86EMUL_MODE_PROT64:
738 		*linear = la;
739 		va_bits = ctxt_virt_addr_bits(ctxt);
740 		if (!__is_canonical_address(la, va_bits))
741 			goto bad;
742 
743 		*max_size = min_t(u64, ~0u, (1ull << va_bits) - la);
744 		if (size > *max_size)
745 			goto bad;
746 		break;
747 	default:
748 		*linear = la = (u32)la;
749 		usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
750 						addr.seg);
751 		if (!usable)
752 			goto bad;
753 		/* code segment in protected mode or read-only data segment */
754 		if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
755 					|| !(desc.type & 2)) && write)
756 			goto bad;
757 		/* unreadable code segment */
758 		if (!fetch && (desc.type & 8) && !(desc.type & 2))
759 			goto bad;
760 		lim = desc_limit_scaled(&desc);
761 		if (!(desc.type & 8) && (desc.type & 4)) {
762 			/* expand-down segment */
763 			if (addr.ea <= lim)
764 				goto bad;
765 			lim = desc.d ? 0xffffffff : 0xffff;
766 		}
767 		if (addr.ea > lim)
768 			goto bad;
769 		if (lim == 0xffffffff)
770 			*max_size = ~0u;
771 		else {
772 			*max_size = (u64)lim + 1 - addr.ea;
773 			if (size > *max_size)
774 				goto bad;
775 		}
776 		break;
777 	}
778 	if (la & (insn_alignment(ctxt, size) - 1))
779 		return emulate_gp(ctxt, 0);
780 	return X86EMUL_CONTINUE;
781 bad:
782 	if (addr.seg == VCPU_SREG_SS)
783 		return emulate_ss(ctxt, 0);
784 	else
785 		return emulate_gp(ctxt, 0);
786 }
787 
linearize(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,unsigned size,bool write,ulong * linear)788 static int linearize(struct x86_emulate_ctxt *ctxt,
789 		     struct segmented_address addr,
790 		     unsigned size, bool write,
791 		     ulong *linear)
792 {
793 	unsigned max_size;
794 	return __linearize(ctxt, addr, &max_size, size, write, false,
795 			   ctxt->mode, linear);
796 }
797 
assign_eip(struct x86_emulate_ctxt * ctxt,ulong dst)798 static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst)
799 {
800 	ulong linear;
801 	int rc;
802 	unsigned max_size;
803 	struct segmented_address addr = { .seg = VCPU_SREG_CS,
804 					   .ea = dst };
805 
806 	if (ctxt->op_bytes != sizeof(unsigned long))
807 		addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
808 	rc = __linearize(ctxt, addr, &max_size, 1, false, true, ctxt->mode, &linear);
809 	if (rc == X86EMUL_CONTINUE)
810 		ctxt->_eip = addr.ea;
811 	return rc;
812 }
813 
emulator_recalc_and_set_mode(struct x86_emulate_ctxt * ctxt)814 static inline int emulator_recalc_and_set_mode(struct x86_emulate_ctxt *ctxt)
815 {
816 	u64 efer;
817 	struct desc_struct cs;
818 	u16 selector;
819 	u32 base3;
820 
821 	ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
822 
823 	if (!(ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PE)) {
824 		/* Real mode. cpu must not have long mode active */
825 		if (efer & EFER_LMA)
826 			return X86EMUL_UNHANDLEABLE;
827 		ctxt->mode = X86EMUL_MODE_REAL;
828 		return X86EMUL_CONTINUE;
829 	}
830 
831 	if (ctxt->eflags & X86_EFLAGS_VM) {
832 		/* Protected/VM86 mode. cpu must not have long mode active */
833 		if (efer & EFER_LMA)
834 			return X86EMUL_UNHANDLEABLE;
835 		ctxt->mode = X86EMUL_MODE_VM86;
836 		return X86EMUL_CONTINUE;
837 	}
838 
839 	if (!ctxt->ops->get_segment(ctxt, &selector, &cs, &base3, VCPU_SREG_CS))
840 		return X86EMUL_UNHANDLEABLE;
841 
842 	if (efer & EFER_LMA) {
843 		if (cs.l) {
844 			/* Proper long mode */
845 			ctxt->mode = X86EMUL_MODE_PROT64;
846 		} else if (cs.d) {
847 			/* 32 bit compatibility mode*/
848 			ctxt->mode = X86EMUL_MODE_PROT32;
849 		} else {
850 			ctxt->mode = X86EMUL_MODE_PROT16;
851 		}
852 	} else {
853 		/* Legacy 32 bit / 16 bit mode */
854 		ctxt->mode = cs.d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
855 	}
856 
857 	return X86EMUL_CONTINUE;
858 }
859 
assign_eip_near(struct x86_emulate_ctxt * ctxt,ulong dst)860 static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
861 {
862 	return assign_eip(ctxt, dst);
863 }
864 
assign_eip_far(struct x86_emulate_ctxt * ctxt,ulong dst)865 static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst)
866 {
867 	int rc = emulator_recalc_and_set_mode(ctxt);
868 
869 	if (rc != X86EMUL_CONTINUE)
870 		return rc;
871 
872 	return assign_eip(ctxt, dst);
873 }
874 
jmp_rel(struct x86_emulate_ctxt * ctxt,int rel)875 static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
876 {
877 	return assign_eip_near(ctxt, ctxt->_eip + rel);
878 }
879 
linear_read_system(struct x86_emulate_ctxt * ctxt,ulong linear,void * data,unsigned size)880 static int linear_read_system(struct x86_emulate_ctxt *ctxt, ulong linear,
881 			      void *data, unsigned size)
882 {
883 	return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, true);
884 }
885 
linear_write_system(struct x86_emulate_ctxt * ctxt,ulong linear,void * data,unsigned int size)886 static int linear_write_system(struct x86_emulate_ctxt *ctxt,
887 			       ulong linear, void *data,
888 			       unsigned int size)
889 {
890 	return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, true);
891 }
892 
segmented_read_std(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,void * data,unsigned size)893 static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
894 			      struct segmented_address addr,
895 			      void *data,
896 			      unsigned size)
897 {
898 	int rc;
899 	ulong linear;
900 
901 	rc = linearize(ctxt, addr, size, false, &linear);
902 	if (rc != X86EMUL_CONTINUE)
903 		return rc;
904 	return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, false);
905 }
906 
segmented_write_std(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,void * data,unsigned int size)907 static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
908 			       struct segmented_address addr,
909 			       void *data,
910 			       unsigned int size)
911 {
912 	int rc;
913 	ulong linear;
914 
915 	rc = linearize(ctxt, addr, size, true, &linear);
916 	if (rc != X86EMUL_CONTINUE)
917 		return rc;
918 	return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, false);
919 }
920 
921 /*
922  * Prefetch the remaining bytes of the instruction without crossing page
923  * boundary if they are not in fetch_cache yet.
924  */
__do_insn_fetch_bytes(struct x86_emulate_ctxt * ctxt,int op_size)925 static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
926 {
927 	int rc;
928 	unsigned size, max_size;
929 	unsigned long linear;
930 	int cur_size = ctxt->fetch.end - ctxt->fetch.data;
931 	struct segmented_address addr = { .seg = VCPU_SREG_CS,
932 					   .ea = ctxt->eip + cur_size };
933 
934 	/*
935 	 * We do not know exactly how many bytes will be needed, and
936 	 * __linearize is expensive, so fetch as much as possible.  We
937 	 * just have to avoid going beyond the 15 byte limit, the end
938 	 * of the segment, or the end of the page.
939 	 *
940 	 * __linearize is called with size 0 so that it does not do any
941 	 * boundary check itself.  Instead, we use max_size to check
942 	 * against op_size.
943 	 */
944 	rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
945 			 &linear);
946 	if (unlikely(rc != X86EMUL_CONTINUE))
947 		return rc;
948 
949 	size = min_t(unsigned, 15UL ^ cur_size, max_size);
950 	size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
951 
952 	/*
953 	 * One instruction can only straddle two pages,
954 	 * and one has been loaded at the beginning of
955 	 * x86_decode_insn.  So, if not enough bytes
956 	 * still, we must have hit the 15-byte boundary.
957 	 */
958 	if (unlikely(size < op_size))
959 		return emulate_gp(ctxt, 0);
960 
961 	rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
962 			      size, &ctxt->exception);
963 	if (unlikely(rc != X86EMUL_CONTINUE))
964 		return rc;
965 	ctxt->fetch.end += size;
966 	return X86EMUL_CONTINUE;
967 }
968 
do_insn_fetch_bytes(struct x86_emulate_ctxt * ctxt,unsigned size)969 static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
970 					       unsigned size)
971 {
972 	unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
973 
974 	if (unlikely(done_size < size))
975 		return __do_insn_fetch_bytes(ctxt, size - done_size);
976 	else
977 		return X86EMUL_CONTINUE;
978 }
979 
980 /* Fetch next part of the instruction being emulated. */
981 #define insn_fetch(_type, _ctxt)					\
982 ({	_type _x;							\
983 									\
984 	rc = do_insn_fetch_bytes(_ctxt, sizeof(_type));			\
985 	if (rc != X86EMUL_CONTINUE)					\
986 		goto done;						\
987 	ctxt->_eip += sizeof(_type);					\
988 	memcpy(&_x, ctxt->fetch.ptr, sizeof(_type));			\
989 	ctxt->fetch.ptr += sizeof(_type);				\
990 	_x;								\
991 })
992 
993 #define insn_fetch_arr(_arr, _size, _ctxt)				\
994 ({									\
995 	rc = do_insn_fetch_bytes(_ctxt, _size);				\
996 	if (rc != X86EMUL_CONTINUE)					\
997 		goto done;						\
998 	ctxt->_eip += (_size);						\
999 	memcpy(_arr, ctxt->fetch.ptr, _size);				\
1000 	ctxt->fetch.ptr += (_size);					\
1001 })
1002 
1003 /*
1004  * Given the 'reg' portion of a ModRM byte, and a register block, return a
1005  * pointer into the block that addresses the relevant register.
1006  * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
1007  */
decode_register(struct x86_emulate_ctxt * ctxt,u8 modrm_reg,int byteop)1008 static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
1009 			     int byteop)
1010 {
1011 	void *p;
1012 	int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
1013 
1014 	if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
1015 		p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
1016 	else
1017 		p = reg_rmw(ctxt, modrm_reg);
1018 	return p;
1019 }
1020 
read_descriptor(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,u16 * size,unsigned long * address,int op_bytes)1021 static int read_descriptor(struct x86_emulate_ctxt *ctxt,
1022 			   struct segmented_address addr,
1023 			   u16 *size, unsigned long *address, int op_bytes)
1024 {
1025 	int rc;
1026 
1027 	if (op_bytes == 2)
1028 		op_bytes = 3;
1029 	*address = 0;
1030 	rc = segmented_read_std(ctxt, addr, size, 2);
1031 	if (rc != X86EMUL_CONTINUE)
1032 		return rc;
1033 	addr.ea += 2;
1034 	rc = segmented_read_std(ctxt, addr, address, op_bytes);
1035 	return rc;
1036 }
1037 
1038 FASTOP2(add);
1039 FASTOP2(or);
1040 FASTOP2(adc);
1041 FASTOP2(sbb);
1042 FASTOP2(and);
1043 FASTOP2(sub);
1044 FASTOP2(xor);
1045 FASTOP2(cmp);
1046 FASTOP2(test);
1047 
1048 FASTOP1SRC2(mul, mul_ex);
1049 FASTOP1SRC2(imul, imul_ex);
1050 FASTOP1SRC2EX(div, div_ex);
1051 FASTOP1SRC2EX(idiv, idiv_ex);
1052 
1053 FASTOP3WCL(shld);
1054 FASTOP3WCL(shrd);
1055 
1056 FASTOP2W(imul);
1057 
1058 FASTOP1(not);
1059 FASTOP1(neg);
1060 FASTOP1(inc);
1061 FASTOP1(dec);
1062 
1063 FASTOP2CL(rol);
1064 FASTOP2CL(ror);
1065 FASTOP2CL(rcl);
1066 FASTOP2CL(rcr);
1067 FASTOP2CL(shl);
1068 FASTOP2CL(shr);
1069 FASTOP2CL(sar);
1070 
1071 FASTOP2W(bsf);
1072 FASTOP2W(bsr);
1073 FASTOP2W(bt);
1074 FASTOP2W(bts);
1075 FASTOP2W(btr);
1076 FASTOP2W(btc);
1077 
1078 FASTOP2(xadd);
1079 
1080 FASTOP2R(cmp, cmp_r);
1081 
em_bsf_c(struct x86_emulate_ctxt * ctxt)1082 static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
1083 {
1084 	/* If src is zero, do not writeback, but update flags */
1085 	if (ctxt->src.val == 0)
1086 		ctxt->dst.type = OP_NONE;
1087 	return fastop(ctxt, em_bsf);
1088 }
1089 
em_bsr_c(struct x86_emulate_ctxt * ctxt)1090 static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
1091 {
1092 	/* If src is zero, do not writeback, but update flags */
1093 	if (ctxt->src.val == 0)
1094 		ctxt->dst.type = OP_NONE;
1095 	return fastop(ctxt, em_bsr);
1096 }
1097 
test_cc(unsigned int condition,unsigned long flags)1098 static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
1099 {
1100 	u8 rc;
1101 	void (*fop)(void) = (void *)em_setcc + SETCC_ALIGN * (condition & 0xf);
1102 
1103 	flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
1104 	asm("push %[flags]; popf; " CALL_NOSPEC
1105 	    : "=a"(rc) : [thunk_target]"r"(fop), [flags]"r"(flags));
1106 	return rc;
1107 }
1108 
fetch_register_operand(struct operand * op)1109 static void fetch_register_operand(struct operand *op)
1110 {
1111 	switch (op->bytes) {
1112 	case 1:
1113 		op->val = *(u8 *)op->addr.reg;
1114 		break;
1115 	case 2:
1116 		op->val = *(u16 *)op->addr.reg;
1117 		break;
1118 	case 4:
1119 		op->val = *(u32 *)op->addr.reg;
1120 		break;
1121 	case 8:
1122 		op->val = *(u64 *)op->addr.reg;
1123 		break;
1124 	}
1125 }
1126 
em_fninit(struct x86_emulate_ctxt * ctxt)1127 static int em_fninit(struct x86_emulate_ctxt *ctxt)
1128 {
1129 	if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1130 		return emulate_nm(ctxt);
1131 
1132 	kvm_fpu_get();
1133 	asm volatile("fninit");
1134 	kvm_fpu_put();
1135 	return X86EMUL_CONTINUE;
1136 }
1137 
em_fnstcw(struct x86_emulate_ctxt * ctxt)1138 static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1139 {
1140 	u16 fcw;
1141 
1142 	if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1143 		return emulate_nm(ctxt);
1144 
1145 	kvm_fpu_get();
1146 	asm volatile("fnstcw %0": "+m"(fcw));
1147 	kvm_fpu_put();
1148 
1149 	ctxt->dst.val = fcw;
1150 
1151 	return X86EMUL_CONTINUE;
1152 }
1153 
em_fnstsw(struct x86_emulate_ctxt * ctxt)1154 static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1155 {
1156 	u16 fsw;
1157 
1158 	if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1159 		return emulate_nm(ctxt);
1160 
1161 	kvm_fpu_get();
1162 	asm volatile("fnstsw %0": "+m"(fsw));
1163 	kvm_fpu_put();
1164 
1165 	ctxt->dst.val = fsw;
1166 
1167 	return X86EMUL_CONTINUE;
1168 }
1169 
decode_register_operand(struct x86_emulate_ctxt * ctxt,struct operand * op)1170 static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
1171 				    struct operand *op)
1172 {
1173 	unsigned reg = ctxt->modrm_reg;
1174 
1175 	if (!(ctxt->d & ModRM))
1176 		reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
1177 
1178 	if (ctxt->d & Sse) {
1179 		op->type = OP_XMM;
1180 		op->bytes = 16;
1181 		op->addr.xmm = reg;
1182 		kvm_read_sse_reg(reg, &op->vec_val);
1183 		return;
1184 	}
1185 	if (ctxt->d & Mmx) {
1186 		reg &= 7;
1187 		op->type = OP_MM;
1188 		op->bytes = 8;
1189 		op->addr.mm = reg;
1190 		return;
1191 	}
1192 
1193 	op->type = OP_REG;
1194 	op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1195 	op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1196 
1197 	fetch_register_operand(op);
1198 	op->orig_val = op->val;
1199 }
1200 
adjust_modrm_seg(struct x86_emulate_ctxt * ctxt,int base_reg)1201 static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1202 {
1203 	if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1204 		ctxt->modrm_seg = VCPU_SREG_SS;
1205 }
1206 
decode_modrm(struct x86_emulate_ctxt * ctxt,struct operand * op)1207 static int decode_modrm(struct x86_emulate_ctxt *ctxt,
1208 			struct operand *op)
1209 {
1210 	u8 sib;
1211 	int index_reg, base_reg, scale;
1212 	int rc = X86EMUL_CONTINUE;
1213 	ulong modrm_ea = 0;
1214 
1215 	ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
1216 	index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
1217 	base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
1218 
1219 	ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
1220 	ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1221 	ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
1222 	ctxt->modrm_seg = VCPU_SREG_DS;
1223 
1224 	if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
1225 		op->type = OP_REG;
1226 		op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1227 		op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
1228 				ctxt->d & ByteOp);
1229 		if (ctxt->d & Sse) {
1230 			op->type = OP_XMM;
1231 			op->bytes = 16;
1232 			op->addr.xmm = ctxt->modrm_rm;
1233 			kvm_read_sse_reg(ctxt->modrm_rm, &op->vec_val);
1234 			return rc;
1235 		}
1236 		if (ctxt->d & Mmx) {
1237 			op->type = OP_MM;
1238 			op->bytes = 8;
1239 			op->addr.mm = ctxt->modrm_rm & 7;
1240 			return rc;
1241 		}
1242 		fetch_register_operand(op);
1243 		return rc;
1244 	}
1245 
1246 	op->type = OP_MEM;
1247 
1248 	if (ctxt->ad_bytes == 2) {
1249 		unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1250 		unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1251 		unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1252 		unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
1253 
1254 		/* 16-bit ModR/M decode. */
1255 		switch (ctxt->modrm_mod) {
1256 		case 0:
1257 			if (ctxt->modrm_rm == 6)
1258 				modrm_ea += insn_fetch(u16, ctxt);
1259 			break;
1260 		case 1:
1261 			modrm_ea += insn_fetch(s8, ctxt);
1262 			break;
1263 		case 2:
1264 			modrm_ea += insn_fetch(u16, ctxt);
1265 			break;
1266 		}
1267 		switch (ctxt->modrm_rm) {
1268 		case 0:
1269 			modrm_ea += bx + si;
1270 			break;
1271 		case 1:
1272 			modrm_ea += bx + di;
1273 			break;
1274 		case 2:
1275 			modrm_ea += bp + si;
1276 			break;
1277 		case 3:
1278 			modrm_ea += bp + di;
1279 			break;
1280 		case 4:
1281 			modrm_ea += si;
1282 			break;
1283 		case 5:
1284 			modrm_ea += di;
1285 			break;
1286 		case 6:
1287 			if (ctxt->modrm_mod != 0)
1288 				modrm_ea += bp;
1289 			break;
1290 		case 7:
1291 			modrm_ea += bx;
1292 			break;
1293 		}
1294 		if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1295 		    (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1296 			ctxt->modrm_seg = VCPU_SREG_SS;
1297 		modrm_ea = (u16)modrm_ea;
1298 	} else {
1299 		/* 32/64-bit ModR/M decode. */
1300 		if ((ctxt->modrm_rm & 7) == 4) {
1301 			sib = insn_fetch(u8, ctxt);
1302 			index_reg |= (sib >> 3) & 7;
1303 			base_reg |= sib & 7;
1304 			scale = sib >> 6;
1305 
1306 			if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
1307 				modrm_ea += insn_fetch(s32, ctxt);
1308 			else {
1309 				modrm_ea += reg_read(ctxt, base_reg);
1310 				adjust_modrm_seg(ctxt, base_reg);
1311 				/* Increment ESP on POP [ESP] */
1312 				if ((ctxt->d & IncSP) &&
1313 				    base_reg == VCPU_REGS_RSP)
1314 					modrm_ea += ctxt->op_bytes;
1315 			}
1316 			if (index_reg != 4)
1317 				modrm_ea += reg_read(ctxt, index_reg) << scale;
1318 		} else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
1319 			modrm_ea += insn_fetch(s32, ctxt);
1320 			if (ctxt->mode == X86EMUL_MODE_PROT64)
1321 				ctxt->rip_relative = 1;
1322 		} else {
1323 			base_reg = ctxt->modrm_rm;
1324 			modrm_ea += reg_read(ctxt, base_reg);
1325 			adjust_modrm_seg(ctxt, base_reg);
1326 		}
1327 		switch (ctxt->modrm_mod) {
1328 		case 1:
1329 			modrm_ea += insn_fetch(s8, ctxt);
1330 			break;
1331 		case 2:
1332 			modrm_ea += insn_fetch(s32, ctxt);
1333 			break;
1334 		}
1335 	}
1336 	op->addr.mem.ea = modrm_ea;
1337 	if (ctxt->ad_bytes != 8)
1338 		ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1339 
1340 done:
1341 	return rc;
1342 }
1343 
decode_abs(struct x86_emulate_ctxt * ctxt,struct operand * op)1344 static int decode_abs(struct x86_emulate_ctxt *ctxt,
1345 		      struct operand *op)
1346 {
1347 	int rc = X86EMUL_CONTINUE;
1348 
1349 	op->type = OP_MEM;
1350 	switch (ctxt->ad_bytes) {
1351 	case 2:
1352 		op->addr.mem.ea = insn_fetch(u16, ctxt);
1353 		break;
1354 	case 4:
1355 		op->addr.mem.ea = insn_fetch(u32, ctxt);
1356 		break;
1357 	case 8:
1358 		op->addr.mem.ea = insn_fetch(u64, ctxt);
1359 		break;
1360 	}
1361 done:
1362 	return rc;
1363 }
1364 
fetch_bit_operand(struct x86_emulate_ctxt * ctxt)1365 static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
1366 {
1367 	long sv = 0, mask;
1368 
1369 	if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1370 		mask = ~((long)ctxt->dst.bytes * 8 - 1);
1371 
1372 		if (ctxt->src.bytes == 2)
1373 			sv = (s16)ctxt->src.val & (s16)mask;
1374 		else if (ctxt->src.bytes == 4)
1375 			sv = (s32)ctxt->src.val & (s32)mask;
1376 		else
1377 			sv = (s64)ctxt->src.val & (s64)mask;
1378 
1379 		ctxt->dst.addr.mem.ea = address_mask(ctxt,
1380 					   ctxt->dst.addr.mem.ea + (sv >> 3));
1381 	}
1382 
1383 	/* only subword offset */
1384 	ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
1385 }
1386 
read_emulated(struct x86_emulate_ctxt * ctxt,unsigned long addr,void * dest,unsigned size)1387 static int read_emulated(struct x86_emulate_ctxt *ctxt,
1388 			 unsigned long addr, void *dest, unsigned size)
1389 {
1390 	int rc;
1391 	struct read_cache *mc = &ctxt->mem_read;
1392 
1393 	if (mc->pos < mc->end)
1394 		goto read_cached;
1395 
1396 	WARN_ON((mc->end + size) >= sizeof(mc->data));
1397 
1398 	rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1399 				      &ctxt->exception);
1400 	if (rc != X86EMUL_CONTINUE)
1401 		return rc;
1402 
1403 	mc->end += size;
1404 
1405 read_cached:
1406 	memcpy(dest, mc->data + mc->pos, size);
1407 	mc->pos += size;
1408 	return X86EMUL_CONTINUE;
1409 }
1410 
segmented_read(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,void * data,unsigned size)1411 static int segmented_read(struct x86_emulate_ctxt *ctxt,
1412 			  struct segmented_address addr,
1413 			  void *data,
1414 			  unsigned size)
1415 {
1416 	int rc;
1417 	ulong linear;
1418 
1419 	rc = linearize(ctxt, addr, size, false, &linear);
1420 	if (rc != X86EMUL_CONTINUE)
1421 		return rc;
1422 	return read_emulated(ctxt, linear, data, size);
1423 }
1424 
segmented_write(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,const void * data,unsigned size)1425 static int segmented_write(struct x86_emulate_ctxt *ctxt,
1426 			   struct segmented_address addr,
1427 			   const void *data,
1428 			   unsigned size)
1429 {
1430 	int rc;
1431 	ulong linear;
1432 
1433 	rc = linearize(ctxt, addr, size, true, &linear);
1434 	if (rc != X86EMUL_CONTINUE)
1435 		return rc;
1436 	return ctxt->ops->write_emulated(ctxt, linear, data, size,
1437 					 &ctxt->exception);
1438 }
1439 
segmented_cmpxchg(struct x86_emulate_ctxt * ctxt,struct segmented_address addr,const void * orig_data,const void * data,unsigned size)1440 static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1441 			     struct segmented_address addr,
1442 			     const void *orig_data, const void *data,
1443 			     unsigned size)
1444 {
1445 	int rc;
1446 	ulong linear;
1447 
1448 	rc = linearize(ctxt, addr, size, true, &linear);
1449 	if (rc != X86EMUL_CONTINUE)
1450 		return rc;
1451 	return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1452 					   size, &ctxt->exception);
1453 }
1454 
pio_in_emulated(struct x86_emulate_ctxt * ctxt,unsigned int size,unsigned short port,void * dest)1455 static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1456 			   unsigned int size, unsigned short port,
1457 			   void *dest)
1458 {
1459 	struct read_cache *rc = &ctxt->io_read;
1460 
1461 	if (rc->pos == rc->end) { /* refill pio read ahead */
1462 		unsigned int in_page, n;
1463 		unsigned int count = ctxt->rep_prefix ?
1464 			address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
1465 		in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
1466 			offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1467 			PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
1468 		n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
1469 		if (n == 0)
1470 			n = 1;
1471 		rc->pos = rc->end = 0;
1472 		if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
1473 			return 0;
1474 		rc->end = n * size;
1475 	}
1476 
1477 	if (ctxt->rep_prefix && (ctxt->d & String) &&
1478 	    !(ctxt->eflags & X86_EFLAGS_DF)) {
1479 		ctxt->dst.data = rc->data + rc->pos;
1480 		ctxt->dst.type = OP_MEM_STR;
1481 		ctxt->dst.count = (rc->end - rc->pos) / size;
1482 		rc->pos = rc->end;
1483 	} else {
1484 		memcpy(dest, rc->data + rc->pos, size);
1485 		rc->pos += size;
1486 	}
1487 	return 1;
1488 }
1489 
read_interrupt_descriptor(struct x86_emulate_ctxt * ctxt,u16 index,struct desc_struct * desc)1490 static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1491 				     u16 index, struct desc_struct *desc)
1492 {
1493 	struct desc_ptr dt;
1494 	ulong addr;
1495 
1496 	ctxt->ops->get_idt(ctxt, &dt);
1497 
1498 	if (dt.size < index * 8 + 7)
1499 		return emulate_gp(ctxt, index << 3 | 0x2);
1500 
1501 	addr = dt.address + index * 8;
1502 	return linear_read_system(ctxt, addr, desc, sizeof(*desc));
1503 }
1504 
get_descriptor_table_ptr(struct x86_emulate_ctxt * ctxt,u16 selector,struct desc_ptr * dt)1505 static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1506 				     u16 selector, struct desc_ptr *dt)
1507 {
1508 	const struct x86_emulate_ops *ops = ctxt->ops;
1509 	u32 base3 = 0;
1510 
1511 	if (selector & 1 << 2) {
1512 		struct desc_struct desc;
1513 		u16 sel;
1514 
1515 		memset(dt, 0, sizeof(*dt));
1516 		if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1517 				      VCPU_SREG_LDTR))
1518 			return;
1519 
1520 		dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1521 		dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
1522 	} else
1523 		ops->get_gdt(ctxt, dt);
1524 }
1525 
get_descriptor_ptr(struct x86_emulate_ctxt * ctxt,u16 selector,ulong * desc_addr_p)1526 static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1527 			      u16 selector, ulong *desc_addr_p)
1528 {
1529 	struct desc_ptr dt;
1530 	u16 index = selector >> 3;
1531 	ulong addr;
1532 
1533 	get_descriptor_table_ptr(ctxt, selector, &dt);
1534 
1535 	if (dt.size < index * 8 + 7)
1536 		return emulate_gp(ctxt, selector & 0xfffc);
1537 
1538 	addr = dt.address + index * 8;
1539 
1540 #ifdef CONFIG_X86_64
1541 	if (addr >> 32 != 0) {
1542 		u64 efer = 0;
1543 
1544 		ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1545 		if (!(efer & EFER_LMA))
1546 			addr &= (u32)-1;
1547 	}
1548 #endif
1549 
1550 	*desc_addr_p = addr;
1551 	return X86EMUL_CONTINUE;
1552 }
1553 
1554 /* allowed just for 8 bytes segments */
read_segment_descriptor(struct x86_emulate_ctxt * ctxt,u16 selector,struct desc_struct * desc,ulong * desc_addr_p)1555 static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1556 				   u16 selector, struct desc_struct *desc,
1557 				   ulong *desc_addr_p)
1558 {
1559 	int rc;
1560 
1561 	rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1562 	if (rc != X86EMUL_CONTINUE)
1563 		return rc;
1564 
1565 	return linear_read_system(ctxt, *desc_addr_p, desc, sizeof(*desc));
1566 }
1567 
1568 /* allowed just for 8 bytes segments */
write_segment_descriptor(struct x86_emulate_ctxt * ctxt,u16 selector,struct desc_struct * desc)1569 static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1570 				    u16 selector, struct desc_struct *desc)
1571 {
1572 	int rc;
1573 	ulong addr;
1574 
1575 	rc = get_descriptor_ptr(ctxt, selector, &addr);
1576 	if (rc != X86EMUL_CONTINUE)
1577 		return rc;
1578 
1579 	return linear_write_system(ctxt, addr, desc, sizeof(*desc));
1580 }
1581 
__load_segment_descriptor(struct x86_emulate_ctxt * ctxt,u16 selector,int seg,u8 cpl,enum x86_transfer_type transfer,struct desc_struct * desc)1582 static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1583 				     u16 selector, int seg, u8 cpl,
1584 				     enum x86_transfer_type transfer,
1585 				     struct desc_struct *desc)
1586 {
1587 	struct desc_struct seg_desc, old_desc;
1588 	u8 dpl, rpl;
1589 	unsigned err_vec = GP_VECTOR;
1590 	u32 err_code = 0;
1591 	bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1592 	ulong desc_addr;
1593 	int ret;
1594 	u16 dummy;
1595 	u32 base3 = 0;
1596 
1597 	memset(&seg_desc, 0, sizeof(seg_desc));
1598 
1599 	if (ctxt->mode == X86EMUL_MODE_REAL) {
1600 		/* set real mode segment descriptor (keep limit etc. for
1601 		 * unreal mode) */
1602 		ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
1603 		set_desc_base(&seg_desc, selector << 4);
1604 		goto load;
1605 	} else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1606 		/* VM86 needs a clean new segment descriptor */
1607 		set_desc_base(&seg_desc, selector << 4);
1608 		set_desc_limit(&seg_desc, 0xffff);
1609 		seg_desc.type = 3;
1610 		seg_desc.p = 1;
1611 		seg_desc.s = 1;
1612 		seg_desc.dpl = 3;
1613 		goto load;
1614 	}
1615 
1616 	rpl = selector & 3;
1617 
1618 	/* TR should be in GDT only */
1619 	if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1620 		goto exception;
1621 
1622 	/* NULL selector is not valid for TR, CS and (except for long mode) SS */
1623 	if (null_selector) {
1624 		if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
1625 			goto exception;
1626 
1627 		if (seg == VCPU_SREG_SS) {
1628 			if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
1629 				goto exception;
1630 
1631 			/*
1632 			 * ctxt->ops->set_segment expects the CPL to be in
1633 			 * SS.DPL, so fake an expand-up 32-bit data segment.
1634 			 */
1635 			seg_desc.type = 3;
1636 			seg_desc.p = 1;
1637 			seg_desc.s = 1;
1638 			seg_desc.dpl = cpl;
1639 			seg_desc.d = 1;
1640 			seg_desc.g = 1;
1641 		}
1642 
1643 		/* Skip all following checks */
1644 		goto load;
1645 	}
1646 
1647 	ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
1648 	if (ret != X86EMUL_CONTINUE)
1649 		return ret;
1650 
1651 	err_code = selector & 0xfffc;
1652 	err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1653 							   GP_VECTOR;
1654 
1655 	/* can't load system descriptor into segment selector */
1656 	if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1657 		if (transfer == X86_TRANSFER_CALL_JMP)
1658 			return X86EMUL_UNHANDLEABLE;
1659 		goto exception;
1660 	}
1661 
1662 	dpl = seg_desc.dpl;
1663 
1664 	switch (seg) {
1665 	case VCPU_SREG_SS:
1666 		/*
1667 		 * segment is not a writable data segment or segment
1668 		 * selector's RPL != CPL or segment selector's RPL != CPL
1669 		 */
1670 		if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1671 			goto exception;
1672 		break;
1673 	case VCPU_SREG_CS:
1674 		if (!(seg_desc.type & 8))
1675 			goto exception;
1676 
1677 		if (seg_desc.type & 4) {
1678 			/* conforming */
1679 			if (dpl > cpl)
1680 				goto exception;
1681 		} else {
1682 			/* nonconforming */
1683 			if (rpl > cpl || dpl != cpl)
1684 				goto exception;
1685 		}
1686 		/* in long-mode d/b must be clear if l is set */
1687 		if (seg_desc.d && seg_desc.l) {
1688 			u64 efer = 0;
1689 
1690 			ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1691 			if (efer & EFER_LMA)
1692 				goto exception;
1693 		}
1694 
1695 		/* CS(RPL) <- CPL */
1696 		selector = (selector & 0xfffc) | cpl;
1697 		break;
1698 	case VCPU_SREG_TR:
1699 		if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1700 			goto exception;
1701 		break;
1702 	case VCPU_SREG_LDTR:
1703 		if (seg_desc.s || seg_desc.type != 2)
1704 			goto exception;
1705 		break;
1706 	default: /*  DS, ES, FS, or GS */
1707 		/*
1708 		 * segment is not a data or readable code segment or
1709 		 * ((segment is a data or nonconforming code segment)
1710 		 * and (both RPL and CPL > DPL))
1711 		 */
1712 		if ((seg_desc.type & 0xa) == 0x8 ||
1713 		    (((seg_desc.type & 0xc) != 0xc) &&
1714 		     (rpl > dpl && cpl > dpl)))
1715 			goto exception;
1716 		break;
1717 	}
1718 
1719 	if (!seg_desc.p) {
1720 		err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1721 		goto exception;
1722 	}
1723 
1724 	if (seg_desc.s) {
1725 		/* mark segment as accessed */
1726 		if (!(seg_desc.type & 1)) {
1727 			seg_desc.type |= 1;
1728 			ret = write_segment_descriptor(ctxt, selector,
1729 						       &seg_desc);
1730 			if (ret != X86EMUL_CONTINUE)
1731 				return ret;
1732 		}
1733 	} else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1734 		ret = linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3));
1735 		if (ret != X86EMUL_CONTINUE)
1736 			return ret;
1737 		if (emul_is_noncanonical_address(get_desc_base(&seg_desc) |
1738 						 ((u64)base3 << 32), ctxt))
1739 			return emulate_gp(ctxt, err_code);
1740 	}
1741 
1742 	if (seg == VCPU_SREG_TR) {
1743 		old_desc = seg_desc;
1744 		seg_desc.type |= 2; /* busy */
1745 		ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1746 						  sizeof(seg_desc), &ctxt->exception);
1747 		if (ret != X86EMUL_CONTINUE)
1748 			return ret;
1749 	}
1750 load:
1751 	ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
1752 	if (desc)
1753 		*desc = seg_desc;
1754 	return X86EMUL_CONTINUE;
1755 exception:
1756 	return emulate_exception(ctxt, err_vec, err_code, true);
1757 }
1758 
load_segment_descriptor(struct x86_emulate_ctxt * ctxt,u16 selector,int seg)1759 static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1760 				   u16 selector, int seg)
1761 {
1762 	u8 cpl = ctxt->ops->cpl(ctxt);
1763 
1764 	/*
1765 	 * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
1766 	 * they can load it at CPL<3 (Intel's manual says only LSS can,
1767 	 * but it's wrong).
1768 	 *
1769 	 * However, the Intel manual says that putting IST=1/DPL=3 in
1770 	 * an interrupt gate will result in SS=3 (the AMD manual instead
1771 	 * says it doesn't), so allow SS=3 in __load_segment_descriptor
1772 	 * and only forbid it here.
1773 	 */
1774 	if (seg == VCPU_SREG_SS && selector == 3 &&
1775 	    ctxt->mode == X86EMUL_MODE_PROT64)
1776 		return emulate_exception(ctxt, GP_VECTOR, 0, true);
1777 
1778 	return __load_segment_descriptor(ctxt, selector, seg, cpl,
1779 					 X86_TRANSFER_NONE, NULL);
1780 }
1781 
write_register_operand(struct operand * op)1782 static void write_register_operand(struct operand *op)
1783 {
1784 	return assign_register(op->addr.reg, op->val, op->bytes);
1785 }
1786 
writeback(struct x86_emulate_ctxt * ctxt,struct operand * op)1787 static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
1788 {
1789 	switch (op->type) {
1790 	case OP_REG:
1791 		write_register_operand(op);
1792 		break;
1793 	case OP_MEM:
1794 		if (ctxt->lock_prefix)
1795 			return segmented_cmpxchg(ctxt,
1796 						 op->addr.mem,
1797 						 &op->orig_val,
1798 						 &op->val,
1799 						 op->bytes);
1800 		else
1801 			return segmented_write(ctxt,
1802 					       op->addr.mem,
1803 					       &op->val,
1804 					       op->bytes);
1805 		break;
1806 	case OP_MEM_STR:
1807 		return segmented_write(ctxt,
1808 				       op->addr.mem,
1809 				       op->data,
1810 				       op->bytes * op->count);
1811 		break;
1812 	case OP_XMM:
1813 		kvm_write_sse_reg(op->addr.xmm, &op->vec_val);
1814 		break;
1815 	case OP_MM:
1816 		kvm_write_mmx_reg(op->addr.mm, &op->mm_val);
1817 		break;
1818 	case OP_NONE:
1819 		/* no writeback */
1820 		break;
1821 	default:
1822 		break;
1823 	}
1824 	return X86EMUL_CONTINUE;
1825 }
1826 
push(struct x86_emulate_ctxt * ctxt,void * data,int bytes)1827 static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
1828 {
1829 	struct segmented_address addr;
1830 
1831 	rsp_increment(ctxt, -bytes);
1832 	addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
1833 	addr.seg = VCPU_SREG_SS;
1834 
1835 	return segmented_write(ctxt, addr, data, bytes);
1836 }
1837 
em_push(struct x86_emulate_ctxt * ctxt)1838 static int em_push(struct x86_emulate_ctxt *ctxt)
1839 {
1840 	/* Disable writeback. */
1841 	ctxt->dst.type = OP_NONE;
1842 	return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
1843 }
1844 
emulate_pop(struct x86_emulate_ctxt * ctxt,void * dest,int len)1845 static int emulate_pop(struct x86_emulate_ctxt *ctxt,
1846 		       void *dest, int len)
1847 {
1848 	int rc;
1849 	struct segmented_address addr;
1850 
1851 	addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
1852 	addr.seg = VCPU_SREG_SS;
1853 	rc = segmented_read(ctxt, addr, dest, len);
1854 	if (rc != X86EMUL_CONTINUE)
1855 		return rc;
1856 
1857 	rsp_increment(ctxt, len);
1858 	return rc;
1859 }
1860 
em_pop(struct x86_emulate_ctxt * ctxt)1861 static int em_pop(struct x86_emulate_ctxt *ctxt)
1862 {
1863 	return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
1864 }
1865 
emulate_popf(struct x86_emulate_ctxt * ctxt,void * dest,int len)1866 static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1867 			void *dest, int len)
1868 {
1869 	int rc;
1870 	unsigned long val, change_mask;
1871 	int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
1872 	int cpl = ctxt->ops->cpl(ctxt);
1873 
1874 	rc = emulate_pop(ctxt, &val, len);
1875 	if (rc != X86EMUL_CONTINUE)
1876 		return rc;
1877 
1878 	change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1879 		      X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1880 		      X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1881 		      X86_EFLAGS_AC | X86_EFLAGS_ID;
1882 
1883 	switch(ctxt->mode) {
1884 	case X86EMUL_MODE_PROT64:
1885 	case X86EMUL_MODE_PROT32:
1886 	case X86EMUL_MODE_PROT16:
1887 		if (cpl == 0)
1888 			change_mask |= X86_EFLAGS_IOPL;
1889 		if (cpl <= iopl)
1890 			change_mask |= X86_EFLAGS_IF;
1891 		break;
1892 	case X86EMUL_MODE_VM86:
1893 		if (iopl < 3)
1894 			return emulate_gp(ctxt, 0);
1895 		change_mask |= X86_EFLAGS_IF;
1896 		break;
1897 	default: /* real mode */
1898 		change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
1899 		break;
1900 	}
1901 
1902 	*(unsigned long *)dest =
1903 		(ctxt->eflags & ~change_mask) | (val & change_mask);
1904 
1905 	return rc;
1906 }
1907 
em_popf(struct x86_emulate_ctxt * ctxt)1908 static int em_popf(struct x86_emulate_ctxt *ctxt)
1909 {
1910 	ctxt->dst.type = OP_REG;
1911 	ctxt->dst.addr.reg = &ctxt->eflags;
1912 	ctxt->dst.bytes = ctxt->op_bytes;
1913 	return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
1914 }
1915 
em_enter(struct x86_emulate_ctxt * ctxt)1916 static int em_enter(struct x86_emulate_ctxt *ctxt)
1917 {
1918 	int rc;
1919 	unsigned frame_size = ctxt->src.val;
1920 	unsigned nesting_level = ctxt->src2.val & 31;
1921 	ulong rbp;
1922 
1923 	if (nesting_level)
1924 		return X86EMUL_UNHANDLEABLE;
1925 
1926 	rbp = reg_read(ctxt, VCPU_REGS_RBP);
1927 	rc = push(ctxt, &rbp, stack_size(ctxt));
1928 	if (rc != X86EMUL_CONTINUE)
1929 		return rc;
1930 	assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
1931 		      stack_mask(ctxt));
1932 	assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1933 		      reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
1934 		      stack_mask(ctxt));
1935 	return X86EMUL_CONTINUE;
1936 }
1937 
em_leave(struct x86_emulate_ctxt * ctxt)1938 static int em_leave(struct x86_emulate_ctxt *ctxt)
1939 {
1940 	assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
1941 		      stack_mask(ctxt));
1942 	return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
1943 }
1944 
em_push_sreg(struct x86_emulate_ctxt * ctxt)1945 static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
1946 {
1947 	int seg = ctxt->src2.val;
1948 
1949 	ctxt->src.val = get_segment_selector(ctxt, seg);
1950 	if (ctxt->op_bytes == 4) {
1951 		rsp_increment(ctxt, -2);
1952 		ctxt->op_bytes = 2;
1953 	}
1954 
1955 	return em_push(ctxt);
1956 }
1957 
em_pop_sreg(struct x86_emulate_ctxt * ctxt)1958 static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
1959 {
1960 	int seg = ctxt->src2.val;
1961 	unsigned long selector;
1962 	int rc;
1963 
1964 	rc = emulate_pop(ctxt, &selector, 2);
1965 	if (rc != X86EMUL_CONTINUE)
1966 		return rc;
1967 
1968 	if (seg == VCPU_SREG_SS)
1969 		ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
1970 	if (ctxt->op_bytes > 2)
1971 		rsp_increment(ctxt, ctxt->op_bytes - 2);
1972 
1973 	rc = load_segment_descriptor(ctxt, (u16)selector, seg);
1974 	return rc;
1975 }
1976 
em_pusha(struct x86_emulate_ctxt * ctxt)1977 static int em_pusha(struct x86_emulate_ctxt *ctxt)
1978 {
1979 	unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
1980 	int rc = X86EMUL_CONTINUE;
1981 	int reg = VCPU_REGS_RAX;
1982 
1983 	while (reg <= VCPU_REGS_RDI) {
1984 		(reg == VCPU_REGS_RSP) ?
1985 		(ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
1986 
1987 		rc = em_push(ctxt);
1988 		if (rc != X86EMUL_CONTINUE)
1989 			return rc;
1990 
1991 		++reg;
1992 	}
1993 
1994 	return rc;
1995 }
1996 
em_pushf(struct x86_emulate_ctxt * ctxt)1997 static int em_pushf(struct x86_emulate_ctxt *ctxt)
1998 {
1999 	ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
2000 	return em_push(ctxt);
2001 }
2002 
em_popa(struct x86_emulate_ctxt * ctxt)2003 static int em_popa(struct x86_emulate_ctxt *ctxt)
2004 {
2005 	int rc = X86EMUL_CONTINUE;
2006 	int reg = VCPU_REGS_RDI;
2007 	u32 val;
2008 
2009 	while (reg >= VCPU_REGS_RAX) {
2010 		if (reg == VCPU_REGS_RSP) {
2011 			rsp_increment(ctxt, ctxt->op_bytes);
2012 			--reg;
2013 		}
2014 
2015 		rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
2016 		if (rc != X86EMUL_CONTINUE)
2017 			break;
2018 		assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
2019 		--reg;
2020 	}
2021 	return rc;
2022 }
2023 
__emulate_int_real(struct x86_emulate_ctxt * ctxt,int irq)2024 static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2025 {
2026 	const struct x86_emulate_ops *ops = ctxt->ops;
2027 	int rc;
2028 	struct desc_ptr dt;
2029 	gva_t cs_addr;
2030 	gva_t eip_addr;
2031 	u16 cs, eip;
2032 
2033 	/* TODO: Add limit checks */
2034 	ctxt->src.val = ctxt->eflags;
2035 	rc = em_push(ctxt);
2036 	if (rc != X86EMUL_CONTINUE)
2037 		return rc;
2038 
2039 	ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
2040 
2041 	ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
2042 	rc = em_push(ctxt);
2043 	if (rc != X86EMUL_CONTINUE)
2044 		return rc;
2045 
2046 	ctxt->src.val = ctxt->_eip;
2047 	rc = em_push(ctxt);
2048 	if (rc != X86EMUL_CONTINUE)
2049 		return rc;
2050 
2051 	ops->get_idt(ctxt, &dt);
2052 
2053 	eip_addr = dt.address + (irq << 2);
2054 	cs_addr = dt.address + (irq << 2) + 2;
2055 
2056 	rc = linear_read_system(ctxt, cs_addr, &cs, 2);
2057 	if (rc != X86EMUL_CONTINUE)
2058 		return rc;
2059 
2060 	rc = linear_read_system(ctxt, eip_addr, &eip, 2);
2061 	if (rc != X86EMUL_CONTINUE)
2062 		return rc;
2063 
2064 	rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
2065 	if (rc != X86EMUL_CONTINUE)
2066 		return rc;
2067 
2068 	ctxt->_eip = eip;
2069 
2070 	return rc;
2071 }
2072 
emulate_int_real(struct x86_emulate_ctxt * ctxt,int irq)2073 int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2074 {
2075 	int rc;
2076 
2077 	invalidate_registers(ctxt);
2078 	rc = __emulate_int_real(ctxt, irq);
2079 	if (rc == X86EMUL_CONTINUE)
2080 		writeback_registers(ctxt);
2081 	return rc;
2082 }
2083 
emulate_int(struct x86_emulate_ctxt * ctxt,int irq)2084 static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
2085 {
2086 	switch(ctxt->mode) {
2087 	case X86EMUL_MODE_REAL:
2088 		return __emulate_int_real(ctxt, irq);
2089 	case X86EMUL_MODE_VM86:
2090 	case X86EMUL_MODE_PROT16:
2091 	case X86EMUL_MODE_PROT32:
2092 	case X86EMUL_MODE_PROT64:
2093 	default:
2094 		/* Protected mode interrupts unimplemented yet */
2095 		return X86EMUL_UNHANDLEABLE;
2096 	}
2097 }
2098 
emulate_iret_real(struct x86_emulate_ctxt * ctxt)2099 static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
2100 {
2101 	int rc = X86EMUL_CONTINUE;
2102 	unsigned long temp_eip = 0;
2103 	unsigned long temp_eflags = 0;
2104 	unsigned long cs = 0;
2105 	unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2106 			     X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2107 			     X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2108 			     X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2109 			     X86_EFLAGS_AC | X86_EFLAGS_ID |
2110 			     X86_EFLAGS_FIXED;
2111 	unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2112 				  X86_EFLAGS_VIP;
2113 
2114 	/* TODO: Add stack limit check */
2115 
2116 	rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
2117 
2118 	if (rc != X86EMUL_CONTINUE)
2119 		return rc;
2120 
2121 	if (temp_eip & ~0xffff)
2122 		return emulate_gp(ctxt, 0);
2123 
2124 	rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
2125 
2126 	if (rc != X86EMUL_CONTINUE)
2127 		return rc;
2128 
2129 	rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
2130 
2131 	if (rc != X86EMUL_CONTINUE)
2132 		return rc;
2133 
2134 	rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
2135 
2136 	if (rc != X86EMUL_CONTINUE)
2137 		return rc;
2138 
2139 	ctxt->_eip = temp_eip;
2140 
2141 	if (ctxt->op_bytes == 4)
2142 		ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
2143 	else if (ctxt->op_bytes == 2) {
2144 		ctxt->eflags &= ~0xffff;
2145 		ctxt->eflags |= temp_eflags;
2146 	}
2147 
2148 	ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2149 	ctxt->eflags |= X86_EFLAGS_FIXED;
2150 	ctxt->ops->set_nmi_mask(ctxt, false);
2151 
2152 	return rc;
2153 }
2154 
em_iret(struct x86_emulate_ctxt * ctxt)2155 static int em_iret(struct x86_emulate_ctxt *ctxt)
2156 {
2157 	switch(ctxt->mode) {
2158 	case X86EMUL_MODE_REAL:
2159 		return emulate_iret_real(ctxt);
2160 	case X86EMUL_MODE_VM86:
2161 	case X86EMUL_MODE_PROT16:
2162 	case X86EMUL_MODE_PROT32:
2163 	case X86EMUL_MODE_PROT64:
2164 	default:
2165 		/* iret from protected mode unimplemented yet */
2166 		return X86EMUL_UNHANDLEABLE;
2167 	}
2168 }
2169 
em_jmp_far(struct x86_emulate_ctxt * ctxt)2170 static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2171 {
2172 	int rc;
2173 	unsigned short sel;
2174 	struct desc_struct new_desc;
2175 	u8 cpl = ctxt->ops->cpl(ctxt);
2176 
2177 	memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
2178 
2179 	rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2180 				       X86_TRANSFER_CALL_JMP,
2181 				       &new_desc);
2182 	if (rc != X86EMUL_CONTINUE)
2183 		return rc;
2184 
2185 	rc = assign_eip_far(ctxt, ctxt->src.val);
2186 	/* Error handling is not implemented. */
2187 	if (rc != X86EMUL_CONTINUE)
2188 		return X86EMUL_UNHANDLEABLE;
2189 
2190 	return rc;
2191 }
2192 
em_jmp_abs(struct x86_emulate_ctxt * ctxt)2193 static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
2194 {
2195 	return assign_eip_near(ctxt, ctxt->src.val);
2196 }
2197 
em_call_near_abs(struct x86_emulate_ctxt * ctxt)2198 static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2199 {
2200 	int rc;
2201 	long int old_eip;
2202 
2203 	old_eip = ctxt->_eip;
2204 	rc = assign_eip_near(ctxt, ctxt->src.val);
2205 	if (rc != X86EMUL_CONTINUE)
2206 		return rc;
2207 	ctxt->src.val = old_eip;
2208 	rc = em_push(ctxt);
2209 	return rc;
2210 }
2211 
em_cmpxchg8b(struct x86_emulate_ctxt * ctxt)2212 static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
2213 {
2214 	u64 old = ctxt->dst.orig_val64;
2215 
2216 	if (ctxt->dst.bytes == 16)
2217 		return X86EMUL_UNHANDLEABLE;
2218 
2219 	if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2220 	    ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2221 		*reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2222 		*reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
2223 		ctxt->eflags &= ~X86_EFLAGS_ZF;
2224 	} else {
2225 		ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2226 			(u32) reg_read(ctxt, VCPU_REGS_RBX);
2227 
2228 		ctxt->eflags |= X86_EFLAGS_ZF;
2229 	}
2230 	return X86EMUL_CONTINUE;
2231 }
2232 
em_ret(struct x86_emulate_ctxt * ctxt)2233 static int em_ret(struct x86_emulate_ctxt *ctxt)
2234 {
2235 	int rc;
2236 	unsigned long eip;
2237 
2238 	rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2239 	if (rc != X86EMUL_CONTINUE)
2240 		return rc;
2241 
2242 	return assign_eip_near(ctxt, eip);
2243 }
2244 
em_ret_far(struct x86_emulate_ctxt * ctxt)2245 static int em_ret_far(struct x86_emulate_ctxt *ctxt)
2246 {
2247 	int rc;
2248 	unsigned long eip, cs;
2249 	int cpl = ctxt->ops->cpl(ctxt);
2250 	struct desc_struct new_desc;
2251 
2252 	rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2253 	if (rc != X86EMUL_CONTINUE)
2254 		return rc;
2255 	rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
2256 	if (rc != X86EMUL_CONTINUE)
2257 		return rc;
2258 	/* Outer-privilege level return is not implemented */
2259 	if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2260 		return X86EMUL_UNHANDLEABLE;
2261 	rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2262 				       X86_TRANSFER_RET,
2263 				       &new_desc);
2264 	if (rc != X86EMUL_CONTINUE)
2265 		return rc;
2266 	rc = assign_eip_far(ctxt, eip);
2267 	/* Error handling is not implemented. */
2268 	if (rc != X86EMUL_CONTINUE)
2269 		return X86EMUL_UNHANDLEABLE;
2270 
2271 	return rc;
2272 }
2273 
em_ret_far_imm(struct x86_emulate_ctxt * ctxt)2274 static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2275 {
2276         int rc;
2277 
2278         rc = em_ret_far(ctxt);
2279         if (rc != X86EMUL_CONTINUE)
2280                 return rc;
2281         rsp_increment(ctxt, ctxt->src.val);
2282         return X86EMUL_CONTINUE;
2283 }
2284 
em_cmpxchg(struct x86_emulate_ctxt * ctxt)2285 static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2286 {
2287 	/* Save real source value, then compare EAX against destination. */
2288 	ctxt->dst.orig_val = ctxt->dst.val;
2289 	ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
2290 	ctxt->src.orig_val = ctxt->src.val;
2291 	ctxt->src.val = ctxt->dst.orig_val;
2292 	fastop(ctxt, em_cmp);
2293 
2294 	if (ctxt->eflags & X86_EFLAGS_ZF) {
2295 		/* Success: write back to memory; no update of EAX */
2296 		ctxt->src.type = OP_NONE;
2297 		ctxt->dst.val = ctxt->src.orig_val;
2298 	} else {
2299 		/* Failure: write the value we saw to EAX. */
2300 		ctxt->src.type = OP_REG;
2301 		ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2302 		ctxt->src.val = ctxt->dst.orig_val;
2303 		/* Create write-cycle to dest by writing the same value */
2304 		ctxt->dst.val = ctxt->dst.orig_val;
2305 	}
2306 	return X86EMUL_CONTINUE;
2307 }
2308 
em_lseg(struct x86_emulate_ctxt * ctxt)2309 static int em_lseg(struct x86_emulate_ctxt *ctxt)
2310 {
2311 	int seg = ctxt->src2.val;
2312 	unsigned short sel;
2313 	int rc;
2314 
2315 	memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
2316 
2317 	rc = load_segment_descriptor(ctxt, sel, seg);
2318 	if (rc != X86EMUL_CONTINUE)
2319 		return rc;
2320 
2321 	ctxt->dst.val = ctxt->src.val;
2322 	return rc;
2323 }
2324 
emulator_has_longmode(struct x86_emulate_ctxt * ctxt)2325 static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2326 {
2327 #ifdef CONFIG_X86_64
2328 	return ctxt->ops->guest_has_long_mode(ctxt);
2329 #else
2330 	return false;
2331 #endif
2332 }
2333 
rsm_set_desc_flags(struct desc_struct * desc,u32 flags)2334 static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2335 {
2336 	desc->g    = (flags >> 23) & 1;
2337 	desc->d    = (flags >> 22) & 1;
2338 	desc->l    = (flags >> 21) & 1;
2339 	desc->avl  = (flags >> 20) & 1;
2340 	desc->p    = (flags >> 15) & 1;
2341 	desc->dpl  = (flags >> 13) & 3;
2342 	desc->s    = (flags >> 12) & 1;
2343 	desc->type = (flags >>  8) & 15;
2344 }
2345 
rsm_load_seg_32(struct x86_emulate_ctxt * ctxt,const char * smstate,int n)2346 static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, const char *smstate,
2347 			   int n)
2348 {
2349 	struct desc_struct desc;
2350 	int offset;
2351 	u16 selector;
2352 
2353 	selector = GET_SMSTATE(u32, smstate, 0x7fa8 + n * 4);
2354 
2355 	if (n < 3)
2356 		offset = 0x7f84 + n * 12;
2357 	else
2358 		offset = 0x7f2c + (n - 3) * 12;
2359 
2360 	set_desc_base(&desc,      GET_SMSTATE(u32, smstate, offset + 8));
2361 	set_desc_limit(&desc,     GET_SMSTATE(u32, smstate, offset + 4));
2362 	rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smstate, offset));
2363 	ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2364 	return X86EMUL_CONTINUE;
2365 }
2366 
2367 #ifdef CONFIG_X86_64
rsm_load_seg_64(struct x86_emulate_ctxt * ctxt,const char * smstate,int n)2368 static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, const char *smstate,
2369 			   int n)
2370 {
2371 	struct desc_struct desc;
2372 	int offset;
2373 	u16 selector;
2374 	u32 base3;
2375 
2376 	offset = 0x7e00 + n * 16;
2377 
2378 	selector =                GET_SMSTATE(u16, smstate, offset);
2379 	rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smstate, offset + 2) << 8);
2380 	set_desc_limit(&desc,     GET_SMSTATE(u32, smstate, offset + 4));
2381 	set_desc_base(&desc,      GET_SMSTATE(u32, smstate, offset + 8));
2382 	base3 =                   GET_SMSTATE(u32, smstate, offset + 12);
2383 
2384 	ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2385 	return X86EMUL_CONTINUE;
2386 }
2387 #endif
2388 
rsm_enter_protected_mode(struct x86_emulate_ctxt * ctxt,u64 cr0,u64 cr3,u64 cr4)2389 static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
2390 				    u64 cr0, u64 cr3, u64 cr4)
2391 {
2392 	int bad;
2393 	u64 pcid;
2394 
2395 	/* In order to later set CR4.PCIDE, CR3[11:0] must be zero.  */
2396 	pcid = 0;
2397 	if (cr4 & X86_CR4_PCIDE) {
2398 		pcid = cr3 & 0xfff;
2399 		cr3 &= ~0xfff;
2400 	}
2401 
2402 	bad = ctxt->ops->set_cr(ctxt, 3, cr3);
2403 	if (bad)
2404 		return X86EMUL_UNHANDLEABLE;
2405 
2406 	/*
2407 	 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2408 	 * Then enable protected mode.	However, PCID cannot be enabled
2409 	 * if EFER.LMA=0, so set it separately.
2410 	 */
2411 	bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2412 	if (bad)
2413 		return X86EMUL_UNHANDLEABLE;
2414 
2415 	bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2416 	if (bad)
2417 		return X86EMUL_UNHANDLEABLE;
2418 
2419 	if (cr4 & X86_CR4_PCIDE) {
2420 		bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2421 		if (bad)
2422 			return X86EMUL_UNHANDLEABLE;
2423 		if (pcid) {
2424 			bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
2425 			if (bad)
2426 				return X86EMUL_UNHANDLEABLE;
2427 		}
2428 
2429 	}
2430 
2431 	return X86EMUL_CONTINUE;
2432 }
2433 
rsm_load_state_32(struct x86_emulate_ctxt * ctxt,const char * smstate)2434 static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt,
2435 			     const char *smstate)
2436 {
2437 	struct desc_struct desc;
2438 	struct desc_ptr dt;
2439 	u16 selector;
2440 	u32 val, cr0, cr3, cr4;
2441 	int i;
2442 
2443 	cr0 =                      GET_SMSTATE(u32, smstate, 0x7ffc);
2444 	cr3 =                      GET_SMSTATE(u32, smstate, 0x7ff8);
2445 	ctxt->eflags =             GET_SMSTATE(u32, smstate, 0x7ff4) | X86_EFLAGS_FIXED;
2446 	ctxt->_eip =               GET_SMSTATE(u32, smstate, 0x7ff0);
2447 
2448 	for (i = 0; i < 8; i++)
2449 		*reg_write(ctxt, i) = GET_SMSTATE(u32, smstate, 0x7fd0 + i * 4);
2450 
2451 	val = GET_SMSTATE(u32, smstate, 0x7fcc);
2452 
2453 	if (ctxt->ops->set_dr(ctxt, 6, val))
2454 		return X86EMUL_UNHANDLEABLE;
2455 
2456 	val = GET_SMSTATE(u32, smstate, 0x7fc8);
2457 
2458 	if (ctxt->ops->set_dr(ctxt, 7, val))
2459 		return X86EMUL_UNHANDLEABLE;
2460 
2461 	selector =                 GET_SMSTATE(u32, smstate, 0x7fc4);
2462 	set_desc_base(&desc,       GET_SMSTATE(u32, smstate, 0x7f64));
2463 	set_desc_limit(&desc,      GET_SMSTATE(u32, smstate, 0x7f60));
2464 	rsm_set_desc_flags(&desc,  GET_SMSTATE(u32, smstate, 0x7f5c));
2465 	ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2466 
2467 	selector =                 GET_SMSTATE(u32, smstate, 0x7fc0);
2468 	set_desc_base(&desc,       GET_SMSTATE(u32, smstate, 0x7f80));
2469 	set_desc_limit(&desc,      GET_SMSTATE(u32, smstate, 0x7f7c));
2470 	rsm_set_desc_flags(&desc,  GET_SMSTATE(u32, smstate, 0x7f78));
2471 	ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2472 
2473 	dt.address =               GET_SMSTATE(u32, smstate, 0x7f74);
2474 	dt.size =                  GET_SMSTATE(u32, smstate, 0x7f70);
2475 	ctxt->ops->set_gdt(ctxt, &dt);
2476 
2477 	dt.address =               GET_SMSTATE(u32, smstate, 0x7f58);
2478 	dt.size =                  GET_SMSTATE(u32, smstate, 0x7f54);
2479 	ctxt->ops->set_idt(ctxt, &dt);
2480 
2481 	for (i = 0; i < 6; i++) {
2482 		int r = rsm_load_seg_32(ctxt, smstate, i);
2483 		if (r != X86EMUL_CONTINUE)
2484 			return r;
2485 	}
2486 
2487 	cr4 = GET_SMSTATE(u32, smstate, 0x7f14);
2488 
2489 	ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7ef8));
2490 
2491 	return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
2492 }
2493 
2494 #ifdef CONFIG_X86_64
rsm_load_state_64(struct x86_emulate_ctxt * ctxt,const char * smstate)2495 static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
2496 			     const char *smstate)
2497 {
2498 	struct desc_struct desc;
2499 	struct desc_ptr dt;
2500 	u64 val, cr0, cr3, cr4;
2501 	u32 base3;
2502 	u16 selector;
2503 	int i, r;
2504 
2505 	for (i = 0; i < 16; i++)
2506 		*reg_write(ctxt, i) = GET_SMSTATE(u64, smstate, 0x7ff8 - i * 8);
2507 
2508 	ctxt->_eip   = GET_SMSTATE(u64, smstate, 0x7f78);
2509 	ctxt->eflags = GET_SMSTATE(u32, smstate, 0x7f70) | X86_EFLAGS_FIXED;
2510 
2511 	val = GET_SMSTATE(u64, smstate, 0x7f68);
2512 
2513 	if (ctxt->ops->set_dr(ctxt, 6, val))
2514 		return X86EMUL_UNHANDLEABLE;
2515 
2516 	val = GET_SMSTATE(u64, smstate, 0x7f60);
2517 
2518 	if (ctxt->ops->set_dr(ctxt, 7, val))
2519 		return X86EMUL_UNHANDLEABLE;
2520 
2521 	cr0 =                       GET_SMSTATE(u64, smstate, 0x7f58);
2522 	cr3 =                       GET_SMSTATE(u64, smstate, 0x7f50);
2523 	cr4 =                       GET_SMSTATE(u64, smstate, 0x7f48);
2524 	ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smstate, 0x7f00));
2525 	val =                       GET_SMSTATE(u64, smstate, 0x7ed0);
2526 
2527 	if (ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA))
2528 		return X86EMUL_UNHANDLEABLE;
2529 
2530 	selector =                  GET_SMSTATE(u32, smstate, 0x7e90);
2531 	rsm_set_desc_flags(&desc,   GET_SMSTATE(u32, smstate, 0x7e92) << 8);
2532 	set_desc_limit(&desc,       GET_SMSTATE(u32, smstate, 0x7e94));
2533 	set_desc_base(&desc,        GET_SMSTATE(u32, smstate, 0x7e98));
2534 	base3 =                     GET_SMSTATE(u32, smstate, 0x7e9c);
2535 	ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2536 
2537 	dt.size =                   GET_SMSTATE(u32, smstate, 0x7e84);
2538 	dt.address =                GET_SMSTATE(u64, smstate, 0x7e88);
2539 	ctxt->ops->set_idt(ctxt, &dt);
2540 
2541 	selector =                  GET_SMSTATE(u32, smstate, 0x7e70);
2542 	rsm_set_desc_flags(&desc,   GET_SMSTATE(u32, smstate, 0x7e72) << 8);
2543 	set_desc_limit(&desc,       GET_SMSTATE(u32, smstate, 0x7e74));
2544 	set_desc_base(&desc,        GET_SMSTATE(u32, smstate, 0x7e78));
2545 	base3 =                     GET_SMSTATE(u32, smstate, 0x7e7c);
2546 	ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2547 
2548 	dt.size =                   GET_SMSTATE(u32, smstate, 0x7e64);
2549 	dt.address =                GET_SMSTATE(u64, smstate, 0x7e68);
2550 	ctxt->ops->set_gdt(ctxt, &dt);
2551 
2552 	r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
2553 	if (r != X86EMUL_CONTINUE)
2554 		return r;
2555 
2556 	for (i = 0; i < 6; i++) {
2557 		r = rsm_load_seg_64(ctxt, smstate, i);
2558 		if (r != X86EMUL_CONTINUE)
2559 			return r;
2560 	}
2561 
2562 	return X86EMUL_CONTINUE;
2563 }
2564 #endif
2565 
em_rsm(struct x86_emulate_ctxt * ctxt)2566 static int em_rsm(struct x86_emulate_ctxt *ctxt)
2567 {
2568 	unsigned long cr0, cr4, efer;
2569 	char buf[512];
2570 	u64 smbase;
2571 	int ret;
2572 
2573 	if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
2574 		return emulate_ud(ctxt);
2575 
2576 	smbase = ctxt->ops->get_smbase(ctxt);
2577 
2578 	ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf));
2579 	if (ret != X86EMUL_CONTINUE)
2580 		return X86EMUL_UNHANDLEABLE;
2581 
2582 	if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
2583 		ctxt->ops->set_nmi_mask(ctxt, false);
2584 
2585 	ctxt->ops->exiting_smm(ctxt);
2586 
2587 	/*
2588 	 * Get back to real mode, to prepare a safe state in which to load
2589 	 * CR0/CR3/CR4/EFER.  It's all a bit more complicated if the vCPU
2590 	 * supports long mode.
2591 	 */
2592 	if (emulator_has_longmode(ctxt)) {
2593 		struct desc_struct cs_desc;
2594 
2595 		/* Zero CR4.PCIDE before CR0.PG.  */
2596 		cr4 = ctxt->ops->get_cr(ctxt, 4);
2597 		if (cr4 & X86_CR4_PCIDE)
2598 			ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2599 
2600 		/* A 32-bit code segment is required to clear EFER.LMA.  */
2601 		memset(&cs_desc, 0, sizeof(cs_desc));
2602 		cs_desc.type = 0xb;
2603 		cs_desc.s = cs_desc.g = cs_desc.p = 1;
2604 		ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2605 	}
2606 
2607 	/* For the 64-bit case, this will clear EFER.LMA.  */
2608 	cr0 = ctxt->ops->get_cr(ctxt, 0);
2609 	if (cr0 & X86_CR0_PE)
2610 		ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
2611 
2612 	if (emulator_has_longmode(ctxt)) {
2613 		/* Clear CR4.PAE before clearing EFER.LME. */
2614 		cr4 = ctxt->ops->get_cr(ctxt, 4);
2615 		if (cr4 & X86_CR4_PAE)
2616 			ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
2617 
2618 		/* And finally go back to 32-bit mode.  */
2619 		efer = 0;
2620 		ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2621 	}
2622 
2623 	/*
2624 	 * Give leave_smm() a chance to make ISA-specific changes to the vCPU
2625 	 * state (e.g. enter guest mode) before loading state from the SMM
2626 	 * state-save area.
2627 	 */
2628 	if (ctxt->ops->leave_smm(ctxt, buf))
2629 		goto emulate_shutdown;
2630 
2631 #ifdef CONFIG_X86_64
2632 	if (emulator_has_longmode(ctxt))
2633 		ret = rsm_load_state_64(ctxt, buf);
2634 	else
2635 #endif
2636 		ret = rsm_load_state_32(ctxt, buf);
2637 
2638 	if (ret != X86EMUL_CONTINUE)
2639 		goto emulate_shutdown;
2640 
2641 	/*
2642 	 * Note, the ctxt->ops callbacks are responsible for handling side
2643 	 * effects when writing MSRs and CRs, e.g. MMU context resets, CPUID
2644 	 * runtime updates, etc...  If that changes, e.g. this flow is moved
2645 	 * out of the emulator to make it look more like enter_smm(), then
2646 	 * those side effects need to be explicitly handled for both success
2647 	 * and shutdown.
2648 	 */
2649 	return emulator_recalc_and_set_mode(ctxt);
2650 
2651 emulate_shutdown:
2652 	ctxt->ops->triple_fault(ctxt);
2653 	return X86EMUL_CONTINUE;
2654 }
2655 
2656 static void
setup_syscalls_segments(struct x86_emulate_ctxt * ctxt,struct desc_struct * cs,struct desc_struct * ss)2657 setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
2658 			struct desc_struct *cs, struct desc_struct *ss)
2659 {
2660 	cs->l = 0;		/* will be adjusted later */
2661 	set_desc_base(cs, 0);	/* flat segment */
2662 	cs->g = 1;		/* 4kb granularity */
2663 	set_desc_limit(cs, 0xfffff);	/* 4GB limit */
2664 	cs->type = 0x0b;	/* Read, Execute, Accessed */
2665 	cs->s = 1;
2666 	cs->dpl = 0;		/* will be adjusted later */
2667 	cs->p = 1;
2668 	cs->d = 1;
2669 	cs->avl = 0;
2670 
2671 	set_desc_base(ss, 0);	/* flat segment */
2672 	set_desc_limit(ss, 0xfffff);	/* 4GB limit */
2673 	ss->g = 1;		/* 4kb granularity */
2674 	ss->s = 1;
2675 	ss->type = 0x03;	/* Read/Write, Accessed */
2676 	ss->d = 1;		/* 32bit stack segment */
2677 	ss->dpl = 0;
2678 	ss->p = 1;
2679 	ss->l = 0;
2680 	ss->avl = 0;
2681 }
2682 
vendor_intel(struct x86_emulate_ctxt * ctxt)2683 static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2684 {
2685 	u32 eax, ebx, ecx, edx;
2686 
2687 	eax = ecx = 0;
2688 	ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
2689 	return is_guest_vendor_intel(ebx, ecx, edx);
2690 }
2691 
em_syscall_is_enabled(struct x86_emulate_ctxt * ctxt)2692 static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2693 {
2694 	const struct x86_emulate_ops *ops = ctxt->ops;
2695 	u32 eax, ebx, ecx, edx;
2696 
2697 	/*
2698 	 * syscall should always be enabled in longmode - so only become
2699 	 * vendor specific (cpuid) if other modes are active...
2700 	 */
2701 	if (ctxt->mode == X86EMUL_MODE_PROT64)
2702 		return true;
2703 
2704 	eax = 0x00000000;
2705 	ecx = 0x00000000;
2706 	ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
2707 	/*
2708 	 * remark: Intel CPUs only support "syscall" in 64bit longmode. Also a
2709 	 * 64bit guest with a 32bit compat-app running will #UD !! While this
2710 	 * behaviour can be fixed (by emulating) into AMD response - CPUs of
2711 	 * AMD can't behave like Intel.
2712 	 */
2713 	if (is_guest_vendor_intel(ebx, ecx, edx))
2714 		return false;
2715 
2716 	if (is_guest_vendor_amd(ebx, ecx, edx) ||
2717 	    is_guest_vendor_hygon(ebx, ecx, edx))
2718 		return true;
2719 
2720 	/*
2721 	 * default: (not Intel, not AMD, not Hygon), apply Intel's
2722 	 * stricter rules...
2723 	 */
2724 	return false;
2725 }
2726 
em_syscall(struct x86_emulate_ctxt * ctxt)2727 static int em_syscall(struct x86_emulate_ctxt *ctxt)
2728 {
2729 	const struct x86_emulate_ops *ops = ctxt->ops;
2730 	struct desc_struct cs, ss;
2731 	u64 msr_data;
2732 	u16 cs_sel, ss_sel;
2733 	u64 efer = 0;
2734 
2735 	/* syscall is not available in real mode */
2736 	if (ctxt->mode == X86EMUL_MODE_REAL ||
2737 	    ctxt->mode == X86EMUL_MODE_VM86)
2738 		return emulate_ud(ctxt);
2739 
2740 	if (!(em_syscall_is_enabled(ctxt)))
2741 		return emulate_ud(ctxt);
2742 
2743 	ops->get_msr(ctxt, MSR_EFER, &efer);
2744 	if (!(efer & EFER_SCE))
2745 		return emulate_ud(ctxt);
2746 
2747 	setup_syscalls_segments(ctxt, &cs, &ss);
2748 	ops->get_msr(ctxt, MSR_STAR, &msr_data);
2749 	msr_data >>= 32;
2750 	cs_sel = (u16)(msr_data & 0xfffc);
2751 	ss_sel = (u16)(msr_data + 8);
2752 
2753 	if (efer & EFER_LMA) {
2754 		cs.d = 0;
2755 		cs.l = 1;
2756 	}
2757 	ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2758 	ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
2759 
2760 	*reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
2761 	if (efer & EFER_LMA) {
2762 #ifdef CONFIG_X86_64
2763 		*reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
2764 
2765 		ops->get_msr(ctxt,
2766 			     ctxt->mode == X86EMUL_MODE_PROT64 ?
2767 			     MSR_LSTAR : MSR_CSTAR, &msr_data);
2768 		ctxt->_eip = msr_data;
2769 
2770 		ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
2771 		ctxt->eflags &= ~msr_data;
2772 		ctxt->eflags |= X86_EFLAGS_FIXED;
2773 #endif
2774 	} else {
2775 		/* legacy mode */
2776 		ops->get_msr(ctxt, MSR_STAR, &msr_data);
2777 		ctxt->_eip = (u32)msr_data;
2778 
2779 		ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
2780 	}
2781 
2782 	ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
2783 	return X86EMUL_CONTINUE;
2784 }
2785 
em_sysenter(struct x86_emulate_ctxt * ctxt)2786 static int em_sysenter(struct x86_emulate_ctxt *ctxt)
2787 {
2788 	const struct x86_emulate_ops *ops = ctxt->ops;
2789 	struct desc_struct cs, ss;
2790 	u64 msr_data;
2791 	u16 cs_sel, ss_sel;
2792 	u64 efer = 0;
2793 
2794 	ops->get_msr(ctxt, MSR_EFER, &efer);
2795 	/* inject #GP if in real mode */
2796 	if (ctxt->mode == X86EMUL_MODE_REAL)
2797 		return emulate_gp(ctxt, 0);
2798 
2799 	/*
2800 	 * Not recognized on AMD in compat mode (but is recognized in legacy
2801 	 * mode).
2802 	 */
2803 	if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
2804 	    && !vendor_intel(ctxt))
2805 		return emulate_ud(ctxt);
2806 
2807 	/* sysenter/sysexit have not been tested in 64bit mode. */
2808 	if (ctxt->mode == X86EMUL_MODE_PROT64)
2809 		return X86EMUL_UNHANDLEABLE;
2810 
2811 	ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
2812 	if ((msr_data & 0xfffc) == 0x0)
2813 		return emulate_gp(ctxt, 0);
2814 
2815 	setup_syscalls_segments(ctxt, &cs, &ss);
2816 	ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
2817 	cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
2818 	ss_sel = cs_sel + 8;
2819 	if (efer & EFER_LMA) {
2820 		cs.d = 0;
2821 		cs.l = 1;
2822 	}
2823 
2824 	ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2825 	ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
2826 
2827 	ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
2828 	ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
2829 
2830 	ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
2831 	*reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2832 							      (u32)msr_data;
2833 	if (efer & EFER_LMA)
2834 		ctxt->mode = X86EMUL_MODE_PROT64;
2835 
2836 	return X86EMUL_CONTINUE;
2837 }
2838 
em_sysexit(struct x86_emulate_ctxt * ctxt)2839 static int em_sysexit(struct x86_emulate_ctxt *ctxt)
2840 {
2841 	const struct x86_emulate_ops *ops = ctxt->ops;
2842 	struct desc_struct cs, ss;
2843 	u64 msr_data, rcx, rdx;
2844 	int usermode;
2845 	u16 cs_sel = 0, ss_sel = 0;
2846 
2847 	/* inject #GP if in real mode or Virtual 8086 mode */
2848 	if (ctxt->mode == X86EMUL_MODE_REAL ||
2849 	    ctxt->mode == X86EMUL_MODE_VM86)
2850 		return emulate_gp(ctxt, 0);
2851 
2852 	setup_syscalls_segments(ctxt, &cs, &ss);
2853 
2854 	if ((ctxt->rex_prefix & 0x8) != 0x0)
2855 		usermode = X86EMUL_MODE_PROT64;
2856 	else
2857 		usermode = X86EMUL_MODE_PROT32;
2858 
2859 	rcx = reg_read(ctxt, VCPU_REGS_RCX);
2860 	rdx = reg_read(ctxt, VCPU_REGS_RDX);
2861 
2862 	cs.dpl = 3;
2863 	ss.dpl = 3;
2864 	ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
2865 	switch (usermode) {
2866 	case X86EMUL_MODE_PROT32:
2867 		cs_sel = (u16)(msr_data + 16);
2868 		if ((msr_data & 0xfffc) == 0x0)
2869 			return emulate_gp(ctxt, 0);
2870 		ss_sel = (u16)(msr_data + 24);
2871 		rcx = (u32)rcx;
2872 		rdx = (u32)rdx;
2873 		break;
2874 	case X86EMUL_MODE_PROT64:
2875 		cs_sel = (u16)(msr_data + 32);
2876 		if (msr_data == 0x0)
2877 			return emulate_gp(ctxt, 0);
2878 		ss_sel = cs_sel + 8;
2879 		cs.d = 0;
2880 		cs.l = 1;
2881 		if (emul_is_noncanonical_address(rcx, ctxt) ||
2882 		    emul_is_noncanonical_address(rdx, ctxt))
2883 			return emulate_gp(ctxt, 0);
2884 		break;
2885 	}
2886 	cs_sel |= SEGMENT_RPL_MASK;
2887 	ss_sel |= SEGMENT_RPL_MASK;
2888 
2889 	ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2890 	ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
2891 
2892 	ctxt->_eip = rdx;
2893 	ctxt->mode = usermode;
2894 	*reg_write(ctxt, VCPU_REGS_RSP) = rcx;
2895 
2896 	return X86EMUL_CONTINUE;
2897 }
2898 
emulator_bad_iopl(struct x86_emulate_ctxt * ctxt)2899 static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
2900 {
2901 	int iopl;
2902 	if (ctxt->mode == X86EMUL_MODE_REAL)
2903 		return false;
2904 	if (ctxt->mode == X86EMUL_MODE_VM86)
2905 		return true;
2906 	iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
2907 	return ctxt->ops->cpl(ctxt) > iopl;
2908 }
2909 
2910 #define VMWARE_PORT_VMPORT	(0x5658)
2911 #define VMWARE_PORT_VMRPC	(0x5659)
2912 
emulator_io_port_access_allowed(struct x86_emulate_ctxt * ctxt,u16 port,u16 len)2913 static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2914 					    u16 port, u16 len)
2915 {
2916 	const struct x86_emulate_ops *ops = ctxt->ops;
2917 	struct desc_struct tr_seg;
2918 	u32 base3;
2919 	int r;
2920 	u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
2921 	unsigned mask = (1 << len) - 1;
2922 	unsigned long base;
2923 
2924 	/*
2925 	 * VMware allows access to these ports even if denied
2926 	 * by TSS I/O permission bitmap. Mimic behavior.
2927 	 */
2928 	if (enable_vmware_backdoor &&
2929 	    ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
2930 		return true;
2931 
2932 	ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
2933 	if (!tr_seg.p)
2934 		return false;
2935 	if (desc_limit_scaled(&tr_seg) < 103)
2936 		return false;
2937 	base = get_desc_base(&tr_seg);
2938 #ifdef CONFIG_X86_64
2939 	base |= ((u64)base3) << 32;
2940 #endif
2941 	r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
2942 	if (r != X86EMUL_CONTINUE)
2943 		return false;
2944 	if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
2945 		return false;
2946 	r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
2947 	if (r != X86EMUL_CONTINUE)
2948 		return false;
2949 	if ((perm >> bit_idx) & mask)
2950 		return false;
2951 	return true;
2952 }
2953 
emulator_io_permited(struct x86_emulate_ctxt * ctxt,u16 port,u16 len)2954 static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2955 				 u16 port, u16 len)
2956 {
2957 	if (ctxt->perm_ok)
2958 		return true;
2959 
2960 	if (emulator_bad_iopl(ctxt))
2961 		if (!emulator_io_port_access_allowed(ctxt, port, len))
2962 			return false;
2963 
2964 	ctxt->perm_ok = true;
2965 
2966 	return true;
2967 }
2968 
string_registers_quirk(struct x86_emulate_ctxt * ctxt)2969 static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2970 {
2971 	/*
2972 	 * Intel CPUs mask the counter and pointers in quite strange
2973 	 * manner when ECX is zero due to REP-string optimizations.
2974 	 */
2975 #ifdef CONFIG_X86_64
2976 	if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2977 		return;
2978 
2979 	*reg_write(ctxt, VCPU_REGS_RCX) = 0;
2980 
2981 	switch (ctxt->b) {
2982 	case 0xa4:	/* movsb */
2983 	case 0xa5:	/* movsd/w */
2984 		*reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
2985 		fallthrough;
2986 	case 0xaa:	/* stosb */
2987 	case 0xab:	/* stosd/w */
2988 		*reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2989 	}
2990 #endif
2991 }
2992 
save_state_to_tss16(struct x86_emulate_ctxt * ctxt,struct tss_segment_16 * tss)2993 static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2994 				struct tss_segment_16 *tss)
2995 {
2996 	tss->ip = ctxt->_eip;
2997 	tss->flag = ctxt->eflags;
2998 	tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2999 	tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
3000 	tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
3001 	tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
3002 	tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
3003 	tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
3004 	tss->si = reg_read(ctxt, VCPU_REGS_RSI);
3005 	tss->di = reg_read(ctxt, VCPU_REGS_RDI);
3006 
3007 	tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3008 	tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3009 	tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3010 	tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3011 	tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
3012 }
3013 
load_state_from_tss16(struct x86_emulate_ctxt * ctxt,struct tss_segment_16 * tss)3014 static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
3015 				 struct tss_segment_16 *tss)
3016 {
3017 	int ret;
3018 	u8 cpl;
3019 
3020 	ctxt->_eip = tss->ip;
3021 	ctxt->eflags = tss->flag | 2;
3022 	*reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
3023 	*reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
3024 	*reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
3025 	*reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
3026 	*reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
3027 	*reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
3028 	*reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
3029 	*reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
3030 
3031 	/*
3032 	 * SDM says that segment selectors are loaded before segment
3033 	 * descriptors
3034 	 */
3035 	set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
3036 	set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3037 	set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3038 	set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3039 	set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3040 
3041 	cpl = tss->cs & 3;
3042 
3043 	/*
3044 	 * Now load segment descriptors. If fault happens at this stage
3045 	 * it is handled in a context of new task
3046 	 */
3047 	ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
3048 					X86_TRANSFER_TASK_SWITCH, NULL);
3049 	if (ret != X86EMUL_CONTINUE)
3050 		return ret;
3051 	ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
3052 					X86_TRANSFER_TASK_SWITCH, NULL);
3053 	if (ret != X86EMUL_CONTINUE)
3054 		return ret;
3055 	ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
3056 					X86_TRANSFER_TASK_SWITCH, NULL);
3057 	if (ret != X86EMUL_CONTINUE)
3058 		return ret;
3059 	ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
3060 					X86_TRANSFER_TASK_SWITCH, NULL);
3061 	if (ret != X86EMUL_CONTINUE)
3062 		return ret;
3063 	ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
3064 					X86_TRANSFER_TASK_SWITCH, NULL);
3065 	if (ret != X86EMUL_CONTINUE)
3066 		return ret;
3067 
3068 	return X86EMUL_CONTINUE;
3069 }
3070 
task_switch_16(struct x86_emulate_ctxt * ctxt,u16 tss_selector,u16 old_tss_sel,ulong old_tss_base,struct desc_struct * new_desc)3071 static int task_switch_16(struct x86_emulate_ctxt *ctxt,
3072 			  u16 tss_selector, u16 old_tss_sel,
3073 			  ulong old_tss_base, struct desc_struct *new_desc)
3074 {
3075 	struct tss_segment_16 tss_seg;
3076 	int ret;
3077 	u32 new_tss_base = get_desc_base(new_desc);
3078 
3079 	ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
3080 	if (ret != X86EMUL_CONTINUE)
3081 		return ret;
3082 
3083 	save_state_to_tss16(ctxt, &tss_seg);
3084 
3085 	ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
3086 	if (ret != X86EMUL_CONTINUE)
3087 		return ret;
3088 
3089 	ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
3090 	if (ret != X86EMUL_CONTINUE)
3091 		return ret;
3092 
3093 	if (old_tss_sel != 0xffff) {
3094 		tss_seg.prev_task_link = old_tss_sel;
3095 
3096 		ret = linear_write_system(ctxt, new_tss_base,
3097 					  &tss_seg.prev_task_link,
3098 					  sizeof(tss_seg.prev_task_link));
3099 		if (ret != X86EMUL_CONTINUE)
3100 			return ret;
3101 	}
3102 
3103 	return load_state_from_tss16(ctxt, &tss_seg);
3104 }
3105 
save_state_to_tss32(struct x86_emulate_ctxt * ctxt,struct tss_segment_32 * tss)3106 static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
3107 				struct tss_segment_32 *tss)
3108 {
3109 	/* CR3 and ldt selector are not saved intentionally */
3110 	tss->eip = ctxt->_eip;
3111 	tss->eflags = ctxt->eflags;
3112 	tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3113 	tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3114 	tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3115 	tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3116 	tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3117 	tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3118 	tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3119 	tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
3120 
3121 	tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3122 	tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3123 	tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3124 	tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3125 	tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3126 	tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
3127 }
3128 
load_state_from_tss32(struct x86_emulate_ctxt * ctxt,struct tss_segment_32 * tss)3129 static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
3130 				 struct tss_segment_32 *tss)
3131 {
3132 	int ret;
3133 	u8 cpl;
3134 
3135 	if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
3136 		return emulate_gp(ctxt, 0);
3137 	ctxt->_eip = tss->eip;
3138 	ctxt->eflags = tss->eflags | 2;
3139 
3140 	/* General purpose registers */
3141 	*reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3142 	*reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3143 	*reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3144 	*reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3145 	*reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3146 	*reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3147 	*reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3148 	*reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
3149 
3150 	/*
3151 	 * SDM says that segment selectors are loaded before segment
3152 	 * descriptors.  This is important because CPL checks will
3153 	 * use CS.RPL.
3154 	 */
3155 	set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3156 	set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3157 	set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3158 	set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3159 	set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3160 	set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3161 	set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
3162 
3163 	/*
3164 	 * If we're switching between Protected Mode and VM86, we need to make
3165 	 * sure to update the mode before loading the segment descriptors so
3166 	 * that the selectors are interpreted correctly.
3167 	 */
3168 	if (ctxt->eflags & X86_EFLAGS_VM) {
3169 		ctxt->mode = X86EMUL_MODE_VM86;
3170 		cpl = 3;
3171 	} else {
3172 		ctxt->mode = X86EMUL_MODE_PROT32;
3173 		cpl = tss->cs & 3;
3174 	}
3175 
3176 	/*
3177 	 * Now load segment descriptors. If fault happens at this stage
3178 	 * it is handled in a context of new task
3179 	 */
3180 	ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
3181 					cpl, X86_TRANSFER_TASK_SWITCH, NULL);
3182 	if (ret != X86EMUL_CONTINUE)
3183 		return ret;
3184 	ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
3185 					X86_TRANSFER_TASK_SWITCH, NULL);
3186 	if (ret != X86EMUL_CONTINUE)
3187 		return ret;
3188 	ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
3189 					X86_TRANSFER_TASK_SWITCH, NULL);
3190 	if (ret != X86EMUL_CONTINUE)
3191 		return ret;
3192 	ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
3193 					X86_TRANSFER_TASK_SWITCH, NULL);
3194 	if (ret != X86EMUL_CONTINUE)
3195 		return ret;
3196 	ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
3197 					X86_TRANSFER_TASK_SWITCH, NULL);
3198 	if (ret != X86EMUL_CONTINUE)
3199 		return ret;
3200 	ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
3201 					X86_TRANSFER_TASK_SWITCH, NULL);
3202 	if (ret != X86EMUL_CONTINUE)
3203 		return ret;
3204 	ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
3205 					X86_TRANSFER_TASK_SWITCH, NULL);
3206 
3207 	return ret;
3208 }
3209 
task_switch_32(struct x86_emulate_ctxt * ctxt,u16 tss_selector,u16 old_tss_sel,ulong old_tss_base,struct desc_struct * new_desc)3210 static int task_switch_32(struct x86_emulate_ctxt *ctxt,
3211 			  u16 tss_selector, u16 old_tss_sel,
3212 			  ulong old_tss_base, struct desc_struct *new_desc)
3213 {
3214 	struct tss_segment_32 tss_seg;
3215 	int ret;
3216 	u32 new_tss_base = get_desc_base(new_desc);
3217 	u32 eip_offset = offsetof(struct tss_segment_32, eip);
3218 	u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
3219 
3220 	ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof(tss_seg));
3221 	if (ret != X86EMUL_CONTINUE)
3222 		return ret;
3223 
3224 	save_state_to_tss32(ctxt, &tss_seg);
3225 
3226 	/* Only GP registers and segment selectors are saved */
3227 	ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3228 				  ldt_sel_offset - eip_offset);
3229 	if (ret != X86EMUL_CONTINUE)
3230 		return ret;
3231 
3232 	ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof(tss_seg));
3233 	if (ret != X86EMUL_CONTINUE)
3234 		return ret;
3235 
3236 	if (old_tss_sel != 0xffff) {
3237 		tss_seg.prev_task_link = old_tss_sel;
3238 
3239 		ret = linear_write_system(ctxt, new_tss_base,
3240 					  &tss_seg.prev_task_link,
3241 					  sizeof(tss_seg.prev_task_link));
3242 		if (ret != X86EMUL_CONTINUE)
3243 			return ret;
3244 	}
3245 
3246 	return load_state_from_tss32(ctxt, &tss_seg);
3247 }
3248 
emulator_do_task_switch(struct x86_emulate_ctxt * ctxt,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code)3249 static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
3250 				   u16 tss_selector, int idt_index, int reason,
3251 				   bool has_error_code, u32 error_code)
3252 {
3253 	const struct x86_emulate_ops *ops = ctxt->ops;
3254 	struct desc_struct curr_tss_desc, next_tss_desc;
3255 	int ret;
3256 	u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
3257 	ulong old_tss_base =
3258 		ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
3259 	u32 desc_limit;
3260 	ulong desc_addr, dr7;
3261 
3262 	/* FIXME: old_tss_base == ~0 ? */
3263 
3264 	ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
3265 	if (ret != X86EMUL_CONTINUE)
3266 		return ret;
3267 	ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
3268 	if (ret != X86EMUL_CONTINUE)
3269 		return ret;
3270 
3271 	/* FIXME: check that next_tss_desc is tss */
3272 
3273 	/*
3274 	 * Check privileges. The three cases are task switch caused by...
3275 	 *
3276 	 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3277 	 * 2. Exception/IRQ/iret: No check is performed
3278 	 * 3. jmp/call to TSS/task-gate: No check is performed since the
3279 	 *    hardware checks it before exiting.
3280 	 */
3281 	if (reason == TASK_SWITCH_GATE) {
3282 		if (idt_index != -1) {
3283 			/* Software interrupts */
3284 			struct desc_struct task_gate_desc;
3285 			int dpl;
3286 
3287 			ret = read_interrupt_descriptor(ctxt, idt_index,
3288 							&task_gate_desc);
3289 			if (ret != X86EMUL_CONTINUE)
3290 				return ret;
3291 
3292 			dpl = task_gate_desc.dpl;
3293 			if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3294 				return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3295 		}
3296 	}
3297 
3298 	desc_limit = desc_limit_scaled(&next_tss_desc);
3299 	if (!next_tss_desc.p ||
3300 	    ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3301 	     desc_limit < 0x2b)) {
3302 		return emulate_ts(ctxt, tss_selector & 0xfffc);
3303 	}
3304 
3305 	if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3306 		curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
3307 		write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
3308 	}
3309 
3310 	if (reason == TASK_SWITCH_IRET)
3311 		ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3312 
3313 	/* set back link to prev task only if NT bit is set in eflags
3314 	   note that old_tss_sel is not used after this point */
3315 	if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3316 		old_tss_sel = 0xffff;
3317 
3318 	if (next_tss_desc.type & 8)
3319 		ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
3320 				     old_tss_base, &next_tss_desc);
3321 	else
3322 		ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
3323 				     old_tss_base, &next_tss_desc);
3324 	if (ret != X86EMUL_CONTINUE)
3325 		return ret;
3326 
3327 	if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3328 		ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3329 
3330 	if (reason != TASK_SWITCH_IRET) {
3331 		next_tss_desc.type |= (1 << 1); /* set busy flag */
3332 		write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
3333 	}
3334 
3335 	ops->set_cr(ctxt, 0,  ops->get_cr(ctxt, 0) | X86_CR0_TS);
3336 	ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
3337 
3338 	if (has_error_code) {
3339 		ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3340 		ctxt->lock_prefix = 0;
3341 		ctxt->src.val = (unsigned long) error_code;
3342 		ret = em_push(ctxt);
3343 	}
3344 
3345 	ops->get_dr(ctxt, 7, &dr7);
3346 	ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3347 
3348 	return ret;
3349 }
3350 
emulator_task_switch(struct x86_emulate_ctxt * ctxt,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code)3351 int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
3352 			 u16 tss_selector, int idt_index, int reason,
3353 			 bool has_error_code, u32 error_code)
3354 {
3355 	int rc;
3356 
3357 	invalidate_registers(ctxt);
3358 	ctxt->_eip = ctxt->eip;
3359 	ctxt->dst.type = OP_NONE;
3360 
3361 	rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
3362 				     has_error_code, error_code);
3363 
3364 	if (rc == X86EMUL_CONTINUE) {
3365 		ctxt->eip = ctxt->_eip;
3366 		writeback_registers(ctxt);
3367 	}
3368 
3369 	return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
3370 }
3371 
string_addr_inc(struct x86_emulate_ctxt * ctxt,int reg,struct operand * op)3372 static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3373 		struct operand *op)
3374 {
3375 	int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
3376 
3377 	register_address_increment(ctxt, reg, df * op->bytes);
3378 	op->addr.mem.ea = register_address(ctxt, reg);
3379 }
3380 
em_das(struct x86_emulate_ctxt * ctxt)3381 static int em_das(struct x86_emulate_ctxt *ctxt)
3382 {
3383 	u8 al, old_al;
3384 	bool af, cf, old_cf;
3385 
3386 	cf = ctxt->eflags & X86_EFLAGS_CF;
3387 	al = ctxt->dst.val;
3388 
3389 	old_al = al;
3390 	old_cf = cf;
3391 	cf = false;
3392 	af = ctxt->eflags & X86_EFLAGS_AF;
3393 	if ((al & 0x0f) > 9 || af) {
3394 		al -= 6;
3395 		cf = old_cf | (al >= 250);
3396 		af = true;
3397 	} else {
3398 		af = false;
3399 	}
3400 	if (old_al > 0x99 || old_cf) {
3401 		al -= 0x60;
3402 		cf = true;
3403 	}
3404 
3405 	ctxt->dst.val = al;
3406 	/* Set PF, ZF, SF */
3407 	ctxt->src.type = OP_IMM;
3408 	ctxt->src.val = 0;
3409 	ctxt->src.bytes = 1;
3410 	fastop(ctxt, em_or);
3411 	ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3412 	if (cf)
3413 		ctxt->eflags |= X86_EFLAGS_CF;
3414 	if (af)
3415 		ctxt->eflags |= X86_EFLAGS_AF;
3416 	return X86EMUL_CONTINUE;
3417 }
3418 
em_aam(struct x86_emulate_ctxt * ctxt)3419 static int em_aam(struct x86_emulate_ctxt *ctxt)
3420 {
3421 	u8 al, ah;
3422 
3423 	if (ctxt->src.val == 0)
3424 		return emulate_de(ctxt);
3425 
3426 	al = ctxt->dst.val & 0xff;
3427 	ah = al / ctxt->src.val;
3428 	al %= ctxt->src.val;
3429 
3430 	ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3431 
3432 	/* Set PF, ZF, SF */
3433 	ctxt->src.type = OP_IMM;
3434 	ctxt->src.val = 0;
3435 	ctxt->src.bytes = 1;
3436 	fastop(ctxt, em_or);
3437 
3438 	return X86EMUL_CONTINUE;
3439 }
3440 
em_aad(struct x86_emulate_ctxt * ctxt)3441 static int em_aad(struct x86_emulate_ctxt *ctxt)
3442 {
3443 	u8 al = ctxt->dst.val & 0xff;
3444 	u8 ah = (ctxt->dst.val >> 8) & 0xff;
3445 
3446 	al = (al + (ah * ctxt->src.val)) & 0xff;
3447 
3448 	ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3449 
3450 	/* Set PF, ZF, SF */
3451 	ctxt->src.type = OP_IMM;
3452 	ctxt->src.val = 0;
3453 	ctxt->src.bytes = 1;
3454 	fastop(ctxt, em_or);
3455 
3456 	return X86EMUL_CONTINUE;
3457 }
3458 
em_call(struct x86_emulate_ctxt * ctxt)3459 static int em_call(struct x86_emulate_ctxt *ctxt)
3460 {
3461 	int rc;
3462 	long rel = ctxt->src.val;
3463 
3464 	ctxt->src.val = (unsigned long)ctxt->_eip;
3465 	rc = jmp_rel(ctxt, rel);
3466 	if (rc != X86EMUL_CONTINUE)
3467 		return rc;
3468 	return em_push(ctxt);
3469 }
3470 
em_call_far(struct x86_emulate_ctxt * ctxt)3471 static int em_call_far(struct x86_emulate_ctxt *ctxt)
3472 {
3473 	u16 sel, old_cs;
3474 	ulong old_eip;
3475 	int rc;
3476 	struct desc_struct old_desc, new_desc;
3477 	const struct x86_emulate_ops *ops = ctxt->ops;
3478 	int cpl = ctxt->ops->cpl(ctxt);
3479 	enum x86emul_mode prev_mode = ctxt->mode;
3480 
3481 	old_eip = ctxt->_eip;
3482 	ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
3483 
3484 	memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
3485 	rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3486 				       X86_TRANSFER_CALL_JMP, &new_desc);
3487 	if (rc != X86EMUL_CONTINUE)
3488 		return rc;
3489 
3490 	rc = assign_eip_far(ctxt, ctxt->src.val);
3491 	if (rc != X86EMUL_CONTINUE)
3492 		goto fail;
3493 
3494 	ctxt->src.val = old_cs;
3495 	rc = em_push(ctxt);
3496 	if (rc != X86EMUL_CONTINUE)
3497 		goto fail;
3498 
3499 	ctxt->src.val = old_eip;
3500 	rc = em_push(ctxt);
3501 	/* If we failed, we tainted the memory, but the very least we should
3502 	   restore cs */
3503 	if (rc != X86EMUL_CONTINUE) {
3504 		pr_warn_once("faulting far call emulation tainted memory\n");
3505 		goto fail;
3506 	}
3507 	return rc;
3508 fail:
3509 	ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
3510 	ctxt->mode = prev_mode;
3511 	return rc;
3512 
3513 }
3514 
em_ret_near_imm(struct x86_emulate_ctxt * ctxt)3515 static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3516 {
3517 	int rc;
3518 	unsigned long eip;
3519 
3520 	rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3521 	if (rc != X86EMUL_CONTINUE)
3522 		return rc;
3523 	rc = assign_eip_near(ctxt, eip);
3524 	if (rc != X86EMUL_CONTINUE)
3525 		return rc;
3526 	rsp_increment(ctxt, ctxt->src.val);
3527 	return X86EMUL_CONTINUE;
3528 }
3529 
em_xchg(struct x86_emulate_ctxt * ctxt)3530 static int em_xchg(struct x86_emulate_ctxt *ctxt)
3531 {
3532 	/* Write back the register source. */
3533 	ctxt->src.val = ctxt->dst.val;
3534 	write_register_operand(&ctxt->src);
3535 
3536 	/* Write back the memory destination with implicit LOCK prefix. */
3537 	ctxt->dst.val = ctxt->src.orig_val;
3538 	ctxt->lock_prefix = 1;
3539 	return X86EMUL_CONTINUE;
3540 }
3541 
em_imul_3op(struct x86_emulate_ctxt * ctxt)3542 static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3543 {
3544 	ctxt->dst.val = ctxt->src2.val;
3545 	return fastop(ctxt, em_imul);
3546 }
3547 
em_cwd(struct x86_emulate_ctxt * ctxt)3548 static int em_cwd(struct x86_emulate_ctxt *ctxt)
3549 {
3550 	ctxt->dst.type = OP_REG;
3551 	ctxt->dst.bytes = ctxt->src.bytes;
3552 	ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
3553 	ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
3554 
3555 	return X86EMUL_CONTINUE;
3556 }
3557 
em_rdpid(struct x86_emulate_ctxt * ctxt)3558 static int em_rdpid(struct x86_emulate_ctxt *ctxt)
3559 {
3560 	u64 tsc_aux = 0;
3561 
3562 	if (!ctxt->ops->guest_has_rdpid(ctxt))
3563 		return emulate_ud(ctxt);
3564 
3565 	ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux);
3566 	ctxt->dst.val = tsc_aux;
3567 	return X86EMUL_CONTINUE;
3568 }
3569 
em_rdtsc(struct x86_emulate_ctxt * ctxt)3570 static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3571 {
3572 	u64 tsc = 0;
3573 
3574 	ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
3575 	*reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3576 	*reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
3577 	return X86EMUL_CONTINUE;
3578 }
3579 
em_rdpmc(struct x86_emulate_ctxt * ctxt)3580 static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3581 {
3582 	u64 pmc;
3583 
3584 	if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
3585 		return emulate_gp(ctxt, 0);
3586 	*reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3587 	*reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
3588 	return X86EMUL_CONTINUE;
3589 }
3590 
em_mov(struct x86_emulate_ctxt * ctxt)3591 static int em_mov(struct x86_emulate_ctxt *ctxt)
3592 {
3593 	memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
3594 	return X86EMUL_CONTINUE;
3595 }
3596 
em_movbe(struct x86_emulate_ctxt * ctxt)3597 static int em_movbe(struct x86_emulate_ctxt *ctxt)
3598 {
3599 	u16 tmp;
3600 
3601 	if (!ctxt->ops->guest_has_movbe(ctxt))
3602 		return emulate_ud(ctxt);
3603 
3604 	switch (ctxt->op_bytes) {
3605 	case 2:
3606 		/*
3607 		 * From MOVBE definition: "...When the operand size is 16 bits,
3608 		 * the upper word of the destination register remains unchanged
3609 		 * ..."
3610 		 *
3611 		 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3612 		 * rules so we have to do the operation almost per hand.
3613 		 */
3614 		tmp = (u16)ctxt->src.val;
3615 		ctxt->dst.val &= ~0xffffUL;
3616 		ctxt->dst.val |= (unsigned long)swab16(tmp);
3617 		break;
3618 	case 4:
3619 		ctxt->dst.val = swab32((u32)ctxt->src.val);
3620 		break;
3621 	case 8:
3622 		ctxt->dst.val = swab64(ctxt->src.val);
3623 		break;
3624 	default:
3625 		BUG();
3626 	}
3627 	return X86EMUL_CONTINUE;
3628 }
3629 
em_cr_write(struct x86_emulate_ctxt * ctxt)3630 static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3631 {
3632 	int cr_num = ctxt->modrm_reg;
3633 	int r;
3634 
3635 	if (ctxt->ops->set_cr(ctxt, cr_num, ctxt->src.val))
3636 		return emulate_gp(ctxt, 0);
3637 
3638 	/* Disable writeback. */
3639 	ctxt->dst.type = OP_NONE;
3640 
3641 	if (cr_num == 0) {
3642 		/*
3643 		 * CR0 write might have updated CR0.PE and/or CR0.PG
3644 		 * which can affect the cpu's execution mode.
3645 		 */
3646 		r = emulator_recalc_and_set_mode(ctxt);
3647 		if (r != X86EMUL_CONTINUE)
3648 			return r;
3649 	}
3650 
3651 	return X86EMUL_CONTINUE;
3652 }
3653 
em_dr_write(struct x86_emulate_ctxt * ctxt)3654 static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3655 {
3656 	unsigned long val;
3657 
3658 	if (ctxt->mode == X86EMUL_MODE_PROT64)
3659 		val = ctxt->src.val & ~0ULL;
3660 	else
3661 		val = ctxt->src.val & ~0U;
3662 
3663 	/* #UD condition is already handled. */
3664 	if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3665 		return emulate_gp(ctxt, 0);
3666 
3667 	/* Disable writeback. */
3668 	ctxt->dst.type = OP_NONE;
3669 	return X86EMUL_CONTINUE;
3670 }
3671 
em_wrmsr(struct x86_emulate_ctxt * ctxt)3672 static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3673 {
3674 	u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
3675 	u64 msr_data;
3676 	int r;
3677 
3678 	msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3679 		| ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3680 	r = ctxt->ops->set_msr(ctxt, msr_index, msr_data);
3681 
3682 	if (r == X86EMUL_IO_NEEDED)
3683 		return r;
3684 
3685 	if (r > 0)
3686 		return emulate_gp(ctxt, 0);
3687 
3688 	return r < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
3689 }
3690 
em_rdmsr(struct x86_emulate_ctxt * ctxt)3691 static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3692 {
3693 	u64 msr_index = reg_read(ctxt, VCPU_REGS_RCX);
3694 	u64 msr_data;
3695 	int r;
3696 
3697 	r = ctxt->ops->get_msr(ctxt, msr_index, &msr_data);
3698 
3699 	if (r == X86EMUL_IO_NEEDED)
3700 		return r;
3701 
3702 	if (r)
3703 		return emulate_gp(ctxt, 0);
3704 
3705 	*reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3706 	*reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
3707 	return X86EMUL_CONTINUE;
3708 }
3709 
em_store_sreg(struct x86_emulate_ctxt * ctxt,int segment)3710 static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
3711 {
3712 	if (segment > VCPU_SREG_GS &&
3713 	    (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3714 	    ctxt->ops->cpl(ctxt) > 0)
3715 		return emulate_gp(ctxt, 0);
3716 
3717 	ctxt->dst.val = get_segment_selector(ctxt, segment);
3718 	if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3719 		ctxt->dst.bytes = 2;
3720 	return X86EMUL_CONTINUE;
3721 }
3722 
em_mov_rm_sreg(struct x86_emulate_ctxt * ctxt)3723 static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3724 {
3725 	if (ctxt->modrm_reg > VCPU_SREG_GS)
3726 		return emulate_ud(ctxt);
3727 
3728 	return em_store_sreg(ctxt, ctxt->modrm_reg);
3729 }
3730 
em_mov_sreg_rm(struct x86_emulate_ctxt * ctxt)3731 static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3732 {
3733 	u16 sel = ctxt->src.val;
3734 
3735 	if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
3736 		return emulate_ud(ctxt);
3737 
3738 	if (ctxt->modrm_reg == VCPU_SREG_SS)
3739 		ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3740 
3741 	/* Disable writeback. */
3742 	ctxt->dst.type = OP_NONE;
3743 	return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
3744 }
3745 
em_sldt(struct x86_emulate_ctxt * ctxt)3746 static int em_sldt(struct x86_emulate_ctxt *ctxt)
3747 {
3748 	return em_store_sreg(ctxt, VCPU_SREG_LDTR);
3749 }
3750 
em_lldt(struct x86_emulate_ctxt * ctxt)3751 static int em_lldt(struct x86_emulate_ctxt *ctxt)
3752 {
3753 	u16 sel = ctxt->src.val;
3754 
3755 	/* Disable writeback. */
3756 	ctxt->dst.type = OP_NONE;
3757 	return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3758 }
3759 
em_str(struct x86_emulate_ctxt * ctxt)3760 static int em_str(struct x86_emulate_ctxt *ctxt)
3761 {
3762 	return em_store_sreg(ctxt, VCPU_SREG_TR);
3763 }
3764 
em_ltr(struct x86_emulate_ctxt * ctxt)3765 static int em_ltr(struct x86_emulate_ctxt *ctxt)
3766 {
3767 	u16 sel = ctxt->src.val;
3768 
3769 	/* Disable writeback. */
3770 	ctxt->dst.type = OP_NONE;
3771 	return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3772 }
3773 
em_invlpg(struct x86_emulate_ctxt * ctxt)3774 static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3775 {
3776 	int rc;
3777 	ulong linear;
3778 
3779 	rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
3780 	if (rc == X86EMUL_CONTINUE)
3781 		ctxt->ops->invlpg(ctxt, linear);
3782 	/* Disable writeback. */
3783 	ctxt->dst.type = OP_NONE;
3784 	return X86EMUL_CONTINUE;
3785 }
3786 
em_clts(struct x86_emulate_ctxt * ctxt)3787 static int em_clts(struct x86_emulate_ctxt *ctxt)
3788 {
3789 	ulong cr0;
3790 
3791 	cr0 = ctxt->ops->get_cr(ctxt, 0);
3792 	cr0 &= ~X86_CR0_TS;
3793 	ctxt->ops->set_cr(ctxt, 0, cr0);
3794 	return X86EMUL_CONTINUE;
3795 }
3796 
em_hypercall(struct x86_emulate_ctxt * ctxt)3797 static int em_hypercall(struct x86_emulate_ctxt *ctxt)
3798 {
3799 	int rc = ctxt->ops->fix_hypercall(ctxt);
3800 
3801 	if (rc != X86EMUL_CONTINUE)
3802 		return rc;
3803 
3804 	/* Let the processor re-execute the fixed hypercall */
3805 	ctxt->_eip = ctxt->eip;
3806 	/* Disable writeback. */
3807 	ctxt->dst.type = OP_NONE;
3808 	return X86EMUL_CONTINUE;
3809 }
3810 
emulate_store_desc_ptr(struct x86_emulate_ctxt * ctxt,void (* get)(struct x86_emulate_ctxt * ctxt,struct desc_ptr * ptr))3811 static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3812 				  void (*get)(struct x86_emulate_ctxt *ctxt,
3813 					      struct desc_ptr *ptr))
3814 {
3815 	struct desc_ptr desc_ptr;
3816 
3817 	if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3818 	    ctxt->ops->cpl(ctxt) > 0)
3819 		return emulate_gp(ctxt, 0);
3820 
3821 	if (ctxt->mode == X86EMUL_MODE_PROT64)
3822 		ctxt->op_bytes = 8;
3823 	get(ctxt, &desc_ptr);
3824 	if (ctxt->op_bytes == 2) {
3825 		ctxt->op_bytes = 4;
3826 		desc_ptr.address &= 0x00ffffff;
3827 	}
3828 	/* Disable writeback. */
3829 	ctxt->dst.type = OP_NONE;
3830 	return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3831 				   &desc_ptr, 2 + ctxt->op_bytes);
3832 }
3833 
em_sgdt(struct x86_emulate_ctxt * ctxt)3834 static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3835 {
3836 	return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3837 }
3838 
em_sidt(struct x86_emulate_ctxt * ctxt)3839 static int em_sidt(struct x86_emulate_ctxt *ctxt)
3840 {
3841 	return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3842 }
3843 
em_lgdt_lidt(struct x86_emulate_ctxt * ctxt,bool lgdt)3844 static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
3845 {
3846 	struct desc_ptr desc_ptr;
3847 	int rc;
3848 
3849 	if (ctxt->mode == X86EMUL_MODE_PROT64)
3850 		ctxt->op_bytes = 8;
3851 	rc = read_descriptor(ctxt, ctxt->src.addr.mem,
3852 			     &desc_ptr.size, &desc_ptr.address,
3853 			     ctxt->op_bytes);
3854 	if (rc != X86EMUL_CONTINUE)
3855 		return rc;
3856 	if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3857 	    emul_is_noncanonical_address(desc_ptr.address, ctxt))
3858 		return emulate_gp(ctxt, 0);
3859 	if (lgdt)
3860 		ctxt->ops->set_gdt(ctxt, &desc_ptr);
3861 	else
3862 		ctxt->ops->set_idt(ctxt, &desc_ptr);
3863 	/* Disable writeback. */
3864 	ctxt->dst.type = OP_NONE;
3865 	return X86EMUL_CONTINUE;
3866 }
3867 
em_lgdt(struct x86_emulate_ctxt * ctxt)3868 static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3869 {
3870 	return em_lgdt_lidt(ctxt, true);
3871 }
3872 
em_lidt(struct x86_emulate_ctxt * ctxt)3873 static int em_lidt(struct x86_emulate_ctxt *ctxt)
3874 {
3875 	return em_lgdt_lidt(ctxt, false);
3876 }
3877 
em_smsw(struct x86_emulate_ctxt * ctxt)3878 static int em_smsw(struct x86_emulate_ctxt *ctxt)
3879 {
3880 	if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
3881 	    ctxt->ops->cpl(ctxt) > 0)
3882 		return emulate_gp(ctxt, 0);
3883 
3884 	if (ctxt->dst.type == OP_MEM)
3885 		ctxt->dst.bytes = 2;
3886 	ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
3887 	return X86EMUL_CONTINUE;
3888 }
3889 
em_lmsw(struct x86_emulate_ctxt * ctxt)3890 static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3891 {
3892 	ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
3893 			  | (ctxt->src.val & 0x0f));
3894 	ctxt->dst.type = OP_NONE;
3895 	return X86EMUL_CONTINUE;
3896 }
3897 
em_loop(struct x86_emulate_ctxt * ctxt)3898 static int em_loop(struct x86_emulate_ctxt *ctxt)
3899 {
3900 	int rc = X86EMUL_CONTINUE;
3901 
3902 	register_address_increment(ctxt, VCPU_REGS_RCX, -1);
3903 	if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
3904 	    (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3905 		rc = jmp_rel(ctxt, ctxt->src.val);
3906 
3907 	return rc;
3908 }
3909 
em_jcxz(struct x86_emulate_ctxt * ctxt)3910 static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3911 {
3912 	int rc = X86EMUL_CONTINUE;
3913 
3914 	if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3915 		rc = jmp_rel(ctxt, ctxt->src.val);
3916 
3917 	return rc;
3918 }
3919 
em_in(struct x86_emulate_ctxt * ctxt)3920 static int em_in(struct x86_emulate_ctxt *ctxt)
3921 {
3922 	if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3923 			     &ctxt->dst.val))
3924 		return X86EMUL_IO_NEEDED;
3925 
3926 	return X86EMUL_CONTINUE;
3927 }
3928 
em_out(struct x86_emulate_ctxt * ctxt)3929 static int em_out(struct x86_emulate_ctxt *ctxt)
3930 {
3931 	ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3932 				    &ctxt->src.val, 1);
3933 	/* Disable writeback. */
3934 	ctxt->dst.type = OP_NONE;
3935 	return X86EMUL_CONTINUE;
3936 }
3937 
em_cli(struct x86_emulate_ctxt * ctxt)3938 static int em_cli(struct x86_emulate_ctxt *ctxt)
3939 {
3940 	if (emulator_bad_iopl(ctxt))
3941 		return emulate_gp(ctxt, 0);
3942 
3943 	ctxt->eflags &= ~X86_EFLAGS_IF;
3944 	return X86EMUL_CONTINUE;
3945 }
3946 
em_sti(struct x86_emulate_ctxt * ctxt)3947 static int em_sti(struct x86_emulate_ctxt *ctxt)
3948 {
3949 	if (emulator_bad_iopl(ctxt))
3950 		return emulate_gp(ctxt, 0);
3951 
3952 	ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3953 	ctxt->eflags |= X86_EFLAGS_IF;
3954 	return X86EMUL_CONTINUE;
3955 }
3956 
em_cpuid(struct x86_emulate_ctxt * ctxt)3957 static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3958 {
3959 	u32 eax, ebx, ecx, edx;
3960 	u64 msr = 0;
3961 
3962 	ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
3963 	if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
3964 	    ctxt->ops->cpl(ctxt)) {
3965 		return emulate_gp(ctxt, 0);
3966 	}
3967 
3968 	eax = reg_read(ctxt, VCPU_REGS_RAX);
3969 	ecx = reg_read(ctxt, VCPU_REGS_RCX);
3970 	ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
3971 	*reg_write(ctxt, VCPU_REGS_RAX) = eax;
3972 	*reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3973 	*reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3974 	*reg_write(ctxt, VCPU_REGS_RDX) = edx;
3975 	return X86EMUL_CONTINUE;
3976 }
3977 
em_sahf(struct x86_emulate_ctxt * ctxt)3978 static int em_sahf(struct x86_emulate_ctxt *ctxt)
3979 {
3980 	u32 flags;
3981 
3982 	flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3983 		X86_EFLAGS_SF;
3984 	flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3985 
3986 	ctxt->eflags &= ~0xffUL;
3987 	ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3988 	return X86EMUL_CONTINUE;
3989 }
3990 
em_lahf(struct x86_emulate_ctxt * ctxt)3991 static int em_lahf(struct x86_emulate_ctxt *ctxt)
3992 {
3993 	*reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3994 	*reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
3995 	return X86EMUL_CONTINUE;
3996 }
3997 
em_bswap(struct x86_emulate_ctxt * ctxt)3998 static int em_bswap(struct x86_emulate_ctxt *ctxt)
3999 {
4000 	switch (ctxt->op_bytes) {
4001 #ifdef CONFIG_X86_64
4002 	case 8:
4003 		asm("bswap %0" : "+r"(ctxt->dst.val));
4004 		break;
4005 #endif
4006 	default:
4007 		asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
4008 		break;
4009 	}
4010 	return X86EMUL_CONTINUE;
4011 }
4012 
em_clflush(struct x86_emulate_ctxt * ctxt)4013 static int em_clflush(struct x86_emulate_ctxt *ctxt)
4014 {
4015 	/* emulating clflush regardless of cpuid */
4016 	return X86EMUL_CONTINUE;
4017 }
4018 
em_clflushopt(struct x86_emulate_ctxt * ctxt)4019 static int em_clflushopt(struct x86_emulate_ctxt *ctxt)
4020 {
4021 	/* emulating clflushopt regardless of cpuid */
4022 	return X86EMUL_CONTINUE;
4023 }
4024 
em_movsxd(struct x86_emulate_ctxt * ctxt)4025 static int em_movsxd(struct x86_emulate_ctxt *ctxt)
4026 {
4027 	ctxt->dst.val = (s32) ctxt->src.val;
4028 	return X86EMUL_CONTINUE;
4029 }
4030 
check_fxsr(struct x86_emulate_ctxt * ctxt)4031 static int check_fxsr(struct x86_emulate_ctxt *ctxt)
4032 {
4033 	if (!ctxt->ops->guest_has_fxsr(ctxt))
4034 		return emulate_ud(ctxt);
4035 
4036 	if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
4037 		return emulate_nm(ctxt);
4038 
4039 	/*
4040 	 * Don't emulate a case that should never be hit, instead of working
4041 	 * around a lack of fxsave64/fxrstor64 on old compilers.
4042 	 */
4043 	if (ctxt->mode >= X86EMUL_MODE_PROT64)
4044 		return X86EMUL_UNHANDLEABLE;
4045 
4046 	return X86EMUL_CONTINUE;
4047 }
4048 
4049 /*
4050  * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
4051  * and restore MXCSR.
4052  */
__fxstate_size(int nregs)4053 static size_t __fxstate_size(int nregs)
4054 {
4055 	return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
4056 }
4057 
fxstate_size(struct x86_emulate_ctxt * ctxt)4058 static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
4059 {
4060 	bool cr4_osfxsr;
4061 	if (ctxt->mode == X86EMUL_MODE_PROT64)
4062 		return __fxstate_size(16);
4063 
4064 	cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
4065 	return __fxstate_size(cr4_osfxsr ? 8 : 0);
4066 }
4067 
4068 /*
4069  * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
4070  *  1) 16 bit mode
4071  *  2) 32 bit mode
4072  *     - like (1), but FIP and FDP (foo) are only 16 bit.  At least Intel CPUs
4073  *       preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
4074  *       save and restore
4075  *  3) 64-bit mode with REX.W prefix
4076  *     - like (2), but XMM 8-15 are being saved and restored
4077  *  4) 64-bit mode without REX.W prefix
4078  *     - like (3), but FIP and FDP are 64 bit
4079  *
4080  * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
4081  * desired result.  (4) is not emulated.
4082  *
4083  * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
4084  * and FPU DS) should match.
4085  */
em_fxsave(struct x86_emulate_ctxt * ctxt)4086 static int em_fxsave(struct x86_emulate_ctxt *ctxt)
4087 {
4088 	struct fxregs_state fx_state;
4089 	int rc;
4090 
4091 	rc = check_fxsr(ctxt);
4092 	if (rc != X86EMUL_CONTINUE)
4093 		return rc;
4094 
4095 	kvm_fpu_get();
4096 
4097 	rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4098 
4099 	kvm_fpu_put();
4100 
4101 	if (rc != X86EMUL_CONTINUE)
4102 		return rc;
4103 
4104 	return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
4105 		                   fxstate_size(ctxt));
4106 }
4107 
4108 /*
4109  * FXRSTOR might restore XMM registers not provided by the guest. Fill
4110  * in the host registers (via FXSAVE) instead, so they won't be modified.
4111  * (preemption has to stay disabled until FXRSTOR).
4112  *
4113  * Use noinline to keep the stack for other functions called by callers small.
4114  */
fxregs_fixup(struct fxregs_state * fx_state,const size_t used_size)4115 static noinline int fxregs_fixup(struct fxregs_state *fx_state,
4116 				 const size_t used_size)
4117 {
4118 	struct fxregs_state fx_tmp;
4119 	int rc;
4120 
4121 	rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
4122 	memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
4123 	       __fxstate_size(16) - used_size);
4124 
4125 	return rc;
4126 }
4127 
em_fxrstor(struct x86_emulate_ctxt * ctxt)4128 static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4129 {
4130 	struct fxregs_state fx_state;
4131 	int rc;
4132 	size_t size;
4133 
4134 	rc = check_fxsr(ctxt);
4135 	if (rc != X86EMUL_CONTINUE)
4136 		return rc;
4137 
4138 	size = fxstate_size(ctxt);
4139 	rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4140 	if (rc != X86EMUL_CONTINUE)
4141 		return rc;
4142 
4143 	kvm_fpu_get();
4144 
4145 	if (size < __fxstate_size(16)) {
4146 		rc = fxregs_fixup(&fx_state, size);
4147 		if (rc != X86EMUL_CONTINUE)
4148 			goto out;
4149 	}
4150 
4151 	if (fx_state.mxcsr >> 16) {
4152 		rc = emulate_gp(ctxt, 0);
4153 		goto out;
4154 	}
4155 
4156 	if (rc == X86EMUL_CONTINUE)
4157 		rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4158 
4159 out:
4160 	kvm_fpu_put();
4161 
4162 	return rc;
4163 }
4164 
em_xsetbv(struct x86_emulate_ctxt * ctxt)4165 static int em_xsetbv(struct x86_emulate_ctxt *ctxt)
4166 {
4167 	u32 eax, ecx, edx;
4168 
4169 	if (!(ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSXSAVE))
4170 		return emulate_ud(ctxt);
4171 
4172 	eax = reg_read(ctxt, VCPU_REGS_RAX);
4173 	edx = reg_read(ctxt, VCPU_REGS_RDX);
4174 	ecx = reg_read(ctxt, VCPU_REGS_RCX);
4175 
4176 	if (ctxt->ops->set_xcr(ctxt, ecx, ((u64)edx << 32) | eax))
4177 		return emulate_gp(ctxt, 0);
4178 
4179 	return X86EMUL_CONTINUE;
4180 }
4181 
valid_cr(int nr)4182 static bool valid_cr(int nr)
4183 {
4184 	switch (nr) {
4185 	case 0:
4186 	case 2 ... 4:
4187 	case 8:
4188 		return true;
4189 	default:
4190 		return false;
4191 	}
4192 }
4193 
check_cr_access(struct x86_emulate_ctxt * ctxt)4194 static int check_cr_access(struct x86_emulate_ctxt *ctxt)
4195 {
4196 	if (!valid_cr(ctxt->modrm_reg))
4197 		return emulate_ud(ctxt);
4198 
4199 	return X86EMUL_CONTINUE;
4200 }
4201 
check_dr7_gd(struct x86_emulate_ctxt * ctxt)4202 static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4203 {
4204 	unsigned long dr7;
4205 
4206 	ctxt->ops->get_dr(ctxt, 7, &dr7);
4207 
4208 	/* Check if DR7.Global_Enable is set */
4209 	return dr7 & (1 << 13);
4210 }
4211 
check_dr_read(struct x86_emulate_ctxt * ctxt)4212 static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4213 {
4214 	int dr = ctxt->modrm_reg;
4215 	u64 cr4;
4216 
4217 	if (dr > 7)
4218 		return emulate_ud(ctxt);
4219 
4220 	cr4 = ctxt->ops->get_cr(ctxt, 4);
4221 	if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4222 		return emulate_ud(ctxt);
4223 
4224 	if (check_dr7_gd(ctxt)) {
4225 		ulong dr6;
4226 
4227 		ctxt->ops->get_dr(ctxt, 6, &dr6);
4228 		dr6 &= ~DR_TRAP_BITS;
4229 		dr6 |= DR6_BD | DR6_ACTIVE_LOW;
4230 		ctxt->ops->set_dr(ctxt, 6, dr6);
4231 		return emulate_db(ctxt);
4232 	}
4233 
4234 	return X86EMUL_CONTINUE;
4235 }
4236 
check_dr_write(struct x86_emulate_ctxt * ctxt)4237 static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4238 {
4239 	u64 new_val = ctxt->src.val64;
4240 	int dr = ctxt->modrm_reg;
4241 
4242 	if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4243 		return emulate_gp(ctxt, 0);
4244 
4245 	return check_dr_read(ctxt);
4246 }
4247 
check_svme(struct x86_emulate_ctxt * ctxt)4248 static int check_svme(struct x86_emulate_ctxt *ctxt)
4249 {
4250 	u64 efer = 0;
4251 
4252 	ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
4253 
4254 	if (!(efer & EFER_SVME))
4255 		return emulate_ud(ctxt);
4256 
4257 	return X86EMUL_CONTINUE;
4258 }
4259 
check_svme_pa(struct x86_emulate_ctxt * ctxt)4260 static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4261 {
4262 	u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
4263 
4264 	/* Valid physical address? */
4265 	if (rax & 0xffff000000000000ULL)
4266 		return emulate_gp(ctxt, 0);
4267 
4268 	return check_svme(ctxt);
4269 }
4270 
check_rdtsc(struct x86_emulate_ctxt * ctxt)4271 static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4272 {
4273 	u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
4274 
4275 	if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
4276 		return emulate_gp(ctxt, 0);
4277 
4278 	return X86EMUL_CONTINUE;
4279 }
4280 
check_rdpmc(struct x86_emulate_ctxt * ctxt)4281 static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4282 {
4283 	u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
4284 	u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
4285 
4286 	/*
4287 	 * VMware allows access to these Pseduo-PMCs even when read via RDPMC
4288 	 * in Ring3 when CR4.PCE=0.
4289 	 */
4290 	if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
4291 		return X86EMUL_CONTINUE;
4292 
4293 	if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
4294 	    ctxt->ops->check_pmc(ctxt, rcx))
4295 		return emulate_gp(ctxt, 0);
4296 
4297 	return X86EMUL_CONTINUE;
4298 }
4299 
check_perm_in(struct x86_emulate_ctxt * ctxt)4300 static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4301 {
4302 	ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4303 	if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
4304 		return emulate_gp(ctxt, 0);
4305 
4306 	return X86EMUL_CONTINUE;
4307 }
4308 
check_perm_out(struct x86_emulate_ctxt * ctxt)4309 static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4310 {
4311 	ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4312 	if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
4313 		return emulate_gp(ctxt, 0);
4314 
4315 	return X86EMUL_CONTINUE;
4316 }
4317 
4318 #define D(_y) { .flags = (_y) }
4319 #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4320 #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4321 		      .intercept = x86_intercept_##_i, .check_perm = (_p) }
4322 #define N    D(NotImpl)
4323 #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
4324 #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4325 #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
4326 #define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
4327 #define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
4328 #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
4329 #define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
4330 #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
4331 #define II(_f, _e, _i) \
4332 	{ .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
4333 #define IIP(_f, _e, _i, _p) \
4334 	{ .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4335 	  .intercept = x86_intercept_##_i, .check_perm = (_p) }
4336 #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
4337 
4338 #define D2bv(_f)      D((_f) | ByteOp), D(_f)
4339 #define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
4340 #define I2bv(_f, _e)  I((_f) | ByteOp, _e), I(_f, _e)
4341 #define F2bv(_f, _e)  F((_f) | ByteOp, _e), F(_f, _e)
4342 #define I2bvIP(_f, _e, _i, _p) \
4343 	IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
4344 
4345 #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e),		\
4346 		F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e),	\
4347 		F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
4348 
4349 static const struct opcode group7_rm0[] = {
4350 	N,
4351 	I(SrcNone | Priv | EmulateOnUD,	em_hypercall),
4352 	N, N, N, N, N, N,
4353 };
4354 
4355 static const struct opcode group7_rm1[] = {
4356 	DI(SrcNone | Priv, monitor),
4357 	DI(SrcNone | Priv, mwait),
4358 	N, N, N, N, N, N,
4359 };
4360 
4361 static const struct opcode group7_rm2[] = {
4362 	N,
4363 	II(ImplicitOps | Priv,			em_xsetbv,	xsetbv),
4364 	N, N, N, N, N, N,
4365 };
4366 
4367 static const struct opcode group7_rm3[] = {
4368 	DIP(SrcNone | Prot | Priv,		vmrun,		check_svme_pa),
4369 	II(SrcNone  | Prot | EmulateOnUD,	em_hypercall,	vmmcall),
4370 	DIP(SrcNone | Prot | Priv,		vmload,		check_svme_pa),
4371 	DIP(SrcNone | Prot | Priv,		vmsave,		check_svme_pa),
4372 	DIP(SrcNone | Prot | Priv,		stgi,		check_svme),
4373 	DIP(SrcNone | Prot | Priv,		clgi,		check_svme),
4374 	DIP(SrcNone | Prot | Priv,		skinit,		check_svme),
4375 	DIP(SrcNone | Prot | Priv,		invlpga,	check_svme),
4376 };
4377 
4378 static const struct opcode group7_rm7[] = {
4379 	N,
4380 	DIP(SrcNone, rdtscp, check_rdtsc),
4381 	N, N, N, N, N, N,
4382 };
4383 
4384 static const struct opcode group1[] = {
4385 	F(Lock, em_add),
4386 	F(Lock | PageTable, em_or),
4387 	F(Lock, em_adc),
4388 	F(Lock, em_sbb),
4389 	F(Lock | PageTable, em_and),
4390 	F(Lock, em_sub),
4391 	F(Lock, em_xor),
4392 	F(NoWrite, em_cmp),
4393 };
4394 
4395 static const struct opcode group1A[] = {
4396 	I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
4397 };
4398 
4399 static const struct opcode group2[] = {
4400 	F(DstMem | ModRM, em_rol),
4401 	F(DstMem | ModRM, em_ror),
4402 	F(DstMem | ModRM, em_rcl),
4403 	F(DstMem | ModRM, em_rcr),
4404 	F(DstMem | ModRM, em_shl),
4405 	F(DstMem | ModRM, em_shr),
4406 	F(DstMem | ModRM, em_shl),
4407 	F(DstMem | ModRM, em_sar),
4408 };
4409 
4410 static const struct opcode group3[] = {
4411 	F(DstMem | SrcImm | NoWrite, em_test),
4412 	F(DstMem | SrcImm | NoWrite, em_test),
4413 	F(DstMem | SrcNone | Lock, em_not),
4414 	F(DstMem | SrcNone | Lock, em_neg),
4415 	F(DstXacc | Src2Mem, em_mul_ex),
4416 	F(DstXacc | Src2Mem, em_imul_ex),
4417 	F(DstXacc | Src2Mem, em_div_ex),
4418 	F(DstXacc | Src2Mem, em_idiv_ex),
4419 };
4420 
4421 static const struct opcode group4[] = {
4422 	F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4423 	F(ByteOp | DstMem | SrcNone | Lock, em_dec),
4424 	N, N, N, N, N, N,
4425 };
4426 
4427 static const struct opcode group5[] = {
4428 	F(DstMem | SrcNone | Lock,		em_inc),
4429 	F(DstMem | SrcNone | Lock,		em_dec),
4430 	I(SrcMem | NearBranch,			em_call_near_abs),
4431 	I(SrcMemFAddr | ImplicitOps,		em_call_far),
4432 	I(SrcMem | NearBranch,			em_jmp_abs),
4433 	I(SrcMemFAddr | ImplicitOps,		em_jmp_far),
4434 	I(SrcMem | Stack | TwoMemOp,		em_push), D(Undefined),
4435 };
4436 
4437 static const struct opcode group6[] = {
4438 	II(Prot | DstMem,	   em_sldt, sldt),
4439 	II(Prot | DstMem,	   em_str, str),
4440 	II(Prot | Priv | SrcMem16, em_lldt, lldt),
4441 	II(Prot | Priv | SrcMem16, em_ltr, ltr),
4442 	N, N, N, N,
4443 };
4444 
4445 static const struct group_dual group7 = { {
4446 	II(Mov | DstMem,			em_sgdt, sgdt),
4447 	II(Mov | DstMem,			em_sidt, sidt),
4448 	II(SrcMem | Priv,			em_lgdt, lgdt),
4449 	II(SrcMem | Priv,			em_lidt, lidt),
4450 	II(SrcNone | DstMem | Mov,		em_smsw, smsw), N,
4451 	II(SrcMem16 | Mov | Priv,		em_lmsw, lmsw),
4452 	II(SrcMem | ByteOp | Priv | NoAccess,	em_invlpg, invlpg),
4453 }, {
4454 	EXT(0, group7_rm0),
4455 	EXT(0, group7_rm1),
4456 	EXT(0, group7_rm2),
4457 	EXT(0, group7_rm3),
4458 	II(SrcNone | DstMem | Mov,		em_smsw, smsw), N,
4459 	II(SrcMem16 | Mov | Priv,		em_lmsw, lmsw),
4460 	EXT(0, group7_rm7),
4461 } };
4462 
4463 static const struct opcode group8[] = {
4464 	N, N, N, N,
4465 	F(DstMem | SrcImmByte | NoWrite,		em_bt),
4466 	F(DstMem | SrcImmByte | Lock | PageTable,	em_bts),
4467 	F(DstMem | SrcImmByte | Lock,			em_btr),
4468 	F(DstMem | SrcImmByte | Lock | PageTable,	em_btc),
4469 };
4470 
4471 /*
4472  * The "memory" destination is actually always a register, since we come
4473  * from the register case of group9.
4474  */
4475 static const struct gprefix pfx_0f_c7_7 = {
4476 	N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdpid),
4477 };
4478 
4479 
4480 static const struct group_dual group9 = { {
4481 	N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
4482 }, {
4483 	N, N, N, N, N, N, N,
4484 	GP(0, &pfx_0f_c7_7),
4485 } };
4486 
4487 static const struct opcode group11[] = {
4488 	I(DstMem | SrcImm | Mov | PageTable, em_mov),
4489 	X7(D(Undefined)),
4490 };
4491 
4492 static const struct gprefix pfx_0f_ae_7 = {
4493 	I(SrcMem | ByteOp, em_clflush), I(SrcMem | ByteOp, em_clflushopt), N, N,
4494 };
4495 
4496 static const struct group_dual group15 = { {
4497 	I(ModRM | Aligned16, em_fxsave),
4498 	I(ModRM | Aligned16, em_fxrstor),
4499 	N, N, N, N, N, GP(0, &pfx_0f_ae_7),
4500 }, {
4501 	N, N, N, N, N, N, N, N,
4502 } };
4503 
4504 static const struct gprefix pfx_0f_6f_0f_7f = {
4505 	I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
4506 };
4507 
4508 static const struct instr_dual instr_dual_0f_2b = {
4509 	I(0, em_mov), N
4510 };
4511 
4512 static const struct gprefix pfx_0f_2b = {
4513 	ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
4514 };
4515 
4516 static const struct gprefix pfx_0f_10_0f_11 = {
4517 	I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
4518 };
4519 
4520 static const struct gprefix pfx_0f_28_0f_29 = {
4521 	I(Aligned, em_mov), I(Aligned, em_mov), N, N,
4522 };
4523 
4524 static const struct gprefix pfx_0f_e7 = {
4525 	N, I(Sse, em_mov), N, N,
4526 };
4527 
4528 static const struct escape escape_d9 = { {
4529 	N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
4530 }, {
4531 	/* 0xC0 - 0xC7 */
4532 	N, N, N, N, N, N, N, N,
4533 	/* 0xC8 - 0xCF */
4534 	N, N, N, N, N, N, N, N,
4535 	/* 0xD0 - 0xC7 */
4536 	N, N, N, N, N, N, N, N,
4537 	/* 0xD8 - 0xDF */
4538 	N, N, N, N, N, N, N, N,
4539 	/* 0xE0 - 0xE7 */
4540 	N, N, N, N, N, N, N, N,
4541 	/* 0xE8 - 0xEF */
4542 	N, N, N, N, N, N, N, N,
4543 	/* 0xF0 - 0xF7 */
4544 	N, N, N, N, N, N, N, N,
4545 	/* 0xF8 - 0xFF */
4546 	N, N, N, N, N, N, N, N,
4547 } };
4548 
4549 static const struct escape escape_db = { {
4550 	N, N, N, N, N, N, N, N,
4551 }, {
4552 	/* 0xC0 - 0xC7 */
4553 	N, N, N, N, N, N, N, N,
4554 	/* 0xC8 - 0xCF */
4555 	N, N, N, N, N, N, N, N,
4556 	/* 0xD0 - 0xC7 */
4557 	N, N, N, N, N, N, N, N,
4558 	/* 0xD8 - 0xDF */
4559 	N, N, N, N, N, N, N, N,
4560 	/* 0xE0 - 0xE7 */
4561 	N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4562 	/* 0xE8 - 0xEF */
4563 	N, N, N, N, N, N, N, N,
4564 	/* 0xF0 - 0xF7 */
4565 	N, N, N, N, N, N, N, N,
4566 	/* 0xF8 - 0xFF */
4567 	N, N, N, N, N, N, N, N,
4568 } };
4569 
4570 static const struct escape escape_dd = { {
4571 	N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
4572 }, {
4573 	/* 0xC0 - 0xC7 */
4574 	N, N, N, N, N, N, N, N,
4575 	/* 0xC8 - 0xCF */
4576 	N, N, N, N, N, N, N, N,
4577 	/* 0xD0 - 0xC7 */
4578 	N, N, N, N, N, N, N, N,
4579 	/* 0xD8 - 0xDF */
4580 	N, N, N, N, N, N, N, N,
4581 	/* 0xE0 - 0xE7 */
4582 	N, N, N, N, N, N, N, N,
4583 	/* 0xE8 - 0xEF */
4584 	N, N, N, N, N, N, N, N,
4585 	/* 0xF0 - 0xF7 */
4586 	N, N, N, N, N, N, N, N,
4587 	/* 0xF8 - 0xFF */
4588 	N, N, N, N, N, N, N, N,
4589 } };
4590 
4591 static const struct instr_dual instr_dual_0f_c3 = {
4592 	I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4593 };
4594 
4595 static const struct mode_dual mode_dual_63 = {
4596 	N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4597 };
4598 
4599 static const struct opcode opcode_table[256] = {
4600 	/* 0x00 - 0x07 */
4601 	F6ALU(Lock, em_add),
4602 	I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4603 	I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
4604 	/* 0x08 - 0x0F */
4605 	F6ALU(Lock | PageTable, em_or),
4606 	I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4607 	N,
4608 	/* 0x10 - 0x17 */
4609 	F6ALU(Lock, em_adc),
4610 	I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4611 	I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
4612 	/* 0x18 - 0x1F */
4613 	F6ALU(Lock, em_sbb),
4614 	I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4615 	I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
4616 	/* 0x20 - 0x27 */
4617 	F6ALU(Lock | PageTable, em_and), N, N,
4618 	/* 0x28 - 0x2F */
4619 	F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
4620 	/* 0x30 - 0x37 */
4621 	F6ALU(Lock, em_xor), N, N,
4622 	/* 0x38 - 0x3F */
4623 	F6ALU(NoWrite, em_cmp), N, N,
4624 	/* 0x40 - 0x4F */
4625 	X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
4626 	/* 0x50 - 0x57 */
4627 	X8(I(SrcReg | Stack, em_push)),
4628 	/* 0x58 - 0x5F */
4629 	X8(I(DstReg | Stack, em_pop)),
4630 	/* 0x60 - 0x67 */
4631 	I(ImplicitOps | Stack | No64, em_pusha),
4632 	I(ImplicitOps | Stack | No64, em_popa),
4633 	N, MD(ModRM, &mode_dual_63),
4634 	N, N, N, N,
4635 	/* 0x68 - 0x6F */
4636 	I(SrcImm | Mov | Stack, em_push),
4637 	I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
4638 	I(SrcImmByte | Mov | Stack, em_push),
4639 	I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
4640 	I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
4641 	I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
4642 	/* 0x70 - 0x7F */
4643 	X16(D(SrcImmByte | NearBranch)),
4644 	/* 0x80 - 0x87 */
4645 	G(ByteOp | DstMem | SrcImm, group1),
4646 	G(DstMem | SrcImm, group1),
4647 	G(ByteOp | DstMem | SrcImm | No64, group1),
4648 	G(DstMem | SrcImmByte, group1),
4649 	F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
4650 	I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
4651 	/* 0x88 - 0x8F */
4652 	I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
4653 	I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
4654 	I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
4655 	D(ModRM | SrcMem | NoAccess | DstReg),
4656 	I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4657 	G(0, group1A),
4658 	/* 0x90 - 0x97 */
4659 	DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
4660 	/* 0x98 - 0x9F */
4661 	D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
4662 	I(SrcImmFAddr | No64, em_call_far), N,
4663 	II(ImplicitOps | Stack, em_pushf, pushf),
4664 	II(ImplicitOps | Stack, em_popf, popf),
4665 	I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
4666 	/* 0xA0 - 0xA7 */
4667 	I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
4668 	I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
4669 	I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
4670 	F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
4671 	/* 0xA8 - 0xAF */
4672 	F2bv(DstAcc | SrcImm | NoWrite, em_test),
4673 	I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4674 	I2bv(SrcSI | DstAcc | Mov | String, em_mov),
4675 	F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
4676 	/* 0xB0 - 0xB7 */
4677 	X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
4678 	/* 0xB8 - 0xBF */
4679 	X8(I(DstReg | SrcImm64 | Mov, em_mov)),
4680 	/* 0xC0 - 0xC7 */
4681 	G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
4682 	I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4683 	I(ImplicitOps | NearBranch, em_ret),
4684 	I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4685 	I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
4686 	G(ByteOp, group11), G(0, group11),
4687 	/* 0xC8 - 0xCF */
4688 	I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
4689 	I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4690 	I(ImplicitOps, em_ret_far),
4691 	D(ImplicitOps), DI(SrcImmByte, intn),
4692 	D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
4693 	/* 0xD0 - 0xD7 */
4694 	G(Src2One | ByteOp, group2), G(Src2One, group2),
4695 	G(Src2CL | ByteOp, group2), G(Src2CL, group2),
4696 	I(DstAcc | SrcImmUByte | No64, em_aam),
4697 	I(DstAcc | SrcImmUByte | No64, em_aad),
4698 	F(DstAcc | ByteOp | No64, em_salc),
4699 	I(DstAcc | SrcXLat | ByteOp, em_mov),
4700 	/* 0xD8 - 0xDF */
4701 	N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
4702 	/* 0xE0 - 0xE7 */
4703 	X3(I(SrcImmByte | NearBranch, em_loop)),
4704 	I(SrcImmByte | NearBranch, em_jcxz),
4705 	I2bvIP(SrcImmUByte | DstAcc, em_in,  in,  check_perm_in),
4706 	I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
4707 	/* 0xE8 - 0xEF */
4708 	I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4709 	I(SrcImmFAddr | No64, em_jmp_far),
4710 	D(SrcImmByte | ImplicitOps | NearBranch),
4711 	I2bvIP(SrcDX | DstAcc, em_in,  in,  check_perm_in),
4712 	I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
4713 	/* 0xF0 - 0xF7 */
4714 	N, DI(ImplicitOps, icebp), N, N,
4715 	DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4716 	G(ByteOp, group3), G(0, group3),
4717 	/* 0xF8 - 0xFF */
4718 	D(ImplicitOps), D(ImplicitOps),
4719 	I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
4720 	D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4721 };
4722 
4723 static const struct opcode twobyte_table[256] = {
4724 	/* 0x00 - 0x0F */
4725 	G(0, group6), GD(0, &group7), N, N,
4726 	N, I(ImplicitOps | EmulateOnUD, em_syscall),
4727 	II(ImplicitOps | Priv, em_clts, clts), N,
4728 	DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
4729 	N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
4730 	/* 0x10 - 0x1F */
4731 	GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
4732 	GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
4733 	N, N, N, N, N, N,
4734 	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 4 * prefetch + 4 * reserved NOP */
4735 	D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
4736 	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4737 	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4738 	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* 8 * reserved NOP */
4739 	D(ImplicitOps | ModRM | SrcMem | NoAccess), /* NOP + 7 * reserved NOP */
4740 	/* 0x20 - 0x2F */
4741 	DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_access),
4742 	DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4743 	IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4744 						check_cr_access),
4745 	IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4746 						check_dr_write),
4747 	N, N, N, N,
4748 	GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4749 	GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
4750 	N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
4751 	N, N, N, N,
4752 	/* 0x30 - 0x3F */
4753 	II(ImplicitOps | Priv, em_wrmsr, wrmsr),
4754 	IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
4755 	II(ImplicitOps | Priv, em_rdmsr, rdmsr),
4756 	IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
4757 	I(ImplicitOps | EmulateOnUD, em_sysenter),
4758 	I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
4759 	N, N,
4760 	N, N, N, N, N, N, N, N,
4761 	/* 0x40 - 0x4F */
4762 	X16(D(DstReg | SrcMem | ModRM)),
4763 	/* 0x50 - 0x5F */
4764 	N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4765 	/* 0x60 - 0x6F */
4766 	N, N, N, N,
4767 	N, N, N, N,
4768 	N, N, N, N,
4769 	N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
4770 	/* 0x70 - 0x7F */
4771 	N, N, N, N,
4772 	N, N, N, N,
4773 	N, N, N, N,
4774 	N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
4775 	/* 0x80 - 0x8F */
4776 	X16(D(SrcImm | NearBranch)),
4777 	/* 0x90 - 0x9F */
4778 	X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
4779 	/* 0xA0 - 0xA7 */
4780 	I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
4781 	II(ImplicitOps, em_cpuid, cpuid),
4782 	F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
4783 	F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4784 	F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
4785 	/* 0xA8 - 0xAF */
4786 	I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
4787 	II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
4788 	F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
4789 	F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4790 	F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
4791 	GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
4792 	/* 0xB0 - 0xB7 */
4793 	I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
4794 	I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
4795 	F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
4796 	I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4797 	I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
4798 	D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
4799 	/* 0xB8 - 0xBF */
4800 	N, N,
4801 	G(BitOp, group8),
4802 	F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4803 	I(DstReg | SrcMem | ModRM, em_bsf_c),
4804 	I(DstReg | SrcMem | ModRM, em_bsr_c),
4805 	D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
4806 	/* 0xC0 - 0xC7 */
4807 	F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
4808 	N, ID(0, &instr_dual_0f_c3),
4809 	N, N, N, GD(0, &group9),
4810 	/* 0xC8 - 0xCF */
4811 	X8(I(DstReg, em_bswap)),
4812 	/* 0xD0 - 0xDF */
4813 	N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4814 	/* 0xE0 - 0xEF */
4815 	N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4816 	N, N, N, N, N, N, N, N,
4817 	/* 0xF0 - 0xFF */
4818 	N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4819 };
4820 
4821 static const struct instr_dual instr_dual_0f_38_f0 = {
4822 	I(DstReg | SrcMem | Mov, em_movbe), N
4823 };
4824 
4825 static const struct instr_dual instr_dual_0f_38_f1 = {
4826 	I(DstMem | SrcReg | Mov, em_movbe), N
4827 };
4828 
4829 static const struct gprefix three_byte_0f_38_f0 = {
4830 	ID(0, &instr_dual_0f_38_f0), N, N, N
4831 };
4832 
4833 static const struct gprefix three_byte_0f_38_f1 = {
4834 	ID(0, &instr_dual_0f_38_f1), N, N, N
4835 };
4836 
4837 /*
4838  * Insns below are selected by the prefix which indexed by the third opcode
4839  * byte.
4840  */
4841 static const struct opcode opcode_map_0f_38[256] = {
4842 	/* 0x00 - 0x7f */
4843 	X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4844 	/* 0x80 - 0xef */
4845 	X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4846 	/* 0xf0 - 0xf1 */
4847 	GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4848 	GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
4849 	/* 0xf2 - 0xff */
4850 	N, N, X4(N), X8(N)
4851 };
4852 
4853 #undef D
4854 #undef N
4855 #undef G
4856 #undef GD
4857 #undef I
4858 #undef GP
4859 #undef EXT
4860 #undef MD
4861 #undef ID
4862 
4863 #undef D2bv
4864 #undef D2bvIP
4865 #undef I2bv
4866 #undef I2bvIP
4867 #undef I6ALU
4868 
imm_size(struct x86_emulate_ctxt * ctxt)4869 static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
4870 {
4871 	unsigned size;
4872 
4873 	size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4874 	if (size == 8)
4875 		size = 4;
4876 	return size;
4877 }
4878 
decode_imm(struct x86_emulate_ctxt * ctxt,struct operand * op,unsigned size,bool sign_extension)4879 static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4880 		      unsigned size, bool sign_extension)
4881 {
4882 	int rc = X86EMUL_CONTINUE;
4883 
4884 	op->type = OP_IMM;
4885 	op->bytes = size;
4886 	op->addr.mem.ea = ctxt->_eip;
4887 	/* NB. Immediates are sign-extended as necessary. */
4888 	switch (op->bytes) {
4889 	case 1:
4890 		op->val = insn_fetch(s8, ctxt);
4891 		break;
4892 	case 2:
4893 		op->val = insn_fetch(s16, ctxt);
4894 		break;
4895 	case 4:
4896 		op->val = insn_fetch(s32, ctxt);
4897 		break;
4898 	case 8:
4899 		op->val = insn_fetch(s64, ctxt);
4900 		break;
4901 	}
4902 	if (!sign_extension) {
4903 		switch (op->bytes) {
4904 		case 1:
4905 			op->val &= 0xff;
4906 			break;
4907 		case 2:
4908 			op->val &= 0xffff;
4909 			break;
4910 		case 4:
4911 			op->val &= 0xffffffff;
4912 			break;
4913 		}
4914 	}
4915 done:
4916 	return rc;
4917 }
4918 
decode_operand(struct x86_emulate_ctxt * ctxt,struct operand * op,unsigned d)4919 static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4920 			  unsigned d)
4921 {
4922 	int rc = X86EMUL_CONTINUE;
4923 
4924 	switch (d) {
4925 	case OpReg:
4926 		decode_register_operand(ctxt, op);
4927 		break;
4928 	case OpImmUByte:
4929 		rc = decode_imm(ctxt, op, 1, false);
4930 		break;
4931 	case OpMem:
4932 		ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4933 	mem_common:
4934 		*op = ctxt->memop;
4935 		ctxt->memopp = op;
4936 		if (ctxt->d & BitOp)
4937 			fetch_bit_operand(ctxt);
4938 		op->orig_val = op->val;
4939 		break;
4940 	case OpMem64:
4941 		ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
4942 		goto mem_common;
4943 	case OpAcc:
4944 		op->type = OP_REG;
4945 		op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4946 		op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4947 		fetch_register_operand(op);
4948 		op->orig_val = op->val;
4949 		break;
4950 	case OpAccLo:
4951 		op->type = OP_REG;
4952 		op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4953 		op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4954 		fetch_register_operand(op);
4955 		op->orig_val = op->val;
4956 		break;
4957 	case OpAccHi:
4958 		if (ctxt->d & ByteOp) {
4959 			op->type = OP_NONE;
4960 			break;
4961 		}
4962 		op->type = OP_REG;
4963 		op->bytes = ctxt->op_bytes;
4964 		op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4965 		fetch_register_operand(op);
4966 		op->orig_val = op->val;
4967 		break;
4968 	case OpDI:
4969 		op->type = OP_MEM;
4970 		op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4971 		op->addr.mem.ea =
4972 			register_address(ctxt, VCPU_REGS_RDI);
4973 		op->addr.mem.seg = VCPU_SREG_ES;
4974 		op->val = 0;
4975 		op->count = 1;
4976 		break;
4977 	case OpDX:
4978 		op->type = OP_REG;
4979 		op->bytes = 2;
4980 		op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4981 		fetch_register_operand(op);
4982 		break;
4983 	case OpCL:
4984 		op->type = OP_IMM;
4985 		op->bytes = 1;
4986 		op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
4987 		break;
4988 	case OpImmByte:
4989 		rc = decode_imm(ctxt, op, 1, true);
4990 		break;
4991 	case OpOne:
4992 		op->type = OP_IMM;
4993 		op->bytes = 1;
4994 		op->val = 1;
4995 		break;
4996 	case OpImm:
4997 		rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4998 		break;
4999 	case OpImm64:
5000 		rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
5001 		break;
5002 	case OpMem8:
5003 		ctxt->memop.bytes = 1;
5004 		if (ctxt->memop.type == OP_REG) {
5005 			ctxt->memop.addr.reg = decode_register(ctxt,
5006 					ctxt->modrm_rm, true);
5007 			fetch_register_operand(&ctxt->memop);
5008 		}
5009 		goto mem_common;
5010 	case OpMem16:
5011 		ctxt->memop.bytes = 2;
5012 		goto mem_common;
5013 	case OpMem32:
5014 		ctxt->memop.bytes = 4;
5015 		goto mem_common;
5016 	case OpImmU16:
5017 		rc = decode_imm(ctxt, op, 2, false);
5018 		break;
5019 	case OpImmU:
5020 		rc = decode_imm(ctxt, op, imm_size(ctxt), false);
5021 		break;
5022 	case OpSI:
5023 		op->type = OP_MEM;
5024 		op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5025 		op->addr.mem.ea =
5026 			register_address(ctxt, VCPU_REGS_RSI);
5027 		op->addr.mem.seg = ctxt->seg_override;
5028 		op->val = 0;
5029 		op->count = 1;
5030 		break;
5031 	case OpXLat:
5032 		op->type = OP_MEM;
5033 		op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
5034 		op->addr.mem.ea =
5035 			address_mask(ctxt,
5036 				reg_read(ctxt, VCPU_REGS_RBX) +
5037 				(reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
5038 		op->addr.mem.seg = ctxt->seg_override;
5039 		op->val = 0;
5040 		break;
5041 	case OpImmFAddr:
5042 		op->type = OP_IMM;
5043 		op->addr.mem.ea = ctxt->_eip;
5044 		op->bytes = ctxt->op_bytes + 2;
5045 		insn_fetch_arr(op->valptr, op->bytes, ctxt);
5046 		break;
5047 	case OpMemFAddr:
5048 		ctxt->memop.bytes = ctxt->op_bytes + 2;
5049 		goto mem_common;
5050 	case OpES:
5051 		op->type = OP_IMM;
5052 		op->val = VCPU_SREG_ES;
5053 		break;
5054 	case OpCS:
5055 		op->type = OP_IMM;
5056 		op->val = VCPU_SREG_CS;
5057 		break;
5058 	case OpSS:
5059 		op->type = OP_IMM;
5060 		op->val = VCPU_SREG_SS;
5061 		break;
5062 	case OpDS:
5063 		op->type = OP_IMM;
5064 		op->val = VCPU_SREG_DS;
5065 		break;
5066 	case OpFS:
5067 		op->type = OP_IMM;
5068 		op->val = VCPU_SREG_FS;
5069 		break;
5070 	case OpGS:
5071 		op->type = OP_IMM;
5072 		op->val = VCPU_SREG_GS;
5073 		break;
5074 	case OpImplicit:
5075 		/* Special instructions do their own operand decoding. */
5076 	default:
5077 		op->type = OP_NONE; /* Disable writeback. */
5078 		break;
5079 	}
5080 
5081 done:
5082 	return rc;
5083 }
5084 
x86_decode_insn(struct x86_emulate_ctxt * ctxt,void * insn,int insn_len,int emulation_type)5085 int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type)
5086 {
5087 	int rc = X86EMUL_CONTINUE;
5088 	int mode = ctxt->mode;
5089 	int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
5090 	bool op_prefix = false;
5091 	bool has_seg_override = false;
5092 	struct opcode opcode;
5093 	u16 dummy;
5094 	struct desc_struct desc;
5095 
5096 	ctxt->memop.type = OP_NONE;
5097 	ctxt->memopp = NULL;
5098 	ctxt->_eip = ctxt->eip;
5099 	ctxt->fetch.ptr = ctxt->fetch.data;
5100 	ctxt->fetch.end = ctxt->fetch.data + insn_len;
5101 	ctxt->opcode_len = 1;
5102 	ctxt->intercept = x86_intercept_none;
5103 	if (insn_len > 0)
5104 		memcpy(ctxt->fetch.data, insn, insn_len);
5105 	else {
5106 		rc = __do_insn_fetch_bytes(ctxt, 1);
5107 		if (rc != X86EMUL_CONTINUE)
5108 			goto done;
5109 	}
5110 
5111 	switch (mode) {
5112 	case X86EMUL_MODE_REAL:
5113 	case X86EMUL_MODE_VM86:
5114 		def_op_bytes = def_ad_bytes = 2;
5115 		ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5116 		if (desc.d)
5117 			def_op_bytes = def_ad_bytes = 4;
5118 		break;
5119 	case X86EMUL_MODE_PROT16:
5120 		def_op_bytes = def_ad_bytes = 2;
5121 		break;
5122 	case X86EMUL_MODE_PROT32:
5123 		def_op_bytes = def_ad_bytes = 4;
5124 		break;
5125 #ifdef CONFIG_X86_64
5126 	case X86EMUL_MODE_PROT64:
5127 		def_op_bytes = 4;
5128 		def_ad_bytes = 8;
5129 		break;
5130 #endif
5131 	default:
5132 		return EMULATION_FAILED;
5133 	}
5134 
5135 	ctxt->op_bytes = def_op_bytes;
5136 	ctxt->ad_bytes = def_ad_bytes;
5137 
5138 	/* Legacy prefixes. */
5139 	for (;;) {
5140 		switch (ctxt->b = insn_fetch(u8, ctxt)) {
5141 		case 0x66:	/* operand-size override */
5142 			op_prefix = true;
5143 			/* switch between 2/4 bytes */
5144 			ctxt->op_bytes = def_op_bytes ^ 6;
5145 			break;
5146 		case 0x67:	/* address-size override */
5147 			if (mode == X86EMUL_MODE_PROT64)
5148 				/* switch between 4/8 bytes */
5149 				ctxt->ad_bytes = def_ad_bytes ^ 12;
5150 			else
5151 				/* switch between 2/4 bytes */
5152 				ctxt->ad_bytes = def_ad_bytes ^ 6;
5153 			break;
5154 		case 0x26:	/* ES override */
5155 			has_seg_override = true;
5156 			ctxt->seg_override = VCPU_SREG_ES;
5157 			break;
5158 		case 0x2e:	/* CS override */
5159 			has_seg_override = true;
5160 			ctxt->seg_override = VCPU_SREG_CS;
5161 			break;
5162 		case 0x36:	/* SS override */
5163 			has_seg_override = true;
5164 			ctxt->seg_override = VCPU_SREG_SS;
5165 			break;
5166 		case 0x3e:	/* DS override */
5167 			has_seg_override = true;
5168 			ctxt->seg_override = VCPU_SREG_DS;
5169 			break;
5170 		case 0x64:	/* FS override */
5171 			has_seg_override = true;
5172 			ctxt->seg_override = VCPU_SREG_FS;
5173 			break;
5174 		case 0x65:	/* GS override */
5175 			has_seg_override = true;
5176 			ctxt->seg_override = VCPU_SREG_GS;
5177 			break;
5178 		case 0x40 ... 0x4f: /* REX */
5179 			if (mode != X86EMUL_MODE_PROT64)
5180 				goto done_prefixes;
5181 			ctxt->rex_prefix = ctxt->b;
5182 			continue;
5183 		case 0xf0:	/* LOCK */
5184 			ctxt->lock_prefix = 1;
5185 			break;
5186 		case 0xf2:	/* REPNE/REPNZ */
5187 		case 0xf3:	/* REP/REPE/REPZ */
5188 			ctxt->rep_prefix = ctxt->b;
5189 			break;
5190 		default:
5191 			goto done_prefixes;
5192 		}
5193 
5194 		/* Any legacy prefix after a REX prefix nullifies its effect. */
5195 
5196 		ctxt->rex_prefix = 0;
5197 	}
5198 
5199 done_prefixes:
5200 
5201 	/* REX prefix. */
5202 	if (ctxt->rex_prefix & 8)
5203 		ctxt->op_bytes = 8;	/* REX.W */
5204 
5205 	/* Opcode byte(s). */
5206 	opcode = opcode_table[ctxt->b];
5207 	/* Two-byte opcode? */
5208 	if (ctxt->b == 0x0f) {
5209 		ctxt->opcode_len = 2;
5210 		ctxt->b = insn_fetch(u8, ctxt);
5211 		opcode = twobyte_table[ctxt->b];
5212 
5213 		/* 0F_38 opcode map */
5214 		if (ctxt->b == 0x38) {
5215 			ctxt->opcode_len = 3;
5216 			ctxt->b = insn_fetch(u8, ctxt);
5217 			opcode = opcode_map_0f_38[ctxt->b];
5218 		}
5219 	}
5220 	ctxt->d = opcode.flags;
5221 
5222 	if (ctxt->d & ModRM)
5223 		ctxt->modrm = insn_fetch(u8, ctxt);
5224 
5225 	/* vex-prefix instructions are not implemented */
5226 	if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
5227 	    (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
5228 		ctxt->d = NotImpl;
5229 	}
5230 
5231 	while (ctxt->d & GroupMask) {
5232 		switch (ctxt->d & GroupMask) {
5233 		case Group:
5234 			goffset = (ctxt->modrm >> 3) & 7;
5235 			opcode = opcode.u.group[goffset];
5236 			break;
5237 		case GroupDual:
5238 			goffset = (ctxt->modrm >> 3) & 7;
5239 			if ((ctxt->modrm >> 6) == 3)
5240 				opcode = opcode.u.gdual->mod3[goffset];
5241 			else
5242 				opcode = opcode.u.gdual->mod012[goffset];
5243 			break;
5244 		case RMExt:
5245 			goffset = ctxt->modrm & 7;
5246 			opcode = opcode.u.group[goffset];
5247 			break;
5248 		case Prefix:
5249 			if (ctxt->rep_prefix && op_prefix)
5250 				return EMULATION_FAILED;
5251 			simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
5252 			switch (simd_prefix) {
5253 			case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5254 			case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5255 			case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5256 			case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5257 			}
5258 			break;
5259 		case Escape:
5260 			if (ctxt->modrm > 0xbf) {
5261 				size_t size = ARRAY_SIZE(opcode.u.esc->high);
5262 				u32 index = array_index_nospec(
5263 					ctxt->modrm - 0xc0, size);
5264 
5265 				opcode = opcode.u.esc->high[index];
5266 			} else {
5267 				opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
5268 			}
5269 			break;
5270 		case InstrDual:
5271 			if ((ctxt->modrm >> 6) == 3)
5272 				opcode = opcode.u.idual->mod3;
5273 			else
5274 				opcode = opcode.u.idual->mod012;
5275 			break;
5276 		case ModeDual:
5277 			if (ctxt->mode == X86EMUL_MODE_PROT64)
5278 				opcode = opcode.u.mdual->mode64;
5279 			else
5280 				opcode = opcode.u.mdual->mode32;
5281 			break;
5282 		default:
5283 			return EMULATION_FAILED;
5284 		}
5285 
5286 		ctxt->d &= ~(u64)GroupMask;
5287 		ctxt->d |= opcode.flags;
5288 	}
5289 
5290 	/* Unrecognised? */
5291 	if (ctxt->d == 0)
5292 		return EMULATION_FAILED;
5293 
5294 	ctxt->execute = opcode.u.execute;
5295 
5296 	if (unlikely(emulation_type & EMULTYPE_TRAP_UD) &&
5297 	    likely(!(ctxt->d & EmulateOnUD)))
5298 		return EMULATION_FAILED;
5299 
5300 	if (unlikely(ctxt->d &
5301 	    (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5302 	     No16))) {
5303 		/*
5304 		 * These are copied unconditionally here, and checked unconditionally
5305 		 * in x86_emulate_insn.
5306 		 */
5307 		ctxt->check_perm = opcode.check_perm;
5308 		ctxt->intercept = opcode.intercept;
5309 
5310 		if (ctxt->d & NotImpl)
5311 			return EMULATION_FAILED;
5312 
5313 		if (mode == X86EMUL_MODE_PROT64) {
5314 			if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5315 				ctxt->op_bytes = 8;
5316 			else if (ctxt->d & NearBranch)
5317 				ctxt->op_bytes = 8;
5318 		}
5319 
5320 		if (ctxt->d & Op3264) {
5321 			if (mode == X86EMUL_MODE_PROT64)
5322 				ctxt->op_bytes = 8;
5323 			else
5324 				ctxt->op_bytes = 4;
5325 		}
5326 
5327 		if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5328 			ctxt->op_bytes = 4;
5329 
5330 		if (ctxt->d & Sse)
5331 			ctxt->op_bytes = 16;
5332 		else if (ctxt->d & Mmx)
5333 			ctxt->op_bytes = 8;
5334 	}
5335 
5336 	/* ModRM and SIB bytes. */
5337 	if (ctxt->d & ModRM) {
5338 		rc = decode_modrm(ctxt, &ctxt->memop);
5339 		if (!has_seg_override) {
5340 			has_seg_override = true;
5341 			ctxt->seg_override = ctxt->modrm_seg;
5342 		}
5343 	} else if (ctxt->d & MemAbs)
5344 		rc = decode_abs(ctxt, &ctxt->memop);
5345 	if (rc != X86EMUL_CONTINUE)
5346 		goto done;
5347 
5348 	if (!has_seg_override)
5349 		ctxt->seg_override = VCPU_SREG_DS;
5350 
5351 	ctxt->memop.addr.mem.seg = ctxt->seg_override;
5352 
5353 	/*
5354 	 * Decode and fetch the source operand: register, memory
5355 	 * or immediate.
5356 	 */
5357 	rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
5358 	if (rc != X86EMUL_CONTINUE)
5359 		goto done;
5360 
5361 	/*
5362 	 * Decode and fetch the second source operand: register, memory
5363 	 * or immediate.
5364 	 */
5365 	rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
5366 	if (rc != X86EMUL_CONTINUE)
5367 		goto done;
5368 
5369 	/* Decode and fetch the destination operand: register or memory. */
5370 	rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
5371 
5372 	if (ctxt->rip_relative && likely(ctxt->memopp))
5373 		ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5374 					ctxt->memopp->addr.mem.ea + ctxt->_eip);
5375 
5376 done:
5377 	if (rc == X86EMUL_PROPAGATE_FAULT)
5378 		ctxt->have_exception = true;
5379 	return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
5380 }
5381 
x86_page_table_writing_insn(struct x86_emulate_ctxt * ctxt)5382 bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5383 {
5384 	return ctxt->d & PageTable;
5385 }
5386 
string_insn_completed(struct x86_emulate_ctxt * ctxt)5387 static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5388 {
5389 	/* The second termination condition only applies for REPE
5390 	 * and REPNE. Test if the repeat string operation prefix is
5391 	 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5392 	 * corresponding termination condition according to:
5393 	 * 	- if REPE/REPZ and ZF = 0 then done
5394 	 * 	- if REPNE/REPNZ and ZF = 1 then done
5395 	 */
5396 	if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5397 	     (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5398 	    && (((ctxt->rep_prefix == REPE_PREFIX) &&
5399 		 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
5400 		|| ((ctxt->rep_prefix == REPNE_PREFIX) &&
5401 		    ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
5402 		return true;
5403 
5404 	return false;
5405 }
5406 
flush_pending_x87_faults(struct x86_emulate_ctxt * ctxt)5407 static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5408 {
5409 	int rc;
5410 
5411 	kvm_fpu_get();
5412 	rc = asm_safe("fwait");
5413 	kvm_fpu_put();
5414 
5415 	if (unlikely(rc != X86EMUL_CONTINUE))
5416 		return emulate_exception(ctxt, MF_VECTOR, 0, false);
5417 
5418 	return X86EMUL_CONTINUE;
5419 }
5420 
fetch_possible_mmx_operand(struct operand * op)5421 static void fetch_possible_mmx_operand(struct operand *op)
5422 {
5423 	if (op->type == OP_MM)
5424 		kvm_read_mmx_reg(op->addr.mm, &op->mm_val);
5425 }
5426 
fastop(struct x86_emulate_ctxt * ctxt,fastop_t fop)5427 static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop)
5428 {
5429 	ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
5430 
5431 	if (!(ctxt->d & ByteOp))
5432 		fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
5433 
5434 	asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
5435 	    : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
5436 	      [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
5437 	    : "c"(ctxt->src2.val));
5438 
5439 	ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
5440 	if (!fop) /* exception is returned in fop variable */
5441 		return emulate_de(ctxt);
5442 	return X86EMUL_CONTINUE;
5443 }
5444 
init_decode_cache(struct x86_emulate_ctxt * ctxt)5445 void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5446 {
5447 	memset(&ctxt->rip_relative, 0,
5448 	       (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
5449 
5450 	ctxt->io_read.pos = 0;
5451 	ctxt->io_read.end = 0;
5452 	ctxt->mem_read.end = 0;
5453 }
5454 
x86_emulate_insn(struct x86_emulate_ctxt * ctxt)5455 int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
5456 {
5457 	const struct x86_emulate_ops *ops = ctxt->ops;
5458 	int rc = X86EMUL_CONTINUE;
5459 	int saved_dst_type = ctxt->dst.type;
5460 	unsigned emul_flags;
5461 
5462 	ctxt->mem_read.pos = 0;
5463 
5464 	/* LOCK prefix is allowed only with some instructions */
5465 	if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
5466 		rc = emulate_ud(ctxt);
5467 		goto done;
5468 	}
5469 
5470 	if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
5471 		rc = emulate_ud(ctxt);
5472 		goto done;
5473 	}
5474 
5475 	emul_flags = ctxt->ops->get_hflags(ctxt);
5476 	if (unlikely(ctxt->d &
5477 		     (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5478 		if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5479 				(ctxt->d & Undefined)) {
5480 			rc = emulate_ud(ctxt);
5481 			goto done;
5482 		}
5483 
5484 		if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5485 		    || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5486 			rc = emulate_ud(ctxt);
5487 			goto done;
5488 		}
5489 
5490 		if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5491 			rc = emulate_nm(ctxt);
5492 			goto done;
5493 		}
5494 
5495 		if (ctxt->d & Mmx) {
5496 			rc = flush_pending_x87_faults(ctxt);
5497 			if (rc != X86EMUL_CONTINUE)
5498 				goto done;
5499 			/*
5500 			 * Now that we know the fpu is exception safe, we can fetch
5501 			 * operands from it.
5502 			 */
5503 			fetch_possible_mmx_operand(&ctxt->src);
5504 			fetch_possible_mmx_operand(&ctxt->src2);
5505 			if (!(ctxt->d & Mov))
5506 				fetch_possible_mmx_operand(&ctxt->dst);
5507 		}
5508 
5509 		if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
5510 			rc = emulator_check_intercept(ctxt, ctxt->intercept,
5511 						      X86_ICPT_PRE_EXCEPT);
5512 			if (rc != X86EMUL_CONTINUE)
5513 				goto done;
5514 		}
5515 
5516 		/* Instruction can only be executed in protected mode */
5517 		if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5518 			rc = emulate_ud(ctxt);
5519 			goto done;
5520 		}
5521 
5522 		/* Privileged instruction can be executed only in CPL=0 */
5523 		if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
5524 			if (ctxt->d & PrivUD)
5525 				rc = emulate_ud(ctxt);
5526 			else
5527 				rc = emulate_gp(ctxt, 0);
5528 			goto done;
5529 		}
5530 
5531 		/* Do instruction specific permission checks */
5532 		if (ctxt->d & CheckPerm) {
5533 			rc = ctxt->check_perm(ctxt);
5534 			if (rc != X86EMUL_CONTINUE)
5535 				goto done;
5536 		}
5537 
5538 		if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
5539 			rc = emulator_check_intercept(ctxt, ctxt->intercept,
5540 						      X86_ICPT_POST_EXCEPT);
5541 			if (rc != X86EMUL_CONTINUE)
5542 				goto done;
5543 		}
5544 
5545 		if (ctxt->rep_prefix && (ctxt->d & String)) {
5546 			/* All REP prefixes have the same first termination condition */
5547 			if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
5548 				string_registers_quirk(ctxt);
5549 				ctxt->eip = ctxt->_eip;
5550 				ctxt->eflags &= ~X86_EFLAGS_RF;
5551 				goto done;
5552 			}
5553 		}
5554 	}
5555 
5556 	if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5557 		rc = segmented_read(ctxt, ctxt->src.addr.mem,
5558 				    ctxt->src.valptr, ctxt->src.bytes);
5559 		if (rc != X86EMUL_CONTINUE)
5560 			goto done;
5561 		ctxt->src.orig_val64 = ctxt->src.val64;
5562 	}
5563 
5564 	if (ctxt->src2.type == OP_MEM) {
5565 		rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5566 				    &ctxt->src2.val, ctxt->src2.bytes);
5567 		if (rc != X86EMUL_CONTINUE)
5568 			goto done;
5569 	}
5570 
5571 	if ((ctxt->d & DstMask) == ImplicitOps)
5572 		goto special_insn;
5573 
5574 
5575 	if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
5576 		/* optimisation - avoid slow emulated read if Mov */
5577 		rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5578 				   &ctxt->dst.val, ctxt->dst.bytes);
5579 		if (rc != X86EMUL_CONTINUE) {
5580 			if (!(ctxt->d & NoWrite) &&
5581 			    rc == X86EMUL_PROPAGATE_FAULT &&
5582 			    ctxt->exception.vector == PF_VECTOR)
5583 				ctxt->exception.error_code |= PFERR_WRITE_MASK;
5584 			goto done;
5585 		}
5586 	}
5587 	/* Copy full 64-bit value for CMPXCHG8B.  */
5588 	ctxt->dst.orig_val64 = ctxt->dst.val64;
5589 
5590 special_insn:
5591 
5592 	if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
5593 		rc = emulator_check_intercept(ctxt, ctxt->intercept,
5594 					      X86_ICPT_POST_MEMACCESS);
5595 		if (rc != X86EMUL_CONTINUE)
5596 			goto done;
5597 	}
5598 
5599 	if (ctxt->rep_prefix && (ctxt->d & String))
5600 		ctxt->eflags |= X86_EFLAGS_RF;
5601 	else
5602 		ctxt->eflags &= ~X86_EFLAGS_RF;
5603 
5604 	if (ctxt->execute) {
5605 		if (ctxt->d & Fastop)
5606 			rc = fastop(ctxt, ctxt->fop);
5607 		else
5608 			rc = ctxt->execute(ctxt);
5609 		if (rc != X86EMUL_CONTINUE)
5610 			goto done;
5611 		goto writeback;
5612 	}
5613 
5614 	if (ctxt->opcode_len == 2)
5615 		goto twobyte_insn;
5616 	else if (ctxt->opcode_len == 3)
5617 		goto threebyte_insn;
5618 
5619 	switch (ctxt->b) {
5620 	case 0x70 ... 0x7f: /* jcc (short) */
5621 		if (test_cc(ctxt->b, ctxt->eflags))
5622 			rc = jmp_rel(ctxt, ctxt->src.val);
5623 		break;
5624 	case 0x8d: /* lea r16/r32, m */
5625 		ctxt->dst.val = ctxt->src.addr.mem.ea;
5626 		break;
5627 	case 0x90 ... 0x97: /* nop / xchg reg, rax */
5628 		if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
5629 			ctxt->dst.type = OP_NONE;
5630 		else
5631 			rc = em_xchg(ctxt);
5632 		break;
5633 	case 0x98: /* cbw/cwde/cdqe */
5634 		switch (ctxt->op_bytes) {
5635 		case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5636 		case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5637 		case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
5638 		}
5639 		break;
5640 	case 0xcc:		/* int3 */
5641 		rc = emulate_int(ctxt, 3);
5642 		break;
5643 	case 0xcd:		/* int n */
5644 		rc = emulate_int(ctxt, ctxt->src.val);
5645 		break;
5646 	case 0xce:		/* into */
5647 		if (ctxt->eflags & X86_EFLAGS_OF)
5648 			rc = emulate_int(ctxt, 4);
5649 		break;
5650 	case 0xe9: /* jmp rel */
5651 	case 0xeb: /* jmp rel short */
5652 		rc = jmp_rel(ctxt, ctxt->src.val);
5653 		ctxt->dst.type = OP_NONE; /* Disable writeback. */
5654 		break;
5655 	case 0xf4:              /* hlt */
5656 		ctxt->ops->halt(ctxt);
5657 		break;
5658 	case 0xf5:	/* cmc */
5659 		/* complement carry flag from eflags reg */
5660 		ctxt->eflags ^= X86_EFLAGS_CF;
5661 		break;
5662 	case 0xf8: /* clc */
5663 		ctxt->eflags &= ~X86_EFLAGS_CF;
5664 		break;
5665 	case 0xf9: /* stc */
5666 		ctxt->eflags |= X86_EFLAGS_CF;
5667 		break;
5668 	case 0xfc: /* cld */
5669 		ctxt->eflags &= ~X86_EFLAGS_DF;
5670 		break;
5671 	case 0xfd: /* std */
5672 		ctxt->eflags |= X86_EFLAGS_DF;
5673 		break;
5674 	default:
5675 		goto cannot_emulate;
5676 	}
5677 
5678 	if (rc != X86EMUL_CONTINUE)
5679 		goto done;
5680 
5681 writeback:
5682 	if (ctxt->d & SrcWrite) {
5683 		BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5684 		rc = writeback(ctxt, &ctxt->src);
5685 		if (rc != X86EMUL_CONTINUE)
5686 			goto done;
5687 	}
5688 	if (!(ctxt->d & NoWrite)) {
5689 		rc = writeback(ctxt, &ctxt->dst);
5690 		if (rc != X86EMUL_CONTINUE)
5691 			goto done;
5692 	}
5693 
5694 	/*
5695 	 * restore dst type in case the decoding will be reused
5696 	 * (happens for string instruction )
5697 	 */
5698 	ctxt->dst.type = saved_dst_type;
5699 
5700 	if ((ctxt->d & SrcMask) == SrcSI)
5701 		string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
5702 
5703 	if ((ctxt->d & DstMask) == DstDI)
5704 		string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
5705 
5706 	if (ctxt->rep_prefix && (ctxt->d & String)) {
5707 		unsigned int count;
5708 		struct read_cache *r = &ctxt->io_read;
5709 		if ((ctxt->d & SrcMask) == SrcSI)
5710 			count = ctxt->src.count;
5711 		else
5712 			count = ctxt->dst.count;
5713 		register_address_increment(ctxt, VCPU_REGS_RCX, -count);
5714 
5715 		if (!string_insn_completed(ctxt)) {
5716 			/*
5717 			 * Re-enter guest when pio read ahead buffer is empty
5718 			 * or, if it is not used, after each 1024 iteration.
5719 			 */
5720 			if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
5721 			    (r->end == 0 || r->end != r->pos)) {
5722 				/*
5723 				 * Reset read cache. Usually happens before
5724 				 * decode, but since instruction is restarted
5725 				 * we have to do it here.
5726 				 */
5727 				ctxt->mem_read.end = 0;
5728 				writeback_registers(ctxt);
5729 				return EMULATION_RESTART;
5730 			}
5731 			goto done; /* skip rip writeback */
5732 		}
5733 		ctxt->eflags &= ~X86_EFLAGS_RF;
5734 	}
5735 
5736 	ctxt->eip = ctxt->_eip;
5737 	if (ctxt->mode != X86EMUL_MODE_PROT64)
5738 		ctxt->eip = (u32)ctxt->_eip;
5739 
5740 done:
5741 	if (rc == X86EMUL_PROPAGATE_FAULT) {
5742 		WARN_ON(ctxt->exception.vector > 0x1f);
5743 		ctxt->have_exception = true;
5744 	}
5745 	if (rc == X86EMUL_INTERCEPTED)
5746 		return EMULATION_INTERCEPTED;
5747 
5748 	if (rc == X86EMUL_CONTINUE)
5749 		writeback_registers(ctxt);
5750 
5751 	return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
5752 
5753 twobyte_insn:
5754 	switch (ctxt->b) {
5755 	case 0x09:		/* wbinvd */
5756 		(ctxt->ops->wbinvd)(ctxt);
5757 		break;
5758 	case 0x08:		/* invd */
5759 	case 0x0d:		/* GrpP (prefetch) */
5760 	case 0x18:		/* Grp16 (prefetch/nop) */
5761 	case 0x1f:		/* nop */
5762 		break;
5763 	case 0x20: /* mov cr, reg */
5764 		ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
5765 		break;
5766 	case 0x21: /* mov from dr to reg */
5767 		ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
5768 		break;
5769 	case 0x40 ... 0x4f:	/* cmov */
5770 		if (test_cc(ctxt->b, ctxt->eflags))
5771 			ctxt->dst.val = ctxt->src.val;
5772 		else if (ctxt->op_bytes != 4)
5773 			ctxt->dst.type = OP_NONE; /* no writeback */
5774 		break;
5775 	case 0x80 ... 0x8f: /* jnz rel, etc*/
5776 		if (test_cc(ctxt->b, ctxt->eflags))
5777 			rc = jmp_rel(ctxt, ctxt->src.val);
5778 		break;
5779 	case 0x90 ... 0x9f:     /* setcc r/m8 */
5780 		ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
5781 		break;
5782 	case 0xb6 ... 0xb7:	/* movzx */
5783 		ctxt->dst.bytes = ctxt->op_bytes;
5784 		ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
5785 						       : (u16) ctxt->src.val;
5786 		break;
5787 	case 0xbe ... 0xbf:	/* movsx */
5788 		ctxt->dst.bytes = ctxt->op_bytes;
5789 		ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
5790 							(s16) ctxt->src.val;
5791 		break;
5792 	default:
5793 		goto cannot_emulate;
5794 	}
5795 
5796 threebyte_insn:
5797 
5798 	if (rc != X86EMUL_CONTINUE)
5799 		goto done;
5800 
5801 	goto writeback;
5802 
5803 cannot_emulate:
5804 	return EMULATION_FAILED;
5805 }
5806 
emulator_invalidate_register_cache(struct x86_emulate_ctxt * ctxt)5807 void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5808 {
5809 	invalidate_registers(ctxt);
5810 }
5811 
emulator_writeback_register_cache(struct x86_emulate_ctxt * ctxt)5812 void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5813 {
5814 	writeback_registers(ctxt);
5815 }
5816 
emulator_can_use_gpa(struct x86_emulate_ctxt * ctxt)5817 bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
5818 {
5819 	if (ctxt->rep_prefix && (ctxt->d & String))
5820 		return false;
5821 
5822 	if (ctxt->d & TwoMemOp)
5823 		return false;
5824 
5825 	return true;
5826 }
5827