blob: 5053e9efb14029b14fa5a37c7a5d0fd69fcbd769 [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 Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya99455492011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020060#define OpMem8 26ull /* 8-bit zero extended memory operand */
Avi Kivitya99455492011-09-13 10:45:41 +030061
Avi Kivity0fe59122011-09-13 10:45:47 +030062#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030063#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030064
65/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080066 * Opcode effective-address decode tables.
67 * Note that we only emulate instructions that have at least one memory
68 * operand (excluding implicit stack references). We assume that stack
69 * references and instruction fetches will never occur in special memory
70 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
71 * not be handled.
72 */
73
74/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030075#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030077#define DstShift 1
78#define ImplicitOps (OpImplicit << DstShift)
79#define DstReg (OpReg << DstShift)
80#define DstMem (OpMem << DstShift)
81#define DstAcc (OpAcc << DstShift)
82#define DstDI (OpDI << DstShift)
83#define DstMem64 (OpMem64 << DstShift)
84#define DstImmUByte (OpImmUByte << DstShift)
85#define DstDX (OpDX << DstShift)
86#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030088#define SrcShift 6
89#define SrcNone (OpNone << SrcShift)
90#define SrcReg (OpReg << SrcShift)
91#define SrcMem (OpMem << SrcShift)
92#define SrcMem16 (OpMem16 << SrcShift)
93#define SrcMem32 (OpMem32 << SrcShift)
94#define SrcImm (OpImm << SrcShift)
95#define SrcImmByte (OpImmByte << SrcShift)
96#define SrcOne (OpOne << SrcShift)
97#define SrcImmUByte (OpImmUByte << SrcShift)
98#define SrcImmU (OpImmU << SrcShift)
99#define SrcSI (OpSI << SrcShift)
100#define SrcImmFAddr (OpImmFAddr << SrcShift)
101#define SrcMemFAddr (OpMemFAddr << SrcShift)
102#define SrcAcc (OpAcc << SrcShift)
103#define SrcImmU16 (OpImmU16 << SrcShift)
104#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200105#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300106#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300107#define BitOp (1<<11)
108#define MemAbs (1<<12) /* Memory operand is absolute displacement */
109#define String (1<<13) /* String instruction (rep capable) */
110#define Stack (1<<14) /* Stack instruction (push/pop) */
111#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
112#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
113#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
114#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
115#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
116#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300117/* Generic ModRM decode. */
118#define ModRM (1<<19)
119/* Destination is only written; never read. */
120#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300121/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200122#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200123#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300124#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300125#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300126#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200127#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200128#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300129#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800130#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100131/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800132#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300133#define Src2None (OpNone << Src2Shift)
134#define Src2CL (OpCL << Src2Shift)
135#define Src2ImmByte (OpImmByte << Src2Shift)
136#define Src2One (OpOne << Src2Shift)
137#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300138#define Src2ES (OpES << Src2Shift)
139#define Src2CS (OpCS << Src2Shift)
140#define Src2SS (OpSS << Src2Shift)
141#define Src2DS (OpDS << Src2Shift)
142#define Src2FS (OpFS << Src2Shift)
143#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300145#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300146#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
147#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
148#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Avi Kivityd0e53322010-07-29 15:11:54 +0300150#define X2(x...) x, x
151#define X3(x...) X2(x), x
152#define X4(x...) X2(x), X2(x)
153#define X5(x...) X4(x), x
154#define X6(x...) X4(x), X2(x)
155#define X7(x...) X4(x), X3(x)
156#define X8(x...) X4(x), X4(x)
157#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300158
Avi Kivityd65b1de2010-07-29 15:11:35 +0300159struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300160 u64 flags : 56;
161 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300162 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300163 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300164 struct opcode *group;
165 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200166 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300167 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200168 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300169};
170
171struct group_dual {
172 struct opcode mod012[8];
173 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300174};
175
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200176struct gprefix {
177 struct opcode pfx_no;
178 struct opcode pfx_66;
179 struct opcode pfx_f2;
180 struct opcode pfx_f3;
181};
182
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200184#define EFLG_ID (1<<21)
185#define EFLG_VIP (1<<20)
186#define EFLG_VIF (1<<19)
187#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200188#define EFLG_VM (1<<17)
189#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200190#define EFLG_IOPL (3<<12)
191#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192#define EFLG_OF (1<<11)
193#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200194#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200195#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196#define EFLG_SF (1<<7)
197#define EFLG_ZF (1<<6)
198#define EFLG_AF (1<<4)
199#define EFLG_PF (1<<2)
200#define EFLG_CF (1<<0)
201
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300202#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
203#define EFLG_RESERVED_ONE_MASK 2
204
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205/*
206 * Instruction emulation:
207 * Most instructions are emulated directly via a fragment of inline assembly
208 * code. This allows us to save/restore EFLAGS and thus very easily pick up
209 * any modified flags.
210 */
211
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800212#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213#define _LO32 "k" /* force 32-bit operand */
214#define _STK "%%rsp" /* stack pointer */
215#elif defined(__i386__)
216#define _LO32 "" /* force 32-bit operand */
217#define _STK "%%esp" /* stack pointer */
218#endif
219
220/*
221 * These EFLAGS bits are restored from saved value during emulation, and
222 * any changes are written back to the saved value after emulation.
223 */
224#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
225
226/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200227#define _PRE_EFLAGS(_sav, _msk, _tmp) \
228 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
229 "movl %"_sav",%"_LO32 _tmp"; " \
230 "push %"_tmp"; " \
231 "push %"_tmp"; " \
232 "movl %"_msk",%"_LO32 _tmp"; " \
233 "andl %"_LO32 _tmp",("_STK"); " \
234 "pushf; " \
235 "notl %"_LO32 _tmp"; " \
236 "andl %"_LO32 _tmp",("_STK"); " \
237 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
238 "pop %"_tmp"; " \
239 "orl %"_LO32 _tmp",("_STK"); " \
240 "popf; " \
241 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242
243/* After executing instruction: write-back necessary bits in EFLAGS. */
244#define _POST_EFLAGS(_sav, _msk, _tmp) \
245 /* _sav |= EFLAGS & _msk; */ \
246 "pushf; " \
247 "pop %"_tmp"; " \
248 "andl %"_msk",%"_LO32 _tmp"; " \
249 "orl %"_LO32 _tmp",%"_sav"; "
250
Avi Kivitydda96d82008-11-26 15:14:10 +0200251#ifdef CONFIG_X86_64
252#define ON64(x) x
253#else
254#define ON64(x)
255#endif
256
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300257#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200258 do { \
259 __asm__ __volatile__ ( \
260 _PRE_EFLAGS("0", "4", "2") \
261 _op _suffix " %"_x"3,%1; " \
262 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300263 : "=m" ((ctxt)->eflags), \
264 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200265 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300266 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200267 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200268
269
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300271#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200272 do { \
273 unsigned long _tmp; \
274 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300275 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200276 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300277 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200278 break; \
279 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300280 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200281 break; \
282 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300283 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200284 break; \
285 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286 } while (0)
287
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300288#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200290 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300291 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300293 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294 break; \
295 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300296 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297 _wx, _wy, _lx, _ly, _qx, _qy); \
298 break; \
299 } \
300 } while (0)
301
302/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300303#define emulate_2op_SrcB(ctxt, _op) \
304 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305
306/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300307#define emulate_2op_SrcV(ctxt, _op) \
308 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309
310/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300311#define emulate_2op_SrcV_nobyte(ctxt, _op) \
312 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100314/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300315#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300316 do { \
317 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300318 _type _clv = (ctxt)->src2.val; \
319 _type _srcv = (ctxt)->src.val; \
320 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300321 \
322 __asm__ __volatile__ ( \
323 _PRE_EFLAGS("0", "5", "2") \
324 _op _suffix " %4,%1 \n" \
325 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300326 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300327 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
328 ); \
329 \
Avi Kivity761441b2011-09-07 16:41:36 +0300330 (ctxt)->src2.val = (unsigned long) _clv; \
331 (ctxt)->src2.val = (unsigned long) _srcv; \
332 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100333 } while (0)
334
Avi Kivity761441b2011-09-07 16:41:36 +0300335#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300336 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300337 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300338 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300339 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300340 break; \
341 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300342 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300343 break; \
344 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300345 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300346 break; \
347 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100348 } while (0)
349
Avi Kivityd1eef452011-09-07 16:41:38 +0300350#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351 do { \
352 unsigned long _tmp; \
353 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200354 __asm__ __volatile__ ( \
355 _PRE_EFLAGS("0", "3", "2") \
356 _op _suffix " %1; " \
357 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300358 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200359 "=&r" (_tmp) \
360 : "i" (EFLAGS_MASK)); \
361 } while (0)
362
363/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300364#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200365 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300366 switch ((ctxt)->dst.bytes) { \
367 case 1: __emulate_1op(ctxt, _op, "b"); break; \
368 case 2: __emulate_1op(ctxt, _op, "w"); break; \
369 case 4: __emulate_1op(ctxt, _op, "l"); break; \
370 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 } \
372 } while (0)
373
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300374#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300375 do { \
376 unsigned long _tmp; \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300377 ulong *rax = &(ctxt)->regs[VCPU_REGS_RAX]; \
378 ulong *rdx = &(ctxt)->regs[VCPU_REGS_RDX]; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300379 \
380 __asm__ __volatile__ ( \
381 _PRE_EFLAGS("0", "5", "1") \
382 "1: \n\t" \
383 _op _suffix " %6; " \
384 "2: \n\t" \
385 _POST_EFLAGS("0", "5", "1") \
386 ".pushsection .fixup,\"ax\" \n\t" \
387 "3: movb $1, %4 \n\t" \
388 "jmp 2b \n\t" \
389 ".popsection \n\t" \
390 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300391 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
392 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
393 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
394 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300395 } while (0)
396
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300397/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300398#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300399 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300400 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300401 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300402 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300403 break; \
404 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300405 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300406 break; \
407 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300408 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300409 break; \
410 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300411 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300412 break; \
413 } \
414 } while (0)
415
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200416static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
417 enum x86_intercept intercept,
418 enum x86_intercept_stage stage)
419{
420 struct x86_instruction_info info = {
421 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300422 .rep_prefix = ctxt->rep_prefix,
423 .modrm_mod = ctxt->modrm_mod,
424 .modrm_reg = ctxt->modrm_reg,
425 .modrm_rm = ctxt->modrm_rm,
426 .src_val = ctxt->src.val64,
427 .src_bytes = ctxt->src.bytes,
428 .dst_bytes = ctxt->dst.bytes,
429 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200430 .next_rip = ctxt->eip,
431 };
432
Avi Kivity29535382011-04-20 13:37:53 +0300433 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200434}
435
Avi Kivityf47cfa32012-06-07 17:49:24 +0300436static void assign_masked(ulong *dest, ulong src, ulong mask)
437{
438 *dest = (*dest & ~mask) | (src & mask);
439}
440
Avi Kivity9dac77f2011-06-01 15:34:25 +0300441static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800442{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300443 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800444}
445
Avi Kivityf47cfa32012-06-07 17:49:24 +0300446static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
447{
448 u16 sel;
449 struct desc_struct ss;
450
451 if (ctxt->mode == X86EMUL_MODE_PROT64)
452 return ~0UL;
453 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
454 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
455}
456
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800458static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300459address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800460{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300461 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800462 return reg;
463 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300464 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800465}
466
467static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300468register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800469{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300470 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800471}
472
Harvey Harrison7a9572752008-02-19 07:40:41 -0800473static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800475{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800477 *reg += inc;
478 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300479 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800480}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481
Avi Kivity9dac77f2011-06-01 15:34:25 +0300482static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800483{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300484 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800485}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300486
Avi Kivity56697682011-04-03 14:08:51 +0300487static u32 desc_limit_scaled(struct desc_struct *desc)
488{
489 u32 limit = get_desc_limit(desc);
490
491 return desc->g ? (limit << 12) | 0xfff : limit;
492}
493
Avi Kivity9dac77f2011-06-01 15:34:25 +0300494static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300495{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300496 ctxt->has_seg_override = true;
497 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300498}
499
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900500static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300501{
502 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
503 return 0;
504
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900505 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300506}
507
Avi Kivity9dac77f2011-06-01 15:34:25 +0300508static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300509{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300510 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300511 return 0;
512
Avi Kivity9dac77f2011-06-01 15:34:25 +0300513 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300514}
515
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200516static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
517 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300518{
Avi Kivityda9cb572010-11-22 17:53:21 +0200519 ctxt->exception.vector = vec;
520 ctxt->exception.error_code = error;
521 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200522 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300523}
524
Joerg Roedel3b88e412011-04-04 12:39:29 +0200525static int emulate_db(struct x86_emulate_ctxt *ctxt)
526{
527 return emulate_exception(ctxt, DB_VECTOR, 0, false);
528}
529
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200530static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300531{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200532 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300533}
534
Avi Kivity618ff152011-04-03 12:32:09 +0300535static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
536{
537 return emulate_exception(ctxt, SS_VECTOR, err, true);
538}
539
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300541{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200542 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300543}
544
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200545static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300546{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300548}
549
Avi Kivity34d1f492010-08-26 11:59:01 +0300550static int emulate_de(struct x86_emulate_ctxt *ctxt)
551{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200552 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300553}
554
Avi Kivity12537912011-03-29 11:41:27 +0200555static int emulate_nm(struct x86_emulate_ctxt *ctxt)
556{
557 return emulate_exception(ctxt, NM_VECTOR, 0, false);
558}
559
Avi Kivity1aa36612011-04-27 13:20:30 +0300560static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
561{
562 u16 selector;
563 struct desc_struct desc;
564
565 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
566 return selector;
567}
568
569static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
570 unsigned seg)
571{
572 u16 dummy;
573 u32 base3;
574 struct desc_struct desc;
575
576 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
577 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
578}
579
Avi Kivity1c11b372012-04-09 18:39:59 +0300580/*
581 * x86 defines three classes of vector instructions: explicitly
582 * aligned, explicitly unaligned, and the rest, which change behaviour
583 * depending on whether they're AVX encoded or not.
584 *
585 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
586 * subject to the same check.
587 */
588static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
589{
590 if (likely(size < 16))
591 return false;
592
593 if (ctxt->d & Aligned)
594 return true;
595 else if (ctxt->d & Unaligned)
596 return false;
597 else if (ctxt->d & Avx)
598 return false;
599 else
600 return true;
601}
602
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400603static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300604 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400605 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300606 ulong *linear)
607{
Avi Kivity618ff152011-04-03 12:32:09 +0300608 struct desc_struct desc;
609 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300610 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300611 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300612 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300613 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300614
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900615 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300616 switch (ctxt->mode) {
617 case X86EMUL_MODE_REAL:
618 break;
619 case X86EMUL_MODE_PROT64:
620 if (((signed long)la << 16) >> 16 != la)
621 return emulate_gp(ctxt, 0);
622 break;
623 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300624 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
625 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300626 if (!usable)
627 goto bad;
628 /* code segment or read-only data segment */
629 if (((desc.type & 8) || !(desc.type & 2)) && write)
630 goto bad;
631 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400632 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300633 goto bad;
634 lim = desc_limit_scaled(&desc);
635 if ((desc.type & 8) || !(desc.type & 4)) {
636 /* expand-up segment */
637 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
638 goto bad;
639 } else {
640 /* exapand-down segment */
641 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
642 goto bad;
643 lim = desc.d ? 0xffffffff : 0xffff;
644 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
645 goto bad;
646 }
Avi Kivity717746e2011-04-20 13:37:53 +0300647 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300648 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300649 cpl = max(cpl, rpl);
650 if (!(desc.type & 8)) {
651 /* data segment */
652 if (cpl > desc.dpl)
653 goto bad;
654 } else if ((desc.type & 8) && !(desc.type & 4)) {
655 /* nonconforming code segment */
656 if (cpl != desc.dpl)
657 goto bad;
658 } else if ((desc.type & 8) && (desc.type & 4)) {
659 /* conforming code segment */
660 if (cpl < desc.dpl)
661 goto bad;
662 }
663 break;
664 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300665 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300666 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300667 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
668 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300669 *linear = la;
670 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300671bad:
672 if (addr.seg == VCPU_SREG_SS)
673 return emulate_ss(ctxt, addr.seg);
674 else
675 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300676}
677
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400678static int linearize(struct x86_emulate_ctxt *ctxt,
679 struct segmented_address addr,
680 unsigned size, bool write,
681 ulong *linear)
682{
683 return __linearize(ctxt, addr, size, write, false, linear);
684}
685
686
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200687static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
688 struct segmented_address addr,
689 void *data,
690 unsigned size)
691{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200692 int rc;
693 ulong linear;
694
Avi Kivity83b87952011-04-03 11:31:19 +0300695 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200696 if (rc != X86EMUL_CONTINUE)
697 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300698 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200699}
700
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900701/*
702 * Fetch the next byte of the instruction being emulated which is pointed to
703 * by ctxt->_eip, then increment ctxt->_eip.
704 *
705 * Also prefetch the remaining bytes of the instruction without crossing page
706 * boundary if they are not in fetch_cache yet.
707 */
708static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200709{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300710 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200711 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300712 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200713
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900714 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400715 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900716 struct segmented_address addr = { .seg = VCPU_SREG_CS,
717 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300718 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900719 size = min(15UL - cur_size,
720 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400721 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900722 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400723 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900724 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
725 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900726 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200727 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300728 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200729 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900730 *dest = fc->data[ctxt->_eip - fc->start];
731 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900732 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200733}
734
735static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900736 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200737{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900738 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200739
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200740 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900741 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200742 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200743 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900744 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900745 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200746 return rc;
747 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900748 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200749}
750
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900751/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900752#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900753({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900754 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900755 if (rc != X86EMUL_CONTINUE) \
756 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900757 (_type)_x; \
758})
759
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900760#define insn_fetch_arr(_arr, _size, _ctxt) \
761({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900762 if (rc != X86EMUL_CONTINUE) \
763 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900764})
765
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000766/*
767 * Given the 'reg' portion of a ModRM byte, and a register block, return a
768 * pointer into the block that addresses the relevant register.
769 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
770 */
771static void *decode_register(u8 modrm_reg, unsigned long *regs,
772 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773{
774 void *p;
775
776 p = &regs[modrm_reg];
777 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
778 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
779 return p;
780}
781
782static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200783 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 u16 *size, unsigned long *address, int op_bytes)
785{
786 int rc;
787
788 if (op_bytes == 2)
789 op_bytes = 3;
790 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200791 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900792 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200794 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200795 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 return rc;
797}
798
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300799static int test_cc(unsigned int condition, unsigned int flags)
800{
801 int rc = 0;
802
803 switch ((condition & 15) >> 1) {
804 case 0: /* o */
805 rc |= (flags & EFLG_OF);
806 break;
807 case 1: /* b/c/nae */
808 rc |= (flags & EFLG_CF);
809 break;
810 case 2: /* z/e */
811 rc |= (flags & EFLG_ZF);
812 break;
813 case 3: /* be/na */
814 rc |= (flags & (EFLG_CF|EFLG_ZF));
815 break;
816 case 4: /* s */
817 rc |= (flags & EFLG_SF);
818 break;
819 case 5: /* p/pe */
820 rc |= (flags & EFLG_PF);
821 break;
822 case 7: /* le/ng */
823 rc |= (flags & EFLG_ZF);
824 /* fall through */
825 case 6: /* l/nge */
826 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
827 break;
828 }
829
830 /* Odd condition identifiers (lsb == 1) have inverted sense. */
831 return (!!rc ^ (condition & 1));
832}
833
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300834static void fetch_register_operand(struct operand *op)
835{
836 switch (op->bytes) {
837 case 1:
838 op->val = *(u8 *)op->addr.reg;
839 break;
840 case 2:
841 op->val = *(u16 *)op->addr.reg;
842 break;
843 case 4:
844 op->val = *(u32 *)op->addr.reg;
845 break;
846 case 8:
847 op->val = *(u64 *)op->addr.reg;
848 break;
849 }
850}
851
Avi Kivity12537912011-03-29 11:41:27 +0200852static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
853{
854 ctxt->ops->get_fpu(ctxt);
855 switch (reg) {
856 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
857 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
858 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
859 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
860 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
861 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
862 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
863 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
864#ifdef CONFIG_X86_64
865 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
866 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
867 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
868 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
869 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
870 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
871 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
872 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
873#endif
874 default: BUG();
875 }
876 ctxt->ops->put_fpu(ctxt);
877}
878
879static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
880 int reg)
881{
882 ctxt->ops->get_fpu(ctxt);
883 switch (reg) {
884 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
885 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
886 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
887 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
888 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
889 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
890 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
891 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
892#ifdef CONFIG_X86_64
893 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
894 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
895 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
896 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
897 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
898 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
899 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
900 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
901#endif
902 default: BUG();
903 }
904 ctxt->ops->put_fpu(ctxt);
905}
906
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300907static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
908{
909 ctxt->ops->get_fpu(ctxt);
910 switch (reg) {
911 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
912 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
913 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
914 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
915 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
916 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
917 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
918 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
919 default: BUG();
920 }
921 ctxt->ops->put_fpu(ctxt);
922}
923
924static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
925{
926 ctxt->ops->get_fpu(ctxt);
927 switch (reg) {
928 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
929 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
930 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
931 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
932 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
933 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
934 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
935 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
936 default: BUG();
937 }
938 ctxt->ops->put_fpu(ctxt);
939}
940
Avi Kivity12537912011-03-29 11:41:27 +0200941static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200942 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200943{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300944 unsigned reg = ctxt->modrm_reg;
945 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200946
Avi Kivity9dac77f2011-06-01 15:34:25 +0300947 if (!(ctxt->d & ModRM))
948 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200949
Avi Kivity9dac77f2011-06-01 15:34:25 +0300950 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200951 op->type = OP_XMM;
952 op->bytes = 16;
953 op->addr.xmm = reg;
954 read_sse_reg(ctxt, &op->vec_val, reg);
955 return;
956 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300957 if (ctxt->d & Mmx) {
958 reg &= 7;
959 op->type = OP_MM;
960 op->bytes = 8;
961 op->addr.mm = reg;
962 return;
963 }
Avi Kivity12537912011-03-29 11:41:27 +0200964
Avi Kivity3c118e22007-10-31 10:27:04 +0200965 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200966 if (ctxt->d & ByteOp) {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300967 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200968 op->bytes = 1;
969 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300970 op->addr.reg = decode_register(reg, ctxt->regs, 0);
971 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200972 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300973 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200974 op->orig_val = op->val;
975}
976
Avi Kivitya6e34072012-06-10 17:15:39 +0300977static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
978{
979 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
980 ctxt->modrm_seg = VCPU_SREG_SS;
981}
982
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200983static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300984 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200985{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200986 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700987 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900988 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300989 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200990
Avi Kivity9dac77f2011-06-01 15:34:25 +0300991 if (ctxt->rex_prefix) {
992 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
993 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
994 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200995 }
996
Avi Kivity9dac77f2011-06-01 15:34:25 +0300997 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
998 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
999 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1000 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001
Avi Kivity9dac77f2011-06-01 15:34:25 +03001002 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001003 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001004 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1005 op->addr.reg = decode_register(ctxt->modrm_rm,
1006 ctxt->regs, ctxt->d & ByteOp);
1007 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001008 op->type = OP_XMM;
1009 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001010 op->addr.xmm = ctxt->modrm_rm;
1011 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001012 return rc;
1013 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001014 if (ctxt->d & Mmx) {
1015 op->type = OP_MM;
1016 op->bytes = 8;
1017 op->addr.xmm = ctxt->modrm_rm & 7;
1018 return rc;
1019 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001020 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001021 return rc;
1022 }
1023
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001024 op->type = OP_MEM;
1025
Avi Kivity9dac77f2011-06-01 15:34:25 +03001026 if (ctxt->ad_bytes == 2) {
1027 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
1028 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
1029 unsigned si = ctxt->regs[VCPU_REGS_RSI];
1030 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001031
1032 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001033 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001034 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001035 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001036 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001037 break;
1038 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001039 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001040 break;
1041 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001042 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001043 break;
1044 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001045 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001046 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001047 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001048 break;
1049 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001050 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001051 break;
1052 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001053 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001054 break;
1055 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001056 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001057 break;
1058 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001059 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001060 break;
1061 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001062 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001063 break;
1064 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001065 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001066 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001067 break;
1068 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001069 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001070 break;
1071 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001072 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1073 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1074 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001075 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001076 } else {
1077 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001078 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001079 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001080 index_reg |= (sib >> 3) & 7;
1081 base_reg |= sib & 7;
1082 scale = sib >> 6;
1083
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001085 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001086 else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001087 modrm_ea += ctxt->regs[base_reg];
Avi Kivitya6e34072012-06-10 17:15:39 +03001088 adjust_modrm_seg(ctxt, base_reg);
1089 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001090 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001091 modrm_ea += ctxt->regs[index_reg] << scale;
1092 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001093 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001094 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001095 } else {
1096 base_reg = ctxt->modrm_rm;
1097 modrm_ea += ctxt->regs[base_reg];
1098 adjust_modrm_seg(ctxt, base_reg);
1099 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001100 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001101 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001102 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001103 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001104 break;
1105 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001106 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001107 break;
1108 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001109 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001110 break;
1111 }
1112 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001113 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114done:
1115 return rc;
1116}
1117
1118static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001119 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001121 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001123 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001124 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001126 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127 break;
1128 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001129 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130 break;
1131 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001132 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133 break;
1134 }
1135done:
1136 return rc;
1137}
1138
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001140{
Sheng Yang7129eec2010-09-28 16:33:32 +08001141 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001142
Avi Kivity9dac77f2011-06-01 15:34:25 +03001143 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1144 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001145
Avi Kivity9dac77f2011-06-01 15:34:25 +03001146 if (ctxt->src.bytes == 2)
1147 sv = (s16)ctxt->src.val & (s16)mask;
1148 else if (ctxt->src.bytes == 4)
1149 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001150
Avi Kivity9dac77f2011-06-01 15:34:25 +03001151 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001152 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001153
1154 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001155 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001156}
1157
Gleb Natapov9de41572010-04-28 19:15:22 +03001158static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001159 unsigned long addr, void *dest, unsigned size)
1160{
1161 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001163
1164 while (size) {
1165 int n = min(size, 8u);
1166 size -= n;
1167 if (mc->pos < mc->end)
1168 goto read_cached;
1169
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001170 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1171 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001172 if (rc != X86EMUL_CONTINUE)
1173 return rc;
1174 mc->end += n;
1175
1176 read_cached:
1177 memcpy(dest, mc->data + mc->pos, n);
1178 mc->pos += n;
1179 dest += n;
1180 addr += n;
1181 }
1182 return X86EMUL_CONTINUE;
1183}
1184
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001185static int segmented_read(struct x86_emulate_ctxt *ctxt,
1186 struct segmented_address addr,
1187 void *data,
1188 unsigned size)
1189{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001190 int rc;
1191 ulong linear;
1192
Avi Kivity83b87952011-04-03 11:31:19 +03001193 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001194 if (rc != X86EMUL_CONTINUE)
1195 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001196 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001197}
1198
1199static int segmented_write(struct x86_emulate_ctxt *ctxt,
1200 struct segmented_address addr,
1201 const void *data,
1202 unsigned size)
1203{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001204 int rc;
1205 ulong linear;
1206
Avi Kivity83b87952011-04-03 11:31:19 +03001207 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001208 if (rc != X86EMUL_CONTINUE)
1209 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001210 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1211 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001212}
1213
1214static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1215 struct segmented_address addr,
1216 const void *orig_data, const void *data,
1217 unsigned size)
1218{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001219 int rc;
1220 ulong linear;
1221
Avi Kivity83b87952011-04-03 11:31:19 +03001222 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001223 if (rc != X86EMUL_CONTINUE)
1224 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001225 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1226 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001227}
1228
Gleb Natapov7b262e92010-03-18 15:20:27 +02001229static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001230 unsigned int size, unsigned short port,
1231 void *dest)
1232{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001234
1235 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001236 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001237 unsigned int count = ctxt->rep_prefix ?
1238 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001239 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001240 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1241 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001242 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1243 count);
1244 if (n == 0)
1245 n = 1;
1246 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001247 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001248 return 0;
1249 rc->end = n * size;
1250 }
1251
1252 memcpy(dest, rc->data + rc->pos, size);
1253 rc->pos += size;
1254 return 1;
1255}
1256
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001257static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1258 u16 index, struct desc_struct *desc)
1259{
1260 struct desc_ptr dt;
1261 ulong addr;
1262
1263 ctxt->ops->get_idt(ctxt, &dt);
1264
1265 if (dt.size < index * 8 + 7)
1266 return emulate_gp(ctxt, index << 3 | 0x2);
1267
1268 addr = dt.address + index * 8;
1269 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1270 &ctxt->exception);
1271}
1272
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001273static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001274 u16 selector, struct desc_ptr *dt)
1275{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001276 struct x86_emulate_ops *ops = ctxt->ops;
1277
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001278 if (selector & 1 << 2) {
1279 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001280 u16 sel;
1281
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001282 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001283 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001284 return;
1285
1286 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1287 dt->address = get_desc_base(&desc);
1288 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001289 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001290}
1291
1292/* allowed just for 8 bytes segments */
1293static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001294 u16 selector, struct desc_struct *desc)
1295{
1296 struct desc_ptr dt;
1297 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001298 ulong addr;
1299
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001300 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001301
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001302 if (dt.size < index * 8 + 7)
1303 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001304
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001305 addr = dt.address + index * 8;
1306 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1307 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001308}
1309
1310/* allowed just for 8 bytes segments */
1311static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001312 u16 selector, struct desc_struct *desc)
1313{
1314 struct desc_ptr dt;
1315 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001316 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001317
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001318 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001319
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001320 if (dt.size < index * 8 + 7)
1321 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001322
1323 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001324 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1325 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001326}
1327
Gleb Natapov5601d052011-03-07 14:55:06 +02001328/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001329static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001330 u16 selector, int seg)
1331{
1332 struct desc_struct seg_desc;
1333 u8 dpl, rpl, cpl;
1334 unsigned err_vec = GP_VECTOR;
1335 u32 err_code = 0;
1336 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1337 int ret;
1338
1339 memset(&seg_desc, 0, sizeof seg_desc);
1340
1341 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1342 || ctxt->mode == X86EMUL_MODE_REAL) {
1343 /* set real mode segment descriptor */
1344 set_desc_base(&seg_desc, selector << 4);
1345 set_desc_limit(&seg_desc, 0xffff);
1346 seg_desc.type = 3;
1347 seg_desc.p = 1;
1348 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001349 if (ctxt->mode == X86EMUL_MODE_VM86)
1350 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001351 goto load;
1352 }
1353
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001354 rpl = selector & 3;
1355 cpl = ctxt->ops->cpl(ctxt);
1356
1357 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1358 if ((seg == VCPU_SREG_CS
1359 || (seg == VCPU_SREG_SS
1360 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1361 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001362 && null_selector)
1363 goto exception;
1364
1365 /* TR should be in GDT only */
1366 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1367 goto exception;
1368
1369 if (null_selector) /* for NULL selector skip all following checks */
1370 goto load;
1371
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001372 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001373 if (ret != X86EMUL_CONTINUE)
1374 return ret;
1375
1376 err_code = selector & 0xfffc;
1377 err_vec = GP_VECTOR;
1378
1379 /* can't load system descriptor into segment selecor */
1380 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1381 goto exception;
1382
1383 if (!seg_desc.p) {
1384 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1385 goto exception;
1386 }
1387
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001388 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001389
1390 switch (seg) {
1391 case VCPU_SREG_SS:
1392 /*
1393 * segment is not a writable data segment or segment
1394 * selector's RPL != CPL or segment selector's RPL != CPL
1395 */
1396 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1397 goto exception;
1398 break;
1399 case VCPU_SREG_CS:
1400 if (!(seg_desc.type & 8))
1401 goto exception;
1402
1403 if (seg_desc.type & 4) {
1404 /* conforming */
1405 if (dpl > cpl)
1406 goto exception;
1407 } else {
1408 /* nonconforming */
1409 if (rpl > cpl || dpl != cpl)
1410 goto exception;
1411 }
1412 /* CS(RPL) <- CPL */
1413 selector = (selector & 0xfffc) | cpl;
1414 break;
1415 case VCPU_SREG_TR:
1416 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1417 goto exception;
1418 break;
1419 case VCPU_SREG_LDTR:
1420 if (seg_desc.s || seg_desc.type != 2)
1421 goto exception;
1422 break;
1423 default: /* DS, ES, FS, or GS */
1424 /*
1425 * segment is not a data or readable code segment or
1426 * ((segment is a data or nonconforming code segment)
1427 * and (both RPL and CPL > DPL))
1428 */
1429 if ((seg_desc.type & 0xa) == 0x8 ||
1430 (((seg_desc.type & 0xc) != 0xc) &&
1431 (rpl > dpl && cpl > dpl)))
1432 goto exception;
1433 break;
1434 }
1435
1436 if (seg_desc.s) {
1437 /* mark segment as accessed */
1438 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001439 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440 if (ret != X86EMUL_CONTINUE)
1441 return ret;
1442 }
1443load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001444 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001445 return X86EMUL_CONTINUE;
1446exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001447 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448 return X86EMUL_PROPAGATE_FAULT;
1449}
1450
Wei Yongjun31be40b2010-08-17 09:17:30 +08001451static void write_register_operand(struct operand *op)
1452{
1453 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1454 switch (op->bytes) {
1455 case 1:
1456 *(u8 *)op->addr.reg = (u8)op->val;
1457 break;
1458 case 2:
1459 *(u16 *)op->addr.reg = (u16)op->val;
1460 break;
1461 case 4:
1462 *op->addr.reg = (u32)op->val;
1463 break; /* 64b: zero-extend */
1464 case 8:
1465 *op->addr.reg = op->val;
1466 break;
1467 }
1468}
1469
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001470static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001471{
1472 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001473
Avi Kivity9dac77f2011-06-01 15:34:25 +03001474 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001475 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001476 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001477 break;
1478 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001479 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001480 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001481 ctxt->dst.addr.mem,
1482 &ctxt->dst.orig_val,
1483 &ctxt->dst.val,
1484 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001485 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001486 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001487 ctxt->dst.addr.mem,
1488 &ctxt->dst.val,
1489 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001490 if (rc != X86EMUL_CONTINUE)
1491 return rc;
1492 break;
Avi Kivity12537912011-03-29 11:41:27 +02001493 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001494 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001495 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001496 case OP_MM:
1497 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1498 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001499 case OP_NONE:
1500 /* no writeback */
1501 break;
1502 default:
1503 break;
1504 }
1505 return X86EMUL_CONTINUE;
1506}
1507
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001508static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001509{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001510 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001511
Avi Kivity9dac77f2011-06-01 15:34:25 +03001512 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1513 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001514 addr.seg = VCPU_SREG_SS;
1515
1516 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001517 ctxt->dst.type = OP_NONE;
1518 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001519}
1520
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001521static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001522 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001523{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001524 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001525 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001526
Avi Kivity9dac77f2011-06-01 15:34:25 +03001527 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001528 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001529 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001530 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001531 return rc;
1532
Avi Kivity9dac77f2011-06-01 15:34:25 +03001533 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001534 return rc;
1535}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001536
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001537static int em_pop(struct x86_emulate_ctxt *ctxt)
1538{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001539 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001540}
1541
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001542static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001543 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001544{
1545 int rc;
1546 unsigned long val, change_mask;
1547 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001548 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001549
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001550 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001551 if (rc != X86EMUL_CONTINUE)
1552 return rc;
1553
1554 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1555 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1556
1557 switch(ctxt->mode) {
1558 case X86EMUL_MODE_PROT64:
1559 case X86EMUL_MODE_PROT32:
1560 case X86EMUL_MODE_PROT16:
1561 if (cpl == 0)
1562 change_mask |= EFLG_IOPL;
1563 if (cpl <= iopl)
1564 change_mask |= EFLG_IF;
1565 break;
1566 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001567 if (iopl < 3)
1568 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001569 change_mask |= EFLG_IF;
1570 break;
1571 default: /* real mode */
1572 change_mask |= (EFLG_IOPL | EFLG_IF);
1573 break;
1574 }
1575
1576 *(unsigned long *)dest =
1577 (ctxt->eflags & ~change_mask) | (val & change_mask);
1578
1579 return rc;
1580}
1581
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001582static int em_popf(struct x86_emulate_ctxt *ctxt)
1583{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001584 ctxt->dst.type = OP_REG;
1585 ctxt->dst.addr.reg = &ctxt->eflags;
1586 ctxt->dst.bytes = ctxt->op_bytes;
1587 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001588}
1589
Avi Kivityf47cfa32012-06-07 17:49:24 +03001590static int em_leave(struct x86_emulate_ctxt *ctxt)
1591{
1592 assign_masked(&ctxt->regs[VCPU_REGS_RSP], ctxt->regs[VCPU_REGS_RBP],
1593 stack_mask(ctxt));
1594 return emulate_pop(ctxt, &ctxt->regs[VCPU_REGS_RBP], ctxt->op_bytes);
1595}
1596
Avi Kivity1cd196e2011-09-13 10:45:51 +03001597static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001598{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001599 int seg = ctxt->src2.val;
1600
Avi Kivity9dac77f2011-06-01 15:34:25 +03001601 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001602
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001603 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001604}
1605
Avi Kivity1cd196e2011-09-13 10:45:51 +03001606static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001607{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001608 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001609 unsigned long selector;
1610 int rc;
1611
Avi Kivity9dac77f2011-06-01 15:34:25 +03001612 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001613 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001614 return rc;
1615
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001616 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001617 return rc;
1618}
1619
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001620static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001621{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001622 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001623 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001624 int reg = VCPU_REGS_RAX;
1625
1626 while (reg <= VCPU_REGS_RDI) {
1627 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001628 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001629
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001630 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001631 if (rc != X86EMUL_CONTINUE)
1632 return rc;
1633
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001634 ++reg;
1635 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001636
Wei Yongjunc37eda12010-06-15 09:03:33 +08001637 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001638}
1639
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001640static int em_pushf(struct x86_emulate_ctxt *ctxt)
1641{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001642 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001643 return em_push(ctxt);
1644}
1645
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001646static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001647{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001648 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001649 int reg = VCPU_REGS_RDI;
1650
1651 while (reg >= VCPU_REGS_RAX) {
1652 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001653 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1654 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001655 --reg;
1656 }
1657
Avi Kivity9dac77f2011-06-01 15:34:25 +03001658 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001659 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001660 break;
1661 --reg;
1662 }
1663 return rc;
1664}
1665
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001666int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001667{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001668 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001669 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001670 struct desc_ptr dt;
1671 gva_t cs_addr;
1672 gva_t eip_addr;
1673 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001674
1675 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001676 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001677 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001678 if (rc != X86EMUL_CONTINUE)
1679 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001680
1681 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1682
Avi Kivity9dac77f2011-06-01 15:34:25 +03001683 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001684 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001685 if (rc != X86EMUL_CONTINUE)
1686 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001687
Avi Kivity9dac77f2011-06-01 15:34:25 +03001688 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001689 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001690 if (rc != X86EMUL_CONTINUE)
1691 return rc;
1692
Avi Kivity4bff1e862011-04-20 13:37:53 +03001693 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001694
1695 eip_addr = dt.address + (irq << 2);
1696 cs_addr = dt.address + (irq << 2) + 2;
1697
Avi Kivity0f65dd72011-04-20 13:37:53 +03001698 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001699 if (rc != X86EMUL_CONTINUE)
1700 return rc;
1701
Avi Kivity0f65dd72011-04-20 13:37:53 +03001702 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001703 if (rc != X86EMUL_CONTINUE)
1704 return rc;
1705
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001706 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001707 if (rc != X86EMUL_CONTINUE)
1708 return rc;
1709
Avi Kivity9dac77f2011-06-01 15:34:25 +03001710 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001711
1712 return rc;
1713}
1714
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001715static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001716{
1717 switch(ctxt->mode) {
1718 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001719 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001720 case X86EMUL_MODE_VM86:
1721 case X86EMUL_MODE_PROT16:
1722 case X86EMUL_MODE_PROT32:
1723 case X86EMUL_MODE_PROT64:
1724 default:
1725 /* Protected mode interrupts unimplemented yet */
1726 return X86EMUL_UNHANDLEABLE;
1727 }
1728}
1729
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001730static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001731{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001732 int rc = X86EMUL_CONTINUE;
1733 unsigned long temp_eip = 0;
1734 unsigned long temp_eflags = 0;
1735 unsigned long cs = 0;
1736 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1737 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1738 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1739 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1740
1741 /* TODO: Add stack limit check */
1742
Avi Kivity9dac77f2011-06-01 15:34:25 +03001743 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001744
1745 if (rc != X86EMUL_CONTINUE)
1746 return rc;
1747
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001748 if (temp_eip & ~0xffff)
1749 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001750
Avi Kivity9dac77f2011-06-01 15:34:25 +03001751 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001752
1753 if (rc != X86EMUL_CONTINUE)
1754 return rc;
1755
Avi Kivity9dac77f2011-06-01 15:34:25 +03001756 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001757
1758 if (rc != X86EMUL_CONTINUE)
1759 return rc;
1760
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001761 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001762
1763 if (rc != X86EMUL_CONTINUE)
1764 return rc;
1765
Avi Kivity9dac77f2011-06-01 15:34:25 +03001766 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001767
1768
Avi Kivity9dac77f2011-06-01 15:34:25 +03001769 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001770 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001771 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001772 ctxt->eflags &= ~0xffff;
1773 ctxt->eflags |= temp_eflags;
1774 }
1775
1776 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1777 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1778
1779 return rc;
1780}
1781
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001782static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001783{
1784 switch(ctxt->mode) {
1785 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001786 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001787 case X86EMUL_MODE_VM86:
1788 case X86EMUL_MODE_PROT16:
1789 case X86EMUL_MODE_PROT32:
1790 case X86EMUL_MODE_PROT64:
1791 default:
1792 /* iret from protected mode unimplemented yet */
1793 return X86EMUL_UNHANDLEABLE;
1794 }
1795}
1796
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001797static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1798{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001799 int rc;
1800 unsigned short sel;
1801
Avi Kivity9dac77f2011-06-01 15:34:25 +03001802 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001803
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001804 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001805 if (rc != X86EMUL_CONTINUE)
1806 return rc;
1807
Avi Kivity9dac77f2011-06-01 15:34:25 +03001808 ctxt->_eip = 0;
1809 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001810 return X86EMUL_CONTINUE;
1811}
1812
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001813static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001814{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001815 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001816 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001817 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001818 break;
1819 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001820 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001821 break;
1822 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001823 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001824 break;
1825 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001826 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001827 break;
1828 case 4: /* sal/shl */
1829 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001830 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001831 break;
1832 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001833 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001834 break;
1835 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001836 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001837 break;
1838 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001839 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001840}
1841
Avi Kivity3329ece2011-09-13 10:45:39 +03001842static int em_not(struct x86_emulate_ctxt *ctxt)
1843{
1844 ctxt->dst.val = ~ctxt->dst.val;
1845 return X86EMUL_CONTINUE;
1846}
1847
1848static int em_neg(struct x86_emulate_ctxt *ctxt)
1849{
1850 emulate_1op(ctxt, "neg");
1851 return X86EMUL_CONTINUE;
1852}
1853
1854static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1855{
1856 u8 ex = 0;
1857
1858 emulate_1op_rax_rdx(ctxt, "mul", ex);
1859 return X86EMUL_CONTINUE;
1860}
1861
1862static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1863{
1864 u8 ex = 0;
1865
1866 emulate_1op_rax_rdx(ctxt, "imul", ex);
1867 return X86EMUL_CONTINUE;
1868}
1869
1870static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001871{
Avi Kivity34d1f492010-08-26 11:59:01 +03001872 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001873
Avi Kivity3329ece2011-09-13 10:45:39 +03001874 emulate_1op_rax_rdx(ctxt, "div", de);
1875 if (de)
1876 return emulate_de(ctxt);
1877 return X86EMUL_CONTINUE;
1878}
1879
1880static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1881{
1882 u8 de = 0;
1883
1884 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001885 if (de)
1886 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001887 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001888}
1889
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001890static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001891{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001892 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001893
Avi Kivity9dac77f2011-06-01 15:34:25 +03001894 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001895 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001896 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001897 break;
1898 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001899 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001901 case 2: /* call near abs */ {
1902 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001903 old_eip = ctxt->_eip;
1904 ctxt->_eip = ctxt->src.val;
1905 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001906 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001907 break;
1908 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001909 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001910 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001911 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001912 case 5: /* jmp far */
1913 rc = em_jmp_far(ctxt);
1914 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001915 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001916 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001917 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001918 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001919 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001920}
1921
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001922static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001924 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001925
Avi Kivity9dac77f2011-06-01 15:34:25 +03001926 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1927 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1928 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1929 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001930 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001931 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001932 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1933 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001934
Laurent Vivier05f086f2007-09-24 11:10:55 +02001935 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001936 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001937 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001938}
1939
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001940static int em_ret(struct x86_emulate_ctxt *ctxt)
1941{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001942 ctxt->dst.type = OP_REG;
1943 ctxt->dst.addr.reg = &ctxt->_eip;
1944 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001945 return em_pop(ctxt);
1946}
1947
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001948static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001949{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001950 int rc;
1951 unsigned long cs;
1952
Avi Kivity9dac77f2011-06-01 15:34:25 +03001953 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001954 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001955 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001956 if (ctxt->op_bytes == 4)
1957 ctxt->_eip = (u32)ctxt->_eip;
1958 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001959 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001960 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001961 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001962 return rc;
1963}
1964
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09001965static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
1966{
1967 /* Save real source value, then compare EAX against destination. */
1968 ctxt->src.orig_val = ctxt->src.val;
1969 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
1970 emulate_2op_SrcV(ctxt, "cmp");
1971
1972 if (ctxt->eflags & EFLG_ZF) {
1973 /* Success: write back to memory. */
1974 ctxt->dst.val = ctxt->src.orig_val;
1975 } else {
1976 /* Failure: write the value we saw to EAX. */
1977 ctxt->dst.type = OP_REG;
1978 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
1979 }
1980 return X86EMUL_CONTINUE;
1981}
1982
Avi Kivityd4b43252011-09-13 10:45:50 +03001983static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001984{
Avi Kivityd4b43252011-09-13 10:45:50 +03001985 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001986 unsigned short sel;
1987 int rc;
1988
Avi Kivity9dac77f2011-06-01 15:34:25 +03001989 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001990
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001991 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001992 if (rc != X86EMUL_CONTINUE)
1993 return rc;
1994
Avi Kivity9dac77f2011-06-01 15:34:25 +03001995 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001996 return rc;
1997}
1998
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001999static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002000setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002001 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002002{
Avi Kivity1aa36612011-04-27 13:20:30 +03002003 u16 selector;
2004
Gleb Natapov79168fd2010-04-28 19:15:30 +03002005 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002006 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002007 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002008
2009 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002010 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002011 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002012 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002013 cs->type = 0x0b; /* Read, Execute, Accessed */
2014 cs->s = 1;
2015 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002016 cs->p = 1;
2017 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002018
Gleb Natapov79168fd2010-04-28 19:15:30 +03002019 set_desc_base(ss, 0); /* flat segment */
2020 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002021 ss->g = 1; /* 4kb granularity */
2022 ss->s = 1;
2023 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002024 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002025 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002026 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002027}
2028
Avi Kivity1a18a692012-02-01 12:23:21 +02002029static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2030{
2031 u32 eax, ebx, ecx, edx;
2032
2033 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002034 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2035 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002036 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2037 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2038}
2039
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002040static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2041{
2042 struct x86_emulate_ops *ops = ctxt->ops;
2043 u32 eax, ebx, ecx, edx;
2044
2045 /*
2046 * syscall should always be enabled in longmode - so only become
2047 * vendor specific (cpuid) if other modes are active...
2048 */
2049 if (ctxt->mode == X86EMUL_MODE_PROT64)
2050 return true;
2051
2052 eax = 0x00000000;
2053 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002054 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2055 /*
2056 * Intel ("GenuineIntel")
2057 * remark: Intel CPUs only support "syscall" in 64bit
2058 * longmode. Also an 64bit guest with a
2059 * 32bit compat-app running will #UD !! While this
2060 * behaviour can be fixed (by emulating) into AMD
2061 * response - CPUs of AMD can't behave like Intel.
2062 */
2063 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2064 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2065 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2066 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002067
Avi Kivity0017f932012-06-07 14:10:16 +03002068 /* AMD ("AuthenticAMD") */
2069 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2070 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2071 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2072 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002073
Avi Kivity0017f932012-06-07 14:10:16 +03002074 /* AMD ("AMDisbetter!") */
2075 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2076 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2077 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2078 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002079
2080 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2081 return false;
2082}
2083
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002084static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002085{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002086 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002087 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002088 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002089 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002090 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002091
2092 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002093 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002094 ctxt->mode == X86EMUL_MODE_VM86)
2095 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002096
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002097 if (!(em_syscall_is_enabled(ctxt)))
2098 return emulate_ud(ctxt);
2099
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002100 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002101 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002102
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002103 if (!(efer & EFER_SCE))
2104 return emulate_ud(ctxt);
2105
Avi Kivity717746e2011-04-20 13:37:53 +03002106 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002107 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002108 cs_sel = (u16)(msr_data & 0xfffc);
2109 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002110
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002111 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002112 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002113 cs.l = 1;
2114 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002115 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2116 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002117
Avi Kivity9dac77f2011-06-01 15:34:25 +03002118 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002119 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002120#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03002121 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002122
Avi Kivity717746e2011-04-20 13:37:53 +03002123 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002124 ctxt->mode == X86EMUL_MODE_PROT64 ?
2125 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002126 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002127
Avi Kivity717746e2011-04-20 13:37:53 +03002128 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002129 ctxt->eflags &= ~(msr_data | EFLG_RF);
2130#endif
2131 } else {
2132 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002133 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002134 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002135
2136 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2137 }
2138
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002139 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002140}
2141
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002142static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002143{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002144 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002145 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002146 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002147 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002148 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002149
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002150 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002151 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002152 if (ctxt->mode == X86EMUL_MODE_REAL)
2153 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002154
Avi Kivity1a18a692012-02-01 12:23:21 +02002155 /*
2156 * Not recognized on AMD in compat mode (but is recognized in legacy
2157 * mode).
2158 */
2159 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2160 && !vendor_intel(ctxt))
2161 return emulate_ud(ctxt);
2162
Andre Przywara8c604352009-06-18 12:56:01 +02002163 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2164 * Therefore, we inject an #UD.
2165 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002166 if (ctxt->mode == X86EMUL_MODE_PROT64)
2167 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002168
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002169 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002170
Avi Kivity717746e2011-04-20 13:37:53 +03002171 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002172 switch (ctxt->mode) {
2173 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002174 if ((msr_data & 0xfffc) == 0x0)
2175 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002176 break;
2177 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002178 if (msr_data == 0x0)
2179 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002180 break;
2181 }
2182
2183 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002184 cs_sel = (u16)msr_data;
2185 cs_sel &= ~SELECTOR_RPL_MASK;
2186 ss_sel = cs_sel + 8;
2187 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002188 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002189 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002190 cs.l = 1;
2191 }
2192
Avi Kivity1aa36612011-04-27 13:20:30 +03002193 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2194 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002195
Avi Kivity717746e2011-04-20 13:37:53 +03002196 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002197 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002198
Avi Kivity717746e2011-04-20 13:37:53 +03002199 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002200 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002201
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002202 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002203}
2204
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002205static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002206{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002207 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002208 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002209 u64 msr_data;
2210 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002211 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002212
Gleb Natapova0044752010-02-10 14:21:31 +02002213 /* inject #GP if in real mode or Virtual 8086 mode */
2214 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002215 ctxt->mode == X86EMUL_MODE_VM86)
2216 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002217
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002218 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002219
Avi Kivity9dac77f2011-06-01 15:34:25 +03002220 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002221 usermode = X86EMUL_MODE_PROT64;
2222 else
2223 usermode = X86EMUL_MODE_PROT32;
2224
2225 cs.dpl = 3;
2226 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002227 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002228 switch (usermode) {
2229 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002230 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002231 if ((msr_data & 0xfffc) == 0x0)
2232 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002233 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002234 break;
2235 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002236 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002237 if (msr_data == 0x0)
2238 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002239 ss_sel = cs_sel + 8;
2240 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002241 cs.l = 1;
2242 break;
2243 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002244 cs_sel |= SELECTOR_RPL_MASK;
2245 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002246
Avi Kivity1aa36612011-04-27 13:20:30 +03002247 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2248 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002249
Avi Kivity9dac77f2011-06-01 15:34:25 +03002250 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2251 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002252
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002253 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002254}
2255
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002256static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002257{
2258 int iopl;
2259 if (ctxt->mode == X86EMUL_MODE_REAL)
2260 return false;
2261 if (ctxt->mode == X86EMUL_MODE_VM86)
2262 return true;
2263 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002264 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002265}
2266
2267static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002268 u16 port, u16 len)
2269{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002270 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002271 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002272 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002273 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002274 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002275 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002276 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002277
Avi Kivity1aa36612011-04-27 13:20:30 +03002278 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002279 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002280 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002281 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002282 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002283 base = get_desc_base(&tr_seg);
2284#ifdef CONFIG_X86_64
2285 base |= ((u64)base3) << 32;
2286#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002287 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002288 if (r != X86EMUL_CONTINUE)
2289 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002290 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002291 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002292 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002293 if (r != X86EMUL_CONTINUE)
2294 return false;
2295 if ((perm >> bit_idx) & mask)
2296 return false;
2297 return true;
2298}
2299
2300static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002301 u16 port, u16 len)
2302{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002303 if (ctxt->perm_ok)
2304 return true;
2305
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002306 if (emulator_bad_iopl(ctxt))
2307 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002308 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002309
2310 ctxt->perm_ok = true;
2311
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002312 return true;
2313}
2314
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002315static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002316 struct tss_segment_16 *tss)
2317{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002318 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002319 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002320 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2321 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2322 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2323 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2324 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2325 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2326 tss->si = ctxt->regs[VCPU_REGS_RSI];
2327 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328
Avi Kivity1aa36612011-04-27 13:20:30 +03002329 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2330 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2331 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2332 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2333 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002334}
2335
2336static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337 struct tss_segment_16 *tss)
2338{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339 int ret;
2340
Avi Kivity9dac77f2011-06-01 15:34:25 +03002341 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002342 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002343 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2344 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2345 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2346 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2347 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2348 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2349 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2350 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002351
2352 /*
2353 * SDM says that segment selectors are loaded before segment
2354 * descriptors
2355 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002356 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2357 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2358 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2359 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2360 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002361
2362 /*
2363 * Now load segment descriptors. If fault happenes at this stage
2364 * it is handled in a context of new task
2365 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002366 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002367 if (ret != X86EMUL_CONTINUE)
2368 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002369 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002370 if (ret != X86EMUL_CONTINUE)
2371 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002372 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002373 if (ret != X86EMUL_CONTINUE)
2374 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002375 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002376 if (ret != X86EMUL_CONTINUE)
2377 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002378 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002379 if (ret != X86EMUL_CONTINUE)
2380 return ret;
2381
2382 return X86EMUL_CONTINUE;
2383}
2384
2385static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002386 u16 tss_selector, u16 old_tss_sel,
2387 ulong old_tss_base, struct desc_struct *new_desc)
2388{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002389 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002390 struct tss_segment_16 tss_seg;
2391 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002392 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002393
Avi Kivity0f65dd72011-04-20 13:37:53 +03002394 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002395 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002396 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002398 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002400 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002401
Avi Kivity0f65dd72011-04-20 13:37:53 +03002402 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002403 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002404 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407
Avi Kivity0f65dd72011-04-20 13:37:53 +03002408 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002409 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002410 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002413
2414 if (old_tss_sel != 0xffff) {
2415 tss_seg.prev_task_link = old_tss_sel;
2416
Avi Kivity0f65dd72011-04-20 13:37:53 +03002417 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418 &tss_seg.prev_task_link,
2419 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002420 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002421 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002423 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002424 }
2425
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002426 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427}
2428
2429static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 struct tss_segment_32 *tss)
2431{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002432 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002433 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002435 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2436 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2437 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2438 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2439 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2440 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2441 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2442 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443
Avi Kivity1aa36612011-04-27 13:20:30 +03002444 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2445 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2446 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2447 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2448 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2449 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2450 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002451}
2452
2453static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454 struct tss_segment_32 *tss)
2455{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002456 int ret;
2457
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002458 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002459 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002460 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002462
2463 /* General purpose registers */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002464 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2465 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2466 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2467 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2468 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2469 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2470 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2471 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472
2473 /*
2474 * SDM says that segment selectors are loaded before segment
2475 * descriptors
2476 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002477 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2478 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2479 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2480 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2481 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2482 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2483 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002484
2485 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002486 * If we're switching between Protected Mode and VM86, we need to make
2487 * sure to update the mode before loading the segment descriptors so
2488 * that the selectors are interpreted correctly.
2489 *
2490 * Need to get rflags to the vcpu struct immediately because it
2491 * influences the CPL which is checked at least when loading the segment
2492 * descriptors and when pushing an error code to the new kernel stack.
2493 *
2494 * TODO Introduce a separate ctxt->ops->set_cpl callback
2495 */
2496 if (ctxt->eflags & X86_EFLAGS_VM)
2497 ctxt->mode = X86EMUL_MODE_VM86;
2498 else
2499 ctxt->mode = X86EMUL_MODE_PROT32;
2500
2501 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2502
2503 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504 * Now load segment descriptors. If fault happenes at this stage
2505 * it is handled in a context of new task
2506 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002507 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002508 if (ret != X86EMUL_CONTINUE)
2509 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002510 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 if (ret != X86EMUL_CONTINUE)
2512 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002513 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514 if (ret != X86EMUL_CONTINUE)
2515 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002516 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517 if (ret != X86EMUL_CONTINUE)
2518 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002519 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520 if (ret != X86EMUL_CONTINUE)
2521 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002522 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523 if (ret != X86EMUL_CONTINUE)
2524 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002525 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526 if (ret != X86EMUL_CONTINUE)
2527 return ret;
2528
2529 return X86EMUL_CONTINUE;
2530}
2531
2532static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002533 u16 tss_selector, u16 old_tss_sel,
2534 ulong old_tss_base, struct desc_struct *new_desc)
2535{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002536 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002537 struct tss_segment_32 tss_seg;
2538 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002539 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540
Avi Kivity0f65dd72011-04-20 13:37:53 +03002541 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002542 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002543 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002547 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548
Avi Kivity0f65dd72011-04-20 13:37:53 +03002549 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002550 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002551 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554
Avi Kivity0f65dd72011-04-20 13:37:53 +03002555 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002556 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002557 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560
2561 if (old_tss_sel != 0xffff) {
2562 tss_seg.prev_task_link = old_tss_sel;
2563
Avi Kivity0f65dd72011-04-20 13:37:53 +03002564 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565 &tss_seg.prev_task_link,
2566 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002567 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002568 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571 }
2572
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002573 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574}
2575
2576static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002577 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002578 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002580 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581 struct desc_struct curr_tss_desc, next_tss_desc;
2582 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002583 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002585 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002586 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587
2588 /* FIXME: old_tss_base == ~0 ? */
2589
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002590 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 if (ret != X86EMUL_CONTINUE)
2592 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002593 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 if (ret != X86EMUL_CONTINUE)
2595 return ret;
2596
2597 /* FIXME: check that next_tss_desc is tss */
2598
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002599 /*
2600 * Check privileges. The three cases are task switch caused by...
2601 *
2602 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2603 * 2. Exception/IRQ/iret: No check is performed
2604 * 3. jmp/call to TSS: Check agains DPL of the TSS
2605 */
2606 if (reason == TASK_SWITCH_GATE) {
2607 if (idt_index != -1) {
2608 /* Software interrupts */
2609 struct desc_struct task_gate_desc;
2610 int dpl;
2611
2612 ret = read_interrupt_descriptor(ctxt, idt_index,
2613 &task_gate_desc);
2614 if (ret != X86EMUL_CONTINUE)
2615 return ret;
2616
2617 dpl = task_gate_desc.dpl;
2618 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2619 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2620 }
2621 } else if (reason != TASK_SWITCH_IRET) {
2622 int dpl = next_tss_desc.dpl;
2623 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2624 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625 }
2626
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002627
Gleb Natapovceffb452010-03-18 15:20:19 +02002628 desc_limit = desc_limit_scaled(&next_tss_desc);
2629 if (!next_tss_desc.p ||
2630 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2631 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002632 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 return X86EMUL_PROPAGATE_FAULT;
2634 }
2635
2636 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2637 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002638 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 }
2640
2641 if (reason == TASK_SWITCH_IRET)
2642 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2643
2644 /* set back link to prev task only if NT bit is set in eflags
2645 note that old_tss_sel is not used afetr this point */
2646 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2647 old_tss_sel = 0xffff;
2648
2649 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002650 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 old_tss_base, &next_tss_desc);
2652 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002653 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002655 if (ret != X86EMUL_CONTINUE)
2656 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657
2658 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2659 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2660
2661 if (reason != TASK_SWITCH_IRET) {
2662 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002663 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 }
2665
Avi Kivity717746e2011-04-20 13:37:53 +03002666 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002667 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668
Jan Kiszkae269fb22010-04-14 15:51:09 +02002669 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002670 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2671 ctxt->lock_prefix = 0;
2672 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002673 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002674 }
2675
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 return ret;
2677}
2678
2679int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002680 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002681 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002683 int rc;
2684
Avi Kivity9dac77f2011-06-01 15:34:25 +03002685 ctxt->_eip = ctxt->eip;
2686 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002688 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002689 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002691 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002692 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002693
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002694 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002695}
2696
Avi Kivity90de84f2010-11-17 15:28:21 +02002697static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002698 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002699{
Gleb Natapova682e352010-03-18 15:20:21 +02002700 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2701
Avi Kivity9dac77f2011-06-01 15:34:25 +03002702 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2703 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002704 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002705}
2706
Avi Kivity7af04fc2010-08-18 14:16:35 +03002707static int em_das(struct x86_emulate_ctxt *ctxt)
2708{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002709 u8 al, old_al;
2710 bool af, cf, old_cf;
2711
2712 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002713 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002714
2715 old_al = al;
2716 old_cf = cf;
2717 cf = false;
2718 af = ctxt->eflags & X86_EFLAGS_AF;
2719 if ((al & 0x0f) > 9 || af) {
2720 al -= 6;
2721 cf = old_cf | (al >= 250);
2722 af = true;
2723 } else {
2724 af = false;
2725 }
2726 if (old_al > 0x99 || old_cf) {
2727 al -= 0x60;
2728 cf = true;
2729 }
2730
Avi Kivity9dac77f2011-06-01 15:34:25 +03002731 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002732 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002733 ctxt->src.type = OP_IMM;
2734 ctxt->src.val = 0;
2735 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002736 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002737 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2738 if (cf)
2739 ctxt->eflags |= X86_EFLAGS_CF;
2740 if (af)
2741 ctxt->eflags |= X86_EFLAGS_AF;
2742 return X86EMUL_CONTINUE;
2743}
2744
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002745static int em_call(struct x86_emulate_ctxt *ctxt)
2746{
2747 long rel = ctxt->src.val;
2748
2749 ctxt->src.val = (unsigned long)ctxt->_eip;
2750 jmp_rel(ctxt, rel);
2751 return em_push(ctxt);
2752}
2753
Avi Kivity0ef753b2010-08-18 14:51:45 +03002754static int em_call_far(struct x86_emulate_ctxt *ctxt)
2755{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002756 u16 sel, old_cs;
2757 ulong old_eip;
2758 int rc;
2759
Avi Kivity1aa36612011-04-27 13:20:30 +03002760 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002761 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002762
Avi Kivity9dac77f2011-06-01 15:34:25 +03002763 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002764 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002765 return X86EMUL_CONTINUE;
2766
Avi Kivity9dac77f2011-06-01 15:34:25 +03002767 ctxt->_eip = 0;
2768 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002769
Avi Kivity9dac77f2011-06-01 15:34:25 +03002770 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002771 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002772 if (rc != X86EMUL_CONTINUE)
2773 return rc;
2774
Avi Kivity9dac77f2011-06-01 15:34:25 +03002775 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002776 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002777}
2778
Avi Kivity40ece7c2010-08-18 15:12:09 +03002779static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2780{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002781 int rc;
2782
Avi Kivity9dac77f2011-06-01 15:34:25 +03002783 ctxt->dst.type = OP_REG;
2784 ctxt->dst.addr.reg = &ctxt->_eip;
2785 ctxt->dst.bytes = ctxt->op_bytes;
2786 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002787 if (rc != X86EMUL_CONTINUE)
2788 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002789 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002790 return X86EMUL_CONTINUE;
2791}
2792
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002793static int em_add(struct x86_emulate_ctxt *ctxt)
2794{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002795 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002796 return X86EMUL_CONTINUE;
2797}
2798
2799static int em_or(struct x86_emulate_ctxt *ctxt)
2800{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002801 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002802 return X86EMUL_CONTINUE;
2803}
2804
2805static int em_adc(struct x86_emulate_ctxt *ctxt)
2806{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002807 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002808 return X86EMUL_CONTINUE;
2809}
2810
2811static int em_sbb(struct x86_emulate_ctxt *ctxt)
2812{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002813 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002814 return X86EMUL_CONTINUE;
2815}
2816
2817static int em_and(struct x86_emulate_ctxt *ctxt)
2818{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002819 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002820 return X86EMUL_CONTINUE;
2821}
2822
2823static int em_sub(struct x86_emulate_ctxt *ctxt)
2824{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002825 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002826 return X86EMUL_CONTINUE;
2827}
2828
2829static int em_xor(struct x86_emulate_ctxt *ctxt)
2830{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002831 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002832 return X86EMUL_CONTINUE;
2833}
2834
2835static int em_cmp(struct x86_emulate_ctxt *ctxt)
2836{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002837 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002838 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002839 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002840 return X86EMUL_CONTINUE;
2841}
2842
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002843static int em_test(struct x86_emulate_ctxt *ctxt)
2844{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002845 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002846 /* Disable writeback. */
2847 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002848 return X86EMUL_CONTINUE;
2849}
2850
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002851static int em_xchg(struct x86_emulate_ctxt *ctxt)
2852{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002853 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002854 ctxt->src.val = ctxt->dst.val;
2855 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002856
2857 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002858 ctxt->dst.val = ctxt->src.orig_val;
2859 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002860 return X86EMUL_CONTINUE;
2861}
2862
Avi Kivity5c82aa22010-08-18 18:31:43 +03002863static int em_imul(struct x86_emulate_ctxt *ctxt)
2864{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002865 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002866 return X86EMUL_CONTINUE;
2867}
2868
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002869static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2870{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002871 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002872 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002873}
2874
Avi Kivity61429142010-08-19 15:13:00 +03002875static int em_cwd(struct x86_emulate_ctxt *ctxt)
2876{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002877 ctxt->dst.type = OP_REG;
2878 ctxt->dst.bytes = ctxt->src.bytes;
2879 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2880 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002881
2882 return X86EMUL_CONTINUE;
2883}
2884
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002885static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2886{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002887 u64 tsc = 0;
2888
Avi Kivity717746e2011-04-20 13:37:53 +03002889 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002890 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2891 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002892 return X86EMUL_CONTINUE;
2893}
2894
Avi Kivity222d21a2011-11-10 14:57:30 +02002895static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2896{
2897 u64 pmc;
2898
2899 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2900 return emulate_gp(ctxt, 0);
2901 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2902 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2903 return X86EMUL_CONTINUE;
2904}
2905
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002906static int em_mov(struct x86_emulate_ctxt *ctxt)
2907{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002908 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002909 return X86EMUL_CONTINUE;
2910}
2911
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002912static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2913{
2914 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2915 return emulate_gp(ctxt, 0);
2916
2917 /* Disable writeback. */
2918 ctxt->dst.type = OP_NONE;
2919 return X86EMUL_CONTINUE;
2920}
2921
2922static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2923{
2924 unsigned long val;
2925
2926 if (ctxt->mode == X86EMUL_MODE_PROT64)
2927 val = ctxt->src.val & ~0ULL;
2928 else
2929 val = ctxt->src.val & ~0U;
2930
2931 /* #UD condition is already handled. */
2932 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2933 return emulate_gp(ctxt, 0);
2934
2935 /* Disable writeback. */
2936 ctxt->dst.type = OP_NONE;
2937 return X86EMUL_CONTINUE;
2938}
2939
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002940static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2941{
2942 u64 msr_data;
2943
2944 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2945 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2946 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2947 return emulate_gp(ctxt, 0);
2948
2949 return X86EMUL_CONTINUE;
2950}
2951
2952static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2953{
2954 u64 msr_data;
2955
2956 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2957 return emulate_gp(ctxt, 0);
2958
2959 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2960 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2961 return X86EMUL_CONTINUE;
2962}
2963
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002964static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2965{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002966 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002967 return emulate_ud(ctxt);
2968
Avi Kivity9dac77f2011-06-01 15:34:25 +03002969 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002970 return X86EMUL_CONTINUE;
2971}
2972
2973static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2974{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002975 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002976
Avi Kivity9dac77f2011-06-01 15:34:25 +03002977 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002978 return emulate_ud(ctxt);
2979
Avi Kivity9dac77f2011-06-01 15:34:25 +03002980 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002981 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2982
2983 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002984 ctxt->dst.type = OP_NONE;
2985 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002986}
2987
Avi Kivity38503912011-03-31 18:48:09 +02002988static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2989{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002990 int rc;
2991 ulong linear;
2992
Avi Kivity9dac77f2011-06-01 15:34:25 +03002993 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002994 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002995 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002996 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002997 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002998 return X86EMUL_CONTINUE;
2999}
3000
Avi Kivity2d04a052011-04-20 15:32:49 +03003001static int em_clts(struct x86_emulate_ctxt *ctxt)
3002{
3003 ulong cr0;
3004
3005 cr0 = ctxt->ops->get_cr(ctxt, 0);
3006 cr0 &= ~X86_CR0_TS;
3007 ctxt->ops->set_cr(ctxt, 0, cr0);
3008 return X86EMUL_CONTINUE;
3009}
3010
Avi Kivity26d05cc2011-04-21 12:07:59 +03003011static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3012{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003013 int rc;
3014
Avi Kivity9dac77f2011-06-01 15:34:25 +03003015 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003016 return X86EMUL_UNHANDLEABLE;
3017
3018 rc = ctxt->ops->fix_hypercall(ctxt);
3019 if (rc != X86EMUL_CONTINUE)
3020 return rc;
3021
3022 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003023 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003024 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003025 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003026 return X86EMUL_CONTINUE;
3027}
3028
Avi Kivity96051572012-06-10 17:21:18 +03003029static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3030 void (*get)(struct x86_emulate_ctxt *ctxt,
3031 struct desc_ptr *ptr))
3032{
3033 struct desc_ptr desc_ptr;
3034
3035 if (ctxt->mode == X86EMUL_MODE_PROT64)
3036 ctxt->op_bytes = 8;
3037 get(ctxt, &desc_ptr);
3038 if (ctxt->op_bytes == 2) {
3039 ctxt->op_bytes = 4;
3040 desc_ptr.address &= 0x00ffffff;
3041 }
3042 /* Disable writeback. */
3043 ctxt->dst.type = OP_NONE;
3044 return segmented_write(ctxt, ctxt->dst.addr.mem,
3045 &desc_ptr, 2 + ctxt->op_bytes);
3046}
3047
3048static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3049{
3050 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3051}
3052
3053static int em_sidt(struct x86_emulate_ctxt *ctxt)
3054{
3055 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3056}
3057
Avi Kivity26d05cc2011-04-21 12:07:59 +03003058static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3059{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003060 struct desc_ptr desc_ptr;
3061 int rc;
3062
Avi Kivity510425f2012-06-07 17:04:36 +03003063 if (ctxt->mode == X86EMUL_MODE_PROT64)
3064 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003065 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003066 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003067 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003068 if (rc != X86EMUL_CONTINUE)
3069 return rc;
3070 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3071 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003073 return X86EMUL_CONTINUE;
3074}
3075
Avi Kivity5ef39c72011-04-21 12:21:50 +03003076static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003077{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003078 int rc;
3079
Avi Kivity5ef39c72011-04-21 12:21:50 +03003080 rc = ctxt->ops->fix_hypercall(ctxt);
3081
Avi Kivity26d05cc2011-04-21 12:07:59 +03003082 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003084 return rc;
3085}
3086
3087static int em_lidt(struct x86_emulate_ctxt *ctxt)
3088{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003089 struct desc_ptr desc_ptr;
3090 int rc;
3091
Avi Kivity510425f2012-06-07 17:04:36 +03003092 if (ctxt->mode == X86EMUL_MODE_PROT64)
3093 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003094 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003095 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003096 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003097 if (rc != X86EMUL_CONTINUE)
3098 return rc;
3099 ctxt->ops->set_idt(ctxt, &desc_ptr);
3100 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003101 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003102 return X86EMUL_CONTINUE;
3103}
3104
3105static int em_smsw(struct x86_emulate_ctxt *ctxt)
3106{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003107 ctxt->dst.bytes = 2;
3108 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003109 return X86EMUL_CONTINUE;
3110}
3111
3112static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3113{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003114 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003115 | (ctxt->src.val & 0x0f));
3116 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003117 return X86EMUL_CONTINUE;
3118}
3119
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003120static int em_loop(struct x86_emulate_ctxt *ctxt)
3121{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003122 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
3123 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
3124 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3125 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003126
3127 return X86EMUL_CONTINUE;
3128}
3129
3130static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3131{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003132 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
3133 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003134
3135 return X86EMUL_CONTINUE;
3136}
3137
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003138static int em_in(struct x86_emulate_ctxt *ctxt)
3139{
3140 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3141 &ctxt->dst.val))
3142 return X86EMUL_IO_NEEDED;
3143
3144 return X86EMUL_CONTINUE;
3145}
3146
3147static int em_out(struct x86_emulate_ctxt *ctxt)
3148{
3149 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3150 &ctxt->src.val, 1);
3151 /* Disable writeback. */
3152 ctxt->dst.type = OP_NONE;
3153 return X86EMUL_CONTINUE;
3154}
3155
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003156static int em_cli(struct x86_emulate_ctxt *ctxt)
3157{
3158 if (emulator_bad_iopl(ctxt))
3159 return emulate_gp(ctxt, 0);
3160
3161 ctxt->eflags &= ~X86_EFLAGS_IF;
3162 return X86EMUL_CONTINUE;
3163}
3164
3165static int em_sti(struct x86_emulate_ctxt *ctxt)
3166{
3167 if (emulator_bad_iopl(ctxt))
3168 return emulate_gp(ctxt, 0);
3169
3170 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3171 ctxt->eflags |= X86_EFLAGS_IF;
3172 return X86EMUL_CONTINUE;
3173}
3174
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003175static int em_bt(struct x86_emulate_ctxt *ctxt)
3176{
3177 /* Disable writeback. */
3178 ctxt->dst.type = OP_NONE;
3179 /* only subword offset */
3180 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3181
3182 emulate_2op_SrcV_nobyte(ctxt, "bt");
3183 return X86EMUL_CONTINUE;
3184}
3185
3186static int em_bts(struct x86_emulate_ctxt *ctxt)
3187{
3188 emulate_2op_SrcV_nobyte(ctxt, "bts");
3189 return X86EMUL_CONTINUE;
3190}
3191
3192static int em_btr(struct x86_emulate_ctxt *ctxt)
3193{
3194 emulate_2op_SrcV_nobyte(ctxt, "btr");
3195 return X86EMUL_CONTINUE;
3196}
3197
3198static int em_btc(struct x86_emulate_ctxt *ctxt)
3199{
3200 emulate_2op_SrcV_nobyte(ctxt, "btc");
3201 return X86EMUL_CONTINUE;
3202}
3203
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003204static int em_bsf(struct x86_emulate_ctxt *ctxt)
3205{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003206 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003207 return X86EMUL_CONTINUE;
3208}
3209
3210static int em_bsr(struct x86_emulate_ctxt *ctxt)
3211{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003212 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003213 return X86EMUL_CONTINUE;
3214}
3215
Avi Kivity6d6eede2012-06-07 14:11:36 +03003216static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3217{
3218 u32 eax, ebx, ecx, edx;
3219
3220 eax = ctxt->regs[VCPU_REGS_RAX];
3221 ecx = ctxt->regs[VCPU_REGS_RCX];
3222 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3223 ctxt->regs[VCPU_REGS_RAX] = eax;
3224 ctxt->regs[VCPU_REGS_RBX] = ebx;
3225 ctxt->regs[VCPU_REGS_RCX] = ecx;
3226 ctxt->regs[VCPU_REGS_RDX] = edx;
3227 return X86EMUL_CONTINUE;
3228}
3229
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003230static bool valid_cr(int nr)
3231{
3232 switch (nr) {
3233 case 0:
3234 case 2 ... 4:
3235 case 8:
3236 return true;
3237 default:
3238 return false;
3239 }
3240}
3241
3242static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3243{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003244 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003245 return emulate_ud(ctxt);
3246
3247 return X86EMUL_CONTINUE;
3248}
3249
3250static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3251{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003252 u64 new_val = ctxt->src.val64;
3253 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003254 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003255
3256 static u64 cr_reserved_bits[] = {
3257 0xffffffff00000000ULL,
3258 0, 0, 0, /* CR3 checked later */
3259 CR4_RESERVED_BITS,
3260 0, 0, 0,
3261 CR8_RESERVED_BITS,
3262 };
3263
3264 if (!valid_cr(cr))
3265 return emulate_ud(ctxt);
3266
3267 if (new_val & cr_reserved_bits[cr])
3268 return emulate_gp(ctxt, 0);
3269
3270 switch (cr) {
3271 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003272 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003273 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3274 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3275 return emulate_gp(ctxt, 0);
3276
Avi Kivity717746e2011-04-20 13:37:53 +03003277 cr4 = ctxt->ops->get_cr(ctxt, 4);
3278 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003279
3280 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3281 !(cr4 & X86_CR4_PAE))
3282 return emulate_gp(ctxt, 0);
3283
3284 break;
3285 }
3286 case 3: {
3287 u64 rsvd = 0;
3288
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003289 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3290 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003291 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003292 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003293 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003294 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003295 rsvd = CR3_NONPAE_RESERVED_BITS;
3296
3297 if (new_val & rsvd)
3298 return emulate_gp(ctxt, 0);
3299
3300 break;
3301 }
3302 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003303 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003304
3305 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3306 return emulate_gp(ctxt, 0);
3307
3308 break;
3309 }
3310 }
3311
3312 return X86EMUL_CONTINUE;
3313}
3314
Joerg Roedel3b88e412011-04-04 12:39:29 +02003315static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3316{
3317 unsigned long dr7;
3318
Avi Kivity717746e2011-04-20 13:37:53 +03003319 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003320
3321 /* Check if DR7.Global_Enable is set */
3322 return dr7 & (1 << 13);
3323}
3324
3325static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3326{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003327 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003328 u64 cr4;
3329
3330 if (dr > 7)
3331 return emulate_ud(ctxt);
3332
Avi Kivity717746e2011-04-20 13:37:53 +03003333 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003334 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3335 return emulate_ud(ctxt);
3336
3337 if (check_dr7_gd(ctxt))
3338 return emulate_db(ctxt);
3339
3340 return X86EMUL_CONTINUE;
3341}
3342
3343static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3344{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003345 u64 new_val = ctxt->src.val64;
3346 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003347
3348 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3349 return emulate_gp(ctxt, 0);
3350
3351 return check_dr_read(ctxt);
3352}
3353
Joerg Roedel01de8b02011-04-04 12:39:31 +02003354static int check_svme(struct x86_emulate_ctxt *ctxt)
3355{
3356 u64 efer;
3357
Avi Kivity717746e2011-04-20 13:37:53 +03003358 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003359
3360 if (!(efer & EFER_SVME))
3361 return emulate_ud(ctxt);
3362
3363 return X86EMUL_CONTINUE;
3364}
3365
3366static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3367{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003368 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003369
3370 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003371 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003372 return emulate_gp(ctxt, 0);
3373
3374 return check_svme(ctxt);
3375}
3376
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003377static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3378{
Avi Kivity717746e2011-04-20 13:37:53 +03003379 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003380
Avi Kivity717746e2011-04-20 13:37:53 +03003381 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003382 return emulate_ud(ctxt);
3383
3384 return X86EMUL_CONTINUE;
3385}
3386
Joerg Roedel80612522011-04-04 12:39:33 +02003387static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3388{
Avi Kivity717746e2011-04-20 13:37:53 +03003389 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003390 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003391
Avi Kivity717746e2011-04-20 13:37:53 +03003392 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003393 (rcx > 3))
3394 return emulate_gp(ctxt, 0);
3395
3396 return X86EMUL_CONTINUE;
3397}
3398
Joerg Roedelf6511932011-04-04 12:39:35 +02003399static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3400{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003401 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3402 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003403 return emulate_gp(ctxt, 0);
3404
3405 return X86EMUL_CONTINUE;
3406}
3407
3408static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3409{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003410 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3411 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003412 return emulate_gp(ctxt, 0);
3413
3414 return X86EMUL_CONTINUE;
3415}
3416
Avi Kivity73fba5f2010-07-29 15:11:53 +03003417#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003418#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003419#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3420 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003421#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003422#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003423#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3424#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003425#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003426#define II(_f, _e, _i) \
3427 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003428#define IIP(_f, _e, _i, _p) \
3429 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3430 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003431#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003432
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003433#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003434#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003435#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003436#define I2bvIP(_f, _e, _i, _p) \
3437 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003438
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003439#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3440 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3441 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003442
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003443static struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003444 DI(SrcNone | Priv, monitor),
3445 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003446 N, N, N, N, N, N,
3447};
3448
Joerg Roedel01de8b02011-04-04 12:39:31 +02003449static struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003450 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3451 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3452 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3453 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3454 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3455 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3456 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3457 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003458};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003459
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003460static struct opcode group7_rm7[] = {
3461 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003462 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003463 N, N, N, N, N, N,
3464};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003465
Avi Kivity73fba5f2010-07-29 15:11:53 +03003466static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003467 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003468 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003469 I(Lock, em_adc),
3470 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003471 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003472 I(Lock, em_sub),
3473 I(Lock, em_xor),
3474 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003475};
3476
3477static struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003478 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003479};
3480
3481static struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003482 I(DstMem | SrcImm, em_test),
3483 I(DstMem | SrcImm, em_test),
3484 I(DstMem | SrcNone | Lock, em_not),
3485 I(DstMem | SrcNone | Lock, em_neg),
3486 I(SrcMem, em_mul_ex),
3487 I(SrcMem, em_imul_ex),
3488 I(SrcMem, em_div_ex),
3489 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003490};
3491
3492static struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003493 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3494 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003495 N, N, N, N, N, N,
3496};
3497
3498static struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003499 I(DstMem | SrcNone | Lock, em_grp45),
3500 I(DstMem | SrcNone | Lock, em_grp45),
3501 I(SrcMem | Stack, em_grp45),
3502 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3503 I(SrcMem | Stack, em_grp45),
3504 I(SrcMemFAddr | ImplicitOps, em_grp45),
3505 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003506};
3507
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003508static struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003509 DI(Prot, sldt),
3510 DI(Prot, str),
3511 DI(Prot | Priv, lldt),
3512 DI(Prot | Priv, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003513 N, N, N, N,
3514};
3515
Avi Kivity73fba5f2010-07-29 15:11:53 +03003516static struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003517 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3518 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003519 II(SrcMem | Priv, em_lgdt, lgdt),
3520 II(SrcMem | Priv, em_lidt, lidt),
3521 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3522 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3523 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003524}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003525 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003526 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003527 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003528 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3529 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3530 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003531} };
3532
3533static struct opcode group8[] = {
3534 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003535 I(DstMem | SrcImmByte, em_bt),
3536 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3537 I(DstMem | SrcImmByte | Lock, em_btr),
3538 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003539};
3540
3541static struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003542 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003543}, {
3544 N, N, N, N, N, N, N, N,
3545} };
3546
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003547static struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003548 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003549 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003550};
3551
Avi Kivityaa97bb42010-01-20 18:09:23 +02003552static struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003553 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003554};
3555
Avi Kivity3e114eb2012-04-09 18:40:01 +03003556static struct gprefix pfx_vmovntpx = {
3557 I(0, em_mov), N, N, N,
3558};
3559
Avi Kivity73fba5f2010-07-29 15:11:53 +03003560static struct opcode opcode_table[256] = {
3561 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003562 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003563 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3564 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003565 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003566 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003567 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3568 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003569 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003570 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003571 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3572 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003573 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003574 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003575 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3576 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003577 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003578 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003579 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003580 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003581 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003582 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003583 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003584 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003585 /* 0x40 - 0x4F */
3586 X16(D(DstReg)),
3587 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003588 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003589 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003590 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003591 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003592 I(ImplicitOps | Stack | No64, em_pusha),
3593 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003594 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3595 N, N, N, N,
3596 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003597 I(SrcImm | Mov | Stack, em_push),
3598 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003599 I(SrcImmByte | Mov | Stack, em_push),
3600 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003601 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3602 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003603 /* 0x70 - 0x7F */
3604 X16(D(SrcImmByte)),
3605 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003606 G(ByteOp | DstMem | SrcImm, group1),
3607 G(DstMem | SrcImm, group1),
3608 G(ByteOp | DstMem | SrcImm | No64, group1),
3609 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003610 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003611 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003612 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003613 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003614 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003615 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003616 D(ModRM | SrcMem | NoAccess | DstReg),
3617 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3618 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003619 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003620 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003621 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003622 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003623 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003624 II(ImplicitOps | Stack, em_pushf, pushf),
3625 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003626 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003627 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003628 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003629 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003630 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003631 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003632 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003633 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3634 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003635 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003636 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003637 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003638 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003639 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003640 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003641 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003642 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003643 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003644 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3645 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003646 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003647 /* 0xC8 - 0xCF */
Avi Kivityf47cfa32012-06-07 17:49:24 +03003648 N, I(Stack, em_leave), N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003649 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003650 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003651 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003652 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003653 N, N, N, N,
3654 /* 0xD8 - 0xDF */
3655 N, N, N, N, N, N, N, N,
3656 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003657 X3(I(SrcImmByte, em_loop)),
3658 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003659 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3660 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003662 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003663 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003664 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3665 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003666 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003667 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003668 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3669 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003670 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003671 D(ImplicitOps), D(ImplicitOps),
3672 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003673 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3674};
3675
3676static struct opcode twobyte_table[256] = {
3677 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003678 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003679 N, I(ImplicitOps | VendorSpecific, em_syscall),
3680 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003681 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003682 N, D(ImplicitOps | ModRM), N, N,
3683 /* 0x10 - 0x1F */
3684 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3685 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003686 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003687 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003688 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3689 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003690 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003691 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3692 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003693 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003694 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003695 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003696 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003697 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003698 I(ImplicitOps | VendorSpecific, em_sysenter),
3699 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003700 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003701 N, N, N, N, N, N, N, N,
3702 /* 0x40 - 0x4F */
3703 X16(D(DstReg | SrcMem | ModRM | Mov)),
3704 /* 0x50 - 0x5F */
3705 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3706 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003707 N, N, N, N,
3708 N, N, N, N,
3709 N, N, N, N,
3710 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003711 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003712 N, N, N, N,
3713 N, N, N, N,
3714 N, N, N, N,
3715 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003716 /* 0x80 - 0x8F */
3717 X16(D(SrcImm)),
3718 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003719 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003720 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003721 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003722 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003723 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3724 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3725 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003726 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003727 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003728 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003729 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3730 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003731 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003732 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003733 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003734 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003735 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003736 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3737 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003738 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003739 /* 0xB8 - 0xBF */
3740 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003741 G(BitOp, group8),
3742 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003743 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003744 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003745 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003746 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003747 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003748 N, N, N, GD(0, &group9),
3749 N, N, N, N, N, N, N, N,
3750 /* 0xD0 - 0xDF */
3751 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3752 /* 0xE0 - 0xEF */
3753 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3754 /* 0xF0 - 0xFF */
3755 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3756};
3757
3758#undef D
3759#undef N
3760#undef G
3761#undef GD
3762#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003763#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003764#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003765
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003766#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003767#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003768#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003769#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003770#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003771
Avi Kivity9dac77f2011-06-01 15:34:25 +03003772static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003773{
3774 unsigned size;
3775
Avi Kivity9dac77f2011-06-01 15:34:25 +03003776 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003777 if (size == 8)
3778 size = 4;
3779 return size;
3780}
3781
3782static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3783 unsigned size, bool sign_extension)
3784{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003785 int rc = X86EMUL_CONTINUE;
3786
3787 op->type = OP_IMM;
3788 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003789 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003790 /* NB. Immediates are sign-extended as necessary. */
3791 switch (op->bytes) {
3792 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003793 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003794 break;
3795 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003796 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003797 break;
3798 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003799 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003800 break;
3801 }
3802 if (!sign_extension) {
3803 switch (op->bytes) {
3804 case 1:
3805 op->val &= 0xff;
3806 break;
3807 case 2:
3808 op->val &= 0xffff;
3809 break;
3810 case 4:
3811 op->val &= 0xffffffff;
3812 break;
3813 }
3814 }
3815done:
3816 return rc;
3817}
3818
Avi Kivitya99455492011-09-13 10:45:41 +03003819static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3820 unsigned d)
3821{
3822 int rc = X86EMUL_CONTINUE;
3823
3824 switch (d) {
3825 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003826 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03003827 break;
3828 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003829 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03003830 break;
3831 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003832 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003833 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03003834 *op = ctxt->memop;
3835 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003836 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03003837 fetch_bit_operand(ctxt);
3838 op->orig_val = op->val;
3839 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003840 case OpMem64:
3841 ctxt->memop.bytes = 8;
3842 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03003843 case OpAcc:
3844 op->type = OP_REG;
3845 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3846 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3847 fetch_register_operand(op);
3848 op->orig_val = op->val;
3849 break;
3850 case OpDI:
3851 op->type = OP_MEM;
3852 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3853 op->addr.mem.ea =
3854 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3855 op->addr.mem.seg = VCPU_SREG_ES;
3856 op->val = 0;
3857 break;
3858 case OpDX:
3859 op->type = OP_REG;
3860 op->bytes = 2;
3861 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3862 fetch_register_operand(op);
3863 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003864 case OpCL:
3865 op->bytes = 1;
3866 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3867 break;
3868 case OpImmByte:
3869 rc = decode_imm(ctxt, op, 1, true);
3870 break;
3871 case OpOne:
3872 op->bytes = 1;
3873 op->val = 1;
3874 break;
3875 case OpImm:
3876 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3877 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003878 case OpMem8:
3879 ctxt->memop.bytes = 1;
3880 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003881 case OpMem16:
3882 ctxt->memop.bytes = 2;
3883 goto mem_common;
3884 case OpMem32:
3885 ctxt->memop.bytes = 4;
3886 goto mem_common;
3887 case OpImmU16:
3888 rc = decode_imm(ctxt, op, 2, false);
3889 break;
3890 case OpImmU:
3891 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3892 break;
3893 case OpSI:
3894 op->type = OP_MEM;
3895 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3896 op->addr.mem.ea =
3897 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3898 op->addr.mem.seg = seg_override(ctxt);
3899 op->val = 0;
3900 break;
3901 case OpImmFAddr:
3902 op->type = OP_IMM;
3903 op->addr.mem.ea = ctxt->_eip;
3904 op->bytes = ctxt->op_bytes + 2;
3905 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3906 break;
3907 case OpMemFAddr:
3908 ctxt->memop.bytes = ctxt->op_bytes + 2;
3909 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003910 case OpES:
3911 op->val = VCPU_SREG_ES;
3912 break;
3913 case OpCS:
3914 op->val = VCPU_SREG_CS;
3915 break;
3916 case OpSS:
3917 op->val = VCPU_SREG_SS;
3918 break;
3919 case OpDS:
3920 op->val = VCPU_SREG_DS;
3921 break;
3922 case OpFS:
3923 op->val = VCPU_SREG_FS;
3924 break;
3925 case OpGS:
3926 op->val = VCPU_SREG_GS;
3927 break;
Avi Kivitya99455492011-09-13 10:45:41 +03003928 case OpImplicit:
3929 /* Special instructions do their own operand decoding. */
3930 default:
3931 op->type = OP_NONE; /* Disable writeback. */
3932 break;
3933 }
3934
3935done:
3936 return rc;
3937}
3938
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003939int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003940{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003941 int rc = X86EMUL_CONTINUE;
3942 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003943 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003944 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003945 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003946
Avi Kivityf09ed832011-09-13 10:45:40 +03003947 ctxt->memop.type = OP_NONE;
3948 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003949 ctxt->_eip = ctxt->eip;
3950 ctxt->fetch.start = ctxt->_eip;
3951 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003952 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003953 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003954
3955 switch (mode) {
3956 case X86EMUL_MODE_REAL:
3957 case X86EMUL_MODE_VM86:
3958 case X86EMUL_MODE_PROT16:
3959 def_op_bytes = def_ad_bytes = 2;
3960 break;
3961 case X86EMUL_MODE_PROT32:
3962 def_op_bytes = def_ad_bytes = 4;
3963 break;
3964#ifdef CONFIG_X86_64
3965 case X86EMUL_MODE_PROT64:
3966 def_op_bytes = 4;
3967 def_ad_bytes = 8;
3968 break;
3969#endif
3970 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003971 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003972 }
3973
Avi Kivity9dac77f2011-06-01 15:34:25 +03003974 ctxt->op_bytes = def_op_bytes;
3975 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003976
3977 /* Legacy prefixes. */
3978 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003979 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003980 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003981 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003982 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003983 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003984 break;
3985 case 0x67: /* address-size override */
3986 if (mode == X86EMUL_MODE_PROT64)
3987 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003988 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003989 else
3990 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003991 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003992 break;
3993 case 0x26: /* ES override */
3994 case 0x2e: /* CS override */
3995 case 0x36: /* SS override */
3996 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003997 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003998 break;
3999 case 0x64: /* FS override */
4000 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004001 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004002 break;
4003 case 0x40 ... 0x4f: /* REX */
4004 if (mode != X86EMUL_MODE_PROT64)
4005 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004006 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004007 continue;
4008 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004009 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004010 break;
4011 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004012 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004013 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004014 break;
4015 default:
4016 goto done_prefixes;
4017 }
4018
4019 /* Any legacy prefix after a REX prefix nullifies its effect. */
4020
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004022 }
4023
4024done_prefixes:
4025
4026 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004027 if (ctxt->rex_prefix & 8)
4028 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004029
4030 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004031 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004032 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004033 if (ctxt->b == 0x0f) {
4034 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004035 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004036 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004037 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004038 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004039
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004040 if (ctxt->d & ModRM)
4041 ctxt->modrm = insn_fetch(u8, ctxt);
4042
Avi Kivity9dac77f2011-06-01 15:34:25 +03004043 while (ctxt->d & GroupMask) {
4044 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004045 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004046 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004047 opcode = opcode.u.group[goffset];
4048 break;
4049 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004050 goffset = (ctxt->modrm >> 3) & 7;
4051 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004052 opcode = opcode.u.gdual->mod3[goffset];
4053 else
4054 opcode = opcode.u.gdual->mod012[goffset];
4055 break;
4056 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004057 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004058 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004059 break;
4060 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004061 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004062 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004063 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004064 switch (simd_prefix) {
4065 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4066 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4067 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4068 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4069 }
4070 break;
4071 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004072 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004073 }
Avi Kivity46561642011-04-24 14:09:59 +03004074
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004075 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004076 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004077 }
4078
Avi Kivity9dac77f2011-06-01 15:34:25 +03004079 ctxt->execute = opcode.u.execute;
4080 ctxt->check_perm = opcode.check_perm;
4081 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004082
4083 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004084 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004085 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004086
Avi Kivity9dac77f2011-06-01 15:34:25 +03004087 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004088 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004089
Avi Kivity9dac77f2011-06-01 15:34:25 +03004090 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4091 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004092
Avi Kivity9dac77f2011-06-01 15:34:25 +03004093 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004094 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004095 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004096 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004097 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004098 }
4099
Avi Kivity9dac77f2011-06-01 15:34:25 +03004100 if (ctxt->d & Sse)
4101 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004102 else if (ctxt->d & Mmx)
4103 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004104
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004105 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004106 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004107 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004108 if (!ctxt->has_seg_override)
4109 set_seg_override(ctxt, ctxt->modrm_seg);
4110 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004111 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004112 if (rc != X86EMUL_CONTINUE)
4113 goto done;
4114
Avi Kivity9dac77f2011-06-01 15:34:25 +03004115 if (!ctxt->has_seg_override)
4116 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004117
Avi Kivityf09ed832011-09-13 10:45:40 +03004118 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004119
Avi Kivityf09ed832011-09-13 10:45:40 +03004120 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4121 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004122
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004123 /*
4124 * Decode and fetch the source operand: register, memory
4125 * or immediate.
4126 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004127 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004128 if (rc != X86EMUL_CONTINUE)
4129 goto done;
4130
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004131 /*
4132 * Decode and fetch the second source operand: register, memory
4133 * or immediate.
4134 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004135 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004136 if (rc != X86EMUL_CONTINUE)
4137 goto done;
4138
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004139 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004140 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004141
4142done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004143 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4144 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004145
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004146 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004147}
4148
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004149bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4150{
4151 return ctxt->d & PageTable;
4152}
4153
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004154static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4155{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004156 /* The second termination condition only applies for REPE
4157 * and REPNE. Test if the repeat string operation prefix is
4158 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4159 * corresponding termination condition according to:
4160 * - if REPE/REPZ and ZF = 0 then done
4161 * - if REPNE/REPNZ and ZF = 1 then done
4162 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004163 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4164 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4165 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004166 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004167 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004168 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4169 return true;
4170
4171 return false;
4172}
4173
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004174static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4175{
4176 bool fault = false;
4177
4178 ctxt->ops->get_fpu(ctxt);
4179 asm volatile("1: fwait \n\t"
4180 "2: \n\t"
4181 ".pushsection .fixup,\"ax\" \n\t"
4182 "3: \n\t"
4183 "movb $1, %[fault] \n\t"
4184 "jmp 2b \n\t"
4185 ".popsection \n\t"
4186 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004187 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004188 ctxt->ops->put_fpu(ctxt);
4189
4190 if (unlikely(fault))
4191 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4192
4193 return X86EMUL_CONTINUE;
4194}
4195
4196static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4197 struct operand *op)
4198{
4199 if (op->type == OP_MM)
4200 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4201}
4202
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004203int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004204{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004205 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004206 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004207 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004208
Avi Kivity9dac77f2011-06-01 15:34:25 +03004209 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004210
Avi Kivity9dac77f2011-06-01 15:34:25 +03004211 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004212 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004213 goto done;
4214 }
4215
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004216 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004217 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004218 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004219 goto done;
4220 }
4221
Avi Kivity9dac77f2011-06-01 15:34:25 +03004222 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004223 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004224 goto done;
4225 }
4226
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004227 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4228 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004229 rc = emulate_ud(ctxt);
4230 goto done;
4231 }
4232
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004233 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004234 rc = emulate_nm(ctxt);
4235 goto done;
4236 }
4237
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004238 if (ctxt->d & Mmx) {
4239 rc = flush_pending_x87_faults(ctxt);
4240 if (rc != X86EMUL_CONTINUE)
4241 goto done;
4242 /*
4243 * Now that we know the fpu is exception safe, we can fetch
4244 * operands from it.
4245 */
4246 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4247 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4248 if (!(ctxt->d & Mov))
4249 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4250 }
4251
Avi Kivity9dac77f2011-06-01 15:34:25 +03004252 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4253 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004254 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004255 if (rc != X86EMUL_CONTINUE)
4256 goto done;
4257 }
4258
Gleb Natapove92805a2010-02-10 14:21:35 +02004259 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004260 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004261 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004262 goto done;
4263 }
4264
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004265 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004266 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004267 rc = emulate_ud(ctxt);
4268 goto done;
4269 }
4270
Joerg Roedeld09beab2011-04-04 12:39:25 +02004271 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004272 if (ctxt->check_perm) {
4273 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004274 if (rc != X86EMUL_CONTINUE)
4275 goto done;
4276 }
4277
Avi Kivity9dac77f2011-06-01 15:34:25 +03004278 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4279 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004280 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004281 if (rc != X86EMUL_CONTINUE)
4282 goto done;
4283 }
4284
Avi Kivity9dac77f2011-06-01 15:34:25 +03004285 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004286 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004287 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4288 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004289 goto done;
4290 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004291 }
4292
Avi Kivity9dac77f2011-06-01 15:34:25 +03004293 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4294 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4295 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004296 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004297 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004298 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004299 }
4300
Avi Kivity9dac77f2011-06-01 15:34:25 +03004301 if (ctxt->src2.type == OP_MEM) {
4302 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4303 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004304 if (rc != X86EMUL_CONTINUE)
4305 goto done;
4306 }
4307
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004309 goto special_insn;
4310
4311
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004313 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004314 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4315 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004316 if (rc != X86EMUL_CONTINUE)
4317 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004318 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004320
Avi Kivity018a98d2007-11-27 19:30:56 +02004321special_insn:
4322
Avi Kivity9dac77f2011-06-01 15:34:25 +03004323 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4324 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004325 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004326 if (rc != X86EMUL_CONTINUE)
4327 goto done;
4328 }
4329
Avi Kivity9dac77f2011-06-01 15:34:25 +03004330 if (ctxt->execute) {
4331 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004332 if (rc != X86EMUL_CONTINUE)
4333 goto done;
4334 goto writeback;
4335 }
4336
Avi Kivity9dac77f2011-06-01 15:34:25 +03004337 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338 goto twobyte_insn;
4339
Avi Kivity9dac77f2011-06-01 15:34:25 +03004340 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004341 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004342 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004343 break;
4344 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004345 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004346 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004347 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004348 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004349 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004352 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 if (test_cc(ctxt->b, ctxt->eflags))
4354 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004355 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004356 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004358 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004359 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004360 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004361 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004362 rc = em_xchg(ctxt);
4363 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004364 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 switch (ctxt->op_bytes) {
4366 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4367 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4368 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004369 }
4370 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004371 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004372 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004373 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004374 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004375 rc = emulate_int(ctxt, 3);
4376 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004377 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004378 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004379 break;
4380 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004381 if (ctxt->eflags & EFLG_OF)
4382 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004383 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004384 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004385 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004386 break;
4387 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004389 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004390 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004391 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004392 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004393 jmp_rel(ctxt, ctxt->src.val);
4394 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004395 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004396 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004397 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004398 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004399 case 0xf5: /* cmc */
4400 /* complement carry flag from eflags reg */
4401 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004402 break;
4403 case 0xf8: /* clc */
4404 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004405 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004406 case 0xf9: /* stc */
4407 ctxt->eflags |= EFLG_CF;
4408 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004409 case 0xfc: /* cld */
4410 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004411 break;
4412 case 0xfd: /* std */
4413 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004414 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004415 default:
4416 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004417 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004418
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004419 if (rc != X86EMUL_CONTINUE)
4420 goto done;
4421
Avi Kivity018a98d2007-11-27 19:30:56 +02004422writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004423 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004424 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004425 goto done;
4426
Gleb Natapov5cd21912010-03-18 15:20:26 +02004427 /*
4428 * restore dst type in case the decoding will be reused
4429 * (happens for string instruction )
4430 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004431 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004432
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 if ((ctxt->d & SrcMask) == SrcSI)
4434 string_addr_inc(ctxt, seg_override(ctxt),
4435 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004436
Avi Kivity9dac77f2011-06-01 15:34:25 +03004437 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004438 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004440
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 if (ctxt->rep_prefix && (ctxt->d & String)) {
4442 struct read_cache *r = &ctxt->io_read;
4443 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004444
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004445 if (!string_insn_completed(ctxt)) {
4446 /*
4447 * Re-enter guest when pio read ahead buffer is empty
4448 * or, if it is not used, after each 1024 iteration.
4449 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004451 (r->end == 0 || r->end != r->pos)) {
4452 /*
4453 * Reset read cache. Usually happens before
4454 * decode, but since instruction is restarted
4455 * we have to do it here.
4456 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004457 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004458 return EMULATION_RESTART;
4459 }
4460 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004461 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004462 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004463
Avi Kivity9dac77f2011-06-01 15:34:25 +03004464 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004465
4466done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004467 if (rc == X86EMUL_PROPAGATE_FAULT)
4468 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004469 if (rc == X86EMUL_INTERCEPTED)
4470 return EMULATION_INTERCEPTED;
4471
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004472 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473
4474twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004475 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004476 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004477 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004478 break;
4479 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004480 case 0x0d: /* GrpP (prefetch) */
4481 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004482 break;
4483 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004485 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004486 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004487 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004488 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004490 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4491 if (!test_cc(ctxt->b, ctxt->eflags))
4492 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004494 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004495 if (test_cc(ctxt->b, ctxt->eflags))
4496 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004497 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004498 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004499 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004500 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004501 case 0xa4: /* shld imm8, r, r/m */
4502 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004503 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004504 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004505 case 0xac: /* shrd imm8, r, r/m */
4506 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004507 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004508 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004509 case 0xae: /* clflush */
4510 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004511 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004512 ctxt->dst.bytes = ctxt->op_bytes;
4513 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4514 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004515 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004517 ctxt->dst.bytes = ctxt->op_bytes;
4518 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4519 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004520 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004521 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004522 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004523 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004524 ctxt->src.val = ctxt->dst.orig_val;
4525 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004526 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004527 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004528 ctxt->dst.bytes = ctxt->op_bytes;
4529 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4530 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004531 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004532 default:
4533 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004534 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004535
4536 if (rc != X86EMUL_CONTINUE)
4537 goto done;
4538
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539 goto writeback;
4540
4541cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004542 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004543}