blob: 33ccd757cb1110f5f77bd65b1c2f87b0cfbf374b [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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-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 Kivitya9945542011-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 Kivity1c73ef6652007-11-01 06:31:28 +0200977static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300978 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200979{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200980 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700981 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900982 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300983 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200984
Avi Kivity9dac77f2011-06-01 15:34:25 +0300985 if (ctxt->rex_prefix) {
986 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
987 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
988 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200989 }
990
Avi Kivity9dac77f2011-06-01 15:34:25 +0300991 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
992 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
993 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
994 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200995
Avi Kivity9dac77f2011-06-01 15:34:25 +0300996 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300997 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300998 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
999 op->addr.reg = decode_register(ctxt->modrm_rm,
1000 ctxt->regs, ctxt->d & ByteOp);
1001 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001002 op->type = OP_XMM;
1003 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001004 op->addr.xmm = ctxt->modrm_rm;
1005 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001006 return rc;
1007 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001008 if (ctxt->d & Mmx) {
1009 op->type = OP_MM;
1010 op->bytes = 8;
1011 op->addr.xmm = ctxt->modrm_rm & 7;
1012 return rc;
1013 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001014 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001015 return rc;
1016 }
1017
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001018 op->type = OP_MEM;
1019
Avi Kivity9dac77f2011-06-01 15:34:25 +03001020 if (ctxt->ad_bytes == 2) {
1021 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
1022 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
1023 unsigned si = ctxt->regs[VCPU_REGS_RSI];
1024 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001025
1026 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001027 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001028 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001029 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001030 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001031 break;
1032 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001033 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001034 break;
1035 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001036 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001037 break;
1038 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001039 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001040 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001041 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001042 break;
1043 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001044 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045 break;
1046 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001047 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001048 break;
1049 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001050 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001051 break;
1052 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001053 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001054 break;
1055 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001056 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001057 break;
1058 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001059 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001060 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001061 break;
1062 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001063 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001064 break;
1065 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001066 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1067 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1068 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001069 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001070 } else {
1071 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001072 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001073 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001074 index_reg |= (sib >> 3) & 7;
1075 base_reg |= sib & 7;
1076 scale = sib >> 6;
1077
Avi Kivity9dac77f2011-06-01 15:34:25 +03001078 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001079 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001080 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03001081 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001082 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001083 modrm_ea += ctxt->regs[index_reg] << scale;
1084 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001085 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001086 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -07001087 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 modrm_ea += ctxt->regs[ctxt->modrm_rm];
1089 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001090 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001091 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001092 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001093 break;
1094 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001095 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001096 break;
1097 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001098 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001099 break;
1100 }
1101 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001102 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001103done:
1104 return rc;
1105}
1106
1107static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001108 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001109{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001110 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001111
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001112 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001113 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001115 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001116 break;
1117 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001118 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119 break;
1120 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001121 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122 break;
1123 }
1124done:
1125 return rc;
1126}
1127
Avi Kivity9dac77f2011-06-01 15:34:25 +03001128static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001129{
Sheng Yang7129eec2010-09-28 16:33:32 +08001130 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001131
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1133 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001134
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 if (ctxt->src.bytes == 2)
1136 sv = (s16)ctxt->src.val & (s16)mask;
1137 else if (ctxt->src.bytes == 4)
1138 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001139
Avi Kivity9dac77f2011-06-01 15:34:25 +03001140 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001141 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001142
1143 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001144 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001145}
1146
Gleb Natapov9de41572010-04-28 19:15:22 +03001147static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001148 unsigned long addr, void *dest, unsigned size)
1149{
1150 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001151 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001152
1153 while (size) {
1154 int n = min(size, 8u);
1155 size -= n;
1156 if (mc->pos < mc->end)
1157 goto read_cached;
1158
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001159 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1160 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001161 if (rc != X86EMUL_CONTINUE)
1162 return rc;
1163 mc->end += n;
1164
1165 read_cached:
1166 memcpy(dest, mc->data + mc->pos, n);
1167 mc->pos += n;
1168 dest += n;
1169 addr += n;
1170 }
1171 return X86EMUL_CONTINUE;
1172}
1173
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001174static int segmented_read(struct x86_emulate_ctxt *ctxt,
1175 struct segmented_address addr,
1176 void *data,
1177 unsigned size)
1178{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001179 int rc;
1180 ulong linear;
1181
Avi Kivity83b87952011-04-03 11:31:19 +03001182 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001183 if (rc != X86EMUL_CONTINUE)
1184 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001185 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001186}
1187
1188static int segmented_write(struct x86_emulate_ctxt *ctxt,
1189 struct segmented_address addr,
1190 const void *data,
1191 unsigned size)
1192{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001193 int rc;
1194 ulong linear;
1195
Avi Kivity83b87952011-04-03 11:31:19 +03001196 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001197 if (rc != X86EMUL_CONTINUE)
1198 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001199 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1200 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001201}
1202
1203static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1204 struct segmented_address addr,
1205 const void *orig_data, const void *data,
1206 unsigned size)
1207{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001208 int rc;
1209 ulong linear;
1210
Avi Kivity83b87952011-04-03 11:31:19 +03001211 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001212 if (rc != X86EMUL_CONTINUE)
1213 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001214 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1215 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001216}
1217
Gleb Natapov7b262e92010-03-18 15:20:27 +02001218static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001219 unsigned int size, unsigned short port,
1220 void *dest)
1221{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001222 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001223
1224 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001225 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001226 unsigned int count = ctxt->rep_prefix ?
1227 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001228 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1230 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001231 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1232 count);
1233 if (n == 0)
1234 n = 1;
1235 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001236 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001237 return 0;
1238 rc->end = n * size;
1239 }
1240
1241 memcpy(dest, rc->data + rc->pos, size);
1242 rc->pos += size;
1243 return 1;
1244}
1245
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001246static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1247 u16 index, struct desc_struct *desc)
1248{
1249 struct desc_ptr dt;
1250 ulong addr;
1251
1252 ctxt->ops->get_idt(ctxt, &dt);
1253
1254 if (dt.size < index * 8 + 7)
1255 return emulate_gp(ctxt, index << 3 | 0x2);
1256
1257 addr = dt.address + index * 8;
1258 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1259 &ctxt->exception);
1260}
1261
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001262static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001263 u16 selector, struct desc_ptr *dt)
1264{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001265 struct x86_emulate_ops *ops = ctxt->ops;
1266
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001267 if (selector & 1 << 2) {
1268 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001269 u16 sel;
1270
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001271 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001272 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001273 return;
1274
1275 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1276 dt->address = get_desc_base(&desc);
1277 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001278 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001279}
1280
1281/* allowed just for 8 bytes segments */
1282static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001283 u16 selector, struct desc_struct *desc)
1284{
1285 struct desc_ptr dt;
1286 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001287 ulong addr;
1288
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001289 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001290
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001291 if (dt.size < index * 8 + 7)
1292 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001293
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001294 addr = dt.address + index * 8;
1295 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1296 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001297}
1298
1299/* allowed just for 8 bytes segments */
1300static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001301 u16 selector, struct desc_struct *desc)
1302{
1303 struct desc_ptr dt;
1304 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001305 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001306
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001307 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001308
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001309 if (dt.size < index * 8 + 7)
1310 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001311
1312 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001313 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1314 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001315}
1316
Gleb Natapov5601d052011-03-07 14:55:06 +02001317/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001318static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001319 u16 selector, int seg)
1320{
1321 struct desc_struct seg_desc;
1322 u8 dpl, rpl, cpl;
1323 unsigned err_vec = GP_VECTOR;
1324 u32 err_code = 0;
1325 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1326 int ret;
1327
1328 memset(&seg_desc, 0, sizeof seg_desc);
1329
1330 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1331 || ctxt->mode == X86EMUL_MODE_REAL) {
1332 /* set real mode segment descriptor */
1333 set_desc_base(&seg_desc, selector << 4);
1334 set_desc_limit(&seg_desc, 0xffff);
1335 seg_desc.type = 3;
1336 seg_desc.p = 1;
1337 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001338 if (ctxt->mode == X86EMUL_MODE_VM86)
1339 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001340 goto load;
1341 }
1342
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001343 rpl = selector & 3;
1344 cpl = ctxt->ops->cpl(ctxt);
1345
1346 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1347 if ((seg == VCPU_SREG_CS
1348 || (seg == VCPU_SREG_SS
1349 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1350 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001351 && null_selector)
1352 goto exception;
1353
1354 /* TR should be in GDT only */
1355 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1356 goto exception;
1357
1358 if (null_selector) /* for NULL selector skip all following checks */
1359 goto load;
1360
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001361 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001362 if (ret != X86EMUL_CONTINUE)
1363 return ret;
1364
1365 err_code = selector & 0xfffc;
1366 err_vec = GP_VECTOR;
1367
1368 /* can't load system descriptor into segment selecor */
1369 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1370 goto exception;
1371
1372 if (!seg_desc.p) {
1373 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1374 goto exception;
1375 }
1376
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001377 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001378
1379 switch (seg) {
1380 case VCPU_SREG_SS:
1381 /*
1382 * segment is not a writable data segment or segment
1383 * selector's RPL != CPL or segment selector's RPL != CPL
1384 */
1385 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1386 goto exception;
1387 break;
1388 case VCPU_SREG_CS:
1389 if (!(seg_desc.type & 8))
1390 goto exception;
1391
1392 if (seg_desc.type & 4) {
1393 /* conforming */
1394 if (dpl > cpl)
1395 goto exception;
1396 } else {
1397 /* nonconforming */
1398 if (rpl > cpl || dpl != cpl)
1399 goto exception;
1400 }
1401 /* CS(RPL) <- CPL */
1402 selector = (selector & 0xfffc) | cpl;
1403 break;
1404 case VCPU_SREG_TR:
1405 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1406 goto exception;
1407 break;
1408 case VCPU_SREG_LDTR:
1409 if (seg_desc.s || seg_desc.type != 2)
1410 goto exception;
1411 break;
1412 default: /* DS, ES, FS, or GS */
1413 /*
1414 * segment is not a data or readable code segment or
1415 * ((segment is a data or nonconforming code segment)
1416 * and (both RPL and CPL > DPL))
1417 */
1418 if ((seg_desc.type & 0xa) == 0x8 ||
1419 (((seg_desc.type & 0xc) != 0xc) &&
1420 (rpl > dpl && cpl > dpl)))
1421 goto exception;
1422 break;
1423 }
1424
1425 if (seg_desc.s) {
1426 /* mark segment as accessed */
1427 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001428 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001429 if (ret != X86EMUL_CONTINUE)
1430 return ret;
1431 }
1432load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001433 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001434 return X86EMUL_CONTINUE;
1435exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001436 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 return X86EMUL_PROPAGATE_FAULT;
1438}
1439
Wei Yongjun31be40b2010-08-17 09:17:30 +08001440static void write_register_operand(struct operand *op)
1441{
1442 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1443 switch (op->bytes) {
1444 case 1:
1445 *(u8 *)op->addr.reg = (u8)op->val;
1446 break;
1447 case 2:
1448 *(u16 *)op->addr.reg = (u16)op->val;
1449 break;
1450 case 4:
1451 *op->addr.reg = (u32)op->val;
1452 break; /* 64b: zero-extend */
1453 case 8:
1454 *op->addr.reg = op->val;
1455 break;
1456 }
1457}
1458
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001459static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001460{
1461 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001462
Avi Kivity9dac77f2011-06-01 15:34:25 +03001463 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001464 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001465 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001466 break;
1467 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001468 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001469 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001470 ctxt->dst.addr.mem,
1471 &ctxt->dst.orig_val,
1472 &ctxt->dst.val,
1473 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001474 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001475 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001476 ctxt->dst.addr.mem,
1477 &ctxt->dst.val,
1478 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001479 if (rc != X86EMUL_CONTINUE)
1480 return rc;
1481 break;
Avi Kivity12537912011-03-29 11:41:27 +02001482 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001483 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001484 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001485 case OP_MM:
1486 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1487 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001488 case OP_NONE:
1489 /* no writeback */
1490 break;
1491 default:
1492 break;
1493 }
1494 return X86EMUL_CONTINUE;
1495}
1496
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001497static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001498{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001499 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001500
Avi Kivity9dac77f2011-06-01 15:34:25 +03001501 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1502 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001503 addr.seg = VCPU_SREG_SS;
1504
1505 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001506 ctxt->dst.type = OP_NONE;
1507 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001508}
1509
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001510static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001511 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001512{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001513 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001514 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001515
Avi Kivity9dac77f2011-06-01 15:34:25 +03001516 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001517 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001518 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001519 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001520 return rc;
1521
Avi Kivity9dac77f2011-06-01 15:34:25 +03001522 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001523 return rc;
1524}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001525
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001526static int em_pop(struct x86_emulate_ctxt *ctxt)
1527{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001528 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001529}
1530
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001531static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001532 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001533{
1534 int rc;
1535 unsigned long val, change_mask;
1536 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001537 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001538
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001539 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001540 if (rc != X86EMUL_CONTINUE)
1541 return rc;
1542
1543 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1544 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1545
1546 switch(ctxt->mode) {
1547 case X86EMUL_MODE_PROT64:
1548 case X86EMUL_MODE_PROT32:
1549 case X86EMUL_MODE_PROT16:
1550 if (cpl == 0)
1551 change_mask |= EFLG_IOPL;
1552 if (cpl <= iopl)
1553 change_mask |= EFLG_IF;
1554 break;
1555 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001556 if (iopl < 3)
1557 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001558 change_mask |= EFLG_IF;
1559 break;
1560 default: /* real mode */
1561 change_mask |= (EFLG_IOPL | EFLG_IF);
1562 break;
1563 }
1564
1565 *(unsigned long *)dest =
1566 (ctxt->eflags & ~change_mask) | (val & change_mask);
1567
1568 return rc;
1569}
1570
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001571static int em_popf(struct x86_emulate_ctxt *ctxt)
1572{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001573 ctxt->dst.type = OP_REG;
1574 ctxt->dst.addr.reg = &ctxt->eflags;
1575 ctxt->dst.bytes = ctxt->op_bytes;
1576 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001577}
1578
Avi Kivityf47cfa32012-06-07 17:49:24 +03001579static int em_leave(struct x86_emulate_ctxt *ctxt)
1580{
1581 assign_masked(&ctxt->regs[VCPU_REGS_RSP], ctxt->regs[VCPU_REGS_RBP],
1582 stack_mask(ctxt));
1583 return emulate_pop(ctxt, &ctxt->regs[VCPU_REGS_RBP], ctxt->op_bytes);
1584}
1585
Avi Kivity1cd196e2011-09-13 10:45:51 +03001586static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001587{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001588 int seg = ctxt->src2.val;
1589
Avi Kivity9dac77f2011-06-01 15:34:25 +03001590 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001591
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001592 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001593}
1594
Avi Kivity1cd196e2011-09-13 10:45:51 +03001595static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001596{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001597 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001598 unsigned long selector;
1599 int rc;
1600
Avi Kivity9dac77f2011-06-01 15:34:25 +03001601 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001602 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001603 return rc;
1604
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001605 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001606 return rc;
1607}
1608
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001609static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001610{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001611 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001612 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001613 int reg = VCPU_REGS_RAX;
1614
1615 while (reg <= VCPU_REGS_RDI) {
1616 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001617 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001618
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001619 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001620 if (rc != X86EMUL_CONTINUE)
1621 return rc;
1622
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001623 ++reg;
1624 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001625
Wei Yongjunc37eda12010-06-15 09:03:33 +08001626 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001627}
1628
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001629static int em_pushf(struct x86_emulate_ctxt *ctxt)
1630{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001631 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001632 return em_push(ctxt);
1633}
1634
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001635static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001636{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001637 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001638 int reg = VCPU_REGS_RDI;
1639
1640 while (reg >= VCPU_REGS_RAX) {
1641 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001642 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1643 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001644 --reg;
1645 }
1646
Avi Kivity9dac77f2011-06-01 15:34:25 +03001647 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001648 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001649 break;
1650 --reg;
1651 }
1652 return rc;
1653}
1654
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001655int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001656{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001657 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001658 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001659 struct desc_ptr dt;
1660 gva_t cs_addr;
1661 gva_t eip_addr;
1662 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001663
1664 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001665 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001666 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001667 if (rc != X86EMUL_CONTINUE)
1668 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001669
1670 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1671
Avi Kivity9dac77f2011-06-01 15:34:25 +03001672 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001673 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001674 if (rc != X86EMUL_CONTINUE)
1675 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001676
Avi Kivity9dac77f2011-06-01 15:34:25 +03001677 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001678 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001679 if (rc != X86EMUL_CONTINUE)
1680 return rc;
1681
Avi Kivity4bff1e862011-04-20 13:37:53 +03001682 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001683
1684 eip_addr = dt.address + (irq << 2);
1685 cs_addr = dt.address + (irq << 2) + 2;
1686
Avi Kivity0f65dd72011-04-20 13:37:53 +03001687 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001688 if (rc != X86EMUL_CONTINUE)
1689 return rc;
1690
Avi Kivity0f65dd72011-04-20 13:37:53 +03001691 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001692 if (rc != X86EMUL_CONTINUE)
1693 return rc;
1694
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001695 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001696 if (rc != X86EMUL_CONTINUE)
1697 return rc;
1698
Avi Kivity9dac77f2011-06-01 15:34:25 +03001699 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001700
1701 return rc;
1702}
1703
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001704static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001705{
1706 switch(ctxt->mode) {
1707 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001708 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001709 case X86EMUL_MODE_VM86:
1710 case X86EMUL_MODE_PROT16:
1711 case X86EMUL_MODE_PROT32:
1712 case X86EMUL_MODE_PROT64:
1713 default:
1714 /* Protected mode interrupts unimplemented yet */
1715 return X86EMUL_UNHANDLEABLE;
1716 }
1717}
1718
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001719static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001720{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001721 int rc = X86EMUL_CONTINUE;
1722 unsigned long temp_eip = 0;
1723 unsigned long temp_eflags = 0;
1724 unsigned long cs = 0;
1725 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1726 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1727 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1728 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1729
1730 /* TODO: Add stack limit check */
1731
Avi Kivity9dac77f2011-06-01 15:34:25 +03001732 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001733
1734 if (rc != X86EMUL_CONTINUE)
1735 return rc;
1736
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001737 if (temp_eip & ~0xffff)
1738 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001739
Avi Kivity9dac77f2011-06-01 15:34:25 +03001740 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001741
1742 if (rc != X86EMUL_CONTINUE)
1743 return rc;
1744
Avi Kivity9dac77f2011-06-01 15:34:25 +03001745 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001746
1747 if (rc != X86EMUL_CONTINUE)
1748 return rc;
1749
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001750 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001751
1752 if (rc != X86EMUL_CONTINUE)
1753 return rc;
1754
Avi Kivity9dac77f2011-06-01 15:34:25 +03001755 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001756
1757
Avi Kivity9dac77f2011-06-01 15:34:25 +03001758 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001759 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001760 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001761 ctxt->eflags &= ~0xffff;
1762 ctxt->eflags |= temp_eflags;
1763 }
1764
1765 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1766 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1767
1768 return rc;
1769}
1770
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001771static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001772{
1773 switch(ctxt->mode) {
1774 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001775 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001776 case X86EMUL_MODE_VM86:
1777 case X86EMUL_MODE_PROT16:
1778 case X86EMUL_MODE_PROT32:
1779 case X86EMUL_MODE_PROT64:
1780 default:
1781 /* iret from protected mode unimplemented yet */
1782 return X86EMUL_UNHANDLEABLE;
1783 }
1784}
1785
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001786static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1787{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001788 int rc;
1789 unsigned short sel;
1790
Avi Kivity9dac77f2011-06-01 15:34:25 +03001791 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001792
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001793 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001794 if (rc != X86EMUL_CONTINUE)
1795 return rc;
1796
Avi Kivity9dac77f2011-06-01 15:34:25 +03001797 ctxt->_eip = 0;
1798 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001799 return X86EMUL_CONTINUE;
1800}
1801
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001802static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001803{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001804 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001805 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001806 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001807 break;
1808 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001809 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001810 break;
1811 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001812 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001813 break;
1814 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001815 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001816 break;
1817 case 4: /* sal/shl */
1818 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001819 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001820 break;
1821 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001822 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823 break;
1824 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001825 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001826 break;
1827 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001828 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001829}
1830
Avi Kivity3329ece2011-09-13 10:45:39 +03001831static int em_not(struct x86_emulate_ctxt *ctxt)
1832{
1833 ctxt->dst.val = ~ctxt->dst.val;
1834 return X86EMUL_CONTINUE;
1835}
1836
1837static int em_neg(struct x86_emulate_ctxt *ctxt)
1838{
1839 emulate_1op(ctxt, "neg");
1840 return X86EMUL_CONTINUE;
1841}
1842
1843static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1844{
1845 u8 ex = 0;
1846
1847 emulate_1op_rax_rdx(ctxt, "mul", ex);
1848 return X86EMUL_CONTINUE;
1849}
1850
1851static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1852{
1853 u8 ex = 0;
1854
1855 emulate_1op_rax_rdx(ctxt, "imul", ex);
1856 return X86EMUL_CONTINUE;
1857}
1858
1859static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001860{
Avi Kivity34d1f492010-08-26 11:59:01 +03001861 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001862
Avi Kivity3329ece2011-09-13 10:45:39 +03001863 emulate_1op_rax_rdx(ctxt, "div", de);
1864 if (de)
1865 return emulate_de(ctxt);
1866 return X86EMUL_CONTINUE;
1867}
1868
1869static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1870{
1871 u8 de = 0;
1872
1873 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001874 if (de)
1875 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001876 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001877}
1878
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001879static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001880{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001881 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001882
Avi Kivity9dac77f2011-06-01 15:34:25 +03001883 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001884 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001885 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001886 break;
1887 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001888 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001889 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001890 case 2: /* call near abs */ {
1891 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001892 old_eip = ctxt->_eip;
1893 ctxt->_eip = ctxt->src.val;
1894 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001895 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001896 break;
1897 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001898 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001899 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001901 case 5: /* jmp far */
1902 rc = em_jmp_far(ctxt);
1903 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001904 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001905 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001906 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001907 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001908 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001909}
1910
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001911static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001912{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001913 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001914
Avi Kivity9dac77f2011-06-01 15:34:25 +03001915 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1916 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1917 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1918 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001919 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001920 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001921 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1922 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923
Laurent Vivier05f086f2007-09-24 11:10:55 +02001924 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001925 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001926 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001927}
1928
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001929static int em_ret(struct x86_emulate_ctxt *ctxt)
1930{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001931 ctxt->dst.type = OP_REG;
1932 ctxt->dst.addr.reg = &ctxt->_eip;
1933 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001934 return em_pop(ctxt);
1935}
1936
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001937static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001938{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001939 int rc;
1940 unsigned long cs;
1941
Avi Kivity9dac77f2011-06-01 15:34:25 +03001942 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001943 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001944 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001945 if (ctxt->op_bytes == 4)
1946 ctxt->_eip = (u32)ctxt->_eip;
1947 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001948 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001949 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001950 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001951 return rc;
1952}
1953
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09001954static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
1955{
1956 /* Save real source value, then compare EAX against destination. */
1957 ctxt->src.orig_val = ctxt->src.val;
1958 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
1959 emulate_2op_SrcV(ctxt, "cmp");
1960
1961 if (ctxt->eflags & EFLG_ZF) {
1962 /* Success: write back to memory. */
1963 ctxt->dst.val = ctxt->src.orig_val;
1964 } else {
1965 /* Failure: write the value we saw to EAX. */
1966 ctxt->dst.type = OP_REG;
1967 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
1968 }
1969 return X86EMUL_CONTINUE;
1970}
1971
Avi Kivityd4b43252011-09-13 10:45:50 +03001972static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001973{
Avi Kivityd4b43252011-09-13 10:45:50 +03001974 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001975 unsigned short sel;
1976 int rc;
1977
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001979
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001980 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001981 if (rc != X86EMUL_CONTINUE)
1982 return rc;
1983
Avi Kivity9dac77f2011-06-01 15:34:25 +03001984 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001985 return rc;
1986}
1987
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001988static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001989setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001990 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001991{
Avi Kivity1aa36612011-04-27 13:20:30 +03001992 u16 selector;
1993
Gleb Natapov79168fd2010-04-28 19:15:30 +03001994 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001995 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001996 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001997
1998 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001999 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002000 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002001 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002002 cs->type = 0x0b; /* Read, Execute, Accessed */
2003 cs->s = 1;
2004 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002005 cs->p = 1;
2006 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002007
Gleb Natapov79168fd2010-04-28 19:15:30 +03002008 set_desc_base(ss, 0); /* flat segment */
2009 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002010 ss->g = 1; /* 4kb granularity */
2011 ss->s = 1;
2012 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002013 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002014 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002015 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002016}
2017
Avi Kivity1a18a692012-02-01 12:23:21 +02002018static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2019{
2020 u32 eax, ebx, ecx, edx;
2021
2022 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002023 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2024 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002025 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2026 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2027}
2028
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002029static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2030{
2031 struct x86_emulate_ops *ops = ctxt->ops;
2032 u32 eax, ebx, ecx, edx;
2033
2034 /*
2035 * syscall should always be enabled in longmode - so only become
2036 * vendor specific (cpuid) if other modes are active...
2037 */
2038 if (ctxt->mode == X86EMUL_MODE_PROT64)
2039 return true;
2040
2041 eax = 0x00000000;
2042 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002043 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2044 /*
2045 * Intel ("GenuineIntel")
2046 * remark: Intel CPUs only support "syscall" in 64bit
2047 * longmode. Also an 64bit guest with a
2048 * 32bit compat-app running will #UD !! While this
2049 * behaviour can be fixed (by emulating) into AMD
2050 * response - CPUs of AMD can't behave like Intel.
2051 */
2052 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2053 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2054 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2055 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002056
Avi Kivity0017f932012-06-07 14:10:16 +03002057 /* AMD ("AuthenticAMD") */
2058 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2059 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2060 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2061 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002062
Avi Kivity0017f932012-06-07 14:10:16 +03002063 /* AMD ("AMDisbetter!") */
2064 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2065 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2066 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2067 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002068
2069 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2070 return false;
2071}
2072
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002073static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002074{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002075 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002076 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002077 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002078 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002079 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002080
2081 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002082 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002083 ctxt->mode == X86EMUL_MODE_VM86)
2084 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002085
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002086 if (!(em_syscall_is_enabled(ctxt)))
2087 return emulate_ud(ctxt);
2088
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002089 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002090 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002091
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002092 if (!(efer & EFER_SCE))
2093 return emulate_ud(ctxt);
2094
Avi Kivity717746e2011-04-20 13:37:53 +03002095 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002096 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002097 cs_sel = (u16)(msr_data & 0xfffc);
2098 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002099
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002100 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002101 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002102 cs.l = 1;
2103 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002104 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2105 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002106
Avi Kivity9dac77f2011-06-01 15:34:25 +03002107 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002108 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002109#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002111
Avi Kivity717746e2011-04-20 13:37:53 +03002112 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002113 ctxt->mode == X86EMUL_MODE_PROT64 ?
2114 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002115 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002116
Avi Kivity717746e2011-04-20 13:37:53 +03002117 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002118 ctxt->eflags &= ~(msr_data | EFLG_RF);
2119#endif
2120 } else {
2121 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002122 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002123 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002124
2125 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2126 }
2127
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002128 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002129}
2130
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002131static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002132{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002133 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002134 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002135 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002136 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002137 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002138
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002139 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002140 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002141 if (ctxt->mode == X86EMUL_MODE_REAL)
2142 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002143
Avi Kivity1a18a692012-02-01 12:23:21 +02002144 /*
2145 * Not recognized on AMD in compat mode (but is recognized in legacy
2146 * mode).
2147 */
2148 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2149 && !vendor_intel(ctxt))
2150 return emulate_ud(ctxt);
2151
Andre Przywara8c604352009-06-18 12:56:01 +02002152 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2153 * Therefore, we inject an #UD.
2154 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002155 if (ctxt->mode == X86EMUL_MODE_PROT64)
2156 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002157
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002158 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002159
Avi Kivity717746e2011-04-20 13:37:53 +03002160 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002161 switch (ctxt->mode) {
2162 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002163 if ((msr_data & 0xfffc) == 0x0)
2164 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002165 break;
2166 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002167 if (msr_data == 0x0)
2168 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002169 break;
2170 }
2171
2172 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002173 cs_sel = (u16)msr_data;
2174 cs_sel &= ~SELECTOR_RPL_MASK;
2175 ss_sel = cs_sel + 8;
2176 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002177 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002178 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002179 cs.l = 1;
2180 }
2181
Avi Kivity1aa36612011-04-27 13:20:30 +03002182 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2183 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002184
Avi Kivity717746e2011-04-20 13:37:53 +03002185 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002186 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002187
Avi Kivity717746e2011-04-20 13:37:53 +03002188 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002189 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002190
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002191 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002192}
2193
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002194static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002195{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002196 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002197 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002198 u64 msr_data;
2199 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002200 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002201
Gleb Natapova0044752010-02-10 14:21:31 +02002202 /* inject #GP if in real mode or Virtual 8086 mode */
2203 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002204 ctxt->mode == X86EMUL_MODE_VM86)
2205 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002206
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002207 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002208
Avi Kivity9dac77f2011-06-01 15:34:25 +03002209 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002210 usermode = X86EMUL_MODE_PROT64;
2211 else
2212 usermode = X86EMUL_MODE_PROT32;
2213
2214 cs.dpl = 3;
2215 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002216 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002217 switch (usermode) {
2218 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002219 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002220 if ((msr_data & 0xfffc) == 0x0)
2221 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002222 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002223 break;
2224 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002225 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002226 if (msr_data == 0x0)
2227 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002228 ss_sel = cs_sel + 8;
2229 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002230 cs.l = 1;
2231 break;
2232 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002233 cs_sel |= SELECTOR_RPL_MASK;
2234 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002235
Avi Kivity1aa36612011-04-27 13:20:30 +03002236 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2237 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002238
Avi Kivity9dac77f2011-06-01 15:34:25 +03002239 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2240 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002241
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002242 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002243}
2244
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002245static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002246{
2247 int iopl;
2248 if (ctxt->mode == X86EMUL_MODE_REAL)
2249 return false;
2250 if (ctxt->mode == X86EMUL_MODE_VM86)
2251 return true;
2252 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002253 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002254}
2255
2256static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002257 u16 port, u16 len)
2258{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002259 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002260 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002261 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002262 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002263 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002264 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002265 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002266
Avi Kivity1aa36612011-04-27 13:20:30 +03002267 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002269 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002270 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002271 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002272 base = get_desc_base(&tr_seg);
2273#ifdef CONFIG_X86_64
2274 base |= ((u64)base3) << 32;
2275#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002276 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002277 if (r != X86EMUL_CONTINUE)
2278 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002279 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002280 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002281 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002282 if (r != X86EMUL_CONTINUE)
2283 return false;
2284 if ((perm >> bit_idx) & mask)
2285 return false;
2286 return true;
2287}
2288
2289static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002290 u16 port, u16 len)
2291{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002292 if (ctxt->perm_ok)
2293 return true;
2294
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002295 if (emulator_bad_iopl(ctxt))
2296 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002297 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002298
2299 ctxt->perm_ok = true;
2300
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002301 return true;
2302}
2303
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002304static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002305 struct tss_segment_16 *tss)
2306{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002307 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002308 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002309 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2310 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2311 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2312 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2313 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2314 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2315 tss->si = ctxt->regs[VCPU_REGS_RSI];
2316 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317
Avi Kivity1aa36612011-04-27 13:20:30 +03002318 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2319 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2320 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2321 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2322 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002323}
2324
2325static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326 struct tss_segment_16 *tss)
2327{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328 int ret;
2329
Avi Kivity9dac77f2011-06-01 15:34:25 +03002330 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002331 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002332 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2333 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2334 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2335 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2336 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2337 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2338 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2339 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002340
2341 /*
2342 * SDM says that segment selectors are loaded before segment
2343 * descriptors
2344 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002345 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2346 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2347 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2348 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2349 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350
2351 /*
2352 * Now load segment descriptors. If fault happenes at this stage
2353 * it is handled in a context of new task
2354 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002355 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002356 if (ret != X86EMUL_CONTINUE)
2357 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002358 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002359 if (ret != X86EMUL_CONTINUE)
2360 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002361 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002362 if (ret != X86EMUL_CONTINUE)
2363 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002364 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002365 if (ret != X86EMUL_CONTINUE)
2366 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002367 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002368 if (ret != X86EMUL_CONTINUE)
2369 return ret;
2370
2371 return X86EMUL_CONTINUE;
2372}
2373
2374static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375 u16 tss_selector, u16 old_tss_sel,
2376 ulong old_tss_base, struct desc_struct *new_desc)
2377{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002378 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002379 struct tss_segment_16 tss_seg;
2380 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002381 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002382
Avi Kivity0f65dd72011-04-20 13:37:53 +03002383 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002384 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002385 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002386 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002388
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002389 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002390
Avi Kivity0f65dd72011-04-20 13:37:53 +03002391 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002392 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002393 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002394 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002395 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002396
Avi Kivity0f65dd72011-04-20 13:37:53 +03002397 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002398 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002399 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002400 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002401 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402
2403 if (old_tss_sel != 0xffff) {
2404 tss_seg.prev_task_link = old_tss_sel;
2405
Avi Kivity0f65dd72011-04-20 13:37:53 +03002406 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407 &tss_seg.prev_task_link,
2408 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002409 &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
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002415 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416}
2417
2418static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419 struct tss_segment_32 *tss)
2420{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002421 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002422 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002423 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002424 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2425 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2426 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2427 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2428 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2429 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2430 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2431 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432
Avi Kivity1aa36612011-04-27 13:20:30 +03002433 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2434 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2435 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2436 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2437 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2438 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2439 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440}
2441
2442static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443 struct tss_segment_32 *tss)
2444{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002445 int ret;
2446
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002447 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002448 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002449 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002450 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002451
2452 /* General purpose registers */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002453 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2454 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2455 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2456 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2457 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2458 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2459 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2460 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461
2462 /*
2463 * SDM says that segment selectors are loaded before segment
2464 * descriptors
2465 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002466 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2467 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2468 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2469 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2470 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2471 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2472 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473
2474 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002475 * If we're switching between Protected Mode and VM86, we need to make
2476 * sure to update the mode before loading the segment descriptors so
2477 * that the selectors are interpreted correctly.
2478 *
2479 * Need to get rflags to the vcpu struct immediately because it
2480 * influences the CPL which is checked at least when loading the segment
2481 * descriptors and when pushing an error code to the new kernel stack.
2482 *
2483 * TODO Introduce a separate ctxt->ops->set_cpl callback
2484 */
2485 if (ctxt->eflags & X86_EFLAGS_VM)
2486 ctxt->mode = X86EMUL_MODE_VM86;
2487 else
2488 ctxt->mode = X86EMUL_MODE_PROT32;
2489
2490 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2491
2492 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002493 * Now load segment descriptors. If fault happenes at this stage
2494 * it is handled in a context of new task
2495 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002496 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497 if (ret != X86EMUL_CONTINUE)
2498 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002499 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500 if (ret != X86EMUL_CONTINUE)
2501 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002502 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503 if (ret != X86EMUL_CONTINUE)
2504 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002505 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506 if (ret != X86EMUL_CONTINUE)
2507 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002508 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509 if (ret != X86EMUL_CONTINUE)
2510 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002511 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512 if (ret != X86EMUL_CONTINUE)
2513 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002514 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002515 if (ret != X86EMUL_CONTINUE)
2516 return ret;
2517
2518 return X86EMUL_CONTINUE;
2519}
2520
2521static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522 u16 tss_selector, u16 old_tss_sel,
2523 ulong old_tss_base, struct desc_struct *new_desc)
2524{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002525 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526 struct tss_segment_32 tss_seg;
2527 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002528 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529
Avi Kivity0f65dd72011-04-20 13:37:53 +03002530 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002531 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002532 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002533 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002535
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002536 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002537
Avi Kivity0f65dd72011-04-20 13:37:53 +03002538 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002539 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002540 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002542 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543
Avi Kivity0f65dd72011-04-20 13:37:53 +03002544 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002545 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002546 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549
2550 if (old_tss_sel != 0xffff) {
2551 tss_seg.prev_task_link = old_tss_sel;
2552
Avi Kivity0f65dd72011-04-20 13:37:53 +03002553 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554 &tss_seg.prev_task_link,
2555 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002556 &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
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002562 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563}
2564
2565static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002566 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002567 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002569 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 struct desc_struct curr_tss_desc, next_tss_desc;
2571 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002572 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002574 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002575 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576
2577 /* FIXME: old_tss_base == ~0 ? */
2578
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002579 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002580 if (ret != X86EMUL_CONTINUE)
2581 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002582 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002583 if (ret != X86EMUL_CONTINUE)
2584 return ret;
2585
2586 /* FIXME: check that next_tss_desc is tss */
2587
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002588 /*
2589 * Check privileges. The three cases are task switch caused by...
2590 *
2591 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2592 * 2. Exception/IRQ/iret: No check is performed
2593 * 3. jmp/call to TSS: Check agains DPL of the TSS
2594 */
2595 if (reason == TASK_SWITCH_GATE) {
2596 if (idt_index != -1) {
2597 /* Software interrupts */
2598 struct desc_struct task_gate_desc;
2599 int dpl;
2600
2601 ret = read_interrupt_descriptor(ctxt, idt_index,
2602 &task_gate_desc);
2603 if (ret != X86EMUL_CONTINUE)
2604 return ret;
2605
2606 dpl = task_gate_desc.dpl;
2607 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2608 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2609 }
2610 } else if (reason != TASK_SWITCH_IRET) {
2611 int dpl = next_tss_desc.dpl;
2612 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2613 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 }
2615
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002616
Gleb Natapovceffb452010-03-18 15:20:19 +02002617 desc_limit = desc_limit_scaled(&next_tss_desc);
2618 if (!next_tss_desc.p ||
2619 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2620 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002621 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002622 return X86EMUL_PROPAGATE_FAULT;
2623 }
2624
2625 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2626 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002627 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 }
2629
2630 if (reason == TASK_SWITCH_IRET)
2631 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2632
2633 /* set back link to prev task only if NT bit is set in eflags
2634 note that old_tss_sel is not used afetr this point */
2635 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2636 old_tss_sel = 0xffff;
2637
2638 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002639 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 old_tss_base, &next_tss_desc);
2641 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002642 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002644 if (ret != X86EMUL_CONTINUE)
2645 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646
2647 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2648 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2649
2650 if (reason != TASK_SWITCH_IRET) {
2651 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002652 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653 }
2654
Avi Kivity717746e2011-04-20 13:37:53 +03002655 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002656 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657
Jan Kiszkae269fb22010-04-14 15:51:09 +02002658 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002659 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2660 ctxt->lock_prefix = 0;
2661 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002662 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002663 }
2664
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 return ret;
2666}
2667
2668int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002669 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002670 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672 int rc;
2673
Avi Kivity9dac77f2011-06-01 15:34:25 +03002674 ctxt->_eip = ctxt->eip;
2675 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002677 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002678 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002679
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002680 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002681 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002683 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684}
2685
Avi Kivity90de84f2010-11-17 15:28:21 +02002686static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002687 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002688{
Gleb Natapova682e352010-03-18 15:20:21 +02002689 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2690
Avi Kivity9dac77f2011-06-01 15:34:25 +03002691 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2692 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002693 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002694}
2695
Avi Kivity7af04fc2010-08-18 14:16:35 +03002696static int em_das(struct x86_emulate_ctxt *ctxt)
2697{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002698 u8 al, old_al;
2699 bool af, cf, old_cf;
2700
2701 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002702 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002703
2704 old_al = al;
2705 old_cf = cf;
2706 cf = false;
2707 af = ctxt->eflags & X86_EFLAGS_AF;
2708 if ((al & 0x0f) > 9 || af) {
2709 al -= 6;
2710 cf = old_cf | (al >= 250);
2711 af = true;
2712 } else {
2713 af = false;
2714 }
2715 if (old_al > 0x99 || old_cf) {
2716 al -= 0x60;
2717 cf = true;
2718 }
2719
Avi Kivity9dac77f2011-06-01 15:34:25 +03002720 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002721 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002722 ctxt->src.type = OP_IMM;
2723 ctxt->src.val = 0;
2724 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002725 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002726 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2727 if (cf)
2728 ctxt->eflags |= X86_EFLAGS_CF;
2729 if (af)
2730 ctxt->eflags |= X86_EFLAGS_AF;
2731 return X86EMUL_CONTINUE;
2732}
2733
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002734static int em_call(struct x86_emulate_ctxt *ctxt)
2735{
2736 long rel = ctxt->src.val;
2737
2738 ctxt->src.val = (unsigned long)ctxt->_eip;
2739 jmp_rel(ctxt, rel);
2740 return em_push(ctxt);
2741}
2742
Avi Kivity0ef753b2010-08-18 14:51:45 +03002743static int em_call_far(struct x86_emulate_ctxt *ctxt)
2744{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002745 u16 sel, old_cs;
2746 ulong old_eip;
2747 int rc;
2748
Avi Kivity1aa36612011-04-27 13:20:30 +03002749 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002750 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002751
Avi Kivity9dac77f2011-06-01 15:34:25 +03002752 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002753 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002754 return X86EMUL_CONTINUE;
2755
Avi Kivity9dac77f2011-06-01 15:34:25 +03002756 ctxt->_eip = 0;
2757 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002758
Avi Kivity9dac77f2011-06-01 15:34:25 +03002759 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002760 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002761 if (rc != X86EMUL_CONTINUE)
2762 return rc;
2763
Avi Kivity9dac77f2011-06-01 15:34:25 +03002764 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002765 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002766}
2767
Avi Kivity40ece7c2010-08-18 15:12:09 +03002768static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2769{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002770 int rc;
2771
Avi Kivity9dac77f2011-06-01 15:34:25 +03002772 ctxt->dst.type = OP_REG;
2773 ctxt->dst.addr.reg = &ctxt->_eip;
2774 ctxt->dst.bytes = ctxt->op_bytes;
2775 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002776 if (rc != X86EMUL_CONTINUE)
2777 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002778 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002779 return X86EMUL_CONTINUE;
2780}
2781
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002782static int em_add(struct x86_emulate_ctxt *ctxt)
2783{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002784 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002785 return X86EMUL_CONTINUE;
2786}
2787
2788static int em_or(struct x86_emulate_ctxt *ctxt)
2789{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002790 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002791 return X86EMUL_CONTINUE;
2792}
2793
2794static int em_adc(struct x86_emulate_ctxt *ctxt)
2795{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002796 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002797 return X86EMUL_CONTINUE;
2798}
2799
2800static int em_sbb(struct x86_emulate_ctxt *ctxt)
2801{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002802 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002803 return X86EMUL_CONTINUE;
2804}
2805
2806static int em_and(struct x86_emulate_ctxt *ctxt)
2807{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002808 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002809 return X86EMUL_CONTINUE;
2810}
2811
2812static int em_sub(struct x86_emulate_ctxt *ctxt)
2813{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002814 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002815 return X86EMUL_CONTINUE;
2816}
2817
2818static int em_xor(struct x86_emulate_ctxt *ctxt)
2819{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002820 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002821 return X86EMUL_CONTINUE;
2822}
2823
2824static int em_cmp(struct x86_emulate_ctxt *ctxt)
2825{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002826 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002827 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002828 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002829 return X86EMUL_CONTINUE;
2830}
2831
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002832static int em_test(struct x86_emulate_ctxt *ctxt)
2833{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002834 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002835 /* Disable writeback. */
2836 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002837 return X86EMUL_CONTINUE;
2838}
2839
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002840static int em_xchg(struct x86_emulate_ctxt *ctxt)
2841{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002842 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002843 ctxt->src.val = ctxt->dst.val;
2844 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002845
2846 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002847 ctxt->dst.val = ctxt->src.orig_val;
2848 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002849 return X86EMUL_CONTINUE;
2850}
2851
Avi Kivity5c82aa22010-08-18 18:31:43 +03002852static int em_imul(struct x86_emulate_ctxt *ctxt)
2853{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002854 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002855 return X86EMUL_CONTINUE;
2856}
2857
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002858static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2859{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002860 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002861 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002862}
2863
Avi Kivity61429142010-08-19 15:13:00 +03002864static int em_cwd(struct x86_emulate_ctxt *ctxt)
2865{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002866 ctxt->dst.type = OP_REG;
2867 ctxt->dst.bytes = ctxt->src.bytes;
2868 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2869 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002870
2871 return X86EMUL_CONTINUE;
2872}
2873
Avi Kivity48bb5d32010-08-18 18:54:34 +03002874static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2875{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002876 u64 tsc = 0;
2877
Avi Kivity717746e2011-04-20 13:37:53 +03002878 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002879 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2880 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002881 return X86EMUL_CONTINUE;
2882}
2883
Avi Kivity222d21a2011-11-10 14:57:30 +02002884static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2885{
2886 u64 pmc;
2887
2888 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2889 return emulate_gp(ctxt, 0);
2890 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2891 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2892 return X86EMUL_CONTINUE;
2893}
2894
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002895static int em_mov(struct x86_emulate_ctxt *ctxt)
2896{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002897 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002898 return X86EMUL_CONTINUE;
2899}
2900
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002901static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2902{
2903 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2904 return emulate_gp(ctxt, 0);
2905
2906 /* Disable writeback. */
2907 ctxt->dst.type = OP_NONE;
2908 return X86EMUL_CONTINUE;
2909}
2910
2911static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2912{
2913 unsigned long val;
2914
2915 if (ctxt->mode == X86EMUL_MODE_PROT64)
2916 val = ctxt->src.val & ~0ULL;
2917 else
2918 val = ctxt->src.val & ~0U;
2919
2920 /* #UD condition is already handled. */
2921 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2922 return emulate_gp(ctxt, 0);
2923
2924 /* Disable writeback. */
2925 ctxt->dst.type = OP_NONE;
2926 return X86EMUL_CONTINUE;
2927}
2928
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002929static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2930{
2931 u64 msr_data;
2932
2933 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2934 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2935 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2936 return emulate_gp(ctxt, 0);
2937
2938 return X86EMUL_CONTINUE;
2939}
2940
2941static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2942{
2943 u64 msr_data;
2944
2945 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2946 return emulate_gp(ctxt, 0);
2947
2948 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2949 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2950 return X86EMUL_CONTINUE;
2951}
2952
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002953static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2954{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002955 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002956 return emulate_ud(ctxt);
2957
Avi Kivity9dac77f2011-06-01 15:34:25 +03002958 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002959 return X86EMUL_CONTINUE;
2960}
2961
2962static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2963{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002964 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002965
Avi Kivity9dac77f2011-06-01 15:34:25 +03002966 if (ctxt->modrm_reg == VCPU_SREG_CS || 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 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002970 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2971
2972 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002973 ctxt->dst.type = OP_NONE;
2974 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002975}
2976
Avi Kivity38503912011-03-31 18:48:09 +02002977static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2978{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002979 int rc;
2980 ulong linear;
2981
Avi Kivity9dac77f2011-06-01 15:34:25 +03002982 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002983 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002984 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002985 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002986 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002987 return X86EMUL_CONTINUE;
2988}
2989
Avi Kivity2d04a052011-04-20 15:32:49 +03002990static int em_clts(struct x86_emulate_ctxt *ctxt)
2991{
2992 ulong cr0;
2993
2994 cr0 = ctxt->ops->get_cr(ctxt, 0);
2995 cr0 &= ~X86_CR0_TS;
2996 ctxt->ops->set_cr(ctxt, 0, cr0);
2997 return X86EMUL_CONTINUE;
2998}
2999
Avi Kivity26d05cc2011-04-21 12:07:59 +03003000static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3001{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003002 int rc;
3003
Avi Kivity9dac77f2011-06-01 15:34:25 +03003004 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003005 return X86EMUL_UNHANDLEABLE;
3006
3007 rc = ctxt->ops->fix_hypercall(ctxt);
3008 if (rc != X86EMUL_CONTINUE)
3009 return rc;
3010
3011 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003012 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003013 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003014 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003015 return X86EMUL_CONTINUE;
3016}
3017
3018static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3019{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003020 struct desc_ptr desc_ptr;
3021 int rc;
3022
Avi Kivity510425f2012-06-07 17:04:36 +03003023 if (ctxt->mode == X86EMUL_MODE_PROT64)
3024 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003025 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003026 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003027 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003028 if (rc != X86EMUL_CONTINUE)
3029 return rc;
3030 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3031 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003032 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003033 return X86EMUL_CONTINUE;
3034}
3035
Avi Kivity5ef39c72011-04-21 12:21:50 +03003036static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003037{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003038 int rc;
3039
Avi Kivity5ef39c72011-04-21 12:21:50 +03003040 rc = ctxt->ops->fix_hypercall(ctxt);
3041
Avi Kivity26d05cc2011-04-21 12:07:59 +03003042 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003043 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003044 return rc;
3045}
3046
3047static int em_lidt(struct x86_emulate_ctxt *ctxt)
3048{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003049 struct desc_ptr desc_ptr;
3050 int rc;
3051
Avi Kivity510425f2012-06-07 17:04:36 +03003052 if (ctxt->mode == X86EMUL_MODE_PROT64)
3053 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003054 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003055 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003056 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003057 if (rc != X86EMUL_CONTINUE)
3058 return rc;
3059 ctxt->ops->set_idt(ctxt, &desc_ptr);
3060 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003061 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003062 return X86EMUL_CONTINUE;
3063}
3064
3065static int em_smsw(struct x86_emulate_ctxt *ctxt)
3066{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003067 ctxt->dst.bytes = 2;
3068 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003069 return X86EMUL_CONTINUE;
3070}
3071
3072static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3073{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003074 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003075 | (ctxt->src.val & 0x0f));
3076 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003077 return X86EMUL_CONTINUE;
3078}
3079
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003080static int em_loop(struct x86_emulate_ctxt *ctxt)
3081{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003082 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
3083 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
3084 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3085 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003086
3087 return X86EMUL_CONTINUE;
3088}
3089
3090static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3091{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
3093 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003094
3095 return X86EMUL_CONTINUE;
3096}
3097
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003098static int em_in(struct x86_emulate_ctxt *ctxt)
3099{
3100 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3101 &ctxt->dst.val))
3102 return X86EMUL_IO_NEEDED;
3103
3104 return X86EMUL_CONTINUE;
3105}
3106
3107static int em_out(struct x86_emulate_ctxt *ctxt)
3108{
3109 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3110 &ctxt->src.val, 1);
3111 /* Disable writeback. */
3112 ctxt->dst.type = OP_NONE;
3113 return X86EMUL_CONTINUE;
3114}
3115
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003116static int em_cli(struct x86_emulate_ctxt *ctxt)
3117{
3118 if (emulator_bad_iopl(ctxt))
3119 return emulate_gp(ctxt, 0);
3120
3121 ctxt->eflags &= ~X86_EFLAGS_IF;
3122 return X86EMUL_CONTINUE;
3123}
3124
3125static int em_sti(struct x86_emulate_ctxt *ctxt)
3126{
3127 if (emulator_bad_iopl(ctxt))
3128 return emulate_gp(ctxt, 0);
3129
3130 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3131 ctxt->eflags |= X86_EFLAGS_IF;
3132 return X86EMUL_CONTINUE;
3133}
3134
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003135static int em_bt(struct x86_emulate_ctxt *ctxt)
3136{
3137 /* Disable writeback. */
3138 ctxt->dst.type = OP_NONE;
3139 /* only subword offset */
3140 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3141
3142 emulate_2op_SrcV_nobyte(ctxt, "bt");
3143 return X86EMUL_CONTINUE;
3144}
3145
3146static int em_bts(struct x86_emulate_ctxt *ctxt)
3147{
3148 emulate_2op_SrcV_nobyte(ctxt, "bts");
3149 return X86EMUL_CONTINUE;
3150}
3151
3152static int em_btr(struct x86_emulate_ctxt *ctxt)
3153{
3154 emulate_2op_SrcV_nobyte(ctxt, "btr");
3155 return X86EMUL_CONTINUE;
3156}
3157
3158static int em_btc(struct x86_emulate_ctxt *ctxt)
3159{
3160 emulate_2op_SrcV_nobyte(ctxt, "btc");
3161 return X86EMUL_CONTINUE;
3162}
3163
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003164static int em_bsf(struct x86_emulate_ctxt *ctxt)
3165{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003166 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003167 return X86EMUL_CONTINUE;
3168}
3169
3170static int em_bsr(struct x86_emulate_ctxt *ctxt)
3171{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003172 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003173 return X86EMUL_CONTINUE;
3174}
3175
Avi Kivity6d6eede2012-06-07 14:11:36 +03003176static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3177{
3178 u32 eax, ebx, ecx, edx;
3179
3180 eax = ctxt->regs[VCPU_REGS_RAX];
3181 ecx = ctxt->regs[VCPU_REGS_RCX];
3182 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3183 ctxt->regs[VCPU_REGS_RAX] = eax;
3184 ctxt->regs[VCPU_REGS_RBX] = ebx;
3185 ctxt->regs[VCPU_REGS_RCX] = ecx;
3186 ctxt->regs[VCPU_REGS_RDX] = edx;
3187 return X86EMUL_CONTINUE;
3188}
3189
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003190static bool valid_cr(int nr)
3191{
3192 switch (nr) {
3193 case 0:
3194 case 2 ... 4:
3195 case 8:
3196 return true;
3197 default:
3198 return false;
3199 }
3200}
3201
3202static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3203{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003204 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003205 return emulate_ud(ctxt);
3206
3207 return X86EMUL_CONTINUE;
3208}
3209
3210static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3211{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003212 u64 new_val = ctxt->src.val64;
3213 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003214 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003215
3216 static u64 cr_reserved_bits[] = {
3217 0xffffffff00000000ULL,
3218 0, 0, 0, /* CR3 checked later */
3219 CR4_RESERVED_BITS,
3220 0, 0, 0,
3221 CR8_RESERVED_BITS,
3222 };
3223
3224 if (!valid_cr(cr))
3225 return emulate_ud(ctxt);
3226
3227 if (new_val & cr_reserved_bits[cr])
3228 return emulate_gp(ctxt, 0);
3229
3230 switch (cr) {
3231 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003232 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003233 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3234 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3235 return emulate_gp(ctxt, 0);
3236
Avi Kivity717746e2011-04-20 13:37:53 +03003237 cr4 = ctxt->ops->get_cr(ctxt, 4);
3238 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003239
3240 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3241 !(cr4 & X86_CR4_PAE))
3242 return emulate_gp(ctxt, 0);
3243
3244 break;
3245 }
3246 case 3: {
3247 u64 rsvd = 0;
3248
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003249 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3250 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003251 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003252 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003253 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003254 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003255 rsvd = CR3_NONPAE_RESERVED_BITS;
3256
3257 if (new_val & rsvd)
3258 return emulate_gp(ctxt, 0);
3259
3260 break;
3261 }
3262 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003263 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003264
3265 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3266 return emulate_gp(ctxt, 0);
3267
3268 break;
3269 }
3270 }
3271
3272 return X86EMUL_CONTINUE;
3273}
3274
Joerg Roedel3b88e412011-04-04 12:39:29 +02003275static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3276{
3277 unsigned long dr7;
3278
Avi Kivity717746e2011-04-20 13:37:53 +03003279 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003280
3281 /* Check if DR7.Global_Enable is set */
3282 return dr7 & (1 << 13);
3283}
3284
3285static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3286{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003287 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003288 u64 cr4;
3289
3290 if (dr > 7)
3291 return emulate_ud(ctxt);
3292
Avi Kivity717746e2011-04-20 13:37:53 +03003293 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003294 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3295 return emulate_ud(ctxt);
3296
3297 if (check_dr7_gd(ctxt))
3298 return emulate_db(ctxt);
3299
3300 return X86EMUL_CONTINUE;
3301}
3302
3303static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3304{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003305 u64 new_val = ctxt->src.val64;
3306 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003307
3308 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3309 return emulate_gp(ctxt, 0);
3310
3311 return check_dr_read(ctxt);
3312}
3313
Joerg Roedel01de8b02011-04-04 12:39:31 +02003314static int check_svme(struct x86_emulate_ctxt *ctxt)
3315{
3316 u64 efer;
3317
Avi Kivity717746e2011-04-20 13:37:53 +03003318 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003319
3320 if (!(efer & EFER_SVME))
3321 return emulate_ud(ctxt);
3322
3323 return X86EMUL_CONTINUE;
3324}
3325
3326static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3327{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003328 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003329
3330 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003331 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003332 return emulate_gp(ctxt, 0);
3333
3334 return check_svme(ctxt);
3335}
3336
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003337static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3338{
Avi Kivity717746e2011-04-20 13:37:53 +03003339 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003340
Avi Kivity717746e2011-04-20 13:37:53 +03003341 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003342 return emulate_ud(ctxt);
3343
3344 return X86EMUL_CONTINUE;
3345}
3346
Joerg Roedel80612522011-04-04 12:39:33 +02003347static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3348{
Avi Kivity717746e2011-04-20 13:37:53 +03003349 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003350 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003351
Avi Kivity717746e2011-04-20 13:37:53 +03003352 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003353 (rcx > 3))
3354 return emulate_gp(ctxt, 0);
3355
3356 return X86EMUL_CONTINUE;
3357}
3358
Joerg Roedelf6511932011-04-04 12:39:35 +02003359static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3360{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003361 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3362 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003363 return emulate_gp(ctxt, 0);
3364
3365 return X86EMUL_CONTINUE;
3366}
3367
3368static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3369{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003370 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3371 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003372 return emulate_gp(ctxt, 0);
3373
3374 return X86EMUL_CONTINUE;
3375}
3376
Avi Kivity73fba5f2010-07-29 15:11:53 +03003377#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003378#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003379#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3380 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003381#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003382#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003383#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3384#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003385#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003386#define II(_f, _e, _i) \
3387 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003388#define IIP(_f, _e, _i, _p) \
3389 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3390 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003391#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003392
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003393#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003394#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003395#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003396#define I2bvIP(_f, _e, _i, _p) \
3397 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003398
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003399#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3400 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3401 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003402
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003403static struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003404 DI(SrcNone | Priv, monitor),
3405 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003406 N, N, N, N, N, N,
3407};
3408
Joerg Roedel01de8b02011-04-04 12:39:31 +02003409static struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003410 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3411 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3412 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3413 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3414 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3415 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3416 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3417 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003418};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003419
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003420static struct opcode group7_rm7[] = {
3421 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003422 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003423 N, N, N, N, N, N,
3424};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003425
Avi Kivity73fba5f2010-07-29 15:11:53 +03003426static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003427 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003428 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003429 I(Lock, em_adc),
3430 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003431 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003432 I(Lock, em_sub),
3433 I(Lock, em_xor),
3434 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003435};
3436
3437static struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003438 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003439};
3440
3441static struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003442 I(DstMem | SrcImm, em_test),
3443 I(DstMem | SrcImm, em_test),
3444 I(DstMem | SrcNone | Lock, em_not),
3445 I(DstMem | SrcNone | Lock, em_neg),
3446 I(SrcMem, em_mul_ex),
3447 I(SrcMem, em_imul_ex),
3448 I(SrcMem, em_div_ex),
3449 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003450};
3451
3452static struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003453 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3454 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003455 N, N, N, N, N, N,
3456};
3457
3458static struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003459 I(DstMem | SrcNone | Lock, em_grp45),
3460 I(DstMem | SrcNone | Lock, em_grp45),
3461 I(SrcMem | Stack, em_grp45),
3462 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3463 I(SrcMem | Stack, em_grp45),
3464 I(SrcMemFAddr | ImplicitOps, em_grp45),
3465 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003466};
3467
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003468static struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003469 DI(Prot, sldt),
3470 DI(Prot, str),
3471 DI(Prot | Priv, lldt),
3472 DI(Prot | Priv, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003473 N, N, N, N,
3474};
3475
Avi Kivity73fba5f2010-07-29 15:11:53 +03003476static struct group_dual group7 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003477 DI(Mov | DstMem | Priv, sgdt),
3478 DI(Mov | DstMem | Priv, sidt),
3479 II(SrcMem | Priv, em_lgdt, lgdt),
3480 II(SrcMem | Priv, em_lidt, lidt),
3481 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3482 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3483 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003484}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003485 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003486 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003487 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003488 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3489 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3490 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003491} };
3492
3493static struct opcode group8[] = {
3494 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003495 I(DstMem | SrcImmByte, em_bt),
3496 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3497 I(DstMem | SrcImmByte | Lock, em_btr),
3498 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003499};
3500
3501static struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003502 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003503}, {
3504 N, N, N, N, N, N, N, N,
3505} };
3506
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003507static struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003508 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003509 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003510};
3511
Avi Kivityaa97bb42010-01-20 18:09:23 +02003512static struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003513 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003514};
3515
Avi Kivity3e114eb2012-04-09 18:40:01 +03003516static struct gprefix pfx_vmovntpx = {
3517 I(0, em_mov), N, N, N,
3518};
3519
Avi Kivity73fba5f2010-07-29 15:11:53 +03003520static struct opcode opcode_table[256] = {
3521 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003522 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003523 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3524 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003525 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003526 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003527 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3528 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003529 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003530 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003531 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3532 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003533 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003534 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003535 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3536 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003537 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003538 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003539 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003540 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003541 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003542 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003543 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003544 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003545 /* 0x40 - 0x4F */
3546 X16(D(DstReg)),
3547 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003548 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003549 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003550 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003551 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003552 I(ImplicitOps | Stack | No64, em_pusha),
3553 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003554 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3555 N, N, N, N,
3556 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003557 I(SrcImm | Mov | Stack, em_push),
3558 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003559 I(SrcImmByte | Mov | Stack, em_push),
3560 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003561 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3562 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003563 /* 0x70 - 0x7F */
3564 X16(D(SrcImmByte)),
3565 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003566 G(ByteOp | DstMem | SrcImm, group1),
3567 G(DstMem | SrcImm, group1),
3568 G(ByteOp | DstMem | SrcImm | No64, group1),
3569 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003570 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003571 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003572 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003573 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003574 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003575 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003576 D(ModRM | SrcMem | NoAccess | DstReg),
3577 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3578 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003579 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003580 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003581 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003582 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003583 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003584 II(ImplicitOps | Stack, em_pushf, pushf),
3585 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003586 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003587 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003588 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003589 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003590 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003591 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003592 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003593 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3594 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003595 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003596 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003597 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003598 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003599 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003600 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003601 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003602 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003603 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003604 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3605 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003606 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003607 /* 0xC8 - 0xCF */
Avi Kivityf47cfa32012-06-07 17:49:24 +03003608 N, I(Stack, em_leave), N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003609 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003610 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003611 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003612 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003613 N, N, N, N,
3614 /* 0xD8 - 0xDF */
3615 N, N, N, N, N, N, N, N,
3616 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003617 X3(I(SrcImmByte, em_loop)),
3618 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003619 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3620 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003621 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003622 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003623 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003624 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3625 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003626 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003627 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003628 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3629 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003630 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003631 D(ImplicitOps), D(ImplicitOps),
3632 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003633 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3634};
3635
3636static struct opcode twobyte_table[256] = {
3637 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003638 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003639 N, I(ImplicitOps | VendorSpecific, em_syscall),
3640 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003641 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003642 N, D(ImplicitOps | ModRM), N, N,
3643 /* 0x10 - 0x1F */
3644 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3645 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003646 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003647 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003648 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3649 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003650 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003651 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3652 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003653 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003654 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003655 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003656 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003657 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003658 I(ImplicitOps | VendorSpecific, em_sysenter),
3659 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003660 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661 N, N, N, N, N, N, N, N,
3662 /* 0x40 - 0x4F */
3663 X16(D(DstReg | SrcMem | ModRM | Mov)),
3664 /* 0x50 - 0x5F */
3665 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3666 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003667 N, N, N, N,
3668 N, N, N, N,
3669 N, N, N, N,
3670 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003671 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003672 N, N, N, N,
3673 N, N, N, N,
3674 N, N, N, N,
3675 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003676 /* 0x80 - 0x8F */
3677 X16(D(SrcImm)),
3678 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003679 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003680 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003681 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003682 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003683 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3684 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3685 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003686 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003687 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003688 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003689 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3690 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003691 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003692 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003693 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003694 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003695 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003696 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3697 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003698 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003699 /* 0xB8 - 0xBF */
3700 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003701 G(BitOp, group8),
3702 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003703 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003704 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003705 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003706 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003707 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003708 N, N, N, GD(0, &group9),
3709 N, N, N, N, N, N, N, N,
3710 /* 0xD0 - 0xDF */
3711 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3712 /* 0xE0 - 0xEF */
3713 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3714 /* 0xF0 - 0xFF */
3715 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3716};
3717
3718#undef D
3719#undef N
3720#undef G
3721#undef GD
3722#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003723#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003724#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003725
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003726#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003727#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003728#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003729#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003730#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003731
Avi Kivity9dac77f2011-06-01 15:34:25 +03003732static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003733{
3734 unsigned size;
3735
Avi Kivity9dac77f2011-06-01 15:34:25 +03003736 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003737 if (size == 8)
3738 size = 4;
3739 return size;
3740}
3741
3742static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3743 unsigned size, bool sign_extension)
3744{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003745 int rc = X86EMUL_CONTINUE;
3746
3747 op->type = OP_IMM;
3748 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003749 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003750 /* NB. Immediates are sign-extended as necessary. */
3751 switch (op->bytes) {
3752 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003753 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003754 break;
3755 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003756 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003757 break;
3758 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003759 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003760 break;
3761 }
3762 if (!sign_extension) {
3763 switch (op->bytes) {
3764 case 1:
3765 op->val &= 0xff;
3766 break;
3767 case 2:
3768 op->val &= 0xffff;
3769 break;
3770 case 4:
3771 op->val &= 0xffffffff;
3772 break;
3773 }
3774 }
3775done:
3776 return rc;
3777}
3778
Avi Kivitya9945542011-09-13 10:45:41 +03003779static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3780 unsigned d)
3781{
3782 int rc = X86EMUL_CONTINUE;
3783
3784 switch (d) {
3785 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003786 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03003787 break;
3788 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003789 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003790 break;
3791 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003792 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003793 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003794 *op = ctxt->memop;
3795 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003796 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003797 fetch_bit_operand(ctxt);
3798 op->orig_val = op->val;
3799 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003800 case OpMem64:
3801 ctxt->memop.bytes = 8;
3802 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003803 case OpAcc:
3804 op->type = OP_REG;
3805 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3806 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3807 fetch_register_operand(op);
3808 op->orig_val = op->val;
3809 break;
3810 case OpDI:
3811 op->type = OP_MEM;
3812 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3813 op->addr.mem.ea =
3814 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3815 op->addr.mem.seg = VCPU_SREG_ES;
3816 op->val = 0;
3817 break;
3818 case OpDX:
3819 op->type = OP_REG;
3820 op->bytes = 2;
3821 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3822 fetch_register_operand(op);
3823 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003824 case OpCL:
3825 op->bytes = 1;
3826 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3827 break;
3828 case OpImmByte:
3829 rc = decode_imm(ctxt, op, 1, true);
3830 break;
3831 case OpOne:
3832 op->bytes = 1;
3833 op->val = 1;
3834 break;
3835 case OpImm:
3836 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3837 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003838 case OpMem8:
3839 ctxt->memop.bytes = 1;
3840 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003841 case OpMem16:
3842 ctxt->memop.bytes = 2;
3843 goto mem_common;
3844 case OpMem32:
3845 ctxt->memop.bytes = 4;
3846 goto mem_common;
3847 case OpImmU16:
3848 rc = decode_imm(ctxt, op, 2, false);
3849 break;
3850 case OpImmU:
3851 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3852 break;
3853 case OpSI:
3854 op->type = OP_MEM;
3855 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3856 op->addr.mem.ea =
3857 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3858 op->addr.mem.seg = seg_override(ctxt);
3859 op->val = 0;
3860 break;
3861 case OpImmFAddr:
3862 op->type = OP_IMM;
3863 op->addr.mem.ea = ctxt->_eip;
3864 op->bytes = ctxt->op_bytes + 2;
3865 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3866 break;
3867 case OpMemFAddr:
3868 ctxt->memop.bytes = ctxt->op_bytes + 2;
3869 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003870 case OpES:
3871 op->val = VCPU_SREG_ES;
3872 break;
3873 case OpCS:
3874 op->val = VCPU_SREG_CS;
3875 break;
3876 case OpSS:
3877 op->val = VCPU_SREG_SS;
3878 break;
3879 case OpDS:
3880 op->val = VCPU_SREG_DS;
3881 break;
3882 case OpFS:
3883 op->val = VCPU_SREG_FS;
3884 break;
3885 case OpGS:
3886 op->val = VCPU_SREG_GS;
3887 break;
Avi Kivitya9945542011-09-13 10:45:41 +03003888 case OpImplicit:
3889 /* Special instructions do their own operand decoding. */
3890 default:
3891 op->type = OP_NONE; /* Disable writeback. */
3892 break;
3893 }
3894
3895done:
3896 return rc;
3897}
3898
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003899int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003900{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003901 int rc = X86EMUL_CONTINUE;
3902 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003903 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003904 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003905 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003906
Avi Kivityf09ed832011-09-13 10:45:40 +03003907 ctxt->memop.type = OP_NONE;
3908 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003909 ctxt->_eip = ctxt->eip;
3910 ctxt->fetch.start = ctxt->_eip;
3911 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003912 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003913 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003914
3915 switch (mode) {
3916 case X86EMUL_MODE_REAL:
3917 case X86EMUL_MODE_VM86:
3918 case X86EMUL_MODE_PROT16:
3919 def_op_bytes = def_ad_bytes = 2;
3920 break;
3921 case X86EMUL_MODE_PROT32:
3922 def_op_bytes = def_ad_bytes = 4;
3923 break;
3924#ifdef CONFIG_X86_64
3925 case X86EMUL_MODE_PROT64:
3926 def_op_bytes = 4;
3927 def_ad_bytes = 8;
3928 break;
3929#endif
3930 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003931 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003932 }
3933
Avi Kivity9dac77f2011-06-01 15:34:25 +03003934 ctxt->op_bytes = def_op_bytes;
3935 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003936
3937 /* Legacy prefixes. */
3938 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003939 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003940 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003941 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003942 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003943 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003944 break;
3945 case 0x67: /* address-size override */
3946 if (mode == X86EMUL_MODE_PROT64)
3947 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003948 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003949 else
3950 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003951 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003952 break;
3953 case 0x26: /* ES override */
3954 case 0x2e: /* CS override */
3955 case 0x36: /* SS override */
3956 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003957 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003958 break;
3959 case 0x64: /* FS override */
3960 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003961 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003962 break;
3963 case 0x40 ... 0x4f: /* REX */
3964 if (mode != X86EMUL_MODE_PROT64)
3965 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003966 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003967 continue;
3968 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003969 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003970 break;
3971 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003972 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003973 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003974 break;
3975 default:
3976 goto done_prefixes;
3977 }
3978
3979 /* Any legacy prefix after a REX prefix nullifies its effect. */
3980
Avi Kivity9dac77f2011-06-01 15:34:25 +03003981 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003982 }
3983
3984done_prefixes:
3985
3986 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003987 if (ctxt->rex_prefix & 8)
3988 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003989
3990 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003991 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003992 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003993 if (ctxt->b == 0x0f) {
3994 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003995 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003996 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003997 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003998 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003999
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004000 if (ctxt->d & ModRM)
4001 ctxt->modrm = insn_fetch(u8, ctxt);
4002
Avi Kivity9dac77f2011-06-01 15:34:25 +03004003 while (ctxt->d & GroupMask) {
4004 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004005 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004006 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004007 opcode = opcode.u.group[goffset];
4008 break;
4009 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004010 goffset = (ctxt->modrm >> 3) & 7;
4011 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004012 opcode = opcode.u.gdual->mod3[goffset];
4013 else
4014 opcode = opcode.u.gdual->mod012[goffset];
4015 break;
4016 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004017 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004018 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004019 break;
4020 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004022 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004023 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004024 switch (simd_prefix) {
4025 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4026 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4027 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4028 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4029 }
4030 break;
4031 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004032 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004033 }
Avi Kivity46561642011-04-24 14:09:59 +03004034
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004035 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004036 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004037 }
4038
Avi Kivity9dac77f2011-06-01 15:34:25 +03004039 ctxt->execute = opcode.u.execute;
4040 ctxt->check_perm = opcode.check_perm;
4041 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004042
4043 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004044 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004045 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004046
Avi Kivity9dac77f2011-06-01 15:34:25 +03004047 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004048 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004049
Avi Kivity9dac77f2011-06-01 15:34:25 +03004050 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4051 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004052
Avi Kivity9dac77f2011-06-01 15:34:25 +03004053 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004054 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004055 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004056 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004057 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004058 }
4059
Avi Kivity9dac77f2011-06-01 15:34:25 +03004060 if (ctxt->d & Sse)
4061 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004062 else if (ctxt->d & Mmx)
4063 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004064
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004065 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004066 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004067 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004068 if (!ctxt->has_seg_override)
4069 set_seg_override(ctxt, ctxt->modrm_seg);
4070 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004071 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004072 if (rc != X86EMUL_CONTINUE)
4073 goto done;
4074
Avi Kivity9dac77f2011-06-01 15:34:25 +03004075 if (!ctxt->has_seg_override)
4076 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004077
Avi Kivityf09ed832011-09-13 10:45:40 +03004078 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004079
Avi Kivityf09ed832011-09-13 10:45:40 +03004080 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4081 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004082
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004083 /*
4084 * Decode and fetch the source operand: register, memory
4085 * or immediate.
4086 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004087 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004088 if (rc != X86EMUL_CONTINUE)
4089 goto done;
4090
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004091 /*
4092 * Decode and fetch the second source operand: register, memory
4093 * or immediate.
4094 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004095 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004096 if (rc != X86EMUL_CONTINUE)
4097 goto done;
4098
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004099 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004100 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004101
4102done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004103 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4104 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004105
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004106 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004107}
4108
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004109bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4110{
4111 return ctxt->d & PageTable;
4112}
4113
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004114static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4115{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004116 /* The second termination condition only applies for REPE
4117 * and REPNE. Test if the repeat string operation prefix is
4118 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4119 * corresponding termination condition according to:
4120 * - if REPE/REPZ and ZF = 0 then done
4121 * - if REPNE/REPNZ and ZF = 1 then done
4122 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004123 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4124 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4125 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004126 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004127 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004128 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4129 return true;
4130
4131 return false;
4132}
4133
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004134static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4135{
4136 bool fault = false;
4137
4138 ctxt->ops->get_fpu(ctxt);
4139 asm volatile("1: fwait \n\t"
4140 "2: \n\t"
4141 ".pushsection .fixup,\"ax\" \n\t"
4142 "3: \n\t"
4143 "movb $1, %[fault] \n\t"
4144 "jmp 2b \n\t"
4145 ".popsection \n\t"
4146 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004147 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004148 ctxt->ops->put_fpu(ctxt);
4149
4150 if (unlikely(fault))
4151 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4152
4153 return X86EMUL_CONTINUE;
4154}
4155
4156static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4157 struct operand *op)
4158{
4159 if (op->type == OP_MM)
4160 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4161}
4162
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004163int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004164{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004165 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004166 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004167 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004168
Avi Kivity9dac77f2011-06-01 15:34:25 +03004169 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004170
Avi Kivity9dac77f2011-06-01 15:34:25 +03004171 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004172 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004173 goto done;
4174 }
4175
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004176 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004177 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004178 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004179 goto done;
4180 }
4181
Avi Kivity9dac77f2011-06-01 15:34:25 +03004182 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004183 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004184 goto done;
4185 }
4186
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004187 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4188 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004189 rc = emulate_ud(ctxt);
4190 goto done;
4191 }
4192
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004193 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004194 rc = emulate_nm(ctxt);
4195 goto done;
4196 }
4197
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004198 if (ctxt->d & Mmx) {
4199 rc = flush_pending_x87_faults(ctxt);
4200 if (rc != X86EMUL_CONTINUE)
4201 goto done;
4202 /*
4203 * Now that we know the fpu is exception safe, we can fetch
4204 * operands from it.
4205 */
4206 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4207 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4208 if (!(ctxt->d & Mov))
4209 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4210 }
4211
Avi Kivity9dac77f2011-06-01 15:34:25 +03004212 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4213 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004214 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004215 if (rc != X86EMUL_CONTINUE)
4216 goto done;
4217 }
4218
Gleb Natapove92805a2010-02-10 14:21:35 +02004219 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004220 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004221 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004222 goto done;
4223 }
4224
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004225 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004226 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004227 rc = emulate_ud(ctxt);
4228 goto done;
4229 }
4230
Joerg Roedeld09beab2011-04-04 12:39:25 +02004231 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004232 if (ctxt->check_perm) {
4233 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004234 if (rc != X86EMUL_CONTINUE)
4235 goto done;
4236 }
4237
Avi Kivity9dac77f2011-06-01 15:34:25 +03004238 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4239 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004240 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004241 if (rc != X86EMUL_CONTINUE)
4242 goto done;
4243 }
4244
Avi Kivity9dac77f2011-06-01 15:34:25 +03004245 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004246 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004247 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4248 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004249 goto done;
4250 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004251 }
4252
Avi Kivity9dac77f2011-06-01 15:34:25 +03004253 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4254 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4255 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004256 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004257 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004258 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004259 }
4260
Avi Kivity9dac77f2011-06-01 15:34:25 +03004261 if (ctxt->src2.type == OP_MEM) {
4262 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4263 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004264 if (rc != X86EMUL_CONTINUE)
4265 goto done;
4266 }
4267
Avi Kivity9dac77f2011-06-01 15:34:25 +03004268 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004269 goto special_insn;
4270
4271
Avi Kivity9dac77f2011-06-01 15:34:25 +03004272 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004273 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004274 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4275 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004276 if (rc != X86EMUL_CONTINUE)
4277 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004278 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004279 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004280
Avi Kivity018a98d2007-11-27 19:30:56 +02004281special_insn:
4282
Avi Kivity9dac77f2011-06-01 15:34:25 +03004283 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4284 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004285 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004286 if (rc != X86EMUL_CONTINUE)
4287 goto done;
4288 }
4289
Avi Kivity9dac77f2011-06-01 15:34:25 +03004290 if (ctxt->execute) {
4291 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004292 if (rc != X86EMUL_CONTINUE)
4293 goto done;
4294 goto writeback;
4295 }
4296
Avi Kivity9dac77f2011-06-01 15:34:25 +03004297 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004298 goto twobyte_insn;
4299
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004301 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004302 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004303 break;
4304 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004305 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004306 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004307 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004308 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004309 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004310 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004311 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004312 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004313 if (test_cc(ctxt->b, ctxt->eflags))
4314 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004315 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004316 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004317 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004318 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004319 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004320 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004321 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004322 rc = em_xchg(ctxt);
4323 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004324 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004325 switch (ctxt->op_bytes) {
4326 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4327 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4328 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004329 }
4330 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004331 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004332 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004333 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004334 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004335 rc = emulate_int(ctxt, 3);
4336 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004337 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004338 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004339 break;
4340 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004341 if (ctxt->eflags & EFLG_OF)
4342 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004343 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004344 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004345 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004346 break;
4347 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004349 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004350 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004351 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004352 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 jmp_rel(ctxt, ctxt->src.val);
4354 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004355 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004356 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004357 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004358 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004359 case 0xf5: /* cmc */
4360 /* complement carry flag from eflags reg */
4361 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004362 break;
4363 case 0xf8: /* clc */
4364 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004365 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004366 case 0xf9: /* stc */
4367 ctxt->eflags |= EFLG_CF;
4368 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004369 case 0xfc: /* cld */
4370 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004371 break;
4372 case 0xfd: /* std */
4373 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004374 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004375 default:
4376 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004377 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004378
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004379 if (rc != X86EMUL_CONTINUE)
4380 goto done;
4381
Avi Kivity018a98d2007-11-27 19:30:56 +02004382writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004383 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004384 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004385 goto done;
4386
Gleb Natapov5cd21912010-03-18 15:20:26 +02004387 /*
4388 * restore dst type in case the decoding will be reused
4389 * (happens for string instruction )
4390 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004391 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004392
Avi Kivity9dac77f2011-06-01 15:34:25 +03004393 if ((ctxt->d & SrcMask) == SrcSI)
4394 string_addr_inc(ctxt, seg_override(ctxt),
4395 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004396
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004398 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004399 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004400
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 if (ctxt->rep_prefix && (ctxt->d & String)) {
4402 struct read_cache *r = &ctxt->io_read;
4403 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004404
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004405 if (!string_insn_completed(ctxt)) {
4406 /*
4407 * Re-enter guest when pio read ahead buffer is empty
4408 * or, if it is not used, after each 1024 iteration.
4409 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004410 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004411 (r->end == 0 || r->end != r->pos)) {
4412 /*
4413 * Reset read cache. Usually happens before
4414 * decode, but since instruction is restarted
4415 * we have to do it here.
4416 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004417 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004418 return EMULATION_RESTART;
4419 }
4420 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004421 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004422 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004423
Avi Kivity9dac77f2011-06-01 15:34:25 +03004424 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004425
4426done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004427 if (rc == X86EMUL_PROPAGATE_FAULT)
4428 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004429 if (rc == X86EMUL_INTERCEPTED)
4430 return EMULATION_INTERCEPTED;
4431
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004432 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433
4434twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004436 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004437 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004438 break;
4439 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004440 case 0x0d: /* GrpP (prefetch) */
4441 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004442 break;
4443 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004445 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004447 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004448 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4451 if (!test_cc(ctxt->b, ctxt->eflags))
4452 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004454 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004455 if (test_cc(ctxt->b, ctxt->eflags))
4456 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004457 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004458 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004459 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004460 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004461 case 0xa4: /* shld imm8, r, r/m */
4462 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004463 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004464 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004465 case 0xac: /* shrd imm8, r, r/m */
4466 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004467 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004468 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004469 case 0xae: /* clflush */
4470 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004472 ctxt->dst.bytes = ctxt->op_bytes;
4473 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4474 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004476 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004477 ctxt->dst.bytes = ctxt->op_bytes;
4478 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4479 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004481 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004482 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004483 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 ctxt->src.val = ctxt->dst.orig_val;
4485 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004486 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004487 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 ctxt->dst.bytes = ctxt->op_bytes;
4489 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4490 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004491 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004492 default:
4493 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004494 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004495
4496 if (rc != X86EMUL_CONTINUE)
4497 goto done;
4498
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499 goto writeback;
4500
4501cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004502 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004503}