blob: acc647d637019999300c8c942e2a5aea92d1bb73 [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 Kivitya6e34072012-06-10 17:15:39 +0300977static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
978{
979 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
980 ctxt->modrm_seg = VCPU_SREG_SS;
981}
982
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200983static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300984 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200985{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200986 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700987 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900988 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300989 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200990
Avi Kivity9dac77f2011-06-01 15:34:25 +0300991 if (ctxt->rex_prefix) {
992 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
993 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
994 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200995 }
996
Avi Kivity9dac77f2011-06-01 15:34:25 +0300997 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
998 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
999 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1000 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001
Avi Kivity9dac77f2011-06-01 15:34:25 +03001002 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001003 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001004 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1005 op->addr.reg = decode_register(ctxt->modrm_rm,
1006 ctxt->regs, ctxt->d & ByteOp);
1007 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001008 op->type = OP_XMM;
1009 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001010 op->addr.xmm = ctxt->modrm_rm;
1011 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001012 return rc;
1013 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001014 if (ctxt->d & Mmx) {
1015 op->type = OP_MM;
1016 op->bytes = 8;
1017 op->addr.xmm = ctxt->modrm_rm & 7;
1018 return rc;
1019 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001020 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001021 return rc;
1022 }
1023
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001024 op->type = OP_MEM;
1025
Avi Kivity9dac77f2011-06-01 15:34:25 +03001026 if (ctxt->ad_bytes == 2) {
1027 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
1028 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
1029 unsigned si = ctxt->regs[VCPU_REGS_RSI];
1030 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001031
1032 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001033 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001034 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001035 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001036 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001037 break;
1038 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001039 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001040 break;
1041 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001042 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001043 break;
1044 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001045 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001046 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001047 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001048 break;
1049 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001050 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001051 break;
1052 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001053 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001054 break;
1055 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001056 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001057 break;
1058 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001059 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001060 break;
1061 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001062 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001063 break;
1064 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001065 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001066 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001067 break;
1068 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001069 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001070 break;
1071 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001072 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1073 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1074 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001075 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001076 } else {
1077 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001078 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001079 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001080 index_reg |= (sib >> 3) & 7;
1081 base_reg |= sib & 7;
1082 scale = sib >> 6;
1083
Avi Kivity9dac77f2011-06-01 15:34:25 +03001084 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001085 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001086 else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001087 modrm_ea += ctxt->regs[base_reg];
Avi Kivitya6e34072012-06-10 17:15:39 +03001088 adjust_modrm_seg(ctxt, base_reg);
1089 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001090 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001091 modrm_ea += ctxt->regs[index_reg] << scale;
1092 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001093 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001094 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001095 } else {
1096 base_reg = ctxt->modrm_rm;
1097 modrm_ea += ctxt->regs[base_reg];
1098 adjust_modrm_seg(ctxt, base_reg);
1099 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001100 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001101 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001102 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001103 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001104 break;
1105 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001106 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001107 break;
1108 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001109 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001110 break;
1111 }
1112 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001113 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114done:
1115 return rc;
1116}
1117
1118static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001119 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001121 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001123 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001124 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001126 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127 break;
1128 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001129 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130 break;
1131 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001132 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001133 break;
1134 }
1135done:
1136 return rc;
1137}
1138
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001140{
Sheng Yang7129eec2010-09-28 16:33:32 +08001141 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001142
Avi Kivity9dac77f2011-06-01 15:34:25 +03001143 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1144 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001145
Avi Kivity9dac77f2011-06-01 15:34:25 +03001146 if (ctxt->src.bytes == 2)
1147 sv = (s16)ctxt->src.val & (s16)mask;
1148 else if (ctxt->src.bytes == 4)
1149 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001150
Avi Kivity9dac77f2011-06-01 15:34:25 +03001151 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001152 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001153
1154 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001155 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001156}
1157
Gleb Natapov9de41572010-04-28 19:15:22 +03001158static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001159 unsigned long addr, void *dest, unsigned size)
1160{
1161 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001163
1164 while (size) {
1165 int n = min(size, 8u);
1166 size -= n;
1167 if (mc->pos < mc->end)
1168 goto read_cached;
1169
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001170 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1171 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001172 if (rc != X86EMUL_CONTINUE)
1173 return rc;
1174 mc->end += n;
1175
1176 read_cached:
1177 memcpy(dest, mc->data + mc->pos, n);
1178 mc->pos += n;
1179 dest += n;
1180 addr += n;
1181 }
1182 return X86EMUL_CONTINUE;
1183}
1184
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001185static int segmented_read(struct x86_emulate_ctxt *ctxt,
1186 struct segmented_address addr,
1187 void *data,
1188 unsigned size)
1189{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001190 int rc;
1191 ulong linear;
1192
Avi Kivity83b87952011-04-03 11:31:19 +03001193 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001194 if (rc != X86EMUL_CONTINUE)
1195 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001196 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001197}
1198
1199static int segmented_write(struct x86_emulate_ctxt *ctxt,
1200 struct segmented_address addr,
1201 const void *data,
1202 unsigned size)
1203{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001204 int rc;
1205 ulong linear;
1206
Avi Kivity83b87952011-04-03 11:31:19 +03001207 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001208 if (rc != X86EMUL_CONTINUE)
1209 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001210 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1211 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001212}
1213
1214static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1215 struct segmented_address addr,
1216 const void *orig_data, const void *data,
1217 unsigned size)
1218{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001219 int rc;
1220 ulong linear;
1221
Avi Kivity83b87952011-04-03 11:31:19 +03001222 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001223 if (rc != X86EMUL_CONTINUE)
1224 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001225 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1226 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001227}
1228
Gleb Natapov7b262e92010-03-18 15:20:27 +02001229static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001230 unsigned int size, unsigned short port,
1231 void *dest)
1232{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001234
1235 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001236 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001237 unsigned int count = ctxt->rep_prefix ?
1238 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001239 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001240 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1241 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001242 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1243 count);
1244 if (n == 0)
1245 n = 1;
1246 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001247 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001248 return 0;
1249 rc->end = n * size;
1250 }
1251
1252 memcpy(dest, rc->data + rc->pos, size);
1253 rc->pos += size;
1254 return 1;
1255}
1256
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001257static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1258 u16 index, struct desc_struct *desc)
1259{
1260 struct desc_ptr dt;
1261 ulong addr;
1262
1263 ctxt->ops->get_idt(ctxt, &dt);
1264
1265 if (dt.size < index * 8 + 7)
1266 return emulate_gp(ctxt, index << 3 | 0x2);
1267
1268 addr = dt.address + index * 8;
1269 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1270 &ctxt->exception);
1271}
1272
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001273static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001274 u16 selector, struct desc_ptr *dt)
1275{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001276 struct x86_emulate_ops *ops = ctxt->ops;
1277
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001278 if (selector & 1 << 2) {
1279 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001280 u16 sel;
1281
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001282 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001283 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001284 return;
1285
1286 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1287 dt->address = get_desc_base(&desc);
1288 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001289 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001290}
1291
1292/* allowed just for 8 bytes segments */
1293static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001294 u16 selector, struct desc_struct *desc)
1295{
1296 struct desc_ptr dt;
1297 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001298 ulong addr;
1299
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001300 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001301
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001302 if (dt.size < index * 8 + 7)
1303 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001304
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001305 addr = dt.address + index * 8;
1306 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1307 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001308}
1309
1310/* allowed just for 8 bytes segments */
1311static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001312 u16 selector, struct desc_struct *desc)
1313{
1314 struct desc_ptr dt;
1315 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001316 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001317
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001318 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001319
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001320 if (dt.size < index * 8 + 7)
1321 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001322
1323 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001324 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1325 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001326}
1327
Gleb Natapov5601d052011-03-07 14:55:06 +02001328/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001329static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001330 u16 selector, int seg)
1331{
1332 struct desc_struct seg_desc;
1333 u8 dpl, rpl, cpl;
1334 unsigned err_vec = GP_VECTOR;
1335 u32 err_code = 0;
1336 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1337 int ret;
1338
1339 memset(&seg_desc, 0, sizeof seg_desc);
1340
1341 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1342 || ctxt->mode == X86EMUL_MODE_REAL) {
1343 /* set real mode segment descriptor */
1344 set_desc_base(&seg_desc, selector << 4);
1345 set_desc_limit(&seg_desc, 0xffff);
1346 seg_desc.type = 3;
1347 seg_desc.p = 1;
1348 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001349 if (ctxt->mode == X86EMUL_MODE_VM86)
1350 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001351 goto load;
1352 }
1353
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001354 rpl = selector & 3;
1355 cpl = ctxt->ops->cpl(ctxt);
1356
1357 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1358 if ((seg == VCPU_SREG_CS
1359 || (seg == VCPU_SREG_SS
1360 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1361 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001362 && null_selector)
1363 goto exception;
1364
1365 /* TR should be in GDT only */
1366 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1367 goto exception;
1368
1369 if (null_selector) /* for NULL selector skip all following checks */
1370 goto load;
1371
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001372 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001373 if (ret != X86EMUL_CONTINUE)
1374 return ret;
1375
1376 err_code = selector & 0xfffc;
1377 err_vec = GP_VECTOR;
1378
1379 /* can't load system descriptor into segment selecor */
1380 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1381 goto exception;
1382
1383 if (!seg_desc.p) {
1384 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1385 goto exception;
1386 }
1387
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001388 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001389
1390 switch (seg) {
1391 case VCPU_SREG_SS:
1392 /*
1393 * segment is not a writable data segment or segment
1394 * selector's RPL != CPL or segment selector's RPL != CPL
1395 */
1396 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1397 goto exception;
1398 break;
1399 case VCPU_SREG_CS:
1400 if (!(seg_desc.type & 8))
1401 goto exception;
1402
1403 if (seg_desc.type & 4) {
1404 /* conforming */
1405 if (dpl > cpl)
1406 goto exception;
1407 } else {
1408 /* nonconforming */
1409 if (rpl > cpl || dpl != cpl)
1410 goto exception;
1411 }
1412 /* CS(RPL) <- CPL */
1413 selector = (selector & 0xfffc) | cpl;
1414 break;
1415 case VCPU_SREG_TR:
1416 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1417 goto exception;
1418 break;
1419 case VCPU_SREG_LDTR:
1420 if (seg_desc.s || seg_desc.type != 2)
1421 goto exception;
1422 break;
1423 default: /* DS, ES, FS, or GS */
1424 /*
1425 * segment is not a data or readable code segment or
1426 * ((segment is a data or nonconforming code segment)
1427 * and (both RPL and CPL > DPL))
1428 */
1429 if ((seg_desc.type & 0xa) == 0x8 ||
1430 (((seg_desc.type & 0xc) != 0xc) &&
1431 (rpl > dpl && cpl > dpl)))
1432 goto exception;
1433 break;
1434 }
1435
1436 if (seg_desc.s) {
1437 /* mark segment as accessed */
1438 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001439 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440 if (ret != X86EMUL_CONTINUE)
1441 return ret;
1442 }
1443load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001444 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001445 return X86EMUL_CONTINUE;
1446exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001447 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448 return X86EMUL_PROPAGATE_FAULT;
1449}
1450
Wei Yongjun31be40b2010-08-17 09:17:30 +08001451static void write_register_operand(struct operand *op)
1452{
1453 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1454 switch (op->bytes) {
1455 case 1:
1456 *(u8 *)op->addr.reg = (u8)op->val;
1457 break;
1458 case 2:
1459 *(u16 *)op->addr.reg = (u16)op->val;
1460 break;
1461 case 4:
1462 *op->addr.reg = (u32)op->val;
1463 break; /* 64b: zero-extend */
1464 case 8:
1465 *op->addr.reg = op->val;
1466 break;
1467 }
1468}
1469
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001470static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001471{
1472 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001473
Avi Kivity9dac77f2011-06-01 15:34:25 +03001474 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001475 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001476 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001477 break;
1478 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001479 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001480 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001481 ctxt->dst.addr.mem,
1482 &ctxt->dst.orig_val,
1483 &ctxt->dst.val,
1484 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001485 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001486 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001487 ctxt->dst.addr.mem,
1488 &ctxt->dst.val,
1489 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001490 if (rc != X86EMUL_CONTINUE)
1491 return rc;
1492 break;
Avi Kivity12537912011-03-29 11:41:27 +02001493 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001494 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001495 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001496 case OP_MM:
1497 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1498 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001499 case OP_NONE:
1500 /* no writeback */
1501 break;
1502 default:
1503 break;
1504 }
1505 return X86EMUL_CONTINUE;
1506}
1507
Avi Kivity51ddff52012-06-12 20:19:40 +03001508static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001509{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001510 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001511
Avi Kivity51ddff52012-06-12 20:19:40 +03001512 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -bytes);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001513 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001514 addr.seg = VCPU_SREG_SS;
1515
Avi Kivity51ddff52012-06-12 20:19:40 +03001516 return segmented_write(ctxt, addr, data, bytes);
1517}
1518
1519static int em_push(struct x86_emulate_ctxt *ctxt)
1520{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001521 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001522 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001523 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001524}
1525
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001526static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001527 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001528{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001530 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001531
Avi Kivity9dac77f2011-06-01 15:34:25 +03001532 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001533 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001534 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001535 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001536 return rc;
1537
Avi Kivity9dac77f2011-06-01 15:34:25 +03001538 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001539 return rc;
1540}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001541
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001542static int em_pop(struct x86_emulate_ctxt *ctxt)
1543{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001544 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001545}
1546
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001547static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001548 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001549{
1550 int rc;
1551 unsigned long val, change_mask;
1552 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001553 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001554
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001555 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001556 if (rc != X86EMUL_CONTINUE)
1557 return rc;
1558
1559 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1560 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1561
1562 switch(ctxt->mode) {
1563 case X86EMUL_MODE_PROT64:
1564 case X86EMUL_MODE_PROT32:
1565 case X86EMUL_MODE_PROT16:
1566 if (cpl == 0)
1567 change_mask |= EFLG_IOPL;
1568 if (cpl <= iopl)
1569 change_mask |= EFLG_IF;
1570 break;
1571 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001572 if (iopl < 3)
1573 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001574 change_mask |= EFLG_IF;
1575 break;
1576 default: /* real mode */
1577 change_mask |= (EFLG_IOPL | EFLG_IF);
1578 break;
1579 }
1580
1581 *(unsigned long *)dest =
1582 (ctxt->eflags & ~change_mask) | (val & change_mask);
1583
1584 return rc;
1585}
1586
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001587static int em_popf(struct x86_emulate_ctxt *ctxt)
1588{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001589 ctxt->dst.type = OP_REG;
1590 ctxt->dst.addr.reg = &ctxt->eflags;
1591 ctxt->dst.bytes = ctxt->op_bytes;
1592 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001593}
1594
Avi Kivityf47cfa32012-06-07 17:49:24 +03001595static int em_leave(struct x86_emulate_ctxt *ctxt)
1596{
1597 assign_masked(&ctxt->regs[VCPU_REGS_RSP], ctxt->regs[VCPU_REGS_RBP],
1598 stack_mask(ctxt));
1599 return emulate_pop(ctxt, &ctxt->regs[VCPU_REGS_RBP], ctxt->op_bytes);
1600}
1601
Avi Kivity1cd196e2011-09-13 10:45:51 +03001602static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001603{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001604 int seg = ctxt->src2.val;
1605
Avi Kivity9dac77f2011-06-01 15:34:25 +03001606 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001607
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001608 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001609}
1610
Avi Kivity1cd196e2011-09-13 10:45:51 +03001611static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001612{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001613 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001614 unsigned long selector;
1615 int rc;
1616
Avi Kivity9dac77f2011-06-01 15:34:25 +03001617 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001618 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001619 return rc;
1620
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001621 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001622 return rc;
1623}
1624
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001625static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001626{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001627 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001628 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001629 int reg = VCPU_REGS_RAX;
1630
1631 while (reg <= VCPU_REGS_RDI) {
1632 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001633 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001634
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001635 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001636 if (rc != X86EMUL_CONTINUE)
1637 return rc;
1638
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001639 ++reg;
1640 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001641
Wei Yongjunc37eda12010-06-15 09:03:33 +08001642 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001643}
1644
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001645static int em_pushf(struct x86_emulate_ctxt *ctxt)
1646{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001647 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001648 return em_push(ctxt);
1649}
1650
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001651static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001652{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001653 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001654 int reg = VCPU_REGS_RDI;
1655
1656 while (reg >= VCPU_REGS_RAX) {
1657 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001658 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1659 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001660 --reg;
1661 }
1662
Avi Kivity9dac77f2011-06-01 15:34:25 +03001663 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001664 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001665 break;
1666 --reg;
1667 }
1668 return rc;
1669}
1670
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001671int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001672{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001673 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001674 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001675 struct desc_ptr dt;
1676 gva_t cs_addr;
1677 gva_t eip_addr;
1678 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001679
1680 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001681 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001682 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001683 if (rc != X86EMUL_CONTINUE)
1684 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001685
1686 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1687
Avi Kivity9dac77f2011-06-01 15:34:25 +03001688 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001689 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001690 if (rc != X86EMUL_CONTINUE)
1691 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001692
Avi Kivity9dac77f2011-06-01 15:34:25 +03001693 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001694 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001695 if (rc != X86EMUL_CONTINUE)
1696 return rc;
1697
Avi Kivity4bff1e862011-04-20 13:37:53 +03001698 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001699
1700 eip_addr = dt.address + (irq << 2);
1701 cs_addr = dt.address + (irq << 2) + 2;
1702
Avi Kivity0f65dd72011-04-20 13:37:53 +03001703 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001704 if (rc != X86EMUL_CONTINUE)
1705 return rc;
1706
Avi Kivity0f65dd72011-04-20 13:37:53 +03001707 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001708 if (rc != X86EMUL_CONTINUE)
1709 return rc;
1710
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001711 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001712 if (rc != X86EMUL_CONTINUE)
1713 return rc;
1714
Avi Kivity9dac77f2011-06-01 15:34:25 +03001715 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001716
1717 return rc;
1718}
1719
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001720static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001721{
1722 switch(ctxt->mode) {
1723 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001724 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001725 case X86EMUL_MODE_VM86:
1726 case X86EMUL_MODE_PROT16:
1727 case X86EMUL_MODE_PROT32:
1728 case X86EMUL_MODE_PROT64:
1729 default:
1730 /* Protected mode interrupts unimplemented yet */
1731 return X86EMUL_UNHANDLEABLE;
1732 }
1733}
1734
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001735static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001736{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001737 int rc = X86EMUL_CONTINUE;
1738 unsigned long temp_eip = 0;
1739 unsigned long temp_eflags = 0;
1740 unsigned long cs = 0;
1741 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1742 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1743 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1744 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1745
1746 /* TODO: Add stack limit check */
1747
Avi Kivity9dac77f2011-06-01 15:34:25 +03001748 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001749
1750 if (rc != X86EMUL_CONTINUE)
1751 return rc;
1752
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001753 if (temp_eip & ~0xffff)
1754 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001755
Avi Kivity9dac77f2011-06-01 15:34:25 +03001756 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001757
1758 if (rc != X86EMUL_CONTINUE)
1759 return rc;
1760
Avi Kivity9dac77f2011-06-01 15:34:25 +03001761 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001762
1763 if (rc != X86EMUL_CONTINUE)
1764 return rc;
1765
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001766 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001767
1768 if (rc != X86EMUL_CONTINUE)
1769 return rc;
1770
Avi Kivity9dac77f2011-06-01 15:34:25 +03001771 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001772
1773
Avi Kivity9dac77f2011-06-01 15:34:25 +03001774 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001775 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001776 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001777 ctxt->eflags &= ~0xffff;
1778 ctxt->eflags |= temp_eflags;
1779 }
1780
1781 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1782 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1783
1784 return rc;
1785}
1786
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001787static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001788{
1789 switch(ctxt->mode) {
1790 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001791 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001792 case X86EMUL_MODE_VM86:
1793 case X86EMUL_MODE_PROT16:
1794 case X86EMUL_MODE_PROT32:
1795 case X86EMUL_MODE_PROT64:
1796 default:
1797 /* iret from protected mode unimplemented yet */
1798 return X86EMUL_UNHANDLEABLE;
1799 }
1800}
1801
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001802static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1803{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001804 int rc;
1805 unsigned short sel;
1806
Avi Kivity9dac77f2011-06-01 15:34:25 +03001807 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001808
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001809 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001810 if (rc != X86EMUL_CONTINUE)
1811 return rc;
1812
Avi Kivity9dac77f2011-06-01 15:34:25 +03001813 ctxt->_eip = 0;
1814 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001815 return X86EMUL_CONTINUE;
1816}
1817
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001818static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001819{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001820 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001821 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001822 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823 break;
1824 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001825 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001826 break;
1827 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001828 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001829 break;
1830 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001831 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001832 break;
1833 case 4: /* sal/shl */
1834 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001835 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001836 break;
1837 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001838 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001839 break;
1840 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001841 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001842 break;
1843 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001844 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001845}
1846
Avi Kivity3329ece2011-09-13 10:45:39 +03001847static int em_not(struct x86_emulate_ctxt *ctxt)
1848{
1849 ctxt->dst.val = ~ctxt->dst.val;
1850 return X86EMUL_CONTINUE;
1851}
1852
1853static int em_neg(struct x86_emulate_ctxt *ctxt)
1854{
1855 emulate_1op(ctxt, "neg");
1856 return X86EMUL_CONTINUE;
1857}
1858
1859static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1860{
1861 u8 ex = 0;
1862
1863 emulate_1op_rax_rdx(ctxt, "mul", ex);
1864 return X86EMUL_CONTINUE;
1865}
1866
1867static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1868{
1869 u8 ex = 0;
1870
1871 emulate_1op_rax_rdx(ctxt, "imul", ex);
1872 return X86EMUL_CONTINUE;
1873}
1874
1875static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001876{
Avi Kivity34d1f492010-08-26 11:59:01 +03001877 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001878
Avi Kivity3329ece2011-09-13 10:45:39 +03001879 emulate_1op_rax_rdx(ctxt, "div", de);
1880 if (de)
1881 return emulate_de(ctxt);
1882 return X86EMUL_CONTINUE;
1883}
1884
1885static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1886{
1887 u8 de = 0;
1888
1889 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001890 if (de)
1891 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001892 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001893}
1894
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001895static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001896{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001897 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001898
Avi Kivity9dac77f2011-06-01 15:34:25 +03001899 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001901 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001902 break;
1903 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001904 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001905 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001906 case 2: /* call near abs */ {
1907 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 old_eip = ctxt->_eip;
1909 ctxt->_eip = ctxt->src.val;
1910 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001911 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001912 break;
1913 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001914 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001915 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001916 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001917 case 5: /* jmp far */
1918 rc = em_jmp_far(ctxt);
1919 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001920 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001921 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001922 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001924 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001925}
1926
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001927static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001928{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001929 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001930
Avi Kivity9dac77f2011-06-01 15:34:25 +03001931 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1932 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1933 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1934 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001935 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001936 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001937 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1938 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001939
Laurent Vivier05f086f2007-09-24 11:10:55 +02001940 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001941 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001942 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001943}
1944
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001945static int em_ret(struct x86_emulate_ctxt *ctxt)
1946{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001947 ctxt->dst.type = OP_REG;
1948 ctxt->dst.addr.reg = &ctxt->_eip;
1949 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001950 return em_pop(ctxt);
1951}
1952
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001953static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001954{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001955 int rc;
1956 unsigned long cs;
1957
Avi Kivity9dac77f2011-06-01 15:34:25 +03001958 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001959 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001960 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 if (ctxt->op_bytes == 4)
1962 ctxt->_eip = (u32)ctxt->_eip;
1963 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001964 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001965 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001966 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001967 return rc;
1968}
1969
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09001970static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
1971{
1972 /* Save real source value, then compare EAX against destination. */
1973 ctxt->src.orig_val = ctxt->src.val;
1974 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
1975 emulate_2op_SrcV(ctxt, "cmp");
1976
1977 if (ctxt->eflags & EFLG_ZF) {
1978 /* Success: write back to memory. */
1979 ctxt->dst.val = ctxt->src.orig_val;
1980 } else {
1981 /* Failure: write the value we saw to EAX. */
1982 ctxt->dst.type = OP_REG;
1983 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
1984 }
1985 return X86EMUL_CONTINUE;
1986}
1987
Avi Kivityd4b43252011-09-13 10:45:50 +03001988static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001989{
Avi Kivityd4b43252011-09-13 10:45:50 +03001990 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001991 unsigned short sel;
1992 int rc;
1993
Avi Kivity9dac77f2011-06-01 15:34:25 +03001994 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001995
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001996 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001997 if (rc != X86EMUL_CONTINUE)
1998 return rc;
1999
Avi Kivity9dac77f2011-06-01 15:34:25 +03002000 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002001 return rc;
2002}
2003
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002004static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002005setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002006 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002007{
Avi Kivity1aa36612011-04-27 13:20:30 +03002008 u16 selector;
2009
Gleb Natapov79168fd2010-04-28 19:15:30 +03002010 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002011 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002012 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002013
2014 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002015 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002016 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002017 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002018 cs->type = 0x0b; /* Read, Execute, Accessed */
2019 cs->s = 1;
2020 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002021 cs->p = 1;
2022 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002023
Gleb Natapov79168fd2010-04-28 19:15:30 +03002024 set_desc_base(ss, 0); /* flat segment */
2025 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002026 ss->g = 1; /* 4kb granularity */
2027 ss->s = 1;
2028 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002029 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002030 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002031 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002032}
2033
Avi Kivity1a18a692012-02-01 12:23:21 +02002034static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2035{
2036 u32 eax, ebx, ecx, edx;
2037
2038 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002039 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2040 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002041 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2042 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2043}
2044
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002045static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2046{
2047 struct x86_emulate_ops *ops = ctxt->ops;
2048 u32 eax, ebx, ecx, edx;
2049
2050 /*
2051 * syscall should always be enabled in longmode - so only become
2052 * vendor specific (cpuid) if other modes are active...
2053 */
2054 if (ctxt->mode == X86EMUL_MODE_PROT64)
2055 return true;
2056
2057 eax = 0x00000000;
2058 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002059 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2060 /*
2061 * Intel ("GenuineIntel")
2062 * remark: Intel CPUs only support "syscall" in 64bit
2063 * longmode. Also an 64bit guest with a
2064 * 32bit compat-app running will #UD !! While this
2065 * behaviour can be fixed (by emulating) into AMD
2066 * response - CPUs of AMD can't behave like Intel.
2067 */
2068 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2069 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2070 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2071 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002072
Avi Kivity0017f932012-06-07 14:10:16 +03002073 /* AMD ("AuthenticAMD") */
2074 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2075 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2076 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2077 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002078
Avi Kivity0017f932012-06-07 14:10:16 +03002079 /* AMD ("AMDisbetter!") */
2080 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2081 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2082 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2083 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002084
2085 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2086 return false;
2087}
2088
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002089static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002090{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002091 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002092 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002093 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002094 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002095 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002096
2097 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002098 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002099 ctxt->mode == X86EMUL_MODE_VM86)
2100 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002101
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002102 if (!(em_syscall_is_enabled(ctxt)))
2103 return emulate_ud(ctxt);
2104
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002105 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002106 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002107
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002108 if (!(efer & EFER_SCE))
2109 return emulate_ud(ctxt);
2110
Avi Kivity717746e2011-04-20 13:37:53 +03002111 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002112 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002113 cs_sel = (u16)(msr_data & 0xfffc);
2114 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002115
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002116 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002117 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002118 cs.l = 1;
2119 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002120 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2121 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002122
Avi Kivity9dac77f2011-06-01 15:34:25 +03002123 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002124 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002125#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03002126 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002127
Avi Kivity717746e2011-04-20 13:37:53 +03002128 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002129 ctxt->mode == X86EMUL_MODE_PROT64 ?
2130 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002131 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002132
Avi Kivity717746e2011-04-20 13:37:53 +03002133 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002134 ctxt->eflags &= ~(msr_data | EFLG_RF);
2135#endif
2136 } else {
2137 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002138 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002139 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002140
2141 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2142 }
2143
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002144 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002145}
2146
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002147static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002148{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002149 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002150 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002151 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002152 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002153 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002154
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002155 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002156 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002157 if (ctxt->mode == X86EMUL_MODE_REAL)
2158 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002159
Avi Kivity1a18a692012-02-01 12:23:21 +02002160 /*
2161 * Not recognized on AMD in compat mode (but is recognized in legacy
2162 * mode).
2163 */
2164 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2165 && !vendor_intel(ctxt))
2166 return emulate_ud(ctxt);
2167
Andre Przywara8c604352009-06-18 12:56:01 +02002168 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2169 * Therefore, we inject an #UD.
2170 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002171 if (ctxt->mode == X86EMUL_MODE_PROT64)
2172 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002173
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002174 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002175
Avi Kivity717746e2011-04-20 13:37:53 +03002176 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002177 switch (ctxt->mode) {
2178 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002179 if ((msr_data & 0xfffc) == 0x0)
2180 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002181 break;
2182 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002183 if (msr_data == 0x0)
2184 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002185 break;
2186 }
2187
2188 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002189 cs_sel = (u16)msr_data;
2190 cs_sel &= ~SELECTOR_RPL_MASK;
2191 ss_sel = cs_sel + 8;
2192 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002193 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002194 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002195 cs.l = 1;
2196 }
2197
Avi Kivity1aa36612011-04-27 13:20:30 +03002198 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2199 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002200
Avi Kivity717746e2011-04-20 13:37:53 +03002201 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002202 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002203
Avi Kivity717746e2011-04-20 13:37:53 +03002204 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002205 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002206
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002207 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002208}
2209
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002210static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002211{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002212 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002214 u64 msr_data;
2215 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002216 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002217
Gleb Natapova0044752010-02-10 14:21:31 +02002218 /* inject #GP if in real mode or Virtual 8086 mode */
2219 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002220 ctxt->mode == X86EMUL_MODE_VM86)
2221 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002222
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002223 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002224
Avi Kivity9dac77f2011-06-01 15:34:25 +03002225 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002226 usermode = X86EMUL_MODE_PROT64;
2227 else
2228 usermode = X86EMUL_MODE_PROT32;
2229
2230 cs.dpl = 3;
2231 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002232 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002233 switch (usermode) {
2234 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002235 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002236 if ((msr_data & 0xfffc) == 0x0)
2237 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002238 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002239 break;
2240 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002241 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002242 if (msr_data == 0x0)
2243 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002244 ss_sel = cs_sel + 8;
2245 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002246 cs.l = 1;
2247 break;
2248 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002249 cs_sel |= SELECTOR_RPL_MASK;
2250 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002251
Avi Kivity1aa36612011-04-27 13:20:30 +03002252 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2253 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002254
Avi Kivity9dac77f2011-06-01 15:34:25 +03002255 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2256 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002257
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002258 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002259}
2260
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002261static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002262{
2263 int iopl;
2264 if (ctxt->mode == X86EMUL_MODE_REAL)
2265 return false;
2266 if (ctxt->mode == X86EMUL_MODE_VM86)
2267 return true;
2268 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002269 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002270}
2271
2272static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002273 u16 port, u16 len)
2274{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002275 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002277 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002278 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002279 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002280 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002281 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002282
Avi Kivity1aa36612011-04-27 13:20:30 +03002283 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002284 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002285 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002286 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002287 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002288 base = get_desc_base(&tr_seg);
2289#ifdef CONFIG_X86_64
2290 base |= ((u64)base3) << 32;
2291#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002292 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002293 if (r != X86EMUL_CONTINUE)
2294 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002295 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002296 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002297 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002298 if (r != X86EMUL_CONTINUE)
2299 return false;
2300 if ((perm >> bit_idx) & mask)
2301 return false;
2302 return true;
2303}
2304
2305static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002306 u16 port, u16 len)
2307{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002308 if (ctxt->perm_ok)
2309 return true;
2310
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002311 if (emulator_bad_iopl(ctxt))
2312 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002313 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002314
2315 ctxt->perm_ok = true;
2316
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002317 return true;
2318}
2319
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002320static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002321 struct tss_segment_16 *tss)
2322{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002323 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002324 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002325 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2326 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2327 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2328 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2329 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2330 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2331 tss->si = ctxt->regs[VCPU_REGS_RSI];
2332 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333
Avi Kivity1aa36612011-04-27 13:20:30 +03002334 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2335 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2336 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2337 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2338 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339}
2340
2341static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002342 struct tss_segment_16 *tss)
2343{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002344 int ret;
2345
Avi Kivity9dac77f2011-06-01 15:34:25 +03002346 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002347 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002348 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2349 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2350 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2351 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2352 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2353 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2354 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2355 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002356
2357 /*
2358 * SDM says that segment selectors are loaded before segment
2359 * descriptors
2360 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002361 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2362 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2363 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2364 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2365 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002366
2367 /*
2368 * Now load segment descriptors. If fault happenes at this stage
2369 * it is handled in a context of new task
2370 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002371 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002372 if (ret != X86EMUL_CONTINUE)
2373 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002374 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375 if (ret != X86EMUL_CONTINUE)
2376 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002377 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002378 if (ret != X86EMUL_CONTINUE)
2379 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002380 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002381 if (ret != X86EMUL_CONTINUE)
2382 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002383 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002384 if (ret != X86EMUL_CONTINUE)
2385 return ret;
2386
2387 return X86EMUL_CONTINUE;
2388}
2389
2390static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002391 u16 tss_selector, u16 old_tss_sel,
2392 ulong old_tss_base, struct desc_struct *new_desc)
2393{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002394 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002395 struct tss_segment_16 tss_seg;
2396 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002397 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002398
Avi Kivity0f65dd72011-04-20 13:37:53 +03002399 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002400 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002401 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002403 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002404
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002405 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406
Avi Kivity0f65dd72011-04-20 13:37:53 +03002407 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002408 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002409 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412
Avi Kivity0f65dd72011-04-20 13:37:53 +03002413 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002414 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002415 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002417 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418
2419 if (old_tss_sel != 0xffff) {
2420 tss_seg.prev_task_link = old_tss_sel;
2421
Avi Kivity0f65dd72011-04-20 13:37:53 +03002422 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002423 &tss_seg.prev_task_link,
2424 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002425 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002426 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002428 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002429 }
2430
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002431 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432}
2433
2434static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002435 struct tss_segment_32 *tss)
2436{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002437 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002438 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002439 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002440 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2441 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2442 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2443 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2444 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2445 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2446 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2447 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002448
Avi Kivity1aa36612011-04-27 13:20:30 +03002449 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2450 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2451 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2452 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2453 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2454 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2455 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002456}
2457
2458static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002459 struct tss_segment_32 *tss)
2460{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461 int ret;
2462
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002463 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002464 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002465 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002466 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002467
2468 /* General purpose registers */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002469 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2470 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2471 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2472 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2473 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2474 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2475 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2476 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002477
2478 /*
2479 * SDM says that segment selectors are loaded before segment
2480 * descriptors
2481 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002482 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2483 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2484 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2485 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2486 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2487 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2488 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489
2490 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002491 * If we're switching between Protected Mode and VM86, we need to make
2492 * sure to update the mode before loading the segment descriptors so
2493 * that the selectors are interpreted correctly.
2494 *
2495 * Need to get rflags to the vcpu struct immediately because it
2496 * influences the CPL which is checked at least when loading the segment
2497 * descriptors and when pushing an error code to the new kernel stack.
2498 *
2499 * TODO Introduce a separate ctxt->ops->set_cpl callback
2500 */
2501 if (ctxt->eflags & X86_EFLAGS_VM)
2502 ctxt->mode = X86EMUL_MODE_VM86;
2503 else
2504 ctxt->mode = X86EMUL_MODE_PROT32;
2505
2506 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2507
2508 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509 * Now load segment descriptors. If fault happenes at this stage
2510 * it is handled in a context of new task
2511 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002512 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513 if (ret != X86EMUL_CONTINUE)
2514 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002515 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516 if (ret != X86EMUL_CONTINUE)
2517 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002518 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519 if (ret != X86EMUL_CONTINUE)
2520 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002521 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522 if (ret != X86EMUL_CONTINUE)
2523 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002524 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525 if (ret != X86EMUL_CONTINUE)
2526 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002527 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 if (ret != X86EMUL_CONTINUE)
2529 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002530 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002531 if (ret != X86EMUL_CONTINUE)
2532 return ret;
2533
2534 return X86EMUL_CONTINUE;
2535}
2536
2537static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002538 u16 tss_selector, u16 old_tss_sel,
2539 ulong old_tss_base, struct desc_struct *new_desc)
2540{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002541 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002542 struct tss_segment_32 tss_seg;
2543 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002544 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545
Avi Kivity0f65dd72011-04-20 13:37:53 +03002546 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002547 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002548 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002552 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553
Avi Kivity0f65dd72011-04-20 13:37:53 +03002554 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002555 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002556 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559
Avi Kivity0f65dd72011-04-20 13:37:53 +03002560 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002561 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002562 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565
2566 if (old_tss_sel != 0xffff) {
2567 tss_seg.prev_task_link = old_tss_sel;
2568
Avi Kivity0f65dd72011-04-20 13:37:53 +03002569 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 &tss_seg.prev_task_link,
2571 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002572 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002573 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 }
2577
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002578 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579}
2580
2581static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002582 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002583 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002585 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586 struct desc_struct curr_tss_desc, next_tss_desc;
2587 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002588 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002590 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002591 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592
2593 /* FIXME: old_tss_base == ~0 ? */
2594
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002595 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 if (ret != X86EMUL_CONTINUE)
2597 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002598 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 if (ret != X86EMUL_CONTINUE)
2600 return ret;
2601
2602 /* FIXME: check that next_tss_desc is tss */
2603
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002604 /*
2605 * Check privileges. The three cases are task switch caused by...
2606 *
2607 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2608 * 2. Exception/IRQ/iret: No check is performed
2609 * 3. jmp/call to TSS: Check agains DPL of the TSS
2610 */
2611 if (reason == TASK_SWITCH_GATE) {
2612 if (idt_index != -1) {
2613 /* Software interrupts */
2614 struct desc_struct task_gate_desc;
2615 int dpl;
2616
2617 ret = read_interrupt_descriptor(ctxt, idt_index,
2618 &task_gate_desc);
2619 if (ret != X86EMUL_CONTINUE)
2620 return ret;
2621
2622 dpl = task_gate_desc.dpl;
2623 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2624 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2625 }
2626 } else if (reason != TASK_SWITCH_IRET) {
2627 int dpl = next_tss_desc.dpl;
2628 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2629 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 }
2631
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002632
Gleb Natapovceffb452010-03-18 15:20:19 +02002633 desc_limit = desc_limit_scaled(&next_tss_desc);
2634 if (!next_tss_desc.p ||
2635 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2636 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002637 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 return X86EMUL_PROPAGATE_FAULT;
2639 }
2640
2641 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2642 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002643 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644 }
2645
2646 if (reason == TASK_SWITCH_IRET)
2647 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2648
2649 /* set back link to prev task only if NT bit is set in eflags
2650 note that old_tss_sel is not used afetr this point */
2651 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2652 old_tss_sel = 0xffff;
2653
2654 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002655 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656 old_tss_base, &next_tss_desc);
2657 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002658 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002660 if (ret != X86EMUL_CONTINUE)
2661 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662
2663 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2664 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2665
2666 if (reason != TASK_SWITCH_IRET) {
2667 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002668 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669 }
2670
Avi Kivity717746e2011-04-20 13:37:53 +03002671 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002672 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002673
Jan Kiszkae269fb22010-04-14 15:51:09 +02002674 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002675 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2676 ctxt->lock_prefix = 0;
2677 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002678 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002679 }
2680
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681 return ret;
2682}
2683
2684int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002685 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002686 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002688 int rc;
2689
Avi Kivity9dac77f2011-06-01 15:34:25 +03002690 ctxt->_eip = ctxt->eip;
2691 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002692
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002693 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002694 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002695
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002696 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002697 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002698
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002699 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002700}
2701
Avi Kivity90de84f2010-11-17 15:28:21 +02002702static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002703 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002704{
Gleb Natapova682e352010-03-18 15:20:21 +02002705 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2706
Avi Kivity9dac77f2011-06-01 15:34:25 +03002707 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2708 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002709 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002710}
2711
Avi Kivity7af04fc2010-08-18 14:16:35 +03002712static int em_das(struct x86_emulate_ctxt *ctxt)
2713{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002714 u8 al, old_al;
2715 bool af, cf, old_cf;
2716
2717 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002718 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002719
2720 old_al = al;
2721 old_cf = cf;
2722 cf = false;
2723 af = ctxt->eflags & X86_EFLAGS_AF;
2724 if ((al & 0x0f) > 9 || af) {
2725 al -= 6;
2726 cf = old_cf | (al >= 250);
2727 af = true;
2728 } else {
2729 af = false;
2730 }
2731 if (old_al > 0x99 || old_cf) {
2732 al -= 0x60;
2733 cf = true;
2734 }
2735
Avi Kivity9dac77f2011-06-01 15:34:25 +03002736 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002737 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002738 ctxt->src.type = OP_IMM;
2739 ctxt->src.val = 0;
2740 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002741 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002742 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2743 if (cf)
2744 ctxt->eflags |= X86_EFLAGS_CF;
2745 if (af)
2746 ctxt->eflags |= X86_EFLAGS_AF;
2747 return X86EMUL_CONTINUE;
2748}
2749
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002750static int em_call(struct x86_emulate_ctxt *ctxt)
2751{
2752 long rel = ctxt->src.val;
2753
2754 ctxt->src.val = (unsigned long)ctxt->_eip;
2755 jmp_rel(ctxt, rel);
2756 return em_push(ctxt);
2757}
2758
Avi Kivity0ef753b2010-08-18 14:51:45 +03002759static int em_call_far(struct x86_emulate_ctxt *ctxt)
2760{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002761 u16 sel, old_cs;
2762 ulong old_eip;
2763 int rc;
2764
Avi Kivity1aa36612011-04-27 13:20:30 +03002765 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002766 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002767
Avi Kivity9dac77f2011-06-01 15:34:25 +03002768 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002769 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002770 return X86EMUL_CONTINUE;
2771
Avi Kivity9dac77f2011-06-01 15:34:25 +03002772 ctxt->_eip = 0;
2773 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002774
Avi Kivity9dac77f2011-06-01 15:34:25 +03002775 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002776 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002777 if (rc != X86EMUL_CONTINUE)
2778 return rc;
2779
Avi Kivity9dac77f2011-06-01 15:34:25 +03002780 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002781 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002782}
2783
Avi Kivity40ece7c2010-08-18 15:12:09 +03002784static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2785{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002786 int rc;
2787
Avi Kivity9dac77f2011-06-01 15:34:25 +03002788 ctxt->dst.type = OP_REG;
2789 ctxt->dst.addr.reg = &ctxt->_eip;
2790 ctxt->dst.bytes = ctxt->op_bytes;
2791 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002792 if (rc != X86EMUL_CONTINUE)
2793 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002794 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002795 return X86EMUL_CONTINUE;
2796}
2797
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002798static int em_add(struct x86_emulate_ctxt *ctxt)
2799{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002800 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002801 return X86EMUL_CONTINUE;
2802}
2803
2804static int em_or(struct x86_emulate_ctxt *ctxt)
2805{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002806 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002807 return X86EMUL_CONTINUE;
2808}
2809
2810static int em_adc(struct x86_emulate_ctxt *ctxt)
2811{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002812 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002813 return X86EMUL_CONTINUE;
2814}
2815
2816static int em_sbb(struct x86_emulate_ctxt *ctxt)
2817{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002818 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002819 return X86EMUL_CONTINUE;
2820}
2821
2822static int em_and(struct x86_emulate_ctxt *ctxt)
2823{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002824 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002825 return X86EMUL_CONTINUE;
2826}
2827
2828static int em_sub(struct x86_emulate_ctxt *ctxt)
2829{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002830 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002831 return X86EMUL_CONTINUE;
2832}
2833
2834static int em_xor(struct x86_emulate_ctxt *ctxt)
2835{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002836 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002837 return X86EMUL_CONTINUE;
2838}
2839
2840static int em_cmp(struct x86_emulate_ctxt *ctxt)
2841{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002842 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002843 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002844 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002845 return X86EMUL_CONTINUE;
2846}
2847
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002848static int em_test(struct x86_emulate_ctxt *ctxt)
2849{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002850 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002851 /* Disable writeback. */
2852 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002853 return X86EMUL_CONTINUE;
2854}
2855
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002856static int em_xchg(struct x86_emulate_ctxt *ctxt)
2857{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002858 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002859 ctxt->src.val = ctxt->dst.val;
2860 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002861
2862 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002863 ctxt->dst.val = ctxt->src.orig_val;
2864 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002865 return X86EMUL_CONTINUE;
2866}
2867
Avi Kivity5c82aa22010-08-18 18:31:43 +03002868static int em_imul(struct x86_emulate_ctxt *ctxt)
2869{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002870 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002871 return X86EMUL_CONTINUE;
2872}
2873
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002874static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2875{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002876 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002877 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002878}
2879
Avi Kivity61429142010-08-19 15:13:00 +03002880static int em_cwd(struct x86_emulate_ctxt *ctxt)
2881{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002882 ctxt->dst.type = OP_REG;
2883 ctxt->dst.bytes = ctxt->src.bytes;
2884 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2885 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002886
2887 return X86EMUL_CONTINUE;
2888}
2889
Avi Kivity48bb5d32010-08-18 18:54:34 +03002890static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2891{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002892 u64 tsc = 0;
2893
Avi Kivity717746e2011-04-20 13:37:53 +03002894 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002895 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2896 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002897 return X86EMUL_CONTINUE;
2898}
2899
Avi Kivity222d21a2011-11-10 14:57:30 +02002900static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2901{
2902 u64 pmc;
2903
2904 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2905 return emulate_gp(ctxt, 0);
2906 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2907 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2908 return X86EMUL_CONTINUE;
2909}
2910
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002911static int em_mov(struct x86_emulate_ctxt *ctxt)
2912{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002913 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002914 return X86EMUL_CONTINUE;
2915}
2916
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002917static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2918{
2919 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2920 return emulate_gp(ctxt, 0);
2921
2922 /* Disable writeback. */
2923 ctxt->dst.type = OP_NONE;
2924 return X86EMUL_CONTINUE;
2925}
2926
2927static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2928{
2929 unsigned long val;
2930
2931 if (ctxt->mode == X86EMUL_MODE_PROT64)
2932 val = ctxt->src.val & ~0ULL;
2933 else
2934 val = ctxt->src.val & ~0U;
2935
2936 /* #UD condition is already handled. */
2937 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2938 return emulate_gp(ctxt, 0);
2939
2940 /* Disable writeback. */
2941 ctxt->dst.type = OP_NONE;
2942 return X86EMUL_CONTINUE;
2943}
2944
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002945static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2946{
2947 u64 msr_data;
2948
2949 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2950 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2951 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2952 return emulate_gp(ctxt, 0);
2953
2954 return X86EMUL_CONTINUE;
2955}
2956
2957static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2958{
2959 u64 msr_data;
2960
2961 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2962 return emulate_gp(ctxt, 0);
2963
2964 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2965 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2966 return X86EMUL_CONTINUE;
2967}
2968
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002969static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2970{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002971 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002972 return emulate_ud(ctxt);
2973
Avi Kivity9dac77f2011-06-01 15:34:25 +03002974 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002975 return X86EMUL_CONTINUE;
2976}
2977
2978static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2979{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002980 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002981
Avi Kivity9dac77f2011-06-01 15:34:25 +03002982 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002983 return emulate_ud(ctxt);
2984
Avi Kivity9dac77f2011-06-01 15:34:25 +03002985 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002986 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2987
2988 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002989 ctxt->dst.type = OP_NONE;
2990 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002991}
2992
Avi Kivity38503912011-03-31 18:48:09 +02002993static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2994{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002995 int rc;
2996 ulong linear;
2997
Avi Kivity9dac77f2011-06-01 15:34:25 +03002998 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002999 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003000 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003001 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003002 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003003 return X86EMUL_CONTINUE;
3004}
3005
Avi Kivity2d04a052011-04-20 15:32:49 +03003006static int em_clts(struct x86_emulate_ctxt *ctxt)
3007{
3008 ulong cr0;
3009
3010 cr0 = ctxt->ops->get_cr(ctxt, 0);
3011 cr0 &= ~X86_CR0_TS;
3012 ctxt->ops->set_cr(ctxt, 0, cr0);
3013 return X86EMUL_CONTINUE;
3014}
3015
Avi Kivity26d05cc2011-04-21 12:07:59 +03003016static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3017{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003018 int rc;
3019
Avi Kivity9dac77f2011-06-01 15:34:25 +03003020 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003021 return X86EMUL_UNHANDLEABLE;
3022
3023 rc = ctxt->ops->fix_hypercall(ctxt);
3024 if (rc != X86EMUL_CONTINUE)
3025 return rc;
3026
3027 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003028 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003029 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003030 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003031 return X86EMUL_CONTINUE;
3032}
3033
Avi Kivity96051572012-06-10 17:21:18 +03003034static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3035 void (*get)(struct x86_emulate_ctxt *ctxt,
3036 struct desc_ptr *ptr))
3037{
3038 struct desc_ptr desc_ptr;
3039
3040 if (ctxt->mode == X86EMUL_MODE_PROT64)
3041 ctxt->op_bytes = 8;
3042 get(ctxt, &desc_ptr);
3043 if (ctxt->op_bytes == 2) {
3044 ctxt->op_bytes = 4;
3045 desc_ptr.address &= 0x00ffffff;
3046 }
3047 /* Disable writeback. */
3048 ctxt->dst.type = OP_NONE;
3049 return segmented_write(ctxt, ctxt->dst.addr.mem,
3050 &desc_ptr, 2 + ctxt->op_bytes);
3051}
3052
3053static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3054{
3055 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3056}
3057
3058static int em_sidt(struct x86_emulate_ctxt *ctxt)
3059{
3060 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3061}
3062
Avi Kivity26d05cc2011-04-21 12:07:59 +03003063static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3064{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003065 struct desc_ptr desc_ptr;
3066 int rc;
3067
Avi Kivity510425f2012-06-07 17:04:36 +03003068 if (ctxt->mode == X86EMUL_MODE_PROT64)
3069 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003070 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003071 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003073 if (rc != X86EMUL_CONTINUE)
3074 return rc;
3075 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3076 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003078 return X86EMUL_CONTINUE;
3079}
3080
Avi Kivity5ef39c72011-04-21 12:21:50 +03003081static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003082{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003083 int rc;
3084
Avi Kivity5ef39c72011-04-21 12:21:50 +03003085 rc = ctxt->ops->fix_hypercall(ctxt);
3086
Avi Kivity26d05cc2011-04-21 12:07:59 +03003087 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003089 return rc;
3090}
3091
3092static int em_lidt(struct x86_emulate_ctxt *ctxt)
3093{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003094 struct desc_ptr desc_ptr;
3095 int rc;
3096
Avi Kivity510425f2012-06-07 17:04:36 +03003097 if (ctxt->mode == X86EMUL_MODE_PROT64)
3098 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003099 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003100 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003101 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003102 if (rc != X86EMUL_CONTINUE)
3103 return rc;
3104 ctxt->ops->set_idt(ctxt, &desc_ptr);
3105 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003106 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003107 return X86EMUL_CONTINUE;
3108}
3109
3110static int em_smsw(struct x86_emulate_ctxt *ctxt)
3111{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003112 ctxt->dst.bytes = 2;
3113 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003114 return X86EMUL_CONTINUE;
3115}
3116
3117static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3118{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003119 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003120 | (ctxt->src.val & 0x0f));
3121 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003122 return X86EMUL_CONTINUE;
3123}
3124
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003125static int em_loop(struct x86_emulate_ctxt *ctxt)
3126{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003127 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
3128 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
3129 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3130 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003131
3132 return X86EMUL_CONTINUE;
3133}
3134
3135static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3136{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003137 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
3138 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003139
3140 return X86EMUL_CONTINUE;
3141}
3142
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003143static int em_in(struct x86_emulate_ctxt *ctxt)
3144{
3145 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3146 &ctxt->dst.val))
3147 return X86EMUL_IO_NEEDED;
3148
3149 return X86EMUL_CONTINUE;
3150}
3151
3152static int em_out(struct x86_emulate_ctxt *ctxt)
3153{
3154 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3155 &ctxt->src.val, 1);
3156 /* Disable writeback. */
3157 ctxt->dst.type = OP_NONE;
3158 return X86EMUL_CONTINUE;
3159}
3160
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003161static int em_cli(struct x86_emulate_ctxt *ctxt)
3162{
3163 if (emulator_bad_iopl(ctxt))
3164 return emulate_gp(ctxt, 0);
3165
3166 ctxt->eflags &= ~X86_EFLAGS_IF;
3167 return X86EMUL_CONTINUE;
3168}
3169
3170static int em_sti(struct x86_emulate_ctxt *ctxt)
3171{
3172 if (emulator_bad_iopl(ctxt))
3173 return emulate_gp(ctxt, 0);
3174
3175 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3176 ctxt->eflags |= X86_EFLAGS_IF;
3177 return X86EMUL_CONTINUE;
3178}
3179
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003180static int em_bt(struct x86_emulate_ctxt *ctxt)
3181{
3182 /* Disable writeback. */
3183 ctxt->dst.type = OP_NONE;
3184 /* only subword offset */
3185 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3186
3187 emulate_2op_SrcV_nobyte(ctxt, "bt");
3188 return X86EMUL_CONTINUE;
3189}
3190
3191static int em_bts(struct x86_emulate_ctxt *ctxt)
3192{
3193 emulate_2op_SrcV_nobyte(ctxt, "bts");
3194 return X86EMUL_CONTINUE;
3195}
3196
3197static int em_btr(struct x86_emulate_ctxt *ctxt)
3198{
3199 emulate_2op_SrcV_nobyte(ctxt, "btr");
3200 return X86EMUL_CONTINUE;
3201}
3202
3203static int em_btc(struct x86_emulate_ctxt *ctxt)
3204{
3205 emulate_2op_SrcV_nobyte(ctxt, "btc");
3206 return X86EMUL_CONTINUE;
3207}
3208
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003209static int em_bsf(struct x86_emulate_ctxt *ctxt)
3210{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003211 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003212 return X86EMUL_CONTINUE;
3213}
3214
3215static int em_bsr(struct x86_emulate_ctxt *ctxt)
3216{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003217 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003218 return X86EMUL_CONTINUE;
3219}
3220
Avi Kivity6d6eede2012-06-07 14:11:36 +03003221static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3222{
3223 u32 eax, ebx, ecx, edx;
3224
3225 eax = ctxt->regs[VCPU_REGS_RAX];
3226 ecx = ctxt->regs[VCPU_REGS_RCX];
3227 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3228 ctxt->regs[VCPU_REGS_RAX] = eax;
3229 ctxt->regs[VCPU_REGS_RBX] = ebx;
3230 ctxt->regs[VCPU_REGS_RCX] = ecx;
3231 ctxt->regs[VCPU_REGS_RDX] = edx;
3232 return X86EMUL_CONTINUE;
3233}
3234
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003235static int em_lahf(struct x86_emulate_ctxt *ctxt)
3236{
3237 ctxt->regs[VCPU_REGS_RAX] &= ~0xff00UL;
3238 ctxt->regs[VCPU_REGS_RAX] |= (ctxt->eflags & 0xff) << 8;
3239 return X86EMUL_CONTINUE;
3240}
3241
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003242static bool valid_cr(int nr)
3243{
3244 switch (nr) {
3245 case 0:
3246 case 2 ... 4:
3247 case 8:
3248 return true;
3249 default:
3250 return false;
3251 }
3252}
3253
3254static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3255{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003256 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003257 return emulate_ud(ctxt);
3258
3259 return X86EMUL_CONTINUE;
3260}
3261
3262static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3263{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003264 u64 new_val = ctxt->src.val64;
3265 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003266 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003267
3268 static u64 cr_reserved_bits[] = {
3269 0xffffffff00000000ULL,
3270 0, 0, 0, /* CR3 checked later */
3271 CR4_RESERVED_BITS,
3272 0, 0, 0,
3273 CR8_RESERVED_BITS,
3274 };
3275
3276 if (!valid_cr(cr))
3277 return emulate_ud(ctxt);
3278
3279 if (new_val & cr_reserved_bits[cr])
3280 return emulate_gp(ctxt, 0);
3281
3282 switch (cr) {
3283 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003284 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003285 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3286 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3287 return emulate_gp(ctxt, 0);
3288
Avi Kivity717746e2011-04-20 13:37:53 +03003289 cr4 = ctxt->ops->get_cr(ctxt, 4);
3290 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003291
3292 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3293 !(cr4 & X86_CR4_PAE))
3294 return emulate_gp(ctxt, 0);
3295
3296 break;
3297 }
3298 case 3: {
3299 u64 rsvd = 0;
3300
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003301 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3302 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003303 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003304 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003305 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003306 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003307 rsvd = CR3_NONPAE_RESERVED_BITS;
3308
3309 if (new_val & rsvd)
3310 return emulate_gp(ctxt, 0);
3311
3312 break;
3313 }
3314 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003315 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003316
3317 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3318 return emulate_gp(ctxt, 0);
3319
3320 break;
3321 }
3322 }
3323
3324 return X86EMUL_CONTINUE;
3325}
3326
Joerg Roedel3b88e412011-04-04 12:39:29 +02003327static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3328{
3329 unsigned long dr7;
3330
Avi Kivity717746e2011-04-20 13:37:53 +03003331 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003332
3333 /* Check if DR7.Global_Enable is set */
3334 return dr7 & (1 << 13);
3335}
3336
3337static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3338{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003339 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003340 u64 cr4;
3341
3342 if (dr > 7)
3343 return emulate_ud(ctxt);
3344
Avi Kivity717746e2011-04-20 13:37:53 +03003345 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003346 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3347 return emulate_ud(ctxt);
3348
3349 if (check_dr7_gd(ctxt))
3350 return emulate_db(ctxt);
3351
3352 return X86EMUL_CONTINUE;
3353}
3354
3355static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3356{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003357 u64 new_val = ctxt->src.val64;
3358 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003359
3360 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3361 return emulate_gp(ctxt, 0);
3362
3363 return check_dr_read(ctxt);
3364}
3365
Joerg Roedel01de8b02011-04-04 12:39:31 +02003366static int check_svme(struct x86_emulate_ctxt *ctxt)
3367{
3368 u64 efer;
3369
Avi Kivity717746e2011-04-20 13:37:53 +03003370 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003371
3372 if (!(efer & EFER_SVME))
3373 return emulate_ud(ctxt);
3374
3375 return X86EMUL_CONTINUE;
3376}
3377
3378static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3379{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003380 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003381
3382 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003383 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003384 return emulate_gp(ctxt, 0);
3385
3386 return check_svme(ctxt);
3387}
3388
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003389static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3390{
Avi Kivity717746e2011-04-20 13:37:53 +03003391 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003392
Avi Kivity717746e2011-04-20 13:37:53 +03003393 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003394 return emulate_ud(ctxt);
3395
3396 return X86EMUL_CONTINUE;
3397}
3398
Joerg Roedel80612522011-04-04 12:39:33 +02003399static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3400{
Avi Kivity717746e2011-04-20 13:37:53 +03003401 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003402 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003403
Avi Kivity717746e2011-04-20 13:37:53 +03003404 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003405 (rcx > 3))
3406 return emulate_gp(ctxt, 0);
3407
3408 return X86EMUL_CONTINUE;
3409}
3410
Joerg Roedelf6511932011-04-04 12:39:35 +02003411static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3412{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003413 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3414 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003415 return emulate_gp(ctxt, 0);
3416
3417 return X86EMUL_CONTINUE;
3418}
3419
3420static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3421{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003422 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3423 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003424 return emulate_gp(ctxt, 0);
3425
3426 return X86EMUL_CONTINUE;
3427}
3428
Avi Kivity73fba5f2010-07-29 15:11:53 +03003429#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003430#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003431#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3432 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003433#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003434#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003435#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3436#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003437#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003438#define II(_f, _e, _i) \
3439 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003440#define IIP(_f, _e, _i, _p) \
3441 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3442 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003443#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003444
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003445#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003446#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003447#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003448#define I2bvIP(_f, _e, _i, _p) \
3449 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003450
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003451#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3452 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3453 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003454
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003455static struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003456 DI(SrcNone | Priv, monitor),
3457 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003458 N, N, N, N, N, N,
3459};
3460
Joerg Roedel01de8b02011-04-04 12:39:31 +02003461static struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003462 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3463 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3464 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3465 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3466 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3467 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3468 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3469 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003470};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003471
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003472static struct opcode group7_rm7[] = {
3473 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003474 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003475 N, N, N, N, N, N,
3476};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003477
Avi Kivity73fba5f2010-07-29 15:11:53 +03003478static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003479 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003480 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003481 I(Lock, em_adc),
3482 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003483 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003484 I(Lock, em_sub),
3485 I(Lock, em_xor),
3486 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003487};
3488
3489static struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003490 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003491};
3492
3493static struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003494 I(DstMem | SrcImm, em_test),
3495 I(DstMem | SrcImm, em_test),
3496 I(DstMem | SrcNone | Lock, em_not),
3497 I(DstMem | SrcNone | Lock, em_neg),
3498 I(SrcMem, em_mul_ex),
3499 I(SrcMem, em_imul_ex),
3500 I(SrcMem, em_div_ex),
3501 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003502};
3503
3504static struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003505 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3506 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003507 N, N, N, N, N, N,
3508};
3509
3510static struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003511 I(DstMem | SrcNone | Lock, em_grp45),
3512 I(DstMem | SrcNone | Lock, em_grp45),
3513 I(SrcMem | Stack, em_grp45),
3514 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3515 I(SrcMem | Stack, em_grp45),
3516 I(SrcMemFAddr | ImplicitOps, em_grp45),
3517 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003518};
3519
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003520static struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003521 DI(Prot, sldt),
3522 DI(Prot, str),
3523 DI(Prot | Priv, lldt),
3524 DI(Prot | Priv, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003525 N, N, N, N,
3526};
3527
Avi Kivity73fba5f2010-07-29 15:11:53 +03003528static struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003529 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3530 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003531 II(SrcMem | Priv, em_lgdt, lgdt),
3532 II(SrcMem | Priv, em_lidt, lidt),
3533 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3534 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3535 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003536}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003537 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003538 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003539 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003540 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3541 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3542 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003543} };
3544
3545static struct opcode group8[] = {
3546 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003547 I(DstMem | SrcImmByte, em_bt),
3548 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3549 I(DstMem | SrcImmByte | Lock, em_btr),
3550 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003551};
3552
3553static struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003554 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003555}, {
3556 N, N, N, N, N, N, N, N,
3557} };
3558
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003559static struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003560 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003561 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003562};
3563
Avi Kivityaa97bb42010-01-20 18:09:23 +02003564static struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003565 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003566};
3567
Avi Kivity3e114eb2012-04-09 18:40:01 +03003568static struct gprefix pfx_vmovntpx = {
3569 I(0, em_mov), N, N, N,
3570};
3571
Avi Kivity73fba5f2010-07-29 15:11:53 +03003572static struct opcode opcode_table[256] = {
3573 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003574 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003575 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3576 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003577 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003578 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003579 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3580 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003581 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003582 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003583 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3584 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003585 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003586 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003587 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3588 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003589 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003590 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003591 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003592 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003593 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003594 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003595 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003596 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003597 /* 0x40 - 0x4F */
3598 X16(D(DstReg)),
3599 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003600 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003601 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003602 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003603 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003604 I(ImplicitOps | Stack | No64, em_pusha),
3605 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003606 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3607 N, N, N, N,
3608 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003609 I(SrcImm | Mov | Stack, em_push),
3610 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003611 I(SrcImmByte | Mov | Stack, em_push),
3612 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003613 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3614 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003615 /* 0x70 - 0x7F */
3616 X16(D(SrcImmByte)),
3617 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003618 G(ByteOp | DstMem | SrcImm, group1),
3619 G(DstMem | SrcImm, group1),
3620 G(ByteOp | DstMem | SrcImm | No64, group1),
3621 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003622 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003623 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003624 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003625 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003626 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003627 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003628 D(ModRM | SrcMem | NoAccess | DstReg),
3629 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3630 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003631 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003632 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003633 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003634 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003635 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003636 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003637 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003638 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003639 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003640 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003641 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003642 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003643 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003644 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003645 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3646 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003647 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003648 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003649 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003650 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003651 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003652 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003653 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003654 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003655 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003656 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3657 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003658 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003659 /* 0xC8 - 0xCF */
Avi Kivityf47cfa32012-06-07 17:49:24 +03003660 N, I(Stack, em_leave), N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003661 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003662 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003663 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003664 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003665 N, N, N, N,
3666 /* 0xD8 - 0xDF */
3667 N, N, N, N, N, N, N, N,
3668 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003669 X3(I(SrcImmByte, em_loop)),
3670 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003671 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3672 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003673 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003674 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003675 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003676 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3677 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003678 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003679 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003680 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3681 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003682 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003683 D(ImplicitOps), D(ImplicitOps),
3684 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003685 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3686};
3687
3688static struct opcode twobyte_table[256] = {
3689 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003690 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003691 N, I(ImplicitOps | VendorSpecific, em_syscall),
3692 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003693 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003694 N, D(ImplicitOps | ModRM), N, N,
3695 /* 0x10 - 0x1F */
3696 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3697 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003698 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003699 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003700 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3701 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003702 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003703 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3704 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003705 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003706 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003707 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003708 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003709 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003710 I(ImplicitOps | VendorSpecific, em_sysenter),
3711 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003712 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003713 N, N, N, N, N, N, N, N,
3714 /* 0x40 - 0x4F */
3715 X16(D(DstReg | SrcMem | ModRM | Mov)),
3716 /* 0x50 - 0x5F */
3717 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3718 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003719 N, N, N, N,
3720 N, N, N, N,
3721 N, N, N, N,
3722 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003723 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003724 N, N, N, N,
3725 N, N, N, N,
3726 N, N, N, N,
3727 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003728 /* 0x80 - 0x8F */
3729 X16(D(SrcImm)),
3730 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003731 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003732 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003733 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003734 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003735 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3736 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3737 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003738 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003739 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003740 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003741 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3742 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003743 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003744 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003745 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003746 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003747 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003748 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3749 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003750 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751 /* 0xB8 - 0xBF */
3752 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003753 G(BitOp, group8),
3754 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003755 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003756 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003757 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003758 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003759 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003760 N, N, N, GD(0, &group9),
3761 N, N, N, N, N, N, N, N,
3762 /* 0xD0 - 0xDF */
3763 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3764 /* 0xE0 - 0xEF */
3765 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3766 /* 0xF0 - 0xFF */
3767 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3768};
3769
3770#undef D
3771#undef N
3772#undef G
3773#undef GD
3774#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003775#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003776#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003777
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003778#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003779#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003780#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003781#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003782#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003783
Avi Kivity9dac77f2011-06-01 15:34:25 +03003784static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003785{
3786 unsigned size;
3787
Avi Kivity9dac77f2011-06-01 15:34:25 +03003788 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003789 if (size == 8)
3790 size = 4;
3791 return size;
3792}
3793
3794static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3795 unsigned size, bool sign_extension)
3796{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003797 int rc = X86EMUL_CONTINUE;
3798
3799 op->type = OP_IMM;
3800 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003801 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003802 /* NB. Immediates are sign-extended as necessary. */
3803 switch (op->bytes) {
3804 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003805 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003806 break;
3807 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003808 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003809 break;
3810 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003811 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003812 break;
3813 }
3814 if (!sign_extension) {
3815 switch (op->bytes) {
3816 case 1:
3817 op->val &= 0xff;
3818 break;
3819 case 2:
3820 op->val &= 0xffff;
3821 break;
3822 case 4:
3823 op->val &= 0xffffffff;
3824 break;
3825 }
3826 }
3827done:
3828 return rc;
3829}
3830
Avi Kivitya9945542011-09-13 10:45:41 +03003831static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3832 unsigned d)
3833{
3834 int rc = X86EMUL_CONTINUE;
3835
3836 switch (d) {
3837 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003838 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03003839 break;
3840 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003841 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003842 break;
3843 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003844 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003845 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003846 *op = ctxt->memop;
3847 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003848 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003849 fetch_bit_operand(ctxt);
3850 op->orig_val = op->val;
3851 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003852 case OpMem64:
3853 ctxt->memop.bytes = 8;
3854 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003855 case OpAcc:
3856 op->type = OP_REG;
3857 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3858 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3859 fetch_register_operand(op);
3860 op->orig_val = op->val;
3861 break;
3862 case OpDI:
3863 op->type = OP_MEM;
3864 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3865 op->addr.mem.ea =
3866 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3867 op->addr.mem.seg = VCPU_SREG_ES;
3868 op->val = 0;
3869 break;
3870 case OpDX:
3871 op->type = OP_REG;
3872 op->bytes = 2;
3873 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3874 fetch_register_operand(op);
3875 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003876 case OpCL:
3877 op->bytes = 1;
3878 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3879 break;
3880 case OpImmByte:
3881 rc = decode_imm(ctxt, op, 1, true);
3882 break;
3883 case OpOne:
3884 op->bytes = 1;
3885 op->val = 1;
3886 break;
3887 case OpImm:
3888 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3889 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003890 case OpMem8:
3891 ctxt->memop.bytes = 1;
3892 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003893 case OpMem16:
3894 ctxt->memop.bytes = 2;
3895 goto mem_common;
3896 case OpMem32:
3897 ctxt->memop.bytes = 4;
3898 goto mem_common;
3899 case OpImmU16:
3900 rc = decode_imm(ctxt, op, 2, false);
3901 break;
3902 case OpImmU:
3903 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3904 break;
3905 case OpSI:
3906 op->type = OP_MEM;
3907 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3908 op->addr.mem.ea =
3909 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3910 op->addr.mem.seg = seg_override(ctxt);
3911 op->val = 0;
3912 break;
3913 case OpImmFAddr:
3914 op->type = OP_IMM;
3915 op->addr.mem.ea = ctxt->_eip;
3916 op->bytes = ctxt->op_bytes + 2;
3917 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3918 break;
3919 case OpMemFAddr:
3920 ctxt->memop.bytes = ctxt->op_bytes + 2;
3921 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003922 case OpES:
3923 op->val = VCPU_SREG_ES;
3924 break;
3925 case OpCS:
3926 op->val = VCPU_SREG_CS;
3927 break;
3928 case OpSS:
3929 op->val = VCPU_SREG_SS;
3930 break;
3931 case OpDS:
3932 op->val = VCPU_SREG_DS;
3933 break;
3934 case OpFS:
3935 op->val = VCPU_SREG_FS;
3936 break;
3937 case OpGS:
3938 op->val = VCPU_SREG_GS;
3939 break;
Avi Kivitya9945542011-09-13 10:45:41 +03003940 case OpImplicit:
3941 /* Special instructions do their own operand decoding. */
3942 default:
3943 op->type = OP_NONE; /* Disable writeback. */
3944 break;
3945 }
3946
3947done:
3948 return rc;
3949}
3950
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003951int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003952{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003953 int rc = X86EMUL_CONTINUE;
3954 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003955 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003956 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003957 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003958
Avi Kivityf09ed832011-09-13 10:45:40 +03003959 ctxt->memop.type = OP_NONE;
3960 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003961 ctxt->_eip = ctxt->eip;
3962 ctxt->fetch.start = ctxt->_eip;
3963 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003964 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003965 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003966
3967 switch (mode) {
3968 case X86EMUL_MODE_REAL:
3969 case X86EMUL_MODE_VM86:
3970 case X86EMUL_MODE_PROT16:
3971 def_op_bytes = def_ad_bytes = 2;
3972 break;
3973 case X86EMUL_MODE_PROT32:
3974 def_op_bytes = def_ad_bytes = 4;
3975 break;
3976#ifdef CONFIG_X86_64
3977 case X86EMUL_MODE_PROT64:
3978 def_op_bytes = 4;
3979 def_ad_bytes = 8;
3980 break;
3981#endif
3982 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003983 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003984 }
3985
Avi Kivity9dac77f2011-06-01 15:34:25 +03003986 ctxt->op_bytes = def_op_bytes;
3987 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003988
3989 /* Legacy prefixes. */
3990 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003991 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003992 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003993 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003994 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003995 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003996 break;
3997 case 0x67: /* address-size override */
3998 if (mode == X86EMUL_MODE_PROT64)
3999 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004000 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004001 else
4002 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004003 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004004 break;
4005 case 0x26: /* ES override */
4006 case 0x2e: /* CS override */
4007 case 0x36: /* SS override */
4008 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004009 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004010 break;
4011 case 0x64: /* FS override */
4012 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004013 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004014 break;
4015 case 0x40 ... 0x4f: /* REX */
4016 if (mode != X86EMUL_MODE_PROT64)
4017 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004018 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004019 continue;
4020 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004022 break;
4023 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004024 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004025 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004026 break;
4027 default:
4028 goto done_prefixes;
4029 }
4030
4031 /* Any legacy prefix after a REX prefix nullifies its effect. */
4032
Avi Kivity9dac77f2011-06-01 15:34:25 +03004033 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004034 }
4035
4036done_prefixes:
4037
4038 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004039 if (ctxt->rex_prefix & 8)
4040 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004041
4042 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004043 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004044 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004045 if (ctxt->b == 0x0f) {
4046 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004047 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004048 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004049 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004050 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004051
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004052 if (ctxt->d & ModRM)
4053 ctxt->modrm = insn_fetch(u8, ctxt);
4054
Avi Kivity9dac77f2011-06-01 15:34:25 +03004055 while (ctxt->d & GroupMask) {
4056 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004057 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004058 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004059 opcode = opcode.u.group[goffset];
4060 break;
4061 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004062 goffset = (ctxt->modrm >> 3) & 7;
4063 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004064 opcode = opcode.u.gdual->mod3[goffset];
4065 else
4066 opcode = opcode.u.gdual->mod012[goffset];
4067 break;
4068 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004069 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004070 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004071 break;
4072 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004073 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004074 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004075 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004076 switch (simd_prefix) {
4077 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4078 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4079 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4080 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4081 }
4082 break;
4083 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004084 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004085 }
Avi Kivity46561642011-04-24 14:09:59 +03004086
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004087 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004088 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004089 }
4090
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091 ctxt->execute = opcode.u.execute;
4092 ctxt->check_perm = opcode.check_perm;
4093 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004094
4095 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004096 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004097 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004098
Avi Kivity9dac77f2011-06-01 15:34:25 +03004099 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004100 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004101
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4103 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004104
Avi Kivity9dac77f2011-06-01 15:34:25 +03004105 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004106 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004107 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004108 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004109 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004110 }
4111
Avi Kivity9dac77f2011-06-01 15:34:25 +03004112 if (ctxt->d & Sse)
4113 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004114 else if (ctxt->d & Mmx)
4115 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004116
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004117 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004118 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004119 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004120 if (!ctxt->has_seg_override)
4121 set_seg_override(ctxt, ctxt->modrm_seg);
4122 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004123 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004124 if (rc != X86EMUL_CONTINUE)
4125 goto done;
4126
Avi Kivity9dac77f2011-06-01 15:34:25 +03004127 if (!ctxt->has_seg_override)
4128 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004129
Avi Kivityf09ed832011-09-13 10:45:40 +03004130 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004131
Avi Kivityf09ed832011-09-13 10:45:40 +03004132 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4133 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004134
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004135 /*
4136 * Decode and fetch the source operand: register, memory
4137 * or immediate.
4138 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004139 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004140 if (rc != X86EMUL_CONTINUE)
4141 goto done;
4142
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004143 /*
4144 * Decode and fetch the second source operand: register, memory
4145 * or immediate.
4146 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004147 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004148 if (rc != X86EMUL_CONTINUE)
4149 goto done;
4150
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004151 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004152 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004153
4154done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004155 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4156 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004157
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004158 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004159}
4160
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004161bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4162{
4163 return ctxt->d & PageTable;
4164}
4165
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004166static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4167{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004168 /* The second termination condition only applies for REPE
4169 * and REPNE. Test if the repeat string operation prefix is
4170 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4171 * corresponding termination condition according to:
4172 * - if REPE/REPZ and ZF = 0 then done
4173 * - if REPNE/REPNZ and ZF = 1 then done
4174 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4176 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4177 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004178 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004179 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004180 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4181 return true;
4182
4183 return false;
4184}
4185
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004186static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4187{
4188 bool fault = false;
4189
4190 ctxt->ops->get_fpu(ctxt);
4191 asm volatile("1: fwait \n\t"
4192 "2: \n\t"
4193 ".pushsection .fixup,\"ax\" \n\t"
4194 "3: \n\t"
4195 "movb $1, %[fault] \n\t"
4196 "jmp 2b \n\t"
4197 ".popsection \n\t"
4198 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004199 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004200 ctxt->ops->put_fpu(ctxt);
4201
4202 if (unlikely(fault))
4203 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4204
4205 return X86EMUL_CONTINUE;
4206}
4207
4208static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4209 struct operand *op)
4210{
4211 if (op->type == OP_MM)
4212 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4213}
4214
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004215int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004216{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004217 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004218 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004219 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004220
Avi Kivity9dac77f2011-06-01 15:34:25 +03004221 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004222
Avi Kivity9dac77f2011-06-01 15:34:25 +03004223 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004224 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004225 goto done;
4226 }
4227
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004228 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004230 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004231 goto done;
4232 }
4233
Avi Kivity9dac77f2011-06-01 15:34:25 +03004234 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004235 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004236 goto done;
4237 }
4238
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004239 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4240 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004241 rc = emulate_ud(ctxt);
4242 goto done;
4243 }
4244
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004245 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004246 rc = emulate_nm(ctxt);
4247 goto done;
4248 }
4249
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004250 if (ctxt->d & Mmx) {
4251 rc = flush_pending_x87_faults(ctxt);
4252 if (rc != X86EMUL_CONTINUE)
4253 goto done;
4254 /*
4255 * Now that we know the fpu is exception safe, we can fetch
4256 * operands from it.
4257 */
4258 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4259 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4260 if (!(ctxt->d & Mov))
4261 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4262 }
4263
Avi Kivity9dac77f2011-06-01 15:34:25 +03004264 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4265 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004266 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004267 if (rc != X86EMUL_CONTINUE)
4268 goto done;
4269 }
4270
Gleb Natapove92805a2010-02-10 14:21:35 +02004271 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004272 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004273 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004274 goto done;
4275 }
4276
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004277 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004278 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004279 rc = emulate_ud(ctxt);
4280 goto done;
4281 }
4282
Joerg Roedeld09beab2011-04-04 12:39:25 +02004283 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004284 if (ctxt->check_perm) {
4285 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004286 if (rc != X86EMUL_CONTINUE)
4287 goto done;
4288 }
4289
Avi Kivity9dac77f2011-06-01 15:34:25 +03004290 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4291 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004292 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004293 if (rc != X86EMUL_CONTINUE)
4294 goto done;
4295 }
4296
Avi Kivity9dac77f2011-06-01 15:34:25 +03004297 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004298 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004299 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4300 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004301 goto done;
4302 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004303 }
4304
Avi Kivity9dac77f2011-06-01 15:34:25 +03004305 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4306 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4307 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004308 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004309 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004310 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004311 }
4312
Avi Kivity9dac77f2011-06-01 15:34:25 +03004313 if (ctxt->src2.type == OP_MEM) {
4314 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4315 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004316 if (rc != X86EMUL_CONTINUE)
4317 goto done;
4318 }
4319
Avi Kivity9dac77f2011-06-01 15:34:25 +03004320 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004321 goto special_insn;
4322
4323
Avi Kivity9dac77f2011-06-01 15:34:25 +03004324 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004325 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004326 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4327 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004328 if (rc != X86EMUL_CONTINUE)
4329 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004330 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004331 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004332
Avi Kivity018a98d2007-11-27 19:30:56 +02004333special_insn:
4334
Avi Kivity9dac77f2011-06-01 15:34:25 +03004335 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4336 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004337 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004338 if (rc != X86EMUL_CONTINUE)
4339 goto done;
4340 }
4341
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 if (ctxt->execute) {
4343 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004344 if (rc != X86EMUL_CONTINUE)
4345 goto done;
4346 goto writeback;
4347 }
4348
Avi Kivity9dac77f2011-06-01 15:34:25 +03004349 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004350 goto twobyte_insn;
4351
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004353 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004354 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004355 break;
4356 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004357 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004358 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004360 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004362 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004363 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004364 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 if (test_cc(ctxt->b, ctxt->eflags))
4366 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004367 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004368 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004369 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004370 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004371 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004372 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004373 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004374 rc = em_xchg(ctxt);
4375 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004376 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004377 switch (ctxt->op_bytes) {
4378 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4379 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4380 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004381 }
4382 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004383 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004384 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004385 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004386 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004387 rc = emulate_int(ctxt, 3);
4388 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004389 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004390 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004391 break;
4392 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004393 if (ctxt->eflags & EFLG_OF)
4394 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004395 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004396 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004397 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004398 break;
4399 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004400 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004401 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004402 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004403 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004404 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 jmp_rel(ctxt, ctxt->src.val);
4406 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004407 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004408 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004409 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004410 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004411 case 0xf5: /* cmc */
4412 /* complement carry flag from eflags reg */
4413 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004414 break;
4415 case 0xf8: /* clc */
4416 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004417 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004418 case 0xf9: /* stc */
4419 ctxt->eflags |= EFLG_CF;
4420 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004421 case 0xfc: /* cld */
4422 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004423 break;
4424 case 0xfd: /* std */
4425 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004426 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004427 default:
4428 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004429 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004430
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004431 if (rc != X86EMUL_CONTINUE)
4432 goto done;
4433
Avi Kivity018a98d2007-11-27 19:30:56 +02004434writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004435 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004436 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004437 goto done;
4438
Gleb Natapov5cd21912010-03-18 15:20:26 +02004439 /*
4440 * restore dst type in case the decoding will be reused
4441 * (happens for string instruction )
4442 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004443 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004444
Avi Kivity9dac77f2011-06-01 15:34:25 +03004445 if ((ctxt->d & SrcMask) == SrcSI)
4446 string_addr_inc(ctxt, seg_override(ctxt),
4447 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004448
Avi Kivity9dac77f2011-06-01 15:34:25 +03004449 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004450 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004451 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004452
Avi Kivity9dac77f2011-06-01 15:34:25 +03004453 if (ctxt->rep_prefix && (ctxt->d & String)) {
4454 struct read_cache *r = &ctxt->io_read;
4455 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004456
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004457 if (!string_insn_completed(ctxt)) {
4458 /*
4459 * Re-enter guest when pio read ahead buffer is empty
4460 * or, if it is not used, after each 1024 iteration.
4461 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004462 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004463 (r->end == 0 || r->end != r->pos)) {
4464 /*
4465 * Reset read cache. Usually happens before
4466 * decode, but since instruction is restarted
4467 * we have to do it here.
4468 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004469 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004470 return EMULATION_RESTART;
4471 }
4472 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004473 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004474 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004475
Avi Kivity9dac77f2011-06-01 15:34:25 +03004476 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004477
4478done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004479 if (rc == X86EMUL_PROPAGATE_FAULT)
4480 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004481 if (rc == X86EMUL_INTERCEPTED)
4482 return EMULATION_INTERCEPTED;
4483
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004484 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004485
4486twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004487 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004488 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004489 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004490 break;
4491 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004492 case 0x0d: /* GrpP (prefetch) */
4493 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004494 break;
4495 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004496 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004497 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004498 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004499 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004501 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004502 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4503 if (!test_cc(ctxt->b, ctxt->eflags))
4504 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004505 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004506 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004507 if (test_cc(ctxt->b, ctxt->eflags))
4508 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004509 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004510 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004511 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004512 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004513 case 0xa4: /* shld imm8, r, r/m */
4514 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004515 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004516 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004517 case 0xac: /* shrd imm8, r, r/m */
4518 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004519 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004520 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004521 case 0xae: /* clflush */
4522 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004524 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004525 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004526 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004528 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004529 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004530 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004531 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004532 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004533 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004534 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004535 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004536 ctxt->src.val = ctxt->dst.orig_val;
4537 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004538 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004539 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004540 ctxt->dst.bytes = ctxt->op_bytes;
4541 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4542 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004543 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004544 default:
4545 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004546 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004547
4548 if (rc != X86EMUL_CONTINUE)
4549 goto done;
4550
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551 goto writeback;
4552
4553cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004554 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004555}