blob: a3b57a27be880649ac7b0ae4144bc62faf105cbc [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 Kivity612e89f2012-06-12 20:03:23 +0300457static int stack_size(struct x86_emulate_ctxt *ctxt)
458{
459 return (__fls(stack_mask(ctxt)) + 1) >> 3;
460}
461
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800463static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300464address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800465{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300466 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800467 return reg;
468 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300469 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800470}
471
472static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300473register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800474{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300475 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800476}
477
Avi Kivity5ad105e2012-08-19 14:34:31 +0300478static void masked_increment(ulong *reg, ulong mask, int inc)
479{
480 assign_masked(reg, *reg + inc, mask);
481}
482
Harvey Harrison7a9572752008-02-19 07:40:41 -0800483static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300484register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800485{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300486 ulong mask;
487
Avi Kivity9dac77f2011-06-01 15:34:25 +0300488 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300489 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300491 mask = ad_mask(ctxt);
492 masked_increment(reg, mask, inc);
493}
494
495static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
496{
497 masked_increment(&ctxt->regs[VCPU_REGS_RSP], stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800498}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499
Avi Kivity9dac77f2011-06-01 15:34:25 +0300500static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800501{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300502 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800503}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300504
Avi Kivity56697682011-04-03 14:08:51 +0300505static u32 desc_limit_scaled(struct desc_struct *desc)
506{
507 u32 limit = get_desc_limit(desc);
508
509 return desc->g ? (limit << 12) | 0xfff : limit;
510}
511
Avi Kivity9dac77f2011-06-01 15:34:25 +0300512static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300513{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300514 ctxt->has_seg_override = true;
515 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300516}
517
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900518static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300519{
520 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
521 return 0;
522
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900523 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300524}
525
Avi Kivity9dac77f2011-06-01 15:34:25 +0300526static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300527{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300528 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300529 return 0;
530
Avi Kivity9dac77f2011-06-01 15:34:25 +0300531 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300532}
533
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200534static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
535 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300536{
Avi Kivityda9cb572010-11-22 17:53:21 +0200537 ctxt->exception.vector = vec;
538 ctxt->exception.error_code = error;
539 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300541}
542
Joerg Roedel3b88e412011-04-04 12:39:29 +0200543static int emulate_db(struct x86_emulate_ctxt *ctxt)
544{
545 return emulate_exception(ctxt, DB_VECTOR, 0, false);
546}
547
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200548static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300549{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200550 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300551}
552
Avi Kivity618ff152011-04-03 12:32:09 +0300553static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
554{
555 return emulate_exception(ctxt, SS_VECTOR, err, true);
556}
557
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200558static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300559{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200560 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300561}
562
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200563static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300564{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200565 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300566}
567
Avi Kivity34d1f492010-08-26 11:59:01 +0300568static int emulate_de(struct x86_emulate_ctxt *ctxt)
569{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300571}
572
Avi Kivity12537912011-03-29 11:41:27 +0200573static int emulate_nm(struct x86_emulate_ctxt *ctxt)
574{
575 return emulate_exception(ctxt, NM_VECTOR, 0, false);
576}
577
Avi Kivity1aa36612011-04-27 13:20:30 +0300578static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
579{
580 u16 selector;
581 struct desc_struct desc;
582
583 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
584 return selector;
585}
586
587static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
588 unsigned seg)
589{
590 u16 dummy;
591 u32 base3;
592 struct desc_struct desc;
593
594 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
595 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
596}
597
Avi Kivity1c11b372012-04-09 18:39:59 +0300598/*
599 * x86 defines three classes of vector instructions: explicitly
600 * aligned, explicitly unaligned, and the rest, which change behaviour
601 * depending on whether they're AVX encoded or not.
602 *
603 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
604 * subject to the same check.
605 */
606static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
607{
608 if (likely(size < 16))
609 return false;
610
611 if (ctxt->d & Aligned)
612 return true;
613 else if (ctxt->d & Unaligned)
614 return false;
615 else if (ctxt->d & Avx)
616 return false;
617 else
618 return true;
619}
620
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400621static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300622 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400623 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300624 ulong *linear)
625{
Avi Kivity618ff152011-04-03 12:32:09 +0300626 struct desc_struct desc;
627 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300628 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300629 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300630 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300631 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300632
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900633 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300634 switch (ctxt->mode) {
635 case X86EMUL_MODE_REAL:
636 break;
637 case X86EMUL_MODE_PROT64:
638 if (((signed long)la << 16) >> 16 != la)
639 return emulate_gp(ctxt, 0);
640 break;
641 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300642 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
643 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300644 if (!usable)
645 goto bad;
646 /* code segment or read-only data segment */
647 if (((desc.type & 8) || !(desc.type & 2)) && write)
648 goto bad;
649 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400650 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300651 goto bad;
652 lim = desc_limit_scaled(&desc);
653 if ((desc.type & 8) || !(desc.type & 4)) {
654 /* expand-up segment */
655 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
656 goto bad;
657 } else {
658 /* exapand-down segment */
659 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
660 goto bad;
661 lim = desc.d ? 0xffffffff : 0xffff;
662 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
663 goto bad;
664 }
Avi Kivity717746e2011-04-20 13:37:53 +0300665 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300666 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300667 cpl = max(cpl, rpl);
668 if (!(desc.type & 8)) {
669 /* data segment */
670 if (cpl > desc.dpl)
671 goto bad;
672 } else if ((desc.type & 8) && !(desc.type & 4)) {
673 /* nonconforming code segment */
674 if (cpl != desc.dpl)
675 goto bad;
676 } else if ((desc.type & 8) && (desc.type & 4)) {
677 /* conforming code segment */
678 if (cpl < desc.dpl)
679 goto bad;
680 }
681 break;
682 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300683 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300684 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300685 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
686 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300687 *linear = la;
688 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300689bad:
690 if (addr.seg == VCPU_SREG_SS)
691 return emulate_ss(ctxt, addr.seg);
692 else
693 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300694}
695
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400696static int linearize(struct x86_emulate_ctxt *ctxt,
697 struct segmented_address addr,
698 unsigned size, bool write,
699 ulong *linear)
700{
701 return __linearize(ctxt, addr, size, write, false, linear);
702}
703
704
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200705static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
706 struct segmented_address addr,
707 void *data,
708 unsigned size)
709{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200710 int rc;
711 ulong linear;
712
Avi Kivity83b87952011-04-03 11:31:19 +0300713 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200714 if (rc != X86EMUL_CONTINUE)
715 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300716 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200717}
718
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900719/*
720 * Fetch the next byte of the instruction being emulated which is pointed to
721 * by ctxt->_eip, then increment ctxt->_eip.
722 *
723 * Also prefetch the remaining bytes of the instruction without crossing page
724 * boundary if they are not in fetch_cache yet.
725 */
726static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200727{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300728 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200729 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300730 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200731
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900732 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400733 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900734 struct segmented_address addr = { .seg = VCPU_SREG_CS,
735 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300736 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900737 size = min(15UL - cur_size,
738 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400739 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900740 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400741 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900742 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
743 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900744 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200745 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300746 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200747 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900748 *dest = fc->data[ctxt->_eip - fc->start];
749 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900750 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200751}
752
753static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900754 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200755{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900756 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200757
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200758 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900759 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200760 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200761 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900762 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900763 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200764 return rc;
765 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900766 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200767}
768
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900769/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900770#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900771({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900772 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900773 if (rc != X86EMUL_CONTINUE) \
774 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900775 (_type)_x; \
776})
777
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900778#define insn_fetch_arr(_arr, _size, _ctxt) \
779({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900780 if (rc != X86EMUL_CONTINUE) \
781 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900782})
783
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000784/*
785 * Given the 'reg' portion of a ModRM byte, and a register block, return a
786 * pointer into the block that addresses the relevant register.
787 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
788 */
789static void *decode_register(u8 modrm_reg, unsigned long *regs,
790 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791{
792 void *p;
793
794 p = &regs[modrm_reg];
795 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
796 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
797 return p;
798}
799
800static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200801 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 u16 *size, unsigned long *address, int op_bytes)
803{
804 int rc;
805
806 if (op_bytes == 2)
807 op_bytes = 3;
808 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200809 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900810 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200812 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200813 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 return rc;
815}
816
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300817static int test_cc(unsigned int condition, unsigned int flags)
818{
819 int rc = 0;
820
821 switch ((condition & 15) >> 1) {
822 case 0: /* o */
823 rc |= (flags & EFLG_OF);
824 break;
825 case 1: /* b/c/nae */
826 rc |= (flags & EFLG_CF);
827 break;
828 case 2: /* z/e */
829 rc |= (flags & EFLG_ZF);
830 break;
831 case 3: /* be/na */
832 rc |= (flags & (EFLG_CF|EFLG_ZF));
833 break;
834 case 4: /* s */
835 rc |= (flags & EFLG_SF);
836 break;
837 case 5: /* p/pe */
838 rc |= (flags & EFLG_PF);
839 break;
840 case 7: /* le/ng */
841 rc |= (flags & EFLG_ZF);
842 /* fall through */
843 case 6: /* l/nge */
844 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
845 break;
846 }
847
848 /* Odd condition identifiers (lsb == 1) have inverted sense. */
849 return (!!rc ^ (condition & 1));
850}
851
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300852static void fetch_register_operand(struct operand *op)
853{
854 switch (op->bytes) {
855 case 1:
856 op->val = *(u8 *)op->addr.reg;
857 break;
858 case 2:
859 op->val = *(u16 *)op->addr.reg;
860 break;
861 case 4:
862 op->val = *(u32 *)op->addr.reg;
863 break;
864 case 8:
865 op->val = *(u64 *)op->addr.reg;
866 break;
867 }
868}
869
Avi Kivity12537912011-03-29 11:41:27 +0200870static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
871{
872 ctxt->ops->get_fpu(ctxt);
873 switch (reg) {
874 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
875 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
876 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
877 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
878 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
879 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
880 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
881 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
882#ifdef CONFIG_X86_64
883 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
884 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
885 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
886 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
887 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
888 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
889 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
890 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
891#endif
892 default: BUG();
893 }
894 ctxt->ops->put_fpu(ctxt);
895}
896
897static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
898 int reg)
899{
900 ctxt->ops->get_fpu(ctxt);
901 switch (reg) {
902 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
903 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
904 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
905 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
906 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
907 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
908 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
909 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
910#ifdef CONFIG_X86_64
911 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
912 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
913 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
914 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
915 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
916 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
917 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
918 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
919#endif
920 default: BUG();
921 }
922 ctxt->ops->put_fpu(ctxt);
923}
924
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300925static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
926{
927 ctxt->ops->get_fpu(ctxt);
928 switch (reg) {
929 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
930 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
931 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
932 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
933 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
934 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
935 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
936 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
937 default: BUG();
938 }
939 ctxt->ops->put_fpu(ctxt);
940}
941
942static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
943{
944 ctxt->ops->get_fpu(ctxt);
945 switch (reg) {
946 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
947 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
948 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
949 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
950 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
951 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
952 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
953 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
954 default: BUG();
955 }
956 ctxt->ops->put_fpu(ctxt);
957}
958
Avi Kivity12537912011-03-29 11:41:27 +0200959static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200960 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200961{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300962 unsigned reg = ctxt->modrm_reg;
963 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200964
Avi Kivity9dac77f2011-06-01 15:34:25 +0300965 if (!(ctxt->d & ModRM))
966 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200967
Avi Kivity9dac77f2011-06-01 15:34:25 +0300968 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200969 op->type = OP_XMM;
970 op->bytes = 16;
971 op->addr.xmm = reg;
972 read_sse_reg(ctxt, &op->vec_val, reg);
973 return;
974 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300975 if (ctxt->d & Mmx) {
976 reg &= 7;
977 op->type = OP_MM;
978 op->bytes = 8;
979 op->addr.mm = reg;
980 return;
981 }
Avi Kivity12537912011-03-29 11:41:27 +0200982
Avi Kivity3c118e22007-10-31 10:27:04 +0200983 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200984 if (ctxt->d & ByteOp) {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300985 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200986 op->bytes = 1;
987 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300988 op->addr.reg = decode_register(reg, ctxt->regs, 0);
989 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200990 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300991 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200992 op->orig_val = op->val;
993}
994
Avi Kivitya6e34072012-06-10 17:15:39 +0300995static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
996{
997 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
998 ctxt->modrm_seg = VCPU_SREG_SS;
999}
1000
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001002 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001003{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001005 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001006 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001007 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001008
Avi Kivity9dac77f2011-06-01 15:34:25 +03001009 if (ctxt->rex_prefix) {
1010 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1011 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1012 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001013 }
1014
Avi Kivity9dac77f2011-06-01 15:34:25 +03001015 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1016 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1017 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1018 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001019
Avi Kivity9dac77f2011-06-01 15:34:25 +03001020 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001021 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001022 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1023 op->addr.reg = decode_register(ctxt->modrm_rm,
1024 ctxt->regs, ctxt->d & ByteOp);
1025 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001026 op->type = OP_XMM;
1027 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001028 op->addr.xmm = ctxt->modrm_rm;
1029 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001030 return rc;
1031 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001032 if (ctxt->d & Mmx) {
1033 op->type = OP_MM;
1034 op->bytes = 8;
1035 op->addr.xmm = ctxt->modrm_rm & 7;
1036 return rc;
1037 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001038 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001039 return rc;
1040 }
1041
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001042 op->type = OP_MEM;
1043
Avi Kivity9dac77f2011-06-01 15:34:25 +03001044 if (ctxt->ad_bytes == 2) {
1045 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
1046 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
1047 unsigned si = ctxt->regs[VCPU_REGS_RSI];
1048 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001049
1050 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001051 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001052 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001053 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001054 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001055 break;
1056 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001057 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001058 break;
1059 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001060 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001061 break;
1062 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001063 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001064 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001065 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001066 break;
1067 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001068 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001069 break;
1070 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001071 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001072 break;
1073 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001074 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001075 break;
1076 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001077 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001078 break;
1079 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001080 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001081 break;
1082 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001083 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001084 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001085 break;
1086 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001087 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001088 break;
1089 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001090 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1091 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1092 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001093 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001094 } else {
1095 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001096 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001097 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001098 index_reg |= (sib >> 3) & 7;
1099 base_reg |= sib & 7;
1100 scale = sib >> 6;
1101
Avi Kivity9dac77f2011-06-01 15:34:25 +03001102 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001103 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001104 else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001105 modrm_ea += ctxt->regs[base_reg];
Avi Kivitya6e34072012-06-10 17:15:39 +03001106 adjust_modrm_seg(ctxt, base_reg);
1107 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001108 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001109 modrm_ea += ctxt->regs[index_reg] << scale;
1110 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001111 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001112 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001113 } else {
1114 base_reg = ctxt->modrm_rm;
1115 modrm_ea += ctxt->regs[base_reg];
1116 adjust_modrm_seg(ctxt, base_reg);
1117 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001118 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001120 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001121 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122 break;
1123 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001124 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 break;
1126 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001127 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001128 break;
1129 }
1130 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001131 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132done:
1133 return rc;
1134}
1135
1136static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001137 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001139 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001140
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001141 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001142 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001143 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001144 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001145 break;
1146 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001147 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001148 break;
1149 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001150 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001151 break;
1152 }
1153done:
1154 return rc;
1155}
1156
Avi Kivity9dac77f2011-06-01 15:34:25 +03001157static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001158{
Sheng Yang7129eec2010-09-28 16:33:32 +08001159 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001160
Avi Kivity9dac77f2011-06-01 15:34:25 +03001161 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1162 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001163
Avi Kivity9dac77f2011-06-01 15:34:25 +03001164 if (ctxt->src.bytes == 2)
1165 sv = (s16)ctxt->src.val & (s16)mask;
1166 else if (ctxt->src.bytes == 4)
1167 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001168
Avi Kivity9dac77f2011-06-01 15:34:25 +03001169 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001170 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001171
1172 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001173 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001174}
1175
Gleb Natapov9de41572010-04-28 19:15:22 +03001176static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001177 unsigned long addr, void *dest, unsigned size)
1178{
1179 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001180 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001181
1182 while (size) {
1183 int n = min(size, 8u);
1184 size -= n;
1185 if (mc->pos < mc->end)
1186 goto read_cached;
1187
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001188 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1189 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001190 if (rc != X86EMUL_CONTINUE)
1191 return rc;
1192 mc->end += n;
1193
1194 read_cached:
1195 memcpy(dest, mc->data + mc->pos, n);
1196 mc->pos += n;
1197 dest += n;
1198 addr += n;
1199 }
1200 return X86EMUL_CONTINUE;
1201}
1202
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001203static int segmented_read(struct x86_emulate_ctxt *ctxt,
1204 struct segmented_address addr,
1205 void *data,
1206 unsigned size)
1207{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001208 int rc;
1209 ulong linear;
1210
Avi Kivity83b87952011-04-03 11:31:19 +03001211 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001212 if (rc != X86EMUL_CONTINUE)
1213 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001214 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001215}
1216
1217static int segmented_write(struct x86_emulate_ctxt *ctxt,
1218 struct segmented_address addr,
1219 const void *data,
1220 unsigned size)
1221{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001222 int rc;
1223 ulong linear;
1224
Avi Kivity83b87952011-04-03 11:31:19 +03001225 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001226 if (rc != X86EMUL_CONTINUE)
1227 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001228 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1229 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001230}
1231
1232static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1233 struct segmented_address addr,
1234 const void *orig_data, const void *data,
1235 unsigned size)
1236{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001237 int rc;
1238 ulong linear;
1239
Avi Kivity83b87952011-04-03 11:31:19 +03001240 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001241 if (rc != X86EMUL_CONTINUE)
1242 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001243 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1244 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001245}
1246
Gleb Natapov7b262e92010-03-18 15:20:27 +02001247static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001248 unsigned int size, unsigned short port,
1249 void *dest)
1250{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001251 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001252
1253 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001254 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001255 unsigned int count = ctxt->rep_prefix ?
1256 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001257 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1259 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001260 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1261 count);
1262 if (n == 0)
1263 n = 1;
1264 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001265 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001266 return 0;
1267 rc->end = n * size;
1268 }
1269
1270 memcpy(dest, rc->data + rc->pos, size);
1271 rc->pos += size;
1272 return 1;
1273}
1274
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001275static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1276 u16 index, struct desc_struct *desc)
1277{
1278 struct desc_ptr dt;
1279 ulong addr;
1280
1281 ctxt->ops->get_idt(ctxt, &dt);
1282
1283 if (dt.size < index * 8 + 7)
1284 return emulate_gp(ctxt, index << 3 | 0x2);
1285
1286 addr = dt.address + index * 8;
1287 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1288 &ctxt->exception);
1289}
1290
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001291static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001292 u16 selector, struct desc_ptr *dt)
1293{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001294 struct x86_emulate_ops *ops = ctxt->ops;
1295
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001296 if (selector & 1 << 2) {
1297 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001298 u16 sel;
1299
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001300 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001301 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001302 return;
1303
1304 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1305 dt->address = get_desc_base(&desc);
1306 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001307 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001308}
1309
1310/* allowed just for 8 bytes segments */
1311static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001312 u16 selector, struct desc_struct *desc,
1313 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001314{
1315 struct desc_ptr dt;
1316 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001317 ulong addr;
1318
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001319 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001320
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001321 if (dt.size < index * 8 + 7)
1322 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001323
Avi Kivitye9194642012-06-13 16:29:39 +03001324 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001325 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1326 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001327}
1328
1329/* allowed just for 8 bytes segments */
1330static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001331 u16 selector, struct desc_struct *desc)
1332{
1333 struct desc_ptr dt;
1334 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001335 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001336
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001337 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001338
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001339 if (dt.size < index * 8 + 7)
1340 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001341
1342 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001343 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1344 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001345}
1346
Gleb Natapov5601d052011-03-07 14:55:06 +02001347/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001348static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001349 u16 selector, int seg)
1350{
Avi Kivity869be992012-06-13 16:30:53 +03001351 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001352 u8 dpl, rpl, cpl;
1353 unsigned err_vec = GP_VECTOR;
1354 u32 err_code = 0;
1355 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001356 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001357 int ret;
1358
1359 memset(&seg_desc, 0, sizeof seg_desc);
1360
1361 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1362 || ctxt->mode == X86EMUL_MODE_REAL) {
1363 /* set real mode segment descriptor */
1364 set_desc_base(&seg_desc, selector << 4);
1365 set_desc_limit(&seg_desc, 0xffff);
1366 seg_desc.type = 3;
1367 seg_desc.p = 1;
1368 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001369 if (ctxt->mode == X86EMUL_MODE_VM86)
1370 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001371 goto load;
1372 }
1373
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001374 rpl = selector & 3;
1375 cpl = ctxt->ops->cpl(ctxt);
1376
1377 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1378 if ((seg == VCPU_SREG_CS
1379 || (seg == VCPU_SREG_SS
1380 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1381 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001382 && null_selector)
1383 goto exception;
1384
1385 /* TR should be in GDT only */
1386 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1387 goto exception;
1388
1389 if (null_selector) /* for NULL selector skip all following checks */
1390 goto load;
1391
Avi Kivitye9194642012-06-13 16:29:39 +03001392 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393 if (ret != X86EMUL_CONTINUE)
1394 return ret;
1395
1396 err_code = selector & 0xfffc;
1397 err_vec = GP_VECTOR;
1398
1399 /* can't load system descriptor into segment selecor */
1400 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1401 goto exception;
1402
1403 if (!seg_desc.p) {
1404 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1405 goto exception;
1406 }
1407
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001408 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001409
1410 switch (seg) {
1411 case VCPU_SREG_SS:
1412 /*
1413 * segment is not a writable data segment or segment
1414 * selector's RPL != CPL or segment selector's RPL != CPL
1415 */
1416 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1417 goto exception;
1418 break;
1419 case VCPU_SREG_CS:
1420 if (!(seg_desc.type & 8))
1421 goto exception;
1422
1423 if (seg_desc.type & 4) {
1424 /* conforming */
1425 if (dpl > cpl)
1426 goto exception;
1427 } else {
1428 /* nonconforming */
1429 if (rpl > cpl || dpl != cpl)
1430 goto exception;
1431 }
1432 /* CS(RPL) <- CPL */
1433 selector = (selector & 0xfffc) | cpl;
1434 break;
1435 case VCPU_SREG_TR:
1436 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1437 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001438 old_desc = seg_desc;
1439 seg_desc.type |= 2; /* busy */
1440 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1441 sizeof(seg_desc), &ctxt->exception);
1442 if (ret != X86EMUL_CONTINUE)
1443 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444 break;
1445 case VCPU_SREG_LDTR:
1446 if (seg_desc.s || seg_desc.type != 2)
1447 goto exception;
1448 break;
1449 default: /* DS, ES, FS, or GS */
1450 /*
1451 * segment is not a data or readable code segment or
1452 * ((segment is a data or nonconforming code segment)
1453 * and (both RPL and CPL > DPL))
1454 */
1455 if ((seg_desc.type & 0xa) == 0x8 ||
1456 (((seg_desc.type & 0xc) != 0xc) &&
1457 (rpl > dpl && cpl > dpl)))
1458 goto exception;
1459 break;
1460 }
1461
1462 if (seg_desc.s) {
1463 /* mark segment as accessed */
1464 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001465 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466 if (ret != X86EMUL_CONTINUE)
1467 return ret;
1468 }
1469load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001470 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001471 return X86EMUL_CONTINUE;
1472exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001473 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001474 return X86EMUL_PROPAGATE_FAULT;
1475}
1476
Wei Yongjun31be40b2010-08-17 09:17:30 +08001477static void write_register_operand(struct operand *op)
1478{
1479 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1480 switch (op->bytes) {
1481 case 1:
1482 *(u8 *)op->addr.reg = (u8)op->val;
1483 break;
1484 case 2:
1485 *(u16 *)op->addr.reg = (u16)op->val;
1486 break;
1487 case 4:
1488 *op->addr.reg = (u32)op->val;
1489 break; /* 64b: zero-extend */
1490 case 8:
1491 *op->addr.reg = op->val;
1492 break;
1493 }
1494}
1495
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001496static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001497{
1498 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001499
Avi Kivity9dac77f2011-06-01 15:34:25 +03001500 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001501 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001502 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001503 break;
1504 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001505 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001506 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001507 ctxt->dst.addr.mem,
1508 &ctxt->dst.orig_val,
1509 &ctxt->dst.val,
1510 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001511 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001512 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001513 ctxt->dst.addr.mem,
1514 &ctxt->dst.val,
1515 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001516 if (rc != X86EMUL_CONTINUE)
1517 return rc;
1518 break;
Avi Kivity12537912011-03-29 11:41:27 +02001519 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001520 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001521 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001522 case OP_MM:
1523 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1524 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001525 case OP_NONE:
1526 /* no writeback */
1527 break;
1528 default:
1529 break;
1530 }
1531 return X86EMUL_CONTINUE;
1532}
1533
Avi Kivity51ddff52012-06-12 20:19:40 +03001534static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001535{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001536 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001537
Avi Kivity5ad105e2012-08-19 14:34:31 +03001538 rsp_increment(ctxt, -bytes);
1539 addr.ea = ctxt->regs[VCPU_REGS_RSP] & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001540 addr.seg = VCPU_SREG_SS;
1541
Avi Kivity51ddff52012-06-12 20:19:40 +03001542 return segmented_write(ctxt, addr, data, bytes);
1543}
1544
1545static int em_push(struct x86_emulate_ctxt *ctxt)
1546{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001547 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001548 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001549 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001550}
1551
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001552static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001553 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001554{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001555 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001556 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001557
Avi Kivity5ad105e2012-08-19 14:34:31 +03001558 addr.ea = ctxt->regs[VCPU_REGS_RSP] & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001559 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001560 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001561 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001562 return rc;
1563
Avi Kivity5ad105e2012-08-19 14:34:31 +03001564 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001565 return rc;
1566}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001567
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001568static int em_pop(struct x86_emulate_ctxt *ctxt)
1569{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001570 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001571}
1572
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001573static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001574 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001575{
1576 int rc;
1577 unsigned long val, change_mask;
1578 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001579 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001580
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001581 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001582 if (rc != X86EMUL_CONTINUE)
1583 return rc;
1584
1585 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1586 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1587
1588 switch(ctxt->mode) {
1589 case X86EMUL_MODE_PROT64:
1590 case X86EMUL_MODE_PROT32:
1591 case X86EMUL_MODE_PROT16:
1592 if (cpl == 0)
1593 change_mask |= EFLG_IOPL;
1594 if (cpl <= iopl)
1595 change_mask |= EFLG_IF;
1596 break;
1597 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001598 if (iopl < 3)
1599 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001600 change_mask |= EFLG_IF;
1601 break;
1602 default: /* real mode */
1603 change_mask |= (EFLG_IOPL | EFLG_IF);
1604 break;
1605 }
1606
1607 *(unsigned long *)dest =
1608 (ctxt->eflags & ~change_mask) | (val & change_mask);
1609
1610 return rc;
1611}
1612
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001613static int em_popf(struct x86_emulate_ctxt *ctxt)
1614{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001615 ctxt->dst.type = OP_REG;
1616 ctxt->dst.addr.reg = &ctxt->eflags;
1617 ctxt->dst.bytes = ctxt->op_bytes;
1618 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001619}
1620
Avi Kivity612e89f2012-06-12 20:03:23 +03001621static int em_enter(struct x86_emulate_ctxt *ctxt)
1622{
1623 int rc;
1624 unsigned frame_size = ctxt->src.val;
1625 unsigned nesting_level = ctxt->src2.val & 31;
1626
1627 if (nesting_level)
1628 return X86EMUL_UNHANDLEABLE;
1629
1630 rc = push(ctxt, &ctxt->regs[VCPU_REGS_RBP], stack_size(ctxt));
1631 if (rc != X86EMUL_CONTINUE)
1632 return rc;
1633 assign_masked(&ctxt->regs[VCPU_REGS_RBP], ctxt->regs[VCPU_REGS_RSP],
1634 stack_mask(ctxt));
1635 assign_masked(&ctxt->regs[VCPU_REGS_RSP],
1636 ctxt->regs[VCPU_REGS_RSP] - frame_size,
1637 stack_mask(ctxt));
1638 return X86EMUL_CONTINUE;
1639}
1640
Avi Kivityf47cfa32012-06-07 17:49:24 +03001641static int em_leave(struct x86_emulate_ctxt *ctxt)
1642{
1643 assign_masked(&ctxt->regs[VCPU_REGS_RSP], ctxt->regs[VCPU_REGS_RBP],
1644 stack_mask(ctxt));
1645 return emulate_pop(ctxt, &ctxt->regs[VCPU_REGS_RBP], ctxt->op_bytes);
1646}
1647
Avi Kivity1cd196e2011-09-13 10:45:51 +03001648static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001649{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001650 int seg = ctxt->src2.val;
1651
Avi Kivity9dac77f2011-06-01 15:34:25 +03001652 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001653
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001654 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001655}
1656
Avi Kivity1cd196e2011-09-13 10:45:51 +03001657static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001658{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001659 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001660 unsigned long selector;
1661 int rc;
1662
Avi Kivity9dac77f2011-06-01 15:34:25 +03001663 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001664 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001665 return rc;
1666
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001667 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001668 return rc;
1669}
1670
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001671static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001672{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001673 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001674 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001675 int reg = VCPU_REGS_RAX;
1676
1677 while (reg <= VCPU_REGS_RDI) {
1678 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001679 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001680
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001681 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001682 if (rc != X86EMUL_CONTINUE)
1683 return rc;
1684
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001685 ++reg;
1686 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001687
Wei Yongjunc37eda12010-06-15 09:03:33 +08001688 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001689}
1690
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001691static int em_pushf(struct x86_emulate_ctxt *ctxt)
1692{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001693 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001694 return em_push(ctxt);
1695}
1696
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001697static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001698{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001699 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001700 int reg = VCPU_REGS_RDI;
1701
1702 while (reg >= VCPU_REGS_RAX) {
1703 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001704 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001705 --reg;
1706 }
1707
Avi Kivity9dac77f2011-06-01 15:34:25 +03001708 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001709 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001710 break;
1711 --reg;
1712 }
1713 return rc;
1714}
1715
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001716int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001717{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001718 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001719 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001720 struct desc_ptr dt;
1721 gva_t cs_addr;
1722 gva_t eip_addr;
1723 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001724
1725 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001726 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001727 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001728 if (rc != X86EMUL_CONTINUE)
1729 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001730
1731 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1732
Avi Kivity9dac77f2011-06-01 15:34:25 +03001733 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001734 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001735 if (rc != X86EMUL_CONTINUE)
1736 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001737
Avi Kivity9dac77f2011-06-01 15:34:25 +03001738 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001739 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001740 if (rc != X86EMUL_CONTINUE)
1741 return rc;
1742
Avi Kivity4bff1e862011-04-20 13:37:53 +03001743 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001744
1745 eip_addr = dt.address + (irq << 2);
1746 cs_addr = dt.address + (irq << 2) + 2;
1747
Avi Kivity0f65dd72011-04-20 13:37:53 +03001748 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001749 if (rc != X86EMUL_CONTINUE)
1750 return rc;
1751
Avi Kivity0f65dd72011-04-20 13:37:53 +03001752 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001753 if (rc != X86EMUL_CONTINUE)
1754 return rc;
1755
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001756 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001757 if (rc != X86EMUL_CONTINUE)
1758 return rc;
1759
Avi Kivity9dac77f2011-06-01 15:34:25 +03001760 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001761
1762 return rc;
1763}
1764
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001765static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001766{
1767 switch(ctxt->mode) {
1768 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001769 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001770 case X86EMUL_MODE_VM86:
1771 case X86EMUL_MODE_PROT16:
1772 case X86EMUL_MODE_PROT32:
1773 case X86EMUL_MODE_PROT64:
1774 default:
1775 /* Protected mode interrupts unimplemented yet */
1776 return X86EMUL_UNHANDLEABLE;
1777 }
1778}
1779
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001780static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001781{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001782 int rc = X86EMUL_CONTINUE;
1783 unsigned long temp_eip = 0;
1784 unsigned long temp_eflags = 0;
1785 unsigned long cs = 0;
1786 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1787 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1788 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1789 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1790
1791 /* TODO: Add stack limit check */
1792
Avi Kivity9dac77f2011-06-01 15:34:25 +03001793 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001794
1795 if (rc != X86EMUL_CONTINUE)
1796 return rc;
1797
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001798 if (temp_eip & ~0xffff)
1799 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001800
Avi Kivity9dac77f2011-06-01 15:34:25 +03001801 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001802
1803 if (rc != X86EMUL_CONTINUE)
1804 return rc;
1805
Avi Kivity9dac77f2011-06-01 15:34:25 +03001806 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001807
1808 if (rc != X86EMUL_CONTINUE)
1809 return rc;
1810
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001811 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001812
1813 if (rc != X86EMUL_CONTINUE)
1814 return rc;
1815
Avi Kivity9dac77f2011-06-01 15:34:25 +03001816 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001817
1818
Avi Kivity9dac77f2011-06-01 15:34:25 +03001819 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001820 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001821 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001822 ctxt->eflags &= ~0xffff;
1823 ctxt->eflags |= temp_eflags;
1824 }
1825
1826 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1827 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1828
1829 return rc;
1830}
1831
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001832static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001833{
1834 switch(ctxt->mode) {
1835 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001836 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001837 case X86EMUL_MODE_VM86:
1838 case X86EMUL_MODE_PROT16:
1839 case X86EMUL_MODE_PROT32:
1840 case X86EMUL_MODE_PROT64:
1841 default:
1842 /* iret from protected mode unimplemented yet */
1843 return X86EMUL_UNHANDLEABLE;
1844 }
1845}
1846
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001847static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1848{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001849 int rc;
1850 unsigned short sel;
1851
Avi Kivity9dac77f2011-06-01 15:34:25 +03001852 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001853
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001854 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001855 if (rc != X86EMUL_CONTINUE)
1856 return rc;
1857
Avi Kivity9dac77f2011-06-01 15:34:25 +03001858 ctxt->_eip = 0;
1859 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001860 return X86EMUL_CONTINUE;
1861}
1862
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001863static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001864{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001865 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001867 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001868 break;
1869 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001870 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001871 break;
1872 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001873 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001874 break;
1875 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001876 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001877 break;
1878 case 4: /* sal/shl */
1879 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001880 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001881 break;
1882 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001883 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001884 break;
1885 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001886 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001887 break;
1888 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001889 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001890}
1891
Avi Kivity3329ece2011-09-13 10:45:39 +03001892static int em_not(struct x86_emulate_ctxt *ctxt)
1893{
1894 ctxt->dst.val = ~ctxt->dst.val;
1895 return X86EMUL_CONTINUE;
1896}
1897
1898static int em_neg(struct x86_emulate_ctxt *ctxt)
1899{
1900 emulate_1op(ctxt, "neg");
1901 return X86EMUL_CONTINUE;
1902}
1903
1904static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1905{
1906 u8 ex = 0;
1907
1908 emulate_1op_rax_rdx(ctxt, "mul", ex);
1909 return X86EMUL_CONTINUE;
1910}
1911
1912static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1913{
1914 u8 ex = 0;
1915
1916 emulate_1op_rax_rdx(ctxt, "imul", ex);
1917 return X86EMUL_CONTINUE;
1918}
1919
1920static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001921{
Avi Kivity34d1f492010-08-26 11:59:01 +03001922 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923
Avi Kivity3329ece2011-09-13 10:45:39 +03001924 emulate_1op_rax_rdx(ctxt, "div", de);
1925 if (de)
1926 return emulate_de(ctxt);
1927 return X86EMUL_CONTINUE;
1928}
1929
1930static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1931{
1932 u8 de = 0;
1933
1934 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001935 if (de)
1936 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001937 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001938}
1939
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001940static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001941{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001942 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001943
Avi Kivity9dac77f2011-06-01 15:34:25 +03001944 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001945 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001946 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001947 break;
1948 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001949 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001950 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001951 case 2: /* call near abs */ {
1952 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001953 old_eip = ctxt->_eip;
1954 ctxt->_eip = ctxt->src.val;
1955 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001956 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001957 break;
1958 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001959 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001960 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001961 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001962 case 5: /* jmp far */
1963 rc = em_jmp_far(ctxt);
1964 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001965 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001966 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001967 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001968 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001969 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001970}
1971
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001972static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001973{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001974 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001975
Avi Kivity9dac77f2011-06-01 15:34:25 +03001976 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1977 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1978 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1979 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001980 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001981 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001982 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1983 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001984
Laurent Vivier05f086f2007-09-24 11:10:55 +02001985 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001986 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001987 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001988}
1989
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001990static int em_ret(struct x86_emulate_ctxt *ctxt)
1991{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001992 ctxt->dst.type = OP_REG;
1993 ctxt->dst.addr.reg = &ctxt->_eip;
1994 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001995 return em_pop(ctxt);
1996}
1997
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001998static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001999{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002000 int rc;
2001 unsigned long cs;
2002
Avi Kivity9dac77f2011-06-01 15:34:25 +03002003 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002004 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002005 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002006 if (ctxt->op_bytes == 4)
2007 ctxt->_eip = (u32)ctxt->_eip;
2008 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002009 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002010 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002011 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002012 return rc;
2013}
2014
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002015static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2016{
2017 /* Save real source value, then compare EAX against destination. */
2018 ctxt->src.orig_val = ctxt->src.val;
2019 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
2020 emulate_2op_SrcV(ctxt, "cmp");
2021
2022 if (ctxt->eflags & EFLG_ZF) {
2023 /* Success: write back to memory. */
2024 ctxt->dst.val = ctxt->src.orig_val;
2025 } else {
2026 /* Failure: write the value we saw to EAX. */
2027 ctxt->dst.type = OP_REG;
2028 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
2029 }
2030 return X86EMUL_CONTINUE;
2031}
2032
Avi Kivityd4b43252011-09-13 10:45:50 +03002033static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002034{
Avi Kivityd4b43252011-09-13 10:45:50 +03002035 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002036 unsigned short sel;
2037 int rc;
2038
Avi Kivity9dac77f2011-06-01 15:34:25 +03002039 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002040
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002041 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002042 if (rc != X86EMUL_CONTINUE)
2043 return rc;
2044
Avi Kivity9dac77f2011-06-01 15:34:25 +03002045 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002046 return rc;
2047}
2048
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002049static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002050setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002051 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002052{
Avi Kivity1aa36612011-04-27 13:20:30 +03002053 u16 selector;
2054
Gleb Natapov79168fd2010-04-28 19:15:30 +03002055 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002056 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002057 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002058
2059 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002060 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002061 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002062 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002063 cs->type = 0x0b; /* Read, Execute, Accessed */
2064 cs->s = 1;
2065 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002066 cs->p = 1;
2067 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002068
Gleb Natapov79168fd2010-04-28 19:15:30 +03002069 set_desc_base(ss, 0); /* flat segment */
2070 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002071 ss->g = 1; /* 4kb granularity */
2072 ss->s = 1;
2073 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002074 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002075 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002076 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002077}
2078
Avi Kivity1a18a692012-02-01 12:23:21 +02002079static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2080{
2081 u32 eax, ebx, ecx, edx;
2082
2083 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002084 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2085 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002086 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2087 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2088}
2089
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002090static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2091{
2092 struct x86_emulate_ops *ops = ctxt->ops;
2093 u32 eax, ebx, ecx, edx;
2094
2095 /*
2096 * syscall should always be enabled in longmode - so only become
2097 * vendor specific (cpuid) if other modes are active...
2098 */
2099 if (ctxt->mode == X86EMUL_MODE_PROT64)
2100 return true;
2101
2102 eax = 0x00000000;
2103 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002104 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2105 /*
2106 * Intel ("GenuineIntel")
2107 * remark: Intel CPUs only support "syscall" in 64bit
2108 * longmode. Also an 64bit guest with a
2109 * 32bit compat-app running will #UD !! While this
2110 * behaviour can be fixed (by emulating) into AMD
2111 * response - CPUs of AMD can't behave like Intel.
2112 */
2113 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2114 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2115 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2116 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002117
Avi Kivity0017f932012-06-07 14:10:16 +03002118 /* AMD ("AuthenticAMD") */
2119 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2120 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2121 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2122 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002123
Avi Kivity0017f932012-06-07 14:10:16 +03002124 /* AMD ("AMDisbetter!") */
2125 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2126 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2127 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2128 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002129
2130 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2131 return false;
2132}
2133
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002134static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002135{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002136 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002137 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002138 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002139 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002140 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002141
2142 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002143 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002144 ctxt->mode == X86EMUL_MODE_VM86)
2145 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002146
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002147 if (!(em_syscall_is_enabled(ctxt)))
2148 return emulate_ud(ctxt);
2149
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002150 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002151 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002152
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002153 if (!(efer & EFER_SCE))
2154 return emulate_ud(ctxt);
2155
Avi Kivity717746e2011-04-20 13:37:53 +03002156 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002157 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002158 cs_sel = (u16)(msr_data & 0xfffc);
2159 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002160
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002161 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002162 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002163 cs.l = 1;
2164 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002165 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2166 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002167
Avi Kivity9dac77f2011-06-01 15:34:25 +03002168 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002169 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002170#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03002171 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002172
Avi Kivity717746e2011-04-20 13:37:53 +03002173 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002174 ctxt->mode == X86EMUL_MODE_PROT64 ?
2175 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002176 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002177
Avi Kivity717746e2011-04-20 13:37:53 +03002178 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002179 ctxt->eflags &= ~(msr_data | EFLG_RF);
2180#endif
2181 } else {
2182 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002183 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002184 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002185
2186 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2187 }
2188
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002189 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002190}
2191
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002192static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002193{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002194 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002195 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002196 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002197 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002198 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002199
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002200 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002201 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002202 if (ctxt->mode == X86EMUL_MODE_REAL)
2203 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002204
Avi Kivity1a18a692012-02-01 12:23:21 +02002205 /*
2206 * Not recognized on AMD in compat mode (but is recognized in legacy
2207 * mode).
2208 */
2209 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2210 && !vendor_intel(ctxt))
2211 return emulate_ud(ctxt);
2212
Andre Przywara8c604352009-06-18 12:56:01 +02002213 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2214 * Therefore, we inject an #UD.
2215 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002216 if (ctxt->mode == X86EMUL_MODE_PROT64)
2217 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002218
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002219 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002220
Avi Kivity717746e2011-04-20 13:37:53 +03002221 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002222 switch (ctxt->mode) {
2223 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002224 if ((msr_data & 0xfffc) == 0x0)
2225 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002226 break;
2227 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002228 if (msr_data == 0x0)
2229 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002230 break;
2231 }
2232
2233 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002234 cs_sel = (u16)msr_data;
2235 cs_sel &= ~SELECTOR_RPL_MASK;
2236 ss_sel = cs_sel + 8;
2237 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002238 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002239 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002240 cs.l = 1;
2241 }
2242
Avi Kivity1aa36612011-04-27 13:20:30 +03002243 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2244 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002245
Avi Kivity717746e2011-04-20 13:37:53 +03002246 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002247 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002248
Avi Kivity717746e2011-04-20 13:37:53 +03002249 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002250 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002251
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002252 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002253}
2254
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002255static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002256{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002257 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002258 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002259 u64 msr_data;
2260 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002261 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002262
Gleb Natapova0044752010-02-10 14:21:31 +02002263 /* inject #GP if in real mode or Virtual 8086 mode */
2264 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002265 ctxt->mode == X86EMUL_MODE_VM86)
2266 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002267
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002268 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002269
Avi Kivity9dac77f2011-06-01 15:34:25 +03002270 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002271 usermode = X86EMUL_MODE_PROT64;
2272 else
2273 usermode = X86EMUL_MODE_PROT32;
2274
2275 cs.dpl = 3;
2276 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002277 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002278 switch (usermode) {
2279 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002280 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002281 if ((msr_data & 0xfffc) == 0x0)
2282 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002283 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002284 break;
2285 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002286 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002287 if (msr_data == 0x0)
2288 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002289 ss_sel = cs_sel + 8;
2290 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002291 cs.l = 1;
2292 break;
2293 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002294 cs_sel |= SELECTOR_RPL_MASK;
2295 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002296
Avi Kivity1aa36612011-04-27 13:20:30 +03002297 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2298 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002299
Avi Kivity9dac77f2011-06-01 15:34:25 +03002300 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2301 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002302
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002303 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002304}
2305
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002306static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002307{
2308 int iopl;
2309 if (ctxt->mode == X86EMUL_MODE_REAL)
2310 return false;
2311 if (ctxt->mode == X86EMUL_MODE_VM86)
2312 return true;
2313 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002314 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002315}
2316
2317static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002318 u16 port, u16 len)
2319{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002320 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002321 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002322 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002323 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002324 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002325 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002326 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002327
Avi Kivity1aa36612011-04-27 13:20:30 +03002328 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002329 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002330 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002331 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002332 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002333 base = get_desc_base(&tr_seg);
2334#ifdef CONFIG_X86_64
2335 base |= ((u64)base3) << 32;
2336#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002337 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002338 if (r != X86EMUL_CONTINUE)
2339 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002340 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002341 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002342 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002343 if (r != X86EMUL_CONTINUE)
2344 return false;
2345 if ((perm >> bit_idx) & mask)
2346 return false;
2347 return true;
2348}
2349
2350static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002351 u16 port, u16 len)
2352{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002353 if (ctxt->perm_ok)
2354 return true;
2355
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002356 if (emulator_bad_iopl(ctxt))
2357 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002358 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002359
2360 ctxt->perm_ok = true;
2361
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002362 return true;
2363}
2364
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002365static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002366 struct tss_segment_16 *tss)
2367{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002368 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002369 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002370 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2371 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2372 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2373 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2374 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2375 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2376 tss->si = ctxt->regs[VCPU_REGS_RSI];
2377 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002378
Avi Kivity1aa36612011-04-27 13:20:30 +03002379 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2380 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2381 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2382 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2383 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002384}
2385
2386static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 struct tss_segment_16 *tss)
2388{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002389 int ret;
2390
Avi Kivity9dac77f2011-06-01 15:34:25 +03002391 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002392 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002393 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2394 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2395 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2396 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2397 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2398 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2399 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2400 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002401
2402 /*
2403 * SDM says that segment selectors are loaded before segment
2404 * descriptors
2405 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002406 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2407 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2408 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2409 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2410 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411
2412 /*
2413 * Now load segment descriptors. If fault happenes at this stage
2414 * it is handled in a context of new task
2415 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002416 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002417 if (ret != X86EMUL_CONTINUE)
2418 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002419 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002420 if (ret != X86EMUL_CONTINUE)
2421 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002422 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002423 if (ret != X86EMUL_CONTINUE)
2424 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002425 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002426 if (ret != X86EMUL_CONTINUE)
2427 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002428 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002429 if (ret != X86EMUL_CONTINUE)
2430 return ret;
2431
2432 return X86EMUL_CONTINUE;
2433}
2434
2435static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436 u16 tss_selector, u16 old_tss_sel,
2437 ulong old_tss_base, struct desc_struct *new_desc)
2438{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002439 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440 struct tss_segment_16 tss_seg;
2441 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002442 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443
Avi Kivity0f65dd72011-04-20 13:37:53 +03002444 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002445 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002446 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002447 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002448 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002449
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002450 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002451
Avi Kivity0f65dd72011-04-20 13:37:53 +03002452 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002453 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002454 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002455 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002456 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457
Avi Kivity0f65dd72011-04-20 13:37:53 +03002458 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002459 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002460 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002462 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002463
2464 if (old_tss_sel != 0xffff) {
2465 tss_seg.prev_task_link = old_tss_sel;
2466
Avi Kivity0f65dd72011-04-20 13:37:53 +03002467 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002468 &tss_seg.prev_task_link,
2469 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002470 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002471 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002474 }
2475
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002476 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002477}
2478
2479static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480 struct tss_segment_32 *tss)
2481{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002482 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002483 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002484 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002485 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2486 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2487 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2488 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2489 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2490 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2491 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2492 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002493
Avi Kivity1aa36612011-04-27 13:20:30 +03002494 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2495 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2496 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2497 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2498 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2499 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2500 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002501}
2502
2503static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504 struct tss_segment_32 *tss)
2505{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506 int ret;
2507
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002508 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002509 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002510 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002512
2513 /* General purpose registers */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002514 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2515 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2516 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2517 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2518 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2519 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2520 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2521 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522
2523 /*
2524 * SDM says that segment selectors are loaded before segment
2525 * descriptors
2526 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002527 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2528 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2529 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2530 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2531 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2532 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2533 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534
2535 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002536 * If we're switching between Protected Mode and VM86, we need to make
2537 * sure to update the mode before loading the segment descriptors so
2538 * that the selectors are interpreted correctly.
2539 *
2540 * Need to get rflags to the vcpu struct immediately because it
2541 * influences the CPL which is checked at least when loading the segment
2542 * descriptors and when pushing an error code to the new kernel stack.
2543 *
2544 * TODO Introduce a separate ctxt->ops->set_cpl callback
2545 */
2546 if (ctxt->eflags & X86_EFLAGS_VM)
2547 ctxt->mode = X86EMUL_MODE_VM86;
2548 else
2549 ctxt->mode = X86EMUL_MODE_PROT32;
2550
2551 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2552
2553 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554 * Now load segment descriptors. If fault happenes at this stage
2555 * it is handled in a context of new task
2556 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002557 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558 if (ret != X86EMUL_CONTINUE)
2559 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002560 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002561 if (ret != X86EMUL_CONTINUE)
2562 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002563 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564 if (ret != X86EMUL_CONTINUE)
2565 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002566 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567 if (ret != X86EMUL_CONTINUE)
2568 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002569 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 if (ret != X86EMUL_CONTINUE)
2571 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002572 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 if (ret != X86EMUL_CONTINUE)
2574 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002575 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 if (ret != X86EMUL_CONTINUE)
2577 return ret;
2578
2579 return X86EMUL_CONTINUE;
2580}
2581
2582static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002583 u16 tss_selector, u16 old_tss_sel,
2584 ulong old_tss_base, struct desc_struct *new_desc)
2585{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002586 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 struct tss_segment_32 tss_seg;
2588 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002589 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590
Avi Kivity0f65dd72011-04-20 13:37:53 +03002591 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002592 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002593 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002597 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598
Avi Kivity0f65dd72011-04-20 13:37:53 +03002599 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002600 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002601 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604
Avi Kivity0f65dd72011-04-20 13:37:53 +03002605 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002606 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002607 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610
2611 if (old_tss_sel != 0xffff) {
2612 tss_seg.prev_task_link = old_tss_sel;
2613
Avi Kivity0f65dd72011-04-20 13:37:53 +03002614 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615 &tss_seg.prev_task_link,
2616 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002617 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002618 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 }
2622
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002623 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624}
2625
2626static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002627 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002628 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002630 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631 struct desc_struct curr_tss_desc, next_tss_desc;
2632 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002633 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002635 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002636 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002637 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638
2639 /* FIXME: old_tss_base == ~0 ? */
2640
Avi Kivitye9194642012-06-13 16:29:39 +03002641 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 if (ret != X86EMUL_CONTINUE)
2643 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002644 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002645 if (ret != X86EMUL_CONTINUE)
2646 return ret;
2647
2648 /* FIXME: check that next_tss_desc is tss */
2649
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002650 /*
2651 * Check privileges. The three cases are task switch caused by...
2652 *
2653 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2654 * 2. Exception/IRQ/iret: No check is performed
2655 * 3. jmp/call to TSS: Check agains DPL of the TSS
2656 */
2657 if (reason == TASK_SWITCH_GATE) {
2658 if (idt_index != -1) {
2659 /* Software interrupts */
2660 struct desc_struct task_gate_desc;
2661 int dpl;
2662
2663 ret = read_interrupt_descriptor(ctxt, idt_index,
2664 &task_gate_desc);
2665 if (ret != X86EMUL_CONTINUE)
2666 return ret;
2667
2668 dpl = task_gate_desc.dpl;
2669 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2670 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2671 }
2672 } else if (reason != TASK_SWITCH_IRET) {
2673 int dpl = next_tss_desc.dpl;
2674 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2675 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 }
2677
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002678
Gleb Natapovceffb452010-03-18 15:20:19 +02002679 desc_limit = desc_limit_scaled(&next_tss_desc);
2680 if (!next_tss_desc.p ||
2681 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2682 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002683 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684 return X86EMUL_PROPAGATE_FAULT;
2685 }
2686
2687 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2688 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002689 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 }
2691
2692 if (reason == TASK_SWITCH_IRET)
2693 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2694
2695 /* set back link to prev task only if NT bit is set in eflags
2696 note that old_tss_sel is not used afetr this point */
2697 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2698 old_tss_sel = 0xffff;
2699
2700 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002701 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002702 old_tss_base, &next_tss_desc);
2703 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002704 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002705 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002706 if (ret != X86EMUL_CONTINUE)
2707 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002708
2709 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2710 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2711
2712 if (reason != TASK_SWITCH_IRET) {
2713 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002714 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 }
2716
Avi Kivity717746e2011-04-20 13:37:53 +03002717 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002718 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719
Jan Kiszkae269fb22010-04-14 15:51:09 +02002720 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002721 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2722 ctxt->lock_prefix = 0;
2723 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002724 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002725 }
2726
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 return ret;
2728}
2729
2730int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002731 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002732 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002734 int rc;
2735
Avi Kivity9dac77f2011-06-01 15:34:25 +03002736 ctxt->_eip = ctxt->eip;
2737 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002739 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002740 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002742 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002743 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002745 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746}
2747
Avi Kivity90de84f2010-11-17 15:28:21 +02002748static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002749 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002750{
Gleb Natapova682e352010-03-18 15:20:21 +02002751 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2752
Avi Kivity9dac77f2011-06-01 15:34:25 +03002753 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2754 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002755 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002756}
2757
Avi Kivity7af04fc2010-08-18 14:16:35 +03002758static int em_das(struct x86_emulate_ctxt *ctxt)
2759{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002760 u8 al, old_al;
2761 bool af, cf, old_cf;
2762
2763 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002764 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002765
2766 old_al = al;
2767 old_cf = cf;
2768 cf = false;
2769 af = ctxt->eflags & X86_EFLAGS_AF;
2770 if ((al & 0x0f) > 9 || af) {
2771 al -= 6;
2772 cf = old_cf | (al >= 250);
2773 af = true;
2774 } else {
2775 af = false;
2776 }
2777 if (old_al > 0x99 || old_cf) {
2778 al -= 0x60;
2779 cf = true;
2780 }
2781
Avi Kivity9dac77f2011-06-01 15:34:25 +03002782 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002783 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002784 ctxt->src.type = OP_IMM;
2785 ctxt->src.val = 0;
2786 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002787 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002788 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2789 if (cf)
2790 ctxt->eflags |= X86_EFLAGS_CF;
2791 if (af)
2792 ctxt->eflags |= X86_EFLAGS_AF;
2793 return X86EMUL_CONTINUE;
2794}
2795
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002796static int em_call(struct x86_emulate_ctxt *ctxt)
2797{
2798 long rel = ctxt->src.val;
2799
2800 ctxt->src.val = (unsigned long)ctxt->_eip;
2801 jmp_rel(ctxt, rel);
2802 return em_push(ctxt);
2803}
2804
Avi Kivity0ef753b2010-08-18 14:51:45 +03002805static int em_call_far(struct x86_emulate_ctxt *ctxt)
2806{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002807 u16 sel, old_cs;
2808 ulong old_eip;
2809 int rc;
2810
Avi Kivity1aa36612011-04-27 13:20:30 +03002811 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002812 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002813
Avi Kivity9dac77f2011-06-01 15:34:25 +03002814 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002815 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002816 return X86EMUL_CONTINUE;
2817
Avi Kivity9dac77f2011-06-01 15:34:25 +03002818 ctxt->_eip = 0;
2819 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002820
Avi Kivity9dac77f2011-06-01 15:34:25 +03002821 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002822 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002823 if (rc != X86EMUL_CONTINUE)
2824 return rc;
2825
Avi Kivity9dac77f2011-06-01 15:34:25 +03002826 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002827 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002828}
2829
Avi Kivity40ece7c2010-08-18 15:12:09 +03002830static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2831{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002832 int rc;
2833
Avi Kivity9dac77f2011-06-01 15:34:25 +03002834 ctxt->dst.type = OP_REG;
2835 ctxt->dst.addr.reg = &ctxt->_eip;
2836 ctxt->dst.bytes = ctxt->op_bytes;
2837 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002838 if (rc != X86EMUL_CONTINUE)
2839 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002840 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002841 return X86EMUL_CONTINUE;
2842}
2843
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002844static int em_add(struct x86_emulate_ctxt *ctxt)
2845{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002846 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002847 return X86EMUL_CONTINUE;
2848}
2849
2850static int em_or(struct x86_emulate_ctxt *ctxt)
2851{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002852 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002853 return X86EMUL_CONTINUE;
2854}
2855
2856static int em_adc(struct x86_emulate_ctxt *ctxt)
2857{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002858 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002859 return X86EMUL_CONTINUE;
2860}
2861
2862static int em_sbb(struct x86_emulate_ctxt *ctxt)
2863{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002864 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002865 return X86EMUL_CONTINUE;
2866}
2867
2868static int em_and(struct x86_emulate_ctxt *ctxt)
2869{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002870 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002871 return X86EMUL_CONTINUE;
2872}
2873
2874static int em_sub(struct x86_emulate_ctxt *ctxt)
2875{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002876 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002877 return X86EMUL_CONTINUE;
2878}
2879
2880static int em_xor(struct x86_emulate_ctxt *ctxt)
2881{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002882 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002883 return X86EMUL_CONTINUE;
2884}
2885
2886static int em_cmp(struct x86_emulate_ctxt *ctxt)
2887{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002888 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002889 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002890 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002891 return X86EMUL_CONTINUE;
2892}
2893
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002894static int em_test(struct x86_emulate_ctxt *ctxt)
2895{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002896 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002897 /* Disable writeback. */
2898 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002899 return X86EMUL_CONTINUE;
2900}
2901
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002902static int em_xchg(struct x86_emulate_ctxt *ctxt)
2903{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002904 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002905 ctxt->src.val = ctxt->dst.val;
2906 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002907
2908 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002909 ctxt->dst.val = ctxt->src.orig_val;
2910 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002911 return X86EMUL_CONTINUE;
2912}
2913
Avi Kivity5c82aa22010-08-18 18:31:43 +03002914static int em_imul(struct x86_emulate_ctxt *ctxt)
2915{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002916 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002917 return X86EMUL_CONTINUE;
2918}
2919
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002920static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2921{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002922 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002923 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002924}
2925
Avi Kivity61429142010-08-19 15:13:00 +03002926static int em_cwd(struct x86_emulate_ctxt *ctxt)
2927{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002928 ctxt->dst.type = OP_REG;
2929 ctxt->dst.bytes = ctxt->src.bytes;
2930 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2931 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002932
2933 return X86EMUL_CONTINUE;
2934}
2935
Avi Kivity48bb5d32010-08-18 18:54:34 +03002936static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2937{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002938 u64 tsc = 0;
2939
Avi Kivity717746e2011-04-20 13:37:53 +03002940 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002941 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2942 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002943 return X86EMUL_CONTINUE;
2944}
2945
Avi Kivity222d21a2011-11-10 14:57:30 +02002946static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2947{
2948 u64 pmc;
2949
2950 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2951 return emulate_gp(ctxt, 0);
2952 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2953 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2954 return X86EMUL_CONTINUE;
2955}
2956
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002957static int em_mov(struct x86_emulate_ctxt *ctxt)
2958{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002959 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002960 return X86EMUL_CONTINUE;
2961}
2962
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002963static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2964{
2965 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2966 return emulate_gp(ctxt, 0);
2967
2968 /* Disable writeback. */
2969 ctxt->dst.type = OP_NONE;
2970 return X86EMUL_CONTINUE;
2971}
2972
2973static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2974{
2975 unsigned long val;
2976
2977 if (ctxt->mode == X86EMUL_MODE_PROT64)
2978 val = ctxt->src.val & ~0ULL;
2979 else
2980 val = ctxt->src.val & ~0U;
2981
2982 /* #UD condition is already handled. */
2983 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2984 return emulate_gp(ctxt, 0);
2985
2986 /* Disable writeback. */
2987 ctxt->dst.type = OP_NONE;
2988 return X86EMUL_CONTINUE;
2989}
2990
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002991static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2992{
2993 u64 msr_data;
2994
2995 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2996 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2997 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2998 return emulate_gp(ctxt, 0);
2999
3000 return X86EMUL_CONTINUE;
3001}
3002
3003static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3004{
3005 u64 msr_data;
3006
3007 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
3008 return emulate_gp(ctxt, 0);
3009
3010 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
3011 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
3012 return X86EMUL_CONTINUE;
3013}
3014
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003015static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3016{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003017 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003018 return emulate_ud(ctxt);
3019
Avi Kivity9dac77f2011-06-01 15:34:25 +03003020 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003021 return X86EMUL_CONTINUE;
3022}
3023
3024static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3025{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003026 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003027
Avi Kivity9dac77f2011-06-01 15:34:25 +03003028 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003029 return emulate_ud(ctxt);
3030
Avi Kivity9dac77f2011-06-01 15:34:25 +03003031 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003032 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3033
3034 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003035 ctxt->dst.type = OP_NONE;
3036 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003037}
3038
Avi Kivitya14e5792012-06-13 12:28:33 +03003039static int em_lldt(struct x86_emulate_ctxt *ctxt)
3040{
3041 u16 sel = ctxt->src.val;
3042
3043 /* Disable writeback. */
3044 ctxt->dst.type = OP_NONE;
3045 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3046}
3047
Avi Kivity80890002012-06-13 16:33:29 +03003048static int em_ltr(struct x86_emulate_ctxt *ctxt)
3049{
3050 u16 sel = ctxt->src.val;
3051
3052 /* Disable writeback. */
3053 ctxt->dst.type = OP_NONE;
3054 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3055}
3056
Avi Kivity38503912011-03-31 18:48:09 +02003057static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3058{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003059 int rc;
3060 ulong linear;
3061
Avi Kivity9dac77f2011-06-01 15:34:25 +03003062 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003063 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003064 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003065 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003066 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003067 return X86EMUL_CONTINUE;
3068}
3069
Avi Kivity2d04a052011-04-20 15:32:49 +03003070static int em_clts(struct x86_emulate_ctxt *ctxt)
3071{
3072 ulong cr0;
3073
3074 cr0 = ctxt->ops->get_cr(ctxt, 0);
3075 cr0 &= ~X86_CR0_TS;
3076 ctxt->ops->set_cr(ctxt, 0, cr0);
3077 return X86EMUL_CONTINUE;
3078}
3079
Avi Kivity26d05cc2011-04-21 12:07:59 +03003080static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3081{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003082 int rc;
3083
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003085 return X86EMUL_UNHANDLEABLE;
3086
3087 rc = ctxt->ops->fix_hypercall(ctxt);
3088 if (rc != X86EMUL_CONTINUE)
3089 return rc;
3090
3091 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003093 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003094 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003095 return X86EMUL_CONTINUE;
3096}
3097
Avi Kivity96051572012-06-10 17:21:18 +03003098static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3099 void (*get)(struct x86_emulate_ctxt *ctxt,
3100 struct desc_ptr *ptr))
3101{
3102 struct desc_ptr desc_ptr;
3103
3104 if (ctxt->mode == X86EMUL_MODE_PROT64)
3105 ctxt->op_bytes = 8;
3106 get(ctxt, &desc_ptr);
3107 if (ctxt->op_bytes == 2) {
3108 ctxt->op_bytes = 4;
3109 desc_ptr.address &= 0x00ffffff;
3110 }
3111 /* Disable writeback. */
3112 ctxt->dst.type = OP_NONE;
3113 return segmented_write(ctxt, ctxt->dst.addr.mem,
3114 &desc_ptr, 2 + ctxt->op_bytes);
3115}
3116
3117static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3118{
3119 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3120}
3121
3122static int em_sidt(struct x86_emulate_ctxt *ctxt)
3123{
3124 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3125}
3126
Avi Kivity26d05cc2011-04-21 12:07:59 +03003127static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3128{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003129 struct desc_ptr desc_ptr;
3130 int rc;
3131
Avi Kivity510425f2012-06-07 17:04:36 +03003132 if (ctxt->mode == X86EMUL_MODE_PROT64)
3133 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003134 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003135 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003136 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003137 if (rc != X86EMUL_CONTINUE)
3138 return rc;
3139 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3140 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003141 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003142 return X86EMUL_CONTINUE;
3143}
3144
Avi Kivity5ef39c72011-04-21 12:21:50 +03003145static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003146{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003147 int rc;
3148
Avi Kivity5ef39c72011-04-21 12:21:50 +03003149 rc = ctxt->ops->fix_hypercall(ctxt);
3150
Avi Kivity26d05cc2011-04-21 12:07:59 +03003151 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003152 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003153 return rc;
3154}
3155
3156static int em_lidt(struct x86_emulate_ctxt *ctxt)
3157{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003158 struct desc_ptr desc_ptr;
3159 int rc;
3160
Avi Kivity510425f2012-06-07 17:04:36 +03003161 if (ctxt->mode == X86EMUL_MODE_PROT64)
3162 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003163 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003164 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003165 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003166 if (rc != X86EMUL_CONTINUE)
3167 return rc;
3168 ctxt->ops->set_idt(ctxt, &desc_ptr);
3169 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003170 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003171 return X86EMUL_CONTINUE;
3172}
3173
3174static int em_smsw(struct x86_emulate_ctxt *ctxt)
3175{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003176 ctxt->dst.bytes = 2;
3177 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003178 return X86EMUL_CONTINUE;
3179}
3180
3181static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3182{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003183 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003184 | (ctxt->src.val & 0x0f));
3185 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003186 return X86EMUL_CONTINUE;
3187}
3188
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003189static int em_loop(struct x86_emulate_ctxt *ctxt)
3190{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003191 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
3192 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
3193 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3194 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003195
3196 return X86EMUL_CONTINUE;
3197}
3198
3199static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3200{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003201 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
3202 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003203
3204 return X86EMUL_CONTINUE;
3205}
3206
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003207static int em_in(struct x86_emulate_ctxt *ctxt)
3208{
3209 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3210 &ctxt->dst.val))
3211 return X86EMUL_IO_NEEDED;
3212
3213 return X86EMUL_CONTINUE;
3214}
3215
3216static int em_out(struct x86_emulate_ctxt *ctxt)
3217{
3218 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3219 &ctxt->src.val, 1);
3220 /* Disable writeback. */
3221 ctxt->dst.type = OP_NONE;
3222 return X86EMUL_CONTINUE;
3223}
3224
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003225static int em_cli(struct x86_emulate_ctxt *ctxt)
3226{
3227 if (emulator_bad_iopl(ctxt))
3228 return emulate_gp(ctxt, 0);
3229
3230 ctxt->eflags &= ~X86_EFLAGS_IF;
3231 return X86EMUL_CONTINUE;
3232}
3233
3234static int em_sti(struct x86_emulate_ctxt *ctxt)
3235{
3236 if (emulator_bad_iopl(ctxt))
3237 return emulate_gp(ctxt, 0);
3238
3239 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3240 ctxt->eflags |= X86_EFLAGS_IF;
3241 return X86EMUL_CONTINUE;
3242}
3243
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003244static int em_bt(struct x86_emulate_ctxt *ctxt)
3245{
3246 /* Disable writeback. */
3247 ctxt->dst.type = OP_NONE;
3248 /* only subword offset */
3249 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3250
3251 emulate_2op_SrcV_nobyte(ctxt, "bt");
3252 return X86EMUL_CONTINUE;
3253}
3254
3255static int em_bts(struct x86_emulate_ctxt *ctxt)
3256{
3257 emulate_2op_SrcV_nobyte(ctxt, "bts");
3258 return X86EMUL_CONTINUE;
3259}
3260
3261static int em_btr(struct x86_emulate_ctxt *ctxt)
3262{
3263 emulate_2op_SrcV_nobyte(ctxt, "btr");
3264 return X86EMUL_CONTINUE;
3265}
3266
3267static int em_btc(struct x86_emulate_ctxt *ctxt)
3268{
3269 emulate_2op_SrcV_nobyte(ctxt, "btc");
3270 return X86EMUL_CONTINUE;
3271}
3272
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003273static int em_bsf(struct x86_emulate_ctxt *ctxt)
3274{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003275 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003276 return X86EMUL_CONTINUE;
3277}
3278
3279static int em_bsr(struct x86_emulate_ctxt *ctxt)
3280{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003281 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003282 return X86EMUL_CONTINUE;
3283}
3284
Avi Kivity6d6eede2012-06-07 14:11:36 +03003285static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3286{
3287 u32 eax, ebx, ecx, edx;
3288
3289 eax = ctxt->regs[VCPU_REGS_RAX];
3290 ecx = ctxt->regs[VCPU_REGS_RCX];
3291 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3292 ctxt->regs[VCPU_REGS_RAX] = eax;
3293 ctxt->regs[VCPU_REGS_RBX] = ebx;
3294 ctxt->regs[VCPU_REGS_RCX] = ecx;
3295 ctxt->regs[VCPU_REGS_RDX] = edx;
3296 return X86EMUL_CONTINUE;
3297}
3298
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003299static int em_lahf(struct x86_emulate_ctxt *ctxt)
3300{
3301 ctxt->regs[VCPU_REGS_RAX] &= ~0xff00UL;
3302 ctxt->regs[VCPU_REGS_RAX] |= (ctxt->eflags & 0xff) << 8;
3303 return X86EMUL_CONTINUE;
3304}
3305
Avi Kivity92998362012-06-13 12:25:06 +03003306static int em_bswap(struct x86_emulate_ctxt *ctxt)
3307{
3308 switch (ctxt->op_bytes) {
3309#ifdef CONFIG_X86_64
3310 case 8:
3311 asm("bswap %0" : "+r"(ctxt->dst.val));
3312 break;
3313#endif
3314 default:
3315 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3316 break;
3317 }
3318 return X86EMUL_CONTINUE;
3319}
3320
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003321static bool valid_cr(int nr)
3322{
3323 switch (nr) {
3324 case 0:
3325 case 2 ... 4:
3326 case 8:
3327 return true;
3328 default:
3329 return false;
3330 }
3331}
3332
3333static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3334{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003335 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003336 return emulate_ud(ctxt);
3337
3338 return X86EMUL_CONTINUE;
3339}
3340
3341static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3342{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003343 u64 new_val = ctxt->src.val64;
3344 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003345 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003346
3347 static u64 cr_reserved_bits[] = {
3348 0xffffffff00000000ULL,
3349 0, 0, 0, /* CR3 checked later */
3350 CR4_RESERVED_BITS,
3351 0, 0, 0,
3352 CR8_RESERVED_BITS,
3353 };
3354
3355 if (!valid_cr(cr))
3356 return emulate_ud(ctxt);
3357
3358 if (new_val & cr_reserved_bits[cr])
3359 return emulate_gp(ctxt, 0);
3360
3361 switch (cr) {
3362 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003363 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003364 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3365 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3366 return emulate_gp(ctxt, 0);
3367
Avi Kivity717746e2011-04-20 13:37:53 +03003368 cr4 = ctxt->ops->get_cr(ctxt, 4);
3369 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003370
3371 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3372 !(cr4 & X86_CR4_PAE))
3373 return emulate_gp(ctxt, 0);
3374
3375 break;
3376 }
3377 case 3: {
3378 u64 rsvd = 0;
3379
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003380 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3381 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003382 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003383 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003384 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003385 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003386 rsvd = CR3_NONPAE_RESERVED_BITS;
3387
3388 if (new_val & rsvd)
3389 return emulate_gp(ctxt, 0);
3390
3391 break;
3392 }
3393 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003394 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003395
3396 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3397 return emulate_gp(ctxt, 0);
3398
3399 break;
3400 }
3401 }
3402
3403 return X86EMUL_CONTINUE;
3404}
3405
Joerg Roedel3b88e412011-04-04 12:39:29 +02003406static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3407{
3408 unsigned long dr7;
3409
Avi Kivity717746e2011-04-20 13:37:53 +03003410 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003411
3412 /* Check if DR7.Global_Enable is set */
3413 return dr7 & (1 << 13);
3414}
3415
3416static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3417{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003418 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003419 u64 cr4;
3420
3421 if (dr > 7)
3422 return emulate_ud(ctxt);
3423
Avi Kivity717746e2011-04-20 13:37:53 +03003424 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003425 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3426 return emulate_ud(ctxt);
3427
3428 if (check_dr7_gd(ctxt))
3429 return emulate_db(ctxt);
3430
3431 return X86EMUL_CONTINUE;
3432}
3433
3434static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3435{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 u64 new_val = ctxt->src.val64;
3437 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003438
3439 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3440 return emulate_gp(ctxt, 0);
3441
3442 return check_dr_read(ctxt);
3443}
3444
Joerg Roedel01de8b02011-04-04 12:39:31 +02003445static int check_svme(struct x86_emulate_ctxt *ctxt)
3446{
3447 u64 efer;
3448
Avi Kivity717746e2011-04-20 13:37:53 +03003449 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003450
3451 if (!(efer & EFER_SVME))
3452 return emulate_ud(ctxt);
3453
3454 return X86EMUL_CONTINUE;
3455}
3456
3457static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3458{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003459 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003460
3461 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003462 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003463 return emulate_gp(ctxt, 0);
3464
3465 return check_svme(ctxt);
3466}
3467
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003468static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3469{
Avi Kivity717746e2011-04-20 13:37:53 +03003470 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003471
Avi Kivity717746e2011-04-20 13:37:53 +03003472 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003473 return emulate_ud(ctxt);
3474
3475 return X86EMUL_CONTINUE;
3476}
3477
Joerg Roedel80612522011-04-04 12:39:33 +02003478static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3479{
Avi Kivity717746e2011-04-20 13:37:53 +03003480 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003481 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003482
Avi Kivity717746e2011-04-20 13:37:53 +03003483 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003484 (rcx > 3))
3485 return emulate_gp(ctxt, 0);
3486
3487 return X86EMUL_CONTINUE;
3488}
3489
Joerg Roedelf6511932011-04-04 12:39:35 +02003490static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3491{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003492 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3493 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003494 return emulate_gp(ctxt, 0);
3495
3496 return X86EMUL_CONTINUE;
3497}
3498
3499static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3500{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003501 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3502 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003503 return emulate_gp(ctxt, 0);
3504
3505 return X86EMUL_CONTINUE;
3506}
3507
Avi Kivity73fba5f2010-07-29 15:11:53 +03003508#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003509#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003510#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3511 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003512#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003513#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003514#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3515#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003516#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003517#define II(_f, _e, _i) \
3518 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003519#define IIP(_f, _e, _i, _p) \
3520 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3521 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003522#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003523
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003524#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003525#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003526#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003527#define I2bvIP(_f, _e, _i, _p) \
3528 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003529
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003530#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3531 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3532 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003533
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003534static struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003535 DI(SrcNone | Priv, monitor),
3536 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003537 N, N, N, N, N, N,
3538};
3539
Joerg Roedel01de8b02011-04-04 12:39:31 +02003540static struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003541 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3542 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3543 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3544 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3545 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3546 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3547 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3548 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003549};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003550
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003551static struct opcode group7_rm7[] = {
3552 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003553 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003554 N, N, N, N, N, N,
3555};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003556
Avi Kivity73fba5f2010-07-29 15:11:53 +03003557static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003558 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003559 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003560 I(Lock, em_adc),
3561 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003562 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003563 I(Lock, em_sub),
3564 I(Lock, em_xor),
3565 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003566};
3567
3568static struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003569 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003570};
3571
3572static struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003573 I(DstMem | SrcImm, em_test),
3574 I(DstMem | SrcImm, em_test),
3575 I(DstMem | SrcNone | Lock, em_not),
3576 I(DstMem | SrcNone | Lock, em_neg),
3577 I(SrcMem, em_mul_ex),
3578 I(SrcMem, em_imul_ex),
3579 I(SrcMem, em_div_ex),
3580 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003581};
3582
3583static struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003584 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3585 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003586 N, N, N, N, N, N,
3587};
3588
3589static struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003590 I(DstMem | SrcNone | Lock, em_grp45),
3591 I(DstMem | SrcNone | Lock, em_grp45),
3592 I(SrcMem | Stack, em_grp45),
3593 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3594 I(SrcMem | Stack, em_grp45),
3595 I(SrcMemFAddr | ImplicitOps, em_grp45),
3596 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003597};
3598
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003599static struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003600 DI(Prot, sldt),
3601 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003602 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003603 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003604 N, N, N, N,
3605};
3606
Avi Kivity73fba5f2010-07-29 15:11:53 +03003607static struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003608 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3609 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003610 II(SrcMem | Priv, em_lgdt, lgdt),
3611 II(SrcMem | Priv, em_lidt, lidt),
3612 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3613 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3614 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003615}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003616 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003617 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003618 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003619 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3620 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3621 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003622} };
3623
3624static struct opcode group8[] = {
3625 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003626 I(DstMem | SrcImmByte, em_bt),
3627 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3628 I(DstMem | SrcImmByte | Lock, em_btr),
3629 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003630};
3631
3632static struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003633 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003634}, {
3635 N, N, N, N, N, N, N, N,
3636} };
3637
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003638static struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003639 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003640 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003641};
3642
Avi Kivityaa97bb42010-01-20 18:09:23 +02003643static struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003644 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003645};
3646
Avi Kivity3e114eb2012-04-09 18:40:01 +03003647static struct gprefix pfx_vmovntpx = {
3648 I(0, em_mov), N, N, N,
3649};
3650
Avi Kivity73fba5f2010-07-29 15:11:53 +03003651static struct opcode opcode_table[256] = {
3652 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003653 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003654 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3655 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003656 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003657 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003658 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3659 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003660 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003661 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003662 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3663 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003664 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003665 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003666 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3667 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003668 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003669 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003670 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003671 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003672 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003673 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003674 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003675 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003676 /* 0x40 - 0x4F */
3677 X16(D(DstReg)),
3678 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003679 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003680 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003681 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003682 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003683 I(ImplicitOps | Stack | No64, em_pusha),
3684 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003685 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3686 N, N, N, N,
3687 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003688 I(SrcImm | Mov | Stack, em_push),
3689 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003690 I(SrcImmByte | Mov | Stack, em_push),
3691 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003692 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3693 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003694 /* 0x70 - 0x7F */
3695 X16(D(SrcImmByte)),
3696 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003697 G(ByteOp | DstMem | SrcImm, group1),
3698 G(DstMem | SrcImm, group1),
3699 G(ByteOp | DstMem | SrcImm | No64, group1),
3700 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003701 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003702 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003703 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003704 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003705 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003706 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003707 D(ModRM | SrcMem | NoAccess | DstReg),
3708 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3709 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003710 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003711 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003712 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003713 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003714 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003715 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003716 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003717 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003718 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003719 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003720 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003721 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003722 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003723 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003724 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3725 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003726 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003727 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003728 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003729 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003730 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003731 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003732 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003733 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003734 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003735 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3736 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003737 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003738 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003739 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3740 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003741 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003742 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003743 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003744 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003745 N, N, N, N,
3746 /* 0xD8 - 0xDF */
3747 N, N, N, N, N, N, N, N,
3748 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003749 X3(I(SrcImmByte, em_loop)),
3750 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003751 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3752 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003753 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003754 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003755 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003756 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3757 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003758 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003759 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003760 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3761 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003762 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003763 D(ImplicitOps), D(ImplicitOps),
3764 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003765 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3766};
3767
3768static struct opcode twobyte_table[256] = {
3769 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003770 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003771 N, I(ImplicitOps | VendorSpecific, em_syscall),
3772 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003773 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003774 N, D(ImplicitOps | ModRM), N, N,
3775 /* 0x10 - 0x1F */
3776 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3777 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003778 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003779 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003780 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3781 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003782 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003783 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3784 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003786 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003787 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003788 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003789 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003790 I(ImplicitOps | VendorSpecific, em_sysenter),
3791 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003792 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003793 N, N, N, N, N, N, N, N,
3794 /* 0x40 - 0x4F */
3795 X16(D(DstReg | SrcMem | ModRM | Mov)),
3796 /* 0x50 - 0x5F */
3797 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3798 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003799 N, N, N, N,
3800 N, N, N, N,
3801 N, N, N, N,
3802 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003803 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003804 N, N, N, N,
3805 N, N, N, N,
3806 N, N, N, N,
3807 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003808 /* 0x80 - 0x8F */
3809 X16(D(SrcImm)),
3810 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003811 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003812 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003813 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003814 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3816 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3817 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003818 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003819 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003820 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003821 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3822 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003823 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003824 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003825 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003826 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003827 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003828 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3829 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003830 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003831 /* 0xB8 - 0xBF */
3832 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003833 G(BitOp, group8),
3834 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003835 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003836 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003837 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003838 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003839 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003840 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003841 /* 0xC8 - 0xCF */
3842 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003843 /* 0xD0 - 0xDF */
3844 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3845 /* 0xE0 - 0xEF */
3846 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3847 /* 0xF0 - 0xFF */
3848 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3849};
3850
3851#undef D
3852#undef N
3853#undef G
3854#undef GD
3855#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003856#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003857#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003858
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003859#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003860#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003861#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003862#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003863#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003864
Avi Kivity9dac77f2011-06-01 15:34:25 +03003865static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003866{
3867 unsigned size;
3868
Avi Kivity9dac77f2011-06-01 15:34:25 +03003869 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003870 if (size == 8)
3871 size = 4;
3872 return size;
3873}
3874
3875static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3876 unsigned size, bool sign_extension)
3877{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003878 int rc = X86EMUL_CONTINUE;
3879
3880 op->type = OP_IMM;
3881 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003882 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003883 /* NB. Immediates are sign-extended as necessary. */
3884 switch (op->bytes) {
3885 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003886 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003887 break;
3888 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003889 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003890 break;
3891 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003892 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003893 break;
3894 }
3895 if (!sign_extension) {
3896 switch (op->bytes) {
3897 case 1:
3898 op->val &= 0xff;
3899 break;
3900 case 2:
3901 op->val &= 0xffff;
3902 break;
3903 case 4:
3904 op->val &= 0xffffffff;
3905 break;
3906 }
3907 }
3908done:
3909 return rc;
3910}
3911
Avi Kivitya9945542011-09-13 10:45:41 +03003912static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3913 unsigned d)
3914{
3915 int rc = X86EMUL_CONTINUE;
3916
3917 switch (d) {
3918 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003919 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03003920 break;
3921 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003922 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003923 break;
3924 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003925 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003926 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003927 *op = ctxt->memop;
3928 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003929 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003930 fetch_bit_operand(ctxt);
3931 op->orig_val = op->val;
3932 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003933 case OpMem64:
3934 ctxt->memop.bytes = 8;
3935 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003936 case OpAcc:
3937 op->type = OP_REG;
3938 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3939 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3940 fetch_register_operand(op);
3941 op->orig_val = op->val;
3942 break;
3943 case OpDI:
3944 op->type = OP_MEM;
3945 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3946 op->addr.mem.ea =
3947 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3948 op->addr.mem.seg = VCPU_SREG_ES;
3949 op->val = 0;
3950 break;
3951 case OpDX:
3952 op->type = OP_REG;
3953 op->bytes = 2;
3954 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3955 fetch_register_operand(op);
3956 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003957 case OpCL:
3958 op->bytes = 1;
3959 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3960 break;
3961 case OpImmByte:
3962 rc = decode_imm(ctxt, op, 1, true);
3963 break;
3964 case OpOne:
3965 op->bytes = 1;
3966 op->val = 1;
3967 break;
3968 case OpImm:
3969 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3970 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003971 case OpMem8:
3972 ctxt->memop.bytes = 1;
3973 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003974 case OpMem16:
3975 ctxt->memop.bytes = 2;
3976 goto mem_common;
3977 case OpMem32:
3978 ctxt->memop.bytes = 4;
3979 goto mem_common;
3980 case OpImmU16:
3981 rc = decode_imm(ctxt, op, 2, false);
3982 break;
3983 case OpImmU:
3984 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3985 break;
3986 case OpSI:
3987 op->type = OP_MEM;
3988 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3989 op->addr.mem.ea =
3990 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3991 op->addr.mem.seg = seg_override(ctxt);
3992 op->val = 0;
3993 break;
3994 case OpImmFAddr:
3995 op->type = OP_IMM;
3996 op->addr.mem.ea = ctxt->_eip;
3997 op->bytes = ctxt->op_bytes + 2;
3998 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3999 break;
4000 case OpMemFAddr:
4001 ctxt->memop.bytes = ctxt->op_bytes + 2;
4002 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004003 case OpES:
4004 op->val = VCPU_SREG_ES;
4005 break;
4006 case OpCS:
4007 op->val = VCPU_SREG_CS;
4008 break;
4009 case OpSS:
4010 op->val = VCPU_SREG_SS;
4011 break;
4012 case OpDS:
4013 op->val = VCPU_SREG_DS;
4014 break;
4015 case OpFS:
4016 op->val = VCPU_SREG_FS;
4017 break;
4018 case OpGS:
4019 op->val = VCPU_SREG_GS;
4020 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004021 case OpImplicit:
4022 /* Special instructions do their own operand decoding. */
4023 default:
4024 op->type = OP_NONE; /* Disable writeback. */
4025 break;
4026 }
4027
4028done:
4029 return rc;
4030}
4031
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004032int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004033{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004034 int rc = X86EMUL_CONTINUE;
4035 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004036 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004037 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004038 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004039
Avi Kivityf09ed832011-09-13 10:45:40 +03004040 ctxt->memop.type = OP_NONE;
4041 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004042 ctxt->_eip = ctxt->eip;
4043 ctxt->fetch.start = ctxt->_eip;
4044 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004045 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004046 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004047
4048 switch (mode) {
4049 case X86EMUL_MODE_REAL:
4050 case X86EMUL_MODE_VM86:
4051 case X86EMUL_MODE_PROT16:
4052 def_op_bytes = def_ad_bytes = 2;
4053 break;
4054 case X86EMUL_MODE_PROT32:
4055 def_op_bytes = def_ad_bytes = 4;
4056 break;
4057#ifdef CONFIG_X86_64
4058 case X86EMUL_MODE_PROT64:
4059 def_op_bytes = 4;
4060 def_ad_bytes = 8;
4061 break;
4062#endif
4063 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004064 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004065 }
4066
Avi Kivity9dac77f2011-06-01 15:34:25 +03004067 ctxt->op_bytes = def_op_bytes;
4068 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004069
4070 /* Legacy prefixes. */
4071 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004072 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004073 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004074 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004075 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004076 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004077 break;
4078 case 0x67: /* address-size override */
4079 if (mode == X86EMUL_MODE_PROT64)
4080 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004081 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004082 else
4083 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004084 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004085 break;
4086 case 0x26: /* ES override */
4087 case 0x2e: /* CS override */
4088 case 0x36: /* SS override */
4089 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004090 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004091 break;
4092 case 0x64: /* FS override */
4093 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004094 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004095 break;
4096 case 0x40 ... 0x4f: /* REX */
4097 if (mode != X86EMUL_MODE_PROT64)
4098 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004099 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004100 continue;
4101 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004103 break;
4104 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004105 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004106 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004107 break;
4108 default:
4109 goto done_prefixes;
4110 }
4111
4112 /* Any legacy prefix after a REX prefix nullifies its effect. */
4113
Avi Kivity9dac77f2011-06-01 15:34:25 +03004114 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004115 }
4116
4117done_prefixes:
4118
4119 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004120 if (ctxt->rex_prefix & 8)
4121 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004122
4123 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004124 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004125 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004126 if (ctxt->b == 0x0f) {
4127 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004128 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004129 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004130 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004131 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004132
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004133 if (ctxt->d & ModRM)
4134 ctxt->modrm = insn_fetch(u8, ctxt);
4135
Avi Kivity9dac77f2011-06-01 15:34:25 +03004136 while (ctxt->d & GroupMask) {
4137 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004138 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004139 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004140 opcode = opcode.u.group[goffset];
4141 break;
4142 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004143 goffset = (ctxt->modrm >> 3) & 7;
4144 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004145 opcode = opcode.u.gdual->mod3[goffset];
4146 else
4147 opcode = opcode.u.gdual->mod012[goffset];
4148 break;
4149 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004150 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004151 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004152 break;
4153 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004154 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004155 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004156 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004157 switch (simd_prefix) {
4158 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4159 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4160 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4161 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4162 }
4163 break;
4164 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004165 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004166 }
Avi Kivity46561642011-04-24 14:09:59 +03004167
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004168 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004169 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004170 }
4171
Avi Kivity9dac77f2011-06-01 15:34:25 +03004172 ctxt->execute = opcode.u.execute;
4173 ctxt->check_perm = opcode.check_perm;
4174 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004175
4176 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004177 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004178 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004179
Avi Kivity9dac77f2011-06-01 15:34:25 +03004180 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004181 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004182
Avi Kivity9dac77f2011-06-01 15:34:25 +03004183 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4184 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004185
Avi Kivity9dac77f2011-06-01 15:34:25 +03004186 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004187 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004188 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004189 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004190 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004191 }
4192
Avi Kivity9dac77f2011-06-01 15:34:25 +03004193 if (ctxt->d & Sse)
4194 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004195 else if (ctxt->d & Mmx)
4196 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004197
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004198 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004199 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004200 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004201 if (!ctxt->has_seg_override)
4202 set_seg_override(ctxt, ctxt->modrm_seg);
4203 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004204 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004205 if (rc != X86EMUL_CONTINUE)
4206 goto done;
4207
Avi Kivity9dac77f2011-06-01 15:34:25 +03004208 if (!ctxt->has_seg_override)
4209 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004210
Avi Kivityf09ed832011-09-13 10:45:40 +03004211 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004212
Avi Kivityf09ed832011-09-13 10:45:40 +03004213 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4214 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004215
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004216 /*
4217 * Decode and fetch the source operand: register, memory
4218 * or immediate.
4219 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004220 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004221 if (rc != X86EMUL_CONTINUE)
4222 goto done;
4223
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004224 /*
4225 * Decode and fetch the second source operand: register, memory
4226 * or immediate.
4227 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004228 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004229 if (rc != X86EMUL_CONTINUE)
4230 goto done;
4231
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004232 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004233 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004234
4235done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004236 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4237 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004238
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004239 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004240}
4241
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004242bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4243{
4244 return ctxt->d & PageTable;
4245}
4246
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004247static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4248{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004249 /* The second termination condition only applies for REPE
4250 * and REPNE. Test if the repeat string operation prefix is
4251 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4252 * corresponding termination condition according to:
4253 * - if REPE/REPZ and ZF = 0 then done
4254 * - if REPNE/REPNZ and ZF = 1 then done
4255 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004256 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4257 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4258 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004259 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004260 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004261 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4262 return true;
4263
4264 return false;
4265}
4266
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004267static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4268{
4269 bool fault = false;
4270
4271 ctxt->ops->get_fpu(ctxt);
4272 asm volatile("1: fwait \n\t"
4273 "2: \n\t"
4274 ".pushsection .fixup,\"ax\" \n\t"
4275 "3: \n\t"
4276 "movb $1, %[fault] \n\t"
4277 "jmp 2b \n\t"
4278 ".popsection \n\t"
4279 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004280 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004281 ctxt->ops->put_fpu(ctxt);
4282
4283 if (unlikely(fault))
4284 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4285
4286 return X86EMUL_CONTINUE;
4287}
4288
4289static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4290 struct operand *op)
4291{
4292 if (op->type == OP_MM)
4293 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4294}
4295
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004296int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004297{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004298 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004299 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004301
Avi Kivity9dac77f2011-06-01 15:34:25 +03004302 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004303
Avi Kivity9dac77f2011-06-01 15:34:25 +03004304 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004305 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004306 goto done;
4307 }
4308
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004309 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004310 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004311 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004312 goto done;
4313 }
4314
Avi Kivity9dac77f2011-06-01 15:34:25 +03004315 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004316 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004317 goto done;
4318 }
4319
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004320 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4321 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004322 rc = emulate_ud(ctxt);
4323 goto done;
4324 }
4325
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004326 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004327 rc = emulate_nm(ctxt);
4328 goto done;
4329 }
4330
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004331 if (ctxt->d & Mmx) {
4332 rc = flush_pending_x87_faults(ctxt);
4333 if (rc != X86EMUL_CONTINUE)
4334 goto done;
4335 /*
4336 * Now that we know the fpu is exception safe, we can fetch
4337 * operands from it.
4338 */
4339 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4340 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4341 if (!(ctxt->d & Mov))
4342 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4343 }
4344
Avi Kivity9dac77f2011-06-01 15:34:25 +03004345 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4346 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004347 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004348 if (rc != X86EMUL_CONTINUE)
4349 goto done;
4350 }
4351
Gleb Natapove92805a2010-02-10 14:21:35 +02004352 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004354 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004355 goto done;
4356 }
4357
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004358 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004360 rc = emulate_ud(ctxt);
4361 goto done;
4362 }
4363
Joerg Roedeld09beab2011-04-04 12:39:25 +02004364 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 if (ctxt->check_perm) {
4366 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004367 if (rc != X86EMUL_CONTINUE)
4368 goto done;
4369 }
4370
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4372 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004373 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004374 if (rc != X86EMUL_CONTINUE)
4375 goto done;
4376 }
4377
Avi Kivity9dac77f2011-06-01 15:34:25 +03004378 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004379 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004380 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4381 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004382 goto done;
4383 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004384 }
4385
Avi Kivity9dac77f2011-06-01 15:34:25 +03004386 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4387 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4388 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004389 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004390 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004391 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004392 }
4393
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 if (ctxt->src2.type == OP_MEM) {
4395 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4396 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004397 if (rc != X86EMUL_CONTINUE)
4398 goto done;
4399 }
4400
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004402 goto special_insn;
4403
4404
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004406 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004407 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4408 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004409 if (rc != X86EMUL_CONTINUE)
4410 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004411 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004413
Avi Kivity018a98d2007-11-27 19:30:56 +02004414special_insn:
4415
Avi Kivity9dac77f2011-06-01 15:34:25 +03004416 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4417 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004418 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004419 if (rc != X86EMUL_CONTINUE)
4420 goto done;
4421 }
4422
Avi Kivity9dac77f2011-06-01 15:34:25 +03004423 if (ctxt->execute) {
4424 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004425 if (rc != X86EMUL_CONTINUE)
4426 goto done;
4427 goto writeback;
4428 }
4429
Avi Kivity9dac77f2011-06-01 15:34:25 +03004430 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431 goto twobyte_insn;
4432
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004434 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004435 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004436 break;
4437 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004438 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004439 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004440 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004441 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004442 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004443 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004445 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004446 if (test_cc(ctxt->b, ctxt->eflags))
4447 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004448 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004449 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004451 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004452 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004453 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004454 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004455 rc = em_xchg(ctxt);
4456 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004457 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004458 switch (ctxt->op_bytes) {
4459 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4460 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4461 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004462 }
4463 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004464 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004465 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004466 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004467 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004468 rc = emulate_int(ctxt, 3);
4469 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004470 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004471 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004472 break;
4473 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004474 if (ctxt->eflags & EFLG_OF)
4475 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004476 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004477 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004478 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004479 break;
4480 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004481 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004482 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004483 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004484 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004485 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004486 jmp_rel(ctxt, ctxt->src.val);
4487 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004488 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004489 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004490 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004491 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004492 case 0xf5: /* cmc */
4493 /* complement carry flag from eflags reg */
4494 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004495 break;
4496 case 0xf8: /* clc */
4497 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004498 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004499 case 0xf9: /* stc */
4500 ctxt->eflags |= EFLG_CF;
4501 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004502 case 0xfc: /* cld */
4503 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004504 break;
4505 case 0xfd: /* std */
4506 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004507 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004508 default:
4509 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004510 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004511
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004512 if (rc != X86EMUL_CONTINUE)
4513 goto done;
4514
Avi Kivity018a98d2007-11-27 19:30:56 +02004515writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004516 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004517 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004518 goto done;
4519
Gleb Natapov5cd21912010-03-18 15:20:26 +02004520 /*
4521 * restore dst type in case the decoding will be reused
4522 * (happens for string instruction )
4523 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004524 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004525
Avi Kivity9dac77f2011-06-01 15:34:25 +03004526 if ((ctxt->d & SrcMask) == SrcSI)
4527 string_addr_inc(ctxt, seg_override(ctxt),
4528 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004529
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004531 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004532 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004533
Avi Kivity9dac77f2011-06-01 15:34:25 +03004534 if (ctxt->rep_prefix && (ctxt->d & String)) {
4535 struct read_cache *r = &ctxt->io_read;
4536 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004537
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004538 if (!string_insn_completed(ctxt)) {
4539 /*
4540 * Re-enter guest when pio read ahead buffer is empty
4541 * or, if it is not used, after each 1024 iteration.
4542 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004543 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004544 (r->end == 0 || r->end != r->pos)) {
4545 /*
4546 * Reset read cache. Usually happens before
4547 * decode, but since instruction is restarted
4548 * we have to do it here.
4549 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004550 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004551 return EMULATION_RESTART;
4552 }
4553 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004554 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004555 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004556
Avi Kivity9dac77f2011-06-01 15:34:25 +03004557 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004558
4559done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004560 if (rc == X86EMUL_PROPAGATE_FAULT)
4561 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004562 if (rc == X86EMUL_INTERCEPTED)
4563 return EMULATION_INTERCEPTED;
4564
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004565 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566
4567twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004568 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004569 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004570 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004571 break;
4572 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004573 case 0x0d: /* GrpP (prefetch) */
4574 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004575 break;
4576 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004577 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004578 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004579 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004580 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004582 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004583 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4584 if (!test_cc(ctxt->b, ctxt->eflags))
4585 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004587 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004588 if (test_cc(ctxt->b, ctxt->eflags))
4589 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004590 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004591 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004592 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004593 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004594 case 0xa4: /* shld imm8, r, r/m */
4595 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004596 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004597 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004598 case 0xac: /* shrd imm8, r, r/m */
4599 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004600 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004601 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004602 case 0xae: /* clflush */
4603 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004604 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004605 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004606 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004607 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004608 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004609 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004610 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004611 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004612 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004614 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004615 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004616 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004617 ctxt->src.val = ctxt->dst.orig_val;
4618 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004619 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004620 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004621 ctxt->dst.bytes = ctxt->op_bytes;
4622 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4623 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004624 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004625 default:
4626 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004627 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004628
4629 if (rc != X86EMUL_CONTINUE)
4630 goto done;
4631
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632 goto writeback;
4633
4634cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004635 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004636}