blob: 2c5d1e65d9d108d1b0bf39a63d877326e5ce3140 [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
Harvey Harrison7a9572752008-02-19 07:40:41 -0800478static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300479register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800480{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300481 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800482 *reg += inc;
483 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300484 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800485}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486
Avi Kivity9dac77f2011-06-01 15:34:25 +0300487static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800488{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300489 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800490}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300491
Avi Kivity56697682011-04-03 14:08:51 +0300492static u32 desc_limit_scaled(struct desc_struct *desc)
493{
494 u32 limit = get_desc_limit(desc);
495
496 return desc->g ? (limit << 12) | 0xfff : limit;
497}
498
Avi Kivity9dac77f2011-06-01 15:34:25 +0300499static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300500{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300501 ctxt->has_seg_override = true;
502 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300503}
504
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900505static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300506{
507 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
508 return 0;
509
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900510 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300511}
512
Avi Kivity9dac77f2011-06-01 15:34:25 +0300513static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300514{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300515 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300516 return 0;
517
Avi Kivity9dac77f2011-06-01 15:34:25 +0300518 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300519}
520
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200521static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
522 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300523{
Avi Kivityda9cb572010-11-22 17:53:21 +0200524 ctxt->exception.vector = vec;
525 ctxt->exception.error_code = error;
526 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200527 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300528}
529
Joerg Roedel3b88e412011-04-04 12:39:29 +0200530static int emulate_db(struct x86_emulate_ctxt *ctxt)
531{
532 return emulate_exception(ctxt, DB_VECTOR, 0, false);
533}
534
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200535static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300536{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200537 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300538}
539
Avi Kivity618ff152011-04-03 12:32:09 +0300540static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
541{
542 return emulate_exception(ctxt, SS_VECTOR, err, true);
543}
544
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200545static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300546{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300548}
549
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200550static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300551{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200552 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300553}
554
Avi Kivity34d1f492010-08-26 11:59:01 +0300555static int emulate_de(struct x86_emulate_ctxt *ctxt)
556{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200557 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300558}
559
Avi Kivity12537912011-03-29 11:41:27 +0200560static int emulate_nm(struct x86_emulate_ctxt *ctxt)
561{
562 return emulate_exception(ctxt, NM_VECTOR, 0, false);
563}
564
Avi Kivity1aa36612011-04-27 13:20:30 +0300565static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
566{
567 u16 selector;
568 struct desc_struct desc;
569
570 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
571 return selector;
572}
573
574static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
575 unsigned seg)
576{
577 u16 dummy;
578 u32 base3;
579 struct desc_struct desc;
580
581 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
582 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
583}
584
Avi Kivity1c11b372012-04-09 18:39:59 +0300585/*
586 * x86 defines three classes of vector instructions: explicitly
587 * aligned, explicitly unaligned, and the rest, which change behaviour
588 * depending on whether they're AVX encoded or not.
589 *
590 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
591 * subject to the same check.
592 */
593static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
594{
595 if (likely(size < 16))
596 return false;
597
598 if (ctxt->d & Aligned)
599 return true;
600 else if (ctxt->d & Unaligned)
601 return false;
602 else if (ctxt->d & Avx)
603 return false;
604 else
605 return true;
606}
607
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400608static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300609 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400610 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300611 ulong *linear)
612{
Avi Kivity618ff152011-04-03 12:32:09 +0300613 struct desc_struct desc;
614 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300615 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300616 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300617 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300618 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300619
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900620 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300621 switch (ctxt->mode) {
622 case X86EMUL_MODE_REAL:
623 break;
624 case X86EMUL_MODE_PROT64:
625 if (((signed long)la << 16) >> 16 != la)
626 return emulate_gp(ctxt, 0);
627 break;
628 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300629 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
630 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300631 if (!usable)
632 goto bad;
633 /* code segment or read-only data segment */
634 if (((desc.type & 8) || !(desc.type & 2)) && write)
635 goto bad;
636 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400637 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300638 goto bad;
639 lim = desc_limit_scaled(&desc);
640 if ((desc.type & 8) || !(desc.type & 4)) {
641 /* expand-up segment */
642 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
643 goto bad;
644 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800645 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300646 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
647 goto bad;
648 lim = desc.d ? 0xffffffff : 0xffff;
649 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
650 goto bad;
651 }
Avi Kivity717746e2011-04-20 13:37:53 +0300652 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300653 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300654 cpl = max(cpl, rpl);
655 if (!(desc.type & 8)) {
656 /* data segment */
657 if (cpl > desc.dpl)
658 goto bad;
659 } else if ((desc.type & 8) && !(desc.type & 4)) {
660 /* nonconforming code segment */
661 if (cpl != desc.dpl)
662 goto bad;
663 } else if ((desc.type & 8) && (desc.type & 4)) {
664 /* conforming code segment */
665 if (cpl < desc.dpl)
666 goto bad;
667 }
668 break;
669 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300670 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300671 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300672 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
673 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300674 *linear = la;
675 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300676bad:
677 if (addr.seg == VCPU_SREG_SS)
678 return emulate_ss(ctxt, addr.seg);
679 else
680 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300681}
682
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400683static int linearize(struct x86_emulate_ctxt *ctxt,
684 struct segmented_address addr,
685 unsigned size, bool write,
686 ulong *linear)
687{
688 return __linearize(ctxt, addr, size, write, false, linear);
689}
690
691
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200692static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
693 struct segmented_address addr,
694 void *data,
695 unsigned size)
696{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200697 int rc;
698 ulong linear;
699
Avi Kivity83b87952011-04-03 11:31:19 +0300700 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200701 if (rc != X86EMUL_CONTINUE)
702 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300703 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200704}
705
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900706/*
707 * Fetch the next byte of the instruction being emulated which is pointed to
708 * by ctxt->_eip, then increment ctxt->_eip.
709 *
710 * Also prefetch the remaining bytes of the instruction without crossing page
711 * boundary if they are not in fetch_cache yet.
712 */
713static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200714{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300715 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200716 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300717 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200718
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900719 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400720 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900721 struct segmented_address addr = { .seg = VCPU_SREG_CS,
722 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300723 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900724 size = min(15UL - cur_size,
725 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400726 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900727 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400728 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900729 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
730 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900731 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200732 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300733 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200734 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900735 *dest = fc->data[ctxt->_eip - fc->start];
736 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900737 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200738}
739
740static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900741 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200742{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900743 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200744
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200745 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900746 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200747 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200748 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900749 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900750 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200751 return rc;
752 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900753 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200754}
755
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900756/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900757#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900758({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900759 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900760 if (rc != X86EMUL_CONTINUE) \
761 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900762 (_type)_x; \
763})
764
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900765#define insn_fetch_arr(_arr, _size, _ctxt) \
766({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900767 if (rc != X86EMUL_CONTINUE) \
768 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900769})
770
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000771/*
772 * Given the 'reg' portion of a ModRM byte, and a register block, return a
773 * pointer into the block that addresses the relevant register.
774 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
775 */
776static void *decode_register(u8 modrm_reg, unsigned long *regs,
777 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778{
779 void *p;
780
781 p = &regs[modrm_reg];
782 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
783 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
784 return p;
785}
786
787static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200788 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 u16 *size, unsigned long *address, int op_bytes)
790{
791 int rc;
792
793 if (op_bytes == 2)
794 op_bytes = 3;
795 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200796 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900797 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200799 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200800 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 return rc;
802}
803
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300804static int test_cc(unsigned int condition, unsigned int flags)
805{
806 int rc = 0;
807
808 switch ((condition & 15) >> 1) {
809 case 0: /* o */
810 rc |= (flags & EFLG_OF);
811 break;
812 case 1: /* b/c/nae */
813 rc |= (flags & EFLG_CF);
814 break;
815 case 2: /* z/e */
816 rc |= (flags & EFLG_ZF);
817 break;
818 case 3: /* be/na */
819 rc |= (flags & (EFLG_CF|EFLG_ZF));
820 break;
821 case 4: /* s */
822 rc |= (flags & EFLG_SF);
823 break;
824 case 5: /* p/pe */
825 rc |= (flags & EFLG_PF);
826 break;
827 case 7: /* le/ng */
828 rc |= (flags & EFLG_ZF);
829 /* fall through */
830 case 6: /* l/nge */
831 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
832 break;
833 }
834
835 /* Odd condition identifiers (lsb == 1) have inverted sense. */
836 return (!!rc ^ (condition & 1));
837}
838
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300839static void fetch_register_operand(struct operand *op)
840{
841 switch (op->bytes) {
842 case 1:
843 op->val = *(u8 *)op->addr.reg;
844 break;
845 case 2:
846 op->val = *(u16 *)op->addr.reg;
847 break;
848 case 4:
849 op->val = *(u32 *)op->addr.reg;
850 break;
851 case 8:
852 op->val = *(u64 *)op->addr.reg;
853 break;
854 }
855}
856
Avi Kivity12537912011-03-29 11:41:27 +0200857static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
858{
859 ctxt->ops->get_fpu(ctxt);
860 switch (reg) {
861 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
862 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
863 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
864 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
865 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
866 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
867 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
868 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
869#ifdef CONFIG_X86_64
870 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
871 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
872 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
873 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
874 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
875 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
876 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
877 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
878#endif
879 default: BUG();
880 }
881 ctxt->ops->put_fpu(ctxt);
882}
883
884static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
885 int reg)
886{
887 ctxt->ops->get_fpu(ctxt);
888 switch (reg) {
889 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
890 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
891 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
892 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
893 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
894 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
895 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
896 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
897#ifdef CONFIG_X86_64
898 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
899 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
900 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
901 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
902 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
903 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
904 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
905 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
906#endif
907 default: BUG();
908 }
909 ctxt->ops->put_fpu(ctxt);
910}
911
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300912static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
913{
914 ctxt->ops->get_fpu(ctxt);
915 switch (reg) {
916 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
917 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
918 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
919 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
920 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
921 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
922 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
923 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
924 default: BUG();
925 }
926 ctxt->ops->put_fpu(ctxt);
927}
928
929static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
930{
931 ctxt->ops->get_fpu(ctxt);
932 switch (reg) {
933 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
934 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
935 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
936 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
937 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
938 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
939 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
940 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
941 default: BUG();
942 }
943 ctxt->ops->put_fpu(ctxt);
944}
945
Avi Kivity12537912011-03-29 11:41:27 +0200946static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200947 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200948{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300949 unsigned reg = ctxt->modrm_reg;
950 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200951
Avi Kivity9dac77f2011-06-01 15:34:25 +0300952 if (!(ctxt->d & ModRM))
953 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200954
Avi Kivity9dac77f2011-06-01 15:34:25 +0300955 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200956 op->type = OP_XMM;
957 op->bytes = 16;
958 op->addr.xmm = reg;
959 read_sse_reg(ctxt, &op->vec_val, reg);
960 return;
961 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300962 if (ctxt->d & Mmx) {
963 reg &= 7;
964 op->type = OP_MM;
965 op->bytes = 8;
966 op->addr.mm = reg;
967 return;
968 }
Avi Kivity12537912011-03-29 11:41:27 +0200969
Avi Kivity3c118e22007-10-31 10:27:04 +0200970 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200971 if (ctxt->d & ByteOp) {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300972 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200973 op->bytes = 1;
974 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300975 op->addr.reg = decode_register(reg, ctxt->regs, 0);
976 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200977 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300978 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200979 op->orig_val = op->val;
980}
981
Avi Kivitya6e34072012-06-10 17:15:39 +0300982static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
983{
984 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
985 ctxt->modrm_seg = VCPU_SREG_SS;
986}
987
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200988static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300989 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200990{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200991 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700992 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900993 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300994 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200995
Avi Kivity9dac77f2011-06-01 15:34:25 +0300996 if (ctxt->rex_prefix) {
997 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
998 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
999 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001000 }
1001
Avi Kivity9dac77f2011-06-01 15:34:25 +03001002 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1003 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1004 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1005 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006
Avi Kivity9dac77f2011-06-01 15:34:25 +03001007 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001008 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001009 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
1010 op->addr.reg = decode_register(ctxt->modrm_rm,
1011 ctxt->regs, ctxt->d & ByteOp);
1012 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001013 op->type = OP_XMM;
1014 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001015 op->addr.xmm = ctxt->modrm_rm;
1016 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001017 return rc;
1018 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001019 if (ctxt->d & Mmx) {
1020 op->type = OP_MM;
1021 op->bytes = 8;
1022 op->addr.xmm = ctxt->modrm_rm & 7;
1023 return rc;
1024 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001025 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001026 return rc;
1027 }
1028
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001029 op->type = OP_MEM;
1030
Avi Kivity9dac77f2011-06-01 15:34:25 +03001031 if (ctxt->ad_bytes == 2) {
1032 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
1033 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
1034 unsigned si = ctxt->regs[VCPU_REGS_RSI];
1035 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001036
1037 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001038 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001039 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001040 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001041 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001042 break;
1043 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001044 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045 break;
1046 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001047 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001048 break;
1049 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001050 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001051 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001052 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001053 break;
1054 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001055 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001056 break;
1057 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001058 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001059 break;
1060 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001061 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001062 break;
1063 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001064 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001065 break;
1066 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001067 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001068 break;
1069 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001070 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001071 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001072 break;
1073 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001074 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001075 break;
1076 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001077 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1078 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1079 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001080 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001081 } else {
1082 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001083 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001084 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001085 index_reg |= (sib >> 3) & 7;
1086 base_reg |= sib & 7;
1087 scale = sib >> 6;
1088
Avi Kivity9dac77f2011-06-01 15:34:25 +03001089 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001090 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001091 else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001092 modrm_ea += ctxt->regs[base_reg];
Avi Kivitya6e34072012-06-10 17:15:39 +03001093 adjust_modrm_seg(ctxt, base_reg);
1094 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001095 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001096 modrm_ea += ctxt->regs[index_reg] << scale;
1097 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001098 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001099 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001100 } else {
1101 base_reg = ctxt->modrm_rm;
1102 modrm_ea += ctxt->regs[base_reg];
1103 adjust_modrm_seg(ctxt, base_reg);
1104 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001105 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001106 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001107 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001108 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001109 break;
1110 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001111 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001112 break;
1113 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001114 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001115 break;
1116 }
1117 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001118 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001119done:
1120 return rc;
1121}
1122
1123static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001124 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001126 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001127
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001128 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001131 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001132 break;
1133 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001134 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001135 break;
1136 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001137 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138 break;
1139 }
1140done:
1141 return rc;
1142}
1143
Avi Kivity9dac77f2011-06-01 15:34:25 +03001144static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001145{
Sheng Yang7129eec2010-09-28 16:33:32 +08001146 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001147
Avi Kivity9dac77f2011-06-01 15:34:25 +03001148 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1149 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001150
Avi Kivity9dac77f2011-06-01 15:34:25 +03001151 if (ctxt->src.bytes == 2)
1152 sv = (s16)ctxt->src.val & (s16)mask;
1153 else if (ctxt->src.bytes == 4)
1154 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001155
Avi Kivity9dac77f2011-06-01 15:34:25 +03001156 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001157 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001158
1159 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001160 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001161}
1162
Gleb Natapov9de41572010-04-28 19:15:22 +03001163static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001164 unsigned long addr, void *dest, unsigned size)
1165{
1166 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001167 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001168
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001169 if (mc->pos < mc->end)
1170 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001171
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001172 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001173
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001174 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1175 &ctxt->exception);
1176 if (rc != X86EMUL_CONTINUE)
1177 return rc;
1178
1179 mc->end += size;
1180
1181read_cached:
1182 memcpy(dest, mc->data + mc->pos, size);
1183 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001184 return X86EMUL_CONTINUE;
1185}
1186
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001187static int segmented_read(struct x86_emulate_ctxt *ctxt,
1188 struct segmented_address addr,
1189 void *data,
1190 unsigned size)
1191{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001192 int rc;
1193 ulong linear;
1194
Avi Kivity83b87952011-04-03 11:31:19 +03001195 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001196 if (rc != X86EMUL_CONTINUE)
1197 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001198 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001199}
1200
1201static int segmented_write(struct x86_emulate_ctxt *ctxt,
1202 struct segmented_address addr,
1203 const void *data,
1204 unsigned size)
1205{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001206 int rc;
1207 ulong linear;
1208
Avi Kivity83b87952011-04-03 11:31:19 +03001209 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001210 if (rc != X86EMUL_CONTINUE)
1211 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001212 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1213 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001214}
1215
1216static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1217 struct segmented_address addr,
1218 const void *orig_data, const void *data,
1219 unsigned size)
1220{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001221 int rc;
1222 ulong linear;
1223
Avi Kivity83b87952011-04-03 11:31:19 +03001224 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001225 if (rc != X86EMUL_CONTINUE)
1226 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001227 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1228 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001229}
1230
Gleb Natapov7b262e92010-03-18 15:20:27 +02001231static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001232 unsigned int size, unsigned short port,
1233 void *dest)
1234{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001235 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001236
1237 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001238 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001239 unsigned int count = ctxt->rep_prefix ?
1240 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001241 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001242 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1243 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001244 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1245 count);
1246 if (n == 0)
1247 n = 1;
1248 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001249 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001250 return 0;
1251 rc->end = n * size;
1252 }
1253
1254 memcpy(dest, rc->data + rc->pos, size);
1255 rc->pos += size;
1256 return 1;
1257}
1258
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001259static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1260 u16 index, struct desc_struct *desc)
1261{
1262 struct desc_ptr dt;
1263 ulong addr;
1264
1265 ctxt->ops->get_idt(ctxt, &dt);
1266
1267 if (dt.size < index * 8 + 7)
1268 return emulate_gp(ctxt, index << 3 | 0x2);
1269
1270 addr = dt.address + index * 8;
1271 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1272 &ctxt->exception);
1273}
1274
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001275static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001276 u16 selector, struct desc_ptr *dt)
1277{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001278 struct x86_emulate_ops *ops = ctxt->ops;
1279
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001280 if (selector & 1 << 2) {
1281 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001282 u16 sel;
1283
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001284 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001285 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001286 return;
1287
1288 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1289 dt->address = get_desc_base(&desc);
1290 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001291 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001292}
1293
1294/* allowed just for 8 bytes segments */
1295static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001296 u16 selector, struct desc_struct *desc,
1297 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001298{
1299 struct desc_ptr dt;
1300 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001301 ulong addr;
1302
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001303 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001304
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001305 if (dt.size < index * 8 + 7)
1306 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001307
Avi Kivitye9194642012-06-13 16:29:39 +03001308 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001309 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1310 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001311}
1312
1313/* allowed just for 8 bytes segments */
1314static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001315 u16 selector, struct desc_struct *desc)
1316{
1317 struct desc_ptr dt;
1318 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001319 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001320
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001321 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001322
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001323 if (dt.size < index * 8 + 7)
1324 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001325
1326 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001327 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1328 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001329}
1330
Gleb Natapov5601d052011-03-07 14:55:06 +02001331/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001332static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001333 u16 selector, int seg)
1334{
Avi Kivity869be992012-06-13 16:30:53 +03001335 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001336 u8 dpl, rpl, cpl;
1337 unsigned err_vec = GP_VECTOR;
1338 u32 err_code = 0;
1339 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001340 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001341 int ret;
1342
1343 memset(&seg_desc, 0, sizeof seg_desc);
1344
1345 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1346 || ctxt->mode == X86EMUL_MODE_REAL) {
1347 /* set real mode segment descriptor */
1348 set_desc_base(&seg_desc, selector << 4);
1349 set_desc_limit(&seg_desc, 0xffff);
1350 seg_desc.type = 3;
1351 seg_desc.p = 1;
1352 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001353 if (ctxt->mode == X86EMUL_MODE_VM86)
1354 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001355 goto load;
1356 }
1357
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001358 rpl = selector & 3;
1359 cpl = ctxt->ops->cpl(ctxt);
1360
1361 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1362 if ((seg == VCPU_SREG_CS
1363 || (seg == VCPU_SREG_SS
1364 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1365 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001366 && null_selector)
1367 goto exception;
1368
1369 /* TR should be in GDT only */
1370 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1371 goto exception;
1372
1373 if (null_selector) /* for NULL selector skip all following checks */
1374 goto load;
1375
Avi Kivitye9194642012-06-13 16:29:39 +03001376 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001377 if (ret != X86EMUL_CONTINUE)
1378 return ret;
1379
1380 err_code = selector & 0xfffc;
1381 err_vec = GP_VECTOR;
1382
Guo Chaofc058682012-06-28 15:19:51 +08001383 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001384 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1385 goto exception;
1386
1387 if (!seg_desc.p) {
1388 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1389 goto exception;
1390 }
1391
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001392 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393
1394 switch (seg) {
1395 case VCPU_SREG_SS:
1396 /*
1397 * segment is not a writable data segment or segment
1398 * selector's RPL != CPL or segment selector's RPL != CPL
1399 */
1400 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1401 goto exception;
1402 break;
1403 case VCPU_SREG_CS:
1404 if (!(seg_desc.type & 8))
1405 goto exception;
1406
1407 if (seg_desc.type & 4) {
1408 /* conforming */
1409 if (dpl > cpl)
1410 goto exception;
1411 } else {
1412 /* nonconforming */
1413 if (rpl > cpl || dpl != cpl)
1414 goto exception;
1415 }
1416 /* CS(RPL) <- CPL */
1417 selector = (selector & 0xfffc) | cpl;
1418 break;
1419 case VCPU_SREG_TR:
1420 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1421 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001422 old_desc = seg_desc;
1423 seg_desc.type |= 2; /* busy */
1424 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1425 sizeof(seg_desc), &ctxt->exception);
1426 if (ret != X86EMUL_CONTINUE)
1427 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001428 break;
1429 case VCPU_SREG_LDTR:
1430 if (seg_desc.s || seg_desc.type != 2)
1431 goto exception;
1432 break;
1433 default: /* DS, ES, FS, or GS */
1434 /*
1435 * segment is not a data or readable code segment or
1436 * ((segment is a data or nonconforming code segment)
1437 * and (both RPL and CPL > DPL))
1438 */
1439 if ((seg_desc.type & 0xa) == 0x8 ||
1440 (((seg_desc.type & 0xc) != 0xc) &&
1441 (rpl > dpl && cpl > dpl)))
1442 goto exception;
1443 break;
1444 }
1445
1446 if (seg_desc.s) {
1447 /* mark segment as accessed */
1448 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001449 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001450 if (ret != X86EMUL_CONTINUE)
1451 return ret;
1452 }
1453load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001454 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455 return X86EMUL_CONTINUE;
1456exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001457 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458 return X86EMUL_PROPAGATE_FAULT;
1459}
1460
Wei Yongjun31be40b2010-08-17 09:17:30 +08001461static void write_register_operand(struct operand *op)
1462{
1463 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1464 switch (op->bytes) {
1465 case 1:
1466 *(u8 *)op->addr.reg = (u8)op->val;
1467 break;
1468 case 2:
1469 *(u16 *)op->addr.reg = (u16)op->val;
1470 break;
1471 case 4:
1472 *op->addr.reg = (u32)op->val;
1473 break; /* 64b: zero-extend */
1474 case 8:
1475 *op->addr.reg = op->val;
1476 break;
1477 }
1478}
1479
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001480static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001481{
1482 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001483
Avi Kivity9dac77f2011-06-01 15:34:25 +03001484 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001485 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001486 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001487 break;
1488 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001489 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001490 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001491 ctxt->dst.addr.mem,
1492 &ctxt->dst.orig_val,
1493 &ctxt->dst.val,
1494 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001495 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001496 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001497 ctxt->dst.addr.mem,
1498 &ctxt->dst.val,
1499 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001500 if (rc != X86EMUL_CONTINUE)
1501 return rc;
1502 break;
Avi Kivity12537912011-03-29 11:41:27 +02001503 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001504 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001505 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001506 case OP_MM:
1507 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1508 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001509 case OP_NONE:
1510 /* no writeback */
1511 break;
1512 default:
1513 break;
1514 }
1515 return X86EMUL_CONTINUE;
1516}
1517
Avi Kivity51ddff52012-06-12 20:19:40 +03001518static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001519{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001520 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001521
Avi Kivity51ddff52012-06-12 20:19:40 +03001522 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -bytes);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001523 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001524 addr.seg = VCPU_SREG_SS;
1525
Avi Kivity51ddff52012-06-12 20:19:40 +03001526 return segmented_write(ctxt, addr, data, bytes);
1527}
1528
1529static int em_push(struct x86_emulate_ctxt *ctxt)
1530{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001531 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001532 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001533 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001534}
1535
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001536static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001537 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001538{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001539 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001540 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001541
Avi Kivity9dac77f2011-06-01 15:34:25 +03001542 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001543 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001544 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001545 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001546 return rc;
1547
Avi Kivity9dac77f2011-06-01 15:34:25 +03001548 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001549 return rc;
1550}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001551
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001552static int em_pop(struct x86_emulate_ctxt *ctxt)
1553{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001554 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001555}
1556
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001557static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001558 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001559{
1560 int rc;
1561 unsigned long val, change_mask;
1562 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001563 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001564
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001565 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001566 if (rc != X86EMUL_CONTINUE)
1567 return rc;
1568
1569 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1570 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1571
1572 switch(ctxt->mode) {
1573 case X86EMUL_MODE_PROT64:
1574 case X86EMUL_MODE_PROT32:
1575 case X86EMUL_MODE_PROT16:
1576 if (cpl == 0)
1577 change_mask |= EFLG_IOPL;
1578 if (cpl <= iopl)
1579 change_mask |= EFLG_IF;
1580 break;
1581 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001582 if (iopl < 3)
1583 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001584 change_mask |= EFLG_IF;
1585 break;
1586 default: /* real mode */
1587 change_mask |= (EFLG_IOPL | EFLG_IF);
1588 break;
1589 }
1590
1591 *(unsigned long *)dest =
1592 (ctxt->eflags & ~change_mask) | (val & change_mask);
1593
1594 return rc;
1595}
1596
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001597static int em_popf(struct x86_emulate_ctxt *ctxt)
1598{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001599 ctxt->dst.type = OP_REG;
1600 ctxt->dst.addr.reg = &ctxt->eflags;
1601 ctxt->dst.bytes = ctxt->op_bytes;
1602 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001603}
1604
Avi Kivity612e89f2012-06-12 20:03:23 +03001605static int em_enter(struct x86_emulate_ctxt *ctxt)
1606{
1607 int rc;
1608 unsigned frame_size = ctxt->src.val;
1609 unsigned nesting_level = ctxt->src2.val & 31;
1610
1611 if (nesting_level)
1612 return X86EMUL_UNHANDLEABLE;
1613
1614 rc = push(ctxt, &ctxt->regs[VCPU_REGS_RBP], stack_size(ctxt));
1615 if (rc != X86EMUL_CONTINUE)
1616 return rc;
1617 assign_masked(&ctxt->regs[VCPU_REGS_RBP], ctxt->regs[VCPU_REGS_RSP],
1618 stack_mask(ctxt));
1619 assign_masked(&ctxt->regs[VCPU_REGS_RSP],
1620 ctxt->regs[VCPU_REGS_RSP] - frame_size,
1621 stack_mask(ctxt));
1622 return X86EMUL_CONTINUE;
1623}
1624
Avi Kivityf47cfa32012-06-07 17:49:24 +03001625static int em_leave(struct x86_emulate_ctxt *ctxt)
1626{
1627 assign_masked(&ctxt->regs[VCPU_REGS_RSP], ctxt->regs[VCPU_REGS_RBP],
1628 stack_mask(ctxt));
1629 return emulate_pop(ctxt, &ctxt->regs[VCPU_REGS_RBP], ctxt->op_bytes);
1630}
1631
Avi Kivity1cd196e2011-09-13 10:45:51 +03001632static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001633{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001634 int seg = ctxt->src2.val;
1635
Avi Kivity9dac77f2011-06-01 15:34:25 +03001636 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001637
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001638 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001639}
1640
Avi Kivity1cd196e2011-09-13 10:45:51 +03001641static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001642{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001643 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001644 unsigned long selector;
1645 int rc;
1646
Avi Kivity9dac77f2011-06-01 15:34:25 +03001647 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001648 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001649 return rc;
1650
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001651 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001652 return rc;
1653}
1654
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001655static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001656{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001657 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001658 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001659 int reg = VCPU_REGS_RAX;
1660
1661 while (reg <= VCPU_REGS_RDI) {
1662 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001663 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001664
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001665 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001666 if (rc != X86EMUL_CONTINUE)
1667 return rc;
1668
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001669 ++reg;
1670 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001671
Wei Yongjunc37eda12010-06-15 09:03:33 +08001672 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001673}
1674
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001675static int em_pushf(struct x86_emulate_ctxt *ctxt)
1676{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001677 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001678 return em_push(ctxt);
1679}
1680
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001681static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001682{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001683 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001684 int reg = VCPU_REGS_RDI;
1685
1686 while (reg >= VCPU_REGS_RAX) {
1687 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001688 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1689 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001690 --reg;
1691 }
1692
Avi Kivity9dac77f2011-06-01 15:34:25 +03001693 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001694 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001695 break;
1696 --reg;
1697 }
1698 return rc;
1699}
1700
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001701int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001702{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001703 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001704 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001705 struct desc_ptr dt;
1706 gva_t cs_addr;
1707 gva_t eip_addr;
1708 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001709
1710 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001711 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001712 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001713 if (rc != X86EMUL_CONTINUE)
1714 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001715
1716 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1717
Avi Kivity9dac77f2011-06-01 15:34:25 +03001718 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001719 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001720 if (rc != X86EMUL_CONTINUE)
1721 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001722
Avi Kivity9dac77f2011-06-01 15:34:25 +03001723 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001724 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001725 if (rc != X86EMUL_CONTINUE)
1726 return rc;
1727
Avi Kivity4bff1e862011-04-20 13:37:53 +03001728 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001729
1730 eip_addr = dt.address + (irq << 2);
1731 cs_addr = dt.address + (irq << 2) + 2;
1732
Avi Kivity0f65dd72011-04-20 13:37:53 +03001733 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001734 if (rc != X86EMUL_CONTINUE)
1735 return rc;
1736
Avi Kivity0f65dd72011-04-20 13:37:53 +03001737 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001738 if (rc != X86EMUL_CONTINUE)
1739 return rc;
1740
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001741 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001742 if (rc != X86EMUL_CONTINUE)
1743 return rc;
1744
Avi Kivity9dac77f2011-06-01 15:34:25 +03001745 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001746
1747 return rc;
1748}
1749
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001750static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001751{
1752 switch(ctxt->mode) {
1753 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001754 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001755 case X86EMUL_MODE_VM86:
1756 case X86EMUL_MODE_PROT16:
1757 case X86EMUL_MODE_PROT32:
1758 case X86EMUL_MODE_PROT64:
1759 default:
1760 /* Protected mode interrupts unimplemented yet */
1761 return X86EMUL_UNHANDLEABLE;
1762 }
1763}
1764
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001765static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001766{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001767 int rc = X86EMUL_CONTINUE;
1768 unsigned long temp_eip = 0;
1769 unsigned long temp_eflags = 0;
1770 unsigned long cs = 0;
1771 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1772 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1773 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1774 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1775
1776 /* TODO: Add stack limit check */
1777
Avi Kivity9dac77f2011-06-01 15:34:25 +03001778 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001779
1780 if (rc != X86EMUL_CONTINUE)
1781 return rc;
1782
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001783 if (temp_eip & ~0xffff)
1784 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001785
Avi Kivity9dac77f2011-06-01 15:34:25 +03001786 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001787
1788 if (rc != X86EMUL_CONTINUE)
1789 return rc;
1790
Avi Kivity9dac77f2011-06-01 15:34:25 +03001791 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001792
1793 if (rc != X86EMUL_CONTINUE)
1794 return rc;
1795
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001796 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001797
1798 if (rc != X86EMUL_CONTINUE)
1799 return rc;
1800
Avi Kivity9dac77f2011-06-01 15:34:25 +03001801 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001802
1803
Avi Kivity9dac77f2011-06-01 15:34:25 +03001804 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001805 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001806 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001807 ctxt->eflags &= ~0xffff;
1808 ctxt->eflags |= temp_eflags;
1809 }
1810
1811 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1812 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1813
1814 return rc;
1815}
1816
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001817static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001818{
1819 switch(ctxt->mode) {
1820 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001821 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001822 case X86EMUL_MODE_VM86:
1823 case X86EMUL_MODE_PROT16:
1824 case X86EMUL_MODE_PROT32:
1825 case X86EMUL_MODE_PROT64:
1826 default:
1827 /* iret from protected mode unimplemented yet */
1828 return X86EMUL_UNHANDLEABLE;
1829 }
1830}
1831
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001832static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1833{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001834 int rc;
1835 unsigned short sel;
1836
Avi Kivity9dac77f2011-06-01 15:34:25 +03001837 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001838
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001839 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001840 if (rc != X86EMUL_CONTINUE)
1841 return rc;
1842
Avi Kivity9dac77f2011-06-01 15:34:25 +03001843 ctxt->_eip = 0;
1844 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001845 return X86EMUL_CONTINUE;
1846}
1847
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001848static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001849{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001850 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001851 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001852 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001853 break;
1854 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001855 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001856 break;
1857 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001858 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001859 break;
1860 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001861 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001862 break;
1863 case 4: /* sal/shl */
1864 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001865 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866 break;
1867 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001868 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001869 break;
1870 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001871 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001872 break;
1873 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001874 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001875}
1876
Avi Kivity3329ece2011-09-13 10:45:39 +03001877static int em_not(struct x86_emulate_ctxt *ctxt)
1878{
1879 ctxt->dst.val = ~ctxt->dst.val;
1880 return X86EMUL_CONTINUE;
1881}
1882
1883static int em_neg(struct x86_emulate_ctxt *ctxt)
1884{
1885 emulate_1op(ctxt, "neg");
1886 return X86EMUL_CONTINUE;
1887}
1888
1889static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1890{
1891 u8 ex = 0;
1892
1893 emulate_1op_rax_rdx(ctxt, "mul", ex);
1894 return X86EMUL_CONTINUE;
1895}
1896
1897static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1898{
1899 u8 ex = 0;
1900
1901 emulate_1op_rax_rdx(ctxt, "imul", ex);
1902 return X86EMUL_CONTINUE;
1903}
1904
1905static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001906{
Avi Kivity34d1f492010-08-26 11:59:01 +03001907 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001908
Avi Kivity3329ece2011-09-13 10:45:39 +03001909 emulate_1op_rax_rdx(ctxt, "div", de);
1910 if (de)
1911 return emulate_de(ctxt);
1912 return X86EMUL_CONTINUE;
1913}
1914
1915static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1916{
1917 u8 de = 0;
1918
1919 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001920 if (de)
1921 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001922 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923}
1924
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001925static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001926{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001927 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001928
Avi Kivity9dac77f2011-06-01 15:34:25 +03001929 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001930 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001931 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001932 break;
1933 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001934 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001935 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001936 case 2: /* call near abs */ {
1937 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001938 old_eip = ctxt->_eip;
1939 ctxt->_eip = ctxt->src.val;
1940 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001941 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001942 break;
1943 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001944 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001945 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001946 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001947 case 5: /* jmp far */
1948 rc = em_jmp_far(ctxt);
1949 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001950 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001951 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001952 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001953 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001954 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001955}
1956
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001957static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001958{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001959 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001960
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1962 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1963 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1964 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001965 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001966 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1968 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001969
Laurent Vivier05f086f2007-09-24 11:10:55 +02001970 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001971 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001972 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001973}
1974
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001975static int em_ret(struct x86_emulate_ctxt *ctxt)
1976{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 ctxt->dst.type = OP_REG;
1978 ctxt->dst.addr.reg = &ctxt->_eip;
1979 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001980 return em_pop(ctxt);
1981}
1982
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001983static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001984{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001985 int rc;
1986 unsigned long cs;
1987
Avi Kivity9dac77f2011-06-01 15:34:25 +03001988 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001989 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001990 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001991 if (ctxt->op_bytes == 4)
1992 ctxt->_eip = (u32)ctxt->_eip;
1993 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001994 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001995 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001996 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001997 return rc;
1998}
1999
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002000static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2001{
2002 /* Save real source value, then compare EAX against destination. */
2003 ctxt->src.orig_val = ctxt->src.val;
2004 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
2005 emulate_2op_SrcV(ctxt, "cmp");
2006
2007 if (ctxt->eflags & EFLG_ZF) {
2008 /* Success: write back to memory. */
2009 ctxt->dst.val = ctxt->src.orig_val;
2010 } else {
2011 /* Failure: write the value we saw to EAX. */
2012 ctxt->dst.type = OP_REG;
2013 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
2014 }
2015 return X86EMUL_CONTINUE;
2016}
2017
Avi Kivityd4b43252011-09-13 10:45:50 +03002018static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002019{
Avi Kivityd4b43252011-09-13 10:45:50 +03002020 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002021 unsigned short sel;
2022 int rc;
2023
Avi Kivity9dac77f2011-06-01 15:34:25 +03002024 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002025
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002026 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002027 if (rc != X86EMUL_CONTINUE)
2028 return rc;
2029
Avi Kivity9dac77f2011-06-01 15:34:25 +03002030 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002031 return rc;
2032}
2033
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002034static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002035setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002036 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002037{
Avi Kivity1aa36612011-04-27 13:20:30 +03002038 u16 selector;
2039
Gleb Natapov79168fd2010-04-28 19:15:30 +03002040 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002041 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002042 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002043
2044 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002045 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002046 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002047 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002048 cs->type = 0x0b; /* Read, Execute, Accessed */
2049 cs->s = 1;
2050 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002051 cs->p = 1;
2052 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002053
Gleb Natapov79168fd2010-04-28 19:15:30 +03002054 set_desc_base(ss, 0); /* flat segment */
2055 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002056 ss->g = 1; /* 4kb granularity */
2057 ss->s = 1;
2058 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002059 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002060 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002061 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002062}
2063
Avi Kivity1a18a692012-02-01 12:23:21 +02002064static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2065{
2066 u32 eax, ebx, ecx, edx;
2067
2068 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002069 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2070 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002071 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2072 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2073}
2074
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002075static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2076{
2077 struct x86_emulate_ops *ops = ctxt->ops;
2078 u32 eax, ebx, ecx, edx;
2079
2080 /*
2081 * syscall should always be enabled in longmode - so only become
2082 * vendor specific (cpuid) if other modes are active...
2083 */
2084 if (ctxt->mode == X86EMUL_MODE_PROT64)
2085 return true;
2086
2087 eax = 0x00000000;
2088 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002089 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2090 /*
2091 * Intel ("GenuineIntel")
2092 * remark: Intel CPUs only support "syscall" in 64bit
2093 * longmode. Also an 64bit guest with a
2094 * 32bit compat-app running will #UD !! While this
2095 * behaviour can be fixed (by emulating) into AMD
2096 * response - CPUs of AMD can't behave like Intel.
2097 */
2098 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2099 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2100 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2101 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002102
Avi Kivity0017f932012-06-07 14:10:16 +03002103 /* AMD ("AuthenticAMD") */
2104 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2105 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2106 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2107 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002108
Avi Kivity0017f932012-06-07 14:10:16 +03002109 /* AMD ("AMDisbetter!") */
2110 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2111 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2112 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2113 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002114
2115 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2116 return false;
2117}
2118
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002119static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002120{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002121 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002122 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002123 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002124 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002125 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002126
2127 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002128 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002129 ctxt->mode == X86EMUL_MODE_VM86)
2130 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002131
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002132 if (!(em_syscall_is_enabled(ctxt)))
2133 return emulate_ud(ctxt);
2134
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002135 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002136 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002137
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002138 if (!(efer & EFER_SCE))
2139 return emulate_ud(ctxt);
2140
Avi Kivity717746e2011-04-20 13:37:53 +03002141 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002142 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002143 cs_sel = (u16)(msr_data & 0xfffc);
2144 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002145
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002146 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002147 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002148 cs.l = 1;
2149 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002150 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2151 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002152
Avi Kivity9dac77f2011-06-01 15:34:25 +03002153 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002154 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002155#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03002156 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002157
Avi Kivity717746e2011-04-20 13:37:53 +03002158 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002159 ctxt->mode == X86EMUL_MODE_PROT64 ?
2160 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002161 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002162
Avi Kivity717746e2011-04-20 13:37:53 +03002163 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002164 ctxt->eflags &= ~(msr_data | EFLG_RF);
2165#endif
2166 } else {
2167 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002168 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002169 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002170
2171 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2172 }
2173
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002174 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002175}
2176
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002177static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002178{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002179 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002180 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002181 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002182 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002183 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002184
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002185 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002186 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002187 if (ctxt->mode == X86EMUL_MODE_REAL)
2188 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002189
Avi Kivity1a18a692012-02-01 12:23:21 +02002190 /*
2191 * Not recognized on AMD in compat mode (but is recognized in legacy
2192 * mode).
2193 */
2194 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2195 && !vendor_intel(ctxt))
2196 return emulate_ud(ctxt);
2197
Andre Przywara8c604352009-06-18 12:56:01 +02002198 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2199 * Therefore, we inject an #UD.
2200 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002201 if (ctxt->mode == X86EMUL_MODE_PROT64)
2202 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002203
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002204 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002205
Avi Kivity717746e2011-04-20 13:37:53 +03002206 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002207 switch (ctxt->mode) {
2208 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002209 if ((msr_data & 0xfffc) == 0x0)
2210 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002211 break;
2212 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002213 if (msr_data == 0x0)
2214 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002215 break;
2216 }
2217
2218 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002219 cs_sel = (u16)msr_data;
2220 cs_sel &= ~SELECTOR_RPL_MASK;
2221 ss_sel = cs_sel + 8;
2222 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002223 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002224 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002225 cs.l = 1;
2226 }
2227
Avi Kivity1aa36612011-04-27 13:20:30 +03002228 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2229 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002230
Avi Kivity717746e2011-04-20 13:37:53 +03002231 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002232 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002233
Avi Kivity717746e2011-04-20 13:37:53 +03002234 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002235 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002236
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002237 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002238}
2239
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002240static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002241{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002242 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002243 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002244 u64 msr_data;
2245 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002246 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002247
Gleb Natapova0044752010-02-10 14:21:31 +02002248 /* inject #GP if in real mode or Virtual 8086 mode */
2249 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002250 ctxt->mode == X86EMUL_MODE_VM86)
2251 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002252
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002253 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002254
Avi Kivity9dac77f2011-06-01 15:34:25 +03002255 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002256 usermode = X86EMUL_MODE_PROT64;
2257 else
2258 usermode = X86EMUL_MODE_PROT32;
2259
2260 cs.dpl = 3;
2261 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002262 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002263 switch (usermode) {
2264 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002265 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002266 if ((msr_data & 0xfffc) == 0x0)
2267 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002269 break;
2270 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002271 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002272 if (msr_data == 0x0)
2273 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002274 ss_sel = cs_sel + 8;
2275 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002276 cs.l = 1;
2277 break;
2278 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002279 cs_sel |= SELECTOR_RPL_MASK;
2280 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002281
Avi Kivity1aa36612011-04-27 13:20:30 +03002282 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2283 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002284
Avi Kivity9dac77f2011-06-01 15:34:25 +03002285 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2286 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002287
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002288 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002289}
2290
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002291static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002292{
2293 int iopl;
2294 if (ctxt->mode == X86EMUL_MODE_REAL)
2295 return false;
2296 if (ctxt->mode == X86EMUL_MODE_VM86)
2297 return true;
2298 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002299 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002300}
2301
2302static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002303 u16 port, u16 len)
2304{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002305 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002306 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002307 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002308 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002309 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002310 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002311 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002312
Avi Kivity1aa36612011-04-27 13:20:30 +03002313 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002314 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002315 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002316 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002317 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002318 base = get_desc_base(&tr_seg);
2319#ifdef CONFIG_X86_64
2320 base |= ((u64)base3) << 32;
2321#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002322 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002323 if (r != X86EMUL_CONTINUE)
2324 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002325 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002326 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002327 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002328 if (r != X86EMUL_CONTINUE)
2329 return false;
2330 if ((perm >> bit_idx) & mask)
2331 return false;
2332 return true;
2333}
2334
2335static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002336 u16 port, u16 len)
2337{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002338 if (ctxt->perm_ok)
2339 return true;
2340
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002341 if (emulator_bad_iopl(ctxt))
2342 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002343 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002344
2345 ctxt->perm_ok = true;
2346
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002347 return true;
2348}
2349
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002351 struct tss_segment_16 *tss)
2352{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002353 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002354 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002355 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2356 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2357 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2358 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2359 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2360 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2361 tss->si = ctxt->regs[VCPU_REGS_RSI];
2362 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002363
Avi Kivity1aa36612011-04-27 13:20:30 +03002364 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2365 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2366 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2367 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2368 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002369}
2370
2371static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002372 struct tss_segment_16 *tss)
2373{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002374 int ret;
2375
Avi Kivity9dac77f2011-06-01 15:34:25 +03002376 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002377 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002378 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2379 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2380 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2381 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2382 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2383 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2384 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2385 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002386
2387 /*
2388 * SDM says that segment selectors are loaded before segment
2389 * descriptors
2390 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002391 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2392 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2393 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2394 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2395 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002396
2397 /*
Guo Chaofc058682012-06-28 15:19:51 +08002398 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399 * it is handled in a context of new task
2400 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002401 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402 if (ret != X86EMUL_CONTINUE)
2403 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002404 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405 if (ret != X86EMUL_CONTINUE)
2406 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002407 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408 if (ret != X86EMUL_CONTINUE)
2409 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002410 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411 if (ret != X86EMUL_CONTINUE)
2412 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002413 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002414 if (ret != X86EMUL_CONTINUE)
2415 return ret;
2416
2417 return X86EMUL_CONTINUE;
2418}
2419
2420static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421 u16 tss_selector, u16 old_tss_sel,
2422 ulong old_tss_base, struct desc_struct *new_desc)
2423{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002424 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002425 struct tss_segment_16 tss_seg;
2426 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002427 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002428
Avi Kivity0f65dd72011-04-20 13:37:53 +03002429 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002430 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002431 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002433 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002435 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436
Avi Kivity0f65dd72011-04-20 13:37:53 +03002437 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002438 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002439 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002441 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002442
Avi Kivity0f65dd72011-04-20 13:37:53 +03002443 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002444 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002445 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002446 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002447 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002448
2449 if (old_tss_sel != 0xffff) {
2450 tss_seg.prev_task_link = old_tss_sel;
2451
Avi Kivity0f65dd72011-04-20 13:37:53 +03002452 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002453 &tss_seg.prev_task_link,
2454 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002455 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002456 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002458 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002459 }
2460
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002461 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002462}
2463
2464static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002465 struct tss_segment_32 *tss)
2466{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002467 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002468 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002470 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2471 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2472 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2473 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2474 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2475 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2476 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2477 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002478
Avi Kivity1aa36612011-04-27 13:20:30 +03002479 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2480 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2481 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2482 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2483 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2484 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2485 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486}
2487
2488static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489 struct tss_segment_32 *tss)
2490{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 int ret;
2492
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002493 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002494 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002495 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002497
2498 /* General purpose registers */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002499 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2500 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2501 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2502 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2503 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2504 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2505 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2506 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507
2508 /*
2509 * SDM says that segment selectors are loaded before segment
2510 * descriptors
2511 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002512 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2513 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2514 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2515 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2516 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2517 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2518 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519
2520 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002521 * If we're switching between Protected Mode and VM86, we need to make
2522 * sure to update the mode before loading the segment descriptors so
2523 * that the selectors are interpreted correctly.
2524 *
2525 * Need to get rflags to the vcpu struct immediately because it
2526 * influences the CPL which is checked at least when loading the segment
2527 * descriptors and when pushing an error code to the new kernel stack.
2528 *
2529 * TODO Introduce a separate ctxt->ops->set_cpl callback
2530 */
2531 if (ctxt->eflags & X86_EFLAGS_VM)
2532 ctxt->mode = X86EMUL_MODE_VM86;
2533 else
2534 ctxt->mode = X86EMUL_MODE_PROT32;
2535
2536 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2537
2538 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539 * Now load segment descriptors. If fault happenes at this stage
2540 * it is handled in a context of new task
2541 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002542 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543 if (ret != X86EMUL_CONTINUE)
2544 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002545 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546 if (ret != X86EMUL_CONTINUE)
2547 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002548 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 if (ret != X86EMUL_CONTINUE)
2550 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002551 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 if (ret != X86EMUL_CONTINUE)
2553 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002554 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002555 if (ret != X86EMUL_CONTINUE)
2556 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002557 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
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->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002561 if (ret != X86EMUL_CONTINUE)
2562 return ret;
2563
2564 return X86EMUL_CONTINUE;
2565}
2566
2567static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568 u16 tss_selector, u16 old_tss_sel,
2569 ulong old_tss_base, struct desc_struct *new_desc)
2570{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002571 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572 struct tss_segment_32 tss_seg;
2573 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002574 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575
Avi Kivity0f65dd72011-04-20 13:37:53 +03002576 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002577 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002578 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002580 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002582 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002583
Avi Kivity0f65dd72011-04-20 13:37:53 +03002584 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002585 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002586 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002588 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589
Avi Kivity0f65dd72011-04-20 13:37:53 +03002590 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002591 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002592 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595
2596 if (old_tss_sel != 0xffff) {
2597 tss_seg.prev_task_link = old_tss_sel;
2598
Avi Kivity0f65dd72011-04-20 13:37:53 +03002599 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 &tss_seg.prev_task_link,
2601 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002602 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002603 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606 }
2607
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002608 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609}
2610
2611static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002612 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002613 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002615 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 struct desc_struct curr_tss_desc, next_tss_desc;
2617 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002618 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002620 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002621 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002622 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623
2624 /* FIXME: old_tss_base == ~0 ? */
2625
Avi Kivitye9194642012-06-13 16:29:39 +03002626 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 if (ret != X86EMUL_CONTINUE)
2628 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002629 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 if (ret != X86EMUL_CONTINUE)
2631 return ret;
2632
2633 /* FIXME: check that next_tss_desc is tss */
2634
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002635 /*
2636 * Check privileges. The three cases are task switch caused by...
2637 *
2638 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2639 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002640 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002641 */
2642 if (reason == TASK_SWITCH_GATE) {
2643 if (idt_index != -1) {
2644 /* Software interrupts */
2645 struct desc_struct task_gate_desc;
2646 int dpl;
2647
2648 ret = read_interrupt_descriptor(ctxt, idt_index,
2649 &task_gate_desc);
2650 if (ret != X86EMUL_CONTINUE)
2651 return ret;
2652
2653 dpl = task_gate_desc.dpl;
2654 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2655 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2656 }
2657 } else if (reason != TASK_SWITCH_IRET) {
2658 int dpl = next_tss_desc.dpl;
2659 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2660 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 }
2662
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002663
Gleb Natapovceffb452010-03-18 15:20:19 +02002664 desc_limit = desc_limit_scaled(&next_tss_desc);
2665 if (!next_tss_desc.p ||
2666 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2667 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002668 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669 return X86EMUL_PROPAGATE_FAULT;
2670 }
2671
2672 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2673 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002674 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 }
2676
2677 if (reason == TASK_SWITCH_IRET)
2678 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2679
2680 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002681 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2683 old_tss_sel = 0xffff;
2684
2685 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002686 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 old_tss_base, &next_tss_desc);
2688 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002689 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002691 if (ret != X86EMUL_CONTINUE)
2692 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002693
2694 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2695 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2696
2697 if (reason != TASK_SWITCH_IRET) {
2698 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002699 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002700 }
2701
Avi Kivity717746e2011-04-20 13:37:53 +03002702 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002703 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002704
Jan Kiszkae269fb22010-04-14 15:51:09 +02002705 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002706 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2707 ctxt->lock_prefix = 0;
2708 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002709 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002710 }
2711
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002712 return ret;
2713}
2714
2715int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002716 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002717 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719 int rc;
2720
Avi Kivity9dac77f2011-06-01 15:34:25 +03002721 ctxt->_eip = ctxt->eip;
2722 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002724 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002725 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002727 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002728 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002730 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731}
2732
Avi Kivity90de84f2010-11-17 15:28:21 +02002733static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002734 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002735{
Gleb Natapova682e352010-03-18 15:20:21 +02002736 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2737
Avi Kivity9dac77f2011-06-01 15:34:25 +03002738 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2739 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002740 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002741}
2742
Avi Kivity7af04fc2010-08-18 14:16:35 +03002743static int em_das(struct x86_emulate_ctxt *ctxt)
2744{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002745 u8 al, old_al;
2746 bool af, cf, old_cf;
2747
2748 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002749 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002750
2751 old_al = al;
2752 old_cf = cf;
2753 cf = false;
2754 af = ctxt->eflags & X86_EFLAGS_AF;
2755 if ((al & 0x0f) > 9 || af) {
2756 al -= 6;
2757 cf = old_cf | (al >= 250);
2758 af = true;
2759 } else {
2760 af = false;
2761 }
2762 if (old_al > 0x99 || old_cf) {
2763 al -= 0x60;
2764 cf = true;
2765 }
2766
Avi Kivity9dac77f2011-06-01 15:34:25 +03002767 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002768 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002769 ctxt->src.type = OP_IMM;
2770 ctxt->src.val = 0;
2771 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002772 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002773 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2774 if (cf)
2775 ctxt->eflags |= X86_EFLAGS_CF;
2776 if (af)
2777 ctxt->eflags |= X86_EFLAGS_AF;
2778 return X86EMUL_CONTINUE;
2779}
2780
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002781static int em_call(struct x86_emulate_ctxt *ctxt)
2782{
2783 long rel = ctxt->src.val;
2784
2785 ctxt->src.val = (unsigned long)ctxt->_eip;
2786 jmp_rel(ctxt, rel);
2787 return em_push(ctxt);
2788}
2789
Avi Kivity0ef753b2010-08-18 14:51:45 +03002790static int em_call_far(struct x86_emulate_ctxt *ctxt)
2791{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002792 u16 sel, old_cs;
2793 ulong old_eip;
2794 int rc;
2795
Avi Kivity1aa36612011-04-27 13:20:30 +03002796 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002797 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002798
Avi Kivity9dac77f2011-06-01 15:34:25 +03002799 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002800 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002801 return X86EMUL_CONTINUE;
2802
Avi Kivity9dac77f2011-06-01 15:34:25 +03002803 ctxt->_eip = 0;
2804 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002805
Avi Kivity9dac77f2011-06-01 15:34:25 +03002806 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002807 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002808 if (rc != X86EMUL_CONTINUE)
2809 return rc;
2810
Avi Kivity9dac77f2011-06-01 15:34:25 +03002811 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002812 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002813}
2814
Avi Kivity40ece7c2010-08-18 15:12:09 +03002815static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2816{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002817 int rc;
2818
Avi Kivity9dac77f2011-06-01 15:34:25 +03002819 ctxt->dst.type = OP_REG;
2820 ctxt->dst.addr.reg = &ctxt->_eip;
2821 ctxt->dst.bytes = ctxt->op_bytes;
2822 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002823 if (rc != X86EMUL_CONTINUE)
2824 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002825 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002826 return X86EMUL_CONTINUE;
2827}
2828
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002829static int em_add(struct x86_emulate_ctxt *ctxt)
2830{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002831 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002832 return X86EMUL_CONTINUE;
2833}
2834
2835static int em_or(struct x86_emulate_ctxt *ctxt)
2836{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002837 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002838 return X86EMUL_CONTINUE;
2839}
2840
2841static int em_adc(struct x86_emulate_ctxt *ctxt)
2842{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002843 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002844 return X86EMUL_CONTINUE;
2845}
2846
2847static int em_sbb(struct x86_emulate_ctxt *ctxt)
2848{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002849 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002850 return X86EMUL_CONTINUE;
2851}
2852
2853static int em_and(struct x86_emulate_ctxt *ctxt)
2854{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002855 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002856 return X86EMUL_CONTINUE;
2857}
2858
2859static int em_sub(struct x86_emulate_ctxt *ctxt)
2860{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002861 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002862 return X86EMUL_CONTINUE;
2863}
2864
2865static int em_xor(struct x86_emulate_ctxt *ctxt)
2866{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002867 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002868 return X86EMUL_CONTINUE;
2869}
2870
2871static int em_cmp(struct x86_emulate_ctxt *ctxt)
2872{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002873 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002874 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002875 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002876 return X86EMUL_CONTINUE;
2877}
2878
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002879static int em_test(struct x86_emulate_ctxt *ctxt)
2880{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002881 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002882 /* Disable writeback. */
2883 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002884 return X86EMUL_CONTINUE;
2885}
2886
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002887static int em_xchg(struct x86_emulate_ctxt *ctxt)
2888{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002889 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002890 ctxt->src.val = ctxt->dst.val;
2891 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002892
2893 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002894 ctxt->dst.val = ctxt->src.orig_val;
2895 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002896 return X86EMUL_CONTINUE;
2897}
2898
Avi Kivity5c82aa22010-08-18 18:31:43 +03002899static int em_imul(struct x86_emulate_ctxt *ctxt)
2900{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002901 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002902 return X86EMUL_CONTINUE;
2903}
2904
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002905static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2906{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002907 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002908 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002909}
2910
Avi Kivity61429142010-08-19 15:13:00 +03002911static int em_cwd(struct x86_emulate_ctxt *ctxt)
2912{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002913 ctxt->dst.type = OP_REG;
2914 ctxt->dst.bytes = ctxt->src.bytes;
2915 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2916 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002917
2918 return X86EMUL_CONTINUE;
2919}
2920
Avi Kivity48bb5d32010-08-18 18:54:34 +03002921static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2922{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002923 u64 tsc = 0;
2924
Avi Kivity717746e2011-04-20 13:37:53 +03002925 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002926 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2927 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002928 return X86EMUL_CONTINUE;
2929}
2930
Avi Kivity222d21a2011-11-10 14:57:30 +02002931static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2932{
2933 u64 pmc;
2934
2935 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2936 return emulate_gp(ctxt, 0);
2937 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2938 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2939 return X86EMUL_CONTINUE;
2940}
2941
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002942static int em_mov(struct x86_emulate_ctxt *ctxt)
2943{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002944 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002945 return X86EMUL_CONTINUE;
2946}
2947
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002948static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2949{
2950 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2951 return emulate_gp(ctxt, 0);
2952
2953 /* Disable writeback. */
2954 ctxt->dst.type = OP_NONE;
2955 return X86EMUL_CONTINUE;
2956}
2957
2958static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2959{
2960 unsigned long val;
2961
2962 if (ctxt->mode == X86EMUL_MODE_PROT64)
2963 val = ctxt->src.val & ~0ULL;
2964 else
2965 val = ctxt->src.val & ~0U;
2966
2967 /* #UD condition is already handled. */
2968 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2969 return emulate_gp(ctxt, 0);
2970
2971 /* Disable writeback. */
2972 ctxt->dst.type = OP_NONE;
2973 return X86EMUL_CONTINUE;
2974}
2975
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002976static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2977{
2978 u64 msr_data;
2979
2980 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2981 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2982 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2983 return emulate_gp(ctxt, 0);
2984
2985 return X86EMUL_CONTINUE;
2986}
2987
2988static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2989{
2990 u64 msr_data;
2991
2992 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2993 return emulate_gp(ctxt, 0);
2994
2995 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2996 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2997 return X86EMUL_CONTINUE;
2998}
2999
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003000static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3001{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003002 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003003 return emulate_ud(ctxt);
3004
Avi Kivity9dac77f2011-06-01 15:34:25 +03003005 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003006 return X86EMUL_CONTINUE;
3007}
3008
3009static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3010{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003011 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003012
Avi Kivity9dac77f2011-06-01 15:34:25 +03003013 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003014 return emulate_ud(ctxt);
3015
Avi Kivity9dac77f2011-06-01 15:34:25 +03003016 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003017 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3018
3019 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003020 ctxt->dst.type = OP_NONE;
3021 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003022}
3023
Avi Kivitya14e5792012-06-13 12:28:33 +03003024static int em_lldt(struct x86_emulate_ctxt *ctxt)
3025{
3026 u16 sel = ctxt->src.val;
3027
3028 /* Disable writeback. */
3029 ctxt->dst.type = OP_NONE;
3030 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3031}
3032
Avi Kivity80890002012-06-13 16:33:29 +03003033static int em_ltr(struct x86_emulate_ctxt *ctxt)
3034{
3035 u16 sel = ctxt->src.val;
3036
3037 /* Disable writeback. */
3038 ctxt->dst.type = OP_NONE;
3039 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3040}
3041
Avi Kivity38503912011-03-31 18:48:09 +02003042static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3043{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003044 int rc;
3045 ulong linear;
3046
Avi Kivity9dac77f2011-06-01 15:34:25 +03003047 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003048 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003049 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003050 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003051 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003052 return X86EMUL_CONTINUE;
3053}
3054
Avi Kivity2d04a052011-04-20 15:32:49 +03003055static int em_clts(struct x86_emulate_ctxt *ctxt)
3056{
3057 ulong cr0;
3058
3059 cr0 = ctxt->ops->get_cr(ctxt, 0);
3060 cr0 &= ~X86_CR0_TS;
3061 ctxt->ops->set_cr(ctxt, 0, cr0);
3062 return X86EMUL_CONTINUE;
3063}
3064
Avi Kivity26d05cc2011-04-21 12:07:59 +03003065static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3066{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003067 int rc;
3068
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003070 return X86EMUL_UNHANDLEABLE;
3071
3072 rc = ctxt->ops->fix_hypercall(ctxt);
3073 if (rc != X86EMUL_CONTINUE)
3074 return rc;
3075
3076 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003078 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003079 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003080 return X86EMUL_CONTINUE;
3081}
3082
Avi Kivity96051572012-06-10 17:21:18 +03003083static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3084 void (*get)(struct x86_emulate_ctxt *ctxt,
3085 struct desc_ptr *ptr))
3086{
3087 struct desc_ptr desc_ptr;
3088
3089 if (ctxt->mode == X86EMUL_MODE_PROT64)
3090 ctxt->op_bytes = 8;
3091 get(ctxt, &desc_ptr);
3092 if (ctxt->op_bytes == 2) {
3093 ctxt->op_bytes = 4;
3094 desc_ptr.address &= 0x00ffffff;
3095 }
3096 /* Disable writeback. */
3097 ctxt->dst.type = OP_NONE;
3098 return segmented_write(ctxt, ctxt->dst.addr.mem,
3099 &desc_ptr, 2 + ctxt->op_bytes);
3100}
3101
3102static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3103{
3104 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3105}
3106
3107static int em_sidt(struct x86_emulate_ctxt *ctxt)
3108{
3109 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3110}
3111
Avi Kivity26d05cc2011-04-21 12:07:59 +03003112static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3113{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003114 struct desc_ptr desc_ptr;
3115 int rc;
3116
Avi Kivity510425f2012-06-07 17:04:36 +03003117 if (ctxt->mode == X86EMUL_MODE_PROT64)
3118 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003119 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003120 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003121 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003122 if (rc != X86EMUL_CONTINUE)
3123 return rc;
3124 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3125 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003126 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003127 return X86EMUL_CONTINUE;
3128}
3129
Avi Kivity5ef39c72011-04-21 12:21:50 +03003130static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003131{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003132 int rc;
3133
Avi Kivity5ef39c72011-04-21 12:21:50 +03003134 rc = ctxt->ops->fix_hypercall(ctxt);
3135
Avi Kivity26d05cc2011-04-21 12:07:59 +03003136 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003137 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003138 return rc;
3139}
3140
3141static int em_lidt(struct x86_emulate_ctxt *ctxt)
3142{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003143 struct desc_ptr desc_ptr;
3144 int rc;
3145
Avi Kivity510425f2012-06-07 17:04:36 +03003146 if (ctxt->mode == X86EMUL_MODE_PROT64)
3147 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003148 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003149 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003150 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003151 if (rc != X86EMUL_CONTINUE)
3152 return rc;
3153 ctxt->ops->set_idt(ctxt, &desc_ptr);
3154 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003155 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003156 return X86EMUL_CONTINUE;
3157}
3158
3159static int em_smsw(struct x86_emulate_ctxt *ctxt)
3160{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003161 ctxt->dst.bytes = 2;
3162 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003163 return X86EMUL_CONTINUE;
3164}
3165
3166static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3167{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003168 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003169 | (ctxt->src.val & 0x0f));
3170 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003171 return X86EMUL_CONTINUE;
3172}
3173
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003174static int em_loop(struct x86_emulate_ctxt *ctxt)
3175{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003176 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
3177 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
3178 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3179 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003180
3181 return X86EMUL_CONTINUE;
3182}
3183
3184static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3185{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003186 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
3187 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003188
3189 return X86EMUL_CONTINUE;
3190}
3191
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003192static int em_in(struct x86_emulate_ctxt *ctxt)
3193{
3194 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3195 &ctxt->dst.val))
3196 return X86EMUL_IO_NEEDED;
3197
3198 return X86EMUL_CONTINUE;
3199}
3200
3201static int em_out(struct x86_emulate_ctxt *ctxt)
3202{
3203 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3204 &ctxt->src.val, 1);
3205 /* Disable writeback. */
3206 ctxt->dst.type = OP_NONE;
3207 return X86EMUL_CONTINUE;
3208}
3209
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003210static int em_cli(struct x86_emulate_ctxt *ctxt)
3211{
3212 if (emulator_bad_iopl(ctxt))
3213 return emulate_gp(ctxt, 0);
3214
3215 ctxt->eflags &= ~X86_EFLAGS_IF;
3216 return X86EMUL_CONTINUE;
3217}
3218
3219static int em_sti(struct x86_emulate_ctxt *ctxt)
3220{
3221 if (emulator_bad_iopl(ctxt))
3222 return emulate_gp(ctxt, 0);
3223
3224 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3225 ctxt->eflags |= X86_EFLAGS_IF;
3226 return X86EMUL_CONTINUE;
3227}
3228
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003229static int em_bt(struct x86_emulate_ctxt *ctxt)
3230{
3231 /* Disable writeback. */
3232 ctxt->dst.type = OP_NONE;
3233 /* only subword offset */
3234 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3235
3236 emulate_2op_SrcV_nobyte(ctxt, "bt");
3237 return X86EMUL_CONTINUE;
3238}
3239
3240static int em_bts(struct x86_emulate_ctxt *ctxt)
3241{
3242 emulate_2op_SrcV_nobyte(ctxt, "bts");
3243 return X86EMUL_CONTINUE;
3244}
3245
3246static int em_btr(struct x86_emulate_ctxt *ctxt)
3247{
3248 emulate_2op_SrcV_nobyte(ctxt, "btr");
3249 return X86EMUL_CONTINUE;
3250}
3251
3252static int em_btc(struct x86_emulate_ctxt *ctxt)
3253{
3254 emulate_2op_SrcV_nobyte(ctxt, "btc");
3255 return X86EMUL_CONTINUE;
3256}
3257
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003258static int em_bsf(struct x86_emulate_ctxt *ctxt)
3259{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003260 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003261 return X86EMUL_CONTINUE;
3262}
3263
3264static int em_bsr(struct x86_emulate_ctxt *ctxt)
3265{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003266 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003267 return X86EMUL_CONTINUE;
3268}
3269
Avi Kivity6d6eede2012-06-07 14:11:36 +03003270static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3271{
3272 u32 eax, ebx, ecx, edx;
3273
3274 eax = ctxt->regs[VCPU_REGS_RAX];
3275 ecx = ctxt->regs[VCPU_REGS_RCX];
3276 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3277 ctxt->regs[VCPU_REGS_RAX] = eax;
3278 ctxt->regs[VCPU_REGS_RBX] = ebx;
3279 ctxt->regs[VCPU_REGS_RCX] = ecx;
3280 ctxt->regs[VCPU_REGS_RDX] = edx;
3281 return X86EMUL_CONTINUE;
3282}
3283
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003284static int em_lahf(struct x86_emulate_ctxt *ctxt)
3285{
3286 ctxt->regs[VCPU_REGS_RAX] &= ~0xff00UL;
3287 ctxt->regs[VCPU_REGS_RAX] |= (ctxt->eflags & 0xff) << 8;
3288 return X86EMUL_CONTINUE;
3289}
3290
Avi Kivity92998362012-06-13 12:25:06 +03003291static int em_bswap(struct x86_emulate_ctxt *ctxt)
3292{
3293 switch (ctxt->op_bytes) {
3294#ifdef CONFIG_X86_64
3295 case 8:
3296 asm("bswap %0" : "+r"(ctxt->dst.val));
3297 break;
3298#endif
3299 default:
3300 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3301 break;
3302 }
3303 return X86EMUL_CONTINUE;
3304}
3305
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003306static bool valid_cr(int nr)
3307{
3308 switch (nr) {
3309 case 0:
3310 case 2 ... 4:
3311 case 8:
3312 return true;
3313 default:
3314 return false;
3315 }
3316}
3317
3318static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3319{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003320 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003321 return emulate_ud(ctxt);
3322
3323 return X86EMUL_CONTINUE;
3324}
3325
3326static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3327{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003328 u64 new_val = ctxt->src.val64;
3329 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003330 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003331
3332 static u64 cr_reserved_bits[] = {
3333 0xffffffff00000000ULL,
3334 0, 0, 0, /* CR3 checked later */
3335 CR4_RESERVED_BITS,
3336 0, 0, 0,
3337 CR8_RESERVED_BITS,
3338 };
3339
3340 if (!valid_cr(cr))
3341 return emulate_ud(ctxt);
3342
3343 if (new_val & cr_reserved_bits[cr])
3344 return emulate_gp(ctxt, 0);
3345
3346 switch (cr) {
3347 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003348 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003349 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3350 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3351 return emulate_gp(ctxt, 0);
3352
Avi Kivity717746e2011-04-20 13:37:53 +03003353 cr4 = ctxt->ops->get_cr(ctxt, 4);
3354 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003355
3356 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3357 !(cr4 & X86_CR4_PAE))
3358 return emulate_gp(ctxt, 0);
3359
3360 break;
3361 }
3362 case 3: {
3363 u64 rsvd = 0;
3364
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003365 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3366 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003367 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003368 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003369 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003370 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003371 rsvd = CR3_NONPAE_RESERVED_BITS;
3372
3373 if (new_val & rsvd)
3374 return emulate_gp(ctxt, 0);
3375
3376 break;
3377 }
3378 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003379 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003380
3381 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3382 return emulate_gp(ctxt, 0);
3383
3384 break;
3385 }
3386 }
3387
3388 return X86EMUL_CONTINUE;
3389}
3390
Joerg Roedel3b88e412011-04-04 12:39:29 +02003391static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3392{
3393 unsigned long dr7;
3394
Avi Kivity717746e2011-04-20 13:37:53 +03003395 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003396
3397 /* Check if DR7.Global_Enable is set */
3398 return dr7 & (1 << 13);
3399}
3400
3401static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3402{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003403 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003404 u64 cr4;
3405
3406 if (dr > 7)
3407 return emulate_ud(ctxt);
3408
Avi Kivity717746e2011-04-20 13:37:53 +03003409 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003410 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3411 return emulate_ud(ctxt);
3412
3413 if (check_dr7_gd(ctxt))
3414 return emulate_db(ctxt);
3415
3416 return X86EMUL_CONTINUE;
3417}
3418
3419static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3420{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003421 u64 new_val = ctxt->src.val64;
3422 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003423
3424 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3425 return emulate_gp(ctxt, 0);
3426
3427 return check_dr_read(ctxt);
3428}
3429
Joerg Roedel01de8b02011-04-04 12:39:31 +02003430static int check_svme(struct x86_emulate_ctxt *ctxt)
3431{
3432 u64 efer;
3433
Avi Kivity717746e2011-04-20 13:37:53 +03003434 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003435
3436 if (!(efer & EFER_SVME))
3437 return emulate_ud(ctxt);
3438
3439 return X86EMUL_CONTINUE;
3440}
3441
3442static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3443{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003444 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003445
3446 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003447 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003448 return emulate_gp(ctxt, 0);
3449
3450 return check_svme(ctxt);
3451}
3452
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003453static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3454{
Avi Kivity717746e2011-04-20 13:37:53 +03003455 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003456
Avi Kivity717746e2011-04-20 13:37:53 +03003457 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003458 return emulate_ud(ctxt);
3459
3460 return X86EMUL_CONTINUE;
3461}
3462
Joerg Roedel80612522011-04-04 12:39:33 +02003463static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3464{
Avi Kivity717746e2011-04-20 13:37:53 +03003465 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003466 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003467
Avi Kivity717746e2011-04-20 13:37:53 +03003468 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003469 (rcx > 3))
3470 return emulate_gp(ctxt, 0);
3471
3472 return X86EMUL_CONTINUE;
3473}
3474
Joerg Roedelf6511932011-04-04 12:39:35 +02003475static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3476{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003477 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3478 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003479 return emulate_gp(ctxt, 0);
3480
3481 return X86EMUL_CONTINUE;
3482}
3483
3484static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3485{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003486 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3487 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003488 return emulate_gp(ctxt, 0);
3489
3490 return X86EMUL_CONTINUE;
3491}
3492
Avi Kivity73fba5f2010-07-29 15:11:53 +03003493#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003494#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003495#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3496 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003497#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003498#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003499#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3500#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003501#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003502#define II(_f, _e, _i) \
3503 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003504#define IIP(_f, _e, _i, _p) \
3505 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3506 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003507#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003508
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003509#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003510#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003511#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003512#define I2bvIP(_f, _e, _i, _p) \
3513 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003514
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003515#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3516 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3517 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003518
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003519static struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003520 DI(SrcNone | Priv, monitor),
3521 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003522 N, N, N, N, N, N,
3523};
3524
Joerg Roedel01de8b02011-04-04 12:39:31 +02003525static struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003526 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3527 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3528 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3529 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3530 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3531 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3532 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3533 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003534};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003535
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003536static struct opcode group7_rm7[] = {
3537 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003538 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003539 N, N, N, N, N, N,
3540};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003541
Avi Kivity73fba5f2010-07-29 15:11:53 +03003542static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003543 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003544 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003545 I(Lock, em_adc),
3546 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003547 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003548 I(Lock, em_sub),
3549 I(Lock, em_xor),
3550 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003551};
3552
3553static struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003554 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003555};
3556
3557static struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003558 I(DstMem | SrcImm, em_test),
3559 I(DstMem | SrcImm, em_test),
3560 I(DstMem | SrcNone | Lock, em_not),
3561 I(DstMem | SrcNone | Lock, em_neg),
3562 I(SrcMem, em_mul_ex),
3563 I(SrcMem, em_imul_ex),
3564 I(SrcMem, em_div_ex),
3565 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003566};
3567
3568static struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003569 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3570 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003571 N, N, N, N, N, N,
3572};
3573
3574static struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003575 I(DstMem | SrcNone | Lock, em_grp45),
3576 I(DstMem | SrcNone | Lock, em_grp45),
3577 I(SrcMem | Stack, em_grp45),
3578 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3579 I(SrcMem | Stack, em_grp45),
3580 I(SrcMemFAddr | ImplicitOps, em_grp45),
3581 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003582};
3583
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003584static struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003585 DI(Prot, sldt),
3586 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003587 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003588 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003589 N, N, N, N,
3590};
3591
Avi Kivity73fba5f2010-07-29 15:11:53 +03003592static struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003593 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3594 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003595 II(SrcMem | Priv, em_lgdt, lgdt),
3596 II(SrcMem | Priv, em_lidt, lidt),
3597 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3598 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3599 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003600}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003601 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003602 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003603 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003604 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3605 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3606 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003607} };
3608
3609static struct opcode group8[] = {
3610 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003611 I(DstMem | SrcImmByte, em_bt),
3612 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3613 I(DstMem | SrcImmByte | Lock, em_btr),
3614 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003615};
3616
3617static struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003618 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003619}, {
3620 N, N, N, N, N, N, N, N,
3621} };
3622
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003623static struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003624 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003625 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003626};
3627
Avi Kivityaa97bb42010-01-20 18:09:23 +02003628static struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003629 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003630};
3631
Avi Kivity3e114eb2012-04-09 18:40:01 +03003632static struct gprefix pfx_vmovntpx = {
3633 I(0, em_mov), N, N, N,
3634};
3635
Avi Kivity73fba5f2010-07-29 15:11:53 +03003636static struct opcode opcode_table[256] = {
3637 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003638 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003639 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3640 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003641 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003642 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003643 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3644 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003645 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003646 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003647 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3648 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003649 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003650 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003651 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3652 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003653 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003654 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003655 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003656 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003657 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003658 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003659 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003660 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661 /* 0x40 - 0x4F */
3662 X16(D(DstReg)),
3663 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003664 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003665 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003666 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003667 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003668 I(ImplicitOps | Stack | No64, em_pusha),
3669 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003670 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3671 N, N, N, N,
3672 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003673 I(SrcImm | Mov | Stack, em_push),
3674 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003675 I(SrcImmByte | Mov | Stack, em_push),
3676 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003677 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3678 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679 /* 0x70 - 0x7F */
3680 X16(D(SrcImmByte)),
3681 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003682 G(ByteOp | DstMem | SrcImm, group1),
3683 G(DstMem | SrcImm, group1),
3684 G(ByteOp | DstMem | SrcImm | No64, group1),
3685 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003686 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003687 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003688 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003689 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003690 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003691 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003692 D(ModRM | SrcMem | NoAccess | DstReg),
3693 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3694 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003695 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003696 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003697 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003698 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003699 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003700 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003701 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003702 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003703 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003704 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003705 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003706 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003707 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003708 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003709 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3710 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003711 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003712 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003713 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003714 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003715 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003716 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003717 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003718 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003719 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003720 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3721 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003722 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003723 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003724 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3725 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003726 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003727 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003728 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003729 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003730 N, N, N, N,
3731 /* 0xD8 - 0xDF */
3732 N, N, N, N, N, N, N, N,
3733 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003734 X3(I(SrcImmByte, em_loop)),
3735 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003736 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3737 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003738 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003739 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003740 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003741 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3742 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003743 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003744 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003745 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3746 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003748 D(ImplicitOps), D(ImplicitOps),
3749 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3751};
3752
3753static struct opcode twobyte_table[256] = {
3754 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003755 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003756 N, I(ImplicitOps | VendorSpecific, em_syscall),
3757 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003758 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003759 N, D(ImplicitOps | ModRM), N, N,
3760 /* 0x10 - 0x1F */
3761 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3762 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003763 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003764 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003765 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3766 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003767 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003768 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3769 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003770 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003771 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003772 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003773 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003774 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003775 I(ImplicitOps | VendorSpecific, em_sysenter),
3776 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003777 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003778 N, N, N, N, N, N, N, N,
3779 /* 0x40 - 0x4F */
3780 X16(D(DstReg | SrcMem | ModRM | Mov)),
3781 /* 0x50 - 0x5F */
3782 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3783 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003784 N, N, N, N,
3785 N, N, N, N,
3786 N, N, N, N,
3787 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003789 N, N, N, N,
3790 N, N, N, N,
3791 N, N, N, N,
3792 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003793 /* 0x80 - 0x8F */
3794 X16(D(SrcImm)),
3795 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003796 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003797 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003798 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003799 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003800 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3801 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3802 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003803 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003804 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003805 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003806 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3807 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003808 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003809 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003810 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003811 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003812 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003813 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3814 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003815 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003816 /* 0xB8 - 0xBF */
3817 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003818 G(BitOp, group8),
3819 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003820 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003821 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003822 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003823 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003824 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003826 /* 0xC8 - 0xCF */
3827 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003828 /* 0xD0 - 0xDF */
3829 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3830 /* 0xE0 - 0xEF */
3831 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3832 /* 0xF0 - 0xFF */
3833 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3834};
3835
3836#undef D
3837#undef N
3838#undef G
3839#undef GD
3840#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003841#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003842#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003843
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003844#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003845#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003846#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003847#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003848#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003849
Avi Kivity9dac77f2011-06-01 15:34:25 +03003850static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003851{
3852 unsigned size;
3853
Avi Kivity9dac77f2011-06-01 15:34:25 +03003854 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003855 if (size == 8)
3856 size = 4;
3857 return size;
3858}
3859
3860static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3861 unsigned size, bool sign_extension)
3862{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003863 int rc = X86EMUL_CONTINUE;
3864
3865 op->type = OP_IMM;
3866 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003867 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003868 /* NB. Immediates are sign-extended as necessary. */
3869 switch (op->bytes) {
3870 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003871 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003872 break;
3873 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003874 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003875 break;
3876 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003877 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003878 break;
3879 }
3880 if (!sign_extension) {
3881 switch (op->bytes) {
3882 case 1:
3883 op->val &= 0xff;
3884 break;
3885 case 2:
3886 op->val &= 0xffff;
3887 break;
3888 case 4:
3889 op->val &= 0xffffffff;
3890 break;
3891 }
3892 }
3893done:
3894 return rc;
3895}
3896
Avi Kivitya9945542011-09-13 10:45:41 +03003897static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3898 unsigned d)
3899{
3900 int rc = X86EMUL_CONTINUE;
3901
3902 switch (d) {
3903 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003904 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03003905 break;
3906 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003907 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003908 break;
3909 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003910 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003911 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003912 *op = ctxt->memop;
3913 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003914 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003915 fetch_bit_operand(ctxt);
3916 op->orig_val = op->val;
3917 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003918 case OpMem64:
3919 ctxt->memop.bytes = 8;
3920 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003921 case OpAcc:
3922 op->type = OP_REG;
3923 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3924 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3925 fetch_register_operand(op);
3926 op->orig_val = op->val;
3927 break;
3928 case OpDI:
3929 op->type = OP_MEM;
3930 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3931 op->addr.mem.ea =
3932 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3933 op->addr.mem.seg = VCPU_SREG_ES;
3934 op->val = 0;
3935 break;
3936 case OpDX:
3937 op->type = OP_REG;
3938 op->bytes = 2;
3939 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3940 fetch_register_operand(op);
3941 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003942 case OpCL:
3943 op->bytes = 1;
3944 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3945 break;
3946 case OpImmByte:
3947 rc = decode_imm(ctxt, op, 1, true);
3948 break;
3949 case OpOne:
3950 op->bytes = 1;
3951 op->val = 1;
3952 break;
3953 case OpImm:
3954 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3955 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003956 case OpMem8:
3957 ctxt->memop.bytes = 1;
3958 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003959 case OpMem16:
3960 ctxt->memop.bytes = 2;
3961 goto mem_common;
3962 case OpMem32:
3963 ctxt->memop.bytes = 4;
3964 goto mem_common;
3965 case OpImmU16:
3966 rc = decode_imm(ctxt, op, 2, false);
3967 break;
3968 case OpImmU:
3969 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3970 break;
3971 case OpSI:
3972 op->type = OP_MEM;
3973 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3974 op->addr.mem.ea =
3975 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3976 op->addr.mem.seg = seg_override(ctxt);
3977 op->val = 0;
3978 break;
3979 case OpImmFAddr:
3980 op->type = OP_IMM;
3981 op->addr.mem.ea = ctxt->_eip;
3982 op->bytes = ctxt->op_bytes + 2;
3983 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3984 break;
3985 case OpMemFAddr:
3986 ctxt->memop.bytes = ctxt->op_bytes + 2;
3987 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003988 case OpES:
3989 op->val = VCPU_SREG_ES;
3990 break;
3991 case OpCS:
3992 op->val = VCPU_SREG_CS;
3993 break;
3994 case OpSS:
3995 op->val = VCPU_SREG_SS;
3996 break;
3997 case OpDS:
3998 op->val = VCPU_SREG_DS;
3999 break;
4000 case OpFS:
4001 op->val = VCPU_SREG_FS;
4002 break;
4003 case OpGS:
4004 op->val = VCPU_SREG_GS;
4005 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004006 case OpImplicit:
4007 /* Special instructions do their own operand decoding. */
4008 default:
4009 op->type = OP_NONE; /* Disable writeback. */
4010 break;
4011 }
4012
4013done:
4014 return rc;
4015}
4016
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004017int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004018{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004019 int rc = X86EMUL_CONTINUE;
4020 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004021 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004022 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004023 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004024
Avi Kivityf09ed832011-09-13 10:45:40 +03004025 ctxt->memop.type = OP_NONE;
4026 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004027 ctxt->_eip = ctxt->eip;
4028 ctxt->fetch.start = ctxt->_eip;
4029 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004030 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004031 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004032
4033 switch (mode) {
4034 case X86EMUL_MODE_REAL:
4035 case X86EMUL_MODE_VM86:
4036 case X86EMUL_MODE_PROT16:
4037 def_op_bytes = def_ad_bytes = 2;
4038 break;
4039 case X86EMUL_MODE_PROT32:
4040 def_op_bytes = def_ad_bytes = 4;
4041 break;
4042#ifdef CONFIG_X86_64
4043 case X86EMUL_MODE_PROT64:
4044 def_op_bytes = 4;
4045 def_ad_bytes = 8;
4046 break;
4047#endif
4048 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004049 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004050 }
4051
Avi Kivity9dac77f2011-06-01 15:34:25 +03004052 ctxt->op_bytes = def_op_bytes;
4053 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004054
4055 /* Legacy prefixes. */
4056 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004057 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004058 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004059 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004060 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004061 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004062 break;
4063 case 0x67: /* address-size override */
4064 if (mode == X86EMUL_MODE_PROT64)
4065 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004066 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004067 else
4068 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004069 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004070 break;
4071 case 0x26: /* ES override */
4072 case 0x2e: /* CS override */
4073 case 0x36: /* SS override */
4074 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004075 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004076 break;
4077 case 0x64: /* FS override */
4078 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004079 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004080 break;
4081 case 0x40 ... 0x4f: /* REX */
4082 if (mode != X86EMUL_MODE_PROT64)
4083 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004084 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004085 continue;
4086 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004087 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004088 break;
4089 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004090 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004092 break;
4093 default:
4094 goto done_prefixes;
4095 }
4096
4097 /* Any legacy prefix after a REX prefix nullifies its effect. */
4098
Avi Kivity9dac77f2011-06-01 15:34:25 +03004099 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004100 }
4101
4102done_prefixes:
4103
4104 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004105 if (ctxt->rex_prefix & 8)
4106 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004107
4108 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004109 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004110 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004111 if (ctxt->b == 0x0f) {
4112 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004113 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004114 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004115 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004116 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004117
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004118 if (ctxt->d & ModRM)
4119 ctxt->modrm = insn_fetch(u8, ctxt);
4120
Avi Kivity9dac77f2011-06-01 15:34:25 +03004121 while (ctxt->d & GroupMask) {
4122 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004123 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004124 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004125 opcode = opcode.u.group[goffset];
4126 break;
4127 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004128 goffset = (ctxt->modrm >> 3) & 7;
4129 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004130 opcode = opcode.u.gdual->mod3[goffset];
4131 else
4132 opcode = opcode.u.gdual->mod012[goffset];
4133 break;
4134 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004135 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004136 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004137 break;
4138 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004139 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004140 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004141 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004142 switch (simd_prefix) {
4143 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4144 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4145 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4146 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4147 }
4148 break;
4149 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004150 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004151 }
Avi Kivity46561642011-04-24 14:09:59 +03004152
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004153 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004154 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004155 }
4156
Avi Kivity9dac77f2011-06-01 15:34:25 +03004157 ctxt->execute = opcode.u.execute;
4158 ctxt->check_perm = opcode.check_perm;
4159 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004160
4161 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004162 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004163 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004164
Avi Kivity9dac77f2011-06-01 15:34:25 +03004165 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004166 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004167
Avi Kivity9dac77f2011-06-01 15:34:25 +03004168 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4169 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004170
Avi Kivity9dac77f2011-06-01 15:34:25 +03004171 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004172 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004173 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004174 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004176 }
4177
Avi Kivity9dac77f2011-06-01 15:34:25 +03004178 if (ctxt->d & Sse)
4179 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004180 else if (ctxt->d & Mmx)
4181 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004182
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004183 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004184 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004185 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004186 if (!ctxt->has_seg_override)
4187 set_seg_override(ctxt, ctxt->modrm_seg);
4188 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004189 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004190 if (rc != X86EMUL_CONTINUE)
4191 goto done;
4192
Avi Kivity9dac77f2011-06-01 15:34:25 +03004193 if (!ctxt->has_seg_override)
4194 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004195
Avi Kivityf09ed832011-09-13 10:45:40 +03004196 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004197
Avi Kivityf09ed832011-09-13 10:45:40 +03004198 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4199 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004200
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004201 /*
4202 * Decode and fetch the source operand: register, memory
4203 * or immediate.
4204 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004205 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004206 if (rc != X86EMUL_CONTINUE)
4207 goto done;
4208
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004209 /*
4210 * Decode and fetch the second source operand: register, memory
4211 * or immediate.
4212 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004213 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004214 if (rc != X86EMUL_CONTINUE)
4215 goto done;
4216
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004217 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004218 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004219
4220done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004221 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4222 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004223
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004224 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004225}
4226
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004227bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4228{
4229 return ctxt->d & PageTable;
4230}
4231
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004232static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4233{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004234 /* The second termination condition only applies for REPE
4235 * and REPNE. Test if the repeat string operation prefix is
4236 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4237 * corresponding termination condition according to:
4238 * - if REPE/REPZ and ZF = 0 then done
4239 * - if REPNE/REPNZ and ZF = 1 then done
4240 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004241 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4242 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4243 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004244 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004245 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004246 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4247 return true;
4248
4249 return false;
4250}
4251
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004252static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4253{
4254 bool fault = false;
4255
4256 ctxt->ops->get_fpu(ctxt);
4257 asm volatile("1: fwait \n\t"
4258 "2: \n\t"
4259 ".pushsection .fixup,\"ax\" \n\t"
4260 "3: \n\t"
4261 "movb $1, %[fault] \n\t"
4262 "jmp 2b \n\t"
4263 ".popsection \n\t"
4264 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004265 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004266 ctxt->ops->put_fpu(ctxt);
4267
4268 if (unlikely(fault))
4269 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4270
4271 return X86EMUL_CONTINUE;
4272}
4273
4274static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4275 struct operand *op)
4276{
4277 if (op->type == OP_MM)
4278 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4279}
4280
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004281int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004282{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004283 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004284 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004285 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004286
Avi Kivity9dac77f2011-06-01 15:34:25 +03004287 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004288
Avi Kivity9dac77f2011-06-01 15:34:25 +03004289 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004290 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004291 goto done;
4292 }
4293
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004294 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004295 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004296 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004297 goto done;
4298 }
4299
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004301 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004302 goto done;
4303 }
4304
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004305 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4306 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004307 rc = emulate_ud(ctxt);
4308 goto done;
4309 }
4310
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004311 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004312 rc = emulate_nm(ctxt);
4313 goto done;
4314 }
4315
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004316 if (ctxt->d & Mmx) {
4317 rc = flush_pending_x87_faults(ctxt);
4318 if (rc != X86EMUL_CONTINUE)
4319 goto done;
4320 /*
4321 * Now that we know the fpu is exception safe, we can fetch
4322 * operands from it.
4323 */
4324 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4325 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4326 if (!(ctxt->d & Mov))
4327 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4328 }
4329
Avi Kivity9dac77f2011-06-01 15:34:25 +03004330 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4331 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004332 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004333 if (rc != X86EMUL_CONTINUE)
4334 goto done;
4335 }
4336
Gleb Natapove92805a2010-02-10 14:21:35 +02004337 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004338 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004339 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004340 goto done;
4341 }
4342
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004343 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004344 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004345 rc = emulate_ud(ctxt);
4346 goto done;
4347 }
4348
Joerg Roedeld09beab2011-04-04 12:39:25 +02004349 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 if (ctxt->check_perm) {
4351 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004352 if (rc != X86EMUL_CONTINUE)
4353 goto done;
4354 }
4355
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4357 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004358 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004359 if (rc != X86EMUL_CONTINUE)
4360 goto done;
4361 }
4362
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004364 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4366 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004367 goto done;
4368 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004369 }
4370
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4372 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4373 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004374 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004375 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004377 }
4378
Avi Kivity9dac77f2011-06-01 15:34:25 +03004379 if (ctxt->src2.type == OP_MEM) {
4380 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4381 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004382 if (rc != X86EMUL_CONTINUE)
4383 goto done;
4384 }
4385
Avi Kivity9dac77f2011-06-01 15:34:25 +03004386 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004387 goto special_insn;
4388
4389
Avi Kivity9dac77f2011-06-01 15:34:25 +03004390 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004391 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004392 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4393 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004394 if (rc != X86EMUL_CONTINUE)
4395 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004396 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004398
Avi Kivity018a98d2007-11-27 19:30:56 +02004399special_insn:
4400
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4402 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004403 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004404 if (rc != X86EMUL_CONTINUE)
4405 goto done;
4406 }
4407
Avi Kivity9dac77f2011-06-01 15:34:25 +03004408 if (ctxt->execute) {
4409 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004410 if (rc != X86EMUL_CONTINUE)
4411 goto done;
4412 goto writeback;
4413 }
4414
Avi Kivity9dac77f2011-06-01 15:34:25 +03004415 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004416 goto twobyte_insn;
4417
Avi Kivity9dac77f2011-06-01 15:34:25 +03004418 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004419 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004420 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004421 break;
4422 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004423 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004424 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004426 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004428 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004429 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004430 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004431 if (test_cc(ctxt->b, ctxt->eflags))
4432 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004433 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004434 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004436 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004437 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004438 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004439 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004440 rc = em_xchg(ctxt);
4441 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004442 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004443 switch (ctxt->op_bytes) {
4444 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4445 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4446 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004447 }
4448 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004449 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004450 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004451 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004452 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004453 rc = emulate_int(ctxt, 3);
4454 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004455 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004456 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004457 break;
4458 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004459 if (ctxt->eflags & EFLG_OF)
4460 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004461 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004462 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004463 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004464 break;
4465 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004466 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004467 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004468 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004469 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004470 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004471 jmp_rel(ctxt, ctxt->src.val);
4472 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004473 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004474 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004475 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004476 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004477 case 0xf5: /* cmc */
4478 /* complement carry flag from eflags reg */
4479 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004480 break;
4481 case 0xf8: /* clc */
4482 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004483 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004484 case 0xf9: /* stc */
4485 ctxt->eflags |= EFLG_CF;
4486 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004487 case 0xfc: /* cld */
4488 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004489 break;
4490 case 0xfd: /* std */
4491 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004492 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004493 default:
4494 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004495 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004496
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004497 if (rc != X86EMUL_CONTINUE)
4498 goto done;
4499
Avi Kivity018a98d2007-11-27 19:30:56 +02004500writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004501 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004502 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004503 goto done;
4504
Gleb Natapov5cd21912010-03-18 15:20:26 +02004505 /*
4506 * restore dst type in case the decoding will be reused
4507 * (happens for string instruction )
4508 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004509 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004510
Avi Kivity9dac77f2011-06-01 15:34:25 +03004511 if ((ctxt->d & SrcMask) == SrcSI)
4512 string_addr_inc(ctxt, seg_override(ctxt),
4513 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004514
Avi Kivity9dac77f2011-06-01 15:34:25 +03004515 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004516 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004517 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004518
Avi Kivity9dac77f2011-06-01 15:34:25 +03004519 if (ctxt->rep_prefix && (ctxt->d & String)) {
4520 struct read_cache *r = &ctxt->io_read;
4521 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004522
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004523 if (!string_insn_completed(ctxt)) {
4524 /*
4525 * Re-enter guest when pio read ahead buffer is empty
4526 * or, if it is not used, after each 1024 iteration.
4527 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004528 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004529 (r->end == 0 || r->end != r->pos)) {
4530 /*
4531 * Reset read cache. Usually happens before
4532 * decode, but since instruction is restarted
4533 * we have to do it here.
4534 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004535 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004536 return EMULATION_RESTART;
4537 }
4538 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004539 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004540 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004541
Avi Kivity9dac77f2011-06-01 15:34:25 +03004542 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004543
4544done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004545 if (rc == X86EMUL_PROPAGATE_FAULT)
4546 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004547 if (rc == X86EMUL_INTERCEPTED)
4548 return EMULATION_INTERCEPTED;
4549
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004550 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551
4552twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004553 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004554 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004555 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004556 break;
4557 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004558 case 0x0d: /* GrpP (prefetch) */
4559 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004560 break;
4561 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004562 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004563 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004565 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004567 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004568 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4569 if (!test_cc(ctxt->b, ctxt->eflags))
4570 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004571 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004572 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 if (test_cc(ctxt->b, ctxt->eflags))
4574 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004575 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004576 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004577 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004578 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004579 case 0xa4: /* shld imm8, r, r/m */
4580 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004581 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004582 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004583 case 0xac: /* shrd imm8, r, r/m */
4584 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004585 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004586 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004587 case 0xae: /* clflush */
4588 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004589 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004590 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004591 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004592 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004593 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004594 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004595 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004596 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004597 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004598 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004599 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004600 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004601 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 ctxt->src.val = ctxt->dst.orig_val;
4603 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004604 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004605 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004606 ctxt->dst.bytes = ctxt->op_bytes;
4607 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4608 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004609 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004610 default:
4611 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004613
4614 if (rc != X86EMUL_CONTINUE)
4615 goto done;
4616
Avi Kivity6aa8b732006-12-10 02:21:36 -08004617 goto writeback;
4618
4619cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004620 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004621}