blob: 6f5a3b076341025962c2ee765897d36409e7743e [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity56e82312009-08-12 15:04:37 +030025#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020026#include <linux/stringify.h>
Nadav Amit3db176d2015-04-19 21:12:59 +030027#include <asm/debugreg.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivity820207c2013-02-09 11:31:45 +020064#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
65#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
Avi Kivitya9945542011-09-13 10:45:41 +030066
Avi Kivity0fe59122011-09-13 10:45:47 +030067#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030068#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030069
70/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 * Opcode effective-address decode tables.
72 * Note that we only emulate instructions that have at least one memory
73 * operand (excluding implicit stack references). We assume that stack
74 * references and instruction fetches will never occur in special memory
75 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
76 * not be handled.
77 */
78
79/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030080#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080081/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030082#define DstShift 1
83#define ImplicitOps (OpImplicit << DstShift)
84#define DstReg (OpReg << DstShift)
85#define DstMem (OpMem << DstShift)
86#define DstAcc (OpAcc << DstShift)
87#define DstDI (OpDI << DstShift)
88#define DstMem64 (OpMem64 << DstShift)
Nadav Amit16bebef2014-12-25 02:52:18 +020089#define DstMem16 (OpMem16 << DstShift)
Avi Kivitya9945542011-09-13 10:45:41 +030090#define DstImmUByte (OpImmUByte << DstShift)
91#define DstDX (OpDX << DstShift)
Avi Kivity820207c2013-02-09 11:31:45 +020092#define DstAccLo (OpAccLo << DstShift)
Avi Kivitya9945542011-09-13 10:45:41 +030093#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030095#define SrcShift 6
96#define SrcNone (OpNone << SrcShift)
97#define SrcReg (OpReg << SrcShift)
98#define SrcMem (OpMem << SrcShift)
99#define SrcMem16 (OpMem16 << SrcShift)
100#define SrcMem32 (OpMem32 << SrcShift)
101#define SrcImm (OpImm << SrcShift)
102#define SrcImmByte (OpImmByte << SrcShift)
103#define SrcOne (OpOne << SrcShift)
104#define SrcImmUByte (OpImmUByte << SrcShift)
105#define SrcImmU (OpImmU << SrcShift)
106#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200107#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300108#define SrcImmFAddr (OpImmFAddr << SrcShift)
109#define SrcMemFAddr (OpMemFAddr << SrcShift)
110#define SrcAcc (OpAcc << SrcShift)
111#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200112#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300113#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200114#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity820207c2013-02-09 11:31:45 +0200115#define SrcAccHi (OpAccHi << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300116#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300117#define BitOp (1<<11)
118#define MemAbs (1<<12) /* Memory operand is absolute displacement */
119#define String (1<<13) /* String instruction (rep capable) */
120#define Stack (1<<14) /* Stack instruction (push/pop) */
121#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
122#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
123#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
124#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
125#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200126#define Escape (5<<15) /* Escape to coprocessor instruction */
Nadav Amit39f062f2014-11-26 15:47:18 +0200127#define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
Nadav Amit2276b512015-01-26 09:32:24 +0200128#define ModeDual (7<<15) /* Different instruction for 32/64 bit */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300129#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300130/* Generic ModRM decode. */
131#define ModRM (1<<19)
132/* Destination is only written; never read. */
133#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300134/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200135#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Borislav Petkovb51e9742013-09-22 16:44:52 +0200136#define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
Avi Kivity5a506b12010-08-01 15:10:29 +0300137#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300138#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300139#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200140#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200141#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300142#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800143#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300144#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100145/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300146#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300147#define Src2None (OpNone << Src2Shift)
Avi Kivityab2c5ce2013-02-09 11:31:46 +0200148#define Src2Mem (OpMem << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300149#define Src2CL (OpCL << Src2Shift)
150#define Src2ImmByte (OpImmByte << Src2Shift)
151#define Src2One (OpOne << Src2Shift)
152#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300153#define Src2ES (OpES << Src2Shift)
154#define Src2CS (OpCS << Src2Shift)
155#define Src2SS (OpSS << Src2Shift)
156#define Src2DS (OpDS << Src2Shift)
157#define Src2FS (OpFS << Src2Shift)
158#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300159#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300160#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300161#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
162#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
163#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200164#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200165#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200166#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Nadav Amit9b88ae92014-05-25 23:05:21 +0300167#define NoMod ((u64)1 << 47) /* Mod field is ignored */
Paolo Bonzinid40a6892014-03-27 11:58:02 +0100168#define Intercept ((u64)1 << 48) /* Has valid intercept field */
169#define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
Nadav Amit68efa762014-06-18 17:19:35 +0300170#define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
Nadav Amit58b70752014-10-24 11:35:09 +0300171#define NearBranch ((u64)1 << 52) /* Near branches */
Nadav Amited9aad22014-11-02 11:55:00 +0200172#define No16 ((u64)1 << 53) /* No 16 bit operand */
Nadav Amitab708092014-12-25 02:52:21 +0200173#define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100174#define Aligned16 ((u64)1 << 55) /* Aligned to 16 byte boundary (e.g. FXSAVE) */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175
Avi Kivity820207c2013-02-09 11:31:45 +0200176#define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177
Avi Kivityd0e53322010-07-29 15:11:54 +0300178#define X2(x...) x, x
179#define X3(x...) X2(x), x
180#define X4(x...) X2(x), X2(x)
181#define X5(x...) X4(x), x
182#define X6(x...) X4(x), X2(x)
183#define X7(x...) X4(x), X3(x)
184#define X8(x...) X4(x), X4(x)
185#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300186
Avi Kivitye28bbd42013-01-04 16:18:48 +0200187#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
188#define FASTOP_SIZE 8
189
190/*
191 * fastop functions have a special calling convention:
192 *
Avi Kivity017da7b2013-02-09 11:31:47 +0200193 * dst: rax (in/out)
194 * src: rdx (in/out)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 * src2: rcx (in)
196 * flags: rflags (in/out)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200197 * ex: rsi (in:fastop pointer, out:zero if exception)
Avi Kivitye28bbd42013-01-04 16:18:48 +0200198 *
199 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
200 * different operand sizes can be reached by calculation, rather than a jump
201 * table (which would be bigger than the code).
202 *
203 * fastop functions are declared as taking a never-defined fastop parameter,
204 * so they can't be called from C directly.
205 */
206
207struct fastop;
208
Avi Kivityd65b1de2010-07-29 15:11:35 +0300209struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300210 u64 flags : 56;
211 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300212 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300213 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200214 const struct opcode *group;
215 const struct group_dual *gdual;
216 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200217 const struct escape *esc;
Nadav Amit39f062f2014-11-26 15:47:18 +0200218 const struct instr_dual *idual;
Nadav Amit2276b512015-01-26 09:32:24 +0200219 const struct mode_dual *mdual;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200220 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300221 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200222 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300223};
224
225struct group_dual {
226 struct opcode mod012[8];
227 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300228};
229
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200230struct gprefix {
231 struct opcode pfx_no;
232 struct opcode pfx_66;
233 struct opcode pfx_f2;
234 struct opcode pfx_f3;
235};
236
Gleb Natapov045a2822012-12-20 16:57:43 +0200237struct escape {
238 struct opcode op[8];
239 struct opcode high[64];
240};
241
Nadav Amit39f062f2014-11-26 15:47:18 +0200242struct instr_dual {
243 struct opcode mod012;
244 struct opcode mod3;
245};
246
Nadav Amit2276b512015-01-26 09:32:24 +0200247struct mode_dual {
248 struct opcode mode32;
249 struct opcode mode64;
250};
251
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300252#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300253
Nadav Amit3dc4bc42014-12-25 02:52:19 +0200254enum x86_transfer_type {
255 X86_TRANSFER_NONE,
256 X86_TRANSFER_CALL_JMP,
257 X86_TRANSFER_RET,
258 X86_TRANSFER_TASK_SWITCH,
259};
260
Avi Kivitydd856ef2012-08-27 23:46:17 +0300261static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
262{
263 if (!(ctxt->regs_valid & (1 << nr))) {
264 ctxt->regs_valid |= 1 << nr;
265 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
266 }
267 return ctxt->_regs[nr];
268}
269
270static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
271{
272 ctxt->regs_valid |= 1 << nr;
273 ctxt->regs_dirty |= 1 << nr;
274 return &ctxt->_regs[nr];
275}
276
277static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
278{
279 reg_read(ctxt, nr);
280 return reg_write(ctxt, nr);
281}
282
283static void writeback_registers(struct x86_emulate_ctxt *ctxt)
284{
285 unsigned reg;
286
287 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
288 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
289}
290
291static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
292{
293 ctxt->regs_dirty = 0;
294 ctxt->regs_valid = 0;
295}
296
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 * These EFLAGS bits are restored from saved value during emulation, and
299 * any changes are written back to the saved value after emulation.
300 */
Nadav Amit0efb0442015-03-29 16:33:03 +0300301#define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
302 X86_EFLAGS_PF|X86_EFLAGS_CF)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303
Avi Kivitydda96d82008-11-26 15:14:10 +0200304#ifdef CONFIG_X86_64
305#define ON64(x) x
306#else
307#define ON64(x)
308#endif
309
Avi Kivity4d758342013-01-19 19:51:55 +0200310static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
311
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600312#define FOP_FUNC(name) \
313 ".align " __stringify(FASTOP_SIZE) " \n\t" \
314 ".type " name ", @function \n\t" \
315 name ":\n\t"
316
Avi Kivityb7d491e2013-01-04 16:18:49 +0200317#define FOP_RET "ret \n\t"
318
319#define FOP_START(op) \
320 extern void em_##op(struct fastop *fake); \
321 asm(".pushsection .text, \"ax\" \n\t" \
322 ".global em_" #op " \n\t" \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600323 FOP_FUNC("em_" #op)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200324
325#define FOP_END \
326 ".popsection")
327
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600328#define FOPNOP() \
329 FOP_FUNC(__stringify(__UNIQUE_ID(nop))) \
330 FOP_RET
Avi Kivity0bdea062013-01-19 19:51:50 +0200331
Avi Kivityb7d491e2013-01-04 16:18:49 +0200332#define FOP1E(op, dst) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600333 FOP_FUNC(#op "_" #dst) \
334 "10: " #op " %" #dst " \n\t" FOP_RET
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200335
336#define FOP1EEX(op, dst) \
337 FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
Avi Kivityb7d491e2013-01-04 16:18:49 +0200338
339#define FASTOP1(op) \
340 FOP_START(op) \
341 FOP1E(op##b, al) \
342 FOP1E(op##w, ax) \
343 FOP1E(op##l, eax) \
344 ON64(FOP1E(op##q, rax)) \
345 FOP_END
346
Avi Kivityb9fa4092013-02-09 11:31:48 +0200347/* 1-operand, using src2 (for MUL/DIV r/m) */
348#define FASTOP1SRC2(op, name) \
349 FOP_START(name) \
350 FOP1E(op, cl) \
351 FOP1E(op, cx) \
352 FOP1E(op, ecx) \
353 ON64(FOP1E(op, rcx)) \
354 FOP_END
355
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200356/* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
357#define FASTOP1SRC2EX(op, name) \
358 FOP_START(name) \
359 FOP1EEX(op, cl) \
360 FOP1EEX(op, cx) \
361 FOP1EEX(op, ecx) \
362 ON64(FOP1EEX(op, rcx)) \
363 FOP_END
364
Avi Kivityf7857f32013-01-04 16:18:53 +0200365#define FOP2E(op, dst, src) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600366 FOP_FUNC(#op "_" #dst "_" #src) \
367 #op " %" #src ", %" #dst " \n\t" FOP_RET
Avi Kivityf7857f32013-01-04 16:18:53 +0200368
369#define FASTOP2(op) \
370 FOP_START(op) \
Avi Kivity017da7b2013-02-09 11:31:47 +0200371 FOP2E(op##b, al, dl) \
372 FOP2E(op##w, ax, dx) \
373 FOP2E(op##l, eax, edx) \
374 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivityf7857f32013-01-04 16:18:53 +0200375 FOP_END
376
Avi Kivity11c363b2013-01-19 19:51:54 +0200377/* 2 operand, word only */
378#define FASTOP2W(op) \
379 FOP_START(op) \
380 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200381 FOP2E(op##w, ax, dx) \
382 FOP2E(op##l, eax, edx) \
383 ON64(FOP2E(op##q, rax, rdx)) \
Avi Kivity11c363b2013-01-19 19:51:54 +0200384 FOP_END
385
Avi Kivity007a3b52013-01-19 19:51:51 +0200386/* 2 operand, src is CL */
387#define FASTOP2CL(op) \
388 FOP_START(op) \
389 FOP2E(op##b, al, cl) \
390 FOP2E(op##w, ax, cl) \
391 FOP2E(op##l, eax, cl) \
392 ON64(FOP2E(op##q, rax, cl)) \
393 FOP_END
394
Nadav Amit5aca3722014-11-02 11:54:50 +0200395/* 2 operand, src and dest are reversed */
396#define FASTOP2R(op, name) \
397 FOP_START(name) \
398 FOP2E(op##b, dl, al) \
399 FOP2E(op##w, dx, ax) \
400 FOP2E(op##l, edx, eax) \
401 ON64(FOP2E(op##q, rdx, rax)) \
402 FOP_END
403
Avi Kivity0bdea062013-01-19 19:51:50 +0200404#define FOP3E(op, dst, src, src2) \
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600405 FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
406 #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
Avi Kivity0bdea062013-01-19 19:51:50 +0200407
408/* 3-operand, word-only, src2=cl */
409#define FASTOP3WCL(op) \
410 FOP_START(op) \
411 FOPNOP() \
Avi Kivity017da7b2013-02-09 11:31:47 +0200412 FOP3E(op##w, ax, dx, cl) \
413 FOP3E(op##l, eax, edx, cl) \
414 ON64(FOP3E(op##q, rax, rdx, cl)) \
Avi Kivity0bdea062013-01-19 19:51:50 +0200415 FOP_END
416
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200417/* Special case for SETcc - 1 instruction per cc */
Josh Poimboeuf1482a082016-01-21 16:49:29 -0600418#define FOP_SETCC(op) \
419 ".align 4 \n\t" \
420 ".type " #op ", @function \n\t" \
421 #op ": \n\t" \
422 #op " %al \n\t" \
423 FOP_RET
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200424
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200425asm(".global kvm_fastop_exception \n"
426 "kvm_fastop_exception: xor %esi, %esi; ret");
427
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200428FOP_START(setcc)
429FOP_SETCC(seto)
430FOP_SETCC(setno)
431FOP_SETCC(setc)
432FOP_SETCC(setnc)
433FOP_SETCC(setz)
434FOP_SETCC(setnz)
435FOP_SETCC(setbe)
436FOP_SETCC(setnbe)
437FOP_SETCC(sets)
438FOP_SETCC(setns)
439FOP_SETCC(setp)
440FOP_SETCC(setnp)
441FOP_SETCC(setl)
442FOP_SETCC(setnl)
443FOP_SETCC(setle)
444FOP_SETCC(setnle)
445FOP_END;
446
Paolo Bonzini326f5782013-05-09 11:32:51 +0200447FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
448FOP_END;
449
Radim Krčmářaae8f342016-11-08 20:54:18 +0100450/*
451 * XXX: inoutclob user must know where the argument is being expanded.
452 * Relying on CC_HAVE_ASM_GOTO would allow us to remove _fault.
453 */
454#define asm_safe(insn, inoutclob...) \
455({ \
456 int _fault = 0; \
457 \
458 asm volatile("1:" insn "\n" \
459 "2:\n" \
460 ".pushsection .fixup, \"ax\"\n" \
461 "3: movl $1, %[_fault]\n" \
462 " jmp 2b\n" \
463 ".popsection\n" \
464 _ASM_EXTABLE(1b, 3b) \
465 : [_fault] "+qm"(_fault) inoutclob ); \
466 \
467 _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
468})
469
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200470static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
471 enum x86_intercept intercept,
472 enum x86_intercept_stage stage)
473{
474 struct x86_instruction_info info = {
475 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 .rep_prefix = ctxt->rep_prefix,
477 .modrm_mod = ctxt->modrm_mod,
478 .modrm_reg = ctxt->modrm_reg,
479 .modrm_rm = ctxt->modrm_rm,
480 .src_val = ctxt->src.val64,
Jan Kiszka6cbc5f52014-06-30 12:52:55 +0200481 .dst_val = ctxt->dst.val64,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482 .src_bytes = ctxt->src.bytes,
483 .dst_bytes = ctxt->dst.bytes,
484 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200485 .next_rip = ctxt->eip,
486 };
487
Avi Kivity29535382011-04-20 13:37:53 +0300488 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200489}
490
Avi Kivityf47cfa32012-06-07 17:49:24 +0300491static void assign_masked(ulong *dest, ulong src, ulong mask)
492{
493 *dest = (*dest & ~mask) | (src & mask);
494}
495
Nadav Amit6fd8e122015-03-30 15:39:20 +0300496static void assign_register(unsigned long *reg, u64 val, int bytes)
497{
498 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
499 switch (bytes) {
500 case 1:
501 *(u8 *)reg = (u8)val;
502 break;
503 case 2:
504 *(u16 *)reg = (u16)val;
505 break;
506 case 4:
507 *reg = (u32)val;
508 break; /* 64b: zero-extend */
509 case 8:
510 *reg = val;
511 break;
512 }
513}
514
Avi Kivity9dac77f2011-06-01 15:34:25 +0300515static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800516{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300517 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800518}
519
Avi Kivityf47cfa32012-06-07 17:49:24 +0300520static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
521{
522 u16 sel;
523 struct desc_struct ss;
524
525 if (ctxt->mode == X86EMUL_MODE_PROT64)
526 return ~0UL;
527 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
528 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
529}
530
Avi Kivity612e89f2012-06-12 20:03:23 +0300531static int stack_size(struct x86_emulate_ctxt *ctxt)
532{
533 return (__fls(stack_mask(ctxt)) + 1) >> 3;
534}
535
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800537static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300538address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800539{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300540 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800541 return reg;
542 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300543 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800544}
545
546static inline unsigned long
Paolo Bonzini01485a22014-11-19 18:25:08 +0100547register_address(struct x86_emulate_ctxt *ctxt, int reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800548{
Paolo Bonzini01485a22014-11-19 18:25:08 +0100549 return address_mask(ctxt, reg_read(ctxt, reg));
Harvey Harrisone4706772008-02-19 07:40:38 -0800550}
551
Avi Kivity5ad105e2012-08-19 14:34:31 +0300552static void masked_increment(ulong *reg, ulong mask, int inc)
553{
554 assign_masked(reg, *reg + inc, mask);
555}
556
Harvey Harrison7a9572752008-02-19 07:40:41 -0800557static inline void
Paolo Bonzini01485a22014-11-19 18:25:08 +0100558register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800559{
Nadav Amitee122a72015-04-28 13:06:00 +0300560 ulong *preg = reg_rmw(ctxt, reg);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300561
Nadav Amitee122a72015-04-28 13:06:00 +0300562 assign_register(preg, *preg + inc, ctxt->ad_bytes);
Avi Kivity5ad105e2012-08-19 14:34:31 +0300563}
564
565static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
566{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300567 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800568}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569
Avi Kivity56697682011-04-03 14:08:51 +0300570static u32 desc_limit_scaled(struct desc_struct *desc)
571{
572 u32 limit = get_desc_limit(desc);
573
574 return desc->g ? (limit << 12) | 0xfff : limit;
575}
576
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900577static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300578{
579 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
580 return 0;
581
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900582 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300583}
584
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200585static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
586 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300587{
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +0200588 WARN_ON(vec > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +0200589 ctxt->exception.vector = vec;
590 ctxt->exception.error_code = error;
591 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200592 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300593}
594
Joerg Roedel3b88e412011-04-04 12:39:29 +0200595static int emulate_db(struct x86_emulate_ctxt *ctxt)
596{
597 return emulate_exception(ctxt, DB_VECTOR, 0, false);
598}
599
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200600static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300601{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200602 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300603}
604
Avi Kivity618ff152011-04-03 12:32:09 +0300605static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
606{
607 return emulate_exception(ctxt, SS_VECTOR, err, true);
608}
609
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200610static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300611{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200612 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300613}
614
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200615static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300616{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200617 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300618}
619
Avi Kivity34d1f492010-08-26 11:59:01 +0300620static int emulate_de(struct x86_emulate_ctxt *ctxt)
621{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200622 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300623}
624
Avi Kivity12537912011-03-29 11:41:27 +0200625static int emulate_nm(struct x86_emulate_ctxt *ctxt)
626{
627 return emulate_exception(ctxt, NM_VECTOR, 0, false);
628}
629
Avi Kivity1aa36612011-04-27 13:20:30 +0300630static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
631{
632 u16 selector;
633 struct desc_struct desc;
634
635 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
636 return selector;
637}
638
639static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
640 unsigned seg)
641{
642 u16 dummy;
643 u32 base3;
644 struct desc_struct desc;
645
646 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
647 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
648}
649
Avi Kivity1c11b372012-04-09 18:39:59 +0300650/*
651 * x86 defines three classes of vector instructions: explicitly
652 * aligned, explicitly unaligned, and the rest, which change behaviour
653 * depending on whether they're AVX encoded or not.
654 *
655 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100656 * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
657 * 512 bytes of data must be aligned to a 16 byte boundary.
Avi Kivity1c11b372012-04-09 18:39:59 +0300658 */
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100659static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
Avi Kivity1c11b372012-04-09 18:39:59 +0300660{
661 if (likely(size < 16))
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100662 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300663
664 if (ctxt->d & Aligned)
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100665 return size;
Avi Kivity1c11b372012-04-09 18:39:59 +0300666 else if (ctxt->d & Unaligned)
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100667 return 1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300668 else if (ctxt->d & Avx)
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100669 return 1;
670 else if (ctxt->d & Aligned16)
671 return 16;
Avi Kivity1c11b372012-04-09 18:39:59 +0300672 else
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100673 return size;
Avi Kivity1c11b372012-04-09 18:39:59 +0300674}
675
Paolo Bonzinid09155d2014-10-27 14:54:44 +0100676static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
677 struct segmented_address addr,
678 unsigned *max_size, unsigned size,
679 bool write, bool fetch,
Nadav Amitd50eaa12014-11-19 17:43:11 +0200680 enum x86emul_mode mode, ulong *linear)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300681{
Avi Kivity618ff152011-04-03 12:32:09 +0300682 struct desc_struct desc;
683 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300684 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300685 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300686 u16 sel;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300687
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900688 la = seg_base(ctxt, addr.seg) + addr.ea;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100689 *max_size = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200690 switch (mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300691 case X86EMUL_MODE_PROT64:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100692 *linear = la;
Nadav Amit4be4de72014-09-18 22:39:40 +0300693 if (is_noncanonical_address(la))
Nadav Amitabc7d8a2014-11-19 17:43:12 +0200694 goto bad;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100695
696 *max_size = min_t(u64, ~0u, (1ull << 48) - la);
697 if (size > *max_size)
698 goto bad;
Avi Kivity618ff152011-04-03 12:32:09 +0300699 break;
700 default:
Paolo Bonzini0c1d77f2016-02-19 18:07:21 +0100701 *linear = la = (u32)la;
Avi Kivity1aa36612011-04-27 13:20:30 +0300702 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
703 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300704 if (!usable)
705 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200706 /* code segment in protected mode or read-only data segment */
707 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
708 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300709 goto bad;
710 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400711 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300712 goto bad;
713 lim = desc_limit_scaled(&desc);
Paolo Bonzini997b0412014-11-19 18:33:38 +0100714 if (!(desc.type & 8) && (desc.type & 4)) {
Guo Chaofc058682012-06-28 15:19:51 +0800715 /* expand-down segment */
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100716 if (addr.ea <= lim)
Avi Kivity618ff152011-04-03 12:32:09 +0300717 goto bad;
718 lim = desc.d ? 0xffffffff : 0xffff;
Avi Kivity618ff152011-04-03 12:32:09 +0300719 }
Paolo Bonzini997b0412014-11-19 18:33:38 +0100720 if (addr.ea > lim)
721 goto bad;
Nadav Amitbac155312015-01-26 09:32:26 +0200722 if (lim == 0xffffffff)
723 *max_size = ~0u;
724 else {
725 *max_size = (u64)lim + 1 - addr.ea;
726 if (size > *max_size)
727 goto bad;
728 }
Avi Kivity618ff152011-04-03 12:32:09 +0300729 break;
730 }
Radim Krčmářbc5e1312016-11-08 20:54:16 +0100731 if (la & (insn_alignment(ctxt, size) - 1))
Avi Kivity1c11b372012-04-09 18:39:59 +0300732 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300733 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300734bad:
735 if (addr.seg == VCPU_SREG_SS)
Paolo Bonzini36061892014-10-27 14:40:49 +0100736 return emulate_ss(ctxt, 0);
Avi Kivity618ff152011-04-03 12:32:09 +0300737 else
Paolo Bonzini36061892014-10-27 14:40:49 +0100738 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300739}
740
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400741static int linearize(struct x86_emulate_ctxt *ctxt,
742 struct segmented_address addr,
743 unsigned size, bool write,
744 ulong *linear)
745{
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100746 unsigned max_size;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200747 return __linearize(ctxt, addr, &max_size, size, write, false,
748 ctxt->mode, linear);
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400749}
750
Nadav Amitd50eaa12014-11-19 17:43:11 +0200751static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
752 enum x86emul_mode mode)
753{
754 ulong linear;
755 int rc;
756 unsigned max_size;
757 struct segmented_address addr = { .seg = VCPU_SREG_CS,
758 .ea = dst };
759
760 if (ctxt->op_bytes != sizeof(unsigned long))
761 addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
762 rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
763 if (rc == X86EMUL_CONTINUE)
764 ctxt->_eip = addr.ea;
765 return rc;
766}
767
768static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
769{
770 return assign_eip(ctxt, dst, ctxt->mode);
771}
772
773static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
774 const struct desc_struct *cs_desc)
775{
776 enum x86emul_mode mode = ctxt->mode;
Nadav Amit82268082015-01-26 09:32:27 +0200777 int rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200778
779#ifdef CONFIG_X86_64
Nadav Amit82268082015-01-26 09:32:27 +0200780 if (ctxt->mode >= X86EMUL_MODE_PROT16) {
781 if (cs_desc->l) {
782 u64 efer = 0;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200783
Nadav Amit82268082015-01-26 09:32:27 +0200784 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
785 if (efer & EFER_LMA)
786 mode = X86EMUL_MODE_PROT64;
787 } else
788 mode = X86EMUL_MODE_PROT32; /* temporary value */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200789 }
790#endif
791 if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
792 mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Nadav Amit82268082015-01-26 09:32:27 +0200793 rc = assign_eip(ctxt, dst, mode);
794 if (rc == X86EMUL_CONTINUE)
795 ctxt->mode = mode;
796 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +0200797}
798
799static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
800{
801 return assign_eip_near(ctxt, ctxt->_eip + rel);
802}
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400803
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200804static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
805 struct segmented_address addr,
806 void *data,
807 unsigned size)
808{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200809 int rc;
810 ulong linear;
811
Avi Kivity83b87952011-04-03 11:31:19 +0300812 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200813 if (rc != X86EMUL_CONTINUE)
814 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300815 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200816}
817
Steve Rutherford736e77c02017-01-11 18:28:29 -0800818static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
819 struct segmented_address addr,
820 void *data,
821 unsigned int size)
822{
823 int rc;
824 ulong linear;
825
826 rc = linearize(ctxt, addr, size, true, &linear);
827 if (rc != X86EMUL_CONTINUE)
828 return rc;
829 return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception);
830}
831
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900832/*
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200833 * Prefetch the remaining bytes of the instruction without crossing page
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900834 * boundary if they are not in fetch_cache yet.
835 */
Paolo Bonzini9506d572014-05-06 13:05:25 +0200836static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
Avi Kivity62266862007-11-20 13:15:52 +0200837{
Avi Kivity62266862007-11-20 13:15:52 +0200838 int rc;
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100839 unsigned size, max_size;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200840 unsigned long linear;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200841 int cur_size = ctxt->fetch.end - ctxt->fetch.data;
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200842 struct segmented_address addr = { .seg = VCPU_SREG_CS,
Paolo Bonzini17052f12014-05-06 16:33:01 +0200843 .ea = ctxt->eip + cur_size };
844
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100845 /*
846 * We do not know exactly how many bytes will be needed, and
847 * __linearize is expensive, so fetch as much as possible. We
848 * just have to avoid going beyond the 15 byte limit, the end
849 * of the segment, or the end of the page.
850 *
851 * __linearize is called with size 0 so that it does not do any
852 * boundary check itself. Instead, we use max_size to check
853 * against op_size.
854 */
Nadav Amitd50eaa12014-11-19 17:43:11 +0200855 rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
856 &linear);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200857 if (unlikely(rc != X86EMUL_CONTINUE))
858 return rc;
859
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100860 size = min_t(unsigned, 15UL ^ cur_size, max_size);
Paolo Bonzini719d5a92014-06-19 11:37:06 +0200861 size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
Paolo Bonzini5cfc7e02014-05-06 13:05:25 +0200862
863 /*
864 * One instruction can only straddle two pages,
865 * and one has been loaded at the beginning of
866 * x86_decode_insn. So, if not enough bytes
867 * still, we must have hit the 15-byte boundary.
868 */
869 if (unlikely(size < op_size))
Paolo Bonzinifd56e152014-10-27 14:40:39 +0100870 return emulate_gp(ctxt, 0);
871
Paolo Bonzini17052f12014-05-06 16:33:01 +0200872 rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
Paolo Bonzini285ca9e2014-05-06 12:24:32 +0200873 size, &ctxt->exception);
874 if (unlikely(rc != X86EMUL_CONTINUE))
875 return rc;
Paolo Bonzini17052f12014-05-06 16:33:01 +0200876 ctxt->fetch.end += size;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900877 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200878}
879
Paolo Bonzini9506d572014-05-06 13:05:25 +0200880static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
881 unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200882{
Nadav Amit08da44a2014-10-03 01:10:04 +0300883 unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
884
885 if (unlikely(done_size < size))
886 return __do_insn_fetch_bytes(ctxt, size - done_size);
Paolo Bonzini9506d572014-05-06 13:05:25 +0200887 else
888 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200889}
890
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900891/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900892#define insn_fetch(_type, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200893({ _type _x; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200894 \
895 rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900896 if (rc != X86EMUL_CONTINUE) \
897 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200898 ctxt->_eip += sizeof(_type); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200899 _x = *(_type __aligned(1) *) ctxt->fetch.ptr; \
900 ctxt->fetch.ptr += sizeof(_type); \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200901 _x; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900902})
903
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900904#define insn_fetch_arr(_arr, _size, _ctxt) \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200905({ \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200906 rc = do_insn_fetch_bytes(_ctxt, _size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900907 if (rc != X86EMUL_CONTINUE) \
908 goto done; \
Paolo Bonzini9506d572014-05-06 13:05:25 +0200909 ctxt->_eip += (_size); \
Paolo Bonzini17052f12014-05-06 16:33:01 +0200910 memcpy(_arr, ctxt->fetch.ptr, _size); \
911 ctxt->fetch.ptr += (_size); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900912})
913
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000914/*
915 * Given the 'reg' portion of a ModRM byte, and a register block, return a
916 * pointer into the block that addresses the relevant register.
917 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
918 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300919static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200920 int byteop)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921{
922 void *p;
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +0200923 int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300926 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
927 else
928 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 return p;
930}
931
932static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200933 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 u16 *size, unsigned long *address, int op_bytes)
935{
936 int rc;
937
938 if (op_bytes == 2)
939 op_bytes = 3;
940 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200941 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900942 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200944 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200945 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946 return rc;
947}
948
Avi Kivity34b77652013-01-19 19:51:56 +0200949FASTOP2(add);
950FASTOP2(or);
951FASTOP2(adc);
952FASTOP2(sbb);
953FASTOP2(and);
954FASTOP2(sub);
955FASTOP2(xor);
956FASTOP2(cmp);
957FASTOP2(test);
958
Avi Kivityb9fa4092013-02-09 11:31:48 +0200959FASTOP1SRC2(mul, mul_ex);
960FASTOP1SRC2(imul, imul_ex);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +0200961FASTOP1SRC2EX(div, div_ex);
962FASTOP1SRC2EX(idiv, idiv_ex);
Avi Kivityb9fa4092013-02-09 11:31:48 +0200963
Avi Kivity34b77652013-01-19 19:51:56 +0200964FASTOP3WCL(shld);
965FASTOP3WCL(shrd);
966
967FASTOP2W(imul);
968
969FASTOP1(not);
970FASTOP1(neg);
971FASTOP1(inc);
972FASTOP1(dec);
973
974FASTOP2CL(rol);
975FASTOP2CL(ror);
976FASTOP2CL(rcl);
977FASTOP2CL(rcr);
978FASTOP2CL(shl);
979FASTOP2CL(shr);
980FASTOP2CL(sar);
981
982FASTOP2W(bsf);
983FASTOP2W(bsr);
984FASTOP2W(bt);
985FASTOP2W(bts);
986FASTOP2W(btr);
987FASTOP2W(btc);
988
Avi Kivitye47a5f52013-02-09 11:31:51 +0200989FASTOP2(xadd);
990
Nadav Amit5aca3722014-11-02 11:54:50 +0200991FASTOP2R(cmp, cmp_r);
992
Nadav Amit900efe22015-03-30 15:39:21 +0300993static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
994{
995 /* If src is zero, do not writeback, but update flags */
996 if (ctxt->src.val == 0)
997 ctxt->dst.type = OP_NONE;
998 return fastop(ctxt, em_bsf);
999}
1000
1001static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
1002{
1003 /* If src is zero, do not writeback, but update flags */
1004 if (ctxt->src.val == 0)
1005 ctxt->dst.type = OP_NONE;
1006 return fastop(ctxt, em_bsr);
1007}
1008
Josh Poimboeufcb7390f2016-01-22 10:16:12 -06001009static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001010{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001011 u8 rc;
1012 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001013
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001014 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +02001015 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001016 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
1017 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001018}
1019
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001020static void fetch_register_operand(struct operand *op)
1021{
1022 switch (op->bytes) {
1023 case 1:
1024 op->val = *(u8 *)op->addr.reg;
1025 break;
1026 case 2:
1027 op->val = *(u16 *)op->addr.reg;
1028 break;
1029 case 4:
1030 op->val = *(u32 *)op->addr.reg;
1031 break;
1032 case 8:
1033 op->val = *(u64 *)op->addr.reg;
1034 break;
1035 }
1036}
1037
Avi Kivity12537912011-03-29 11:41:27 +02001038static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1039{
1040 ctxt->ops->get_fpu(ctxt);
1041 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001042 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1043 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1044 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1045 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1046 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1047 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1048 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1049 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001050#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001051 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1052 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1053 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1054 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1055 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1056 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1057 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1058 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001059#endif
1060 default: BUG();
1061 }
1062 ctxt->ops->put_fpu(ctxt);
1063}
1064
1065static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1066 int reg)
1067{
1068 ctxt->ops->get_fpu(ctxt);
1069 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001070 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1071 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1072 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1073 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1074 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1075 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1076 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1077 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001078#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001079 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1080 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1081 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1082 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1083 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1084 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1085 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1086 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001087#endif
1088 default: BUG();
1089 }
1090 ctxt->ops->put_fpu(ctxt);
1091}
1092
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001093static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1094{
1095 ctxt->ops->get_fpu(ctxt);
1096 switch (reg) {
1097 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1098 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1099 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1100 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1101 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1102 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1103 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1104 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1105 default: BUG();
1106 }
1107 ctxt->ops->put_fpu(ctxt);
1108}
1109
1110static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1111{
1112 ctxt->ops->get_fpu(ctxt);
1113 switch (reg) {
1114 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1115 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1116 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1117 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1118 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1119 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1120 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1121 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1122 default: BUG();
1123 }
1124 ctxt->ops->put_fpu(ctxt);
1125}
1126
Gleb Natapov045a2822012-12-20 16:57:43 +02001127static 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 ctxt->ops->get_fpu(ctxt);
1133 asm volatile("fninit");
1134 ctxt->ops->put_fpu(ctxt);
1135 return X86EMUL_CONTINUE;
1136}
1137
1138static 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 ctxt->ops->get_fpu(ctxt);
1146 asm volatile("fnstcw %0": "+m"(fcw));
1147 ctxt->ops->put_fpu(ctxt);
1148
Gleb Natapov045a2822012-12-20 16:57:43 +02001149 ctxt->dst.val = fcw;
1150
1151 return X86EMUL_CONTINUE;
1152}
1153
1154static 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 ctxt->ops->get_fpu(ctxt);
1162 asm volatile("fnstsw %0": "+m"(fsw));
1163 ctxt->ops->put_fpu(ctxt);
1164
Gleb Natapov045a2822012-12-20 16:57:43 +02001165 ctxt->dst.val = fsw;
1166
1167 return X86EMUL_CONTINUE;
1168}
1169
Avi Kivity12537912011-03-29 11:41:27 +02001170static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001171 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001172{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001173 unsigned reg = ctxt->modrm_reg;
Avi Kivity33615aa2007-10-31 11:15:56 +02001174
Avi Kivity9dac77f2011-06-01 15:34:25 +03001175 if (!(ctxt->d & ModRM))
1176 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001177
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001179 op->type = OP_XMM;
1180 op->bytes = 16;
1181 op->addr.xmm = reg;
1182 read_sse_reg(ctxt, &op->vec_val, reg);
1183 return;
1184 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001185 if (ctxt->d & Mmx) {
1186 reg &= 7;
1187 op->type = OP_MM;
1188 op->bytes = 8;
1189 op->addr.mm = reg;
1190 return;
1191 }
Avi Kivity12537912011-03-29 11:41:27 +02001192
Avi Kivity3c118e22007-10-31 10:27:04 +02001193 op->type = OP_REG;
Gleb Natapov6d4d85e2013-11-04 15:52:42 +02001194 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1195 op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
1196
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001197 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001198 op->orig_val = op->val;
1199}
1200
Avi Kivitya6e34072012-06-10 17:15:39 +03001201static 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
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001207static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001208 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 u8 sib;
Bandan Das02357bd2014-04-16 12:46:11 -04001211 int index_reg, base_reg, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001212 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001213 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001214
Bandan Das02357bd2014-04-16 12:46:11 -04001215 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 */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001218
Bandan Das02357bd2014-04-16 12:46:11 -04001219 ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001220 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
Bandan Das02357bd2014-04-16 12:46:11 -04001221 ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001222 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223
Nadav Amit9b88ae92014-05-25 23:05:21 +03001224 if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001225 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001226 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Paolo Bonzini8acb42072013-05-30 16:35:55 +02001227 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02001228 ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001230 op->type = OP_XMM;
1231 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 op->addr.xmm = ctxt->modrm_rm;
1233 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001234 return rc;
1235 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001236 if (ctxt->d & Mmx) {
1237 op->type = OP_MM;
1238 op->bytes = 8;
Paolo Bonzinibdc90722014-05-06 14:03:29 +02001239 op->addr.mm = ctxt->modrm_rm & 7;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001240 return rc;
1241 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001242 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001243 return rc;
1244 }
1245
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001246 op->type = OP_MEM;
1247
Avi Kivity9dac77f2011-06-01 15:34:25 +03001248 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001249 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);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001253
1254 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001255 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001257 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001258 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 break;
1260 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001261 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001262 break;
1263 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001264 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265 break;
1266 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001267 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001268 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001269 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001270 break;
1271 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001272 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001273 break;
1274 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001275 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276 break;
1277 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001278 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279 break;
1280 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001281 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 break;
1283 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001284 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001285 break;
1286 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001287 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001288 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001289 break;
1290 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001291 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001292 break;
1293 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001294 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;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001297 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001298 } else {
1299 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001300 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001301 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302 index_reg |= (sib >> 3) & 7;
1303 base_reg |= sib & 7;
1304 scale = sib >> 6;
1305
Avi Kivity9dac77f2011-06-01 15:34:25 +03001306 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001307 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001308 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001309 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001310 adjust_modrm_seg(ctxt, base_reg);
Nadav Amitab708092014-12-25 02:52:21 +02001311 /* Increment ESP on POP [ESP] */
1312 if ((ctxt->d & IncSP) &&
1313 base_reg == VCPU_REGS_RSP)
1314 modrm_ea += ctxt->op_bytes;
Avi Kivitya6e34072012-06-10 17:15:39 +03001315 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001316 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001317 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001318 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Nadav Amit5b38ab82014-11-02 11:54:41 +02001319 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity84411d82008-06-15 21:53:26 -07001320 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001321 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001322 } else {
1323 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001324 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001325 adjust_modrm_seg(ctxt, base_reg);
1326 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001327 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001328 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001329 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001330 break;
1331 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001332 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001333 break;
1334 }
1335 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001336 op->addr.mem.ea = modrm_ea;
Bandan Das41061cd2014-04-16 12:46:14 -04001337 if (ctxt->ad_bytes != 8)
1338 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
1339
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001340done:
1341 return rc;
1342}
1343
1344static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001345 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001346{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001347 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001348
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001349 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001350 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001351 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001352 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001353 break;
1354 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001355 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001356 break;
1357 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001358 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001359 break;
1360 }
1361done:
1362 return rc;
1363}
1364
Avi Kivity9dac77f2011-06-01 15:34:25 +03001365static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001366{
Sheng Yang7129eec2010-09-28 16:33:32 +08001367 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001368
Avi Kivity9dac77f2011-06-01 15:34:25 +03001369 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
Nadav Amit7dec5602014-06-15 16:12:57 +03001370 mask = ~((long)ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001371
Avi Kivity9dac77f2011-06-01 15:34:25 +03001372 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;
Nadav Amit7dec5602014-06-15 16:12:57 +03001376 else
1377 sv = (s64)ctxt->src.val & (s64)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001378
Nadav Amit1c1c35a2014-11-19 17:43:09 +02001379 ctxt->dst.addr.mem.ea = address_mask(ctxt,
1380 ctxt->dst.addr.mem.ea + (sv >> 3));
Wei Yongjun35c843c2010-08-09 11:34:56 +08001381 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001382
1383 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001384 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001385}
1386
Gleb Natapov9de41572010-04-28 19:15:22 +03001387static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001388 unsigned long addr, void *dest, unsigned size)
1389{
1390 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001391 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001392
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001393 if (mc->pos < mc->end)
1394 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001395
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001396 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001397
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001398 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
1405read_cached:
1406 memcpy(dest, mc->data + mc->pos, size);
1407 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001408 return X86EMUL_CONTINUE;
1409}
1410
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001411static int segmented_read(struct x86_emulate_ctxt *ctxt,
1412 struct segmented_address addr,
1413 void *data,
1414 unsigned size)
1415{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001416 int rc;
1417 ulong linear;
1418
Avi Kivity83b87952011-04-03 11:31:19 +03001419 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001420 if (rc != X86EMUL_CONTINUE)
1421 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001422 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001423}
1424
1425static int segmented_write(struct x86_emulate_ctxt *ctxt,
1426 struct segmented_address addr,
1427 const void *data,
1428 unsigned size)
1429{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001430 int rc;
1431 ulong linear;
1432
Avi Kivity83b87952011-04-03 11:31:19 +03001433 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001434 if (rc != X86EMUL_CONTINUE)
1435 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001436 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1437 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001438}
1439
1440static 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{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001445 int rc;
1446 ulong linear;
1447
Avi Kivity83b87952011-04-03 11:31:19 +03001448 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001449 if (rc != X86EMUL_CONTINUE)
1450 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001451 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1452 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001453}
1454
Gleb Natapov7b262e92010-03-18 15:20:27 +02001455static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001456 unsigned int size, unsigned short port,
1457 void *dest)
1458{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001459 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001460
1461 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001462 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001463 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001464 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Nadav Amit0efb0442015-03-29 16:33:03 +03001465 in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001466 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1467 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Mark Rustadb55a8142014-07-25 06:27:05 -07001468 n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001469 if (n == 0)
1470 n = 1;
1471 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001472 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001473 return 0;
1474 rc->end = n * size;
1475 }
1476
Nadav Amite6e39f02014-04-18 03:35:10 +03001477 if (ctxt->rep_prefix && (ctxt->d & String) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03001478 !(ctxt->eflags & X86_EFLAGS_DF)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001479 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 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001487 return 1;
1488}
1489
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001490static 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 ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1503 &ctxt->exception);
1504}
1505
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001506static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001507 u16 selector, struct desc_ptr *dt)
1508{
Mathias Krause0225fb52012-08-30 01:30:16 +02001509 const struct x86_emulate_ops *ops = ctxt->ops;
Nadav Amit2eedcac2014-06-02 18:34:05 +03001510 u32 base3 = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001511
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001512 if (selector & 1 << 2) {
1513 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001514 u16 sel;
1515
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001516 memset (dt, 0, sizeof *dt);
Nadav Amit2eedcac2014-06-02 18:34:05 +03001517 if (!ops->get_segment(ctxt, &sel, &desc, &base3,
1518 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 return;
1520
1521 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
Nadav Amit2eedcac2014-06-02 18:34:05 +03001522 dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001524 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001525}
1526
Nadav Amitedccda72014-12-25 02:52:23 +02001527static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
1528 u16 selector, ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001529{
1530 struct desc_ptr dt;
1531 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001532 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001533
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001534 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001535
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001536 if (dt.size < index * 8 + 7)
1537 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001538
1539 addr = dt.address + index * 8;
Nadav Amitedccda72014-12-25 02:52:23 +02001540
1541#ifdef CONFIG_X86_64
1542 if (addr >> 32 != 0) {
1543 u64 efer = 0;
1544
1545 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1546 if (!(efer & EFER_LMA))
1547 addr &= (u32)-1;
1548 }
1549#endif
1550
1551 *desc_addr_p = addr;
1552 return X86EMUL_CONTINUE;
1553}
1554
1555/* allowed just for 8 bytes segments */
1556static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1557 u16 selector, struct desc_struct *desc,
1558 ulong *desc_addr_p)
1559{
1560 int rc;
1561
1562 rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
1563 if (rc != X86EMUL_CONTINUE)
1564 return rc;
1565
1566 return ctxt->ops->read_std(ctxt, *desc_addr_p, desc, sizeof(*desc),
1567 &ctxt->exception);
1568}
1569
1570/* allowed just for 8 bytes segments */
1571static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1572 u16 selector, struct desc_struct *desc)
1573{
1574 int rc;
1575 ulong addr;
1576
1577 rc = get_descriptor_ptr(ctxt, selector, &addr);
1578 if (rc != X86EMUL_CONTINUE)
1579 return rc;
1580
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001581 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1582 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001583}
1584
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001585static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Nadav Amitd1442d82014-09-18 22:39:39 +03001586 u16 selector, int seg, u8 cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001587 enum x86_transfer_type transfer,
Nadav Amitd1442d82014-09-18 22:39:39 +03001588 struct desc_struct *desc)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001589{
Avi Kivity869be992012-06-13 16:30:53 +03001590 struct desc_struct seg_desc, old_desc;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001591 u8 dpl, rpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001592 unsigned err_vec = GP_VECTOR;
1593 u32 err_code = 0;
1594 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001595 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001596 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001597 u16 dummy;
Nadav Amite37a75a2014-06-02 18:34:04 +03001598 u32 base3 = 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001599
1600 memset(&seg_desc, 0, sizeof seg_desc);
1601
Kevin Wolff8da94e2013-04-11 14:06:03 +02001602 if (ctxt->mode == X86EMUL_MODE_REAL) {
1603 /* set real mode segment descriptor (keep limit etc. for
1604 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001605 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001606 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001607 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001608 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1609 /* VM86 needs a clean new segment descriptor */
1610 set_desc_base(&seg_desc, selector << 4);
1611 set_desc_limit(&seg_desc, 0xffff);
1612 seg_desc.type = 3;
1613 seg_desc.p = 1;
1614 seg_desc.s = 1;
1615 seg_desc.dpl = 3;
1616 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001617 }
1618
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001619 rpl = selector & 3;
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001620
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001621 /* TR should be in GDT only */
1622 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1623 goto exception;
1624
Paolo Bonzini7718ffc2017-01-12 15:02:32 +01001625 /* NULL selector is not valid for TR, CS and (except for long mode) SS */
1626 if (null_selector) {
1627 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
1628 goto exception;
1629
1630 if (seg == VCPU_SREG_SS) {
1631 if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
1632 goto exception;
1633
1634 /*
1635 * ctxt->ops->set_segment expects the CPL to be in
1636 * SS.DPL, so fake an expand-up 32-bit data segment.
1637 */
1638 seg_desc.type = 3;
1639 seg_desc.p = 1;
1640 seg_desc.s = 1;
1641 seg_desc.dpl = cpl;
1642 seg_desc.d = 1;
1643 seg_desc.g = 1;
1644 }
1645
1646 /* Skip all following checks */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001647 goto load;
Paolo Bonzini7718ffc2017-01-12 15:02:32 +01001648 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001649
Avi Kivitye9194642012-06-13 16:29:39 +03001650 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001651 if (ret != X86EMUL_CONTINUE)
1652 return ret;
1653
1654 err_code = selector & 0xfffc;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001655 err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
1656 GP_VECTOR;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001657
Guo Chaofc058682012-06-28 15:19:51 +08001658 /* can't load system descriptor into segment selector */
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001659 if (seg <= VCPU_SREG_GS && !seg_desc.s) {
1660 if (transfer == X86_TRANSFER_CALL_JMP)
1661 return X86EMUL_UNHANDLEABLE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001662 goto exception;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001663 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001664
1665 if (!seg_desc.p) {
1666 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1667 goto exception;
1668 }
1669
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001670 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001671
1672 switch (seg) {
1673 case VCPU_SREG_SS:
1674 /*
1675 * segment is not a writable data segment or segment
1676 * selector's RPL != CPL or segment selector's RPL != CPL
1677 */
1678 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1679 goto exception;
1680 break;
1681 case VCPU_SREG_CS:
1682 if (!(seg_desc.type & 8))
1683 goto exception;
1684
1685 if (seg_desc.type & 4) {
1686 /* conforming */
1687 if (dpl > cpl)
1688 goto exception;
1689 } else {
1690 /* nonconforming */
1691 if (rpl > cpl || dpl != cpl)
1692 goto exception;
1693 }
Nadav Amit040c8dc2014-09-18 22:39:43 +03001694 /* in long-mode d/b must be clear if l is set */
1695 if (seg_desc.d && seg_desc.l) {
1696 u64 efer = 0;
1697
1698 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
1699 if (efer & EFER_LMA)
1700 goto exception;
1701 }
1702
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001703 /* CS(RPL) <- CPL */
1704 selector = (selector & 0xfffc) | cpl;
1705 break;
1706 case VCPU_SREG_TR:
1707 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1708 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001709 old_desc = seg_desc;
1710 seg_desc.type |= 2; /* busy */
1711 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1712 sizeof(seg_desc), &ctxt->exception);
1713 if (ret != X86EMUL_CONTINUE)
1714 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001715 break;
1716 case VCPU_SREG_LDTR:
1717 if (seg_desc.s || seg_desc.type != 2)
1718 goto exception;
1719 break;
1720 default: /* DS, ES, FS, or GS */
1721 /*
1722 * segment is not a data or readable code segment or
1723 * ((segment is a data or nonconforming code segment)
1724 * and (both RPL and CPL > DPL))
1725 */
1726 if ((seg_desc.type & 0xa) == 0x8 ||
1727 (((seg_desc.type & 0xc) != 0xc) &&
1728 (rpl > dpl && cpl > dpl)))
1729 goto exception;
1730 break;
1731 }
1732
1733 if (seg_desc.s) {
1734 /* mark segment as accessed */
Nadav Amite2cefa72014-12-25 02:52:22 +02001735 if (!(seg_desc.type & 1)) {
1736 seg_desc.type |= 1;
1737 ret = write_segment_descriptor(ctxt, selector,
1738 &seg_desc);
1739 if (ret != X86EMUL_CONTINUE)
1740 return ret;
1741 }
Nadav Amite37a75a2014-06-02 18:34:04 +03001742 } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
1743 ret = ctxt->ops->read_std(ctxt, desc_addr+8, &base3,
1744 sizeof(base3), &ctxt->exception);
1745 if (ret != X86EMUL_CONTINUE)
1746 return ret;
Nadav Amit9a9abf62014-11-02 11:54:56 +02001747 if (is_noncanonical_address(get_desc_base(&seg_desc) |
1748 ((u64)base3 << 32)))
1749 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001750 }
1751load:
Nadav Amite37a75a2014-06-02 18:34:04 +03001752 ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
Nadav Amitd1442d82014-09-18 22:39:39 +03001753 if (desc)
1754 *desc = seg_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001755 return X86EMUL_CONTINUE;
1756exception:
Paolo Bonzini592f0852014-08-20 10:05:08 +02001757 return emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001758}
1759
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001760static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1761 u16 selector, int seg)
1762{
1763 u8 cpl = ctxt->ops->cpl(ctxt);
Paolo Bonzini7718ffc2017-01-12 15:02:32 +01001764
1765 /*
1766 * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
1767 * they can load it at CPL<3 (Intel's manual says only LSS can,
1768 * but it's wrong).
1769 *
1770 * However, the Intel manual says that putting IST=1/DPL=3 in
1771 * an interrupt gate will result in SS=3 (the AMD manual instead
1772 * says it doesn't), so allow SS=3 in __load_segment_descriptor
1773 * and only forbid it here.
1774 */
1775 if (seg == VCPU_SREG_SS && selector == 3 &&
1776 ctxt->mode == X86EMUL_MODE_PROT64)
1777 return emulate_exception(ctxt, GP_VECTOR, 0, true);
1778
Nadav Amit3dc4bc42014-12-25 02:52:19 +02001779 return __load_segment_descriptor(ctxt, selector, seg, cpl,
1780 X86_TRANSFER_NONE, NULL);
Paolo Bonzini2356aae2014-05-15 17:56:57 +02001781}
1782
Wei Yongjun31be40b2010-08-17 09:17:30 +08001783static void write_register_operand(struct operand *op)
1784{
Nadav Amit6fd8e122015-03-30 15:39:20 +03001785 return assign_register(op->addr.reg, op->val, op->bytes);
Wei Yongjun31be40b2010-08-17 09:17:30 +08001786}
1787
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001788static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001789{
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001790 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001791 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001792 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001793 break;
1794 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001795 if (ctxt->lock_prefix)
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001796 return segmented_cmpxchg(ctxt,
1797 op->addr.mem,
1798 &op->orig_val,
1799 &op->val,
1800 op->bytes);
1801 else
1802 return segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001803 op->addr.mem,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001804 &op->val,
1805 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001806 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001807 case OP_MEM_STR:
Paolo Bonzinif5f87df2014-04-01 13:23:24 +02001808 return segmented_write(ctxt,
1809 op->addr.mem,
1810 op->data,
1811 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001812 break;
Avi Kivity12537912011-03-29 11:41:27 +02001813 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001814 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001815 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001816 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001817 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001818 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001819 case OP_NONE:
1820 /* no writeback */
1821 break;
1822 default:
1823 break;
1824 }
1825 return X86EMUL_CONTINUE;
1826}
1827
Avi Kivity51ddff52012-06-12 20:19:40 +03001828static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001829{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001830 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001831
Avi Kivity5ad105e2012-08-19 14:34:31 +03001832 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001833 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001834 addr.seg = VCPU_SREG_SS;
1835
Avi Kivity51ddff52012-06-12 20:19:40 +03001836 return segmented_write(ctxt, addr, data, bytes);
1837}
1838
1839static int em_push(struct x86_emulate_ctxt *ctxt)
1840{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001841 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001842 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001843 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001844}
1845
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001846static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001847 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001848{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001849 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001850 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001851
Avi Kivitydd856ef2012-08-27 23:46:17 +03001852 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001853 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001854 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001855 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001856 return rc;
1857
Avi Kivity5ad105e2012-08-19 14:34:31 +03001858 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001859 return rc;
1860}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001861
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001862static int em_pop(struct x86_emulate_ctxt *ctxt)
1863{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001864 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001865}
1866
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001867static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001868 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001869{
1870 int rc;
1871 unsigned long val, change_mask;
Nadav Amit0efb0442015-03-29 16:33:03 +03001872 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001873 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001874
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001875 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001876 if (rc != X86EMUL_CONTINUE)
1877 return rc;
1878
Nadav Amit0efb0442015-03-29 16:33:03 +03001879 change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
1880 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
1881 X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
1882 X86_EFLAGS_AC | X86_EFLAGS_ID;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001883
1884 switch(ctxt->mode) {
1885 case X86EMUL_MODE_PROT64:
1886 case X86EMUL_MODE_PROT32:
1887 case X86EMUL_MODE_PROT16:
1888 if (cpl == 0)
Nadav Amit0efb0442015-03-29 16:33:03 +03001889 change_mask |= X86_EFLAGS_IOPL;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001890 if (cpl <= iopl)
Nadav Amit0efb0442015-03-29 16:33:03 +03001891 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001892 break;
1893 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001894 if (iopl < 3)
1895 return emulate_gp(ctxt, 0);
Nadav Amit0efb0442015-03-29 16:33:03 +03001896 change_mask |= X86_EFLAGS_IF;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001897 break;
1898 default: /* real mode */
Nadav Amit0efb0442015-03-29 16:33:03 +03001899 change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001900 break;
1901 }
1902
1903 *(unsigned long *)dest =
1904 (ctxt->eflags & ~change_mask) | (val & change_mask);
1905
1906 return rc;
1907}
1908
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001909static int em_popf(struct x86_emulate_ctxt *ctxt)
1910{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001911 ctxt->dst.type = OP_REG;
1912 ctxt->dst.addr.reg = &ctxt->eflags;
1913 ctxt->dst.bytes = ctxt->op_bytes;
1914 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001915}
1916
Avi Kivity612e89f2012-06-12 20:03:23 +03001917static int em_enter(struct x86_emulate_ctxt *ctxt)
1918{
1919 int rc;
1920 unsigned frame_size = ctxt->src.val;
1921 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001922 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001923
1924 if (nesting_level)
1925 return X86EMUL_UNHANDLEABLE;
1926
Avi Kivitydd856ef2012-08-27 23:46:17 +03001927 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1928 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001929 if (rc != X86EMUL_CONTINUE)
1930 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001931 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001932 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001933 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1934 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001935 stack_mask(ctxt));
1936 return X86EMUL_CONTINUE;
1937}
1938
Avi Kivityf47cfa32012-06-07 17:49:24 +03001939static int em_leave(struct x86_emulate_ctxt *ctxt)
1940{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001941 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001942 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001943 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001944}
1945
Avi Kivity1cd196e2011-09-13 10:45:51 +03001946static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001947{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001948 int seg = ctxt->src2.val;
1949
Avi Kivity9dac77f2011-06-01 15:34:25 +03001950 ctxt->src.val = get_segment_selector(ctxt, seg);
Nadav Amit0fcc2072014-11-02 11:54:51 +02001951 if (ctxt->op_bytes == 4) {
1952 rsp_increment(ctxt, -2);
1953 ctxt->op_bytes = 2;
1954 }
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001955
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001956 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001957}
1958
Avi Kivity1cd196e2011-09-13 10:45:51 +03001959static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001960{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001961 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001962 unsigned long selector;
1963 int rc;
1964
Nadav Amit3313bc42014-12-25 02:52:17 +02001965 rc = emulate_pop(ctxt, &selector, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001966 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001967 return rc;
1968
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001969 if (ctxt->modrm_reg == VCPU_SREG_SS)
1970 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Nadav Amit3313bc42014-12-25 02:52:17 +02001971 if (ctxt->op_bytes > 2)
1972 rsp_increment(ctxt, ctxt->op_bytes - 2);
Paolo Bonzinia5457e72014-06-05 17:29:34 +02001973
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001974 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001975 return rc;
1976}
1977
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001978static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001979{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001980 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001981 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001982 int reg = VCPU_REGS_RAX;
1983
1984 while (reg <= VCPU_REGS_RDI) {
1985 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001986 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001987
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001988 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001989 if (rc != X86EMUL_CONTINUE)
1990 return rc;
1991
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001992 ++reg;
1993 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001994
Wei Yongjunc37eda12010-06-15 09:03:33 +08001995 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001996}
1997
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001998static int em_pushf(struct x86_emulate_ctxt *ctxt)
1999{
Nadav Amit0efb0442015-03-29 16:33:03 +03002000 ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09002001 return em_push(ctxt);
2002}
2003
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09002004static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002005{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002006 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002007 int reg = VCPU_REGS_RDI;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002008 u32 val;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002009
2010 while (reg >= VCPU_REGS_RAX) {
2011 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03002012 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002013 --reg;
2014 }
2015
Nadav Amit6fd8e122015-03-30 15:39:20 +03002016 rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002017 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002018 break;
Nadav Amit6fd8e122015-03-30 15:39:20 +03002019 assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002020 --reg;
2021 }
2022 return rc;
2023}
2024
Avi Kivitydd856ef2012-08-27 23:46:17 +03002025static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002026{
Mathias Krause0225fb52012-08-30 01:30:16 +02002027 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002028 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002029 struct desc_ptr dt;
2030 gva_t cs_addr;
2031 gva_t eip_addr;
2032 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002033
2034 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002035 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002036 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002037 if (rc != X86EMUL_CONTINUE)
2038 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002039
Nadav Amit0efb0442015-03-29 16:33:03 +03002040 ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002041
Avi Kivity9dac77f2011-06-01 15:34:25 +03002042 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002043 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002044 if (rc != X86EMUL_CONTINUE)
2045 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002046
Avi Kivity9dac77f2011-06-01 15:34:25 +03002047 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002048 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03002049 if (rc != X86EMUL_CONTINUE)
2050 return rc;
2051
Avi Kivity4bff1e862011-04-20 13:37:53 +03002052 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002053
2054 eip_addr = dt.address + (irq << 2);
2055 cs_addr = dt.address + (irq << 2) + 2;
2056
Avi Kivity0f65dd72011-04-20 13:37:53 +03002057 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002058 if (rc != X86EMUL_CONTINUE)
2059 return rc;
2060
Avi Kivity0f65dd72011-04-20 13:37:53 +03002061 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002062 if (rc != X86EMUL_CONTINUE)
2063 return rc;
2064
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002065 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002066 if (rc != X86EMUL_CONTINUE)
2067 return rc;
2068
Avi Kivity9dac77f2011-06-01 15:34:25 +03002069 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002070
2071 return rc;
2072}
2073
Avi Kivitydd856ef2012-08-27 23:46:17 +03002074int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2075{
2076 int rc;
2077
2078 invalidate_registers(ctxt);
2079 rc = __emulate_int_real(ctxt, irq);
2080 if (rc == X86EMUL_CONTINUE)
2081 writeback_registers(ctxt);
2082 return rc;
2083}
2084
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002085static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002086{
2087 switch(ctxt->mode) {
2088 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002089 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002090 case X86EMUL_MODE_VM86:
2091 case X86EMUL_MODE_PROT16:
2092 case X86EMUL_MODE_PROT32:
2093 case X86EMUL_MODE_PROT64:
2094 default:
2095 /* Protected mode interrupts unimplemented yet */
2096 return X86EMUL_UNHANDLEABLE;
2097 }
2098}
2099
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002100static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002101{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002102 int rc = X86EMUL_CONTINUE;
2103 unsigned long temp_eip = 0;
2104 unsigned long temp_eflags = 0;
2105 unsigned long cs = 0;
Nadav Amit0efb0442015-03-29 16:33:03 +03002106 unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
2107 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
2108 X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
2109 X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
2110 X86_EFLAGS_AC | X86_EFLAGS_ID |
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002111 X86_EFLAGS_FIXED;
Nadav Amit0efb0442015-03-29 16:33:03 +03002112 unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
2113 X86_EFLAGS_VIP;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002114
2115 /* TODO: Add stack limit check */
2116
Avi Kivity9dac77f2011-06-01 15:34:25 +03002117 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002118
2119 if (rc != X86EMUL_CONTINUE)
2120 return rc;
2121
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002122 if (temp_eip & ~0xffff)
2123 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002124
Avi Kivity9dac77f2011-06-01 15:34:25 +03002125 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002126
2127 if (rc != X86EMUL_CONTINUE)
2128 return rc;
2129
Avi Kivity9dac77f2011-06-01 15:34:25 +03002130 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002131
2132 if (rc != X86EMUL_CONTINUE)
2133 return rc;
2134
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002135 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002136
2137 if (rc != X86EMUL_CONTINUE)
2138 return rc;
2139
Avi Kivity9dac77f2011-06-01 15:34:25 +03002140 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002141
Avi Kivity9dac77f2011-06-01 15:34:25 +03002142 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002143 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002144 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002145 ctxt->eflags &= ~0xffff;
2146 ctxt->eflags |= temp_eflags;
2147 }
2148
2149 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002150 ctxt->eflags |= X86_EFLAGS_FIXED;
Nadav Amit801806d2015-01-26 09:32:23 +02002151 ctxt->ops->set_nmi_mask(ctxt, false);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002152
2153 return rc;
2154}
2155
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002156static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002157{
2158 switch(ctxt->mode) {
2159 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002160 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002161 case X86EMUL_MODE_VM86:
2162 case X86EMUL_MODE_PROT16:
2163 case X86EMUL_MODE_PROT32:
2164 case X86EMUL_MODE_PROT64:
2165 default:
2166 /* iret from protected mode unimplemented yet */
2167 return X86EMUL_UNHANDLEABLE;
2168 }
2169}
2170
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002171static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2172{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002173 int rc;
Radim Krčmář2117d532016-11-23 21:15:00 +01002174 unsigned short sel;
2175 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002176 u8 cpl = ctxt->ops->cpl(ctxt);
2177
Avi Kivity9dac77f2011-06-01 15:34:25 +03002178 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002179
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002180 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
2181 X86_TRANSFER_CALL_JMP,
Nadav Amitd1442d82014-09-18 22:39:39 +03002182 &new_desc);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002183 if (rc != X86EMUL_CONTINUE)
2184 return rc;
2185
Nadav Amitd50eaa12014-11-19 17:43:11 +02002186 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002187 /* Error handling is not implemented. */
2188 if (rc != X86EMUL_CONTINUE)
2189 return X86EMUL_UNHANDLEABLE;
2190
Nadav Amitd1442d82014-09-18 22:39:39 +03002191 return rc;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002192}
2193
Nadav Amitf7784042014-09-18 22:39:41 +03002194static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002195{
Nadav Amitf7784042014-09-18 22:39:41 +03002196 return assign_eip_near(ctxt, ctxt->src.val);
2197}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002198
Nadav Amitf7784042014-09-18 22:39:41 +03002199static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
2200{
2201 int rc;
2202 long int old_eip;
2203
2204 old_eip = ctxt->_eip;
2205 rc = assign_eip_near(ctxt, ctxt->src.val);
2206 if (rc != X86EMUL_CONTINUE)
2207 return rc;
2208 ctxt->src.val = old_eip;
2209 rc = em_push(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002210 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002211}
2212
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002213static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002214{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002215 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002216
Nadav Amitaaa05f22014-06-02 18:34:10 +03002217 if (ctxt->dst.bytes == 16)
2218 return X86EMUL_UNHANDLEABLE;
2219
Avi Kivitydd856ef2012-08-27 23:46:17 +03002220 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2221 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2222 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2223 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Nadav Amit0efb0442015-03-29 16:33:03 +03002224 ctxt->eflags &= ~X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002225 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002226 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2227 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002228
Nadav Amit0efb0442015-03-29 16:33:03 +03002229 ctxt->eflags |= X86_EFLAGS_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002230 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002231 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002232}
2233
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002234static int em_ret(struct x86_emulate_ctxt *ctxt)
2235{
Nadav Amit234f3ce2014-09-18 22:39:38 +03002236 int rc;
2237 unsigned long eip;
2238
2239 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
2240 if (rc != X86EMUL_CONTINUE)
2241 return rc;
2242
2243 return assign_eip_near(ctxt, eip);
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002244}
2245
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002246static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002247{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002248 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002249 unsigned long eip, cs;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002250 int cpl = ctxt->ops->cpl(ctxt);
Radim Krčmář2117d532016-11-23 21:15:00 +01002251 struct desc_struct new_desc;
Nadav Amitd1442d82014-09-18 22:39:39 +03002252
2253 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002254 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002255 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002256 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002257 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002258 return rc;
Nadav Amit9e8919a2014-06-15 16:12:59 +03002259 /* Outer-privilege level return is not implemented */
2260 if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
2261 return X86EMUL_UNHANDLEABLE;
Nadav Amit3dc4bc42014-12-25 02:52:19 +02002262 rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
2263 X86_TRANSFER_RET,
Nadav Amitd1442d82014-09-18 22:39:39 +03002264 &new_desc);
2265 if (rc != X86EMUL_CONTINUE)
2266 return rc;
Nadav Amitd50eaa12014-11-19 17:43:11 +02002267 rc = assign_eip_far(ctxt, eip, &new_desc);
Radim Krčmář2117d532016-11-23 21:15:00 +01002268 /* Error handling is not implemented. */
2269 if (rc != X86EMUL_CONTINUE)
2270 return X86EMUL_UNHANDLEABLE;
2271
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002272 return rc;
2273}
2274
Bruce Rogers32611072013-09-09 09:40:20 -06002275static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
2276{
2277 int rc;
2278
2279 rc = em_ret_far(ctxt);
2280 if (rc != X86EMUL_CONTINUE)
2281 return rc;
2282 rsp_increment(ctxt, ctxt->src.val);
2283 return X86EMUL_CONTINUE;
2284}
2285
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002286static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2287{
2288 /* Save real source value, then compare EAX against destination. */
Nadav Amit37c564f2014-06-02 18:34:07 +03002289 ctxt->dst.orig_val = ctxt->dst.val;
2290 ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002291 ctxt->src.orig_val = ctxt->src.val;
Nadav Amit37c564f2014-06-02 18:34:07 +03002292 ctxt->src.val = ctxt->dst.orig_val;
Avi Kivity158de572013-01-19 19:51:57 +02002293 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002294
Nadav Amit0efb0442015-03-29 16:33:03 +03002295 if (ctxt->eflags & X86_EFLAGS_ZF) {
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002296 /* Success: write back to memory; no update of EAX */
2297 ctxt->src.type = OP_NONE;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002298 ctxt->dst.val = ctxt->src.orig_val;
2299 } else {
2300 /* Failure: write the value we saw to EAX. */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02002301 ctxt->src.type = OP_REG;
2302 ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
2303 ctxt->src.val = ctxt->dst.orig_val;
2304 /* Create write-cycle to dest by writing the same value */
Nadav Amit37c564f2014-06-02 18:34:07 +03002305 ctxt->dst.val = ctxt->dst.orig_val;
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002306 }
2307 return X86EMUL_CONTINUE;
2308}
2309
Avi Kivityd4b43252011-09-13 10:45:50 +03002310static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002311{
Avi Kivityd4b43252011-09-13 10:45:50 +03002312 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002313 unsigned short sel;
2314 int rc;
2315
Avi Kivity9dac77f2011-06-01 15:34:25 +03002316 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002317
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002318 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002319 if (rc != X86EMUL_CONTINUE)
2320 return rc;
2321
Avi Kivity9dac77f2011-06-01 15:34:25 +03002322 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002323 return rc;
2324}
2325
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002326static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
2327{
2328 u32 eax, ebx, ecx, edx;
2329
2330 eax = 0x80000001;
2331 ecx = 0;
2332 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2333 return edx & bit(X86_FEATURE_LM);
2334}
2335
2336#define GET_SMSTATE(type, smbase, offset) \
2337 ({ \
2338 type __val; \
Radim Krčmářf40606b2015-10-30 16:36:25 +01002339 int r = ctxt->ops->read_phys(ctxt, smbase + offset, &__val, \
2340 sizeof(__val)); \
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002341 if (r != X86EMUL_CONTINUE) \
2342 return X86EMUL_UNHANDLEABLE; \
2343 __val; \
2344 })
2345
2346static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
2347{
2348 desc->g = (flags >> 23) & 1;
2349 desc->d = (flags >> 22) & 1;
2350 desc->l = (flags >> 21) & 1;
2351 desc->avl = (flags >> 20) & 1;
2352 desc->p = (flags >> 15) & 1;
2353 desc->dpl = (flags >> 13) & 3;
2354 desc->s = (flags >> 12) & 1;
2355 desc->type = (flags >> 8) & 15;
2356}
2357
2358static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
2359{
2360 struct desc_struct desc;
2361 int offset;
2362 u16 selector;
2363
2364 selector = GET_SMSTATE(u32, smbase, 0x7fa8 + n * 4);
2365
2366 if (n < 3)
2367 offset = 0x7f84 + n * 12;
2368 else
2369 offset = 0x7f2c + (n - 3) * 12;
2370
2371 set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
2372 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
2373 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, offset));
2374 ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
2375 return X86EMUL_CONTINUE;
2376}
2377
2378static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
2379{
2380 struct desc_struct desc;
2381 int offset;
2382 u16 selector;
2383 u32 base3;
2384
2385 offset = 0x7e00 + n * 16;
2386
2387 selector = GET_SMSTATE(u16, smbase, offset);
2388 rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smbase, offset + 2) << 8);
2389 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
2390 set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
2391 base3 = GET_SMSTATE(u32, smbase, offset + 12);
2392
2393 ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
2394 return X86EMUL_CONTINUE;
2395}
2396
2397static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
Paolo Bonzini18276e92017-12-21 00:49:14 +01002398 u64 cr0, u64 cr3, u64 cr4)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002399{
2400 int bad;
Paolo Bonzini18276e92017-12-21 00:49:14 +01002401 u64 pcid;
2402
2403 /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */
2404 pcid = 0;
2405 if (cr4 & X86_CR4_PCIDE) {
2406 pcid = cr3 & 0xfff;
2407 cr3 &= ~0xfff;
2408 }
2409
2410 bad = ctxt->ops->set_cr(ctxt, 3, cr3);
2411 if (bad)
2412 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002413
2414 /*
2415 * First enable PAE, long mode needs it before CR0.PG = 1 is set.
2416 * Then enable protected mode. However, PCID cannot be enabled
2417 * if EFER.LMA=0, so set it separately.
2418 */
2419 bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2420 if (bad)
2421 return X86EMUL_UNHANDLEABLE;
2422
2423 bad = ctxt->ops->set_cr(ctxt, 0, cr0);
2424 if (bad)
2425 return X86EMUL_UNHANDLEABLE;
2426
2427 if (cr4 & X86_CR4_PCIDE) {
2428 bad = ctxt->ops->set_cr(ctxt, 4, cr4);
2429 if (bad)
2430 return X86EMUL_UNHANDLEABLE;
Paolo Bonzini18276e92017-12-21 00:49:14 +01002431 if (pcid) {
2432 bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
2433 if (bad)
2434 return X86EMUL_UNHANDLEABLE;
2435 }
2436
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002437 }
2438
2439 return X86EMUL_CONTINUE;
2440}
2441
2442static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt, u64 smbase)
2443{
2444 struct desc_struct desc;
2445 struct desc_ptr dt;
2446 u16 selector;
Paolo Bonzini18276e92017-12-21 00:49:14 +01002447 u32 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002448 int i;
2449
2450 cr0 = GET_SMSTATE(u32, smbase, 0x7ffc);
Paolo Bonzini18276e92017-12-21 00:49:14 +01002451 cr3 = GET_SMSTATE(u32, smbase, 0x7ff8);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002452 ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7ff4) | X86_EFLAGS_FIXED;
2453 ctxt->_eip = GET_SMSTATE(u32, smbase, 0x7ff0);
2454
2455 for (i = 0; i < 8; i++)
2456 *reg_write(ctxt, i) = GET_SMSTATE(u32, smbase, 0x7fd0 + i * 4);
2457
2458 val = GET_SMSTATE(u32, smbase, 0x7fcc);
2459 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
2460 val = GET_SMSTATE(u32, smbase, 0x7fc8);
2461 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2462
2463 selector = GET_SMSTATE(u32, smbase, 0x7fc4);
2464 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f64));
2465 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f60));
2466 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f5c));
2467 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
2468
2469 selector = GET_SMSTATE(u32, smbase, 0x7fc0);
2470 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f80));
2471 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f7c));
2472 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f78));
2473 ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
2474
2475 dt.address = GET_SMSTATE(u32, smbase, 0x7f74);
2476 dt.size = GET_SMSTATE(u32, smbase, 0x7f70);
2477 ctxt->ops->set_gdt(ctxt, &dt);
2478
2479 dt.address = GET_SMSTATE(u32, smbase, 0x7f58);
2480 dt.size = GET_SMSTATE(u32, smbase, 0x7f54);
2481 ctxt->ops->set_idt(ctxt, &dt);
2482
2483 for (i = 0; i < 6; i++) {
2484 int r = rsm_load_seg_32(ctxt, smbase, i);
2485 if (r != X86EMUL_CONTINUE)
2486 return r;
2487 }
2488
2489 cr4 = GET_SMSTATE(u32, smbase, 0x7f14);
2490
2491 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7ef8));
2492
Paolo Bonzini18276e92017-12-21 00:49:14 +01002493 return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002494}
2495
2496static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, u64 smbase)
2497{
2498 struct desc_struct desc;
2499 struct desc_ptr dt;
Paolo Bonzini18276e92017-12-21 00:49:14 +01002500 u64 val, cr0, cr3, cr4;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002501 u32 base3;
2502 u16 selector;
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002503 int i, r;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002504
2505 for (i = 0; i < 16; i++)
2506 *reg_write(ctxt, i) = GET_SMSTATE(u64, smbase, 0x7ff8 - i * 8);
2507
2508 ctxt->_eip = GET_SMSTATE(u64, smbase, 0x7f78);
2509 ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7f70) | X86_EFLAGS_FIXED;
2510
2511 val = GET_SMSTATE(u32, smbase, 0x7f68);
2512 ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
2513 val = GET_SMSTATE(u32, smbase, 0x7f60);
2514 ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
2515
2516 cr0 = GET_SMSTATE(u64, smbase, 0x7f58);
Paolo Bonzini18276e92017-12-21 00:49:14 +01002517 cr3 = GET_SMSTATE(u64, smbase, 0x7f50);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002518 cr4 = GET_SMSTATE(u64, smbase, 0x7f48);
2519 ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7f00));
2520 val = GET_SMSTATE(u64, smbase, 0x7ed0);
2521 ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
2522
2523 selector = GET_SMSTATE(u32, smbase, 0x7e90);
2524 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e92) << 8);
2525 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e94));
2526 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e98));
2527 base3 = GET_SMSTATE(u32, smbase, 0x7e9c);
2528 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
2529
2530 dt.size = GET_SMSTATE(u32, smbase, 0x7e84);
2531 dt.address = GET_SMSTATE(u64, smbase, 0x7e88);
2532 ctxt->ops->set_idt(ctxt, &dt);
2533
2534 selector = GET_SMSTATE(u32, smbase, 0x7e70);
2535 rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e72) << 8);
2536 set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e74));
2537 set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e78));
2538 base3 = GET_SMSTATE(u32, smbase, 0x7e7c);
2539 ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
2540
2541 dt.size = GET_SMSTATE(u32, smbase, 0x7e64);
2542 dt.address = GET_SMSTATE(u64, smbase, 0x7e68);
2543 ctxt->ops->set_gdt(ctxt, &dt);
2544
Paolo Bonzini18276e92017-12-21 00:49:14 +01002545 r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002546 if (r != X86EMUL_CONTINUE)
2547 return r;
2548
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002549 for (i = 0; i < 6; i++) {
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002550 r = rsm_load_seg_64(ctxt, smbase, i);
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002551 if (r != X86EMUL_CONTINUE)
2552 return r;
2553 }
2554
Paolo Bonzinib10d92a2015-10-14 15:25:52 +02002555 return X86EMUL_CONTINUE;
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002556}
2557
Paolo Bonzini64d60672015-05-07 11:36:11 +02002558static int em_rsm(struct x86_emulate_ctxt *ctxt)
2559{
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002560 unsigned long cr0, cr4, efer;
2561 u64 smbase;
2562 int ret;
2563
Ladi Prosek1eeb7942017-04-25 16:42:44 +02002564 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
Paolo Bonzini64d60672015-05-07 11:36:11 +02002565 return emulate_ud(ctxt);
2566
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002567 /*
2568 * Get back to real mode, to prepare a safe state in which to load
Paolo Bonzini89651a32015-11-03 13:43:05 +01002569 * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
2570 * supports long mode.
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002571 */
Paolo Bonzini89651a32015-11-03 13:43:05 +01002572 cr4 = ctxt->ops->get_cr(ctxt, 4);
2573 if (emulator_has_longmode(ctxt)) {
2574 struct desc_struct cs_desc;
2575
2576 /* Zero CR4.PCIDE before CR0.PG. */
2577 if (cr4 & X86_CR4_PCIDE) {
2578 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
2579 cr4 &= ~X86_CR4_PCIDE;
2580 }
2581
2582 /* A 32-bit code segment is required to clear EFER.LMA. */
2583 memset(&cs_desc, 0, sizeof(cs_desc));
2584 cs_desc.type = 0xb;
2585 cs_desc.s = cs_desc.g = cs_desc.p = 1;
2586 ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
2587 }
2588
2589 /* For the 64-bit case, this will clear EFER.LMA. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002590 cr0 = ctxt->ops->get_cr(ctxt, 0);
2591 if (cr0 & X86_CR0_PE)
2592 ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
Paolo Bonzini89651a32015-11-03 13:43:05 +01002593
2594 /* Now clear CR4.PAE (which must be done before clearing EFER.LME). */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002595 if (cr4 & X86_CR4_PAE)
2596 ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
Paolo Bonzini89651a32015-11-03 13:43:05 +01002597
2598 /* And finally go back to 32-bit mode. */
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002599 efer = 0;
2600 ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
2601
2602 smbase = ctxt->ops->get_smbase(ctxt);
2603 if (emulator_has_longmode(ctxt))
2604 ret = rsm_load_state_64(ctxt, smbase + 0x8000);
2605 else
2606 ret = rsm_load_state_32(ctxt, smbase + 0x8000);
2607
2608 if (ret != X86EMUL_CONTINUE) {
2609 /* FIXME: should triple fault */
2610 return X86EMUL_UNHANDLEABLE;
2611 }
2612
Ladi Prosek1eeb7942017-04-25 16:42:44 +02002613 if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002614 ctxt->ops->set_nmi_mask(ctxt, false);
2615
Ladi Prosek1eeb7942017-04-25 16:42:44 +02002616 ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
2617 ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
Paolo Bonzini660a5d52015-05-05 11:50:23 +02002618 return X86EMUL_CONTINUE;
Paolo Bonzini64d60672015-05-07 11:36:11 +02002619}
2620
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002621static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002622setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002623 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002624{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002625 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002626 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002627 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002628 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002629 cs->type = 0x0b; /* Read, Execute, Accessed */
2630 cs->s = 1;
2631 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002632 cs->p = 1;
2633 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002634 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002635
Gleb Natapov79168fd2010-04-28 19:15:30 +03002636 set_desc_base(ss, 0); /* flat segment */
2637 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002638 ss->g = 1; /* 4kb granularity */
2639 ss->s = 1;
2640 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002641 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002642 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002643 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002644 ss->l = 0;
2645 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002646}
2647
Avi Kivity1a18a692012-02-01 12:23:21 +02002648static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2649{
2650 u32 eax, ebx, ecx, edx;
2651
2652 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002653 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2654 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002655 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2656 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2657}
2658
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002659static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2660{
Mathias Krause0225fb52012-08-30 01:30:16 +02002661 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002662 u32 eax, ebx, ecx, edx;
2663
2664 /*
2665 * syscall should always be enabled in longmode - so only become
2666 * vendor specific (cpuid) if other modes are active...
2667 */
2668 if (ctxt->mode == X86EMUL_MODE_PROT64)
2669 return true;
2670
2671 eax = 0x00000000;
2672 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002673 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2674 /*
2675 * Intel ("GenuineIntel")
2676 * remark: Intel CPUs only support "syscall" in 64bit
2677 * longmode. Also an 64bit guest with a
2678 * 32bit compat-app running will #UD !! While this
2679 * behaviour can be fixed (by emulating) into AMD
2680 * response - CPUs of AMD can't behave like Intel.
2681 */
2682 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2683 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2684 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2685 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002686
Avi Kivity0017f932012-06-07 14:10:16 +03002687 /* AMD ("AuthenticAMD") */
2688 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2689 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2690 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2691 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002692
Avi Kivity0017f932012-06-07 14:10:16 +03002693 /* AMD ("AMDisbetter!") */
2694 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2695 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2696 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2697 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002698
2699 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2700 return false;
2701}
2702
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002703static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002704{
Mathias Krause0225fb52012-08-30 01:30:16 +02002705 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002706 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002707 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002708 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002709 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002710
2711 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002712 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002713 ctxt->mode == X86EMUL_MODE_VM86)
2714 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002715
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002716 if (!(em_syscall_is_enabled(ctxt)))
2717 return emulate_ud(ctxt);
2718
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002719 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002720 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002721
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002722 if (!(efer & EFER_SCE))
2723 return emulate_ud(ctxt);
2724
Avi Kivity717746e2011-04-20 13:37:53 +03002725 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002726 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002727 cs_sel = (u16)(msr_data & 0xfffc);
2728 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002729
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002730 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002731 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002732 cs.l = 1;
2733 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002734 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2735 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002736
Avi Kivitydd856ef2012-08-27 23:46:17 +03002737 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002738 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002739#ifdef CONFIG_X86_64
Nadav Amit6c6cb692014-07-21 14:37:30 +03002740 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002741
Avi Kivity717746e2011-04-20 13:37:53 +03002742 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002743 ctxt->mode == X86EMUL_MODE_PROT64 ?
2744 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002745 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002746
Avi Kivity717746e2011-04-20 13:37:53 +03002747 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Nadav Amit6c6cb692014-07-21 14:37:30 +03002748 ctxt->eflags &= ~msr_data;
Wanpeng Li35fd68a2015-04-08 14:08:14 +08002749 ctxt->eflags |= X86_EFLAGS_FIXED;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002750#endif
2751 } else {
2752 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002753 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002754 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002755
Nadav Amit0efb0442015-03-29 16:33:03 +03002756 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002757 }
2758
Paolo Bonzini922e5622017-06-07 15:13:14 +02002759 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002760 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002761}
2762
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002763static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002764{
Mathias Krause0225fb52012-08-30 01:30:16 +02002765 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002766 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002767 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002768 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002769 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002770
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002771 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002772 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002773 if (ctxt->mode == X86EMUL_MODE_REAL)
2774 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002775
Avi Kivity1a18a692012-02-01 12:23:21 +02002776 /*
2777 * Not recognized on AMD in compat mode (but is recognized in legacy
2778 * mode).
2779 */
Nadav Amitf3747372015-01-01 23:11:11 +02002780 if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
Avi Kivity1a18a692012-02-01 12:23:21 +02002781 && !vendor_intel(ctxt))
2782 return emulate_ud(ctxt);
2783
Nadav Amitb2c9d432014-11-02 11:55:01 +02002784 /* sysenter/sysexit have not been tested in 64bit mode. */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002785 if (ctxt->mode == X86EMUL_MODE_PROT64)
Nadav Amitb2c9d432014-11-02 11:55:01 +02002786 return X86EMUL_UNHANDLEABLE;
Andre Przywara8c604352009-06-18 12:56:01 +02002787
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002788 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002789
Avi Kivity717746e2011-04-20 13:37:53 +03002790 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002791 if ((msr_data & 0xfffc) == 0x0)
2792 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002793
Nadav Amit0efb0442015-03-29 16:33:03 +03002794 ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
Nadav Amitb32a9912015-03-29 16:33:04 +03002795 cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002796 ss_sel = cs_sel + 8;
Nadav Amitf3747372015-01-01 23:11:11 +02002797 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002798 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002799 cs.l = 1;
2800 }
2801
Avi Kivity1aa36612011-04-27 13:20:30 +03002802 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2803 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002804
Avi Kivity717746e2011-04-20 13:37:53 +03002805 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002806 ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002807
Avi Kivity717746e2011-04-20 13:37:53 +03002808 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Nadav Amitf3747372015-01-01 23:11:11 +02002809 *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
2810 (u32)msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002811
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002812 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002813}
2814
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002815static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002816{
Mathias Krause0225fb52012-08-30 01:30:16 +02002817 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002818 struct desc_struct cs, ss;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002819 u64 msr_data, rcx, rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002820 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002821 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002822
Gleb Natapova0044752010-02-10 14:21:31 +02002823 /* inject #GP if in real mode or Virtual 8086 mode */
2824 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002825 ctxt->mode == X86EMUL_MODE_VM86)
2826 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002827
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002828 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002829
Avi Kivity9dac77f2011-06-01 15:34:25 +03002830 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002831 usermode = X86EMUL_MODE_PROT64;
2832 else
2833 usermode = X86EMUL_MODE_PROT32;
2834
Nadav Amit234f3ce2014-09-18 22:39:38 +03002835 rcx = reg_read(ctxt, VCPU_REGS_RCX);
2836 rdx = reg_read(ctxt, VCPU_REGS_RDX);
2837
Andre Przywara4668f052009-06-18 12:56:02 +02002838 cs.dpl = 3;
2839 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002840 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002841 switch (usermode) {
2842 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002843 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002844 if ((msr_data & 0xfffc) == 0x0)
2845 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002846 ss_sel = (u16)(msr_data + 24);
Nadav Amitbf0b6822014-09-18 22:39:45 +03002847 rcx = (u32)rcx;
2848 rdx = (u32)rdx;
Andre Przywara4668f052009-06-18 12:56:02 +02002849 break;
2850 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002851 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002852 if (msr_data == 0x0)
2853 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002854 ss_sel = cs_sel + 8;
2855 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002856 cs.l = 1;
Nadav Amit234f3ce2014-09-18 22:39:38 +03002857 if (is_noncanonical_address(rcx) ||
2858 is_noncanonical_address(rdx))
2859 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002860 break;
2861 }
Nadav Amitb32a9912015-03-29 16:33:04 +03002862 cs_sel |= SEGMENT_RPL_MASK;
2863 ss_sel |= SEGMENT_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002864
Avi Kivity1aa36612011-04-27 13:20:30 +03002865 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2866 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002867
Nadav Amit234f3ce2014-09-18 22:39:38 +03002868 ctxt->_eip = rdx;
2869 *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
Andre Przywara4668f052009-06-18 12:56:02 +02002870
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002871 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002872}
2873
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002874static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002875{
2876 int iopl;
2877 if (ctxt->mode == X86EMUL_MODE_REAL)
2878 return false;
2879 if (ctxt->mode == X86EMUL_MODE_VM86)
2880 return true;
Nadav Amit0efb0442015-03-29 16:33:03 +03002881 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002882 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002883}
2884
2885static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002886 u16 port, u16 len)
2887{
Mathias Krause0225fb52012-08-30 01:30:16 +02002888 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002889 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002890 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002891 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002892 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002893 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002894 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002895
Avi Kivity1aa36612011-04-27 13:20:30 +03002896 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002897 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002898 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002899 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002900 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002901 base = get_desc_base(&tr_seg);
2902#ifdef CONFIG_X86_64
2903 base |= ((u64)base3) << 32;
2904#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002905 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002906 if (r != X86EMUL_CONTINUE)
2907 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002908 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002909 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002910 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002911 if (r != X86EMUL_CONTINUE)
2912 return false;
2913 if ((perm >> bit_idx) & mask)
2914 return false;
2915 return true;
2916}
2917
2918static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002919 u16 port, u16 len)
2920{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002921 if (ctxt->perm_ok)
2922 return true;
2923
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002924 if (emulator_bad_iopl(ctxt))
2925 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002926 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002927
2928 ctxt->perm_ok = true;
2929
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002930 return true;
2931}
2932
Nadav Amit428e3d02015-04-28 13:06:01 +03002933static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
2934{
2935 /*
2936 * Intel CPUs mask the counter and pointers in quite strange
2937 * manner when ECX is zero due to REP-string optimizations.
2938 */
2939#ifdef CONFIG_X86_64
2940 if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
2941 return;
2942
2943 *reg_write(ctxt, VCPU_REGS_RCX) = 0;
2944
2945 switch (ctxt->b) {
2946 case 0xa4: /* movsb */
2947 case 0xa5: /* movsd/w */
2948 *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
2949 /* fall through */
2950 case 0xaa: /* stosb */
2951 case 0xab: /* stosd/w */
2952 *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
2953 }
2954#endif
2955}
2956
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002957static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002958 struct tss_segment_16 *tss)
2959{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002960 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002961 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002962 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2963 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2964 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2965 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2966 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2967 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2968 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2969 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002970
Avi Kivity1aa36612011-04-27 13:20:30 +03002971 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2972 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2973 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2974 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2975 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002976}
2977
2978static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002979 struct tss_segment_16 *tss)
2980{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002981 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02002982 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002983
Avi Kivity9dac77f2011-06-01 15:34:25 +03002984 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002985 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002986 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2987 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2988 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2989 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2990 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2991 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2992 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2993 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002994
2995 /*
2996 * SDM says that segment selectors are loaded before segment
2997 * descriptors
2998 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002999 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
3000 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3001 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3002 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3003 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003004
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003005 cpl = tss->cs & 3;
3006
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003007 /*
Guo Chaofc058682012-06-28 15:19:51 +08003008 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003009 * it is handled in a context of new task
3010 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003011 ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003012 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003013 if (ret != X86EMUL_CONTINUE)
3014 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003015 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003016 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003017 if (ret != X86EMUL_CONTINUE)
3018 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003019 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003020 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003021 if (ret != X86EMUL_CONTINUE)
3022 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003023 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003024 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003025 if (ret != X86EMUL_CONTINUE)
3026 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003027 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003028 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003029 if (ret != X86EMUL_CONTINUE)
3030 return ret;
3031
3032 return X86EMUL_CONTINUE;
3033}
3034
3035static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003036 u16 tss_selector, u16 old_tss_sel,
3037 ulong old_tss_base, struct desc_struct *new_desc)
3038{
Mathias Krause0225fb52012-08-30 01:30:16 +02003039 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003040 struct tss_segment_16 tss_seg;
3041 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003042 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003043
Avi Kivity0f65dd72011-04-20 13:37:53 +03003044 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003045 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003046 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003047 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003048
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003049 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003050
Avi Kivity0f65dd72011-04-20 13:37:53 +03003051 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003052 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003053 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003054 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003055
Avi Kivity0f65dd72011-04-20 13:37:53 +03003056 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003057 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003058 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003059 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003060
3061 if (old_tss_sel != 0xffff) {
3062 tss_seg.prev_task_link = old_tss_sel;
3063
Avi Kivity0f65dd72011-04-20 13:37:53 +03003064 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003065 &tss_seg.prev_task_link,
3066 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03003067 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003068 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003069 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003070 }
3071
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003072 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003073}
3074
3075static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003076 struct tss_segment_32 *tss)
3077{
Nadav Amit5c7411e2014-04-07 18:37:47 +03003078 /* CR3 and ldt selector are not saved intentionally */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003079 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003080 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003081 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
3082 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
3083 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
3084 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
3085 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
3086 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
3087 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
3088 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003089
Avi Kivity1aa36612011-04-27 13:20:30 +03003090 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
3091 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
3092 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
3093 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
3094 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
3095 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003096}
3097
3098static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003099 struct tss_segment_32 *tss)
3100{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003101 int ret;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003102 u8 cpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003103
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003104 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003105 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003106 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003107 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01003108
3109 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03003110 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
3111 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
3112 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
3113 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
3114 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
3115 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
3116 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
3117 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003118
3119 /*
3120 * SDM says that segment selectors are loaded before segment
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003121 * descriptors. This is important because CPL checks will
3122 * use CS.RPL.
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003123 */
Avi Kivity1aa36612011-04-27 13:20:30 +03003124 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
3125 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
3126 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
3127 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
3128 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
3129 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
3130 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003131
3132 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01003133 * If we're switching between Protected Mode and VM86, we need to make
3134 * sure to update the mode before loading the segment descriptors so
3135 * that the selectors are interpreted correctly.
Kevin Wolf4cee4792012-02-08 14:34:41 +01003136 */
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003137 if (ctxt->eflags & X86_EFLAGS_VM) {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003138 ctxt->mode = X86EMUL_MODE_VM86;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003139 cpl = 3;
3140 } else {
Kevin Wolf4cee4792012-02-08 14:34:41 +01003141 ctxt->mode = X86EMUL_MODE_PROT32;
Paolo Bonzini2356aae2014-05-15 17:56:57 +02003142 cpl = tss->cs & 3;
3143 }
Kevin Wolf4cee4792012-02-08 14:34:41 +01003144
3145 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003146 * Now load segment descriptors. If fault happenes at this stage
3147 * it is handled in a context of new task
3148 */
Nadav Amitd1442d82014-09-18 22:39:39 +03003149 ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003150 cpl, X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003151 if (ret != X86EMUL_CONTINUE)
3152 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003153 ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003154 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003155 if (ret != X86EMUL_CONTINUE)
3156 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003157 ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003158 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003159 if (ret != X86EMUL_CONTINUE)
3160 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003161 ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003162 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003163 if (ret != X86EMUL_CONTINUE)
3164 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003165 ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003166 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003167 if (ret != X86EMUL_CONTINUE)
3168 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003169 ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003170 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003171 if (ret != X86EMUL_CONTINUE)
3172 return ret;
Nadav Amitd1442d82014-09-18 22:39:39 +03003173 ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003174 X86_TRANSFER_TASK_SWITCH, NULL);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003175
Eugene Korenevsky2f729b12015-03-29 01:27:17 +03003176 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003177}
3178
3179static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003180 u16 tss_selector, u16 old_tss_sel,
3181 ulong old_tss_base, struct desc_struct *new_desc)
3182{
Mathias Krause0225fb52012-08-30 01:30:16 +02003183 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003184 struct tss_segment_32 tss_seg;
3185 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003186 u32 new_tss_base = get_desc_base(new_desc);
Nadav Amit5c7411e2014-04-07 18:37:47 +03003187 u32 eip_offset = offsetof(struct tss_segment_32, eip);
3188 u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003189
Avi Kivity0f65dd72011-04-20 13:37:53 +03003190 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003191 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003192 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003193 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003194
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003195 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003196
Nadav Amit5c7411e2014-04-07 18:37:47 +03003197 /* Only GP registers and segment selectors are saved */
3198 ret = ops->write_std(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
3199 ldt_sel_offset - eip_offset, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003200 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003201 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003202
Avi Kivity0f65dd72011-04-20 13:37:53 +03003203 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02003204 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003205 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003206 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003207
3208 if (old_tss_sel != 0xffff) {
3209 tss_seg.prev_task_link = old_tss_sel;
3210
Avi Kivity0f65dd72011-04-20 13:37:53 +03003211 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003212 &tss_seg.prev_task_link,
3213 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03003214 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02003215 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003216 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003217 }
3218
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003219 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003220}
3221
3222static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003223 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003224 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003225{
Mathias Krause0225fb52012-08-30 01:30:16 +02003226 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003227 struct desc_struct curr_tss_desc, next_tss_desc;
3228 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03003229 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003230 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03003231 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02003232 u32 desc_limit;
Nadav Amit3db176d2015-04-19 21:12:59 +03003233 ulong desc_addr, dr7;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003234
3235 /* FIXME: old_tss_base == ~0 ? */
3236
Avi Kivitye9194642012-06-13 16:29:39 +03003237 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003238 if (ret != X86EMUL_CONTINUE)
3239 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03003240 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003241 if (ret != X86EMUL_CONTINUE)
3242 return ret;
3243
3244 /* FIXME: check that next_tss_desc is tss */
3245
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003246 /*
3247 * Check privileges. The three cases are task switch caused by...
3248 *
3249 * 1. jmp/call/int to task gate: Check against DPL of the task gate
3250 * 2. Exception/IRQ/iret: No check is performed
Nadav Amit2c2ca2d2014-11-02 11:54:57 +02003251 * 3. jmp/call to TSS/task-gate: No check is performed since the
3252 * hardware checks it before exiting.
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003253 */
3254 if (reason == TASK_SWITCH_GATE) {
3255 if (idt_index != -1) {
3256 /* Software interrupts */
3257 struct desc_struct task_gate_desc;
3258 int dpl;
3259
3260 ret = read_interrupt_descriptor(ctxt, idt_index,
3261 &task_gate_desc);
3262 if (ret != X86EMUL_CONTINUE)
3263 return ret;
3264
3265 dpl = task_gate_desc.dpl;
3266 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
3267 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
3268 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003269 }
3270
Gleb Natapovceffb452010-03-18 15:20:19 +02003271 desc_limit = desc_limit_scaled(&next_tss_desc);
3272 if (!next_tss_desc.p ||
3273 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
3274 desc_limit < 0x2b)) {
Paolo Bonzini592f0852014-08-20 10:05:08 +02003275 return emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003276 }
3277
3278 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3279 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003280 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003281 }
3282
3283 if (reason == TASK_SWITCH_IRET)
3284 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
3285
3286 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08003287 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003288 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
3289 old_tss_sel = 0xffff;
3290
3291 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003292 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003293 old_tss_base, &next_tss_desc);
3294 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003295 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003296 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02003297 if (ret != X86EMUL_CONTINUE)
3298 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003299
3300 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
3301 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
3302
3303 if (reason != TASK_SWITCH_IRET) {
3304 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003305 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003306 }
3307
Avi Kivity717746e2011-04-20 13:37:53 +03003308 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03003309 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003310
Jan Kiszkae269fb22010-04-14 15:51:09 +02003311 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003312 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
3313 ctxt->lock_prefix = 0;
3314 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003315 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02003316 }
3317
Nadav Amit3db176d2015-04-19 21:12:59 +03003318 ops->get_dr(ctxt, 7, &dr7);
3319 ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
3320
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003321 return ret;
3322}
3323
3324int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003325 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003326 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003327{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003328 int rc;
3329
Avi Kivitydd856ef2012-08-27 23:46:17 +03003330 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003331 ctxt->_eip = ctxt->eip;
3332 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003333
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003334 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02003335 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003336
Avi Kivitydd856ef2012-08-27 23:46:17 +03003337 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003338 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003339 writeback_registers(ctxt);
3340 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003341
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003342 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02003343}
3344
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03003345static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
3346 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02003347{
Nadav Amit0efb0442015-03-29 16:33:03 +03003348 int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02003349
Paolo Bonzini01485a22014-11-19 18:25:08 +01003350 register_address_increment(ctxt, reg, df * op->bytes);
3351 op->addr.mem.ea = register_address(ctxt, reg);
Gleb Natapova682e352010-03-18 15:20:21 +02003352}
3353
Avi Kivity7af04fc2010-08-18 14:16:35 +03003354static int em_das(struct x86_emulate_ctxt *ctxt)
3355{
Avi Kivity7af04fc2010-08-18 14:16:35 +03003356 u8 al, old_al;
3357 bool af, cf, old_cf;
3358
3359 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003360 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003361
3362 old_al = al;
3363 old_cf = cf;
3364 cf = false;
3365 af = ctxt->eflags & X86_EFLAGS_AF;
3366 if ((al & 0x0f) > 9 || af) {
3367 al -= 6;
3368 cf = old_cf | (al >= 250);
3369 af = true;
3370 } else {
3371 af = false;
3372 }
3373 if (old_al > 0x99 || old_cf) {
3374 al -= 0x60;
3375 cf = true;
3376 }
3377
Avi Kivity9dac77f2011-06-01 15:34:25 +03003378 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03003379 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003380 ctxt->src.type = OP_IMM;
3381 ctxt->src.val = 0;
3382 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02003383 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03003384 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
3385 if (cf)
3386 ctxt->eflags |= X86_EFLAGS_CF;
3387 if (af)
3388 ctxt->eflags |= X86_EFLAGS_AF;
3389 return X86EMUL_CONTINUE;
3390}
3391
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003392static int em_aam(struct x86_emulate_ctxt *ctxt)
3393{
3394 u8 al, ah;
3395
3396 if (ctxt->src.val == 0)
3397 return emulate_de(ctxt);
3398
3399 al = ctxt->dst.val & 0xff;
3400 ah = al / ctxt->src.val;
3401 al %= ctxt->src.val;
3402
3403 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3404
3405 /* Set PF, ZF, SF */
3406 ctxt->src.type = OP_IMM;
3407 ctxt->src.val = 0;
3408 ctxt->src.bytes = 1;
3409 fastop(ctxt, em_or);
3410
3411 return X86EMUL_CONTINUE;
3412}
3413
Gleb Natapov7f662272012-12-10 11:42:30 +02003414static int em_aad(struct x86_emulate_ctxt *ctxt)
3415{
3416 u8 al = ctxt->dst.val & 0xff;
3417 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3418
3419 al = (al + (ah * ctxt->src.val)) & 0xff;
3420
3421 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3422
Gleb Natapovf583c292013-02-13 17:50:39 +02003423 /* Set PF, ZF, SF */
3424 ctxt->src.type = OP_IMM;
3425 ctxt->src.val = 0;
3426 ctxt->src.bytes = 1;
3427 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003428
3429 return X86EMUL_CONTINUE;
3430}
3431
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003432static int em_call(struct x86_emulate_ctxt *ctxt)
3433{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003434 int rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003435 long rel = ctxt->src.val;
3436
3437 ctxt->src.val = (unsigned long)ctxt->_eip;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003438 rc = jmp_rel(ctxt, rel);
3439 if (rc != X86EMUL_CONTINUE)
3440 return rc;
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003441 return em_push(ctxt);
3442}
3443
Avi Kivity0ef753b2010-08-18 14:51:45 +03003444static int em_call_far(struct x86_emulate_ctxt *ctxt)
3445{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003446 u16 sel, old_cs;
3447 ulong old_eip;
3448 int rc;
Nadav Amitd1442d82014-09-18 22:39:39 +03003449 struct desc_struct old_desc, new_desc;
3450 const struct x86_emulate_ops *ops = ctxt->ops;
3451 int cpl = ctxt->ops->cpl(ctxt);
Nadav Amit82268082015-01-26 09:32:27 +02003452 enum x86emul_mode prev_mode = ctxt->mode;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003453
Avi Kivity9dac77f2011-06-01 15:34:25 +03003454 old_eip = ctxt->_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003455 ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003456
Avi Kivity9dac77f2011-06-01 15:34:25 +03003457 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Nadav Amit3dc4bc42014-12-25 02:52:19 +02003458 rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
3459 X86_TRANSFER_CALL_JMP, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003460 if (rc != X86EMUL_CONTINUE)
Nadav Amit80976db2014-12-25 02:52:20 +02003461 return rc;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003462
Nadav Amitd50eaa12014-11-19 17:43:11 +02003463 rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
Nadav Amitd1442d82014-09-18 22:39:39 +03003464 if (rc != X86EMUL_CONTINUE)
3465 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003466
Avi Kivity9dac77f2011-06-01 15:34:25 +03003467 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003468 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003469 if (rc != X86EMUL_CONTINUE)
Nadav Amitd1442d82014-09-18 22:39:39 +03003470 goto fail;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003471
Avi Kivity9dac77f2011-06-01 15:34:25 +03003472 ctxt->src.val = old_eip;
Nadav Amitd1442d82014-09-18 22:39:39 +03003473 rc = em_push(ctxt);
3474 /* If we failed, we tainted the memory, but the very least we should
3475 restore cs */
Nadav Amit82268082015-01-26 09:32:27 +02003476 if (rc != X86EMUL_CONTINUE) {
3477 pr_warn_once("faulting far call emulation tainted memory\n");
Nadav Amitd1442d82014-09-18 22:39:39 +03003478 goto fail;
Nadav Amit82268082015-01-26 09:32:27 +02003479 }
Nadav Amitd1442d82014-09-18 22:39:39 +03003480 return rc;
3481fail:
3482 ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
Nadav Amit82268082015-01-26 09:32:27 +02003483 ctxt->mode = prev_mode;
Nadav Amitd1442d82014-09-18 22:39:39 +03003484 return rc;
3485
Avi Kivity0ef753b2010-08-18 14:51:45 +03003486}
3487
Avi Kivity40ece7c2010-08-18 15:12:09 +03003488static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3489{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003490 int rc;
Nadav Amit234f3ce2014-09-18 22:39:38 +03003491 unsigned long eip;
Avi Kivity40ece7c2010-08-18 15:12:09 +03003492
Nadav Amit234f3ce2014-09-18 22:39:38 +03003493 rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
3494 if (rc != X86EMUL_CONTINUE)
3495 return rc;
3496 rc = assign_eip_near(ctxt, eip);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003497 if (rc != X86EMUL_CONTINUE)
3498 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003499 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003500 return X86EMUL_CONTINUE;
3501}
3502
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003503static int em_xchg(struct x86_emulate_ctxt *ctxt)
3504{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003505 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003506 ctxt->src.val = ctxt->dst.val;
3507 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003508
3509 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003510 ctxt->dst.val = ctxt->src.orig_val;
3511 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003512 return X86EMUL_CONTINUE;
3513}
3514
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003515static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3516{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003517 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003518 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003519}
3520
Avi Kivity61429142010-08-19 15:13:00 +03003521static int em_cwd(struct x86_emulate_ctxt *ctxt)
3522{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003523 ctxt->dst.type = OP_REG;
3524 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003525 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003526 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003527
3528 return X86EMUL_CONTINUE;
3529}
3530
Avi Kivity48bb5d32010-08-18 18:54:34 +03003531static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3532{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003533 u64 tsc = 0;
3534
Avi Kivity717746e2011-04-20 13:37:53 +03003535 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003536 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3537 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003538 return X86EMUL_CONTINUE;
3539}
3540
Avi Kivity222d21a2011-11-10 14:57:30 +02003541static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3542{
3543 u64 pmc;
3544
Avi Kivitydd856ef2012-08-27 23:46:17 +03003545 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003546 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003547 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3548 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003549 return X86EMUL_CONTINUE;
3550}
3551
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003552static int em_mov(struct x86_emulate_ctxt *ctxt)
3553{
Paolo Bonzini54cfdb32014-03-27 11:36:25 +01003554 memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003555 return X86EMUL_CONTINUE;
3556}
3557
Borislav Petkov84cffe42013-10-29 12:54:56 +01003558#define FFL(x) bit(X86_FEATURE_##x)
3559
3560static int em_movbe(struct x86_emulate_ctxt *ctxt)
3561{
3562 u32 ebx, ecx, edx, eax = 1;
3563 u16 tmp;
3564
3565 /*
3566 * Check MOVBE is set in the guest-visible CPUID leaf.
3567 */
3568 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3569 if (!(ecx & FFL(MOVBE)))
3570 return emulate_ud(ctxt);
3571
3572 switch (ctxt->op_bytes) {
3573 case 2:
3574 /*
3575 * From MOVBE definition: "...When the operand size is 16 bits,
3576 * the upper word of the destination register remains unchanged
3577 * ..."
3578 *
3579 * Both casting ->valptr and ->val to u16 breaks strict aliasing
3580 * rules so we have to do the operation almost per hand.
3581 */
3582 tmp = (u16)ctxt->src.val;
3583 ctxt->dst.val &= ~0xffffUL;
3584 ctxt->dst.val |= (unsigned long)swab16(tmp);
3585 break;
3586 case 4:
3587 ctxt->dst.val = swab32((u32)ctxt->src.val);
3588 break;
3589 case 8:
3590 ctxt->dst.val = swab64(ctxt->src.val);
3591 break;
3592 default:
Paolo Bonzini592f0852014-08-20 10:05:08 +02003593 BUG();
Borislav Petkov84cffe42013-10-29 12:54:56 +01003594 }
3595 return X86EMUL_CONTINUE;
3596}
3597
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003598static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3599{
3600 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3601 return emulate_gp(ctxt, 0);
3602
3603 /* Disable writeback. */
3604 ctxt->dst.type = OP_NONE;
3605 return X86EMUL_CONTINUE;
3606}
3607
3608static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3609{
3610 unsigned long val;
3611
3612 if (ctxt->mode == X86EMUL_MODE_PROT64)
3613 val = ctxt->src.val & ~0ULL;
3614 else
3615 val = ctxt->src.val & ~0U;
3616
3617 /* #UD condition is already handled. */
3618 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3619 return emulate_gp(ctxt, 0);
3620
3621 /* Disable writeback. */
3622 ctxt->dst.type = OP_NONE;
3623 return X86EMUL_CONTINUE;
3624}
3625
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003626static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3627{
3628 u64 msr_data;
3629
Avi Kivitydd856ef2012-08-27 23:46:17 +03003630 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3631 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3632 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003633 return emulate_gp(ctxt, 0);
3634
3635 return X86EMUL_CONTINUE;
3636}
3637
3638static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3639{
3640 u64 msr_data;
3641
Avi Kivitydd856ef2012-08-27 23:46:17 +03003642 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003643 return emulate_gp(ctxt, 0);
3644
Avi Kivitydd856ef2012-08-27 23:46:17 +03003645 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3646 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003647 return X86EMUL_CONTINUE;
3648}
3649
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003650static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3651{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003652 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003653 return emulate_ud(ctxt);
3654
Avi Kivity9dac77f2011-06-01 15:34:25 +03003655 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Nadav Amitb5bbf102014-11-02 11:54:46 +02003656 if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
3657 ctxt->dst.bytes = 2;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003658 return X86EMUL_CONTINUE;
3659}
3660
3661static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003664
Avi Kivity9dac77f2011-06-01 15:34:25 +03003665 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003666 return emulate_ud(ctxt);
3667
Avi Kivity9dac77f2011-06-01 15:34:25 +03003668 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003669 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3670
3671 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003672 ctxt->dst.type = OP_NONE;
3673 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003674}
3675
Avi Kivitya14e5792012-06-13 12:28:33 +03003676static int em_lldt(struct x86_emulate_ctxt *ctxt)
3677{
3678 u16 sel = ctxt->src.val;
3679
3680 /* Disable writeback. */
3681 ctxt->dst.type = OP_NONE;
3682 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3683}
3684
Avi Kivity80890002012-06-13 16:33:29 +03003685static int em_ltr(struct x86_emulate_ctxt *ctxt)
3686{
3687 u16 sel = ctxt->src.val;
3688
3689 /* Disable writeback. */
3690 ctxt->dst.type = OP_NONE;
3691 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3692}
3693
Avi Kivity38503912011-03-31 18:48:09 +02003694static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3695{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003696 int rc;
3697 ulong linear;
3698
Avi Kivity9dac77f2011-06-01 15:34:25 +03003699 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003700 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003701 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003702 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003703 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003704 return X86EMUL_CONTINUE;
3705}
3706
Avi Kivity2d04a052011-04-20 15:32:49 +03003707static int em_clts(struct x86_emulate_ctxt *ctxt)
3708{
3709 ulong cr0;
3710
3711 cr0 = ctxt->ops->get_cr(ctxt, 0);
3712 cr0 &= ~X86_CR0_TS;
3713 ctxt->ops->set_cr(ctxt, 0, cr0);
3714 return X86EMUL_CONTINUE;
3715}
3716
Jan Kiszkab34a8052015-03-09 20:27:43 +01003717static int em_hypercall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003718{
Nadav Amit0f54a322014-08-29 11:26:55 +03003719 int rc = ctxt->ops->fix_hypercall(ctxt);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003720
Avi Kivity26d05cc2011-04-21 12:07:59 +03003721 if (rc != X86EMUL_CONTINUE)
3722 return rc;
3723
3724 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003725 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003726 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003727 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003728 return X86EMUL_CONTINUE;
3729}
3730
Avi Kivity96051572012-06-10 17:21:18 +03003731static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3732 void (*get)(struct x86_emulate_ctxt *ctxt,
3733 struct desc_ptr *ptr))
3734{
3735 struct desc_ptr desc_ptr;
3736
3737 if (ctxt->mode == X86EMUL_MODE_PROT64)
3738 ctxt->op_bytes = 8;
3739 get(ctxt, &desc_ptr);
3740 if (ctxt->op_bytes == 2) {
3741 ctxt->op_bytes = 4;
3742 desc_ptr.address &= 0x00ffffff;
3743 }
3744 /* Disable writeback. */
3745 ctxt->dst.type = OP_NONE;
Steve Rutherford736e77c02017-01-11 18:28:29 -08003746 return segmented_write_std(ctxt, ctxt->dst.addr.mem,
3747 &desc_ptr, 2 + ctxt->op_bytes);
Avi Kivity96051572012-06-10 17:21:18 +03003748}
3749
3750static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3751{
3752 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3753}
3754
3755static int em_sidt(struct x86_emulate_ctxt *ctxt)
3756{
3757 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3758}
3759
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003760static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003761{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003762 struct desc_ptr desc_ptr;
3763 int rc;
3764
Avi Kivity510425f2012-06-07 17:04:36 +03003765 if (ctxt->mode == X86EMUL_MODE_PROT64)
3766 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003767 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003768 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003769 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003770 if (rc != X86EMUL_CONTINUE)
3771 return rc;
Nadav Amit9a9abf62014-11-02 11:54:56 +02003772 if (ctxt->mode == X86EMUL_MODE_PROT64 &&
3773 is_noncanonical_address(desc_ptr.address))
3774 return emulate_gp(ctxt, 0);
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003775 if (lgdt)
3776 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3777 else
3778 ctxt->ops->set_idt(ctxt, &desc_ptr);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003779 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003780 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003781 return X86EMUL_CONTINUE;
3782}
3783
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003784static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3785{
3786 return em_lgdt_lidt(ctxt, true);
3787}
3788
Avi Kivity26d05cc2011-04-21 12:07:59 +03003789static int em_lidt(struct x86_emulate_ctxt *ctxt)
3790{
Nadav Amit5b7f6a12014-11-02 11:54:55 +02003791 return em_lgdt_lidt(ctxt, false);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003792}
3793
3794static int em_smsw(struct x86_emulate_ctxt *ctxt)
3795{
Nadav Amit32e94d02014-06-02 18:34:11 +03003796 if (ctxt->dst.type == OP_MEM)
3797 ctxt->dst.bytes = 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003798 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003799 return X86EMUL_CONTINUE;
3800}
3801
3802static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3803{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003804 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003805 | (ctxt->src.val & 0x0f));
3806 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003807 return X86EMUL_CONTINUE;
3808}
3809
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003810static int em_loop(struct x86_emulate_ctxt *ctxt)
3811{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003812 int rc = X86EMUL_CONTINUE;
3813
Paolo Bonzini01485a22014-11-19 18:25:08 +01003814 register_address_increment(ctxt, VCPU_REGS_RCX, -1);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003815 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003816 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
Nadav Amit234f3ce2014-09-18 22:39:38 +03003817 rc = jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003818
Nadav Amit234f3ce2014-09-18 22:39:38 +03003819 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003820}
3821
3822static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3823{
Nadav Amit234f3ce2014-09-18 22:39:38 +03003824 int rc = X86EMUL_CONTINUE;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003825
Nadav Amit234f3ce2014-09-18 22:39:38 +03003826 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
3827 rc = jmp_rel(ctxt, ctxt->src.val);
3828
3829 return rc;
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003830}
3831
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003832static int em_in(struct x86_emulate_ctxt *ctxt)
3833{
3834 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3835 &ctxt->dst.val))
3836 return X86EMUL_IO_NEEDED;
3837
3838 return X86EMUL_CONTINUE;
3839}
3840
3841static int em_out(struct x86_emulate_ctxt *ctxt)
3842{
3843 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3844 &ctxt->src.val, 1);
3845 /* Disable writeback. */
3846 ctxt->dst.type = OP_NONE;
3847 return X86EMUL_CONTINUE;
3848}
3849
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003850static int em_cli(struct x86_emulate_ctxt *ctxt)
3851{
3852 if (emulator_bad_iopl(ctxt))
3853 return emulate_gp(ctxt, 0);
3854
3855 ctxt->eflags &= ~X86_EFLAGS_IF;
3856 return X86EMUL_CONTINUE;
3857}
3858
3859static int em_sti(struct x86_emulate_ctxt *ctxt)
3860{
3861 if (emulator_bad_iopl(ctxt))
3862 return emulate_gp(ctxt, 0);
3863
3864 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3865 ctxt->eflags |= X86_EFLAGS_IF;
3866 return X86EMUL_CONTINUE;
3867}
3868
Avi Kivity6d6eede2012-06-07 14:11:36 +03003869static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3870{
3871 u32 eax, ebx, ecx, edx;
3872
Avi Kivitydd856ef2012-08-27 23:46:17 +03003873 eax = reg_read(ctxt, VCPU_REGS_RAX);
3874 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003875 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003876 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3877 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3878 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3879 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003880 return X86EMUL_CONTINUE;
3881}
3882
Paolo Bonzini98f73632013-10-31 11:19:42 +01003883static int em_sahf(struct x86_emulate_ctxt *ctxt)
3884{
3885 u32 flags;
3886
Nadav Amit0efb0442015-03-29 16:33:03 +03003887 flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
3888 X86_EFLAGS_SF;
Paolo Bonzini98f73632013-10-31 11:19:42 +01003889 flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
3890
3891 ctxt->eflags &= ~0xffUL;
3892 ctxt->eflags |= flags | X86_EFLAGS_FIXED;
3893 return X86EMUL_CONTINUE;
3894}
3895
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003896static int em_lahf(struct x86_emulate_ctxt *ctxt)
3897{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003898 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3899 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003900 return X86EMUL_CONTINUE;
3901}
3902
Avi Kivity92998362012-06-13 12:25:06 +03003903static int em_bswap(struct x86_emulate_ctxt *ctxt)
3904{
3905 switch (ctxt->op_bytes) {
3906#ifdef CONFIG_X86_64
3907 case 8:
3908 asm("bswap %0" : "+r"(ctxt->dst.val));
3909 break;
3910#endif
3911 default:
3912 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3913 break;
3914 }
3915 return X86EMUL_CONTINUE;
3916}
3917
Nadav Amit13e457e2014-10-13 13:04:13 +03003918static int em_clflush(struct x86_emulate_ctxt *ctxt)
3919{
3920 /* emulating clflush regardless of cpuid */
3921 return X86EMUL_CONTINUE;
3922}
3923
Nadav Amit2276b512015-01-26 09:32:24 +02003924static int em_movsxd(struct x86_emulate_ctxt *ctxt)
3925{
3926 ctxt->dst.val = (s32) ctxt->src.val;
3927 return X86EMUL_CONTINUE;
3928}
3929
Radim Krčmář83fedbb2016-11-09 19:07:06 +01003930static int check_fxsr(struct x86_emulate_ctxt *ctxt)
3931{
3932 u32 eax = 1, ebx, ecx = 0, edx;
3933
3934 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3935 if (!(edx & FFL(FXSR)))
3936 return emulate_ud(ctxt);
3937
3938 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
3939 return emulate_nm(ctxt);
3940
3941 /*
3942 * Don't emulate a case that should never be hit, instead of working
3943 * around a lack of fxsave64/fxrstor64 on old compilers.
3944 */
3945 if (ctxt->mode >= X86EMUL_MODE_PROT64)
3946 return X86EMUL_UNHANDLEABLE;
3947
3948 return X86EMUL_CONTINUE;
3949}
3950
3951/*
3952 * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
3953 * 1) 16 bit mode
3954 * 2) 32 bit mode
3955 * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
3956 * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
3957 * save and restore
3958 * 3) 64-bit mode with REX.W prefix
3959 * - like (2), but XMM 8-15 are being saved and restored
3960 * 4) 64-bit mode without REX.W prefix
3961 * - like (3), but FIP and FDP are 64 bit
3962 *
3963 * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
3964 * desired result. (4) is not emulated.
3965 *
3966 * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
3967 * and FPU DS) should match.
3968 */
3969static int em_fxsave(struct x86_emulate_ctxt *ctxt)
3970{
3971 struct fxregs_state fx_state;
3972 size_t size;
3973 int rc;
3974
3975 rc = check_fxsr(ctxt);
3976 if (rc != X86EMUL_CONTINUE)
3977 return rc;
3978
3979 ctxt->ops->get_fpu(ctxt);
3980
3981 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
3982
3983 ctxt->ops->put_fpu(ctxt);
3984
3985 if (rc != X86EMUL_CONTINUE)
3986 return rc;
3987
3988 if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR)
3989 size = offsetof(struct fxregs_state, xmm_space[8 * 16/4]);
3990 else
3991 size = offsetof(struct fxregs_state, xmm_space[0]);
3992
Steve Rutherford736e77c02017-01-11 18:28:29 -08003993 return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
Radim Krčmář83fedbb2016-11-09 19:07:06 +01003994}
3995
3996static int fxrstor_fixup(struct x86_emulate_ctxt *ctxt,
3997 struct fxregs_state *new)
3998{
3999 int rc = X86EMUL_CONTINUE;
4000 struct fxregs_state old;
4001
4002 rc = asm_safe("fxsave %[fx]", , [fx] "+m"(old));
4003 if (rc != X86EMUL_CONTINUE)
4004 return rc;
4005
4006 /*
4007 * 64 bit host will restore XMM 8-15, which is not correct on non-64
4008 * bit guests. Load the current values in order to preserve 64 bit
4009 * XMMs after fxrstor.
4010 */
4011#ifdef CONFIG_X86_64
4012 /* XXX: accessing XMM 8-15 very awkwardly */
4013 memcpy(&new->xmm_space[8 * 16/4], &old.xmm_space[8 * 16/4], 8 * 16);
4014#endif
4015
4016 /*
4017 * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but
4018 * does save and restore MXCSR.
4019 */
4020 if (!(ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))
4021 memcpy(new->xmm_space, old.xmm_space, 8 * 16);
4022
4023 return rc;
4024}
4025
4026static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
4027{
4028 struct fxregs_state fx_state;
4029 int rc;
4030
4031 rc = check_fxsr(ctxt);
4032 if (rc != X86EMUL_CONTINUE)
4033 return rc;
4034
Steve Rutherford736e77c02017-01-11 18:28:29 -08004035 rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, 512);
Radim Krčmář83fedbb2016-11-09 19:07:06 +01004036 if (rc != X86EMUL_CONTINUE)
4037 return rc;
4038
4039 if (fx_state.mxcsr >> 16)
4040 return emulate_gp(ctxt, 0);
4041
4042 ctxt->ops->get_fpu(ctxt);
4043
4044 if (ctxt->mode < X86EMUL_MODE_PROT64)
4045 rc = fxrstor_fixup(ctxt, &fx_state);
4046
4047 if (rc == X86EMUL_CONTINUE)
4048 rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
4049
4050 ctxt->ops->put_fpu(ctxt);
4051
4052 return rc;
4053}
4054
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004055static bool valid_cr(int nr)
4056{
4057 switch (nr) {
4058 case 0:
4059 case 2 ... 4:
4060 case 8:
4061 return true;
4062 default:
4063 return false;
4064 }
4065}
4066
4067static int check_cr_read(struct x86_emulate_ctxt *ctxt)
4068{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004069 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004070 return emulate_ud(ctxt);
4071
4072 return X86EMUL_CONTINUE;
4073}
4074
4075static int check_cr_write(struct x86_emulate_ctxt *ctxt)
4076{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004077 u64 new_val = ctxt->src.val64;
4078 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004079 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004080
4081 static u64 cr_reserved_bits[] = {
4082 0xffffffff00000000ULL,
4083 0, 0, 0, /* CR3 checked later */
4084 CR4_RESERVED_BITS,
4085 0, 0, 0,
4086 CR8_RESERVED_BITS,
4087 };
4088
4089 if (!valid_cr(cr))
4090 return emulate_ud(ctxt);
4091
4092 if (new_val & cr_reserved_bits[cr])
4093 return emulate_gp(ctxt, 0);
4094
4095 switch (cr) {
4096 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004097 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004098 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
4099 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
4100 return emulate_gp(ctxt, 0);
4101
Avi Kivity717746e2011-04-20 13:37:53 +03004102 cr4 = ctxt->ops->get_cr(ctxt, 4);
4103 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004104
4105 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
4106 !(cr4 & X86_CR4_PAE))
4107 return emulate_gp(ctxt, 0);
4108
4109 break;
4110 }
4111 case 3: {
4112 u64 rsvd = 0;
4113
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03004114 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
4115 if (efer & EFER_LMA)
Nadav Amit9d88fca2014-11-02 11:54:52 +02004116 rsvd = CR3_L_MODE_RESERVED_BITS & ~CR3_PCID_INVD;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004117
4118 if (new_val & rsvd)
4119 return emulate_gp(ctxt, 0);
4120
4121 break;
4122 }
4123 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03004124 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004125
4126 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
4127 return emulate_gp(ctxt, 0);
4128
4129 break;
4130 }
4131 }
4132
4133 return X86EMUL_CONTINUE;
4134}
4135
Joerg Roedel3b88e412011-04-04 12:39:29 +02004136static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
4137{
4138 unsigned long dr7;
4139
Avi Kivity717746e2011-04-20 13:37:53 +03004140 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004141
4142 /* Check if DR7.Global_Enable is set */
4143 return dr7 & (1 << 13);
4144}
4145
4146static int check_dr_read(struct x86_emulate_ctxt *ctxt)
4147{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004148 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004149 u64 cr4;
4150
4151 if (dr > 7)
4152 return emulate_ud(ctxt);
4153
Avi Kivity717746e2011-04-20 13:37:53 +03004154 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004155 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
4156 return emulate_ud(ctxt);
4157
Nadav Amit6d2a0522014-11-02 11:54:43 +02004158 if (check_dr7_gd(ctxt)) {
4159 ulong dr6;
4160
4161 ctxt->ops->get_dr(ctxt, 6, &dr6);
4162 dr6 &= ~15;
4163 dr6 |= DR6_BD | DR6_RTM;
4164 ctxt->ops->set_dr(ctxt, 6, dr6);
Joerg Roedel3b88e412011-04-04 12:39:29 +02004165 return emulate_db(ctxt);
Nadav Amit6d2a0522014-11-02 11:54:43 +02004166 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004167
4168 return X86EMUL_CONTINUE;
4169}
4170
4171static int check_dr_write(struct x86_emulate_ctxt *ctxt)
4172{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004173 u64 new_val = ctxt->src.val64;
4174 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02004175
4176 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
4177 return emulate_gp(ctxt, 0);
4178
4179 return check_dr_read(ctxt);
4180}
4181
Joerg Roedel01de8b02011-04-04 12:39:31 +02004182static int check_svme(struct x86_emulate_ctxt *ctxt)
4183{
4184 u64 efer;
4185
Avi Kivity717746e2011-04-20 13:37:53 +03004186 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004187
4188 if (!(efer & EFER_SVME))
4189 return emulate_ud(ctxt);
4190
4191 return X86EMUL_CONTINUE;
4192}
4193
4194static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
4195{
Avi Kivitydd856ef2012-08-27 23:46:17 +03004196 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02004197
4198 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07004199 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004200 return emulate_gp(ctxt, 0);
4201
4202 return check_svme(ctxt);
4203}
4204
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004205static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
4206{
Avi Kivity717746e2011-04-20 13:37:53 +03004207 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004208
Avi Kivity717746e2011-04-20 13:37:53 +03004209 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004210 return emulate_ud(ctxt);
4211
4212 return X86EMUL_CONTINUE;
4213}
4214
Joerg Roedel80612522011-04-04 12:39:33 +02004215static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
4216{
Avi Kivity717746e2011-04-20 13:37:53 +03004217 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03004218 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02004219
Avi Kivity717746e2011-04-20 13:37:53 +03004220 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Nadav Amit67f4d422014-06-02 18:34:09 +03004221 ctxt->ops->check_pmc(ctxt, rcx))
Joerg Roedel80612522011-04-04 12:39:33 +02004222 return emulate_gp(ctxt, 0);
4223
4224 return X86EMUL_CONTINUE;
4225}
4226
Joerg Roedelf6511932011-04-04 12:39:35 +02004227static int check_perm_in(struct x86_emulate_ctxt *ctxt)
4228{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
4230 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004231 return emulate_gp(ctxt, 0);
4232
4233 return X86EMUL_CONTINUE;
4234}
4235
4236static int check_perm_out(struct x86_emulate_ctxt *ctxt)
4237{
Avi Kivity9dac77f2011-06-01 15:34:25 +03004238 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
4239 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02004240 return emulate_gp(ctxt, 0);
4241
4242 return X86EMUL_CONTINUE;
4243}
4244
Avi Kivity73fba5f2010-07-29 15:11:53 +03004245#define D(_y) { .flags = (_y) }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004246#define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
4247#define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
4248 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03004249#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02004250#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004251#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
4252#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Nadav Amit39f062f2014-11-26 15:47:18 +02004253#define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
Nadav Amit2276b512015-01-26 09:32:24 +02004254#define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
Gleb Natapov045a2822012-12-20 16:57:43 +02004255#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004256#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02004257#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02004258#define II(_f, _e, _i) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004259 { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02004260#define IIP(_f, _e, _i, _p) \
Paolo Bonzinid40a6892014-03-27 11:58:02 +01004261 { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
4262 .intercept = x86_intercept_##_i, .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02004263#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03004264
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004265#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02004266#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004267#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02004268#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004269#define I2bvIP(_f, _e, _i, _p) \
4270 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004271
Avi Kivityfb864fb2013-01-04 16:18:54 +02004272#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
4273 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
4274 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03004275
Nadav Amit0f54a322014-08-29 11:26:55 +03004276static const struct opcode group7_rm0[] = {
4277 N,
Jan Kiszkab34a8052015-03-09 20:27:43 +01004278 I(SrcNone | Priv | EmulateOnUD, em_hypercall),
Nadav Amit0f54a322014-08-29 11:26:55 +03004279 N, N, N, N, N, N,
4280};
4281
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004282static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004283 DI(SrcNone | Priv, monitor),
4284 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004285 N, N, N, N, N, N,
4286};
4287
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004288static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004289 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
Jan Kiszkab34a8052015-03-09 20:27:43 +01004290 II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004291 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
4292 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
4293 DIP(SrcNone | Prot | Priv, stgi, check_svme),
4294 DIP(SrcNone | Prot | Priv, clgi, check_svme),
4295 DIP(SrcNone | Prot | Priv, skinit, check_svme),
4296 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004297};
Avi Kivity6230f7f2010-08-26 18:34:55 +03004298
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004299static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004300 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004301 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004302 N, N, N, N, N, N,
4303};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004304
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004305static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004306 F(Lock, em_add),
4307 F(Lock | PageTable, em_or),
4308 F(Lock, em_adc),
4309 F(Lock, em_sbb),
4310 F(Lock | PageTable, em_and),
4311 F(Lock, em_sub),
4312 F(Lock, em_xor),
4313 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004314};
4315
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004316static const struct opcode group1A[] = {
Nadav Amitab708092014-12-25 02:52:21 +02004317 I(DstMem | SrcNone | Mov | Stack | IncSP, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004318};
4319
Avi Kivity007a3b52013-01-19 19:51:51 +02004320static const struct opcode group2[] = {
4321 F(DstMem | ModRM, em_rol),
4322 F(DstMem | ModRM, em_ror),
4323 F(DstMem | ModRM, em_rcl),
4324 F(DstMem | ModRM, em_rcr),
4325 F(DstMem | ModRM, em_shl),
4326 F(DstMem | ModRM, em_shr),
4327 F(DstMem | ModRM, em_shl),
4328 F(DstMem | ModRM, em_sar),
4329};
4330
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004331static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02004332 F(DstMem | SrcImm | NoWrite, em_test),
4333 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02004334 F(DstMem | SrcNone | Lock, em_not),
4335 F(DstMem | SrcNone | Lock, em_neg),
Avi Kivityb9fa4092013-02-09 11:31:48 +02004336 F(DstXacc | Src2Mem, em_mul_ex),
4337 F(DstXacc | Src2Mem, em_imul_ex),
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02004338 F(DstXacc | Src2Mem, em_div_ex),
4339 F(DstXacc | Src2Mem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004340};
4341
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004342static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004343 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
4344 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004345 N, N, N, N, N, N,
4346};
4347
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004348static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02004349 F(DstMem | SrcNone | Lock, em_inc),
4350 F(DstMem | SrcNone | Lock, em_dec),
Nadav Amit58b70752014-10-24 11:35:09 +03004351 I(SrcMem | NearBranch, em_call_near_abs),
Nadav Amitacac6f892015-05-03 20:22:57 +03004352 I(SrcMemFAddr | ImplicitOps, em_call_far),
Nadav Amit58b70752014-10-24 11:35:09 +03004353 I(SrcMem | NearBranch, em_jmp_abs),
Nadav Amitf7784042014-09-18 22:39:41 +03004354 I(SrcMemFAddr | ImplicitOps, em_jmp_far),
4355 I(SrcMem | Stack, em_push), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004356};
4357
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004358static const struct opcode group6[] = {
Nadav Amit63ea0a42015-01-08 11:59:03 +01004359 DI(Prot | DstMem, sldt),
4360 DI(Prot | DstMem, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03004361 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03004362 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004363 N, N, N, N,
4364};
4365
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004366static const struct group_dual group7 = { {
Nadav Amit606b1c32014-06-02 18:34:06 +03004367 II(Mov | DstMem, em_sgdt, sgdt),
4368 II(Mov | DstMem, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004369 II(SrcMem | Priv, em_lgdt, lgdt),
4370 II(SrcMem | Priv, em_lidt, lidt),
4371 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4372 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4373 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004374}, {
Nadav Amit0f54a322014-08-29 11:26:55 +03004375 EXT(0, group7_rm0),
Avi Kivity5ef39c72011-04-21 12:21:50 +03004376 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004377 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004378 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
4379 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
4380 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004381} };
4382
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004383static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004384 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02004385 F(DstMem | SrcImmByte | NoWrite, em_bt),
4386 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
4387 F(DstMem | SrcImmByte | Lock, em_btr),
4388 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004389};
4390
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004391static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004392 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004393}, {
4394 N, N, N, N, N, N, N, N,
4395} };
4396
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004397static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004398 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004399 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004400};
4401
Nadav Amit13e457e2014-10-13 13:04:13 +03004402static const struct gprefix pfx_0f_ae_7 = {
Nadav Amit3f6f1482014-10-13 13:04:14 +03004403 I(SrcMem | ByteOp, em_clflush), N, N, N,
Nadav Amit13e457e2014-10-13 13:04:13 +03004404};
4405
4406static const struct group_dual group15 = { {
Radim Krčmář83fedbb2016-11-09 19:07:06 +01004407 I(ModRM | Aligned16, em_fxsave),
4408 I(ModRM | Aligned16, em_fxrstor),
4409 N, N, N, N, N, GP(0, &pfx_0f_ae_7),
Nadav Amit13e457e2014-10-13 13:04:13 +03004410}, {
4411 N, N, N, N, N, N, N, N,
4412} };
4413
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004414static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03004415 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02004416};
4417
Nadav Amit39f062f2014-11-26 15:47:18 +02004418static const struct instr_dual instr_dual_0f_2b = {
4419 I(0, em_mov), N
4420};
4421
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004422static const struct gprefix pfx_0f_2b = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004423 ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004424};
4425
Igor Mammedov27ce8252014-03-15 21:01:59 +01004426static const struct gprefix pfx_0f_28_0f_29 = {
Igor Mammedov6fec27d2014-03-15 21:02:00 +01004427 I(Aligned, em_mov), I(Aligned, em_mov), N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004428};
4429
Alex Williamson0a370272014-07-11 11:56:31 -06004430static const struct gprefix pfx_0f_e7 = {
4431 N, I(Sse, em_mov), N, N,
4432};
4433
Gleb Natapov045a2822012-12-20 16:57:43 +02004434static const struct escape escape_d9 = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004435 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004436}, {
4437 /* 0xC0 - 0xC7 */
4438 N, N, N, N, N, N, N, N,
4439 /* 0xC8 - 0xCF */
4440 N, N, N, N, N, N, N, N,
4441 /* 0xD0 - 0xC7 */
4442 N, N, N, N, N, N, N, N,
4443 /* 0xD8 - 0xDF */
4444 N, N, N, N, N, N, N, N,
4445 /* 0xE0 - 0xE7 */
4446 N, N, N, N, N, N, N, N,
4447 /* 0xE8 - 0xEF */
4448 N, N, N, N, N, N, N, N,
4449 /* 0xF0 - 0xF7 */
4450 N, N, N, N, N, N, N, N,
4451 /* 0xF8 - 0xFF */
4452 N, N, N, N, N, N, N, N,
4453} };
4454
4455static const struct escape escape_db = { {
4456 N, N, N, N, N, N, N, N,
4457}, {
4458 /* 0xC0 - 0xC7 */
4459 N, N, N, N, N, N, N, N,
4460 /* 0xC8 - 0xCF */
4461 N, N, N, N, N, N, N, N,
4462 /* 0xD0 - 0xC7 */
4463 N, N, N, N, N, N, N, N,
4464 /* 0xD8 - 0xDF */
4465 N, N, N, N, N, N, N, N,
4466 /* 0xE0 - 0xE7 */
4467 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
4468 /* 0xE8 - 0xEF */
4469 N, N, N, N, N, N, N, N,
4470 /* 0xF0 - 0xF7 */
4471 N, N, N, N, N, N, N, N,
4472 /* 0xF8 - 0xFF */
4473 N, N, N, N, N, N, N, N,
4474} };
4475
4476static const struct escape escape_dd = { {
Nadav Amit16bebef2014-12-25 02:52:18 +02004477 N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
Gleb Natapov045a2822012-12-20 16:57:43 +02004478}, {
4479 /* 0xC0 - 0xC7 */
4480 N, N, N, N, N, N, N, N,
4481 /* 0xC8 - 0xCF */
4482 N, N, N, N, N, N, N, N,
4483 /* 0xD0 - 0xC7 */
4484 N, N, N, N, N, N, N, N,
4485 /* 0xD8 - 0xDF */
4486 N, N, N, N, N, N, N, N,
4487 /* 0xE0 - 0xE7 */
4488 N, N, N, N, N, N, N, N,
4489 /* 0xE8 - 0xEF */
4490 N, N, N, N, N, N, N, N,
4491 /* 0xF0 - 0xF7 */
4492 N, N, N, N, N, N, N, N,
4493 /* 0xF8 - 0xFF */
4494 N, N, N, N, N, N, N, N,
4495} };
4496
Nadav Amit39f062f2014-11-26 15:47:18 +02004497static const struct instr_dual instr_dual_0f_c3 = {
4498 I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
4499};
4500
Nadav Amit2276b512015-01-26 09:32:24 +02004501static const struct mode_dual mode_dual_63 = {
4502 N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
4503};
4504
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004505static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004506 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004507 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004508 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
4509 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004510 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004511 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004512 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
4513 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004514 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004515 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004516 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
4517 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004518 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004519 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03004520 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
4521 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004522 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004523 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004524 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004525 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004526 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004527 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004528 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004529 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004530 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02004531 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004532 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03004533 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004534 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09004535 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004536 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09004537 I(ImplicitOps | Stack | No64, em_pusha),
4538 I(ImplicitOps | Stack | No64, em_popa),
Nadav Amit2276b512015-01-26 09:32:24 +02004539 N, MD(ModRM, &mode_dual_63),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004540 N, N, N, N,
4541 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03004542 I(SrcImm | Mov | Stack, em_push),
4543 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03004544 I(SrcImmByte | Mov | Stack, em_push),
4545 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004546 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09004547 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03004548 /* 0x70 - 0x7F */
Nadav Amit58b70752014-10-24 11:35:09 +03004549 X16(D(SrcImmByte | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004550 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09004551 G(ByteOp | DstMem | SrcImm, group1),
4552 G(DstMem | SrcImm, group1),
4553 G(ByteOp | DstMem | SrcImm | No64, group1),
4554 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02004555 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004556 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004557 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004558 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004559 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004560 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09004561 D(ModRM | SrcMem | NoAccess | DstReg),
4562 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
4563 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004564 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004565 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004566 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03004567 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08004568 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09004569 II(ImplicitOps | Stack, em_pushf, pushf),
Paolo Bonzini98f73632013-10-31 11:19:42 +01004570 II(ImplicitOps | Stack, em_popf, popf),
4571 I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004572 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004573 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004574 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004575 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004576 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004577 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02004578 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004579 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
4580 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Nadav Amit5aca3722014-11-02 11:54:50 +02004581 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004582 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03004583 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004584 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02004585 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004586 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004587 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Nadav Amit58b70752014-10-24 11:35:09 +03004588 I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
4589 I(ImplicitOps | NearBranch, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03004590 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
4591 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03004592 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004593 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03004594 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
Nadav Amit16794aa2015-01-26 09:32:22 +02004595 I(ImplicitOps | SrcImmU16, em_ret_far_imm),
4596 I(ImplicitOps, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02004597 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004598 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004599 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02004600 G(Src2One | ByteOp, group2), G(Src2One, group2),
4601 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02004602 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02004603 I(DstAcc | SrcImmUByte | No64, em_aad),
4604 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004605 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004606 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02004607 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004608 /* 0xE0 - 0xE7 */
Nadav Amit58b70752014-10-24 11:35:09 +03004609 X3(I(SrcImmByte | NearBranch, em_loop)),
4610 I(SrcImmByte | NearBranch, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004611 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4612 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004613 /* 0xE8 - 0xEF */
Nadav Amit58b70752014-10-24 11:35:09 +03004614 I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
4615 I(SrcImmFAddr | No64, em_jmp_far),
4616 D(SrcImmByte | ImplicitOps | NearBranch),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004617 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4618 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004619 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004620 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004621 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4622 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004623 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004624 D(ImplicitOps), D(ImplicitOps),
4625 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004626 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4627};
4628
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004629static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004630 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004631 G(0, group6), GD(0, &group7), N, N,
Borislav Petkovb51e9742013-09-22 16:44:52 +02004632 N, I(ImplicitOps | EmulateOnUD, em_syscall),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004633 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004634 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004635 N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004636 /* 0x10 - 0x1F */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02004637 N, N, N, N, N, N, N, N,
Nadav Amit3f6f1482014-10-13 13:04:14 +03004638 D(ImplicitOps | ModRM | SrcMem | NoAccess),
4639 N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004640 /* 0x20 - 0x2F */
Nadav Amit9b88ae92014-05-25 23:05:21 +03004641 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
4642 DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
4643 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
4644 check_cr_write),
4645 IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
4646 check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004647 N, N, N, N,
Igor Mammedov27ce8252014-03-15 21:01:59 +01004648 GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
4649 GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
Paolo Bonzinid5b77062014-07-14 12:54:48 +02004650 N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
Avi Kivity3e114eb2012-04-09 18:40:01 +03004651 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004652 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004653 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004654 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004655 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004656 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Borislav Petkovb51e9742013-09-22 16:44:52 +02004657 I(ImplicitOps | EmulateOnUD, em_sysenter),
4658 I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004659 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004660 N, N, N, N, N, N, N, N,
4661 /* 0x40 - 0x4F */
Nadav Amit140bad82014-06-15 16:13:00 +03004662 X16(D(DstReg | SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004663 /* 0x50 - 0x5F */
4664 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4665 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004666 N, N, N, N,
4667 N, N, N, N,
4668 N, N, N, N,
4669 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004670 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004671 N, N, N, N,
4672 N, N, N, N,
4673 N, N, N, N,
4674 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004675 /* 0x80 - 0x8F */
Nadav Amit58b70752014-10-24 11:35:09 +03004676 X16(D(SrcImm | NearBranch)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004677 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004678 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004679 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004680 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004681 II(ImplicitOps, em_cpuid, cpuid),
4682 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004683 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4684 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004685 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004686 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Paolo Bonzini89651a32015-11-03 13:43:05 +01004687 II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004688 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004689 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4690 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Nadav Amit13e457e2014-10-13 13:04:13 +03004691 GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004692 /* 0xB0 - 0xB7 */
Nadav Amit2fcf5c82015-01-26 09:32:21 +02004693 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004694 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004695 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004696 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4697 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004698 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004699 /* 0xB8 - 0xBF */
4700 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004701 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004702 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Nadav Amit900efe22015-03-30 15:39:21 +03004703 I(DstReg | SrcMem | ModRM, em_bsf_c),
4704 I(DstReg | SrcMem | ModRM, em_bsr_c),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004705 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004706 /* 0xC0 - 0xC7 */
Avi Kivitye47a5f52013-02-09 11:31:51 +02004707 F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
Nadav Amit39f062f2014-11-26 15:47:18 +02004708 N, ID(0, &instr_dual_0f_c3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004709 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004710 /* 0xC8 - 0xCF */
4711 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004712 /* 0xD0 - 0xDF */
4713 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4714 /* 0xE0 - 0xEF */
Alex Williamson0a370272014-07-11 11:56:31 -06004715 N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
4716 N, N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004717 /* 0xF0 - 0xFF */
4718 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4719};
4720
Nadav Amit39f062f2014-11-26 15:47:18 +02004721static const struct instr_dual instr_dual_0f_38_f0 = {
4722 I(DstReg | SrcMem | Mov, em_movbe), N
4723};
4724
4725static const struct instr_dual instr_dual_0f_38_f1 = {
4726 I(DstMem | SrcReg | Mov, em_movbe), N
4727};
4728
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004729static const struct gprefix three_byte_0f_38_f0 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004730 ID(0, &instr_dual_0f_38_f0), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004731};
4732
4733static const struct gprefix three_byte_0f_38_f1 = {
Nadav Amit39f062f2014-11-26 15:47:18 +02004734 ID(0, &instr_dual_0f_38_f1), N, N, N
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004735};
4736
4737/*
4738 * Insns below are selected by the prefix which indexed by the third opcode
4739 * byte.
4740 */
4741static const struct opcode opcode_map_0f_38[256] = {
4742 /* 0x00 - 0x7f */
4743 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004744 /* 0x80 - 0xef */
4745 X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
4746 /* 0xf0 - 0xf1 */
Nadav Amit53bb4f72014-12-07 11:49:42 +02004747 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
4748 GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Borislav Petkov84cffe42013-10-29 12:54:56 +01004749 /* 0xf2 - 0xff */
4750 N, N, X4(N), X8(N)
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01004751};
4752
Avi Kivity73fba5f2010-07-29 15:11:53 +03004753#undef D
4754#undef N
4755#undef G
4756#undef GD
4757#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004758#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004759#undef EXT
Nadav Amit2276b512015-01-26 09:32:24 +02004760#undef MD
Nadav Amit2b42fce2015-01-26 09:32:25 +02004761#undef ID
Avi Kivity73fba5f2010-07-29 15:11:53 +03004762
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004763#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004764#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004765#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004766#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004767#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004768
Avi Kivity9dac77f2011-06-01 15:34:25 +03004769static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004770{
4771 unsigned size;
4772
Avi Kivity9dac77f2011-06-01 15:34:25 +03004773 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004774 if (size == 8)
4775 size = 4;
4776 return size;
4777}
4778
4779static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4780 unsigned size, bool sign_extension)
4781{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004782 int rc = X86EMUL_CONTINUE;
4783
4784 op->type = OP_IMM;
4785 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004787 /* NB. Immediates are sign-extended as necessary. */
4788 switch (op->bytes) {
4789 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004790 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004791 break;
4792 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004793 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004794 break;
4795 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004796 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004797 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004798 case 8:
4799 op->val = insn_fetch(s64, ctxt);
4800 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004801 }
4802 if (!sign_extension) {
4803 switch (op->bytes) {
4804 case 1:
4805 op->val &= 0xff;
4806 break;
4807 case 2:
4808 op->val &= 0xffff;
4809 break;
4810 case 4:
4811 op->val &= 0xffffffff;
4812 break;
4813 }
4814 }
4815done:
4816 return rc;
4817}
4818
Avi Kivitya9945542011-09-13 10:45:41 +03004819static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4820 unsigned d)
4821{
4822 int rc = X86EMUL_CONTINUE;
4823
4824 switch (d) {
4825 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004826 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004827 break;
4828 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004829 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004830 break;
4831 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004832 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004833 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004834 *op = ctxt->memop;
4835 ctxt->memopp = op;
Paolo Bonzini96888972014-04-01 14:54:19 +02004836 if (ctxt->d & BitOp)
Avi Kivitya9945542011-09-13 10:45:41 +03004837 fetch_bit_operand(ctxt);
4838 op->orig_val = op->val;
4839 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004840 case OpMem64:
Nadav Amitaaa05f22014-06-02 18:34:10 +03004841 ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
Avi Kivity41ddf972011-09-13 10:45:48 +03004842 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004843 case OpAcc:
4844 op->type = OP_REG;
4845 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004846 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004847 fetch_register_operand(op);
4848 op->orig_val = op->val;
4849 break;
Avi Kivity820207c2013-02-09 11:31:45 +02004850 case OpAccLo:
4851 op->type = OP_REG;
4852 op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
4853 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
4854 fetch_register_operand(op);
4855 op->orig_val = op->val;
4856 break;
4857 case OpAccHi:
4858 if (ctxt->d & ByteOp) {
4859 op->type = OP_NONE;
4860 break;
4861 }
4862 op->type = OP_REG;
4863 op->bytes = ctxt->op_bytes;
4864 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
4865 fetch_register_operand(op);
4866 op->orig_val = op->val;
4867 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004868 case OpDI:
4869 op->type = OP_MEM;
4870 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4871 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004872 register_address(ctxt, VCPU_REGS_RDI);
Avi Kivitya9945542011-09-13 10:45:41 +03004873 op->addr.mem.seg = VCPU_SREG_ES;
4874 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004875 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004876 break;
4877 case OpDX:
4878 op->type = OP_REG;
4879 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004880 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004881 fetch_register_operand(op);
4882 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004883 case OpCL:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004884 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004885 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004886 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004887 break;
4888 case OpImmByte:
4889 rc = decode_imm(ctxt, op, 1, true);
4890 break;
4891 case OpOne:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004892 op->type = OP_IMM;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004893 op->bytes = 1;
4894 op->val = 1;
4895 break;
4896 case OpImm:
4897 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4898 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004899 case OpImm64:
4900 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4901 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004902 case OpMem8:
4903 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004904 if (ctxt->memop.type == OP_REG) {
Gleb Natapovaa9ac1a2013-11-04 15:52:41 +02004905 ctxt->memop.addr.reg = decode_register(ctxt,
4906 ctxt->modrm_rm, true);
Gleb Natapov660696d2013-04-24 13:38:36 +03004907 fetch_register_operand(&ctxt->memop);
4908 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004909 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004910 case OpMem16:
4911 ctxt->memop.bytes = 2;
4912 goto mem_common;
4913 case OpMem32:
4914 ctxt->memop.bytes = 4;
4915 goto mem_common;
4916 case OpImmU16:
4917 rc = decode_imm(ctxt, op, 2, false);
4918 break;
4919 case OpImmU:
4920 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4921 break;
4922 case OpSI:
4923 op->type = OP_MEM;
4924 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4925 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004926 register_address(ctxt, VCPU_REGS_RSI);
Bandan Das573e80f2014-04-16 12:46:13 -04004927 op->addr.mem.seg = ctxt->seg_override;
Avi Kivity0fe59122011-09-13 10:45:47 +03004928 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004929 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004930 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004931 case OpXLat:
4932 op->type = OP_MEM;
4933 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4934 op->addr.mem.ea =
Paolo Bonzini01485a22014-11-19 18:25:08 +01004935 address_mask(ctxt,
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004936 reg_read(ctxt, VCPU_REGS_RBX) +
4937 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
Bandan Das573e80f2014-04-16 12:46:13 -04004938 op->addr.mem.seg = ctxt->seg_override;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004939 op->val = 0;
4940 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004941 case OpImmFAddr:
4942 op->type = OP_IMM;
4943 op->addr.mem.ea = ctxt->_eip;
4944 op->bytes = ctxt->op_bytes + 2;
4945 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4946 break;
4947 case OpMemFAddr:
4948 ctxt->memop.bytes = ctxt->op_bytes + 2;
4949 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004950 case OpES:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004951 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004952 op->val = VCPU_SREG_ES;
4953 break;
4954 case OpCS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004955 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004956 op->val = VCPU_SREG_CS;
4957 break;
4958 case OpSS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004959 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004960 op->val = VCPU_SREG_SS;
4961 break;
4962 case OpDS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004963 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004964 op->val = VCPU_SREG_DS;
4965 break;
4966 case OpFS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004967 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004968 op->val = VCPU_SREG_FS;
4969 break;
4970 case OpGS:
Nadav Amitd29b9d72014-11-02 11:54:47 +02004971 op->type = OP_IMM;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004972 op->val = VCPU_SREG_GS;
4973 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004974 case OpImplicit:
4975 /* Special instructions do their own operand decoding. */
4976 default:
4977 op->type = OP_NONE; /* Disable writeback. */
4978 break;
4979 }
4980
4981done:
4982 return rc;
4983}
4984
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004985int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004986{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004987 int rc = X86EMUL_CONTINUE;
4988 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004989 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004990 bool op_prefix = false;
Bandan Das573e80f2014-04-16 12:46:13 -04004991 bool has_seg_override = false;
Avi Kivity46561642011-04-24 14:09:59 +03004992 struct opcode opcode;
Wanpeng Liec73d162017-11-05 16:54:47 -08004993 u16 dummy;
4994 struct desc_struct desc;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004995
Avi Kivityf09ed832011-09-13 10:45:40 +03004996 ctxt->memop.type = OP_NONE;
4997 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004998 ctxt->_eip = ctxt->eip;
Paolo Bonzini17052f12014-05-06 16:33:01 +02004999 ctxt->fetch.ptr = ctxt->fetch.data;
5000 ctxt->fetch.end = ctxt->fetch.data + insn_len;
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005001 ctxt->opcode_len = 1;
Andre Przywaradc25e892010-12-21 11:12:07 +01005002 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005003 memcpy(ctxt->fetch.data, insn, insn_len);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005004 else {
Paolo Bonzini9506d572014-05-06 13:05:25 +02005005 rc = __do_insn_fetch_bytes(ctxt, 1);
Paolo Bonzini285ca9e2014-05-06 12:24:32 +02005006 if (rc != X86EMUL_CONTINUE)
5007 return rc;
5008 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005009
5010 switch (mode) {
5011 case X86EMUL_MODE_REAL:
5012 case X86EMUL_MODE_VM86:
Wanpeng Liec73d162017-11-05 16:54:47 -08005013 def_op_bytes = def_ad_bytes = 2;
5014 ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
5015 if (desc.d)
5016 def_op_bytes = def_ad_bytes = 4;
5017 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005018 case X86EMUL_MODE_PROT16:
5019 def_op_bytes = def_ad_bytes = 2;
5020 break;
5021 case X86EMUL_MODE_PROT32:
5022 def_op_bytes = def_ad_bytes = 4;
5023 break;
5024#ifdef CONFIG_X86_64
5025 case X86EMUL_MODE_PROT64:
5026 def_op_bytes = 4;
5027 def_ad_bytes = 8;
5028 break;
5029#endif
5030 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005031 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005032 }
5033
Avi Kivity9dac77f2011-06-01 15:34:25 +03005034 ctxt->op_bytes = def_op_bytes;
5035 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005036
5037 /* Legacy prefixes. */
5038 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005039 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005040 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005041 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005042 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005043 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005044 break;
5045 case 0x67: /* address-size override */
5046 if (mode == X86EMUL_MODE_PROT64)
5047 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005048 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005049 else
5050 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005051 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005052 break;
5053 case 0x26: /* ES override */
5054 case 0x2e: /* CS override */
5055 case 0x36: /* SS override */
5056 case 0x3e: /* DS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005057 has_seg_override = true;
5058 ctxt->seg_override = (ctxt->b >> 3) & 3;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005059 break;
5060 case 0x64: /* FS override */
5061 case 0x65: /* GS override */
Bandan Das573e80f2014-04-16 12:46:13 -04005062 has_seg_override = true;
5063 ctxt->seg_override = ctxt->b & 7;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005064 break;
5065 case 0x40 ... 0x4f: /* REX */
5066 if (mode != X86EMUL_MODE_PROT64)
5067 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005068 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005069 continue;
5070 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005071 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005072 break;
5073 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005074 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005075 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005076 break;
5077 default:
5078 goto done_prefixes;
5079 }
5080
5081 /* Any legacy prefix after a REX prefix nullifies its effect. */
5082
Avi Kivity9dac77f2011-06-01 15:34:25 +03005083 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005084 }
5085
5086done_prefixes:
5087
5088 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005089 if (ctxt->rex_prefix & 8)
5090 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005091
5092 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005093 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08005094 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005095 if (ctxt->b == 0x0f) {
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005096 ctxt->opcode_len = 2;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09005097 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005098 opcode = twobyte_table[ctxt->b];
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005099
5100 /* 0F_38 opcode map */
5101 if (ctxt->b == 0x38) {
5102 ctxt->opcode_len = 3;
5103 ctxt->b = insn_fetch(u8, ctxt);
5104 opcode = opcode_map_0f_38[ctxt->b];
5105 }
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005106 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005107 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005108
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09005109 if (ctxt->d & ModRM)
5110 ctxt->modrm = insn_fetch(u8, ctxt);
5111
Nadav Amit7fe864d2014-06-02 18:34:03 +03005112 /* vex-prefix instructions are not implemented */
5113 if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
Nadav Amitd14cb5d2014-11-02 11:54:58 +02005114 (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
Nadav Amit7fe864d2014-06-02 18:34:03 +03005115 ctxt->d = NotImpl;
5116 }
5117
Avi Kivity9dac77f2011-06-01 15:34:25 +03005118 while (ctxt->d & GroupMask) {
5119 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03005120 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005121 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03005122 opcode = opcode.u.group[goffset];
5123 break;
5124 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005125 goffset = (ctxt->modrm >> 3) & 7;
5126 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03005127 opcode = opcode.u.gdual->mod3[goffset];
5128 else
5129 opcode = opcode.u.gdual->mod012[goffset];
5130 break;
5131 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005132 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02005133 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03005134 break;
5135 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005136 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005137 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005138 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03005139 switch (simd_prefix) {
5140 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
5141 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
5142 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
5143 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
5144 }
5145 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02005146 case Escape:
5147 if (ctxt->modrm > 0xbf)
5148 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
5149 else
5150 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
5151 break;
Nadav Amit39f062f2014-11-26 15:47:18 +02005152 case InstrDual:
5153 if ((ctxt->modrm >> 6) == 3)
5154 opcode = opcode.u.idual->mod3;
5155 else
5156 opcode = opcode.u.idual->mod012;
5157 break;
Nadav Amit2276b512015-01-26 09:32:24 +02005158 case ModeDual:
5159 if (ctxt->mode == X86EMUL_MODE_PROT64)
5160 opcode = opcode.u.mdual->mode64;
5161 else
5162 opcode = opcode.u.mdual->mode32;
5163 break;
Avi Kivity46561642011-04-24 14:09:59 +03005164 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005165 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005166 }
Avi Kivity46561642011-04-24 14:09:59 +03005167
Avi Kivityb1ea50b2011-09-13 10:45:42 +03005168 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005169 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02005170 }
5171
Paolo Bonzinie24186e2014-03-27 12:00:57 +01005172 /* Unrecognised? */
5173 if (ctxt->d == 0)
5174 return EMULATION_FAILED;
5175
Avi Kivity9dac77f2011-06-01 15:34:25 +03005176 ctxt->execute = opcode.u.execute;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005177
Nadav Amit3a6095a2014-08-13 16:50:13 +03005178 if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
5179 return EMULATION_FAILED;
5180
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005181 if (unlikely(ctxt->d &
Nadav Amited9aad22014-11-02 11:55:00 +02005182 (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
5183 No16))) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005184 /*
5185 * These are copied unconditionally here, and checked unconditionally
5186 * in x86_emulate_insn.
5187 */
5188 ctxt->check_perm = opcode.check_perm;
5189 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005190
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005191 if (ctxt->d & NotImpl)
5192 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02005193
Nadav Amit58b70752014-10-24 11:35:09 +03005194 if (mode == X86EMUL_MODE_PROT64) {
5195 if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
5196 ctxt->op_bytes = 8;
5197 else if (ctxt->d & NearBranch)
5198 ctxt->op_bytes = 8;
5199 }
Avi Kivity7f9b4b72010-08-01 14:46:54 +03005200
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005201 if (ctxt->d & Op3264) {
5202 if (mode == X86EMUL_MODE_PROT64)
5203 ctxt->op_bytes = 8;
5204 else
5205 ctxt->op_bytes = 4;
5206 }
5207
Nadav Amited9aad22014-11-02 11:55:00 +02005208 if ((ctxt->d & No16) && ctxt->op_bytes == 2)
5209 ctxt->op_bytes = 4;
5210
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005211 if (ctxt->d & Sse)
5212 ctxt->op_bytes = 16;
5213 else if (ctxt->d & Mmx)
5214 ctxt->op_bytes = 8;
5215 }
Avi Kivity12537912011-03-29 11:41:27 +02005216
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005217 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005218 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03005219 rc = decode_modrm(ctxt, &ctxt->memop);
Bandan Das573e80f2014-04-16 12:46:13 -04005220 if (!has_seg_override) {
5221 has_seg_override = true;
5222 ctxt->seg_override = ctxt->modrm_seg;
5223 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005224 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03005225 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005226 if (rc != X86EMUL_CONTINUE)
5227 goto done;
5228
Bandan Das573e80f2014-04-16 12:46:13 -04005229 if (!has_seg_override)
5230 ctxt->seg_override = VCPU_SREG_DS;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005231
Bandan Das573e80f2014-04-16 12:46:13 -04005232 ctxt->memop.addr.mem.seg = ctxt->seg_override;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005233
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005234 /*
5235 * Decode and fetch the source operand: register, memory
5236 * or immediate.
5237 */
Avi Kivity0fe59122011-09-13 10:45:47 +03005238 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005239 if (rc != X86EMUL_CONTINUE)
5240 goto done;
5241
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005242 /*
5243 * Decode and fetch the second source operand: register, memory
5244 * or immediate.
5245 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03005246 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03005247 if (rc != X86EMUL_CONTINUE)
5248 goto done;
5249
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005250 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03005251 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005252
Owen Hofmannd9092f52016-10-27 11:25:52 -07005253 if (ctxt->rip_relative && likely(ctxt->memopp))
Nadav Amit1c1c35a2014-11-19 17:43:09 +02005254 ctxt->memopp->addr.mem.ea = address_mask(ctxt,
5255 ctxt->memopp->addr.mem.ea + ctxt->_eip);
Avi Kivitycb16c342011-06-19 19:21:11 +03005256
Paolo Bonzinia430c912014-10-23 14:54:14 +02005257done:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09005258 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03005259}
5260
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08005261bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
5262{
5263 return ctxt->d & PageTable;
5264}
5265
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005266static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
5267{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005268 /* The second termination condition only applies for REPE
5269 * and REPNE. Test if the repeat string operation prefix is
5270 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
5271 * corresponding termination condition according to:
5272 * - if REPE/REPZ and ZF = 0 then done
5273 * - if REPNE/REPNZ and ZF = 1 then done
5274 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005275 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
5276 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
5277 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005278 ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03005279 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Nadav Amit0efb0442015-03-29 16:33:03 +03005280 ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005281 return true;
5282
5283 return false;
5284}
5285
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005286static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
5287{
Radim Krčmářaae8f342016-11-08 20:54:18 +01005288 int rc;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005289
5290 ctxt->ops->get_fpu(ctxt);
Radim Krčmářaae8f342016-11-08 20:54:18 +01005291 rc = asm_safe("fwait");
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005292 ctxt->ops->put_fpu(ctxt);
5293
Radim Krčmářaae8f342016-11-08 20:54:18 +01005294 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005295 return emulate_exception(ctxt, MF_VECTOR, 0, false);
5296
5297 return X86EMUL_CONTINUE;
5298}
5299
5300static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
5301 struct operand *op)
5302{
5303 if (op->type == OP_MM)
5304 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
5305}
5306
Avi Kivitye28bbd42013-01-04 16:18:48 +02005307static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
5308{
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005309 register void *__sp asm(_ASM_SP);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005310 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005311
Avi Kivityb9fa4092013-02-09 11:31:48 +02005312 if (!(ctxt->d & ByteOp))
5313 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005314
Avi Kivitye28bbd42013-01-04 16:18:48 +02005315 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005316 : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005317 [fastop]"+S"(fop), "+r"(__sp)
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005318 : "c"(ctxt->src2.val));
Josh Poimboeuf4548f632016-03-09 12:59:50 -06005319
Avi Kivitye28bbd42013-01-04 16:18:48 +02005320 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
Avi Kivityb8c0b6a2013-02-09 11:31:49 +02005321 if (!fop) /* exception is returned in fop variable */
5322 return emulate_de(ctxt);
Avi Kivitye28bbd42013-01-04 16:18:48 +02005323 return X86EMUL_CONTINUE;
5324}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005325
Bandan Das14985072014-04-16 12:46:09 -04005326void init_decode_cache(struct x86_emulate_ctxt *ctxt)
5327{
Bandan Das573e80f2014-04-16 12:46:13 -04005328 memset(&ctxt->rip_relative, 0,
5329 (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
Bandan Das14985072014-04-16 12:46:09 -04005330
Bandan Das14985072014-04-16 12:46:09 -04005331 ctxt->io_read.pos = 0;
5332 ctxt->io_read.end = 0;
Bandan Das14985072014-04-16 12:46:09 -04005333 ctxt->mem_read.end = 0;
5334}
5335
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09005336int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005337{
Mathias Krause0225fb52012-08-30 01:30:16 +02005338 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09005339 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005340 int saved_dst_type = ctxt->dst.type;
Ladi Prosek1eeb7942017-04-25 16:42:44 +02005341 unsigned emul_flags;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005342
Avi Kivity9dac77f2011-06-01 15:34:25 +03005343 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04005344
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005345 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005346 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005347 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02005348 goto done;
5349 }
5350
Avi Kivity9dac77f2011-06-01 15:34:25 +03005351 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02005352 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03005353 goto done;
5354 }
5355
Ladi Prosek1eeb7942017-04-25 16:42:44 +02005356 emul_flags = ctxt->ops->get_hflags(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005357 if (unlikely(ctxt->d &
5358 (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
5359 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
5360 (ctxt->d & Undefined)) {
5361 rc = emulate_ud(ctxt);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005362 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005363 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03005364
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005365 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
5366 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
5367 rc = emulate_ud(ctxt);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005368 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005369 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005370
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005371 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
5372 rc = emulate_nm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02005373 goto done;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005374 }
Joerg Roedeld09beab2011-04-04 12:39:25 +02005375
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005376 if (ctxt->d & Mmx) {
5377 rc = flush_pending_x87_faults(ctxt);
5378 if (rc != X86EMUL_CONTINUE)
5379 goto done;
5380 /*
5381 * Now that we know the fpu is exception safe, we can fetch
5382 * operands from it.
5383 */
5384 fetch_possible_mmx_operand(ctxt, &ctxt->src);
5385 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
5386 if (!(ctxt->d & Mov))
5387 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
5388 }
Avi Kivityc4f035c2011-04-04 12:39:22 +02005389
Ladi Prosek1eeb7942017-04-25 16:42:44 +02005390 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005391 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5392 X86_ICPT_PRE_EXCEPT);
5393 if (rc != X86EMUL_CONTINUE)
5394 goto done;
5395 }
5396
Nadav Amit64a38292014-12-10 11:19:04 +02005397 /* Instruction can only be executed in protected mode */
5398 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
5399 rc = emulate_ud(ctxt);
5400 goto done;
5401 }
5402
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005403 /* Privileged instruction can be executed only in CPL=0 */
5404 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Nadav Amit68efa762014-06-18 17:19:35 +03005405 if (ctxt->d & PrivUD)
5406 rc = emulate_ud(ctxt);
5407 else
5408 rc = emulate_gp(ctxt, 0);
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005409 goto done;
5410 }
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005411
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005412 /* Do instruction specific permission checks */
Bandan Das685bbf42014-04-16 12:46:10 -04005413 if (ctxt->d & CheckPerm) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005414 rc = ctxt->check_perm(ctxt);
5415 if (rc != X86EMUL_CONTINUE)
5416 goto done;
5417 }
5418
Ladi Prosek1eeb7942017-04-25 16:42:44 +02005419 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005420 rc = emulator_check_intercept(ctxt, ctxt->intercept,
5421 X86_ICPT_POST_EXCEPT);
5422 if (rc != X86EMUL_CONTINUE)
5423 goto done;
5424 }
5425
5426 if (ctxt->rep_prefix && (ctxt->d & String)) {
5427 /* All REP prefixes have the same first termination condition */
5428 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Nadav Amit428e3d02015-04-28 13:06:01 +03005429 string_registers_quirk(ctxt);
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005430 ctxt->eip = ctxt->_eip;
Nadav Amit0efb0442015-03-29 16:33:03 +03005431 ctxt->eflags &= ~X86_EFLAGS_RF;
Paolo Bonzinid40a6892014-03-27 11:58:02 +01005432 goto done;
5433 }
5434 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02005435 }
5436
Avi Kivity9dac77f2011-06-01 15:34:25 +03005437 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
5438 rc = segmented_read(ctxt, ctxt->src.addr.mem,
5439 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09005440 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005441 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005442 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005443 }
5444
Avi Kivity9dac77f2011-06-01 15:34:25 +03005445 if (ctxt->src2.type == OP_MEM) {
5446 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
5447 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02005448 if (rc != X86EMUL_CONTINUE)
5449 goto done;
5450 }
5451
Avi Kivity9dac77f2011-06-01 15:34:25 +03005452 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02005453 goto special_insn;
5454
5455
Avi Kivity9dac77f2011-06-01 15:34:25 +03005456 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005457 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005458 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
5459 &ctxt->dst.val, ctxt->dst.bytes);
Nadav Amitc205fb72014-12-25 02:52:16 +02005460 if (rc != X86EMUL_CONTINUE) {
Paolo Bonzinid44e1212015-02-09 10:02:05 +01005461 if (!(ctxt->d & NoWrite) &&
5462 rc == X86EMUL_PROPAGATE_FAULT &&
Nadav Amitc205fb72014-12-25 02:52:16 +02005463 ctxt->exception.vector == PF_VECTOR)
5464 ctxt->exception.error_code |= PFERR_WRITE_MASK;
Gleb Natapov69f55cb2010-03-18 15:20:20 +02005465 goto done;
Nadav Amitc205fb72014-12-25 02:52:16 +02005466 }
Avi Kivity038e51d2007-01-22 20:40:40 -08005467 }
Paolo Bonzini4ff6f8e2015-02-12 17:04:47 +01005468 /* Copy full 64-bit value for CMPXCHG8B. */
5469 ctxt->dst.orig_val64 = ctxt->dst.val64;
Avi Kivity038e51d2007-01-22 20:40:40 -08005470
Avi Kivity018a98d2007-11-27 19:30:56 +02005471special_insn:
5472
Ladi Prosek1eeb7942017-04-25 16:42:44 +02005473 if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03005474 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005475 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02005476 if (rc != X86EMUL_CONTINUE)
5477 goto done;
5478 }
5479
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005480 if (ctxt->rep_prefix && (ctxt->d & String))
Nadav Amit0efb0442015-03-29 16:33:03 +03005481 ctxt->eflags |= X86_EFLAGS_RF;
Nadav Amitb9a1ecb2014-07-24 14:51:23 +03005482 else
Nadav Amit0efb0442015-03-29 16:33:03 +03005483 ctxt->eflags &= ~X86_EFLAGS_RF;
Nadav Amit4467c3f2014-07-21 14:37:29 +03005484
Avi Kivity9dac77f2011-06-01 15:34:25 +03005485 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02005486 if (ctxt->d & Fastop) {
5487 void (*fop)(struct fastop *) = (void *)ctxt->execute;
5488 rc = fastop(ctxt, fop);
5489 if (rc != X86EMUL_CONTINUE)
5490 goto done;
5491 goto writeback;
5492 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03005493 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03005494 if (rc != X86EMUL_CONTINUE)
5495 goto done;
5496 goto writeback;
5497 }
5498
Borislav Petkov1ce19dc2013-09-22 16:44:51 +02005499 if (ctxt->opcode_len == 2)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005500 goto twobyte_insn;
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005501 else if (ctxt->opcode_len == 3)
5502 goto threebyte_insn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005503
Avi Kivity9dac77f2011-06-01 15:34:25 +03005504 switch (ctxt->b) {
Gleb Natapovb2833e32009-04-12 13:36:30 +03005505 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005506 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005507 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005508 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005509 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005510 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03005511 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03005512 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005513 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Nadav Amita825f5c2014-06-15 16:13:01 +03005514 ctxt->dst.type = OP_NONE;
5515 else
5516 rc = em_xchg(ctxt);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09005517 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005518 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005519 switch (ctxt->op_bytes) {
5520 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
5521 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
5522 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08005523 }
5524 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005525 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005526 rc = emulate_int(ctxt, 3);
5527 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005528 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005529 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005530 break;
5531 case 0xce: /* into */
Nadav Amit0efb0442015-03-29 16:33:03 +03005532 if (ctxt->eflags & X86_EFLAGS_OF)
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09005533 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03005534 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005535 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09005536 case 0xeb: /* jmp rel short */
Nadav Amit234f3ce2014-09-18 22:39:38 +03005537 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity9dac77f2011-06-01 15:34:25 +03005538 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07005539 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005540 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03005541 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03005542 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02005543 case 0xf5: /* cmc */
5544 /* complement carry flag from eflags reg */
Nadav Amit0efb0442015-03-29 16:33:03 +03005545 ctxt->eflags ^= X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005546 break;
5547 case 0xf8: /* clc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005548 ctxt->eflags &= ~X86_EFLAGS_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02005549 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005550 case 0xf9: /* stc */
Nadav Amit0efb0442015-03-29 16:33:03 +03005551 ctxt->eflags |= X86_EFLAGS_CF;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03005552 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005553 case 0xfc: /* cld */
Nadav Amit0efb0442015-03-29 16:33:03 +03005554 ctxt->eflags &= ~X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005555 break;
5556 case 0xfd: /* std */
Nadav Amit0efb0442015-03-29 16:33:03 +03005557 ctxt->eflags |= X86_EFLAGS_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03005558 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005559 default:
5560 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005562
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005563 if (rc != X86EMUL_CONTINUE)
5564 goto done;
5565
Avi Kivity018a98d2007-11-27 19:30:56 +02005566writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02005567 if (ctxt->d & SrcWrite) {
5568 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
5569 rc = writeback(ctxt, &ctxt->src);
5570 if (rc != X86EMUL_CONTINUE)
5571 goto done;
5572 }
Nadav Amitee212292014-06-15 16:12:58 +03005573 if (!(ctxt->d & NoWrite)) {
5574 rc = writeback(ctxt, &ctxt->dst);
5575 if (rc != X86EMUL_CONTINUE)
5576 goto done;
5577 }
Avi Kivity018a98d2007-11-27 19:30:56 +02005578
Gleb Natapov5cd21912010-03-18 15:20:26 +02005579 /*
5580 * restore dst type in case the decoding will be reused
5581 * (happens for string instruction )
5582 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005583 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005584
Avi Kivity9dac77f2011-06-01 15:34:25 +03005585 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005586 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02005587
Avi Kivity9dac77f2011-06-01 15:34:25 +03005588 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03005589 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02005590
Avi Kivity9dac77f2011-06-01 15:34:25 +03005591 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005592 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03005593 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03005594 if ((ctxt->d & SrcMask) == SrcSI)
5595 count = ctxt->src.count;
5596 else
5597 count = ctxt->dst.count;
Paolo Bonzini01485a22014-11-19 18:25:08 +01005598 register_address_increment(ctxt, VCPU_REGS_RCX, -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03005599
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005600 if (!string_insn_completed(ctxt)) {
5601 /*
5602 * Re-enter guest when pio read ahead buffer is empty
5603 * or, if it is not used, after each 1024 iteration.
5604 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03005605 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005606 (r->end == 0 || r->end != r->pos)) {
5607 /*
5608 * Reset read cache. Usually happens before
5609 * decode, but since instruction is restarted
5610 * we have to do it here.
5611 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005612 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03005613 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005614 return EMULATION_RESTART;
5615 }
5616 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03005617 }
Nadav Amit0efb0442015-03-29 16:33:03 +03005618 ctxt->eflags &= ~X86_EFLAGS_RF;
Gleb Natapov5cd21912010-03-18 15:20:26 +02005619 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005620
Avi Kivity9dac77f2011-06-01 15:34:25 +03005621 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02005622
5623done:
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005624 if (rc == X86EMUL_PROPAGATE_FAULT) {
5625 WARN_ON(ctxt->exception.vector > 0x1f);
Avi Kivityda9cb572010-11-22 17:53:21 +02005626 ctxt->have_exception = true;
Paolo Bonzinie0ad0b42014-08-20 10:08:23 +02005627 }
Joerg Roedel775fde82011-04-04 12:39:24 +02005628 if (rc == X86EMUL_INTERCEPTED)
5629 return EMULATION_INTERCEPTED;
5630
Avi Kivitydd856ef2012-08-27 23:46:17 +03005631 if (rc == X86EMUL_CONTINUE)
5632 writeback_registers(ctxt);
5633
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03005634 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005635
5636twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03005637 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02005638 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02005639 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005640 break;
5641 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02005642 case 0x0d: /* GrpP (prefetch) */
5643 case 0x18: /* Grp16 (prefetch/nop) */
Paolo Bonzini103f98e2013-05-30 13:22:39 +02005644 case 0x1f: /* nop */
Avi Kivity018a98d2007-11-27 19:30:56 +02005645 break;
5646 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005647 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02005648 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005649 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005650 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005652 case 0x40 ... 0x4f: /* cmov */
Nadav Amit140bad82014-06-15 16:13:00 +03005653 if (test_cc(ctxt->b, ctxt->eflags))
5654 ctxt->dst.val = ctxt->src.val;
Nadav Amitb91aa142015-03-30 15:39:19 +03005655 else if (ctxt->op_bytes != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03005656 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005657 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03005658 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03005659 if (test_cc(ctxt->b, ctxt->eflags))
Nadav Amit234f3ce2014-09-18 22:39:38 +03005660 rc = jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02005661 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08005662 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005663 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08005664 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005665 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005666 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005667 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03005668 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005669 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005670 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03005671 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03005672 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03005673 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005674 break;
Avi Kivity91269b82010-07-25 14:51:16 +03005675 default:
5676 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005677 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005678
Borislav Petkov0bc5eed2013-10-29 12:54:10 +01005679threebyte_insn:
5680
Avi Kivity7d9ddae2010-08-30 17:12:28 +03005681 if (rc != X86EMUL_CONTINUE)
5682 goto done;
5683
Avi Kivity6aa8b732006-12-10 02:21:36 -08005684 goto writeback;
5685
5686cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02005687 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688}
Avi Kivitydd856ef2012-08-27 23:46:17 +03005689
5690void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
5691{
5692 invalidate_registers(ctxt);
5693}
5694
5695void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
5696{
5697 writeback_registers(ctxt);
5698}