blob: e46809b8f31b1fb7d55c5276ed5ad3208a15f5af [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 Kivitya9945542011-09-13 10:45:41 +030054
Avi Kivity0fe59122011-09-13 10:45:47 +030055#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030056#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030057
58/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080059 * Opcode effective-address decode tables.
60 * Note that we only emulate instructions that have at least one memory
61 * operand (excluding implicit stack references). We assume that stack
62 * references and instruction fetches will never occur in special memory
63 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
64 * not be handled.
65 */
66
67/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030068#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080069/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030070#define DstShift 1
71#define ImplicitOps (OpImplicit << DstShift)
72#define DstReg (OpReg << DstShift)
73#define DstMem (OpMem << DstShift)
74#define DstAcc (OpAcc << DstShift)
75#define DstDI (OpDI << DstShift)
76#define DstMem64 (OpMem64 << DstShift)
77#define DstImmUByte (OpImmUByte << DstShift)
78#define DstDX (OpDX << DstShift)
79#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080080/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030081#define SrcShift 6
82#define SrcNone (OpNone << SrcShift)
83#define SrcReg (OpReg << SrcShift)
84#define SrcMem (OpMem << SrcShift)
85#define SrcMem16 (OpMem16 << SrcShift)
86#define SrcMem32 (OpMem32 << SrcShift)
87#define SrcImm (OpImm << SrcShift)
88#define SrcImmByte (OpImmByte << SrcShift)
89#define SrcOne (OpOne << SrcShift)
90#define SrcImmUByte (OpImmUByte << SrcShift)
91#define SrcImmU (OpImmU << SrcShift)
92#define SrcSI (OpSI << SrcShift)
93#define SrcImmFAddr (OpImmFAddr << SrcShift)
94#define SrcMemFAddr (OpMemFAddr << SrcShift)
95#define SrcAcc (OpAcc << SrcShift)
96#define SrcImmU16 (OpImmU16 << SrcShift)
97#define SrcDX (OpDX << SrcShift)
98#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -030099#define BitOp (1<<11)
100#define MemAbs (1<<12) /* Memory operand is absolute displacement */
101#define String (1<<13) /* String instruction (rep capable) */
102#define Stack (1<<14) /* Stack instruction (push/pop) */
103#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
104#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
105#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
106#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
107#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
108#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300109/* Generic ModRM decode. */
110#define ModRM (1<<19)
111/* Destination is only written; never read. */
112#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300113/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200114#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200115#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300116#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300117#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300118#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200119#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200120#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300121#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100122/* Source 2 operand type */
Avi Kivity4dd6a572011-09-13 10:45:43 +0300123#define Src2Shift (29)
124#define Src2None (OpNone << Src2Shift)
125#define Src2CL (OpCL << Src2Shift)
126#define Src2ImmByte (OpImmByte << Src2Shift)
127#define Src2One (OpOne << Src2Shift)
128#define Src2Imm (OpImm << Src2Shift)
129#define Src2Mask (OpMask << Src2Shift)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
Avi Kivityd0e53322010-07-29 15:11:54 +0300131#define X2(x...) x, x
132#define X3(x...) X2(x), x
133#define X4(x...) X2(x), X2(x)
134#define X5(x...) X4(x), x
135#define X6(x...) X4(x), X2(x)
136#define X7(x...) X4(x), X3(x)
137#define X8(x...) X4(x), X4(x)
138#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300139
Avi Kivityd65b1de2010-07-29 15:11:35 +0300140struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300141 u64 flags : 56;
142 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300143 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300144 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300145 struct opcode *group;
146 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200147 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300148 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200149 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300150};
151
152struct group_dual {
153 struct opcode mod012[8];
154 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300155};
156
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200157struct gprefix {
158 struct opcode pfx_no;
159 struct opcode pfx_66;
160 struct opcode pfx_f2;
161 struct opcode pfx_f3;
162};
163
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200165#define EFLG_ID (1<<21)
166#define EFLG_VIP (1<<20)
167#define EFLG_VIF (1<<19)
168#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200169#define EFLG_VM (1<<17)
170#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200171#define EFLG_IOPL (3<<12)
172#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173#define EFLG_OF (1<<11)
174#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200175#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200176#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177#define EFLG_SF (1<<7)
178#define EFLG_ZF (1<<6)
179#define EFLG_AF (1<<4)
180#define EFLG_PF (1<<2)
181#define EFLG_CF (1<<0)
182
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300183#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
184#define EFLG_RESERVED_ONE_MASK 2
185
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186/*
187 * Instruction emulation:
188 * Most instructions are emulated directly via a fragment of inline assembly
189 * code. This allows us to save/restore EFLAGS and thus very easily pick up
190 * any modified flags.
191 */
192
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800193#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194#define _LO32 "k" /* force 32-bit operand */
195#define _STK "%%rsp" /* stack pointer */
196#elif defined(__i386__)
197#define _LO32 "" /* force 32-bit operand */
198#define _STK "%%esp" /* stack pointer */
199#endif
200
201/*
202 * These EFLAGS bits are restored from saved value during emulation, and
203 * any changes are written back to the saved value after emulation.
204 */
205#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
206
207/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200208#define _PRE_EFLAGS(_sav, _msk, _tmp) \
209 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
210 "movl %"_sav",%"_LO32 _tmp"; " \
211 "push %"_tmp"; " \
212 "push %"_tmp"; " \
213 "movl %"_msk",%"_LO32 _tmp"; " \
214 "andl %"_LO32 _tmp",("_STK"); " \
215 "pushf; " \
216 "notl %"_LO32 _tmp"; " \
217 "andl %"_LO32 _tmp",("_STK"); " \
218 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
219 "pop %"_tmp"; " \
220 "orl %"_LO32 _tmp",("_STK"); " \
221 "popf; " \
222 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223
224/* After executing instruction: write-back necessary bits in EFLAGS. */
225#define _POST_EFLAGS(_sav, _msk, _tmp) \
226 /* _sav |= EFLAGS & _msk; */ \
227 "pushf; " \
228 "pop %"_tmp"; " \
229 "andl %"_msk",%"_LO32 _tmp"; " \
230 "orl %"_LO32 _tmp",%"_sav"; "
231
Avi Kivitydda96d82008-11-26 15:14:10 +0200232#ifdef CONFIG_X86_64
233#define ON64(x) x
234#else
235#define ON64(x)
236#endif
237
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300238#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200239 do { \
240 __asm__ __volatile__ ( \
241 _PRE_EFLAGS("0", "4", "2") \
242 _op _suffix " %"_x"3,%1; " \
243 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300244 : "=m" ((ctxt)->eflags), \
245 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200246 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300247 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200248 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200249
250
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300252#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200253 do { \
254 unsigned long _tmp; \
255 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300256 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200257 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300258 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200259 break; \
260 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300261 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200262 break; \
263 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300264 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200265 break; \
266 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267 } while (0)
268
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300269#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200271 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300272 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300274 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275 break; \
276 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300277 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278 _wx, _wy, _lx, _ly, _qx, _qy); \
279 break; \
280 } \
281 } while (0)
282
283/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300284#define emulate_2op_SrcB(ctxt, _op) \
285 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286
287/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300288#define emulate_2op_SrcV(ctxt, _op) \
289 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
291/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300292#define emulate_2op_SrcV_nobyte(ctxt, _op) \
293 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100295/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300296#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300297 do { \
298 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300299 _type _clv = (ctxt)->src2.val; \
300 _type _srcv = (ctxt)->src.val; \
301 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300302 \
303 __asm__ __volatile__ ( \
304 _PRE_EFLAGS("0", "5", "2") \
305 _op _suffix " %4,%1 \n" \
306 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300307 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300308 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
309 ); \
310 \
Avi Kivity761441b2011-09-07 16:41:36 +0300311 (ctxt)->src2.val = (unsigned long) _clv; \
312 (ctxt)->src2.val = (unsigned long) _srcv; \
313 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100314 } while (0)
315
Avi Kivity761441b2011-09-07 16:41:36 +0300316#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300317 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300318 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300319 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300320 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300321 break; \
322 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300323 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300324 break; \
325 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300326 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300327 break; \
328 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100329 } while (0)
330
Avi Kivityd1eef452011-09-07 16:41:38 +0300331#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332 do { \
333 unsigned long _tmp; \
334 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200335 __asm__ __volatile__ ( \
336 _PRE_EFLAGS("0", "3", "2") \
337 _op _suffix " %1; " \
338 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300339 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200340 "=&r" (_tmp) \
341 : "i" (EFLAGS_MASK)); \
342 } while (0)
343
344/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300345#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200346 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300347 switch ((ctxt)->dst.bytes) { \
348 case 1: __emulate_1op(ctxt, _op, "b"); break; \
349 case 2: __emulate_1op(ctxt, _op, "w"); break; \
350 case 4: __emulate_1op(ctxt, _op, "l"); break; \
351 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352 } \
353 } while (0)
354
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300355#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300356 do { \
357 unsigned long _tmp; \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300358 ulong *rax = &(ctxt)->regs[VCPU_REGS_RAX]; \
359 ulong *rdx = &(ctxt)->regs[VCPU_REGS_RDX]; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300360 \
361 __asm__ __volatile__ ( \
362 _PRE_EFLAGS("0", "5", "1") \
363 "1: \n\t" \
364 _op _suffix " %6; " \
365 "2: \n\t" \
366 _POST_EFLAGS("0", "5", "1") \
367 ".pushsection .fixup,\"ax\" \n\t" \
368 "3: movb $1, %4 \n\t" \
369 "jmp 2b \n\t" \
370 ".popsection \n\t" \
371 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300372 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
373 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
374 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
375 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300376 } while (0)
377
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300378/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300379#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300380 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300381 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300382 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300383 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300384 break; \
385 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300386 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300387 break; \
388 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300389 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300390 break; \
391 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300392 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300393 break; \
394 } \
395 } while (0)
396
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200397static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
398 enum x86_intercept intercept,
399 enum x86_intercept_stage stage)
400{
401 struct x86_instruction_info info = {
402 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300403 .rep_prefix = ctxt->rep_prefix,
404 .modrm_mod = ctxt->modrm_mod,
405 .modrm_reg = ctxt->modrm_reg,
406 .modrm_rm = ctxt->modrm_rm,
407 .src_val = ctxt->src.val64,
408 .src_bytes = ctxt->src.bytes,
409 .dst_bytes = ctxt->dst.bytes,
410 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200411 .next_rip = ctxt->eip,
412 };
413
Avi Kivity29535382011-04-20 13:37:53 +0300414 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200415}
416
Avi Kivity9dac77f2011-06-01 15:34:25 +0300417static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800418{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300419 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800420}
421
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800423static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300424address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800425{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300426 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800427 return reg;
428 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300429 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800430}
431
432static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300433register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800434{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300435 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800436}
437
Harvey Harrison7a9572752008-02-19 07:40:41 -0800438static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300439register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800440{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300441 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800442 *reg += inc;
443 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300444 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800445}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446
Avi Kivity9dac77f2011-06-01 15:34:25 +0300447static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800448{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300449 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800450}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300451
Avi Kivity56697682011-04-03 14:08:51 +0300452static u32 desc_limit_scaled(struct desc_struct *desc)
453{
454 u32 limit = get_desc_limit(desc);
455
456 return desc->g ? (limit << 12) | 0xfff : limit;
457}
458
Avi Kivity9dac77f2011-06-01 15:34:25 +0300459static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300460{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300461 ctxt->has_seg_override = true;
462 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300463}
464
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900465static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300466{
467 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
468 return 0;
469
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900470 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300471}
472
Avi Kivity9dac77f2011-06-01 15:34:25 +0300473static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300474{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300475 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300476 return 0;
477
Avi Kivity9dac77f2011-06-01 15:34:25 +0300478 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300479}
480
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200481static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
482 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300483{
Avi Kivityda9cb572010-11-22 17:53:21 +0200484 ctxt->exception.vector = vec;
485 ctxt->exception.error_code = error;
486 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200487 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300488}
489
Joerg Roedel3b88e412011-04-04 12:39:29 +0200490static int emulate_db(struct x86_emulate_ctxt *ctxt)
491{
492 return emulate_exception(ctxt, DB_VECTOR, 0, false);
493}
494
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200495static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300496{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200497 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300498}
499
Avi Kivity618ff152011-04-03 12:32:09 +0300500static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
501{
502 return emulate_exception(ctxt, SS_VECTOR, err, true);
503}
504
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200505static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300506{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200507 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300508}
509
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200510static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300511{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200512 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300513}
514
Avi Kivity34d1f492010-08-26 11:59:01 +0300515static int emulate_de(struct x86_emulate_ctxt *ctxt)
516{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200517 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300518}
519
Avi Kivity12537912011-03-29 11:41:27 +0200520static int emulate_nm(struct x86_emulate_ctxt *ctxt)
521{
522 return emulate_exception(ctxt, NM_VECTOR, 0, false);
523}
524
Avi Kivity1aa36612011-04-27 13:20:30 +0300525static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
526{
527 u16 selector;
528 struct desc_struct desc;
529
530 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
531 return selector;
532}
533
534static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
535 unsigned seg)
536{
537 u16 dummy;
538 u32 base3;
539 struct desc_struct desc;
540
541 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
542 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
543}
544
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400545static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300546 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400547 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300548 ulong *linear)
549{
Avi Kivity618ff152011-04-03 12:32:09 +0300550 struct desc_struct desc;
551 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300552 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300553 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300554 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300555 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300556
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900557 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300558 switch (ctxt->mode) {
559 case X86EMUL_MODE_REAL:
560 break;
561 case X86EMUL_MODE_PROT64:
562 if (((signed long)la << 16) >> 16 != la)
563 return emulate_gp(ctxt, 0);
564 break;
565 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300566 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
567 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300568 if (!usable)
569 goto bad;
570 /* code segment or read-only data segment */
571 if (((desc.type & 8) || !(desc.type & 2)) && write)
572 goto bad;
573 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400574 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300575 goto bad;
576 lim = desc_limit_scaled(&desc);
577 if ((desc.type & 8) || !(desc.type & 4)) {
578 /* expand-up segment */
579 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
580 goto bad;
581 } else {
582 /* exapand-down segment */
583 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
584 goto bad;
585 lim = desc.d ? 0xffffffff : 0xffff;
586 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
587 goto bad;
588 }
Avi Kivity717746e2011-04-20 13:37:53 +0300589 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300590 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300591 cpl = max(cpl, rpl);
592 if (!(desc.type & 8)) {
593 /* data segment */
594 if (cpl > desc.dpl)
595 goto bad;
596 } else if ((desc.type & 8) && !(desc.type & 4)) {
597 /* nonconforming code segment */
598 if (cpl != desc.dpl)
599 goto bad;
600 } else if ((desc.type & 8) && (desc.type & 4)) {
601 /* conforming code segment */
602 if (cpl < desc.dpl)
603 goto bad;
604 }
605 break;
606 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300607 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300608 la &= (u32)-1;
609 *linear = la;
610 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300611bad:
612 if (addr.seg == VCPU_SREG_SS)
613 return emulate_ss(ctxt, addr.seg);
614 else
615 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300616}
617
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400618static int linearize(struct x86_emulate_ctxt *ctxt,
619 struct segmented_address addr,
620 unsigned size, bool write,
621 ulong *linear)
622{
623 return __linearize(ctxt, addr, size, write, false, linear);
624}
625
626
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200627static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
628 struct segmented_address addr,
629 void *data,
630 unsigned size)
631{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200632 int rc;
633 ulong linear;
634
Avi Kivity83b87952011-04-03 11:31:19 +0300635 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200636 if (rc != X86EMUL_CONTINUE)
637 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300638 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200639}
640
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900641/*
642 * Fetch the next byte of the instruction being emulated which is pointed to
643 * by ctxt->_eip, then increment ctxt->_eip.
644 *
645 * Also prefetch the remaining bytes of the instruction without crossing page
646 * boundary if they are not in fetch_cache yet.
647 */
648static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200649{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300650 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200651 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300652 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200653
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900654 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400655 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900656 struct segmented_address addr = { .seg = VCPU_SREG_CS,
657 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300658 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900659 size = min(15UL - cur_size,
660 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400661 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900662 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400663 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900664 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
665 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900666 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200667 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300668 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200669 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900670 *dest = fc->data[ctxt->_eip - fc->start];
671 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900672 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200673}
674
675static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900676 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200677{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900678 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200679
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200680 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900681 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200682 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200683 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900684 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900685 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200686 return rc;
687 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900688 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200689}
690
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900691/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900692#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900693({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900694 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900695 if (rc != X86EMUL_CONTINUE) \
696 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900697 (_type)_x; \
698})
699
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900700#define insn_fetch_arr(_arr, _size, _ctxt) \
701({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900702 if (rc != X86EMUL_CONTINUE) \
703 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900704})
705
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000706/*
707 * Given the 'reg' portion of a ModRM byte, and a register block, return a
708 * pointer into the block that addresses the relevant register.
709 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
710 */
711static void *decode_register(u8 modrm_reg, unsigned long *regs,
712 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713{
714 void *p;
715
716 p = &regs[modrm_reg];
717 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
718 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
719 return p;
720}
721
722static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200723 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 u16 *size, unsigned long *address, int op_bytes)
725{
726 int rc;
727
728 if (op_bytes == 2)
729 op_bytes = 3;
730 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200731 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900732 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200734 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200735 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 return rc;
737}
738
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300739static int test_cc(unsigned int condition, unsigned int flags)
740{
741 int rc = 0;
742
743 switch ((condition & 15) >> 1) {
744 case 0: /* o */
745 rc |= (flags & EFLG_OF);
746 break;
747 case 1: /* b/c/nae */
748 rc |= (flags & EFLG_CF);
749 break;
750 case 2: /* z/e */
751 rc |= (flags & EFLG_ZF);
752 break;
753 case 3: /* be/na */
754 rc |= (flags & (EFLG_CF|EFLG_ZF));
755 break;
756 case 4: /* s */
757 rc |= (flags & EFLG_SF);
758 break;
759 case 5: /* p/pe */
760 rc |= (flags & EFLG_PF);
761 break;
762 case 7: /* le/ng */
763 rc |= (flags & EFLG_ZF);
764 /* fall through */
765 case 6: /* l/nge */
766 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
767 break;
768 }
769
770 /* Odd condition identifiers (lsb == 1) have inverted sense. */
771 return (!!rc ^ (condition & 1));
772}
773
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300774static void fetch_register_operand(struct operand *op)
775{
776 switch (op->bytes) {
777 case 1:
778 op->val = *(u8 *)op->addr.reg;
779 break;
780 case 2:
781 op->val = *(u16 *)op->addr.reg;
782 break;
783 case 4:
784 op->val = *(u32 *)op->addr.reg;
785 break;
786 case 8:
787 op->val = *(u64 *)op->addr.reg;
788 break;
789 }
790}
791
Avi Kivity12537912011-03-29 11:41:27 +0200792static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
793{
794 ctxt->ops->get_fpu(ctxt);
795 switch (reg) {
796 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
797 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
798 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
799 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
800 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
801 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
802 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
803 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
804#ifdef CONFIG_X86_64
805 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
806 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
807 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
808 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
809 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
810 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
811 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
812 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
813#endif
814 default: BUG();
815 }
816 ctxt->ops->put_fpu(ctxt);
817}
818
819static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
820 int reg)
821{
822 ctxt->ops->get_fpu(ctxt);
823 switch (reg) {
824 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
825 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
826 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
827 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
828 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
829 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
830 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
831 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
832#ifdef CONFIG_X86_64
833 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
834 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
835 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
836 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
837 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
838 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
839 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
840 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
841#endif
842 default: BUG();
843 }
844 ctxt->ops->put_fpu(ctxt);
845}
846
847static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
848 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200849 int inhibit_bytereg)
850{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300851 unsigned reg = ctxt->modrm_reg;
852 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200853
Avi Kivity9dac77f2011-06-01 15:34:25 +0300854 if (!(ctxt->d & ModRM))
855 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200856
Avi Kivity9dac77f2011-06-01 15:34:25 +0300857 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200858 op->type = OP_XMM;
859 op->bytes = 16;
860 op->addr.xmm = reg;
861 read_sse_reg(ctxt, &op->vec_val, reg);
862 return;
863 }
864
Avi Kivity3c118e22007-10-31 10:27:04 +0200865 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300866 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
867 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200868 op->bytes = 1;
869 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300870 op->addr.reg = decode_register(reg, ctxt->regs, 0);
871 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200872 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300873 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200874 op->orig_val = op->val;
875}
876
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200877static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300878 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200879{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200880 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700881 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900882 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300883 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200884
Avi Kivity9dac77f2011-06-01 15:34:25 +0300885 if (ctxt->rex_prefix) {
886 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
887 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
888 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200889 }
890
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900891 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300892 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
893 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
894 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
895 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200896
Avi Kivity9dac77f2011-06-01 15:34:25 +0300897 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300898 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300899 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
900 op->addr.reg = decode_register(ctxt->modrm_rm,
901 ctxt->regs, ctxt->d & ByteOp);
902 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200903 op->type = OP_XMM;
904 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300905 op->addr.xmm = ctxt->modrm_rm;
906 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200907 return rc;
908 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300909 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200910 return rc;
911 }
912
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300913 op->type = OP_MEM;
914
Avi Kivity9dac77f2011-06-01 15:34:25 +0300915 if (ctxt->ad_bytes == 2) {
916 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
917 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
918 unsigned si = ctxt->regs[VCPU_REGS_RSI];
919 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200920
921 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300922 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200923 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300924 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900925 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200926 break;
927 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900928 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200929 break;
930 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900931 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932 break;
933 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300934 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300936 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200937 break;
938 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300939 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200940 break;
941 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300942 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200943 break;
944 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300945 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200946 break;
947 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300948 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200949 break;
950 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300951 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200952 break;
953 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300954 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300955 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200956 break;
957 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300958 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200959 break;
960 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300961 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
962 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
963 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300964 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200965 } else {
966 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300967 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900968 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200969 index_reg |= (sib >> 3) & 7;
970 base_reg |= sib & 7;
971 scale = sib >> 6;
972
Avi Kivity9dac77f2011-06-01 15:34:25 +0300973 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900974 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700975 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300976 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700977 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300978 modrm_ea += ctxt->regs[index_reg] << scale;
979 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700980 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300981 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700982 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300983 modrm_ea += ctxt->regs[ctxt->modrm_rm];
984 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200985 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300986 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900987 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200988 break;
989 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900990 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200991 break;
992 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900993 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200994 break;
995 }
996 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200997 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200998done:
999 return rc;
1000}
1001
1002static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001003 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001005 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001007 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001008 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001009 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001010 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001011 break;
1012 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001013 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001014 break;
1015 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001016 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001017 break;
1018 }
1019done:
1020 return rc;
1021}
1022
Avi Kivity9dac77f2011-06-01 15:34:25 +03001023static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001024{
Sheng Yang7129eec2010-09-28 16:33:32 +08001025 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001026
Avi Kivity9dac77f2011-06-01 15:34:25 +03001027 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1028 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001029
Avi Kivity9dac77f2011-06-01 15:34:25 +03001030 if (ctxt->src.bytes == 2)
1031 sv = (s16)ctxt->src.val & (s16)mask;
1032 else if (ctxt->src.bytes == 4)
1033 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001034
Avi Kivity9dac77f2011-06-01 15:34:25 +03001035 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001036 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001037
1038 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001039 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001040}
1041
Gleb Natapov9de41572010-04-28 19:15:22 +03001042static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001043 unsigned long addr, void *dest, unsigned size)
1044{
1045 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001046 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001047
1048 while (size) {
1049 int n = min(size, 8u);
1050 size -= n;
1051 if (mc->pos < mc->end)
1052 goto read_cached;
1053
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001054 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1055 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001056 if (rc != X86EMUL_CONTINUE)
1057 return rc;
1058 mc->end += n;
1059
1060 read_cached:
1061 memcpy(dest, mc->data + mc->pos, n);
1062 mc->pos += n;
1063 dest += n;
1064 addr += n;
1065 }
1066 return X86EMUL_CONTINUE;
1067}
1068
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001069static int segmented_read(struct x86_emulate_ctxt *ctxt,
1070 struct segmented_address addr,
1071 void *data,
1072 unsigned size)
1073{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001074 int rc;
1075 ulong linear;
1076
Avi Kivity83b87952011-04-03 11:31:19 +03001077 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001078 if (rc != X86EMUL_CONTINUE)
1079 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001080 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001081}
1082
1083static int segmented_write(struct x86_emulate_ctxt *ctxt,
1084 struct segmented_address addr,
1085 const void *data,
1086 unsigned size)
1087{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001088 int rc;
1089 ulong linear;
1090
Avi Kivity83b87952011-04-03 11:31:19 +03001091 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001092 if (rc != X86EMUL_CONTINUE)
1093 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001094 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1095 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001096}
1097
1098static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1099 struct segmented_address addr,
1100 const void *orig_data, const void *data,
1101 unsigned size)
1102{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001103 int rc;
1104 ulong linear;
1105
Avi Kivity83b87952011-04-03 11:31:19 +03001106 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001107 if (rc != X86EMUL_CONTINUE)
1108 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001109 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1110 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001111}
1112
Gleb Natapov7b262e92010-03-18 15:20:27 +02001113static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001114 unsigned int size, unsigned short port,
1115 void *dest)
1116{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001117 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001118
1119 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001120 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001121 unsigned int count = ctxt->rep_prefix ?
1122 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001123 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001124 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1125 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001126 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1127 count);
1128 if (n == 0)
1129 n = 1;
1130 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001131 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001132 return 0;
1133 rc->end = n * size;
1134 }
1135
1136 memcpy(dest, rc->data + rc->pos, size);
1137 rc->pos += size;
1138 return 1;
1139}
1140
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001141static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001142 u16 selector, struct desc_ptr *dt)
1143{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001144 struct x86_emulate_ops *ops = ctxt->ops;
1145
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001146 if (selector & 1 << 2) {
1147 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001148 u16 sel;
1149
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001150 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001151 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001152 return;
1153
1154 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1155 dt->address = get_desc_base(&desc);
1156 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001157 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001158}
1159
1160/* allowed just for 8 bytes segments */
1161static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001162 u16 selector, struct desc_struct *desc)
1163{
1164 struct desc_ptr dt;
1165 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001166 ulong addr;
1167
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001168 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001169
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001170 if (dt.size < index * 8 + 7)
1171 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001172
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001173 addr = dt.address + index * 8;
1174 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1175 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001176}
1177
1178/* allowed just for 8 bytes segments */
1179static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001180 u16 selector, struct desc_struct *desc)
1181{
1182 struct desc_ptr dt;
1183 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001184 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001185
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001186 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001187
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001188 if (dt.size < index * 8 + 7)
1189 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001190
1191 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001192 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1193 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001194}
1195
Gleb Natapov5601d052011-03-07 14:55:06 +02001196/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001197static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198 u16 selector, int seg)
1199{
1200 struct desc_struct seg_desc;
1201 u8 dpl, rpl, cpl;
1202 unsigned err_vec = GP_VECTOR;
1203 u32 err_code = 0;
1204 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1205 int ret;
1206
1207 memset(&seg_desc, 0, sizeof seg_desc);
1208
1209 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1210 || ctxt->mode == X86EMUL_MODE_REAL) {
1211 /* set real mode segment descriptor */
1212 set_desc_base(&seg_desc, selector << 4);
1213 set_desc_limit(&seg_desc, 0xffff);
1214 seg_desc.type = 3;
1215 seg_desc.p = 1;
1216 seg_desc.s = 1;
1217 goto load;
1218 }
1219
1220 /* NULL selector is not valid for TR, CS and SS */
1221 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1222 && null_selector)
1223 goto exception;
1224
1225 /* TR should be in GDT only */
1226 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1227 goto exception;
1228
1229 if (null_selector) /* for NULL selector skip all following checks */
1230 goto load;
1231
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001232 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001233 if (ret != X86EMUL_CONTINUE)
1234 return ret;
1235
1236 err_code = selector & 0xfffc;
1237 err_vec = GP_VECTOR;
1238
1239 /* can't load system descriptor into segment selecor */
1240 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1241 goto exception;
1242
1243 if (!seg_desc.p) {
1244 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1245 goto exception;
1246 }
1247
1248 rpl = selector & 3;
1249 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001250 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001251
1252 switch (seg) {
1253 case VCPU_SREG_SS:
1254 /*
1255 * segment is not a writable data segment or segment
1256 * selector's RPL != CPL or segment selector's RPL != CPL
1257 */
1258 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1259 goto exception;
1260 break;
1261 case VCPU_SREG_CS:
1262 if (!(seg_desc.type & 8))
1263 goto exception;
1264
1265 if (seg_desc.type & 4) {
1266 /* conforming */
1267 if (dpl > cpl)
1268 goto exception;
1269 } else {
1270 /* nonconforming */
1271 if (rpl > cpl || dpl != cpl)
1272 goto exception;
1273 }
1274 /* CS(RPL) <- CPL */
1275 selector = (selector & 0xfffc) | cpl;
1276 break;
1277 case VCPU_SREG_TR:
1278 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1279 goto exception;
1280 break;
1281 case VCPU_SREG_LDTR:
1282 if (seg_desc.s || seg_desc.type != 2)
1283 goto exception;
1284 break;
1285 default: /* DS, ES, FS, or GS */
1286 /*
1287 * segment is not a data or readable code segment or
1288 * ((segment is a data or nonconforming code segment)
1289 * and (both RPL and CPL > DPL))
1290 */
1291 if ((seg_desc.type & 0xa) == 0x8 ||
1292 (((seg_desc.type & 0xc) != 0xc) &&
1293 (rpl > dpl && cpl > dpl)))
1294 goto exception;
1295 break;
1296 }
1297
1298 if (seg_desc.s) {
1299 /* mark segment as accessed */
1300 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001301 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001302 if (ret != X86EMUL_CONTINUE)
1303 return ret;
1304 }
1305load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001306 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001307 return X86EMUL_CONTINUE;
1308exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001309 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001310 return X86EMUL_PROPAGATE_FAULT;
1311}
1312
Wei Yongjun31be40b2010-08-17 09:17:30 +08001313static void write_register_operand(struct operand *op)
1314{
1315 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1316 switch (op->bytes) {
1317 case 1:
1318 *(u8 *)op->addr.reg = (u8)op->val;
1319 break;
1320 case 2:
1321 *(u16 *)op->addr.reg = (u16)op->val;
1322 break;
1323 case 4:
1324 *op->addr.reg = (u32)op->val;
1325 break; /* 64b: zero-extend */
1326 case 8:
1327 *op->addr.reg = op->val;
1328 break;
1329 }
1330}
1331
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001332static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001333{
1334 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001335
Avi Kivity9dac77f2011-06-01 15:34:25 +03001336 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001337 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001338 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001339 break;
1340 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001341 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001342 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001343 ctxt->dst.addr.mem,
1344 &ctxt->dst.orig_val,
1345 &ctxt->dst.val,
1346 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001347 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001348 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001349 ctxt->dst.addr.mem,
1350 &ctxt->dst.val,
1351 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001352 if (rc != X86EMUL_CONTINUE)
1353 return rc;
1354 break;
Avi Kivity12537912011-03-29 11:41:27 +02001355 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001356 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001357 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001358 case OP_NONE:
1359 /* no writeback */
1360 break;
1361 default:
1362 break;
1363 }
1364 return X86EMUL_CONTINUE;
1365}
1366
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001367static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001368{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001369 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001370
Avi Kivity9dac77f2011-06-01 15:34:25 +03001371 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1372 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001373 addr.seg = VCPU_SREG_SS;
1374
1375 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001376 ctxt->dst.type = OP_NONE;
1377 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001378}
1379
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001380static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001381 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001383 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001384 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001385
Avi Kivity9dac77f2011-06-01 15:34:25 +03001386 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001387 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001388 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001389 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001390 return rc;
1391
Avi Kivity9dac77f2011-06-01 15:34:25 +03001392 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001393 return rc;
1394}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001395
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001396static int em_pop(struct x86_emulate_ctxt *ctxt)
1397{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001398 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001399}
1400
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001401static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001402 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001403{
1404 int rc;
1405 unsigned long val, change_mask;
1406 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001407 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001408
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001409 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001410 if (rc != X86EMUL_CONTINUE)
1411 return rc;
1412
1413 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1414 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1415
1416 switch(ctxt->mode) {
1417 case X86EMUL_MODE_PROT64:
1418 case X86EMUL_MODE_PROT32:
1419 case X86EMUL_MODE_PROT16:
1420 if (cpl == 0)
1421 change_mask |= EFLG_IOPL;
1422 if (cpl <= iopl)
1423 change_mask |= EFLG_IF;
1424 break;
1425 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001426 if (iopl < 3)
1427 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001428 change_mask |= EFLG_IF;
1429 break;
1430 default: /* real mode */
1431 change_mask |= (EFLG_IOPL | EFLG_IF);
1432 break;
1433 }
1434
1435 *(unsigned long *)dest =
1436 (ctxt->eflags & ~change_mask) | (val & change_mask);
1437
1438 return rc;
1439}
1440
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001441static int em_popf(struct x86_emulate_ctxt *ctxt)
1442{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001443 ctxt->dst.type = OP_REG;
1444 ctxt->dst.addr.reg = &ctxt->eflags;
1445 ctxt->dst.bytes = ctxt->op_bytes;
1446 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001447}
1448
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001449static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001450{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001451 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001452
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001453 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001454}
1455
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001456static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001457{
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001458 unsigned long selector;
1459 int rc;
1460
Avi Kivity9dac77f2011-06-01 15:34:25 +03001461 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001462 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001463 return rc;
1464
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001465 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001466 return rc;
1467}
1468
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001469static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001470{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001471 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001472 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001473 int reg = VCPU_REGS_RAX;
1474
1475 while (reg <= VCPU_REGS_RDI) {
1476 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001477 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001478
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001479 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001480 if (rc != X86EMUL_CONTINUE)
1481 return rc;
1482
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001483 ++reg;
1484 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001485
Wei Yongjunc37eda12010-06-15 09:03:33 +08001486 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001487}
1488
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001489static int em_pushf(struct x86_emulate_ctxt *ctxt)
1490{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001491 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001492 return em_push(ctxt);
1493}
1494
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001495static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001496{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001497 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001498 int reg = VCPU_REGS_RDI;
1499
1500 while (reg >= VCPU_REGS_RAX) {
1501 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001502 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1503 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001504 --reg;
1505 }
1506
Avi Kivity9dac77f2011-06-01 15:34:25 +03001507 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001508 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001509 break;
1510 --reg;
1511 }
1512 return rc;
1513}
1514
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001515int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001516{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001517 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001518 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001519 struct desc_ptr dt;
1520 gva_t cs_addr;
1521 gva_t eip_addr;
1522 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001523
1524 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001525 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001526 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001527 if (rc != X86EMUL_CONTINUE)
1528 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001529
1530 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1531
Avi Kivity9dac77f2011-06-01 15:34:25 +03001532 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001533 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001534 if (rc != X86EMUL_CONTINUE)
1535 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001536
Avi Kivity9dac77f2011-06-01 15:34:25 +03001537 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001538 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001539 if (rc != X86EMUL_CONTINUE)
1540 return rc;
1541
Avi Kivity4bff1e862011-04-20 13:37:53 +03001542 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001543
1544 eip_addr = dt.address + (irq << 2);
1545 cs_addr = dt.address + (irq << 2) + 2;
1546
Avi Kivity0f65dd72011-04-20 13:37:53 +03001547 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001548 if (rc != X86EMUL_CONTINUE)
1549 return rc;
1550
Avi Kivity0f65dd72011-04-20 13:37:53 +03001551 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001552 if (rc != X86EMUL_CONTINUE)
1553 return rc;
1554
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001555 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001556 if (rc != X86EMUL_CONTINUE)
1557 return rc;
1558
Avi Kivity9dac77f2011-06-01 15:34:25 +03001559 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001560
1561 return rc;
1562}
1563
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001564static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001565{
1566 switch(ctxt->mode) {
1567 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001568 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001569 case X86EMUL_MODE_VM86:
1570 case X86EMUL_MODE_PROT16:
1571 case X86EMUL_MODE_PROT32:
1572 case X86EMUL_MODE_PROT64:
1573 default:
1574 /* Protected mode interrupts unimplemented yet */
1575 return X86EMUL_UNHANDLEABLE;
1576 }
1577}
1578
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001579static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001580{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001581 int rc = X86EMUL_CONTINUE;
1582 unsigned long temp_eip = 0;
1583 unsigned long temp_eflags = 0;
1584 unsigned long cs = 0;
1585 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1586 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1587 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1588 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1589
1590 /* TODO: Add stack limit check */
1591
Avi Kivity9dac77f2011-06-01 15:34:25 +03001592 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001593
1594 if (rc != X86EMUL_CONTINUE)
1595 return rc;
1596
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001597 if (temp_eip & ~0xffff)
1598 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001599
Avi Kivity9dac77f2011-06-01 15:34:25 +03001600 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001601
1602 if (rc != X86EMUL_CONTINUE)
1603 return rc;
1604
Avi Kivity9dac77f2011-06-01 15:34:25 +03001605 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001606
1607 if (rc != X86EMUL_CONTINUE)
1608 return rc;
1609
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001610 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001611
1612 if (rc != X86EMUL_CONTINUE)
1613 return rc;
1614
Avi Kivity9dac77f2011-06-01 15:34:25 +03001615 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001616
1617
Avi Kivity9dac77f2011-06-01 15:34:25 +03001618 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001619 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001620 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001621 ctxt->eflags &= ~0xffff;
1622 ctxt->eflags |= temp_eflags;
1623 }
1624
1625 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1626 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1627
1628 return rc;
1629}
1630
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001631static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001632{
1633 switch(ctxt->mode) {
1634 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001635 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001636 case X86EMUL_MODE_VM86:
1637 case X86EMUL_MODE_PROT16:
1638 case X86EMUL_MODE_PROT32:
1639 case X86EMUL_MODE_PROT64:
1640 default:
1641 /* iret from protected mode unimplemented yet */
1642 return X86EMUL_UNHANDLEABLE;
1643 }
1644}
1645
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001646static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1647{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001648 int rc;
1649 unsigned short sel;
1650
Avi Kivity9dac77f2011-06-01 15:34:25 +03001651 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001652
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001653 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001654 if (rc != X86EMUL_CONTINUE)
1655 return rc;
1656
Avi Kivity9dac77f2011-06-01 15:34:25 +03001657 ctxt->_eip = 0;
1658 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001659 return X86EMUL_CONTINUE;
1660}
1661
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001662static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001663{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001664 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665}
1666
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001667static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001668{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001669 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001670 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001671 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001672 break;
1673 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001674 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001675 break;
1676 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001677 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001678 break;
1679 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001680 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681 break;
1682 case 4: /* sal/shl */
1683 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001684 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001685 break;
1686 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001687 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001688 break;
1689 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001690 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001691 break;
1692 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001693 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001694}
1695
Avi Kivity3329ece2011-09-13 10:45:39 +03001696static int em_not(struct x86_emulate_ctxt *ctxt)
1697{
1698 ctxt->dst.val = ~ctxt->dst.val;
1699 return X86EMUL_CONTINUE;
1700}
1701
1702static int em_neg(struct x86_emulate_ctxt *ctxt)
1703{
1704 emulate_1op(ctxt, "neg");
1705 return X86EMUL_CONTINUE;
1706}
1707
1708static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1709{
1710 u8 ex = 0;
1711
1712 emulate_1op_rax_rdx(ctxt, "mul", ex);
1713 return X86EMUL_CONTINUE;
1714}
1715
1716static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1717{
1718 u8 ex = 0;
1719
1720 emulate_1op_rax_rdx(ctxt, "imul", ex);
1721 return X86EMUL_CONTINUE;
1722}
1723
1724static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001725{
Avi Kivity34d1f492010-08-26 11:59:01 +03001726 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001727
Avi Kivity3329ece2011-09-13 10:45:39 +03001728 emulate_1op_rax_rdx(ctxt, "div", de);
1729 if (de)
1730 return emulate_de(ctxt);
1731 return X86EMUL_CONTINUE;
1732}
1733
1734static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1735{
1736 u8 de = 0;
1737
1738 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001739 if (de)
1740 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001741 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001742}
1743
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001744static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001746 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747
Avi Kivity9dac77f2011-06-01 15:34:25 +03001748 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001749 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001750 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001751 break;
1752 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001753 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001755 case 2: /* call near abs */ {
1756 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001757 old_eip = ctxt->_eip;
1758 ctxt->_eip = ctxt->src.val;
1759 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001760 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001761 break;
1762 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001764 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001765 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001766 case 5: /* jmp far */
1767 rc = em_jmp_far(ctxt);
1768 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001769 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001770 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001773 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774}
1775
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001776static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001778 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001779
Avi Kivity9dac77f2011-06-01 15:34:25 +03001780 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1781 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1782 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1783 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001784 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001785 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001786 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1787 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788
Laurent Vivier05f086f2007-09-24 11:10:55 +02001789 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001790 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001791 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001792}
1793
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001794static int em_ret(struct x86_emulate_ctxt *ctxt)
1795{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001796 ctxt->dst.type = OP_REG;
1797 ctxt->dst.addr.reg = &ctxt->_eip;
1798 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001799 return em_pop(ctxt);
1800}
1801
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001802static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001803{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001804 int rc;
1805 unsigned long cs;
1806
Avi Kivity9dac77f2011-06-01 15:34:25 +03001807 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001808 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001809 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001810 if (ctxt->op_bytes == 4)
1811 ctxt->_eip = (u32)ctxt->_eip;
1812 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001813 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001814 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001815 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001816 return rc;
1817}
1818
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001819static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001820{
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001821 unsigned short sel;
1822 int rc;
1823
Avi Kivity9dac77f2011-06-01 15:34:25 +03001824 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001825
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001826 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001827 if (rc != X86EMUL_CONTINUE)
1828 return rc;
1829
Avi Kivity9dac77f2011-06-01 15:34:25 +03001830 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001831 return rc;
1832}
1833
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001834static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001835setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001836 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001837{
Avi Kivity1aa36612011-04-27 13:20:30 +03001838 u16 selector;
1839
Gleb Natapov79168fd2010-04-28 19:15:30 +03001840 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001841 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001842 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001843
1844 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001845 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001846 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001847 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001848 cs->type = 0x0b; /* Read, Execute, Accessed */
1849 cs->s = 1;
1850 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001851 cs->p = 1;
1852 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001853
Gleb Natapov79168fd2010-04-28 19:15:30 +03001854 set_desc_base(ss, 0); /* flat segment */
1855 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001856 ss->g = 1; /* 4kb granularity */
1857 ss->s = 1;
1858 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001859 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001860 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001861 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001862}
1863
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001864static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001865{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001866 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001867 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001869 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001870 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001871
1872 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001873 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001874 ctxt->mode == X86EMUL_MODE_VM86)
1875 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001876
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001877 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001878 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879
Avi Kivity717746e2011-04-20 13:37:53 +03001880 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001881 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 cs_sel = (u16)(msr_data & 0xfffc);
1883 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001884
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001885 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001886 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001887 cs.l = 1;
1888 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001889 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1890 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891
Avi Kivity9dac77f2011-06-01 15:34:25 +03001892 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001893 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001894#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001895 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001896
Avi Kivity717746e2011-04-20 13:37:53 +03001897 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001898 ctxt->mode == X86EMUL_MODE_PROT64 ?
1899 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001900 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001901
Avi Kivity717746e2011-04-20 13:37:53 +03001902 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001903 ctxt->eflags &= ~(msr_data | EFLG_RF);
1904#endif
1905 } else {
1906 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001907 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001909
1910 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1911 }
1912
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001913 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001914}
1915
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001916static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001917{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001918 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001919 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001920 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001921 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001922 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001923
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001924 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001925 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001926 if (ctxt->mode == X86EMUL_MODE_REAL)
1927 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001928
1929 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1930 * Therefore, we inject an #UD.
1931 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001932 if (ctxt->mode == X86EMUL_MODE_PROT64)
1933 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001934
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001935 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001936
Avi Kivity717746e2011-04-20 13:37:53 +03001937 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001938 switch (ctxt->mode) {
1939 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001940 if ((msr_data & 0xfffc) == 0x0)
1941 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001942 break;
1943 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001944 if (msr_data == 0x0)
1945 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001946 break;
1947 }
1948
1949 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001950 cs_sel = (u16)msr_data;
1951 cs_sel &= ~SELECTOR_RPL_MASK;
1952 ss_sel = cs_sel + 8;
1953 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001954 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001955 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001956 cs.l = 1;
1957 }
1958
Avi Kivity1aa36612011-04-27 13:20:30 +03001959 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1960 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001961
Avi Kivity717746e2011-04-20 13:37:53 +03001962 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001963 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001964
Avi Kivity717746e2011-04-20 13:37:53 +03001965 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001966 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001967
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001968 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001969}
1970
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001971static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001972{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001973 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001974 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001975 u64 msr_data;
1976 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001977 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001978
Gleb Natapova0044752010-02-10 14:21:31 +02001979 /* inject #GP if in real mode or Virtual 8086 mode */
1980 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001981 ctxt->mode == X86EMUL_MODE_VM86)
1982 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001983
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001984 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001985
Avi Kivity9dac77f2011-06-01 15:34:25 +03001986 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02001987 usermode = X86EMUL_MODE_PROT64;
1988 else
1989 usermode = X86EMUL_MODE_PROT32;
1990
1991 cs.dpl = 3;
1992 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03001993 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001994 switch (usermode) {
1995 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001996 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001997 if ((msr_data & 0xfffc) == 0x0)
1998 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001999 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002000 break;
2001 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002002 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002003 if (msr_data == 0x0)
2004 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002005 ss_sel = cs_sel + 8;
2006 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002007 cs.l = 1;
2008 break;
2009 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002010 cs_sel |= SELECTOR_RPL_MASK;
2011 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002012
Avi Kivity1aa36612011-04-27 13:20:30 +03002013 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2014 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002015
Avi Kivity9dac77f2011-06-01 15:34:25 +03002016 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2017 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002018
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002019 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002020}
2021
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002022static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002023{
2024 int iopl;
2025 if (ctxt->mode == X86EMUL_MODE_REAL)
2026 return false;
2027 if (ctxt->mode == X86EMUL_MODE_VM86)
2028 return true;
2029 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002030 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002031}
2032
2033static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002034 u16 port, u16 len)
2035{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002036 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002037 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002038 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002039 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002040 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002041 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002042 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002043
Avi Kivity1aa36612011-04-27 13:20:30 +03002044 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002045 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002046 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002047 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002048 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002049 base = get_desc_base(&tr_seg);
2050#ifdef CONFIG_X86_64
2051 base |= ((u64)base3) << 32;
2052#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002053 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002054 if (r != X86EMUL_CONTINUE)
2055 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002056 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002057 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002058 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002059 if (r != X86EMUL_CONTINUE)
2060 return false;
2061 if ((perm >> bit_idx) & mask)
2062 return false;
2063 return true;
2064}
2065
2066static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002067 u16 port, u16 len)
2068{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002069 if (ctxt->perm_ok)
2070 return true;
2071
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002072 if (emulator_bad_iopl(ctxt))
2073 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002074 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002075
2076 ctxt->perm_ok = true;
2077
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002078 return true;
2079}
2080
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002081static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002082 struct tss_segment_16 *tss)
2083{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002084 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002085 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002086 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2087 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2088 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2089 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2090 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2091 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2092 tss->si = ctxt->regs[VCPU_REGS_RSI];
2093 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002094
Avi Kivity1aa36612011-04-27 13:20:30 +03002095 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2096 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2097 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2098 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2099 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002100}
2101
2102static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002103 struct tss_segment_16 *tss)
2104{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002105 int ret;
2106
Avi Kivity9dac77f2011-06-01 15:34:25 +03002107 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002108 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002109 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2110 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2111 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2112 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2113 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2114 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2115 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2116 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002117
2118 /*
2119 * SDM says that segment selectors are loaded before segment
2120 * descriptors
2121 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002122 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2123 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2124 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2125 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2126 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002127
2128 /*
2129 * Now load segment descriptors. If fault happenes at this stage
2130 * it is handled in a context of new task
2131 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002132 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002133 if (ret != X86EMUL_CONTINUE)
2134 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002135 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002136 if (ret != X86EMUL_CONTINUE)
2137 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002138 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002139 if (ret != X86EMUL_CONTINUE)
2140 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002141 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002142 if (ret != X86EMUL_CONTINUE)
2143 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002144 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002145 if (ret != X86EMUL_CONTINUE)
2146 return ret;
2147
2148 return X86EMUL_CONTINUE;
2149}
2150
2151static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 u16 tss_selector, u16 old_tss_sel,
2153 ulong old_tss_base, struct desc_struct *new_desc)
2154{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002155 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002156 struct tss_segment_16 tss_seg;
2157 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002158 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002159
Avi Kivity0f65dd72011-04-20 13:37:53 +03002160 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002161 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002162 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002164 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002166 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167
Avi Kivity0f65dd72011-04-20 13:37:53 +03002168 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002169 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002170 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002171 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002172 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002173
Avi Kivity0f65dd72011-04-20 13:37:53 +03002174 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002175 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002176 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002179
2180 if (old_tss_sel != 0xffff) {
2181 tss_seg.prev_task_link = old_tss_sel;
2182
Avi Kivity0f65dd72011-04-20 13:37:53 +03002183 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002184 &tss_seg.prev_task_link,
2185 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002186 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002187 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002188 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190 }
2191
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002192 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002193}
2194
2195static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196 struct tss_segment_32 *tss)
2197{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002198 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002199 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002200 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002201 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2202 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2203 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2204 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2205 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2206 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2207 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2208 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002209
Avi Kivity1aa36612011-04-27 13:20:30 +03002210 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2211 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2212 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2213 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2214 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2215 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2216 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002217}
2218
2219static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002220 struct tss_segment_32 *tss)
2221{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002222 int ret;
2223
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002224 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002225 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002226 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002227 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002228 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2229 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2230 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2231 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2232 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2233 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2234 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2235 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002236
2237 /*
2238 * SDM says that segment selectors are loaded before segment
2239 * descriptors
2240 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002241 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2242 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2243 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2244 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2245 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2246 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2247 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002248
2249 /*
2250 * Now load segment descriptors. If fault happenes at this stage
2251 * it is handled in a context of new task
2252 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002253 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002254 if (ret != X86EMUL_CONTINUE)
2255 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002256 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002257 if (ret != X86EMUL_CONTINUE)
2258 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002259 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002260 if (ret != X86EMUL_CONTINUE)
2261 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002262 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002263 if (ret != X86EMUL_CONTINUE)
2264 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002265 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002266 if (ret != X86EMUL_CONTINUE)
2267 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002268 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002269 if (ret != X86EMUL_CONTINUE)
2270 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002271 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002272 if (ret != X86EMUL_CONTINUE)
2273 return ret;
2274
2275 return X86EMUL_CONTINUE;
2276}
2277
2278static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002279 u16 tss_selector, u16 old_tss_sel,
2280 ulong old_tss_base, struct desc_struct *new_desc)
2281{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002282 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002283 struct tss_segment_32 tss_seg;
2284 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002285 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286
Avi Kivity0f65dd72011-04-20 13:37:53 +03002287 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002288 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002289 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002290 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002291 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002293 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294
Avi Kivity0f65dd72011-04-20 13:37:53 +03002295 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002296 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002297 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002299 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002300
Avi Kivity0f65dd72011-04-20 13:37:53 +03002301 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002302 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002303 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002304 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002305 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002306
2307 if (old_tss_sel != 0xffff) {
2308 tss_seg.prev_task_link = old_tss_sel;
2309
Avi Kivity0f65dd72011-04-20 13:37:53 +03002310 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311 &tss_seg.prev_task_link,
2312 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002313 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002314 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002315 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002316 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317 }
2318
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002319 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002320}
2321
2322static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002323 u16 tss_selector, int reason,
2324 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002325{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002326 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327 struct desc_struct curr_tss_desc, next_tss_desc;
2328 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002329 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002330 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002331 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002332 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333
2334 /* FIXME: old_tss_base == ~0 ? */
2335
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002336 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337 if (ret != X86EMUL_CONTINUE)
2338 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002339 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002340 if (ret != X86EMUL_CONTINUE)
2341 return ret;
2342
2343 /* FIXME: check that next_tss_desc is tss */
2344
2345 if (reason != TASK_SWITCH_IRET) {
2346 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002347 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002348 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002349 }
2350
Gleb Natapovceffb452010-03-18 15:20:19 +02002351 desc_limit = desc_limit_scaled(&next_tss_desc);
2352 if (!next_tss_desc.p ||
2353 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2354 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002355 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002356 return X86EMUL_PROPAGATE_FAULT;
2357 }
2358
2359 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2360 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002361 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002362 }
2363
2364 if (reason == TASK_SWITCH_IRET)
2365 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2366
2367 /* set back link to prev task only if NT bit is set in eflags
2368 note that old_tss_sel is not used afetr this point */
2369 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2370 old_tss_sel = 0xffff;
2371
2372 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002373 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002374 old_tss_base, &next_tss_desc);
2375 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002376 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002377 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002378 if (ret != X86EMUL_CONTINUE)
2379 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002380
2381 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2382 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2383
2384 if (reason != TASK_SWITCH_IRET) {
2385 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002386 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 }
2388
Avi Kivity717746e2011-04-20 13:37:53 +03002389 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002390 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002391
Jan Kiszkae269fb22010-04-14 15:51:09 +02002392 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002393 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2394 ctxt->lock_prefix = 0;
2395 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002396 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002397 }
2398
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399 return ret;
2400}
2401
2402int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002403 u16 tss_selector, int reason,
2404 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406 int rc;
2407
Avi Kivity9dac77f2011-06-01 15:34:25 +03002408 ctxt->_eip = ctxt->eip;
2409 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002411 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002412 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002413
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002414 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002415 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002417 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418}
2419
Avi Kivity90de84f2010-11-17 15:28:21 +02002420static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002421 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002422{
Gleb Natapova682e352010-03-18 15:20:21 +02002423 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2424
Avi Kivity9dac77f2011-06-01 15:34:25 +03002425 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2426 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002427 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002428}
2429
Avi Kivity7af04fc2010-08-18 14:16:35 +03002430static int em_das(struct x86_emulate_ctxt *ctxt)
2431{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002432 u8 al, old_al;
2433 bool af, cf, old_cf;
2434
2435 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002436 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002437
2438 old_al = al;
2439 old_cf = cf;
2440 cf = false;
2441 af = ctxt->eflags & X86_EFLAGS_AF;
2442 if ((al & 0x0f) > 9 || af) {
2443 al -= 6;
2444 cf = old_cf | (al >= 250);
2445 af = true;
2446 } else {
2447 af = false;
2448 }
2449 if (old_al > 0x99 || old_cf) {
2450 al -= 0x60;
2451 cf = true;
2452 }
2453
Avi Kivity9dac77f2011-06-01 15:34:25 +03002454 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002455 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002456 ctxt->src.type = OP_IMM;
2457 ctxt->src.val = 0;
2458 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002459 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002460 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2461 if (cf)
2462 ctxt->eflags |= X86_EFLAGS_CF;
2463 if (af)
2464 ctxt->eflags |= X86_EFLAGS_AF;
2465 return X86EMUL_CONTINUE;
2466}
2467
Avi Kivity0ef753b2010-08-18 14:51:45 +03002468static int em_call_far(struct x86_emulate_ctxt *ctxt)
2469{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002470 u16 sel, old_cs;
2471 ulong old_eip;
2472 int rc;
2473
Avi Kivity1aa36612011-04-27 13:20:30 +03002474 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002475 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002476
Avi Kivity9dac77f2011-06-01 15:34:25 +03002477 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002478 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002479 return X86EMUL_CONTINUE;
2480
Avi Kivity9dac77f2011-06-01 15:34:25 +03002481 ctxt->_eip = 0;
2482 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002483
Avi Kivity9dac77f2011-06-01 15:34:25 +03002484 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002485 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002486 if (rc != X86EMUL_CONTINUE)
2487 return rc;
2488
Avi Kivity9dac77f2011-06-01 15:34:25 +03002489 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002490 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002491}
2492
Avi Kivity40ece7c2010-08-18 15:12:09 +03002493static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2494{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002495 int rc;
2496
Avi Kivity9dac77f2011-06-01 15:34:25 +03002497 ctxt->dst.type = OP_REG;
2498 ctxt->dst.addr.reg = &ctxt->_eip;
2499 ctxt->dst.bytes = ctxt->op_bytes;
2500 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002501 if (rc != X86EMUL_CONTINUE)
2502 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002503 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002504 return X86EMUL_CONTINUE;
2505}
2506
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002507static int em_add(struct x86_emulate_ctxt *ctxt)
2508{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002509 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002510 return X86EMUL_CONTINUE;
2511}
2512
2513static int em_or(struct x86_emulate_ctxt *ctxt)
2514{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002515 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002516 return X86EMUL_CONTINUE;
2517}
2518
2519static int em_adc(struct x86_emulate_ctxt *ctxt)
2520{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002521 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002522 return X86EMUL_CONTINUE;
2523}
2524
2525static int em_sbb(struct x86_emulate_ctxt *ctxt)
2526{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002527 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002528 return X86EMUL_CONTINUE;
2529}
2530
2531static int em_and(struct x86_emulate_ctxt *ctxt)
2532{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002533 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002534 return X86EMUL_CONTINUE;
2535}
2536
2537static int em_sub(struct x86_emulate_ctxt *ctxt)
2538{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002539 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002540 return X86EMUL_CONTINUE;
2541}
2542
2543static int em_xor(struct x86_emulate_ctxt *ctxt)
2544{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002545 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002546 return X86EMUL_CONTINUE;
2547}
2548
2549static int em_cmp(struct x86_emulate_ctxt *ctxt)
2550{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002551 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002552 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002553 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002554 return X86EMUL_CONTINUE;
2555}
2556
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002557static int em_test(struct x86_emulate_ctxt *ctxt)
2558{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002559 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002560 /* Disable writeback. */
2561 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002562 return X86EMUL_CONTINUE;
2563}
2564
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002565static int em_xchg(struct x86_emulate_ctxt *ctxt)
2566{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002567 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002568 ctxt->src.val = ctxt->dst.val;
2569 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002570
2571 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002572 ctxt->dst.val = ctxt->src.orig_val;
2573 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002574 return X86EMUL_CONTINUE;
2575}
2576
Avi Kivity5c82aa22010-08-18 18:31:43 +03002577static int em_imul(struct x86_emulate_ctxt *ctxt)
2578{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002579 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002580 return X86EMUL_CONTINUE;
2581}
2582
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002583static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2584{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002585 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002586 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002587}
2588
Avi Kivity61429142010-08-19 15:13:00 +03002589static int em_cwd(struct x86_emulate_ctxt *ctxt)
2590{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002591 ctxt->dst.type = OP_REG;
2592 ctxt->dst.bytes = ctxt->src.bytes;
2593 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2594 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002595
2596 return X86EMUL_CONTINUE;
2597}
2598
Avi Kivity48bb5d32010-08-18 18:54:34 +03002599static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2600{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002601 u64 tsc = 0;
2602
Avi Kivity717746e2011-04-20 13:37:53 +03002603 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002604 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2605 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002606 return X86EMUL_CONTINUE;
2607}
2608
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002609static int em_mov(struct x86_emulate_ctxt *ctxt)
2610{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002611 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002612 return X86EMUL_CONTINUE;
2613}
2614
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002615static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2616{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002617 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002618 return emulate_ud(ctxt);
2619
Avi Kivity9dac77f2011-06-01 15:34:25 +03002620 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002621 return X86EMUL_CONTINUE;
2622}
2623
2624static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2625{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002626 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002627
Avi Kivity9dac77f2011-06-01 15:34:25 +03002628 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002629 return emulate_ud(ctxt);
2630
Avi Kivity9dac77f2011-06-01 15:34:25 +03002631 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002632 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2633
2634 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002635 ctxt->dst.type = OP_NONE;
2636 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002637}
2638
Avi Kivityaa97bb42010-01-20 18:09:23 +02002639static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2640{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002641 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002642 return X86EMUL_CONTINUE;
2643}
2644
Avi Kivity38503912011-03-31 18:48:09 +02002645static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2646{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002647 int rc;
2648 ulong linear;
2649
Avi Kivity9dac77f2011-06-01 15:34:25 +03002650 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002651 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002652 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002653 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002654 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002655 return X86EMUL_CONTINUE;
2656}
2657
Avi Kivity2d04a052011-04-20 15:32:49 +03002658static int em_clts(struct x86_emulate_ctxt *ctxt)
2659{
2660 ulong cr0;
2661
2662 cr0 = ctxt->ops->get_cr(ctxt, 0);
2663 cr0 &= ~X86_CR0_TS;
2664 ctxt->ops->set_cr(ctxt, 0, cr0);
2665 return X86EMUL_CONTINUE;
2666}
2667
Avi Kivity26d05cc2011-04-21 12:07:59 +03002668static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2669{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002670 int rc;
2671
Avi Kivity9dac77f2011-06-01 15:34:25 +03002672 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002673 return X86EMUL_UNHANDLEABLE;
2674
2675 rc = ctxt->ops->fix_hypercall(ctxt);
2676 if (rc != X86EMUL_CONTINUE)
2677 return rc;
2678
2679 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002680 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002681 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002682 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002683 return X86EMUL_CONTINUE;
2684}
2685
2686static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2687{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002688 struct desc_ptr desc_ptr;
2689 int rc;
2690
Avi Kivity9dac77f2011-06-01 15:34:25 +03002691 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002692 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002693 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002694 if (rc != X86EMUL_CONTINUE)
2695 return rc;
2696 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2697 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002698 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002699 return X86EMUL_CONTINUE;
2700}
2701
Avi Kivity5ef39c72011-04-21 12:21:50 +03002702static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002703{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002704 int rc;
2705
Avi Kivity5ef39c72011-04-21 12:21:50 +03002706 rc = ctxt->ops->fix_hypercall(ctxt);
2707
Avi Kivity26d05cc2011-04-21 12:07:59 +03002708 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002709 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002710 return rc;
2711}
2712
2713static int em_lidt(struct x86_emulate_ctxt *ctxt)
2714{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002715 struct desc_ptr desc_ptr;
2716 int rc;
2717
Avi Kivity9dac77f2011-06-01 15:34:25 +03002718 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002719 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002720 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002721 if (rc != X86EMUL_CONTINUE)
2722 return rc;
2723 ctxt->ops->set_idt(ctxt, &desc_ptr);
2724 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002725 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002726 return X86EMUL_CONTINUE;
2727}
2728
2729static int em_smsw(struct x86_emulate_ctxt *ctxt)
2730{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002731 ctxt->dst.bytes = 2;
2732 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002733 return X86EMUL_CONTINUE;
2734}
2735
2736static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2737{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002738 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002739 | (ctxt->src.val & 0x0f));
2740 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002741 return X86EMUL_CONTINUE;
2742}
2743
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002744static int em_loop(struct x86_emulate_ctxt *ctxt)
2745{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002746 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2747 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2748 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2749 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002750
2751 return X86EMUL_CONTINUE;
2752}
2753
2754static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2755{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002756 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2757 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002758
2759 return X86EMUL_CONTINUE;
2760}
2761
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002762static int em_cli(struct x86_emulate_ctxt *ctxt)
2763{
2764 if (emulator_bad_iopl(ctxt))
2765 return emulate_gp(ctxt, 0);
2766
2767 ctxt->eflags &= ~X86_EFLAGS_IF;
2768 return X86EMUL_CONTINUE;
2769}
2770
2771static int em_sti(struct x86_emulate_ctxt *ctxt)
2772{
2773 if (emulator_bad_iopl(ctxt))
2774 return emulate_gp(ctxt, 0);
2775
2776 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2777 ctxt->eflags |= X86_EFLAGS_IF;
2778 return X86EMUL_CONTINUE;
2779}
2780
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002781static bool valid_cr(int nr)
2782{
2783 switch (nr) {
2784 case 0:
2785 case 2 ... 4:
2786 case 8:
2787 return true;
2788 default:
2789 return false;
2790 }
2791}
2792
2793static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2794{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002795 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002796 return emulate_ud(ctxt);
2797
2798 return X86EMUL_CONTINUE;
2799}
2800
2801static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2802{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002803 u64 new_val = ctxt->src.val64;
2804 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002805 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002806
2807 static u64 cr_reserved_bits[] = {
2808 0xffffffff00000000ULL,
2809 0, 0, 0, /* CR3 checked later */
2810 CR4_RESERVED_BITS,
2811 0, 0, 0,
2812 CR8_RESERVED_BITS,
2813 };
2814
2815 if (!valid_cr(cr))
2816 return emulate_ud(ctxt);
2817
2818 if (new_val & cr_reserved_bits[cr])
2819 return emulate_gp(ctxt, 0);
2820
2821 switch (cr) {
2822 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002823 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002824 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2825 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2826 return emulate_gp(ctxt, 0);
2827
Avi Kivity717746e2011-04-20 13:37:53 +03002828 cr4 = ctxt->ops->get_cr(ctxt, 4);
2829 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002830
2831 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2832 !(cr4 & X86_CR4_PAE))
2833 return emulate_gp(ctxt, 0);
2834
2835 break;
2836 }
2837 case 3: {
2838 u64 rsvd = 0;
2839
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002840 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2841 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002842 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002843 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002844 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002845 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002846 rsvd = CR3_NONPAE_RESERVED_BITS;
2847
2848 if (new_val & rsvd)
2849 return emulate_gp(ctxt, 0);
2850
2851 break;
2852 }
2853 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002854 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002855
Avi Kivity717746e2011-04-20 13:37:53 +03002856 cr4 = ctxt->ops->get_cr(ctxt, 4);
2857 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002858
2859 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2860 return emulate_gp(ctxt, 0);
2861
2862 break;
2863 }
2864 }
2865
2866 return X86EMUL_CONTINUE;
2867}
2868
Joerg Roedel3b88e412011-04-04 12:39:29 +02002869static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2870{
2871 unsigned long dr7;
2872
Avi Kivity717746e2011-04-20 13:37:53 +03002873 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002874
2875 /* Check if DR7.Global_Enable is set */
2876 return dr7 & (1 << 13);
2877}
2878
2879static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2880{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002881 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002882 u64 cr4;
2883
2884 if (dr > 7)
2885 return emulate_ud(ctxt);
2886
Avi Kivity717746e2011-04-20 13:37:53 +03002887 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002888 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2889 return emulate_ud(ctxt);
2890
2891 if (check_dr7_gd(ctxt))
2892 return emulate_db(ctxt);
2893
2894 return X86EMUL_CONTINUE;
2895}
2896
2897static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2898{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002899 u64 new_val = ctxt->src.val64;
2900 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002901
2902 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2903 return emulate_gp(ctxt, 0);
2904
2905 return check_dr_read(ctxt);
2906}
2907
Joerg Roedel01de8b02011-04-04 12:39:31 +02002908static int check_svme(struct x86_emulate_ctxt *ctxt)
2909{
2910 u64 efer;
2911
Avi Kivity717746e2011-04-20 13:37:53 +03002912 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002913
2914 if (!(efer & EFER_SVME))
2915 return emulate_ud(ctxt);
2916
2917 return X86EMUL_CONTINUE;
2918}
2919
2920static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2921{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002922 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002923
2924 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002925 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002926 return emulate_gp(ctxt, 0);
2927
2928 return check_svme(ctxt);
2929}
2930
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002931static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2932{
Avi Kivity717746e2011-04-20 13:37:53 +03002933 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002934
Avi Kivity717746e2011-04-20 13:37:53 +03002935 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002936 return emulate_ud(ctxt);
2937
2938 return X86EMUL_CONTINUE;
2939}
2940
Joerg Roedel80612522011-04-04 12:39:33 +02002941static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2942{
Avi Kivity717746e2011-04-20 13:37:53 +03002943 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002944 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002945
Avi Kivity717746e2011-04-20 13:37:53 +03002946 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002947 (rcx > 3))
2948 return emulate_gp(ctxt, 0);
2949
2950 return X86EMUL_CONTINUE;
2951}
2952
Joerg Roedelf6511932011-04-04 12:39:35 +02002953static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2954{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002955 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2956 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002957 return emulate_gp(ctxt, 0);
2958
2959 return X86EMUL_CONTINUE;
2960}
2961
2962static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2963{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002964 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2965 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002966 return emulate_gp(ctxt, 0);
2967
2968 return X86EMUL_CONTINUE;
2969}
2970
Avi Kivity73fba5f2010-07-29 15:11:53 +03002971#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002972#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002973#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2974 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002975#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002976#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002977#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002978#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002979#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002980#define II(_f, _e, _i) \
2981 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002982#define IIP(_f, _e, _i, _p) \
2983 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2984 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002985#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002986
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002987#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002988#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002989#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2990
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002991#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2992 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2993 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002994
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002995static struct opcode group7_rm1[] = {
2996 DI(SrcNone | ModRM | Priv, monitor),
2997 DI(SrcNone | ModRM | Priv, mwait),
2998 N, N, N, N, N, N,
2999};
3000
Joerg Roedel01de8b02011-04-04 12:39:31 +02003001static struct opcode group7_rm3[] = {
3002 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003003 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003004 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3005 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3006 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3007 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3008 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3009 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3010};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003011
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003012static struct opcode group7_rm7[] = {
3013 N,
3014 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3015 N, N, N, N, N, N,
3016};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003017
Avi Kivity73fba5f2010-07-29 15:11:53 +03003018static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003019 I(Lock, em_add),
3020 I(Lock, em_or),
3021 I(Lock, em_adc),
3022 I(Lock, em_sbb),
3023 I(Lock, em_and),
3024 I(Lock, em_sub),
3025 I(Lock, em_xor),
3026 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003027};
3028
3029static struct opcode group1A[] = {
3030 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3031};
3032
3033static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003034 I(DstMem | SrcImm | ModRM, em_test),
3035 I(DstMem | SrcImm | ModRM, em_test),
3036 I(DstMem | SrcNone | ModRM | Lock, em_not),
3037 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3038 I(SrcMem | ModRM, em_mul_ex),
3039 I(SrcMem | ModRM, em_imul_ex),
3040 I(SrcMem | ModRM, em_div_ex),
3041 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003042};
3043
3044static struct opcode group4[] = {
3045 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3046 N, N, N, N, N, N,
3047};
3048
3049static struct opcode group5[] = {
3050 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003051 D(SrcMem | ModRM | Stack),
3052 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003053 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3054 D(SrcMem | ModRM | Stack), N,
3055};
3056
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003057static struct opcode group6[] = {
3058 DI(ModRM | Prot, sldt),
3059 DI(ModRM | Prot, str),
3060 DI(ModRM | Prot | Priv, lldt),
3061 DI(ModRM | Prot | Priv, ltr),
3062 N, N, N, N,
3063};
3064
Avi Kivity73fba5f2010-07-29 15:11:53 +03003065static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003066 DI(ModRM | Mov | DstMem | Priv, sgdt),
3067 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003068 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3069 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3070 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3071 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3072 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003073}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003074 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3075 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003076 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003077 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3078 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003079} };
3080
3081static struct opcode group8[] = {
3082 N, N, N, N,
3083 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3084 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3085};
3086
3087static struct group_dual group9 = { {
3088 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3089}, {
3090 N, N, N, N, N, N, N, N,
3091} };
3092
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003093static struct opcode group11[] = {
3094 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3095};
3096
Avi Kivityaa97bb42010-01-20 18:09:23 +02003097static struct gprefix pfx_0f_6f_0f_7f = {
3098 N, N, N, I(Sse, em_movdqu),
3099};
3100
Avi Kivity73fba5f2010-07-29 15:11:53 +03003101static struct opcode opcode_table[256] = {
3102 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003103 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003104 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3105 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003106 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003107 D(ImplicitOps | Stack | No64), N,
3108 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003109 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003110 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3111 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003112 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003113 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3114 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003115 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003116 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003117 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003118 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003119 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003120 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003121 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003122 /* 0x40 - 0x4F */
3123 X16(D(DstReg)),
3124 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003125 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003126 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003127 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003128 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003129 I(ImplicitOps | Stack | No64, em_pusha),
3130 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003131 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3132 N, N, N, N,
3133 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003134 I(SrcImm | Mov | Stack, em_push),
3135 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003136 I(SrcImmByte | Mov | Stack, em_push),
3137 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003138 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3139 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003140 /* 0x70 - 0x7F */
3141 X16(D(SrcImmByte)),
3142 /* 0x80 - 0x87 */
3143 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3144 G(DstMem | SrcImm | ModRM | Group, group1),
3145 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3146 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003147 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003148 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003149 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003150 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3151 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003152 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3153 D(ModRM | SrcMem | NoAccess | DstReg),
3154 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3155 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003156 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003157 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003158 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003159 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003160 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003161 II(ImplicitOps | Stack, em_pushf, pushf),
3162 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003163 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003164 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3165 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3166 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003167 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003168 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003169 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003170 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3171 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003172 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003173 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003174 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003175 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003176 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003177 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003178 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003179 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003180 I(ImplicitOps | Stack, em_ret),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003181 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003182 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003183 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003184 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003185 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003186 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003187 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003188 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003189 N, N, N, N,
3190 /* 0xD8 - 0xDF */
3191 N, N, N, N, N, N, N, N,
3192 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003193 X3(I(SrcImmByte, em_loop)),
3194 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003195 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3196 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003197 /* 0xE8 - 0xEF */
3198 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003199 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003200 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3201 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003202 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003203 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003204 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3205 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003206 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003207 D(ImplicitOps), D(ImplicitOps),
3208 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003209 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3210};
3211
3212static struct opcode twobyte_table[256] = {
3213 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003214 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003215 N, I(ImplicitOps | VendorSpecific, em_syscall),
3216 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003217 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003218 N, D(ImplicitOps | ModRM), N, N,
3219 /* 0x10 - 0x1F */
3220 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3221 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003222 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003223 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003224 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003225 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003226 N, N, N, N,
3227 N, N, N, N, N, N, N, N,
3228 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003229 DI(ImplicitOps | Priv, wrmsr),
3230 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3231 DI(ImplicitOps | Priv, rdmsr),
3232 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003233 I(ImplicitOps | VendorSpecific, em_sysenter),
3234 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003235 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003236 N, N, N, N, N, N, N, N,
3237 /* 0x40 - 0x4F */
3238 X16(D(DstReg | SrcMem | ModRM | Mov)),
3239 /* 0x50 - 0x5F */
3240 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3241 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003242 N, N, N, N,
3243 N, N, N, N,
3244 N, N, N, N,
3245 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003246 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003247 N, N, N, N,
3248 N, N, N, N,
3249 N, N, N, N,
3250 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003251 /* 0x80 - 0x8F */
3252 X16(D(SrcImm)),
3253 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003254 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003255 /* 0xA0 - 0xA7 */
3256 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003257 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003258 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3259 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3260 /* 0xA8 - 0xAF */
3261 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003262 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003263 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3264 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003265 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003266 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003267 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003268 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3269 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3270 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003271 /* 0xB8 - 0xBF */
3272 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003273 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003274 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3275 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003276 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003277 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003278 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003279 N, N, N, GD(0, &group9),
3280 N, N, N, N, N, N, N, N,
3281 /* 0xD0 - 0xDF */
3282 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3283 /* 0xE0 - 0xEF */
3284 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3285 /* 0xF0 - 0xFF */
3286 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3287};
3288
3289#undef D
3290#undef N
3291#undef G
3292#undef GD
3293#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003294#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003295#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003296
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003297#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003298#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003299#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003300#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003301
Avi Kivity9dac77f2011-06-01 15:34:25 +03003302static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003303{
3304 unsigned size;
3305
Avi Kivity9dac77f2011-06-01 15:34:25 +03003306 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003307 if (size == 8)
3308 size = 4;
3309 return size;
3310}
3311
3312static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3313 unsigned size, bool sign_extension)
3314{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003315 int rc = X86EMUL_CONTINUE;
3316
3317 op->type = OP_IMM;
3318 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003320 /* NB. Immediates are sign-extended as necessary. */
3321 switch (op->bytes) {
3322 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003323 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003324 break;
3325 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003326 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003327 break;
3328 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003329 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003330 break;
3331 }
3332 if (!sign_extension) {
3333 switch (op->bytes) {
3334 case 1:
3335 op->val &= 0xff;
3336 break;
3337 case 2:
3338 op->val &= 0xffff;
3339 break;
3340 case 4:
3341 op->val &= 0xffffffff;
3342 break;
3343 }
3344 }
3345done:
3346 return rc;
3347}
3348
Avi Kivitya9945542011-09-13 10:45:41 +03003349static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3350 unsigned d)
3351{
3352 int rc = X86EMUL_CONTINUE;
3353
3354 switch (d) {
3355 case OpReg:
3356 decode_register_operand(ctxt, op,
Avi Kivity52179732011-09-13 10:45:46 +03003357 op == &ctxt->dst &&
Avi Kivitya9945542011-09-13 10:45:41 +03003358 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
3359 break;
3360 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003361 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003362 break;
3363 case OpMem:
3364 case OpMem64:
Avi Kivity0fe59122011-09-13 10:45:47 +03003365 if (d == OpMem64)
3366 ctxt->memop.bytes = 8;
3367 else
3368 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3369 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003370 *op = ctxt->memop;
3371 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003372 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003373 fetch_bit_operand(ctxt);
3374 op->orig_val = op->val;
3375 break;
3376 case OpAcc:
3377 op->type = OP_REG;
3378 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3379 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3380 fetch_register_operand(op);
3381 op->orig_val = op->val;
3382 break;
3383 case OpDI:
3384 op->type = OP_MEM;
3385 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3386 op->addr.mem.ea =
3387 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3388 op->addr.mem.seg = VCPU_SREG_ES;
3389 op->val = 0;
3390 break;
3391 case OpDX:
3392 op->type = OP_REG;
3393 op->bytes = 2;
3394 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3395 fetch_register_operand(op);
3396 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003397 case OpCL:
3398 op->bytes = 1;
3399 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3400 break;
3401 case OpImmByte:
3402 rc = decode_imm(ctxt, op, 1, true);
3403 break;
3404 case OpOne:
3405 op->bytes = 1;
3406 op->val = 1;
3407 break;
3408 case OpImm:
3409 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3410 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03003411 case OpMem16:
3412 ctxt->memop.bytes = 2;
3413 goto mem_common;
3414 case OpMem32:
3415 ctxt->memop.bytes = 4;
3416 goto mem_common;
3417 case OpImmU16:
3418 rc = decode_imm(ctxt, op, 2, false);
3419 break;
3420 case OpImmU:
3421 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3422 break;
3423 case OpSI:
3424 op->type = OP_MEM;
3425 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3426 op->addr.mem.ea =
3427 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3428 op->addr.mem.seg = seg_override(ctxt);
3429 op->val = 0;
3430 break;
3431 case OpImmFAddr:
3432 op->type = OP_IMM;
3433 op->addr.mem.ea = ctxt->_eip;
3434 op->bytes = ctxt->op_bytes + 2;
3435 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3436 break;
3437 case OpMemFAddr:
3438 ctxt->memop.bytes = ctxt->op_bytes + 2;
3439 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003440 case OpImplicit:
3441 /* Special instructions do their own operand decoding. */
3442 default:
3443 op->type = OP_NONE; /* Disable writeback. */
3444 break;
3445 }
3446
3447done:
3448 return rc;
3449}
3450
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003451int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003452{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003453 int rc = X86EMUL_CONTINUE;
3454 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003455 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003456 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003457 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003458
Avi Kivityf09ed832011-09-13 10:45:40 +03003459 ctxt->memop.type = OP_NONE;
3460 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003461 ctxt->_eip = ctxt->eip;
3462 ctxt->fetch.start = ctxt->_eip;
3463 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003464 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003465 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003466
3467 switch (mode) {
3468 case X86EMUL_MODE_REAL:
3469 case X86EMUL_MODE_VM86:
3470 case X86EMUL_MODE_PROT16:
3471 def_op_bytes = def_ad_bytes = 2;
3472 break;
3473 case X86EMUL_MODE_PROT32:
3474 def_op_bytes = def_ad_bytes = 4;
3475 break;
3476#ifdef CONFIG_X86_64
3477 case X86EMUL_MODE_PROT64:
3478 def_op_bytes = 4;
3479 def_ad_bytes = 8;
3480 break;
3481#endif
3482 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003483 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003484 }
3485
Avi Kivity9dac77f2011-06-01 15:34:25 +03003486 ctxt->op_bytes = def_op_bytes;
3487 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003488
3489 /* Legacy prefixes. */
3490 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003491 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003492 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003493 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003494 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003495 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003496 break;
3497 case 0x67: /* address-size override */
3498 if (mode == X86EMUL_MODE_PROT64)
3499 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003500 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003501 else
3502 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003503 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003504 break;
3505 case 0x26: /* ES override */
3506 case 0x2e: /* CS override */
3507 case 0x36: /* SS override */
3508 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003509 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003510 break;
3511 case 0x64: /* FS override */
3512 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003513 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003514 break;
3515 case 0x40 ... 0x4f: /* REX */
3516 if (mode != X86EMUL_MODE_PROT64)
3517 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003518 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003519 continue;
3520 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003521 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003522 break;
3523 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003524 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003525 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003526 break;
3527 default:
3528 goto done_prefixes;
3529 }
3530
3531 /* Any legacy prefix after a REX prefix nullifies its effect. */
3532
Avi Kivity9dac77f2011-06-01 15:34:25 +03003533 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003534 }
3535
3536done_prefixes:
3537
3538 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003539 if (ctxt->rex_prefix & 8)
3540 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003541
3542 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003543 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003544 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003545 if (ctxt->b == 0x0f) {
3546 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003547 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003548 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003549 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003550 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003551
Avi Kivity9dac77f2011-06-01 15:34:25 +03003552 while (ctxt->d & GroupMask) {
3553 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003554 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003555 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003556 --ctxt->_eip;
3557 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003558 opcode = opcode.u.group[goffset];
3559 break;
3560 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003561 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003562 --ctxt->_eip;
3563 goffset = (ctxt->modrm >> 3) & 7;
3564 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003565 opcode = opcode.u.gdual->mod3[goffset];
3566 else
3567 opcode = opcode.u.gdual->mod012[goffset];
3568 break;
3569 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003570 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003571 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003572 break;
3573 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003574 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003575 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003576 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003577 switch (simd_prefix) {
3578 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3579 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3580 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3581 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3582 }
3583 break;
3584 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003585 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003586 }
Avi Kivity46561642011-04-24 14:09:59 +03003587
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003588 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003589 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003590 }
3591
Avi Kivity9dac77f2011-06-01 15:34:25 +03003592 ctxt->execute = opcode.u.execute;
3593 ctxt->check_perm = opcode.check_perm;
3594 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003595
3596 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003597 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003598 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003599
Avi Kivity9dac77f2011-06-01 15:34:25 +03003600 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003601 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003602
Avi Kivity9dac77f2011-06-01 15:34:25 +03003603 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3604 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003605
Avi Kivity9dac77f2011-06-01 15:34:25 +03003606 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003607 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003608 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003609 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003610 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003611 }
3612
Avi Kivity9dac77f2011-06-01 15:34:25 +03003613 if (ctxt->d & Sse)
3614 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003615
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003616 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003617 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003618 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003619 if (!ctxt->has_seg_override)
3620 set_seg_override(ctxt, ctxt->modrm_seg);
3621 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003622 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003623 if (rc != X86EMUL_CONTINUE)
3624 goto done;
3625
Avi Kivity9dac77f2011-06-01 15:34:25 +03003626 if (!ctxt->has_seg_override)
3627 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003628
Avi Kivityf09ed832011-09-13 10:45:40 +03003629 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003630
Avi Kivityf09ed832011-09-13 10:45:40 +03003631 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
3632 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003633
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003634 /*
3635 * Decode and fetch the source operand: register, memory
3636 * or immediate.
3637 */
Avi Kivity0fe59122011-09-13 10:45:47 +03003638 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003639 if (rc != X86EMUL_CONTINUE)
3640 goto done;
3641
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003642 /*
3643 * Decode and fetch the second source operand: register, memory
3644 * or immediate.
3645 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03003646 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003647 if (rc != X86EMUL_CONTINUE)
3648 goto done;
3649
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003650 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03003651 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003652
3653done:
Avi Kivityf09ed832011-09-13 10:45:40 +03003654 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
3655 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003656
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003657 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003658}
3659
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003660static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3661{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003662 /* The second termination condition only applies for REPE
3663 * and REPNE. Test if the repeat string operation prefix is
3664 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3665 * corresponding termination condition according to:
3666 * - if REPE/REPZ and ZF = 0 then done
3667 * - if REPNE/REPNZ and ZF = 1 then done
3668 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003669 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3670 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3671 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003672 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003673 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003674 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3675 return true;
3676
3677 return false;
3678}
3679
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003680int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003681{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003682 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003683 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003684 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003685 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003686
Avi Kivity9dac77f2011-06-01 15:34:25 +03003687 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003688
Avi Kivity9dac77f2011-06-01 15:34:25 +03003689 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003690 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003691 goto done;
3692 }
3693
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003694 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003695 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003696 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003697 goto done;
3698 }
3699
Avi Kivity9dac77f2011-06-01 15:34:25 +03003700 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003701 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003702 goto done;
3703 }
3704
Avi Kivity9dac77f2011-06-01 15:34:25 +03003705 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003706 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3707 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003708 rc = emulate_ud(ctxt);
3709 goto done;
3710 }
3711
Avi Kivity9dac77f2011-06-01 15:34:25 +03003712 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003713 rc = emulate_nm(ctxt);
3714 goto done;
3715 }
3716
Avi Kivity9dac77f2011-06-01 15:34:25 +03003717 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3718 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003719 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003720 if (rc != X86EMUL_CONTINUE)
3721 goto done;
3722 }
3723
Gleb Natapove92805a2010-02-10 14:21:35 +02003724 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003725 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003726 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003727 goto done;
3728 }
3729
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003730 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003731 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003732 rc = emulate_ud(ctxt);
3733 goto done;
3734 }
3735
Joerg Roedeld09beab2011-04-04 12:39:25 +02003736 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003737 if (ctxt->check_perm) {
3738 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003739 if (rc != X86EMUL_CONTINUE)
3740 goto done;
3741 }
3742
Avi Kivity9dac77f2011-06-01 15:34:25 +03003743 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3744 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003745 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003746 if (rc != X86EMUL_CONTINUE)
3747 goto done;
3748 }
3749
Avi Kivity9dac77f2011-06-01 15:34:25 +03003750 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003751 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003752 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3753 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003754 goto done;
3755 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003756 }
3757
Avi Kivity9dac77f2011-06-01 15:34:25 +03003758 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3759 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3760 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003761 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003762 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003763 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003764 }
3765
Avi Kivity9dac77f2011-06-01 15:34:25 +03003766 if (ctxt->src2.type == OP_MEM) {
3767 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3768 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003769 if (rc != X86EMUL_CONTINUE)
3770 goto done;
3771 }
3772
Avi Kivity9dac77f2011-06-01 15:34:25 +03003773 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003774 goto special_insn;
3775
3776
Avi Kivity9dac77f2011-06-01 15:34:25 +03003777 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003778 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003779 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3780 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003781 if (rc != X86EMUL_CONTINUE)
3782 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003783 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003784 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003785
Avi Kivity018a98d2007-11-27 19:30:56 +02003786special_insn:
3787
Avi Kivity9dac77f2011-06-01 15:34:25 +03003788 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3789 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003790 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003791 if (rc != X86EMUL_CONTINUE)
3792 goto done;
3793 }
3794
Avi Kivity9dac77f2011-06-01 15:34:25 +03003795 if (ctxt->execute) {
3796 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003797 if (rc != X86EMUL_CONTINUE)
3798 goto done;
3799 goto writeback;
3800 }
3801
Avi Kivity9dac77f2011-06-01 15:34:25 +03003802 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803 goto twobyte_insn;
3804
Avi Kivity9dac77f2011-06-01 15:34:25 +03003805 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003806 case 0x06: /* push es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003807 rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003808 break;
3809 case 0x07: /* pop es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003810 rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003811 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003812 case 0x0e: /* push cs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003813 rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003814 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003815 case 0x16: /* push ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003816 rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003817 break;
3818 case 0x17: /* pop ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003819 rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003820 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003821 case 0x1e: /* push ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003822 rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003823 break;
3824 case 0x1f: /* pop ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003825 rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003826 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003827 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003828 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02003829 break;
3830 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003831 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02003832 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003833 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003834 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003836 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003837 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003838 case 0x6c: /* insb */
3839 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003840 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003841 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003842 case 0x6e: /* outsb */
3843 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003844 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003845 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003846 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003847 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003848 if (test_cc(ctxt->b, ctxt->eflags))
3849 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003850 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003851 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003852 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003853 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003855 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003856 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003857 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003858 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003859 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003860 rc = em_xchg(ctxt);
3861 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003862 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003863 switch (ctxt->op_bytes) {
3864 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
3865 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
3866 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003867 }
3868 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003869 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003870 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003871 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003872 case 0xc4: /* les */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003873 rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003874 break;
3875 case 0xc5: /* lds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003876 rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003877 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003878 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003879 rc = emulate_int(ctxt, 3);
3880 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003881 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003882 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003883 break;
3884 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003885 if (ctxt->eflags & EFLG_OF)
3886 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003887 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003888 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003889 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003890 break;
3891 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003892 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003893 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003894 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003895 case 0xe4: /* inb */
3896 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003897 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003898 case 0xe6: /* outb */
3899 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003900 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003901 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003902 long int rel = ctxt->src.val;
3903 ctxt->src.val = (unsigned long) ctxt->_eip;
3904 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003905 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003906 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003907 }
3908 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003909 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003910 jmp_rel(ctxt, ctxt->src.val);
3911 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003912 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003913 case 0xec: /* in al,dx */
3914 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003915 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003916 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3917 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003918 goto done; /* IO is needed */
3919 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003920 case 0xee: /* out dx,al */
3921 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003922 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003923 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3924 &ctxt->src.val, 1);
3925 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003926 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003927 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003928 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003929 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003930 case 0xf5: /* cmc */
3931 /* complement carry flag from eflags reg */
3932 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003933 break;
3934 case 0xf8: /* clc */
3935 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003936 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003937 case 0xf9: /* stc */
3938 ctxt->eflags |= EFLG_CF;
3939 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003940 case 0xfc: /* cld */
3941 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003942 break;
3943 case 0xfd: /* std */
3944 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003945 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003946 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003947 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003948 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003949 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003950 rc = em_grp45(ctxt);
3951 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003952 default:
3953 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003955
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003956 if (rc != X86EMUL_CONTINUE)
3957 goto done;
3958
Avi Kivity018a98d2007-11-27 19:30:56 +02003959writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003960 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003961 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003962 goto done;
3963
Gleb Natapov5cd21912010-03-18 15:20:26 +02003964 /*
3965 * restore dst type in case the decoding will be reused
3966 * (happens for string instruction )
3967 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003968 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003969
Avi Kivity9dac77f2011-06-01 15:34:25 +03003970 if ((ctxt->d & SrcMask) == SrcSI)
3971 string_addr_inc(ctxt, seg_override(ctxt),
3972 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003973
Avi Kivity9dac77f2011-06-01 15:34:25 +03003974 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003975 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003976 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003977
Avi Kivity9dac77f2011-06-01 15:34:25 +03003978 if (ctxt->rep_prefix && (ctxt->d & String)) {
3979 struct read_cache *r = &ctxt->io_read;
3980 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003981
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003982 if (!string_insn_completed(ctxt)) {
3983 /*
3984 * Re-enter guest when pio read ahead buffer is empty
3985 * or, if it is not used, after each 1024 iteration.
3986 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003987 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003988 (r->end == 0 || r->end != r->pos)) {
3989 /*
3990 * Reset read cache. Usually happens before
3991 * decode, but since instruction is restarted
3992 * we have to do it here.
3993 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003994 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003995 return EMULATION_RESTART;
3996 }
3997 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003998 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003999 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004000
Avi Kivity9dac77f2011-06-01 15:34:25 +03004001 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004002
4003done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004004 if (rc == X86EMUL_PROPAGATE_FAULT)
4005 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004006 if (rc == X86EMUL_INTERCEPTED)
4007 return EMULATION_INTERCEPTED;
4008
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004009 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004010
4011twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004012 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004013 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004014 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004015 break;
4016 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004017 case 0x0d: /* GrpP (prefetch) */
4018 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004019 break;
4020 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004022 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004024 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004025 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004026 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004027 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004028 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004029 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004030 goto done;
4031 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004032 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004033 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004035 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004036 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004037 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004038 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004039 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004040 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004041 goto done;
4042 }
4043
Avi Kivity9dac77f2011-06-01 15:34:25 +03004044 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004045 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004046 case 0x30:
4047 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004048 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4049 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4050 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004051 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004052 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004053 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004054 }
4055 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004056 break;
4057 case 0x32:
4058 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004059 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004060 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004061 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004062 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004063 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004064 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4065 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004066 }
4067 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004068 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004069 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004070 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4071 if (!test_cc(ctxt->b, ctxt->eflags))
4072 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004074 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004075 if (test_cc(ctxt->b, ctxt->eflags))
4076 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004077 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004078 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004079 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004080 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004081 case 0xa0: /* push fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004082 rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004083 break;
4084 case 0xa1: /* pop fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004085 rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004086 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004087 case 0xa3:
4088 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004089 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004090 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004092 emulate_2op_SrcV_nobyte(ctxt, "bt");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004093 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004094 case 0xa4: /* shld imm8, r, r/m */
4095 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004096 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004097 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004098 case 0xa8: /* push gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004099 rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004100 break;
4101 case 0xa9: /* pop gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004102 rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004103 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004104 case 0xab:
4105 bts: /* bts */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004106 emulate_2op_SrcV_nobyte(ctxt, "bts");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004107 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004108 case 0xac: /* shrd imm8, r, r/m */
4109 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004110 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004111 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004112 case 0xae: /* clflush */
4113 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114 case 0xb0 ... 0xb1: /* cmpxchg */
4115 /*
4116 * Save real source value, then compare EAX against
4117 * destination.
4118 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004119 ctxt->src.orig_val = ctxt->src.val;
4120 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004121 emulate_2op_SrcV(ctxt, "cmp");
Laurent Vivier05f086f2007-09-24 11:10:55 +02004122 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004123 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004124 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125 } else {
4126 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004127 ctxt->dst.type = OP_REG;
4128 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129 }
4130 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004131 case 0xb2: /* lss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004132 rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004133 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 case 0xb3:
4135 btr: /* btr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004136 emulate_2op_SrcV_nobyte(ctxt, "btr");
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004138 case 0xb4: /* lfs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004139 rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004140 break;
4141 case 0xb5: /* lgs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004142 rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004143 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004145 ctxt->dst.bytes = ctxt->op_bytes;
4146 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4147 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004150 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151 case 0:
4152 goto bt;
4153 case 1:
4154 goto bts;
4155 case 2:
4156 goto btr;
4157 case 3:
4158 goto btc;
4159 }
4160 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004161 case 0xbb:
4162 btc: /* btc */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004163 emulate_2op_SrcV_nobyte(ctxt, "btc");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004164 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004165 case 0xbc: { /* bsf */
4166 u8 zf;
4167 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004168 : "=r"(ctxt->dst.val), "=q"(zf)
4169 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004170 ctxt->eflags &= ~X86_EFLAGS_ZF;
4171 if (zf) {
4172 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004173 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004174 }
4175 break;
4176 }
4177 case 0xbd: { /* bsr */
4178 u8 zf;
4179 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004180 : "=r"(ctxt->dst.val), "=q"(zf)
4181 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004182 ctxt->eflags &= ~X86_EFLAGS_ZF;
4183 if (zf) {
4184 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004185 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004186 }
4187 break;
4188 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004190 ctxt->dst.bytes = ctxt->op_bytes;
4191 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4192 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004194 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004195 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004196 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004197 ctxt->src.val = ctxt->dst.orig_val;
4198 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004199 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004200 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004201 ctxt->dst.bytes = ctxt->op_bytes;
4202 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4203 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004204 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004205 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004206 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004207 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004208 default:
4209 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004210 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004211
4212 if (rc != X86EMUL_CONTINUE)
4213 goto done;
4214
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215 goto writeback;
4216
4217cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004218 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219}