blob: 24c84251648b2306b7b4f383c0bc33768c199fb8 [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 Kivity9dac77f2011-06-01 15:34:25 +0300436static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800437{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300438 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800439}
440
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800442static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300443address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800444{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300445 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800446 return reg;
447 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800449}
450
451static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300452register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800453{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300454 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800455}
456
Harvey Harrison7a9572752008-02-19 07:40:41 -0800457static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300458register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800459{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300460 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800461 *reg += inc;
462 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300463 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800464}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465
Avi Kivity9dac77f2011-06-01 15:34:25 +0300466static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800467{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300468 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800469}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300470
Avi Kivity56697682011-04-03 14:08:51 +0300471static u32 desc_limit_scaled(struct desc_struct *desc)
472{
473 u32 limit = get_desc_limit(desc);
474
475 return desc->g ? (limit << 12) | 0xfff : limit;
476}
477
Avi Kivity9dac77f2011-06-01 15:34:25 +0300478static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300479{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300480 ctxt->has_seg_override = true;
481 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300482}
483
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900484static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300485{
486 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
487 return 0;
488
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900489 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300490}
491
Avi Kivity9dac77f2011-06-01 15:34:25 +0300492static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300493{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300494 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300495 return 0;
496
Avi Kivity9dac77f2011-06-01 15:34:25 +0300497 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300498}
499
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200500static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
501 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300502{
Avi Kivityda9cb572010-11-22 17:53:21 +0200503 ctxt->exception.vector = vec;
504 ctxt->exception.error_code = error;
505 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200506 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300507}
508
Joerg Roedel3b88e412011-04-04 12:39:29 +0200509static int emulate_db(struct x86_emulate_ctxt *ctxt)
510{
511 return emulate_exception(ctxt, DB_VECTOR, 0, false);
512}
513
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200514static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300515{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200516 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300517}
518
Avi Kivity618ff152011-04-03 12:32:09 +0300519static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
520{
521 return emulate_exception(ctxt, SS_VECTOR, err, true);
522}
523
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200524static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300525{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200526 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300527}
528
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200529static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300530{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200531 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300532}
533
Avi Kivity34d1f492010-08-26 11:59:01 +0300534static int emulate_de(struct x86_emulate_ctxt *ctxt)
535{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200536 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300537}
538
Avi Kivity12537912011-03-29 11:41:27 +0200539static int emulate_nm(struct x86_emulate_ctxt *ctxt)
540{
541 return emulate_exception(ctxt, NM_VECTOR, 0, false);
542}
543
Avi Kivity1aa36612011-04-27 13:20:30 +0300544static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
545{
546 u16 selector;
547 struct desc_struct desc;
548
549 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
550 return selector;
551}
552
553static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
554 unsigned seg)
555{
556 u16 dummy;
557 u32 base3;
558 struct desc_struct desc;
559
560 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
561 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
562}
563
Avi Kivity1c11b372012-04-09 18:39:59 +0300564/*
565 * x86 defines three classes of vector instructions: explicitly
566 * aligned, explicitly unaligned, and the rest, which change behaviour
567 * depending on whether they're AVX encoded or not.
568 *
569 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
570 * subject to the same check.
571 */
572static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
573{
574 if (likely(size < 16))
575 return false;
576
577 if (ctxt->d & Aligned)
578 return true;
579 else if (ctxt->d & Unaligned)
580 return false;
581 else if (ctxt->d & Avx)
582 return false;
583 else
584 return true;
585}
586
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400587static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300588 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400589 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300590 ulong *linear)
591{
Avi Kivity618ff152011-04-03 12:32:09 +0300592 struct desc_struct desc;
593 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300594 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300595 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300596 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300597 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300598
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900599 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300600 switch (ctxt->mode) {
601 case X86EMUL_MODE_REAL:
602 break;
603 case X86EMUL_MODE_PROT64:
604 if (((signed long)la << 16) >> 16 != la)
605 return emulate_gp(ctxt, 0);
606 break;
607 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300608 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
609 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300610 if (!usable)
611 goto bad;
612 /* code segment or read-only data segment */
613 if (((desc.type & 8) || !(desc.type & 2)) && write)
614 goto bad;
615 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400616 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300617 goto bad;
618 lim = desc_limit_scaled(&desc);
619 if ((desc.type & 8) || !(desc.type & 4)) {
620 /* expand-up segment */
621 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
622 goto bad;
623 } else {
624 /* exapand-down segment */
625 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
626 goto bad;
627 lim = desc.d ? 0xffffffff : 0xffff;
628 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
629 goto bad;
630 }
Avi Kivity717746e2011-04-20 13:37:53 +0300631 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300632 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300633 cpl = max(cpl, rpl);
634 if (!(desc.type & 8)) {
635 /* data segment */
636 if (cpl > desc.dpl)
637 goto bad;
638 } else if ((desc.type & 8) && !(desc.type & 4)) {
639 /* nonconforming code segment */
640 if (cpl != desc.dpl)
641 goto bad;
642 } else if ((desc.type & 8) && (desc.type & 4)) {
643 /* conforming code segment */
644 if (cpl < desc.dpl)
645 goto bad;
646 }
647 break;
648 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300649 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300650 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300651 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
652 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300653 *linear = la;
654 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300655bad:
656 if (addr.seg == VCPU_SREG_SS)
657 return emulate_ss(ctxt, addr.seg);
658 else
659 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300660}
661
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400662static int linearize(struct x86_emulate_ctxt *ctxt,
663 struct segmented_address addr,
664 unsigned size, bool write,
665 ulong *linear)
666{
667 return __linearize(ctxt, addr, size, write, false, linear);
668}
669
670
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200671static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
672 struct segmented_address addr,
673 void *data,
674 unsigned size)
675{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200676 int rc;
677 ulong linear;
678
Avi Kivity83b87952011-04-03 11:31:19 +0300679 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200680 if (rc != X86EMUL_CONTINUE)
681 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300682 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200683}
684
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900685/*
686 * Fetch the next byte of the instruction being emulated which is pointed to
687 * by ctxt->_eip, then increment ctxt->_eip.
688 *
689 * Also prefetch the remaining bytes of the instruction without crossing page
690 * boundary if they are not in fetch_cache yet.
691 */
692static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200693{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300694 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200695 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300696 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200697
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900698 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400699 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900700 struct segmented_address addr = { .seg = VCPU_SREG_CS,
701 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300702 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900703 size = min(15UL - cur_size,
704 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400705 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900706 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400707 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900708 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
709 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900710 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200711 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300712 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200713 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900714 *dest = fc->data[ctxt->_eip - fc->start];
715 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900716 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200717}
718
719static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900720 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200721{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900722 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200723
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200724 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900725 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200726 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200727 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900728 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900729 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200730 return rc;
731 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900732 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200733}
734
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900735/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900736#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900737({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900738 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900739 if (rc != X86EMUL_CONTINUE) \
740 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900741 (_type)_x; \
742})
743
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900744#define insn_fetch_arr(_arr, _size, _ctxt) \
745({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900746 if (rc != X86EMUL_CONTINUE) \
747 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900748})
749
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000750/*
751 * Given the 'reg' portion of a ModRM byte, and a register block, return a
752 * pointer into the block that addresses the relevant register.
753 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
754 */
755static void *decode_register(u8 modrm_reg, unsigned long *regs,
756 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757{
758 void *p;
759
760 p = &regs[modrm_reg];
761 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
762 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
763 return p;
764}
765
766static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200767 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 u16 *size, unsigned long *address, int op_bytes)
769{
770 int rc;
771
772 if (op_bytes == 2)
773 op_bytes = 3;
774 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200775 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900776 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200778 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200779 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 return rc;
781}
782
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300783static int test_cc(unsigned int condition, unsigned int flags)
784{
785 int rc = 0;
786
787 switch ((condition & 15) >> 1) {
788 case 0: /* o */
789 rc |= (flags & EFLG_OF);
790 break;
791 case 1: /* b/c/nae */
792 rc |= (flags & EFLG_CF);
793 break;
794 case 2: /* z/e */
795 rc |= (flags & EFLG_ZF);
796 break;
797 case 3: /* be/na */
798 rc |= (flags & (EFLG_CF|EFLG_ZF));
799 break;
800 case 4: /* s */
801 rc |= (flags & EFLG_SF);
802 break;
803 case 5: /* p/pe */
804 rc |= (flags & EFLG_PF);
805 break;
806 case 7: /* le/ng */
807 rc |= (flags & EFLG_ZF);
808 /* fall through */
809 case 6: /* l/nge */
810 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
811 break;
812 }
813
814 /* Odd condition identifiers (lsb == 1) have inverted sense. */
815 return (!!rc ^ (condition & 1));
816}
817
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300818static void fetch_register_operand(struct operand *op)
819{
820 switch (op->bytes) {
821 case 1:
822 op->val = *(u8 *)op->addr.reg;
823 break;
824 case 2:
825 op->val = *(u16 *)op->addr.reg;
826 break;
827 case 4:
828 op->val = *(u32 *)op->addr.reg;
829 break;
830 case 8:
831 op->val = *(u64 *)op->addr.reg;
832 break;
833 }
834}
835
Avi Kivity12537912011-03-29 11:41:27 +0200836static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
837{
838 ctxt->ops->get_fpu(ctxt);
839 switch (reg) {
840 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
841 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
842 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
843 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
844 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
845 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
846 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
847 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
848#ifdef CONFIG_X86_64
849 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
850 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
851 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
852 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
853 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
854 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
855 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
856 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
857#endif
858 default: BUG();
859 }
860 ctxt->ops->put_fpu(ctxt);
861}
862
863static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
864 int reg)
865{
866 ctxt->ops->get_fpu(ctxt);
867 switch (reg) {
868 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
869 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
870 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
871 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
872 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
873 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
874 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
875 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
876#ifdef CONFIG_X86_64
877 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
878 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
879 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
880 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
881 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
882 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
883 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
884 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
885#endif
886 default: BUG();
887 }
888 ctxt->ops->put_fpu(ctxt);
889}
890
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300891static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
892{
893 ctxt->ops->get_fpu(ctxt);
894 switch (reg) {
895 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
896 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
897 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
898 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
899 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
900 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
901 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
902 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
903 default: BUG();
904 }
905 ctxt->ops->put_fpu(ctxt);
906}
907
908static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
909{
910 ctxt->ops->get_fpu(ctxt);
911 switch (reg) {
912 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
913 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
914 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
915 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
916 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
917 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
918 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
919 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
920 default: BUG();
921 }
922 ctxt->ops->put_fpu(ctxt);
923}
924
Avi Kivity12537912011-03-29 11:41:27 +0200925static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200926 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200927{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300928 unsigned reg = ctxt->modrm_reg;
929 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200930
Avi Kivity9dac77f2011-06-01 15:34:25 +0300931 if (!(ctxt->d & ModRM))
932 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200933
Avi Kivity9dac77f2011-06-01 15:34:25 +0300934 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200935 op->type = OP_XMM;
936 op->bytes = 16;
937 op->addr.xmm = reg;
938 read_sse_reg(ctxt, &op->vec_val, reg);
939 return;
940 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300941 if (ctxt->d & Mmx) {
942 reg &= 7;
943 op->type = OP_MM;
944 op->bytes = 8;
945 op->addr.mm = reg;
946 return;
947 }
Avi Kivity12537912011-03-29 11:41:27 +0200948
Avi Kivity3c118e22007-10-31 10:27:04 +0200949 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200950 if (ctxt->d & ByteOp) {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300951 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200952 op->bytes = 1;
953 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300954 op->addr.reg = decode_register(reg, ctxt->regs, 0);
955 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200956 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300957 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200958 op->orig_val = op->val;
959}
960
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200961static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300962 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200963{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200964 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700965 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900966 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300967 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200968
Avi Kivity9dac77f2011-06-01 15:34:25 +0300969 if (ctxt->rex_prefix) {
970 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
971 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
972 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200973 }
974
Avi Kivity9dac77f2011-06-01 15:34:25 +0300975 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
976 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
977 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
978 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200979
Avi Kivity9dac77f2011-06-01 15:34:25 +0300980 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300981 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300982 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
983 op->addr.reg = decode_register(ctxt->modrm_rm,
984 ctxt->regs, ctxt->d & ByteOp);
985 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200986 op->type = OP_XMM;
987 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300988 op->addr.xmm = ctxt->modrm_rm;
989 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200990 return rc;
991 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300992 if (ctxt->d & Mmx) {
993 op->type = OP_MM;
994 op->bytes = 8;
995 op->addr.xmm = ctxt->modrm_rm & 7;
996 return rc;
997 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300998 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200999 return rc;
1000 }
1001
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001002 op->type = OP_MEM;
1003
Avi Kivity9dac77f2011-06-01 15:34:25 +03001004 if (ctxt->ad_bytes == 2) {
1005 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
1006 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
1007 unsigned si = ctxt->regs[VCPU_REGS_RSI];
1008 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001009
1010 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001011 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001012 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001013 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001014 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001015 break;
1016 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001017 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001018 break;
1019 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001020 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001021 break;
1022 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001023 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001024 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001025 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001026 break;
1027 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001028 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001029 break;
1030 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001031 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001032 break;
1033 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001034 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001035 break;
1036 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001037 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001038 break;
1039 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001040 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001041 break;
1042 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001043 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001044 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045 break;
1046 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001047 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001048 break;
1049 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001050 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1051 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1052 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001053 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001054 } else {
1055 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001056 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001057 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001058 index_reg |= (sib >> 3) & 7;
1059 base_reg |= sib & 7;
1060 scale = sib >> 6;
1061
Avi Kivity9dac77f2011-06-01 15:34:25 +03001062 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001063 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001064 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03001065 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001066 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001067 modrm_ea += ctxt->regs[index_reg] << scale;
1068 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001069 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001070 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -07001071 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +03001072 modrm_ea += ctxt->regs[ctxt->modrm_rm];
1073 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001074 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001075 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001076 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001077 break;
1078 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001079 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001080 break;
1081 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001082 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001083 break;
1084 }
1085 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001086 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001087done:
1088 return rc;
1089}
1090
1091static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001092 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001093{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001094 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001095
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001096 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001097 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001098 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001099 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001100 break;
1101 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001102 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001103 break;
1104 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001105 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001106 break;
1107 }
1108done:
1109 return rc;
1110}
1111
Avi Kivity9dac77f2011-06-01 15:34:25 +03001112static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001113{
Sheng Yang7129eec2010-09-28 16:33:32 +08001114 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001115
Avi Kivity9dac77f2011-06-01 15:34:25 +03001116 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1117 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001118
Avi Kivity9dac77f2011-06-01 15:34:25 +03001119 if (ctxt->src.bytes == 2)
1120 sv = (s16)ctxt->src.val & (s16)mask;
1121 else if (ctxt->src.bytes == 4)
1122 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001123
Avi Kivity9dac77f2011-06-01 15:34:25 +03001124 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001125 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001126
1127 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001128 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001129}
1130
Gleb Natapov9de41572010-04-28 19:15:22 +03001131static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001132 unsigned long addr, void *dest, unsigned size)
1133{
1134 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001136
1137 while (size) {
1138 int n = min(size, 8u);
1139 size -= n;
1140 if (mc->pos < mc->end)
1141 goto read_cached;
1142
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001143 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1144 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001145 if (rc != X86EMUL_CONTINUE)
1146 return rc;
1147 mc->end += n;
1148
1149 read_cached:
1150 memcpy(dest, mc->data + mc->pos, n);
1151 mc->pos += n;
1152 dest += n;
1153 addr += n;
1154 }
1155 return X86EMUL_CONTINUE;
1156}
1157
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001158static int segmented_read(struct x86_emulate_ctxt *ctxt,
1159 struct segmented_address addr,
1160 void *data,
1161 unsigned size)
1162{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001163 int rc;
1164 ulong linear;
1165
Avi Kivity83b87952011-04-03 11:31:19 +03001166 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001167 if (rc != X86EMUL_CONTINUE)
1168 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001169 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001170}
1171
1172static int segmented_write(struct x86_emulate_ctxt *ctxt,
1173 struct segmented_address addr,
1174 const void *data,
1175 unsigned size)
1176{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001177 int rc;
1178 ulong linear;
1179
Avi Kivity83b87952011-04-03 11:31:19 +03001180 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001181 if (rc != X86EMUL_CONTINUE)
1182 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001183 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1184 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001185}
1186
1187static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1188 struct segmented_address addr,
1189 const void *orig_data, const 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, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001196 if (rc != X86EMUL_CONTINUE)
1197 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001198 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1199 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001200}
1201
Gleb Natapov7b262e92010-03-18 15:20:27 +02001202static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001203 unsigned int size, unsigned short port,
1204 void *dest)
1205{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001206 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001207
1208 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001209 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001210 unsigned int count = ctxt->rep_prefix ?
1211 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001212 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1214 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001215 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1216 count);
1217 if (n == 0)
1218 n = 1;
1219 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001220 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001221 return 0;
1222 rc->end = n * size;
1223 }
1224
1225 memcpy(dest, rc->data + rc->pos, size);
1226 rc->pos += size;
1227 return 1;
1228}
1229
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001230static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1231 u16 index, struct desc_struct *desc)
1232{
1233 struct desc_ptr dt;
1234 ulong addr;
1235
1236 ctxt->ops->get_idt(ctxt, &dt);
1237
1238 if (dt.size < index * 8 + 7)
1239 return emulate_gp(ctxt, index << 3 | 0x2);
1240
1241 addr = dt.address + index * 8;
1242 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1243 &ctxt->exception);
1244}
1245
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001246static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001247 u16 selector, struct desc_ptr *dt)
1248{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001249 struct x86_emulate_ops *ops = ctxt->ops;
1250
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001251 if (selector & 1 << 2) {
1252 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001253 u16 sel;
1254
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001255 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001256 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001257 return;
1258
1259 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1260 dt->address = get_desc_base(&desc);
1261 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001262 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001263}
1264
1265/* allowed just for 8 bytes segments */
1266static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001267 u16 selector, struct desc_struct *desc)
1268{
1269 struct desc_ptr dt;
1270 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001271 ulong addr;
1272
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001273 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001274
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001275 if (dt.size < index * 8 + 7)
1276 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001277
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001278 addr = dt.address + index * 8;
1279 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1280 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001281}
1282
1283/* allowed just for 8 bytes segments */
1284static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001285 u16 selector, struct desc_struct *desc)
1286{
1287 struct desc_ptr dt;
1288 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001289 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001290
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001291 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001292
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001293 if (dt.size < index * 8 + 7)
1294 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001295
1296 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001297 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1298 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001299}
1300
Gleb Natapov5601d052011-03-07 14:55:06 +02001301/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001302static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001303 u16 selector, int seg)
1304{
1305 struct desc_struct seg_desc;
1306 u8 dpl, rpl, cpl;
1307 unsigned err_vec = GP_VECTOR;
1308 u32 err_code = 0;
1309 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1310 int ret;
1311
1312 memset(&seg_desc, 0, sizeof seg_desc);
1313
1314 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1315 || ctxt->mode == X86EMUL_MODE_REAL) {
1316 /* set real mode segment descriptor */
1317 set_desc_base(&seg_desc, selector << 4);
1318 set_desc_limit(&seg_desc, 0xffff);
1319 seg_desc.type = 3;
1320 seg_desc.p = 1;
1321 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001322 if (ctxt->mode == X86EMUL_MODE_VM86)
1323 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001324 goto load;
1325 }
1326
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001327 rpl = selector & 3;
1328 cpl = ctxt->ops->cpl(ctxt);
1329
1330 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1331 if ((seg == VCPU_SREG_CS
1332 || (seg == VCPU_SREG_SS
1333 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1334 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001335 && null_selector)
1336 goto exception;
1337
1338 /* TR should be in GDT only */
1339 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1340 goto exception;
1341
1342 if (null_selector) /* for NULL selector skip all following checks */
1343 goto load;
1344
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001345 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001346 if (ret != X86EMUL_CONTINUE)
1347 return ret;
1348
1349 err_code = selector & 0xfffc;
1350 err_vec = GP_VECTOR;
1351
1352 /* can't load system descriptor into segment selecor */
1353 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1354 goto exception;
1355
1356 if (!seg_desc.p) {
1357 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1358 goto exception;
1359 }
1360
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001361 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001362
1363 switch (seg) {
1364 case VCPU_SREG_SS:
1365 /*
1366 * segment is not a writable data segment or segment
1367 * selector's RPL != CPL or segment selector's RPL != CPL
1368 */
1369 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1370 goto exception;
1371 break;
1372 case VCPU_SREG_CS:
1373 if (!(seg_desc.type & 8))
1374 goto exception;
1375
1376 if (seg_desc.type & 4) {
1377 /* conforming */
1378 if (dpl > cpl)
1379 goto exception;
1380 } else {
1381 /* nonconforming */
1382 if (rpl > cpl || dpl != cpl)
1383 goto exception;
1384 }
1385 /* CS(RPL) <- CPL */
1386 selector = (selector & 0xfffc) | cpl;
1387 break;
1388 case VCPU_SREG_TR:
1389 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1390 goto exception;
1391 break;
1392 case VCPU_SREG_LDTR:
1393 if (seg_desc.s || seg_desc.type != 2)
1394 goto exception;
1395 break;
1396 default: /* DS, ES, FS, or GS */
1397 /*
1398 * segment is not a data or readable code segment or
1399 * ((segment is a data or nonconforming code segment)
1400 * and (both RPL and CPL > DPL))
1401 */
1402 if ((seg_desc.type & 0xa) == 0x8 ||
1403 (((seg_desc.type & 0xc) != 0xc) &&
1404 (rpl > dpl && cpl > dpl)))
1405 goto exception;
1406 break;
1407 }
1408
1409 if (seg_desc.s) {
1410 /* mark segment as accessed */
1411 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001412 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001413 if (ret != X86EMUL_CONTINUE)
1414 return ret;
1415 }
1416load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001417 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001418 return X86EMUL_CONTINUE;
1419exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001420 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001421 return X86EMUL_PROPAGATE_FAULT;
1422}
1423
Wei Yongjun31be40b2010-08-17 09:17:30 +08001424static void write_register_operand(struct operand *op)
1425{
1426 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1427 switch (op->bytes) {
1428 case 1:
1429 *(u8 *)op->addr.reg = (u8)op->val;
1430 break;
1431 case 2:
1432 *(u16 *)op->addr.reg = (u16)op->val;
1433 break;
1434 case 4:
1435 *op->addr.reg = (u32)op->val;
1436 break; /* 64b: zero-extend */
1437 case 8:
1438 *op->addr.reg = op->val;
1439 break;
1440 }
1441}
1442
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001443static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001444{
1445 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001446
Avi Kivity9dac77f2011-06-01 15:34:25 +03001447 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001448 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001449 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001450 break;
1451 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001452 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001453 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001454 ctxt->dst.addr.mem,
1455 &ctxt->dst.orig_val,
1456 &ctxt->dst.val,
1457 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001458 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001459 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001460 ctxt->dst.addr.mem,
1461 &ctxt->dst.val,
1462 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001463 if (rc != X86EMUL_CONTINUE)
1464 return rc;
1465 break;
Avi Kivity12537912011-03-29 11:41:27 +02001466 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001467 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001468 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001469 case OP_MM:
1470 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1471 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001472 case OP_NONE:
1473 /* no writeback */
1474 break;
1475 default:
1476 break;
1477 }
1478 return X86EMUL_CONTINUE;
1479}
1480
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001481static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001482{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001483 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001484
Avi Kivity9dac77f2011-06-01 15:34:25 +03001485 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1486 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001487 addr.seg = VCPU_SREG_SS;
1488
1489 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001490 ctxt->dst.type = OP_NONE;
1491 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001492}
1493
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001494static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001495 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001496{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001497 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001498 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001499
Avi Kivity9dac77f2011-06-01 15:34:25 +03001500 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001501 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001502 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001503 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001504 return rc;
1505
Avi Kivity9dac77f2011-06-01 15:34:25 +03001506 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001507 return rc;
1508}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001509
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001510static int em_pop(struct x86_emulate_ctxt *ctxt)
1511{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001512 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001513}
1514
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001515static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001516 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001517{
1518 int rc;
1519 unsigned long val, change_mask;
1520 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001521 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001522
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001523 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001524 if (rc != X86EMUL_CONTINUE)
1525 return rc;
1526
1527 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1528 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1529
1530 switch(ctxt->mode) {
1531 case X86EMUL_MODE_PROT64:
1532 case X86EMUL_MODE_PROT32:
1533 case X86EMUL_MODE_PROT16:
1534 if (cpl == 0)
1535 change_mask |= EFLG_IOPL;
1536 if (cpl <= iopl)
1537 change_mask |= EFLG_IF;
1538 break;
1539 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001540 if (iopl < 3)
1541 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001542 change_mask |= EFLG_IF;
1543 break;
1544 default: /* real mode */
1545 change_mask |= (EFLG_IOPL | EFLG_IF);
1546 break;
1547 }
1548
1549 *(unsigned long *)dest =
1550 (ctxt->eflags & ~change_mask) | (val & change_mask);
1551
1552 return rc;
1553}
1554
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001555static int em_popf(struct x86_emulate_ctxt *ctxt)
1556{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001557 ctxt->dst.type = OP_REG;
1558 ctxt->dst.addr.reg = &ctxt->eflags;
1559 ctxt->dst.bytes = ctxt->op_bytes;
1560 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001561}
1562
Avi Kivity1cd196e2011-09-13 10:45:51 +03001563static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001564{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001565 int seg = ctxt->src2.val;
1566
Avi Kivity9dac77f2011-06-01 15:34:25 +03001567 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001568
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001569 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001570}
1571
Avi Kivity1cd196e2011-09-13 10:45:51 +03001572static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001573{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001574 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001575 unsigned long selector;
1576 int rc;
1577
Avi Kivity9dac77f2011-06-01 15:34:25 +03001578 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001579 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001580 return rc;
1581
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001582 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001583 return rc;
1584}
1585
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001586static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001587{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001588 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001589 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001590 int reg = VCPU_REGS_RAX;
1591
1592 while (reg <= VCPU_REGS_RDI) {
1593 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001594 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001595
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001596 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001597 if (rc != X86EMUL_CONTINUE)
1598 return rc;
1599
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001600 ++reg;
1601 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001602
Wei Yongjunc37eda12010-06-15 09:03:33 +08001603 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001604}
1605
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001606static int em_pushf(struct x86_emulate_ctxt *ctxt)
1607{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001608 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001609 return em_push(ctxt);
1610}
1611
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001612static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001613{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001614 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001615 int reg = VCPU_REGS_RDI;
1616
1617 while (reg >= VCPU_REGS_RAX) {
1618 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001619 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1620 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001621 --reg;
1622 }
1623
Avi Kivity9dac77f2011-06-01 15:34:25 +03001624 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001625 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001626 break;
1627 --reg;
1628 }
1629 return rc;
1630}
1631
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001632int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001633{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001634 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001635 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001636 struct desc_ptr dt;
1637 gva_t cs_addr;
1638 gva_t eip_addr;
1639 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001640
1641 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001642 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001643 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001644 if (rc != X86EMUL_CONTINUE)
1645 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001646
1647 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1648
Avi Kivity9dac77f2011-06-01 15:34:25 +03001649 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001650 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001651 if (rc != X86EMUL_CONTINUE)
1652 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001653
Avi Kivity9dac77f2011-06-01 15:34:25 +03001654 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001655 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001656 if (rc != X86EMUL_CONTINUE)
1657 return rc;
1658
Avi Kivity4bff1e862011-04-20 13:37:53 +03001659 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001660
1661 eip_addr = dt.address + (irq << 2);
1662 cs_addr = dt.address + (irq << 2) + 2;
1663
Avi Kivity0f65dd72011-04-20 13:37:53 +03001664 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001665 if (rc != X86EMUL_CONTINUE)
1666 return rc;
1667
Avi Kivity0f65dd72011-04-20 13:37:53 +03001668 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001669 if (rc != X86EMUL_CONTINUE)
1670 return rc;
1671
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001672 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001673 if (rc != X86EMUL_CONTINUE)
1674 return rc;
1675
Avi Kivity9dac77f2011-06-01 15:34:25 +03001676 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001677
1678 return rc;
1679}
1680
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001681static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001682{
1683 switch(ctxt->mode) {
1684 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001685 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001686 case X86EMUL_MODE_VM86:
1687 case X86EMUL_MODE_PROT16:
1688 case X86EMUL_MODE_PROT32:
1689 case X86EMUL_MODE_PROT64:
1690 default:
1691 /* Protected mode interrupts unimplemented yet */
1692 return X86EMUL_UNHANDLEABLE;
1693 }
1694}
1695
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001696static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001697{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001698 int rc = X86EMUL_CONTINUE;
1699 unsigned long temp_eip = 0;
1700 unsigned long temp_eflags = 0;
1701 unsigned long cs = 0;
1702 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1703 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1704 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1705 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1706
1707 /* TODO: Add stack limit check */
1708
Avi Kivity9dac77f2011-06-01 15:34:25 +03001709 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001710
1711 if (rc != X86EMUL_CONTINUE)
1712 return rc;
1713
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001714 if (temp_eip & ~0xffff)
1715 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001716
Avi Kivity9dac77f2011-06-01 15:34:25 +03001717 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001718
1719 if (rc != X86EMUL_CONTINUE)
1720 return rc;
1721
Avi Kivity9dac77f2011-06-01 15:34:25 +03001722 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001723
1724 if (rc != X86EMUL_CONTINUE)
1725 return rc;
1726
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001727 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001728
1729 if (rc != X86EMUL_CONTINUE)
1730 return rc;
1731
Avi Kivity9dac77f2011-06-01 15:34:25 +03001732 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001733
1734
Avi Kivity9dac77f2011-06-01 15:34:25 +03001735 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001736 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001737 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001738 ctxt->eflags &= ~0xffff;
1739 ctxt->eflags |= temp_eflags;
1740 }
1741
1742 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1743 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1744
1745 return rc;
1746}
1747
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001748static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001749{
1750 switch(ctxt->mode) {
1751 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001752 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001753 case X86EMUL_MODE_VM86:
1754 case X86EMUL_MODE_PROT16:
1755 case X86EMUL_MODE_PROT32:
1756 case X86EMUL_MODE_PROT64:
1757 default:
1758 /* iret from protected mode unimplemented yet */
1759 return X86EMUL_UNHANDLEABLE;
1760 }
1761}
1762
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001763static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1764{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001765 int rc;
1766 unsigned short sel;
1767
Avi Kivity9dac77f2011-06-01 15:34:25 +03001768 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001769
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001770 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001771 if (rc != X86EMUL_CONTINUE)
1772 return rc;
1773
Avi Kivity9dac77f2011-06-01 15:34:25 +03001774 ctxt->_eip = 0;
1775 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001776 return X86EMUL_CONTINUE;
1777}
1778
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001779static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001780{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001781 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001783 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784 break;
1785 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001786 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001787 break;
1788 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001789 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001790 break;
1791 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001792 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793 break;
1794 case 4: /* sal/shl */
1795 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001796 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001797 break;
1798 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001799 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800 break;
1801 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001802 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001803 break;
1804 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001805 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001806}
1807
Avi Kivity3329ece2011-09-13 10:45:39 +03001808static int em_not(struct x86_emulate_ctxt *ctxt)
1809{
1810 ctxt->dst.val = ~ctxt->dst.val;
1811 return X86EMUL_CONTINUE;
1812}
1813
1814static int em_neg(struct x86_emulate_ctxt *ctxt)
1815{
1816 emulate_1op(ctxt, "neg");
1817 return X86EMUL_CONTINUE;
1818}
1819
1820static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1821{
1822 u8 ex = 0;
1823
1824 emulate_1op_rax_rdx(ctxt, "mul", ex);
1825 return X86EMUL_CONTINUE;
1826}
1827
1828static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1829{
1830 u8 ex = 0;
1831
1832 emulate_1op_rax_rdx(ctxt, "imul", ex);
1833 return X86EMUL_CONTINUE;
1834}
1835
1836static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001837{
Avi Kivity34d1f492010-08-26 11:59:01 +03001838 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001839
Avi Kivity3329ece2011-09-13 10:45:39 +03001840 emulate_1op_rax_rdx(ctxt, "div", de);
1841 if (de)
1842 return emulate_de(ctxt);
1843 return X86EMUL_CONTINUE;
1844}
1845
1846static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1847{
1848 u8 de = 0;
1849
1850 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001851 if (de)
1852 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001853 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001854}
1855
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001856static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001857{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001858 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001859
Avi Kivity9dac77f2011-06-01 15:34:25 +03001860 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001861 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001862 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001863 break;
1864 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001865 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001866 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001867 case 2: /* call near abs */ {
1868 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001869 old_eip = ctxt->_eip;
1870 ctxt->_eip = ctxt->src.val;
1871 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001872 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001873 break;
1874 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001875 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001876 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001877 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001878 case 5: /* jmp far */
1879 rc = em_jmp_far(ctxt);
1880 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001881 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001882 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001883 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001884 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001885 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001886}
1887
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001888static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001889{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001890 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001891
Avi Kivity9dac77f2011-06-01 15:34:25 +03001892 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1893 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1894 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1895 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001896 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001897 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001898 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1899 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001900
Laurent Vivier05f086f2007-09-24 11:10:55 +02001901 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001902 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001903 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001904}
1905
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001906static int em_ret(struct x86_emulate_ctxt *ctxt)
1907{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 ctxt->dst.type = OP_REG;
1909 ctxt->dst.addr.reg = &ctxt->_eip;
1910 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001911 return em_pop(ctxt);
1912}
1913
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001914static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001915{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001916 int rc;
1917 unsigned long cs;
1918
Avi Kivity9dac77f2011-06-01 15:34:25 +03001919 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001920 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001921 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001922 if (ctxt->op_bytes == 4)
1923 ctxt->_eip = (u32)ctxt->_eip;
1924 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001925 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001926 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001927 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001928 return rc;
1929}
1930
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09001931static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
1932{
1933 /* Save real source value, then compare EAX against destination. */
1934 ctxt->src.orig_val = ctxt->src.val;
1935 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
1936 emulate_2op_SrcV(ctxt, "cmp");
1937
1938 if (ctxt->eflags & EFLG_ZF) {
1939 /* Success: write back to memory. */
1940 ctxt->dst.val = ctxt->src.orig_val;
1941 } else {
1942 /* Failure: write the value we saw to EAX. */
1943 ctxt->dst.type = OP_REG;
1944 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
1945 }
1946 return X86EMUL_CONTINUE;
1947}
1948
Avi Kivityd4b43252011-09-13 10:45:50 +03001949static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001950{
Avi Kivityd4b43252011-09-13 10:45:50 +03001951 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001952 unsigned short sel;
1953 int rc;
1954
Avi Kivity9dac77f2011-06-01 15:34:25 +03001955 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001956
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001957 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001958 if (rc != X86EMUL_CONTINUE)
1959 return rc;
1960
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001962 return rc;
1963}
1964
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001965static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001966setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001967 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001968{
Avi Kivity1aa36612011-04-27 13:20:30 +03001969 u16 selector;
1970
Gleb Natapov79168fd2010-04-28 19:15:30 +03001971 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001972 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001973 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001974
1975 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001976 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001977 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001978 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001979 cs->type = 0x0b; /* Read, Execute, Accessed */
1980 cs->s = 1;
1981 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001982 cs->p = 1;
1983 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001984
Gleb Natapov79168fd2010-04-28 19:15:30 +03001985 set_desc_base(ss, 0); /* flat segment */
1986 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001987 ss->g = 1; /* 4kb granularity */
1988 ss->s = 1;
1989 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001990 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001991 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001992 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001993}
1994
Avi Kivity1a18a692012-02-01 12:23:21 +02001995static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
1996{
1997 u32 eax, ebx, ecx, edx;
1998
1999 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002000 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2001 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002002 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2003 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2004}
2005
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002006static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2007{
2008 struct x86_emulate_ops *ops = ctxt->ops;
2009 u32 eax, ebx, ecx, edx;
2010
2011 /*
2012 * syscall should always be enabled in longmode - so only become
2013 * vendor specific (cpuid) if other modes are active...
2014 */
2015 if (ctxt->mode == X86EMUL_MODE_PROT64)
2016 return true;
2017
2018 eax = 0x00000000;
2019 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002020 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2021 /*
2022 * Intel ("GenuineIntel")
2023 * remark: Intel CPUs only support "syscall" in 64bit
2024 * longmode. Also an 64bit guest with a
2025 * 32bit compat-app running will #UD !! While this
2026 * behaviour can be fixed (by emulating) into AMD
2027 * response - CPUs of AMD can't behave like Intel.
2028 */
2029 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2030 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2031 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2032 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002033
Avi Kivity0017f932012-06-07 14:10:16 +03002034 /* AMD ("AuthenticAMD") */
2035 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2036 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2037 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2038 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002039
Avi Kivity0017f932012-06-07 14:10:16 +03002040 /* AMD ("AMDisbetter!") */
2041 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2042 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2043 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2044 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002045
2046 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2047 return false;
2048}
2049
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002050static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002051{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002052 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002053 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002054 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002055 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002056 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002057
2058 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002059 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002060 ctxt->mode == X86EMUL_MODE_VM86)
2061 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002062
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002063 if (!(em_syscall_is_enabled(ctxt)))
2064 return emulate_ud(ctxt);
2065
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002066 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002067 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002068
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002069 if (!(efer & EFER_SCE))
2070 return emulate_ud(ctxt);
2071
Avi Kivity717746e2011-04-20 13:37:53 +03002072 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002073 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002074 cs_sel = (u16)(msr_data & 0xfffc);
2075 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002076
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002077 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002078 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002079 cs.l = 1;
2080 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002081 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2082 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002083
Avi Kivity9dac77f2011-06-01 15:34:25 +03002084 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002085 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002086#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002088
Avi Kivity717746e2011-04-20 13:37:53 +03002089 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002090 ctxt->mode == X86EMUL_MODE_PROT64 ?
2091 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002092 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002093
Avi Kivity717746e2011-04-20 13:37:53 +03002094 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002095 ctxt->eflags &= ~(msr_data | EFLG_RF);
2096#endif
2097 } else {
2098 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002099 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002100 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002101
2102 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2103 }
2104
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002105 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002106}
2107
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002108static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002109{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002110 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002111 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002112 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002113 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002114 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002115
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002116 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002117 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002118 if (ctxt->mode == X86EMUL_MODE_REAL)
2119 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002120
Avi Kivity1a18a692012-02-01 12:23:21 +02002121 /*
2122 * Not recognized on AMD in compat mode (but is recognized in legacy
2123 * mode).
2124 */
2125 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2126 && !vendor_intel(ctxt))
2127 return emulate_ud(ctxt);
2128
Andre Przywara8c604352009-06-18 12:56:01 +02002129 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2130 * Therefore, we inject an #UD.
2131 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002132 if (ctxt->mode == X86EMUL_MODE_PROT64)
2133 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002134
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002135 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002136
Avi Kivity717746e2011-04-20 13:37:53 +03002137 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002138 switch (ctxt->mode) {
2139 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002140 if ((msr_data & 0xfffc) == 0x0)
2141 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002142 break;
2143 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002144 if (msr_data == 0x0)
2145 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002146 break;
2147 }
2148
2149 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002150 cs_sel = (u16)msr_data;
2151 cs_sel &= ~SELECTOR_RPL_MASK;
2152 ss_sel = cs_sel + 8;
2153 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002154 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002155 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002156 cs.l = 1;
2157 }
2158
Avi Kivity1aa36612011-04-27 13:20:30 +03002159 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2160 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002161
Avi Kivity717746e2011-04-20 13:37:53 +03002162 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002163 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002164
Avi Kivity717746e2011-04-20 13:37:53 +03002165 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002166 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002167
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002168 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002169}
2170
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002171static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002172{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002173 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002174 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002175 u64 msr_data;
2176 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002177 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002178
Gleb Natapova0044752010-02-10 14:21:31 +02002179 /* inject #GP if in real mode or Virtual 8086 mode */
2180 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002181 ctxt->mode == X86EMUL_MODE_VM86)
2182 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002183
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002184 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002185
Avi Kivity9dac77f2011-06-01 15:34:25 +03002186 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002187 usermode = X86EMUL_MODE_PROT64;
2188 else
2189 usermode = X86EMUL_MODE_PROT32;
2190
2191 cs.dpl = 3;
2192 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002193 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002194 switch (usermode) {
2195 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002196 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002197 if ((msr_data & 0xfffc) == 0x0)
2198 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002199 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002200 break;
2201 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002202 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002203 if (msr_data == 0x0)
2204 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002205 ss_sel = cs_sel + 8;
2206 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002207 cs.l = 1;
2208 break;
2209 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002210 cs_sel |= SELECTOR_RPL_MASK;
2211 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002212
Avi Kivity1aa36612011-04-27 13:20:30 +03002213 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2214 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002215
Avi Kivity9dac77f2011-06-01 15:34:25 +03002216 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2217 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002218
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002219 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002220}
2221
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002222static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002223{
2224 int iopl;
2225 if (ctxt->mode == X86EMUL_MODE_REAL)
2226 return false;
2227 if (ctxt->mode == X86EMUL_MODE_VM86)
2228 return true;
2229 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002230 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002231}
2232
2233static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002234 u16 port, u16 len)
2235{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002236 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002237 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002238 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002239 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002240 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002241 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002242 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002243
Avi Kivity1aa36612011-04-27 13:20:30 +03002244 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002245 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002246 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002247 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002248 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002249 base = get_desc_base(&tr_seg);
2250#ifdef CONFIG_X86_64
2251 base |= ((u64)base3) << 32;
2252#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002253 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002254 if (r != X86EMUL_CONTINUE)
2255 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002256 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002257 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002258 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002259 if (r != X86EMUL_CONTINUE)
2260 return false;
2261 if ((perm >> bit_idx) & mask)
2262 return false;
2263 return true;
2264}
2265
2266static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002267 u16 port, u16 len)
2268{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002269 if (ctxt->perm_ok)
2270 return true;
2271
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002272 if (emulator_bad_iopl(ctxt))
2273 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002274 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002275
2276 ctxt->perm_ok = true;
2277
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002278 return true;
2279}
2280
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002281static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282 struct tss_segment_16 *tss)
2283{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002284 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002285 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002286 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2287 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2288 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2289 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2290 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2291 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2292 tss->si = ctxt->regs[VCPU_REGS_RSI];
2293 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294
Avi Kivity1aa36612011-04-27 13:20:30 +03002295 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2296 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2297 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2298 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2299 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002300}
2301
2302static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303 struct tss_segment_16 *tss)
2304{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002305 int ret;
2306
Avi Kivity9dac77f2011-06-01 15:34:25 +03002307 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002308 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002309 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2310 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2311 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2312 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2313 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2314 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2315 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2316 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317
2318 /*
2319 * SDM says that segment selectors are loaded before segment
2320 * descriptors
2321 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002322 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2323 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2324 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2325 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2326 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327
2328 /*
2329 * Now load segment descriptors. If fault happenes at this stage
2330 * it is handled in a context of new task
2331 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002332 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 if (ret != X86EMUL_CONTINUE)
2334 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002335 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002336 if (ret != X86EMUL_CONTINUE)
2337 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002338 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339 if (ret != X86EMUL_CONTINUE)
2340 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002341 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002342 if (ret != X86EMUL_CONTINUE)
2343 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002344 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002345 if (ret != X86EMUL_CONTINUE)
2346 return ret;
2347
2348 return X86EMUL_CONTINUE;
2349}
2350
2351static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002352 u16 tss_selector, u16 old_tss_sel,
2353 ulong old_tss_base, struct desc_struct *new_desc)
2354{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002355 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002356 struct tss_segment_16 tss_seg;
2357 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002358 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002359
Avi Kivity0f65dd72011-04-20 13:37:53 +03002360 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002361 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002362 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002363 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002364 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002365
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002366 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002367
Avi Kivity0f65dd72011-04-20 13:37:53 +03002368 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002369 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002370 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002371 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002372 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002373
Avi Kivity0f65dd72011-04-20 13:37:53 +03002374 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002375 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002376 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002377 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002378 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002379
2380 if (old_tss_sel != 0xffff) {
2381 tss_seg.prev_task_link = old_tss_sel;
2382
Avi Kivity0f65dd72011-04-20 13:37:53 +03002383 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002384 &tss_seg.prev_task_link,
2385 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002386 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002387 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002388 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002389 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002390 }
2391
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002392 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002393}
2394
2395static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002396 struct tss_segment_32 *tss)
2397{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002398 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002399 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002400 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002401 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2402 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2403 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2404 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2405 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2406 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2407 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2408 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409
Avi Kivity1aa36612011-04-27 13:20:30 +03002410 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2411 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2412 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2413 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2414 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2415 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2416 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002417}
2418
2419static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002420 struct tss_segment_32 *tss)
2421{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422 int ret;
2423
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002424 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002425 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002426 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002428
2429 /* General purpose registers */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002430 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2431 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2432 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2433 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2434 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2435 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2436 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2437 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002438
2439 /*
2440 * SDM says that segment selectors are loaded before segment
2441 * descriptors
2442 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002443 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2444 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2445 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2446 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2447 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2448 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2449 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002450
2451 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002452 * If we're switching between Protected Mode and VM86, we need to make
2453 * sure to update the mode before loading the segment descriptors so
2454 * that the selectors are interpreted correctly.
2455 *
2456 * Need to get rflags to the vcpu struct immediately because it
2457 * influences the CPL which is checked at least when loading the segment
2458 * descriptors and when pushing an error code to the new kernel stack.
2459 *
2460 * TODO Introduce a separate ctxt->ops->set_cpl callback
2461 */
2462 if (ctxt->eflags & X86_EFLAGS_VM)
2463 ctxt->mode = X86EMUL_MODE_VM86;
2464 else
2465 ctxt->mode = X86EMUL_MODE_PROT32;
2466
2467 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2468
2469 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002470 * Now load segment descriptors. If fault happenes at this stage
2471 * it is handled in a context of new task
2472 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002473 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002474 if (ret != X86EMUL_CONTINUE)
2475 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002476 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002477 if (ret != X86EMUL_CONTINUE)
2478 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002479 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480 if (ret != X86EMUL_CONTINUE)
2481 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002482 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483 if (ret != X86EMUL_CONTINUE)
2484 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002485 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486 if (ret != X86EMUL_CONTINUE)
2487 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002488 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489 if (ret != X86EMUL_CONTINUE)
2490 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002491 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492 if (ret != X86EMUL_CONTINUE)
2493 return ret;
2494
2495 return X86EMUL_CONTINUE;
2496}
2497
2498static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002499 u16 tss_selector, u16 old_tss_sel,
2500 ulong old_tss_base, struct desc_struct *new_desc)
2501{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002502 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503 struct tss_segment_32 tss_seg;
2504 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002505 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506
Avi Kivity0f65dd72011-04-20 13:37:53 +03002507 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002508 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002509 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002510 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002513 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514
Avi Kivity0f65dd72011-04-20 13:37:53 +03002515 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002516 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002517 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520
Avi Kivity0f65dd72011-04-20 13:37:53 +03002521 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002522 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002523 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526
2527 if (old_tss_sel != 0xffff) {
2528 tss_seg.prev_task_link = old_tss_sel;
2529
Avi Kivity0f65dd72011-04-20 13:37:53 +03002530 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002531 &tss_seg.prev_task_link,
2532 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002533 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002534 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002535 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002536 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002537 }
2538
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002539 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540}
2541
2542static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002543 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002544 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002546 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547 struct desc_struct curr_tss_desc, next_tss_desc;
2548 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002549 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002551 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002552 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553
2554 /* FIXME: old_tss_base == ~0 ? */
2555
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002556 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557 if (ret != X86EMUL_CONTINUE)
2558 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002559 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560 if (ret != X86EMUL_CONTINUE)
2561 return ret;
2562
2563 /* FIXME: check that next_tss_desc is tss */
2564
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002565 /*
2566 * Check privileges. The three cases are task switch caused by...
2567 *
2568 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2569 * 2. Exception/IRQ/iret: No check is performed
2570 * 3. jmp/call to TSS: Check agains DPL of the TSS
2571 */
2572 if (reason == TASK_SWITCH_GATE) {
2573 if (idt_index != -1) {
2574 /* Software interrupts */
2575 struct desc_struct task_gate_desc;
2576 int dpl;
2577
2578 ret = read_interrupt_descriptor(ctxt, idt_index,
2579 &task_gate_desc);
2580 if (ret != X86EMUL_CONTINUE)
2581 return ret;
2582
2583 dpl = task_gate_desc.dpl;
2584 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2585 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2586 }
2587 } else if (reason != TASK_SWITCH_IRET) {
2588 int dpl = next_tss_desc.dpl;
2589 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2590 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 }
2592
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002593
Gleb Natapovceffb452010-03-18 15:20:19 +02002594 desc_limit = desc_limit_scaled(&next_tss_desc);
2595 if (!next_tss_desc.p ||
2596 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2597 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002598 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 return X86EMUL_PROPAGATE_FAULT;
2600 }
2601
2602 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2603 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002604 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 }
2606
2607 if (reason == TASK_SWITCH_IRET)
2608 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2609
2610 /* set back link to prev task only if NT bit is set in eflags
2611 note that old_tss_sel is not used afetr this point */
2612 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2613 old_tss_sel = 0xffff;
2614
2615 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002616 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 old_tss_base, &next_tss_desc);
2618 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002619 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002621 if (ret != X86EMUL_CONTINUE)
2622 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623
2624 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2625 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2626
2627 if (reason != TASK_SWITCH_IRET) {
2628 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002629 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 }
2631
Avi Kivity717746e2011-04-20 13:37:53 +03002632 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002633 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634
Jan Kiszkae269fb22010-04-14 15:51:09 +02002635 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002636 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2637 ctxt->lock_prefix = 0;
2638 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002639 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002640 }
2641
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 return ret;
2643}
2644
2645int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002646 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002647 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 int rc;
2650
Avi Kivity9dac77f2011-06-01 15:34:25 +03002651 ctxt->_eip = ctxt->eip;
2652 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002654 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002655 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002657 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002658 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002660 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661}
2662
Avi Kivity90de84f2010-11-17 15:28:21 +02002663static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002664 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002665{
Gleb Natapova682e352010-03-18 15:20:21 +02002666 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2667
Avi Kivity9dac77f2011-06-01 15:34:25 +03002668 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2669 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002670 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002671}
2672
Avi Kivity7af04fc2010-08-18 14:16:35 +03002673static int em_das(struct x86_emulate_ctxt *ctxt)
2674{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002675 u8 al, old_al;
2676 bool af, cf, old_cf;
2677
2678 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002679 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002680
2681 old_al = al;
2682 old_cf = cf;
2683 cf = false;
2684 af = ctxt->eflags & X86_EFLAGS_AF;
2685 if ((al & 0x0f) > 9 || af) {
2686 al -= 6;
2687 cf = old_cf | (al >= 250);
2688 af = true;
2689 } else {
2690 af = false;
2691 }
2692 if (old_al > 0x99 || old_cf) {
2693 al -= 0x60;
2694 cf = true;
2695 }
2696
Avi Kivity9dac77f2011-06-01 15:34:25 +03002697 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002698 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002699 ctxt->src.type = OP_IMM;
2700 ctxt->src.val = 0;
2701 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002702 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002703 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2704 if (cf)
2705 ctxt->eflags |= X86_EFLAGS_CF;
2706 if (af)
2707 ctxt->eflags |= X86_EFLAGS_AF;
2708 return X86EMUL_CONTINUE;
2709}
2710
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002711static int em_call(struct x86_emulate_ctxt *ctxt)
2712{
2713 long rel = ctxt->src.val;
2714
2715 ctxt->src.val = (unsigned long)ctxt->_eip;
2716 jmp_rel(ctxt, rel);
2717 return em_push(ctxt);
2718}
2719
Avi Kivity0ef753b2010-08-18 14:51:45 +03002720static int em_call_far(struct x86_emulate_ctxt *ctxt)
2721{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002722 u16 sel, old_cs;
2723 ulong old_eip;
2724 int rc;
2725
Avi Kivity1aa36612011-04-27 13:20:30 +03002726 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002727 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002728
Avi Kivity9dac77f2011-06-01 15:34:25 +03002729 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002730 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002731 return X86EMUL_CONTINUE;
2732
Avi Kivity9dac77f2011-06-01 15:34:25 +03002733 ctxt->_eip = 0;
2734 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002735
Avi Kivity9dac77f2011-06-01 15:34:25 +03002736 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002737 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002738 if (rc != X86EMUL_CONTINUE)
2739 return rc;
2740
Avi Kivity9dac77f2011-06-01 15:34:25 +03002741 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002742 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002743}
2744
Avi Kivity40ece7c2010-08-18 15:12:09 +03002745static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2746{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002747 int rc;
2748
Avi Kivity9dac77f2011-06-01 15:34:25 +03002749 ctxt->dst.type = OP_REG;
2750 ctxt->dst.addr.reg = &ctxt->_eip;
2751 ctxt->dst.bytes = ctxt->op_bytes;
2752 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002753 if (rc != X86EMUL_CONTINUE)
2754 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002755 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002756 return X86EMUL_CONTINUE;
2757}
2758
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002759static int em_add(struct x86_emulate_ctxt *ctxt)
2760{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002761 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002762 return X86EMUL_CONTINUE;
2763}
2764
2765static int em_or(struct x86_emulate_ctxt *ctxt)
2766{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002767 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002768 return X86EMUL_CONTINUE;
2769}
2770
2771static int em_adc(struct x86_emulate_ctxt *ctxt)
2772{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002773 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002774 return X86EMUL_CONTINUE;
2775}
2776
2777static int em_sbb(struct x86_emulate_ctxt *ctxt)
2778{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002779 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002780 return X86EMUL_CONTINUE;
2781}
2782
2783static int em_and(struct x86_emulate_ctxt *ctxt)
2784{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002785 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002786 return X86EMUL_CONTINUE;
2787}
2788
2789static int em_sub(struct x86_emulate_ctxt *ctxt)
2790{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002791 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002792 return X86EMUL_CONTINUE;
2793}
2794
2795static int em_xor(struct x86_emulate_ctxt *ctxt)
2796{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002797 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002798 return X86EMUL_CONTINUE;
2799}
2800
2801static int em_cmp(struct x86_emulate_ctxt *ctxt)
2802{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002803 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002804 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002805 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002806 return X86EMUL_CONTINUE;
2807}
2808
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002809static int em_test(struct x86_emulate_ctxt *ctxt)
2810{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002811 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002812 /* Disable writeback. */
2813 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002814 return X86EMUL_CONTINUE;
2815}
2816
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002817static int em_xchg(struct x86_emulate_ctxt *ctxt)
2818{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002819 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002820 ctxt->src.val = ctxt->dst.val;
2821 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002822
2823 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002824 ctxt->dst.val = ctxt->src.orig_val;
2825 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002826 return X86EMUL_CONTINUE;
2827}
2828
Avi Kivity5c82aa22010-08-18 18:31:43 +03002829static int em_imul(struct x86_emulate_ctxt *ctxt)
2830{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002831 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002832 return X86EMUL_CONTINUE;
2833}
2834
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002835static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2836{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002837 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002838 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002839}
2840
Avi Kivity61429142010-08-19 15:13:00 +03002841static int em_cwd(struct x86_emulate_ctxt *ctxt)
2842{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002843 ctxt->dst.type = OP_REG;
2844 ctxt->dst.bytes = ctxt->src.bytes;
2845 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2846 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002847
2848 return X86EMUL_CONTINUE;
2849}
2850
Avi Kivity48bb5d32010-08-18 18:54:34 +03002851static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2852{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002853 u64 tsc = 0;
2854
Avi Kivity717746e2011-04-20 13:37:53 +03002855 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002856 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2857 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002858 return X86EMUL_CONTINUE;
2859}
2860
Avi Kivity222d21a2011-11-10 14:57:30 +02002861static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2862{
2863 u64 pmc;
2864
2865 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2866 return emulate_gp(ctxt, 0);
2867 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2868 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2869 return X86EMUL_CONTINUE;
2870}
2871
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002872static int em_mov(struct x86_emulate_ctxt *ctxt)
2873{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002874 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002875 return X86EMUL_CONTINUE;
2876}
2877
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002878static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2879{
2880 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2881 return emulate_gp(ctxt, 0);
2882
2883 /* Disable writeback. */
2884 ctxt->dst.type = OP_NONE;
2885 return X86EMUL_CONTINUE;
2886}
2887
2888static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2889{
2890 unsigned long val;
2891
2892 if (ctxt->mode == X86EMUL_MODE_PROT64)
2893 val = ctxt->src.val & ~0ULL;
2894 else
2895 val = ctxt->src.val & ~0U;
2896
2897 /* #UD condition is already handled. */
2898 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2899 return emulate_gp(ctxt, 0);
2900
2901 /* Disable writeback. */
2902 ctxt->dst.type = OP_NONE;
2903 return X86EMUL_CONTINUE;
2904}
2905
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002906static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2907{
2908 u64 msr_data;
2909
2910 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2911 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2912 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2913 return emulate_gp(ctxt, 0);
2914
2915 return X86EMUL_CONTINUE;
2916}
2917
2918static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2919{
2920 u64 msr_data;
2921
2922 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2923 return emulate_gp(ctxt, 0);
2924
2925 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2926 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2927 return X86EMUL_CONTINUE;
2928}
2929
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002930static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2931{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002932 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002933 return emulate_ud(ctxt);
2934
Avi Kivity9dac77f2011-06-01 15:34:25 +03002935 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002936 return X86EMUL_CONTINUE;
2937}
2938
2939static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2940{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002941 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002942
Avi Kivity9dac77f2011-06-01 15:34:25 +03002943 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002944 return emulate_ud(ctxt);
2945
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002947 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2948
2949 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002950 ctxt->dst.type = OP_NONE;
2951 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002952}
2953
Avi Kivity38503912011-03-31 18:48:09 +02002954static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2955{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002956 int rc;
2957 ulong linear;
2958
Avi Kivity9dac77f2011-06-01 15:34:25 +03002959 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002960 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002961 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002962 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002963 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002964 return X86EMUL_CONTINUE;
2965}
2966
Avi Kivity2d04a052011-04-20 15:32:49 +03002967static int em_clts(struct x86_emulate_ctxt *ctxt)
2968{
2969 ulong cr0;
2970
2971 cr0 = ctxt->ops->get_cr(ctxt, 0);
2972 cr0 &= ~X86_CR0_TS;
2973 ctxt->ops->set_cr(ctxt, 0, cr0);
2974 return X86EMUL_CONTINUE;
2975}
2976
Avi Kivity26d05cc2011-04-21 12:07:59 +03002977static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2978{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002979 int rc;
2980
Avi Kivity9dac77f2011-06-01 15:34:25 +03002981 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002982 return X86EMUL_UNHANDLEABLE;
2983
2984 rc = ctxt->ops->fix_hypercall(ctxt);
2985 if (rc != X86EMUL_CONTINUE)
2986 return rc;
2987
2988 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002989 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002990 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002991 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002992 return X86EMUL_CONTINUE;
2993}
2994
2995static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2996{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002997 struct desc_ptr desc_ptr;
2998 int rc;
2999
Avi Kivity510425f2012-06-07 17:04:36 +03003000 if (ctxt->mode == X86EMUL_MODE_PROT64)
3001 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003002 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003003 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003004 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003005 if (rc != X86EMUL_CONTINUE)
3006 return rc;
3007 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3008 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003009 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003010 return X86EMUL_CONTINUE;
3011}
3012
Avi Kivity5ef39c72011-04-21 12:21:50 +03003013static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003014{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003015 int rc;
3016
Avi Kivity5ef39c72011-04-21 12:21:50 +03003017 rc = ctxt->ops->fix_hypercall(ctxt);
3018
Avi Kivity26d05cc2011-04-21 12:07:59 +03003019 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003020 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003021 return rc;
3022}
3023
3024static int em_lidt(struct x86_emulate_ctxt *ctxt)
3025{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003026 struct desc_ptr desc_ptr;
3027 int rc;
3028
Avi Kivity510425f2012-06-07 17:04:36 +03003029 if (ctxt->mode == X86EMUL_MODE_PROT64)
3030 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003031 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003032 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003033 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003034 if (rc != X86EMUL_CONTINUE)
3035 return rc;
3036 ctxt->ops->set_idt(ctxt, &desc_ptr);
3037 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003038 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003039 return X86EMUL_CONTINUE;
3040}
3041
3042static int em_smsw(struct x86_emulate_ctxt *ctxt)
3043{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003044 ctxt->dst.bytes = 2;
3045 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003046 return X86EMUL_CONTINUE;
3047}
3048
3049static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3050{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003051 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003052 | (ctxt->src.val & 0x0f));
3053 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003054 return X86EMUL_CONTINUE;
3055}
3056
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003057static int em_loop(struct x86_emulate_ctxt *ctxt)
3058{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003059 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
3060 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
3061 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3062 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003063
3064 return X86EMUL_CONTINUE;
3065}
3066
3067static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3068{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
3070 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003071
3072 return X86EMUL_CONTINUE;
3073}
3074
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003075static int em_in(struct x86_emulate_ctxt *ctxt)
3076{
3077 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3078 &ctxt->dst.val))
3079 return X86EMUL_IO_NEEDED;
3080
3081 return X86EMUL_CONTINUE;
3082}
3083
3084static int em_out(struct x86_emulate_ctxt *ctxt)
3085{
3086 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3087 &ctxt->src.val, 1);
3088 /* Disable writeback. */
3089 ctxt->dst.type = OP_NONE;
3090 return X86EMUL_CONTINUE;
3091}
3092
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003093static int em_cli(struct x86_emulate_ctxt *ctxt)
3094{
3095 if (emulator_bad_iopl(ctxt))
3096 return emulate_gp(ctxt, 0);
3097
3098 ctxt->eflags &= ~X86_EFLAGS_IF;
3099 return X86EMUL_CONTINUE;
3100}
3101
3102static int em_sti(struct x86_emulate_ctxt *ctxt)
3103{
3104 if (emulator_bad_iopl(ctxt))
3105 return emulate_gp(ctxt, 0);
3106
3107 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3108 ctxt->eflags |= X86_EFLAGS_IF;
3109 return X86EMUL_CONTINUE;
3110}
3111
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003112static int em_bt(struct x86_emulate_ctxt *ctxt)
3113{
3114 /* Disable writeback. */
3115 ctxt->dst.type = OP_NONE;
3116 /* only subword offset */
3117 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3118
3119 emulate_2op_SrcV_nobyte(ctxt, "bt");
3120 return X86EMUL_CONTINUE;
3121}
3122
3123static int em_bts(struct x86_emulate_ctxt *ctxt)
3124{
3125 emulate_2op_SrcV_nobyte(ctxt, "bts");
3126 return X86EMUL_CONTINUE;
3127}
3128
3129static int em_btr(struct x86_emulate_ctxt *ctxt)
3130{
3131 emulate_2op_SrcV_nobyte(ctxt, "btr");
3132 return X86EMUL_CONTINUE;
3133}
3134
3135static int em_btc(struct x86_emulate_ctxt *ctxt)
3136{
3137 emulate_2op_SrcV_nobyte(ctxt, "btc");
3138 return X86EMUL_CONTINUE;
3139}
3140
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003141static int em_bsf(struct x86_emulate_ctxt *ctxt)
3142{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003143 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003144 return X86EMUL_CONTINUE;
3145}
3146
3147static int em_bsr(struct x86_emulate_ctxt *ctxt)
3148{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003149 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003150 return X86EMUL_CONTINUE;
3151}
3152
Avi Kivity6d6eede2012-06-07 14:11:36 +03003153static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3154{
3155 u32 eax, ebx, ecx, edx;
3156
3157 eax = ctxt->regs[VCPU_REGS_RAX];
3158 ecx = ctxt->regs[VCPU_REGS_RCX];
3159 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
3160 ctxt->regs[VCPU_REGS_RAX] = eax;
3161 ctxt->regs[VCPU_REGS_RBX] = ebx;
3162 ctxt->regs[VCPU_REGS_RCX] = ecx;
3163 ctxt->regs[VCPU_REGS_RDX] = edx;
3164 return X86EMUL_CONTINUE;
3165}
3166
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003167static bool valid_cr(int nr)
3168{
3169 switch (nr) {
3170 case 0:
3171 case 2 ... 4:
3172 case 8:
3173 return true;
3174 default:
3175 return false;
3176 }
3177}
3178
3179static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3180{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003181 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003182 return emulate_ud(ctxt);
3183
3184 return X86EMUL_CONTINUE;
3185}
3186
3187static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3188{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003189 u64 new_val = ctxt->src.val64;
3190 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003191 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003192
3193 static u64 cr_reserved_bits[] = {
3194 0xffffffff00000000ULL,
3195 0, 0, 0, /* CR3 checked later */
3196 CR4_RESERVED_BITS,
3197 0, 0, 0,
3198 CR8_RESERVED_BITS,
3199 };
3200
3201 if (!valid_cr(cr))
3202 return emulate_ud(ctxt);
3203
3204 if (new_val & cr_reserved_bits[cr])
3205 return emulate_gp(ctxt, 0);
3206
3207 switch (cr) {
3208 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003209 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003210 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3211 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3212 return emulate_gp(ctxt, 0);
3213
Avi Kivity717746e2011-04-20 13:37:53 +03003214 cr4 = ctxt->ops->get_cr(ctxt, 4);
3215 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003216
3217 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3218 !(cr4 & X86_CR4_PAE))
3219 return emulate_gp(ctxt, 0);
3220
3221 break;
3222 }
3223 case 3: {
3224 u64 rsvd = 0;
3225
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003226 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3227 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003228 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003229 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003230 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003231 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003232 rsvd = CR3_NONPAE_RESERVED_BITS;
3233
3234 if (new_val & rsvd)
3235 return emulate_gp(ctxt, 0);
3236
3237 break;
3238 }
3239 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003240 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003241
3242 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3243 return emulate_gp(ctxt, 0);
3244
3245 break;
3246 }
3247 }
3248
3249 return X86EMUL_CONTINUE;
3250}
3251
Joerg Roedel3b88e412011-04-04 12:39:29 +02003252static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3253{
3254 unsigned long dr7;
3255
Avi Kivity717746e2011-04-20 13:37:53 +03003256 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003257
3258 /* Check if DR7.Global_Enable is set */
3259 return dr7 & (1 << 13);
3260}
3261
3262static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3263{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003264 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003265 u64 cr4;
3266
3267 if (dr > 7)
3268 return emulate_ud(ctxt);
3269
Avi Kivity717746e2011-04-20 13:37:53 +03003270 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003271 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3272 return emulate_ud(ctxt);
3273
3274 if (check_dr7_gd(ctxt))
3275 return emulate_db(ctxt);
3276
3277 return X86EMUL_CONTINUE;
3278}
3279
3280static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3281{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003282 u64 new_val = ctxt->src.val64;
3283 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003284
3285 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3286 return emulate_gp(ctxt, 0);
3287
3288 return check_dr_read(ctxt);
3289}
3290
Joerg Roedel01de8b02011-04-04 12:39:31 +02003291static int check_svme(struct x86_emulate_ctxt *ctxt)
3292{
3293 u64 efer;
3294
Avi Kivity717746e2011-04-20 13:37:53 +03003295 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003296
3297 if (!(efer & EFER_SVME))
3298 return emulate_ud(ctxt);
3299
3300 return X86EMUL_CONTINUE;
3301}
3302
3303static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3304{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003305 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003306
3307 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003308 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003309 return emulate_gp(ctxt, 0);
3310
3311 return check_svme(ctxt);
3312}
3313
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003314static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3315{
Avi Kivity717746e2011-04-20 13:37:53 +03003316 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003317
Avi Kivity717746e2011-04-20 13:37:53 +03003318 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003319 return emulate_ud(ctxt);
3320
3321 return X86EMUL_CONTINUE;
3322}
3323
Joerg Roedel80612522011-04-04 12:39:33 +02003324static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3325{
Avi Kivity717746e2011-04-20 13:37:53 +03003326 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003327 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003328
Avi Kivity717746e2011-04-20 13:37:53 +03003329 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003330 (rcx > 3))
3331 return emulate_gp(ctxt, 0);
3332
3333 return X86EMUL_CONTINUE;
3334}
3335
Joerg Roedelf6511932011-04-04 12:39:35 +02003336static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3337{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003338 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3339 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003340 return emulate_gp(ctxt, 0);
3341
3342 return X86EMUL_CONTINUE;
3343}
3344
3345static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3346{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003347 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3348 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003349 return emulate_gp(ctxt, 0);
3350
3351 return X86EMUL_CONTINUE;
3352}
3353
Avi Kivity73fba5f2010-07-29 15:11:53 +03003354#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003355#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003356#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3357 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003358#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003359#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003360#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3361#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003362#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003363#define II(_f, _e, _i) \
3364 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003365#define IIP(_f, _e, _i, _p) \
3366 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3367 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003368#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003369
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003370#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003371#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003372#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003373#define I2bvIP(_f, _e, _i, _p) \
3374 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003375
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003376#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3377 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3378 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003379
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003380static struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003381 DI(SrcNone | Priv, monitor),
3382 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003383 N, N, N, N, N, N,
3384};
3385
Joerg Roedel01de8b02011-04-04 12:39:31 +02003386static struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003387 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3388 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3389 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3390 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3391 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3392 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3393 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3394 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003395};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003396
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003397static struct opcode group7_rm7[] = {
3398 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003399 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003400 N, N, N, N, N, N,
3401};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003402
Avi Kivity73fba5f2010-07-29 15:11:53 +03003403static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003404 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003405 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003406 I(Lock, em_adc),
3407 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003408 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003409 I(Lock, em_sub),
3410 I(Lock, em_xor),
3411 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003412};
3413
3414static struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003415 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003416};
3417
3418static struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003419 I(DstMem | SrcImm, em_test),
3420 I(DstMem | SrcImm, em_test),
3421 I(DstMem | SrcNone | Lock, em_not),
3422 I(DstMem | SrcNone | Lock, em_neg),
3423 I(SrcMem, em_mul_ex),
3424 I(SrcMem, em_imul_ex),
3425 I(SrcMem, em_div_ex),
3426 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003427};
3428
3429static struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003430 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3431 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003432 N, N, N, N, N, N,
3433};
3434
3435static struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003436 I(DstMem | SrcNone | Lock, em_grp45),
3437 I(DstMem | SrcNone | Lock, em_grp45),
3438 I(SrcMem | Stack, em_grp45),
3439 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3440 I(SrcMem | Stack, em_grp45),
3441 I(SrcMemFAddr | ImplicitOps, em_grp45),
3442 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003443};
3444
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003445static struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003446 DI(Prot, sldt),
3447 DI(Prot, str),
3448 DI(Prot | Priv, lldt),
3449 DI(Prot | Priv, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003450 N, N, N, N,
3451};
3452
Avi Kivity73fba5f2010-07-29 15:11:53 +03003453static struct group_dual group7 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003454 DI(Mov | DstMem | Priv, sgdt),
3455 DI(Mov | DstMem | Priv, sidt),
3456 II(SrcMem | Priv, em_lgdt, lgdt),
3457 II(SrcMem | Priv, em_lidt, lidt),
3458 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3459 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3460 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003461}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003462 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003463 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003464 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003465 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3466 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3467 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003468} };
3469
3470static struct opcode group8[] = {
3471 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003472 I(DstMem | SrcImmByte, em_bt),
3473 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3474 I(DstMem | SrcImmByte | Lock, em_btr),
3475 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003476};
3477
3478static struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003479 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003480}, {
3481 N, N, N, N, N, N, N, N,
3482} };
3483
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003484static struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003485 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003486 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003487};
3488
Avi Kivityaa97bb42010-01-20 18:09:23 +02003489static struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003490 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003491};
3492
Avi Kivity3e114eb2012-04-09 18:40:01 +03003493static struct gprefix pfx_vmovntpx = {
3494 I(0, em_mov), N, N, N,
3495};
3496
Avi Kivity73fba5f2010-07-29 15:11:53 +03003497static struct opcode opcode_table[256] = {
3498 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003499 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003500 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3501 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003502 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003503 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003504 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3505 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003506 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003507 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003508 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3509 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003510 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003511 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003512 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3513 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003514 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003515 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003516 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003517 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003518 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003519 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003520 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003521 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003522 /* 0x40 - 0x4F */
3523 X16(D(DstReg)),
3524 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003525 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003526 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003527 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003528 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003529 I(ImplicitOps | Stack | No64, em_pusha),
3530 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003531 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3532 N, N, N, N,
3533 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003534 I(SrcImm | Mov | Stack, em_push),
3535 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003536 I(SrcImmByte | Mov | Stack, em_push),
3537 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003538 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3539 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003540 /* 0x70 - 0x7F */
3541 X16(D(SrcImmByte)),
3542 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003543 G(ByteOp | DstMem | SrcImm, group1),
3544 G(DstMem | SrcImm, group1),
3545 G(ByteOp | DstMem | SrcImm | No64, group1),
3546 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003547 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003548 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003549 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003550 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003551 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003552 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003553 D(ModRM | SrcMem | NoAccess | DstReg),
3554 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3555 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003556 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003557 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003558 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003559 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003560 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003561 II(ImplicitOps | Stack, em_pushf, pushf),
3562 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003563 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003564 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003565 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003566 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003567 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003568 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003569 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003570 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3571 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003572 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003573 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003574 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003575 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003576 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003577 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003578 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003579 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003580 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003581 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3582 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003583 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003584 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003585 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003586 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003587 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003588 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003589 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003590 N, N, N, N,
3591 /* 0xD8 - 0xDF */
3592 N, N, N, N, N, N, N, N,
3593 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003594 X3(I(SrcImmByte, em_loop)),
3595 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003596 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3597 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003598 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003599 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003600 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003601 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3602 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003603 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003604 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003605 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3606 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003607 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003608 D(ImplicitOps), D(ImplicitOps),
3609 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003610 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3611};
3612
3613static struct opcode twobyte_table[256] = {
3614 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003615 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003616 N, I(ImplicitOps | VendorSpecific, em_syscall),
3617 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003618 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003619 N, D(ImplicitOps | ModRM), N, N,
3620 /* 0x10 - 0x1F */
3621 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3622 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003623 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003624 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003625 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3626 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003627 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003628 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3629 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003630 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003631 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003632 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003633 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003634 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003635 I(ImplicitOps | VendorSpecific, em_sysenter),
3636 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003637 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003638 N, N, N, N, N, N, N, N,
3639 /* 0x40 - 0x4F */
3640 X16(D(DstReg | SrcMem | ModRM | Mov)),
3641 /* 0x50 - 0x5F */
3642 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3643 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003644 N, N, N, N,
3645 N, N, N, N,
3646 N, N, N, N,
3647 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003648 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003649 N, N, N, N,
3650 N, N, N, N,
3651 N, N, N, N,
3652 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003653 /* 0x80 - 0x8F */
3654 X16(D(SrcImm)),
3655 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003656 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003657 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003658 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003659 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003660 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3661 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3662 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003663 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003664 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003665 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003666 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3667 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003668 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003669 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003670 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003671 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003672 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003673 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3674 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003675 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003676 /* 0xB8 - 0xBF */
3677 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003678 G(BitOp, group8),
3679 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003680 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003681 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003682 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003683 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003684 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003685 N, N, N, GD(0, &group9),
3686 N, N, N, N, N, N, N, N,
3687 /* 0xD0 - 0xDF */
3688 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3689 /* 0xE0 - 0xEF */
3690 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3691 /* 0xF0 - 0xFF */
3692 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3693};
3694
3695#undef D
3696#undef N
3697#undef G
3698#undef GD
3699#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003700#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003701#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003702
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003703#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003704#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003705#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003706#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003707#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003708
Avi Kivity9dac77f2011-06-01 15:34:25 +03003709static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003710{
3711 unsigned size;
3712
Avi Kivity9dac77f2011-06-01 15:34:25 +03003713 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003714 if (size == 8)
3715 size = 4;
3716 return size;
3717}
3718
3719static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3720 unsigned size, bool sign_extension)
3721{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003722 int rc = X86EMUL_CONTINUE;
3723
3724 op->type = OP_IMM;
3725 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003726 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003727 /* NB. Immediates are sign-extended as necessary. */
3728 switch (op->bytes) {
3729 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003730 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003731 break;
3732 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003733 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003734 break;
3735 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003736 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003737 break;
3738 }
3739 if (!sign_extension) {
3740 switch (op->bytes) {
3741 case 1:
3742 op->val &= 0xff;
3743 break;
3744 case 2:
3745 op->val &= 0xffff;
3746 break;
3747 case 4:
3748 op->val &= 0xffffffff;
3749 break;
3750 }
3751 }
3752done:
3753 return rc;
3754}
3755
Avi Kivitya9945542011-09-13 10:45:41 +03003756static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3757 unsigned d)
3758{
3759 int rc = X86EMUL_CONTINUE;
3760
3761 switch (d) {
3762 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003763 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03003764 break;
3765 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003766 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003767 break;
3768 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003769 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003770 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003771 *op = ctxt->memop;
3772 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003773 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003774 fetch_bit_operand(ctxt);
3775 op->orig_val = op->val;
3776 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003777 case OpMem64:
3778 ctxt->memop.bytes = 8;
3779 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003780 case OpAcc:
3781 op->type = OP_REG;
3782 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3783 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3784 fetch_register_operand(op);
3785 op->orig_val = op->val;
3786 break;
3787 case OpDI:
3788 op->type = OP_MEM;
3789 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3790 op->addr.mem.ea =
3791 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3792 op->addr.mem.seg = VCPU_SREG_ES;
3793 op->val = 0;
3794 break;
3795 case OpDX:
3796 op->type = OP_REG;
3797 op->bytes = 2;
3798 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3799 fetch_register_operand(op);
3800 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003801 case OpCL:
3802 op->bytes = 1;
3803 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3804 break;
3805 case OpImmByte:
3806 rc = decode_imm(ctxt, op, 1, true);
3807 break;
3808 case OpOne:
3809 op->bytes = 1;
3810 op->val = 1;
3811 break;
3812 case OpImm:
3813 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3814 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003815 case OpMem8:
3816 ctxt->memop.bytes = 1;
3817 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003818 case OpMem16:
3819 ctxt->memop.bytes = 2;
3820 goto mem_common;
3821 case OpMem32:
3822 ctxt->memop.bytes = 4;
3823 goto mem_common;
3824 case OpImmU16:
3825 rc = decode_imm(ctxt, op, 2, false);
3826 break;
3827 case OpImmU:
3828 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3829 break;
3830 case OpSI:
3831 op->type = OP_MEM;
3832 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3833 op->addr.mem.ea =
3834 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3835 op->addr.mem.seg = seg_override(ctxt);
3836 op->val = 0;
3837 break;
3838 case OpImmFAddr:
3839 op->type = OP_IMM;
3840 op->addr.mem.ea = ctxt->_eip;
3841 op->bytes = ctxt->op_bytes + 2;
3842 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3843 break;
3844 case OpMemFAddr:
3845 ctxt->memop.bytes = ctxt->op_bytes + 2;
3846 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003847 case OpES:
3848 op->val = VCPU_SREG_ES;
3849 break;
3850 case OpCS:
3851 op->val = VCPU_SREG_CS;
3852 break;
3853 case OpSS:
3854 op->val = VCPU_SREG_SS;
3855 break;
3856 case OpDS:
3857 op->val = VCPU_SREG_DS;
3858 break;
3859 case OpFS:
3860 op->val = VCPU_SREG_FS;
3861 break;
3862 case OpGS:
3863 op->val = VCPU_SREG_GS;
3864 break;
Avi Kivitya9945542011-09-13 10:45:41 +03003865 case OpImplicit:
3866 /* Special instructions do their own operand decoding. */
3867 default:
3868 op->type = OP_NONE; /* Disable writeback. */
3869 break;
3870 }
3871
3872done:
3873 return rc;
3874}
3875
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003876int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003877{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003878 int rc = X86EMUL_CONTINUE;
3879 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003880 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003881 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003882 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003883
Avi Kivityf09ed832011-09-13 10:45:40 +03003884 ctxt->memop.type = OP_NONE;
3885 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003886 ctxt->_eip = ctxt->eip;
3887 ctxt->fetch.start = ctxt->_eip;
3888 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003889 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003890 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003891
3892 switch (mode) {
3893 case X86EMUL_MODE_REAL:
3894 case X86EMUL_MODE_VM86:
3895 case X86EMUL_MODE_PROT16:
3896 def_op_bytes = def_ad_bytes = 2;
3897 break;
3898 case X86EMUL_MODE_PROT32:
3899 def_op_bytes = def_ad_bytes = 4;
3900 break;
3901#ifdef CONFIG_X86_64
3902 case X86EMUL_MODE_PROT64:
3903 def_op_bytes = 4;
3904 def_ad_bytes = 8;
3905 break;
3906#endif
3907 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003908 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003909 }
3910
Avi Kivity9dac77f2011-06-01 15:34:25 +03003911 ctxt->op_bytes = def_op_bytes;
3912 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003913
3914 /* Legacy prefixes. */
3915 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003916 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003917 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003918 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003919 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003920 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003921 break;
3922 case 0x67: /* address-size override */
3923 if (mode == X86EMUL_MODE_PROT64)
3924 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003925 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003926 else
3927 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003928 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003929 break;
3930 case 0x26: /* ES override */
3931 case 0x2e: /* CS override */
3932 case 0x36: /* SS override */
3933 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003934 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003935 break;
3936 case 0x64: /* FS override */
3937 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003938 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003939 break;
3940 case 0x40 ... 0x4f: /* REX */
3941 if (mode != X86EMUL_MODE_PROT64)
3942 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003943 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003944 continue;
3945 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003946 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003947 break;
3948 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003949 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003950 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003951 break;
3952 default:
3953 goto done_prefixes;
3954 }
3955
3956 /* Any legacy prefix after a REX prefix nullifies its effect. */
3957
Avi Kivity9dac77f2011-06-01 15:34:25 +03003958 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003959 }
3960
3961done_prefixes:
3962
3963 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003964 if (ctxt->rex_prefix & 8)
3965 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003966
3967 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003968 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003969 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003970 if (ctxt->b == 0x0f) {
3971 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003972 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003973 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003974 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003975 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003976
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09003977 if (ctxt->d & ModRM)
3978 ctxt->modrm = insn_fetch(u8, ctxt);
3979
Avi Kivity9dac77f2011-06-01 15:34:25 +03003980 while (ctxt->d & GroupMask) {
3981 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003982 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003983 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003984 opcode = opcode.u.group[goffset];
3985 break;
3986 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003987 goffset = (ctxt->modrm >> 3) & 7;
3988 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003989 opcode = opcode.u.gdual->mod3[goffset];
3990 else
3991 opcode = opcode.u.gdual->mod012[goffset];
3992 break;
3993 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003994 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003995 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003996 break;
3997 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003998 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003999 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004000 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004001 switch (simd_prefix) {
4002 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4003 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4004 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4005 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4006 }
4007 break;
4008 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004009 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004010 }
Avi Kivity46561642011-04-24 14:09:59 +03004011
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004012 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004013 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004014 }
4015
Avi Kivity9dac77f2011-06-01 15:34:25 +03004016 ctxt->execute = opcode.u.execute;
4017 ctxt->check_perm = opcode.check_perm;
4018 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004019
4020 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004022 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004023
Avi Kivity9dac77f2011-06-01 15:34:25 +03004024 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004025 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004026
Avi Kivity9dac77f2011-06-01 15:34:25 +03004027 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4028 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004029
Avi Kivity9dac77f2011-06-01 15:34:25 +03004030 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004031 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004032 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004033 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004034 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004035 }
4036
Avi Kivity9dac77f2011-06-01 15:34:25 +03004037 if (ctxt->d & Sse)
4038 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004039 else if (ctxt->d & Mmx)
4040 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004041
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004042 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004043 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004044 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004045 if (!ctxt->has_seg_override)
4046 set_seg_override(ctxt, ctxt->modrm_seg);
4047 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004048 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004049 if (rc != X86EMUL_CONTINUE)
4050 goto done;
4051
Avi Kivity9dac77f2011-06-01 15:34:25 +03004052 if (!ctxt->has_seg_override)
4053 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004054
Avi Kivityf09ed832011-09-13 10:45:40 +03004055 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004056
Avi Kivityf09ed832011-09-13 10:45:40 +03004057 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4058 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004059
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004060 /*
4061 * Decode and fetch the source operand: register, memory
4062 * or immediate.
4063 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004064 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004065 if (rc != X86EMUL_CONTINUE)
4066 goto done;
4067
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004068 /*
4069 * Decode and fetch the second source operand: register, memory
4070 * or immediate.
4071 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004072 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004073 if (rc != X86EMUL_CONTINUE)
4074 goto done;
4075
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004076 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004077 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004078
4079done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004080 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4081 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004082
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004083 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004084}
4085
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004086bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4087{
4088 return ctxt->d & PageTable;
4089}
4090
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004091static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4092{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004093 /* The second termination condition only applies for REPE
4094 * and REPNE. Test if the repeat string operation prefix is
4095 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4096 * corresponding termination condition according to:
4097 * - if REPE/REPZ and ZF = 0 then done
4098 * - if REPNE/REPNZ and ZF = 1 then done
4099 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004100 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4101 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4102 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004103 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004104 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004105 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4106 return true;
4107
4108 return false;
4109}
4110
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004111static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4112{
4113 bool fault = false;
4114
4115 ctxt->ops->get_fpu(ctxt);
4116 asm volatile("1: fwait \n\t"
4117 "2: \n\t"
4118 ".pushsection .fixup,\"ax\" \n\t"
4119 "3: \n\t"
4120 "movb $1, %[fault] \n\t"
4121 "jmp 2b \n\t"
4122 ".popsection \n\t"
4123 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004124 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004125 ctxt->ops->put_fpu(ctxt);
4126
4127 if (unlikely(fault))
4128 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4129
4130 return X86EMUL_CONTINUE;
4131}
4132
4133static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4134 struct operand *op)
4135{
4136 if (op->type == OP_MM)
4137 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4138}
4139
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004140int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004141{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004142 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004143 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004144 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004145
Avi Kivity9dac77f2011-06-01 15:34:25 +03004146 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004147
Avi Kivity9dac77f2011-06-01 15:34:25 +03004148 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004149 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004150 goto done;
4151 }
4152
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004153 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004154 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004155 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004156 goto done;
4157 }
4158
Avi Kivity9dac77f2011-06-01 15:34:25 +03004159 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004160 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004161 goto done;
4162 }
4163
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004164 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4165 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004166 rc = emulate_ud(ctxt);
4167 goto done;
4168 }
4169
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004170 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004171 rc = emulate_nm(ctxt);
4172 goto done;
4173 }
4174
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004175 if (ctxt->d & Mmx) {
4176 rc = flush_pending_x87_faults(ctxt);
4177 if (rc != X86EMUL_CONTINUE)
4178 goto done;
4179 /*
4180 * Now that we know the fpu is exception safe, we can fetch
4181 * operands from it.
4182 */
4183 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4184 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4185 if (!(ctxt->d & Mov))
4186 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4187 }
4188
Avi Kivity9dac77f2011-06-01 15:34:25 +03004189 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4190 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004191 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004192 if (rc != X86EMUL_CONTINUE)
4193 goto done;
4194 }
4195
Gleb Natapove92805a2010-02-10 14:21:35 +02004196 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004197 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004198 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004199 goto done;
4200 }
4201
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004202 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004203 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004204 rc = emulate_ud(ctxt);
4205 goto done;
4206 }
4207
Joerg Roedeld09beab2011-04-04 12:39:25 +02004208 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004209 if (ctxt->check_perm) {
4210 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004211 if (rc != X86EMUL_CONTINUE)
4212 goto done;
4213 }
4214
Avi Kivity9dac77f2011-06-01 15:34:25 +03004215 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4216 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004217 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004218 if (rc != X86EMUL_CONTINUE)
4219 goto done;
4220 }
4221
Avi Kivity9dac77f2011-06-01 15:34:25 +03004222 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004223 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004224 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4225 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004226 goto done;
4227 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004228 }
4229
Avi Kivity9dac77f2011-06-01 15:34:25 +03004230 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4231 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4232 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004233 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004234 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004235 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004236 }
4237
Avi Kivity9dac77f2011-06-01 15:34:25 +03004238 if (ctxt->src2.type == OP_MEM) {
4239 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4240 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004241 if (rc != X86EMUL_CONTINUE)
4242 goto done;
4243 }
4244
Avi Kivity9dac77f2011-06-01 15:34:25 +03004245 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004246 goto special_insn;
4247
4248
Avi Kivity9dac77f2011-06-01 15:34:25 +03004249 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004250 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004251 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4252 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004253 if (rc != X86EMUL_CONTINUE)
4254 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004255 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004256 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004257
Avi Kivity018a98d2007-11-27 19:30:56 +02004258special_insn:
4259
Avi Kivity9dac77f2011-06-01 15:34:25 +03004260 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4261 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004262 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004263 if (rc != X86EMUL_CONTINUE)
4264 goto done;
4265 }
4266
Avi Kivity9dac77f2011-06-01 15:34:25 +03004267 if (ctxt->execute) {
4268 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004269 if (rc != X86EMUL_CONTINUE)
4270 goto done;
4271 goto writeback;
4272 }
4273
Avi Kivity9dac77f2011-06-01 15:34:25 +03004274 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275 goto twobyte_insn;
4276
Avi Kivity9dac77f2011-06-01 15:34:25 +03004277 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004278 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004279 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004280 break;
4281 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004282 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004283 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004284 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004285 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004286 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004287 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004288 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004289 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004290 if (test_cc(ctxt->b, ctxt->eflags))
4291 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004292 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004293 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004294 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004295 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004296 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004297 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004298 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004299 rc = em_xchg(ctxt);
4300 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004301 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004302 switch (ctxt->op_bytes) {
4303 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4304 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4305 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004306 }
4307 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004308 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004309 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004310 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004311 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004312 rc = emulate_int(ctxt, 3);
4313 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004314 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004315 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004316 break;
4317 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004318 if (ctxt->eflags & EFLG_OF)
4319 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004320 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004321 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004322 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004323 break;
4324 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004325 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004326 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004327 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004328 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004329 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004330 jmp_rel(ctxt, ctxt->src.val);
4331 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004332 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004333 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004334 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004335 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004336 case 0xf5: /* cmc */
4337 /* complement carry flag from eflags reg */
4338 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004339 break;
4340 case 0xf8: /* clc */
4341 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004342 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004343 case 0xf9: /* stc */
4344 ctxt->eflags |= EFLG_CF;
4345 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004346 case 0xfc: /* cld */
4347 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004348 break;
4349 case 0xfd: /* std */
4350 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004351 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004352 default:
4353 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004355
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004356 if (rc != X86EMUL_CONTINUE)
4357 goto done;
4358
Avi Kivity018a98d2007-11-27 19:30:56 +02004359writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004360 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004361 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004362 goto done;
4363
Gleb Natapov5cd21912010-03-18 15:20:26 +02004364 /*
4365 * restore dst type in case the decoding will be reused
4366 * (happens for string instruction )
4367 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004368 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004369
Avi Kivity9dac77f2011-06-01 15:34:25 +03004370 if ((ctxt->d & SrcMask) == SrcSI)
4371 string_addr_inc(ctxt, seg_override(ctxt),
4372 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004373
Avi Kivity9dac77f2011-06-01 15:34:25 +03004374 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004375 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004377
Avi Kivity9dac77f2011-06-01 15:34:25 +03004378 if (ctxt->rep_prefix && (ctxt->d & String)) {
4379 struct read_cache *r = &ctxt->io_read;
4380 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004381
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004382 if (!string_insn_completed(ctxt)) {
4383 /*
4384 * Re-enter guest when pio read ahead buffer is empty
4385 * or, if it is not used, after each 1024 iteration.
4386 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004387 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004388 (r->end == 0 || r->end != r->pos)) {
4389 /*
4390 * Reset read cache. Usually happens before
4391 * decode, but since instruction is restarted
4392 * we have to do it here.
4393 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004395 return EMULATION_RESTART;
4396 }
4397 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004398 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004399 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004400
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004402
4403done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004404 if (rc == X86EMUL_PROPAGATE_FAULT)
4405 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004406 if (rc == X86EMUL_INTERCEPTED)
4407 return EMULATION_INTERCEPTED;
4408
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004409 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410
4411twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004413 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004414 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004415 break;
4416 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004417 case 0x0d: /* GrpP (prefetch) */
4418 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004419 break;
4420 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004421 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004422 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004423 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004424 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004426 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004427 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4428 if (!test_cc(ctxt->b, ctxt->eflags))
4429 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004431 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004432 if (test_cc(ctxt->b, ctxt->eflags))
4433 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004434 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004435 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004436 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004437 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004438 case 0xa4: /* shld imm8, r, r/m */
4439 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004440 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004441 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004442 case 0xac: /* shrd imm8, r, r/m */
4443 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004444 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004445 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004446 case 0xae: /* clflush */
4447 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004448 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004449 ctxt->dst.bytes = ctxt->op_bytes;
4450 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4451 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004454 ctxt->dst.bytes = ctxt->op_bytes;
4455 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4456 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004457 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004458 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004459 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004460 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004461 ctxt->src.val = ctxt->dst.orig_val;
4462 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004463 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004464 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004465 ctxt->dst.bytes = ctxt->op_bytes;
4466 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4467 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004468 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004469 default:
4470 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004472
4473 if (rc != X86EMUL_CONTINUE)
4474 goto done;
4475
Avi Kivity6aa8b732006-12-10 02:21:36 -08004476 goto writeback;
4477
4478cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004479 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480}