blob: 6eaedac7cf6aa9962298b9fc50c428718f941e28 [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 Kivitya99455492011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020060#define OpMem8 26ull /* 8-bit zero extended memory operand */
Avi Kivitya99455492011-09-13 10:45:41 +030061
Avi Kivity0fe59122011-09-13 10:45:47 +030062#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030063#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030064
65/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080066 * Opcode effective-address decode tables.
67 * Note that we only emulate instructions that have at least one memory
68 * operand (excluding implicit stack references). We assume that stack
69 * references and instruction fetches will never occur in special memory
70 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
71 * not be handled.
72 */
73
74/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030075#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030077#define DstShift 1
78#define ImplicitOps (OpImplicit << DstShift)
79#define DstReg (OpReg << DstShift)
80#define DstMem (OpMem << DstShift)
81#define DstAcc (OpAcc << DstShift)
82#define DstDI (OpDI << DstShift)
83#define DstMem64 (OpMem64 << DstShift)
84#define DstImmUByte (OpImmUByte << DstShift)
85#define DstDX (OpDX << DstShift)
86#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030088#define SrcShift 6
89#define SrcNone (OpNone << SrcShift)
90#define SrcReg (OpReg << SrcShift)
91#define SrcMem (OpMem << SrcShift)
92#define SrcMem16 (OpMem16 << SrcShift)
93#define SrcMem32 (OpMem32 << SrcShift)
94#define SrcImm (OpImm << SrcShift)
95#define SrcImmByte (OpImmByte << SrcShift)
96#define SrcOne (OpOne << SrcShift)
97#define SrcImmUByte (OpImmUByte << SrcShift)
98#define SrcImmU (OpImmU << SrcShift)
99#define SrcSI (OpSI << SrcShift)
100#define SrcImmFAddr (OpImmFAddr << SrcShift)
101#define SrcMemFAddr (OpMemFAddr << SrcShift)
102#define SrcAcc (OpAcc << SrcShift)
103#define SrcImmU16 (OpImmU16 << SrcShift)
104#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200105#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300106#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300107#define BitOp (1<<11)
108#define MemAbs (1<<12) /* Memory operand is absolute displacement */
109#define String (1<<13) /* String instruction (rep capable) */
110#define Stack (1<<14) /* Stack instruction (push/pop) */
111#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
112#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
113#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
114#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
115#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
116#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300117/* Generic ModRM decode. */
118#define ModRM (1<<19)
119/* Destination is only written; never read. */
120#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300121/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200122#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200123#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300124#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300125#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300126#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200127#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200128#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300129#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800130#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100131/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800132#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300133#define Src2None (OpNone << Src2Shift)
134#define Src2CL (OpCL << Src2Shift)
135#define Src2ImmByte (OpImmByte << Src2Shift)
136#define Src2One (OpOne << Src2Shift)
137#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300138#define Src2ES (OpES << Src2Shift)
139#define Src2CS (OpCS << Src2Shift)
140#define Src2SS (OpSS << Src2Shift)
141#define Src2DS (OpDS << Src2Shift)
142#define Src2FS (OpFS << Src2Shift)
143#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2Mask (OpMask << Src2Shift)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145
Avi Kivityd0e53322010-07-29 15:11:54 +0300146#define X2(x...) x, x
147#define X3(x...) X2(x), x
148#define X4(x...) X2(x), X2(x)
149#define X5(x...) X4(x), x
150#define X6(x...) X4(x), X2(x)
151#define X7(x...) X4(x), X3(x)
152#define X8(x...) X4(x), X4(x)
153#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300154
Avi Kivityd65b1de2010-07-29 15:11:35 +0300155struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300156 u64 flags : 56;
157 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300158 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300159 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300160 struct opcode *group;
161 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200162 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300163 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200164 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300165};
166
167struct group_dual {
168 struct opcode mod012[8];
169 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300170};
171
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200172struct gprefix {
173 struct opcode pfx_no;
174 struct opcode pfx_66;
175 struct opcode pfx_f2;
176 struct opcode pfx_f3;
177};
178
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200180#define EFLG_ID (1<<21)
181#define EFLG_VIP (1<<20)
182#define EFLG_VIF (1<<19)
183#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200184#define EFLG_VM (1<<17)
185#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200186#define EFLG_IOPL (3<<12)
187#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188#define EFLG_OF (1<<11)
189#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200190#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200191#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192#define EFLG_SF (1<<7)
193#define EFLG_ZF (1<<6)
194#define EFLG_AF (1<<4)
195#define EFLG_PF (1<<2)
196#define EFLG_CF (1<<0)
197
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300198#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
199#define EFLG_RESERVED_ONE_MASK 2
200
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201/*
202 * Instruction emulation:
203 * Most instructions are emulated directly via a fragment of inline assembly
204 * code. This allows us to save/restore EFLAGS and thus very easily pick up
205 * any modified flags.
206 */
207
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800208#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209#define _LO32 "k" /* force 32-bit operand */
210#define _STK "%%rsp" /* stack pointer */
211#elif defined(__i386__)
212#define _LO32 "" /* force 32-bit operand */
213#define _STK "%%esp" /* stack pointer */
214#endif
215
216/*
217 * These EFLAGS bits are restored from saved value during emulation, and
218 * any changes are written back to the saved value after emulation.
219 */
220#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
221
222/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200223#define _PRE_EFLAGS(_sav, _msk, _tmp) \
224 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
225 "movl %"_sav",%"_LO32 _tmp"; " \
226 "push %"_tmp"; " \
227 "push %"_tmp"; " \
228 "movl %"_msk",%"_LO32 _tmp"; " \
229 "andl %"_LO32 _tmp",("_STK"); " \
230 "pushf; " \
231 "notl %"_LO32 _tmp"; " \
232 "andl %"_LO32 _tmp",("_STK"); " \
233 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
234 "pop %"_tmp"; " \
235 "orl %"_LO32 _tmp",("_STK"); " \
236 "popf; " \
237 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238
239/* After executing instruction: write-back necessary bits in EFLAGS. */
240#define _POST_EFLAGS(_sav, _msk, _tmp) \
241 /* _sav |= EFLAGS & _msk; */ \
242 "pushf; " \
243 "pop %"_tmp"; " \
244 "andl %"_msk",%"_LO32 _tmp"; " \
245 "orl %"_LO32 _tmp",%"_sav"; "
246
Avi Kivitydda96d82008-11-26 15:14:10 +0200247#ifdef CONFIG_X86_64
248#define ON64(x) x
249#else
250#define ON64(x)
251#endif
252
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300253#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200254 do { \
255 __asm__ __volatile__ ( \
256 _PRE_EFLAGS("0", "4", "2") \
257 _op _suffix " %"_x"3,%1; " \
258 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300259 : "=m" ((ctxt)->eflags), \
260 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200261 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300262 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200263 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200264
265
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300267#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200268 do { \
269 unsigned long _tmp; \
270 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300271 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200272 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300273 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200274 break; \
275 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300276 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200277 break; \
278 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300279 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200280 break; \
281 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 } while (0)
283
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300284#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200286 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300287 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300289 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290 break; \
291 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300292 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 _wx, _wy, _lx, _ly, _qx, _qy); \
294 break; \
295 } \
296 } while (0)
297
298/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300299#define emulate_2op_SrcB(ctxt, _op) \
300 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301
302/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300303#define emulate_2op_SrcV(ctxt, _op) \
304 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305
306/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300307#define emulate_2op_SrcV_nobyte(ctxt, _op) \
308 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100310/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300311#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300312 do { \
313 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300314 _type _clv = (ctxt)->src2.val; \
315 _type _srcv = (ctxt)->src.val; \
316 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300317 \
318 __asm__ __volatile__ ( \
319 _PRE_EFLAGS("0", "5", "2") \
320 _op _suffix " %4,%1 \n" \
321 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300322 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300323 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
324 ); \
325 \
Avi Kivity761441b2011-09-07 16:41:36 +0300326 (ctxt)->src2.val = (unsigned long) _clv; \
327 (ctxt)->src2.val = (unsigned long) _srcv; \
328 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100329 } while (0)
330
Avi Kivity761441b2011-09-07 16:41:36 +0300331#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300332 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300333 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300334 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300335 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300336 break; \
337 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300338 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300339 break; \
340 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300341 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300342 break; \
343 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100344 } while (0)
345
Avi Kivityd1eef452011-09-07 16:41:38 +0300346#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 do { \
348 unsigned long _tmp; \
349 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200350 __asm__ __volatile__ ( \
351 _PRE_EFLAGS("0", "3", "2") \
352 _op _suffix " %1; " \
353 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300354 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200355 "=&r" (_tmp) \
356 : "i" (EFLAGS_MASK)); \
357 } while (0)
358
359/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300360#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200361 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300362 switch ((ctxt)->dst.bytes) { \
363 case 1: __emulate_1op(ctxt, _op, "b"); break; \
364 case 2: __emulate_1op(ctxt, _op, "w"); break; \
365 case 4: __emulate_1op(ctxt, _op, "l"); break; \
366 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 } \
368 } while (0)
369
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300370#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300371 do { \
372 unsigned long _tmp; \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300373 ulong *rax = &(ctxt)->regs[VCPU_REGS_RAX]; \
374 ulong *rdx = &(ctxt)->regs[VCPU_REGS_RDX]; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300375 \
376 __asm__ __volatile__ ( \
377 _PRE_EFLAGS("0", "5", "1") \
378 "1: \n\t" \
379 _op _suffix " %6; " \
380 "2: \n\t" \
381 _POST_EFLAGS("0", "5", "1") \
382 ".pushsection .fixup,\"ax\" \n\t" \
383 "3: movb $1, %4 \n\t" \
384 "jmp 2b \n\t" \
385 ".popsection \n\t" \
386 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300387 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
388 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
389 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
390 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300391 } while (0)
392
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300393/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300394#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300395 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300396 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300397 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300398 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300399 break; \
400 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300401 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300402 break; \
403 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300404 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300405 break; \
406 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300407 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300408 break; \
409 } \
410 } while (0)
411
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200412static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
413 enum x86_intercept intercept,
414 enum x86_intercept_stage stage)
415{
416 struct x86_instruction_info info = {
417 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300418 .rep_prefix = ctxt->rep_prefix,
419 .modrm_mod = ctxt->modrm_mod,
420 .modrm_reg = ctxt->modrm_reg,
421 .modrm_rm = ctxt->modrm_rm,
422 .src_val = ctxt->src.val64,
423 .src_bytes = ctxt->src.bytes,
424 .dst_bytes = ctxt->dst.bytes,
425 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200426 .next_rip = ctxt->eip,
427 };
428
Avi Kivity29535382011-04-20 13:37:53 +0300429 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200430}
431
Avi Kivity9dac77f2011-06-01 15:34:25 +0300432static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800433{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300434 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800435}
436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800438static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300439address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800440{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300441 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800442 return reg;
443 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300444 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800445}
446
447static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800449{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300450 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800451}
452
Harvey Harrison7a9572752008-02-19 07:40:41 -0800453static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300454register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800455{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300456 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800457 *reg += inc;
458 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300459 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800460}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461
Avi Kivity9dac77f2011-06-01 15:34:25 +0300462static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800463{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300464 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800465}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300466
Avi Kivity56697682011-04-03 14:08:51 +0300467static u32 desc_limit_scaled(struct desc_struct *desc)
468{
469 u32 limit = get_desc_limit(desc);
470
471 return desc->g ? (limit << 12) | 0xfff : limit;
472}
473
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300475{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476 ctxt->has_seg_override = true;
477 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300478}
479
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900480static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300481{
482 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
483 return 0;
484
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900485 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300486}
487
Avi Kivity9dac77f2011-06-01 15:34:25 +0300488static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300489{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300490 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300491 return 0;
492
Avi Kivity9dac77f2011-06-01 15:34:25 +0300493 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300494}
495
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200496static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
497 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300498{
Avi Kivityda9cb572010-11-22 17:53:21 +0200499 ctxt->exception.vector = vec;
500 ctxt->exception.error_code = error;
501 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200502 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300503}
504
Joerg Roedel3b88e412011-04-04 12:39:29 +0200505static int emulate_db(struct x86_emulate_ctxt *ctxt)
506{
507 return emulate_exception(ctxt, DB_VECTOR, 0, false);
508}
509
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200510static int emulate_gp(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, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300513}
514
Avi Kivity618ff152011-04-03 12:32:09 +0300515static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
516{
517 return emulate_exception(ctxt, SS_VECTOR, err, true);
518}
519
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200520static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300521{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200522 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300523}
524
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300526{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200527 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300528}
529
Avi Kivity34d1f492010-08-26 11:59:01 +0300530static int emulate_de(struct x86_emulate_ctxt *ctxt)
531{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200532 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300533}
534
Avi Kivity12537912011-03-29 11:41:27 +0200535static int emulate_nm(struct x86_emulate_ctxt *ctxt)
536{
537 return emulate_exception(ctxt, NM_VECTOR, 0, false);
538}
539
Avi Kivity1aa36612011-04-27 13:20:30 +0300540static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
541{
542 u16 selector;
543 struct desc_struct desc;
544
545 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
546 return selector;
547}
548
549static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
550 unsigned seg)
551{
552 u16 dummy;
553 u32 base3;
554 struct desc_struct desc;
555
556 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
557 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
558}
559
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400560static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300561 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400562 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300563 ulong *linear)
564{
Avi Kivity618ff152011-04-03 12:32:09 +0300565 struct desc_struct desc;
566 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300567 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300568 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300569 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300570 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300571
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900572 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300573 switch (ctxt->mode) {
574 case X86EMUL_MODE_REAL:
575 break;
576 case X86EMUL_MODE_PROT64:
577 if (((signed long)la << 16) >> 16 != la)
578 return emulate_gp(ctxt, 0);
579 break;
580 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300581 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
582 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300583 if (!usable)
584 goto bad;
585 /* code segment or read-only data segment */
586 if (((desc.type & 8) || !(desc.type & 2)) && write)
587 goto bad;
588 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400589 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300590 goto bad;
591 lim = desc_limit_scaled(&desc);
592 if ((desc.type & 8) || !(desc.type & 4)) {
593 /* expand-up segment */
594 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
595 goto bad;
596 } else {
597 /* exapand-down segment */
598 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
599 goto bad;
600 lim = desc.d ? 0xffffffff : 0xffff;
601 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
602 goto bad;
603 }
Avi Kivity717746e2011-04-20 13:37:53 +0300604 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300605 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300606 cpl = max(cpl, rpl);
607 if (!(desc.type & 8)) {
608 /* data segment */
609 if (cpl > desc.dpl)
610 goto bad;
611 } else if ((desc.type & 8) && !(desc.type & 4)) {
612 /* nonconforming code segment */
613 if (cpl != desc.dpl)
614 goto bad;
615 } else if ((desc.type & 8) && (desc.type & 4)) {
616 /* conforming code segment */
617 if (cpl < desc.dpl)
618 goto bad;
619 }
620 break;
621 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300622 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300623 la &= (u32)-1;
624 *linear = la;
625 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300626bad:
627 if (addr.seg == VCPU_SREG_SS)
628 return emulate_ss(ctxt, addr.seg);
629 else
630 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300631}
632
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400633static int linearize(struct x86_emulate_ctxt *ctxt,
634 struct segmented_address addr,
635 unsigned size, bool write,
636 ulong *linear)
637{
638 return __linearize(ctxt, addr, size, write, false, linear);
639}
640
641
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200642static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
643 struct segmented_address addr,
644 void *data,
645 unsigned size)
646{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200647 int rc;
648 ulong linear;
649
Avi Kivity83b87952011-04-03 11:31:19 +0300650 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200651 if (rc != X86EMUL_CONTINUE)
652 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300653 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200654}
655
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900656/*
657 * Fetch the next byte of the instruction being emulated which is pointed to
658 * by ctxt->_eip, then increment ctxt->_eip.
659 *
660 * Also prefetch the remaining bytes of the instruction without crossing page
661 * boundary if they are not in fetch_cache yet.
662 */
663static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200664{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300665 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200666 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300667 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200668
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900669 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400670 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900671 struct segmented_address addr = { .seg = VCPU_SREG_CS,
672 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300673 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900674 size = min(15UL - cur_size,
675 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400676 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900677 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400678 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900679 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
680 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900681 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200682 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300683 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200684 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900685 *dest = fc->data[ctxt->_eip - fc->start];
686 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900687 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200688}
689
690static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900691 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200692{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900693 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200694
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200695 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900696 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200697 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200698 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900699 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900700 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200701 return rc;
702 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900703 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200704}
705
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900706/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900707#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900708({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900709 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900710 if (rc != X86EMUL_CONTINUE) \
711 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900712 (_type)_x; \
713})
714
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900715#define insn_fetch_arr(_arr, _size, _ctxt) \
716({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900717 if (rc != X86EMUL_CONTINUE) \
718 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900719})
720
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000721/*
722 * Given the 'reg' portion of a ModRM byte, and a register block, return a
723 * pointer into the block that addresses the relevant register.
724 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
725 */
726static void *decode_register(u8 modrm_reg, unsigned long *regs,
727 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728{
729 void *p;
730
731 p = &regs[modrm_reg];
732 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
733 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
734 return p;
735}
736
737static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200738 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 u16 *size, unsigned long *address, int op_bytes)
740{
741 int rc;
742
743 if (op_bytes == 2)
744 op_bytes = 3;
745 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200746 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900747 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200749 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200750 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 return rc;
752}
753
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300754static int test_cc(unsigned int condition, unsigned int flags)
755{
756 int rc = 0;
757
758 switch ((condition & 15) >> 1) {
759 case 0: /* o */
760 rc |= (flags & EFLG_OF);
761 break;
762 case 1: /* b/c/nae */
763 rc |= (flags & EFLG_CF);
764 break;
765 case 2: /* z/e */
766 rc |= (flags & EFLG_ZF);
767 break;
768 case 3: /* be/na */
769 rc |= (flags & (EFLG_CF|EFLG_ZF));
770 break;
771 case 4: /* s */
772 rc |= (flags & EFLG_SF);
773 break;
774 case 5: /* p/pe */
775 rc |= (flags & EFLG_PF);
776 break;
777 case 7: /* le/ng */
778 rc |= (flags & EFLG_ZF);
779 /* fall through */
780 case 6: /* l/nge */
781 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
782 break;
783 }
784
785 /* Odd condition identifiers (lsb == 1) have inverted sense. */
786 return (!!rc ^ (condition & 1));
787}
788
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300789static void fetch_register_operand(struct operand *op)
790{
791 switch (op->bytes) {
792 case 1:
793 op->val = *(u8 *)op->addr.reg;
794 break;
795 case 2:
796 op->val = *(u16 *)op->addr.reg;
797 break;
798 case 4:
799 op->val = *(u32 *)op->addr.reg;
800 break;
801 case 8:
802 op->val = *(u64 *)op->addr.reg;
803 break;
804 }
805}
806
Avi Kivity12537912011-03-29 11:41:27 +0200807static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
808{
809 ctxt->ops->get_fpu(ctxt);
810 switch (reg) {
811 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
812 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
813 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
814 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
815 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
816 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
817 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
818 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
819#ifdef CONFIG_X86_64
820 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
821 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
822 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
823 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
824 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
825 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
826 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
827 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
828#endif
829 default: BUG();
830 }
831 ctxt->ops->put_fpu(ctxt);
832}
833
834static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
835 int reg)
836{
837 ctxt->ops->get_fpu(ctxt);
838 switch (reg) {
839 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
840 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
841 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
842 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
843 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
844 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
845 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
846 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
847#ifdef CONFIG_X86_64
848 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
849 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
850 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
851 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
852 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
853 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
854 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
855 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
856#endif
857 default: BUG();
858 }
859 ctxt->ops->put_fpu(ctxt);
860}
861
862static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200863 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200864{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300865 unsigned reg = ctxt->modrm_reg;
866 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200867
Avi Kivity9dac77f2011-06-01 15:34:25 +0300868 if (!(ctxt->d & ModRM))
869 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200870
Avi Kivity9dac77f2011-06-01 15:34:25 +0300871 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200872 op->type = OP_XMM;
873 op->bytes = 16;
874 op->addr.xmm = reg;
875 read_sse_reg(ctxt, &op->vec_val, reg);
876 return;
877 }
878
Avi Kivity3c118e22007-10-31 10:27:04 +0200879 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200880 if (ctxt->d & ByteOp) {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300881 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200882 op->bytes = 1;
883 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300884 op->addr.reg = decode_register(reg, ctxt->regs, 0);
885 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200886 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300887 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200888 op->orig_val = op->val;
889}
890
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200891static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300892 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200893{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700895 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900896 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300897 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200898
Avi Kivity9dac77f2011-06-01 15:34:25 +0300899 if (ctxt->rex_prefix) {
900 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
901 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
902 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200903 }
904
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900905 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300906 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
907 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
908 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
909 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200910
Avi Kivity9dac77f2011-06-01 15:34:25 +0300911 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300912 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300913 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
914 op->addr.reg = decode_register(ctxt->modrm_rm,
915 ctxt->regs, ctxt->d & ByteOp);
916 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200917 op->type = OP_XMM;
918 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300919 op->addr.xmm = ctxt->modrm_rm;
920 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200921 return rc;
922 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300923 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200924 return rc;
925 }
926
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300927 op->type = OP_MEM;
928
Avi Kivity9dac77f2011-06-01 15:34:25 +0300929 if (ctxt->ad_bytes == 2) {
930 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
931 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
932 unsigned si = ctxt->regs[VCPU_REGS_RSI];
933 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200934
935 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300936 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200937 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300938 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900939 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200940 break;
941 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900942 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200943 break;
944 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900945 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200946 break;
947 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300948 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200949 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300950 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200951 break;
952 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300953 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200954 break;
955 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300956 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200957 break;
958 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300959 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200960 break;
961 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300962 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200963 break;
964 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300965 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200966 break;
967 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300968 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300969 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200970 break;
971 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300972 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200973 break;
974 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300975 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
976 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
977 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300978 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200979 } else {
980 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300981 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900982 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200983 index_reg |= (sib >> 3) & 7;
984 base_reg |= sib & 7;
985 scale = sib >> 6;
986
Avi Kivity9dac77f2011-06-01 15:34:25 +0300987 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900988 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700989 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300990 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700991 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300992 modrm_ea += ctxt->regs[index_reg] << scale;
993 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700994 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300995 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700996 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300997 modrm_ea += ctxt->regs[ctxt->modrm_rm];
998 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200999 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001000 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001001 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001002 break;
1003 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001004 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001005 break;
1006 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001007 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001008 break;
1009 }
1010 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001011 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001012done:
1013 return rc;
1014}
1015
1016static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001017 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001018{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001019 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001020
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001021 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001022 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001023 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001024 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001025 break;
1026 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001027 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001028 break;
1029 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001030 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001031 break;
1032 }
1033done:
1034 return rc;
1035}
1036
Avi Kivity9dac77f2011-06-01 15:34:25 +03001037static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001038{
Sheng Yang7129eec2010-09-28 16:33:32 +08001039 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001040
Avi Kivity9dac77f2011-06-01 15:34:25 +03001041 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1042 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001043
Avi Kivity9dac77f2011-06-01 15:34:25 +03001044 if (ctxt->src.bytes == 2)
1045 sv = (s16)ctxt->src.val & (s16)mask;
1046 else if (ctxt->src.bytes == 4)
1047 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001048
Avi Kivity9dac77f2011-06-01 15:34:25 +03001049 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001050 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001051
1052 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001053 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001054}
1055
Gleb Natapov9de41572010-04-28 19:15:22 +03001056static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001057 unsigned long addr, void *dest, unsigned size)
1058{
1059 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001060 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001061
1062 while (size) {
1063 int n = min(size, 8u);
1064 size -= n;
1065 if (mc->pos < mc->end)
1066 goto read_cached;
1067
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001068 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1069 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001070 if (rc != X86EMUL_CONTINUE)
1071 return rc;
1072 mc->end += n;
1073
1074 read_cached:
1075 memcpy(dest, mc->data + mc->pos, n);
1076 mc->pos += n;
1077 dest += n;
1078 addr += n;
1079 }
1080 return X86EMUL_CONTINUE;
1081}
1082
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001083static int segmented_read(struct x86_emulate_ctxt *ctxt,
1084 struct segmented_address addr,
1085 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, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001092 if (rc != X86EMUL_CONTINUE)
1093 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001094 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001095}
1096
1097static int segmented_write(struct x86_emulate_ctxt *ctxt,
1098 struct segmented_address addr,
1099 const void *data,
1100 unsigned size)
1101{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001102 int rc;
1103 ulong linear;
1104
Avi Kivity83b87952011-04-03 11:31:19 +03001105 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001106 if (rc != X86EMUL_CONTINUE)
1107 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001108 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1109 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001110}
1111
1112static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1113 struct segmented_address addr,
1114 const void *orig_data, const void *data,
1115 unsigned size)
1116{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001117 int rc;
1118 ulong linear;
1119
Avi Kivity83b87952011-04-03 11:31:19 +03001120 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001121 if (rc != X86EMUL_CONTINUE)
1122 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001123 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1124 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001125}
1126
Gleb Natapov7b262e92010-03-18 15:20:27 +02001127static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001128 unsigned int size, unsigned short port,
1129 void *dest)
1130{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001132
1133 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001134 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 unsigned int count = ctxt->rep_prefix ?
1136 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001137 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001138 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1139 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001140 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1141 count);
1142 if (n == 0)
1143 n = 1;
1144 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001145 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001146 return 0;
1147 rc->end = n * size;
1148 }
1149
1150 memcpy(dest, rc->data + rc->pos, size);
1151 rc->pos += size;
1152 return 1;
1153}
1154
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001155static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001156 u16 selector, struct desc_ptr *dt)
1157{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001158 struct x86_emulate_ops *ops = ctxt->ops;
1159
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001160 if (selector & 1 << 2) {
1161 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001162 u16 sel;
1163
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001164 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001165 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001166 return;
1167
1168 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1169 dt->address = get_desc_base(&desc);
1170 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001171 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001172}
1173
1174/* allowed just for 8 bytes segments */
1175static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001176 u16 selector, struct desc_struct *desc)
1177{
1178 struct desc_ptr dt;
1179 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001180 ulong addr;
1181
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001182 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001183
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001184 if (dt.size < index * 8 + 7)
1185 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001186
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001187 addr = dt.address + index * 8;
1188 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1189 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001190}
1191
1192/* allowed just for 8 bytes segments */
1193static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001194 u16 selector, struct desc_struct *desc)
1195{
1196 struct desc_ptr dt;
1197 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001199
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001200 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001201
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001202 if (dt.size < index * 8 + 7)
1203 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001204
1205 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001206 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1207 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001208}
1209
Gleb Natapov5601d052011-03-07 14:55:06 +02001210/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001211static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001212 u16 selector, int seg)
1213{
1214 struct desc_struct seg_desc;
1215 u8 dpl, rpl, cpl;
1216 unsigned err_vec = GP_VECTOR;
1217 u32 err_code = 0;
1218 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1219 int ret;
1220
1221 memset(&seg_desc, 0, sizeof seg_desc);
1222
1223 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1224 || ctxt->mode == X86EMUL_MODE_REAL) {
1225 /* set real mode segment descriptor */
1226 set_desc_base(&seg_desc, selector << 4);
1227 set_desc_limit(&seg_desc, 0xffff);
1228 seg_desc.type = 3;
1229 seg_desc.p = 1;
1230 seg_desc.s = 1;
1231 goto load;
1232 }
1233
1234 /* NULL selector is not valid for TR, CS and SS */
1235 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1236 && null_selector)
1237 goto exception;
1238
1239 /* TR should be in GDT only */
1240 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1241 goto exception;
1242
1243 if (null_selector) /* for NULL selector skip all following checks */
1244 goto load;
1245
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001246 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001247 if (ret != X86EMUL_CONTINUE)
1248 return ret;
1249
1250 err_code = selector & 0xfffc;
1251 err_vec = GP_VECTOR;
1252
1253 /* can't load system descriptor into segment selecor */
1254 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1255 goto exception;
1256
1257 if (!seg_desc.p) {
1258 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1259 goto exception;
1260 }
1261
1262 rpl = selector & 3;
1263 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001264 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001265
1266 switch (seg) {
1267 case VCPU_SREG_SS:
1268 /*
1269 * segment is not a writable data segment or segment
1270 * selector's RPL != CPL or segment selector's RPL != CPL
1271 */
1272 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1273 goto exception;
1274 break;
1275 case VCPU_SREG_CS:
1276 if (!(seg_desc.type & 8))
1277 goto exception;
1278
1279 if (seg_desc.type & 4) {
1280 /* conforming */
1281 if (dpl > cpl)
1282 goto exception;
1283 } else {
1284 /* nonconforming */
1285 if (rpl > cpl || dpl != cpl)
1286 goto exception;
1287 }
1288 /* CS(RPL) <- CPL */
1289 selector = (selector & 0xfffc) | cpl;
1290 break;
1291 case VCPU_SREG_TR:
1292 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1293 goto exception;
1294 break;
1295 case VCPU_SREG_LDTR:
1296 if (seg_desc.s || seg_desc.type != 2)
1297 goto exception;
1298 break;
1299 default: /* DS, ES, FS, or GS */
1300 /*
1301 * segment is not a data or readable code segment or
1302 * ((segment is a data or nonconforming code segment)
1303 * and (both RPL and CPL > DPL))
1304 */
1305 if ((seg_desc.type & 0xa) == 0x8 ||
1306 (((seg_desc.type & 0xc) != 0xc) &&
1307 (rpl > dpl && cpl > dpl)))
1308 goto exception;
1309 break;
1310 }
1311
1312 if (seg_desc.s) {
1313 /* mark segment as accessed */
1314 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001315 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001316 if (ret != X86EMUL_CONTINUE)
1317 return ret;
1318 }
1319load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001320 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001321 return X86EMUL_CONTINUE;
1322exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001323 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001324 return X86EMUL_PROPAGATE_FAULT;
1325}
1326
Wei Yongjun31be40b2010-08-17 09:17:30 +08001327static void write_register_operand(struct operand *op)
1328{
1329 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1330 switch (op->bytes) {
1331 case 1:
1332 *(u8 *)op->addr.reg = (u8)op->val;
1333 break;
1334 case 2:
1335 *(u16 *)op->addr.reg = (u16)op->val;
1336 break;
1337 case 4:
1338 *op->addr.reg = (u32)op->val;
1339 break; /* 64b: zero-extend */
1340 case 8:
1341 *op->addr.reg = op->val;
1342 break;
1343 }
1344}
1345
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001346static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001347{
1348 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001349
Avi Kivity9dac77f2011-06-01 15:34:25 +03001350 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001351 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001352 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001353 break;
1354 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001355 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001356 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001357 ctxt->dst.addr.mem,
1358 &ctxt->dst.orig_val,
1359 &ctxt->dst.val,
1360 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001361 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001362 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001363 ctxt->dst.addr.mem,
1364 &ctxt->dst.val,
1365 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001366 if (rc != X86EMUL_CONTINUE)
1367 return rc;
1368 break;
Avi Kivity12537912011-03-29 11:41:27 +02001369 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001370 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001371 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001372 case OP_NONE:
1373 /* no writeback */
1374 break;
1375 default:
1376 break;
1377 }
1378 return X86EMUL_CONTINUE;
1379}
1380
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001381static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001383 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001384
Avi Kivity9dac77f2011-06-01 15:34:25 +03001385 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1386 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001387 addr.seg = VCPU_SREG_SS;
1388
1389 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001390 ctxt->dst.type = OP_NONE;
1391 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392}
1393
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001394static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001395 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001397 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001398 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001399
Avi Kivity9dac77f2011-06-01 15:34:25 +03001400 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001401 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001402 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001403 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001404 return rc;
1405
Avi Kivity9dac77f2011-06-01 15:34:25 +03001406 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001407 return rc;
1408}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001409
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001410static int em_pop(struct x86_emulate_ctxt *ctxt)
1411{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001412 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001413}
1414
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001415static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001416 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001417{
1418 int rc;
1419 unsigned long val, change_mask;
1420 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001421 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001422
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001423 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001424 if (rc != X86EMUL_CONTINUE)
1425 return rc;
1426
1427 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1428 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1429
1430 switch(ctxt->mode) {
1431 case X86EMUL_MODE_PROT64:
1432 case X86EMUL_MODE_PROT32:
1433 case X86EMUL_MODE_PROT16:
1434 if (cpl == 0)
1435 change_mask |= EFLG_IOPL;
1436 if (cpl <= iopl)
1437 change_mask |= EFLG_IF;
1438 break;
1439 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001440 if (iopl < 3)
1441 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001442 change_mask |= EFLG_IF;
1443 break;
1444 default: /* real mode */
1445 change_mask |= (EFLG_IOPL | EFLG_IF);
1446 break;
1447 }
1448
1449 *(unsigned long *)dest =
1450 (ctxt->eflags & ~change_mask) | (val & change_mask);
1451
1452 return rc;
1453}
1454
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001455static int em_popf(struct x86_emulate_ctxt *ctxt)
1456{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001457 ctxt->dst.type = OP_REG;
1458 ctxt->dst.addr.reg = &ctxt->eflags;
1459 ctxt->dst.bytes = ctxt->op_bytes;
1460 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001461}
1462
Avi Kivity1cd196e2011-09-13 10:45:51 +03001463static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001464{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001465 int seg = ctxt->src2.val;
1466
Avi Kivity9dac77f2011-06-01 15:34:25 +03001467 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001468
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001469 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001470}
1471
Avi Kivity1cd196e2011-09-13 10:45:51 +03001472static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001473{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001474 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001475 unsigned long selector;
1476 int rc;
1477
Avi Kivity9dac77f2011-06-01 15:34:25 +03001478 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001479 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001480 return rc;
1481
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001482 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001483 return rc;
1484}
1485
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001486static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001487{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001488 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001489 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001490 int reg = VCPU_REGS_RAX;
1491
1492 while (reg <= VCPU_REGS_RDI) {
1493 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001494 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001495
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001496 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001497 if (rc != X86EMUL_CONTINUE)
1498 return rc;
1499
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001500 ++reg;
1501 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001502
Wei Yongjunc37eda12010-06-15 09:03:33 +08001503 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001504}
1505
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001506static int em_pushf(struct x86_emulate_ctxt *ctxt)
1507{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001508 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001509 return em_push(ctxt);
1510}
1511
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001512static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001513{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001514 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001515 int reg = VCPU_REGS_RDI;
1516
1517 while (reg >= VCPU_REGS_RAX) {
1518 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001519 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1520 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001521 --reg;
1522 }
1523
Avi Kivity9dac77f2011-06-01 15:34:25 +03001524 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001525 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001526 break;
1527 --reg;
1528 }
1529 return rc;
1530}
1531
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001532int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001533{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001534 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001535 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001536 struct desc_ptr dt;
1537 gva_t cs_addr;
1538 gva_t eip_addr;
1539 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001540
1541 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001542 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001543 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001544 if (rc != X86EMUL_CONTINUE)
1545 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001546
1547 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1548
Avi Kivity9dac77f2011-06-01 15:34:25 +03001549 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001550 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001551 if (rc != X86EMUL_CONTINUE)
1552 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001553
Avi Kivity9dac77f2011-06-01 15:34:25 +03001554 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001555 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001556 if (rc != X86EMUL_CONTINUE)
1557 return rc;
1558
Avi Kivity4bff1e862011-04-20 13:37:53 +03001559 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001560
1561 eip_addr = dt.address + (irq << 2);
1562 cs_addr = dt.address + (irq << 2) + 2;
1563
Avi Kivity0f65dd72011-04-20 13:37:53 +03001564 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001565 if (rc != X86EMUL_CONTINUE)
1566 return rc;
1567
Avi Kivity0f65dd72011-04-20 13:37:53 +03001568 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001569 if (rc != X86EMUL_CONTINUE)
1570 return rc;
1571
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001572 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001573 if (rc != X86EMUL_CONTINUE)
1574 return rc;
1575
Avi Kivity9dac77f2011-06-01 15:34:25 +03001576 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001577
1578 return rc;
1579}
1580
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001581static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001582{
1583 switch(ctxt->mode) {
1584 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001585 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001586 case X86EMUL_MODE_VM86:
1587 case X86EMUL_MODE_PROT16:
1588 case X86EMUL_MODE_PROT32:
1589 case X86EMUL_MODE_PROT64:
1590 default:
1591 /* Protected mode interrupts unimplemented yet */
1592 return X86EMUL_UNHANDLEABLE;
1593 }
1594}
1595
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001596static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001597{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001598 int rc = X86EMUL_CONTINUE;
1599 unsigned long temp_eip = 0;
1600 unsigned long temp_eflags = 0;
1601 unsigned long cs = 0;
1602 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1603 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1604 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1605 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1606
1607 /* TODO: Add stack limit check */
1608
Avi Kivity9dac77f2011-06-01 15:34:25 +03001609 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001610
1611 if (rc != X86EMUL_CONTINUE)
1612 return rc;
1613
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001614 if (temp_eip & ~0xffff)
1615 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001616
Avi Kivity9dac77f2011-06-01 15:34:25 +03001617 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001618
1619 if (rc != X86EMUL_CONTINUE)
1620 return rc;
1621
Avi Kivity9dac77f2011-06-01 15:34:25 +03001622 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001623
1624 if (rc != X86EMUL_CONTINUE)
1625 return rc;
1626
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001627 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001628
1629 if (rc != X86EMUL_CONTINUE)
1630 return rc;
1631
Avi Kivity9dac77f2011-06-01 15:34:25 +03001632 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001633
1634
Avi Kivity9dac77f2011-06-01 15:34:25 +03001635 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001636 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001637 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001638 ctxt->eflags &= ~0xffff;
1639 ctxt->eflags |= temp_eflags;
1640 }
1641
1642 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1643 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1644
1645 return rc;
1646}
1647
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001648static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001649{
1650 switch(ctxt->mode) {
1651 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001652 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001653 case X86EMUL_MODE_VM86:
1654 case X86EMUL_MODE_PROT16:
1655 case X86EMUL_MODE_PROT32:
1656 case X86EMUL_MODE_PROT64:
1657 default:
1658 /* iret from protected mode unimplemented yet */
1659 return X86EMUL_UNHANDLEABLE;
1660 }
1661}
1662
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001663static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1664{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001665 int rc;
1666 unsigned short sel;
1667
Avi Kivity9dac77f2011-06-01 15:34:25 +03001668 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001669
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001670 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001671 if (rc != X86EMUL_CONTINUE)
1672 return rc;
1673
Avi Kivity9dac77f2011-06-01 15:34:25 +03001674 ctxt->_eip = 0;
1675 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001676 return X86EMUL_CONTINUE;
1677}
1678
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001679static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001680{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001681 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001682 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001683 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001684 break;
1685 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001686 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001687 break;
1688 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001689 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690 break;
1691 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001692 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001693 break;
1694 case 4: /* sal/shl */
1695 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001696 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697 break;
1698 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001699 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001700 break;
1701 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001702 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001703 break;
1704 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001705 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706}
1707
Avi Kivity3329ece2011-09-13 10:45:39 +03001708static int em_not(struct x86_emulate_ctxt *ctxt)
1709{
1710 ctxt->dst.val = ~ctxt->dst.val;
1711 return X86EMUL_CONTINUE;
1712}
1713
1714static int em_neg(struct x86_emulate_ctxt *ctxt)
1715{
1716 emulate_1op(ctxt, "neg");
1717 return X86EMUL_CONTINUE;
1718}
1719
1720static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1721{
1722 u8 ex = 0;
1723
1724 emulate_1op_rax_rdx(ctxt, "mul", ex);
1725 return X86EMUL_CONTINUE;
1726}
1727
1728static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1729{
1730 u8 ex = 0;
1731
1732 emulate_1op_rax_rdx(ctxt, "imul", ex);
1733 return X86EMUL_CONTINUE;
1734}
1735
1736static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737{
Avi Kivity34d1f492010-08-26 11:59:01 +03001738 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739
Avi Kivity3329ece2011-09-13 10:45:39 +03001740 emulate_1op_rax_rdx(ctxt, "div", de);
1741 if (de)
1742 return emulate_de(ctxt);
1743 return X86EMUL_CONTINUE;
1744}
1745
1746static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1747{
1748 u8 de = 0;
1749
1750 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001751 if (de)
1752 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001753 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754}
1755
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001756static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001758 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001759
Avi Kivity9dac77f2011-06-01 15:34:25 +03001760 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001762 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763 break;
1764 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001765 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001766 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001767 case 2: /* call near abs */ {
1768 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001769 old_eip = ctxt->_eip;
1770 ctxt->_eip = ctxt->src.val;
1771 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001772 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001773 break;
1774 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001776 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001778 case 5: /* jmp far */
1779 rc = em_jmp_far(ctxt);
1780 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001782 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001783 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001785 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001786}
1787
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001788static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001790 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791
Avi Kivity9dac77f2011-06-01 15:34:25 +03001792 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1793 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1794 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1795 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001796 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001797 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001798 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1799 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800
Laurent Vivier05f086f2007-09-24 11:10:55 +02001801 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001802 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001803 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001804}
1805
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001806static int em_ret(struct x86_emulate_ctxt *ctxt)
1807{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001808 ctxt->dst.type = OP_REG;
1809 ctxt->dst.addr.reg = &ctxt->_eip;
1810 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001811 return em_pop(ctxt);
1812}
1813
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001814static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001815{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001816 int rc;
1817 unsigned long cs;
1818
Avi Kivity9dac77f2011-06-01 15:34:25 +03001819 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001820 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001821 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001822 if (ctxt->op_bytes == 4)
1823 ctxt->_eip = (u32)ctxt->_eip;
1824 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001825 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001826 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001827 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001828 return rc;
1829}
1830
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09001831static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
1832{
1833 /* Save real source value, then compare EAX against destination. */
1834 ctxt->src.orig_val = ctxt->src.val;
1835 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
1836 emulate_2op_SrcV(ctxt, "cmp");
1837
1838 if (ctxt->eflags & EFLG_ZF) {
1839 /* Success: write back to memory. */
1840 ctxt->dst.val = ctxt->src.orig_val;
1841 } else {
1842 /* Failure: write the value we saw to EAX. */
1843 ctxt->dst.type = OP_REG;
1844 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
1845 }
1846 return X86EMUL_CONTINUE;
1847}
1848
Avi Kivityd4b43252011-09-13 10:45:50 +03001849static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001850{
Avi Kivityd4b43252011-09-13 10:45:50 +03001851 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001852 unsigned short sel;
1853 int rc;
1854
Avi Kivity9dac77f2011-06-01 15:34:25 +03001855 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001856
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001857 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001858 if (rc != X86EMUL_CONTINUE)
1859 return rc;
1860
Avi Kivity9dac77f2011-06-01 15:34:25 +03001861 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001862 return rc;
1863}
1864
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001865static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001866setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001867 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868{
Avi Kivity1aa36612011-04-27 13:20:30 +03001869 u16 selector;
1870
Gleb Natapov79168fd2010-04-28 19:15:30 +03001871 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001872 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001873 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001874
1875 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001876 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001878 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879 cs->type = 0x0b; /* Read, Execute, Accessed */
1880 cs->s = 1;
1881 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 cs->p = 1;
1883 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001884
Gleb Natapov79168fd2010-04-28 19:15:30 +03001885 set_desc_base(ss, 0); /* flat segment */
1886 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001887 ss->g = 1; /* 4kb granularity */
1888 ss->s = 1;
1889 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001890 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001892 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001893}
1894
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01001895static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
1896{
1897 struct x86_emulate_ops *ops = ctxt->ops;
1898 u32 eax, ebx, ecx, edx;
1899
1900 /*
1901 * syscall should always be enabled in longmode - so only become
1902 * vendor specific (cpuid) if other modes are active...
1903 */
1904 if (ctxt->mode == X86EMUL_MODE_PROT64)
1905 return true;
1906
1907 eax = 0x00000000;
1908 ecx = 0x00000000;
1909 if (ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx)) {
1910 /*
1911 * Intel ("GenuineIntel")
1912 * remark: Intel CPUs only support "syscall" in 64bit
1913 * longmode. Also an 64bit guest with a
1914 * 32bit compat-app running will #UD !! While this
1915 * behaviour can be fixed (by emulating) into AMD
1916 * response - CPUs of AMD can't behave like Intel.
1917 */
1918 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
1919 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
1920 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
1921 return false;
1922
1923 /* AMD ("AuthenticAMD") */
1924 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
1925 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
1926 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
1927 return true;
1928
1929 /* AMD ("AMDisbetter!") */
1930 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
1931 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
1932 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
1933 return true;
1934 }
1935
1936 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
1937 return false;
1938}
1939
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001940static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001941{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001942 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001943 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001944 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001945 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001946 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001947
1948 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001949 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001950 ctxt->mode == X86EMUL_MODE_VM86)
1951 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001952
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01001953 if (!(em_syscall_is_enabled(ctxt)))
1954 return emulate_ud(ctxt);
1955
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001956 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001957 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001958
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01001959 if (!(efer & EFER_SCE))
1960 return emulate_ud(ctxt);
1961
Avi Kivity717746e2011-04-20 13:37:53 +03001962 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001963 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001964 cs_sel = (u16)(msr_data & 0xfffc);
1965 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001966
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001967 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001968 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001969 cs.l = 1;
1970 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001971 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1972 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001973
Avi Kivity9dac77f2011-06-01 15:34:25 +03001974 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001975 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001976#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001978
Avi Kivity717746e2011-04-20 13:37:53 +03001979 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001980 ctxt->mode == X86EMUL_MODE_PROT64 ?
1981 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001982 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001983
Avi Kivity717746e2011-04-20 13:37:53 +03001984 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001985 ctxt->eflags &= ~(msr_data | EFLG_RF);
1986#endif
1987 } else {
1988 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001989 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001990 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001991
1992 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1993 }
1994
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001995 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001996}
1997
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001998static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001999{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002000 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002001 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002002 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002003 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002004 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002005
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002006 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002007 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002008 if (ctxt->mode == X86EMUL_MODE_REAL)
2009 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002010
2011 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2012 * Therefore, we inject an #UD.
2013 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002014 if (ctxt->mode == X86EMUL_MODE_PROT64)
2015 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002016
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002017 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002018
Avi Kivity717746e2011-04-20 13:37:53 +03002019 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002020 switch (ctxt->mode) {
2021 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002022 if ((msr_data & 0xfffc) == 0x0)
2023 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002024 break;
2025 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002026 if (msr_data == 0x0)
2027 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002028 break;
2029 }
2030
2031 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002032 cs_sel = (u16)msr_data;
2033 cs_sel &= ~SELECTOR_RPL_MASK;
2034 ss_sel = cs_sel + 8;
2035 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002036 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002037 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002038 cs.l = 1;
2039 }
2040
Avi Kivity1aa36612011-04-27 13:20:30 +03002041 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2042 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002043
Avi Kivity717746e2011-04-20 13:37:53 +03002044 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002045 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002046
Avi Kivity717746e2011-04-20 13:37:53 +03002047 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002048 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002049
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002050 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002051}
2052
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002053static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002054{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002055 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002056 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002057 u64 msr_data;
2058 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002059 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002060
Gleb Natapova0044752010-02-10 14:21:31 +02002061 /* inject #GP if in real mode or Virtual 8086 mode */
2062 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002063 ctxt->mode == X86EMUL_MODE_VM86)
2064 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002065
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002066 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002067
Avi Kivity9dac77f2011-06-01 15:34:25 +03002068 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002069 usermode = X86EMUL_MODE_PROT64;
2070 else
2071 usermode = X86EMUL_MODE_PROT32;
2072
2073 cs.dpl = 3;
2074 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002075 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002076 switch (usermode) {
2077 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002078 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002079 if ((msr_data & 0xfffc) == 0x0)
2080 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002081 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002082 break;
2083 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002084 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002085 if (msr_data == 0x0)
2086 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002087 ss_sel = cs_sel + 8;
2088 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002089 cs.l = 1;
2090 break;
2091 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002092 cs_sel |= SELECTOR_RPL_MASK;
2093 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002094
Avi Kivity1aa36612011-04-27 13:20:30 +03002095 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2096 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002097
Avi Kivity9dac77f2011-06-01 15:34:25 +03002098 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2099 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002100
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002101 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002102}
2103
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002104static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002105{
2106 int iopl;
2107 if (ctxt->mode == X86EMUL_MODE_REAL)
2108 return false;
2109 if (ctxt->mode == X86EMUL_MODE_VM86)
2110 return true;
2111 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002112 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002113}
2114
2115static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002116 u16 port, u16 len)
2117{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002118 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002119 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002120 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002121 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002122 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002123 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002124 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002125
Avi Kivity1aa36612011-04-27 13:20:30 +03002126 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002127 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002128 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002129 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002130 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002131 base = get_desc_base(&tr_seg);
2132#ifdef CONFIG_X86_64
2133 base |= ((u64)base3) << 32;
2134#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002135 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002136 if (r != X86EMUL_CONTINUE)
2137 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002138 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002139 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002140 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002141 if (r != X86EMUL_CONTINUE)
2142 return false;
2143 if ((perm >> bit_idx) & mask)
2144 return false;
2145 return true;
2146}
2147
2148static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002149 u16 port, u16 len)
2150{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002151 if (ctxt->perm_ok)
2152 return true;
2153
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002154 if (emulator_bad_iopl(ctxt))
2155 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002156 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002157
2158 ctxt->perm_ok = true;
2159
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002160 return true;
2161}
2162
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002164 struct tss_segment_16 *tss)
2165{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002166 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002168 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2169 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2170 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2171 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2172 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2173 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2174 tss->si = ctxt->regs[VCPU_REGS_RSI];
2175 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002176
Avi Kivity1aa36612011-04-27 13:20:30 +03002177 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2178 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2179 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2180 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2181 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002182}
2183
2184static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185 struct tss_segment_16 *tss)
2186{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002187 int ret;
2188
Avi Kivity9dac77f2011-06-01 15:34:25 +03002189 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002191 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2192 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2193 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2194 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2195 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2196 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2197 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2198 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002199
2200 /*
2201 * SDM says that segment selectors are loaded before segment
2202 * descriptors
2203 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002204 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2205 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2206 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2207 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2208 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002209
2210 /*
2211 * Now load segment descriptors. If fault happenes at this stage
2212 * it is handled in a context of new task
2213 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002214 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002215 if (ret != X86EMUL_CONTINUE)
2216 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002217 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218 if (ret != X86EMUL_CONTINUE)
2219 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002220 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002221 if (ret != X86EMUL_CONTINUE)
2222 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002223 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002224 if (ret != X86EMUL_CONTINUE)
2225 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002226 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002227 if (ret != X86EMUL_CONTINUE)
2228 return ret;
2229
2230 return X86EMUL_CONTINUE;
2231}
2232
2233static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002234 u16 tss_selector, u16 old_tss_sel,
2235 ulong old_tss_base, struct desc_struct *new_desc)
2236{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002237 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002238 struct tss_segment_16 tss_seg;
2239 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002240 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002241
Avi Kivity0f65dd72011-04-20 13:37:53 +03002242 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002243 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002244 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002245 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002246 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002247
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002248 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002249
Avi Kivity0f65dd72011-04-20 13:37:53 +03002250 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002251 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002252 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002253 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002254 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002255
Avi Kivity0f65dd72011-04-20 13:37:53 +03002256 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002257 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002258 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002259 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002260 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002261
2262 if (old_tss_sel != 0xffff) {
2263 tss_seg.prev_task_link = old_tss_sel;
2264
Avi Kivity0f65dd72011-04-20 13:37:53 +03002265 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002266 &tss_seg.prev_task_link,
2267 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002268 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002269 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002270 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002271 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002272 }
2273
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002274 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002275}
2276
2277static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002278 struct tss_segment_32 *tss)
2279{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002280 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002281 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002283 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2284 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2285 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2286 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2287 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2288 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2289 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2290 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002291
Avi Kivity1aa36612011-04-27 13:20:30 +03002292 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2293 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2294 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2295 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2296 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2297 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2298 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002299}
2300
2301static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002302 struct tss_segment_32 *tss)
2303{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002304 int ret;
2305
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002306 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002307 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002308 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002309 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002310 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2311 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2312 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2313 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2314 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2315 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2316 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2317 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318
2319 /*
2320 * SDM says that segment selectors are loaded before segment
2321 * descriptors
2322 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002323 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2324 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2325 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2326 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2327 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2328 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2329 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002330
2331 /*
2332 * Now load segment descriptors. If fault happenes at this stage
2333 * it is handled in a context of new task
2334 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002335 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002336 if (ret != X86EMUL_CONTINUE)
2337 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002338 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339 if (ret != X86EMUL_CONTINUE)
2340 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002341 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002342 if (ret != X86EMUL_CONTINUE)
2343 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002344 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002345 if (ret != X86EMUL_CONTINUE)
2346 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002347 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002348 if (ret != X86EMUL_CONTINUE)
2349 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002350 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002351 if (ret != X86EMUL_CONTINUE)
2352 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002353 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002354 if (ret != X86EMUL_CONTINUE)
2355 return ret;
2356
2357 return X86EMUL_CONTINUE;
2358}
2359
2360static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002361 u16 tss_selector, u16 old_tss_sel,
2362 ulong old_tss_base, struct desc_struct *new_desc)
2363{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002364 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002365 struct tss_segment_32 tss_seg;
2366 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002367 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002368
Avi Kivity0f65dd72011-04-20 13:37:53 +03002369 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002370 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002371 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002372 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002373 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002374
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002375 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002376
Avi Kivity0f65dd72011-04-20 13:37:53 +03002377 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002378 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002379 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002380 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002381 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002382
Avi Kivity0f65dd72011-04-20 13:37:53 +03002383 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002384 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002385 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002386 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002388
2389 if (old_tss_sel != 0xffff) {
2390 tss_seg.prev_task_link = old_tss_sel;
2391
Avi Kivity0f65dd72011-04-20 13:37:53 +03002392 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002393 &tss_seg.prev_task_link,
2394 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002395 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002396 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002398 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399 }
2400
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002401 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402}
2403
2404static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002405 u16 tss_selector, int reason,
2406 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002408 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 struct desc_struct curr_tss_desc, next_tss_desc;
2410 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002411 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002413 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002414 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002415
2416 /* FIXME: old_tss_base == ~0 ? */
2417
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002418 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419 if (ret != X86EMUL_CONTINUE)
2420 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002421 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422 if (ret != X86EMUL_CONTINUE)
2423 return ret;
2424
2425 /* FIXME: check that next_tss_desc is tss */
2426
2427 if (reason != TASK_SWITCH_IRET) {
2428 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002429 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002430 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002431 }
2432
Gleb Natapovceffb452010-03-18 15:20:19 +02002433 desc_limit = desc_limit_scaled(&next_tss_desc);
2434 if (!next_tss_desc.p ||
2435 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2436 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002437 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002438 return X86EMUL_PROPAGATE_FAULT;
2439 }
2440
2441 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2442 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002443 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002444 }
2445
2446 if (reason == TASK_SWITCH_IRET)
2447 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2448
2449 /* set back link to prev task only if NT bit is set in eflags
2450 note that old_tss_sel is not used afetr this point */
2451 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2452 old_tss_sel = 0xffff;
2453
2454 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002455 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002456 old_tss_base, &next_tss_desc);
2457 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002458 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002459 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002460 if (ret != X86EMUL_CONTINUE)
2461 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002462
2463 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2464 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2465
2466 if (reason != TASK_SWITCH_IRET) {
2467 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002468 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 }
2470
Avi Kivity717746e2011-04-20 13:37:53 +03002471 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002472 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473
Jan Kiszkae269fb22010-04-14 15:51:09 +02002474 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002475 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2476 ctxt->lock_prefix = 0;
2477 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002478 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002479 }
2480
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002481 return ret;
2482}
2483
2484int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002485 u16 tss_selector, int reason,
2486 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002488 int rc;
2489
Avi Kivity9dac77f2011-06-01 15:34:25 +03002490 ctxt->_eip = ctxt->eip;
2491 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002493 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002494 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002496 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002497 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002499 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500}
2501
Avi Kivity90de84f2010-11-17 15:28:21 +02002502static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002503 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002504{
Gleb Natapova682e352010-03-18 15:20:21 +02002505 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2506
Avi Kivity9dac77f2011-06-01 15:34:25 +03002507 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2508 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002509 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002510}
2511
Avi Kivity7af04fc2010-08-18 14:16:35 +03002512static int em_das(struct x86_emulate_ctxt *ctxt)
2513{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002514 u8 al, old_al;
2515 bool af, cf, old_cf;
2516
2517 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002518 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002519
2520 old_al = al;
2521 old_cf = cf;
2522 cf = false;
2523 af = ctxt->eflags & X86_EFLAGS_AF;
2524 if ((al & 0x0f) > 9 || af) {
2525 al -= 6;
2526 cf = old_cf | (al >= 250);
2527 af = true;
2528 } else {
2529 af = false;
2530 }
2531 if (old_al > 0x99 || old_cf) {
2532 al -= 0x60;
2533 cf = true;
2534 }
2535
Avi Kivity9dac77f2011-06-01 15:34:25 +03002536 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002537 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002538 ctxt->src.type = OP_IMM;
2539 ctxt->src.val = 0;
2540 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002541 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002542 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2543 if (cf)
2544 ctxt->eflags |= X86_EFLAGS_CF;
2545 if (af)
2546 ctxt->eflags |= X86_EFLAGS_AF;
2547 return X86EMUL_CONTINUE;
2548}
2549
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002550static int em_call(struct x86_emulate_ctxt *ctxt)
2551{
2552 long rel = ctxt->src.val;
2553
2554 ctxt->src.val = (unsigned long)ctxt->_eip;
2555 jmp_rel(ctxt, rel);
2556 return em_push(ctxt);
2557}
2558
Avi Kivity0ef753b2010-08-18 14:51:45 +03002559static int em_call_far(struct x86_emulate_ctxt *ctxt)
2560{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002561 u16 sel, old_cs;
2562 ulong old_eip;
2563 int rc;
2564
Avi Kivity1aa36612011-04-27 13:20:30 +03002565 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002566 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002567
Avi Kivity9dac77f2011-06-01 15:34:25 +03002568 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002569 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002570 return X86EMUL_CONTINUE;
2571
Avi Kivity9dac77f2011-06-01 15:34:25 +03002572 ctxt->_eip = 0;
2573 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002574
Avi Kivity9dac77f2011-06-01 15:34:25 +03002575 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002576 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002577 if (rc != X86EMUL_CONTINUE)
2578 return rc;
2579
Avi Kivity9dac77f2011-06-01 15:34:25 +03002580 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002581 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002582}
2583
Avi Kivity40ece7c2010-08-18 15:12:09 +03002584static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2585{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002586 int rc;
2587
Avi Kivity9dac77f2011-06-01 15:34:25 +03002588 ctxt->dst.type = OP_REG;
2589 ctxt->dst.addr.reg = &ctxt->_eip;
2590 ctxt->dst.bytes = ctxt->op_bytes;
2591 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002592 if (rc != X86EMUL_CONTINUE)
2593 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002594 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002595 return X86EMUL_CONTINUE;
2596}
2597
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002598static int em_add(struct x86_emulate_ctxt *ctxt)
2599{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002600 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002601 return X86EMUL_CONTINUE;
2602}
2603
2604static int em_or(struct x86_emulate_ctxt *ctxt)
2605{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002606 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002607 return X86EMUL_CONTINUE;
2608}
2609
2610static int em_adc(struct x86_emulate_ctxt *ctxt)
2611{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002612 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002613 return X86EMUL_CONTINUE;
2614}
2615
2616static int em_sbb(struct x86_emulate_ctxt *ctxt)
2617{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002618 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002619 return X86EMUL_CONTINUE;
2620}
2621
2622static int em_and(struct x86_emulate_ctxt *ctxt)
2623{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002624 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002625 return X86EMUL_CONTINUE;
2626}
2627
2628static int em_sub(struct x86_emulate_ctxt *ctxt)
2629{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002630 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002631 return X86EMUL_CONTINUE;
2632}
2633
2634static int em_xor(struct x86_emulate_ctxt *ctxt)
2635{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002636 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002637 return X86EMUL_CONTINUE;
2638}
2639
2640static int em_cmp(struct x86_emulate_ctxt *ctxt)
2641{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002642 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002643 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002644 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002645 return X86EMUL_CONTINUE;
2646}
2647
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002648static int em_test(struct x86_emulate_ctxt *ctxt)
2649{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002650 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002651 /* Disable writeback. */
2652 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002653 return X86EMUL_CONTINUE;
2654}
2655
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002656static int em_xchg(struct x86_emulate_ctxt *ctxt)
2657{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002658 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002659 ctxt->src.val = ctxt->dst.val;
2660 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002661
2662 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002663 ctxt->dst.val = ctxt->src.orig_val;
2664 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002665 return X86EMUL_CONTINUE;
2666}
2667
Avi Kivity5c82aa22010-08-18 18:31:43 +03002668static int em_imul(struct x86_emulate_ctxt *ctxt)
2669{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002670 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002671 return X86EMUL_CONTINUE;
2672}
2673
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002674static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2675{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002676 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002677 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002678}
2679
Avi Kivity61429142010-08-19 15:13:00 +03002680static int em_cwd(struct x86_emulate_ctxt *ctxt)
2681{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002682 ctxt->dst.type = OP_REG;
2683 ctxt->dst.bytes = ctxt->src.bytes;
2684 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2685 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002686
2687 return X86EMUL_CONTINUE;
2688}
2689
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002690static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2691{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002692 u64 tsc = 0;
2693
Avi Kivity717746e2011-04-20 13:37:53 +03002694 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002695 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2696 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002697 return X86EMUL_CONTINUE;
2698}
2699
Avi Kivity222d21a2011-11-10 14:57:30 +02002700static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2701{
2702 u64 pmc;
2703
2704 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2705 return emulate_gp(ctxt, 0);
2706 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2707 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2708 return X86EMUL_CONTINUE;
2709}
2710
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002711static int em_mov(struct x86_emulate_ctxt *ctxt)
2712{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002713 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002714 return X86EMUL_CONTINUE;
2715}
2716
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002717static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2718{
2719 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2720 return emulate_gp(ctxt, 0);
2721
2722 /* Disable writeback. */
2723 ctxt->dst.type = OP_NONE;
2724 return X86EMUL_CONTINUE;
2725}
2726
2727static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2728{
2729 unsigned long val;
2730
2731 if (ctxt->mode == X86EMUL_MODE_PROT64)
2732 val = ctxt->src.val & ~0ULL;
2733 else
2734 val = ctxt->src.val & ~0U;
2735
2736 /* #UD condition is already handled. */
2737 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2738 return emulate_gp(ctxt, 0);
2739
2740 /* Disable writeback. */
2741 ctxt->dst.type = OP_NONE;
2742 return X86EMUL_CONTINUE;
2743}
2744
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002745static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2746{
2747 u64 msr_data;
2748
2749 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2750 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2751 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2752 return emulate_gp(ctxt, 0);
2753
2754 return X86EMUL_CONTINUE;
2755}
2756
2757static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2758{
2759 u64 msr_data;
2760
2761 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2762 return emulate_gp(ctxt, 0);
2763
2764 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2765 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2766 return X86EMUL_CONTINUE;
2767}
2768
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002769static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2770{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002771 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002772 return emulate_ud(ctxt);
2773
Avi Kivity9dac77f2011-06-01 15:34:25 +03002774 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002775 return X86EMUL_CONTINUE;
2776}
2777
2778static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2779{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002780 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002781
Avi Kivity9dac77f2011-06-01 15:34:25 +03002782 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002783 return emulate_ud(ctxt);
2784
Avi Kivity9dac77f2011-06-01 15:34:25 +03002785 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002786 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2787
2788 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002789 ctxt->dst.type = OP_NONE;
2790 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002791}
2792
Avi Kivityaa97bb42010-01-20 18:09:23 +02002793static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2794{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002795 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002796 return X86EMUL_CONTINUE;
2797}
2798
Avi Kivity38503912011-03-31 18:48:09 +02002799static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2800{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002801 int rc;
2802 ulong linear;
2803
Avi Kivity9dac77f2011-06-01 15:34:25 +03002804 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002805 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002806 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002807 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002808 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002809 return X86EMUL_CONTINUE;
2810}
2811
Avi Kivity2d04a052011-04-20 15:32:49 +03002812static int em_clts(struct x86_emulate_ctxt *ctxt)
2813{
2814 ulong cr0;
2815
2816 cr0 = ctxt->ops->get_cr(ctxt, 0);
2817 cr0 &= ~X86_CR0_TS;
2818 ctxt->ops->set_cr(ctxt, 0, cr0);
2819 return X86EMUL_CONTINUE;
2820}
2821
Avi Kivity26d05cc2011-04-21 12:07:59 +03002822static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2823{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002824 int rc;
2825
Avi Kivity9dac77f2011-06-01 15:34:25 +03002826 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002827 return X86EMUL_UNHANDLEABLE;
2828
2829 rc = ctxt->ops->fix_hypercall(ctxt);
2830 if (rc != X86EMUL_CONTINUE)
2831 return rc;
2832
2833 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002834 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002835 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002836 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002837 return X86EMUL_CONTINUE;
2838}
2839
2840static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2841{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002842 struct desc_ptr desc_ptr;
2843 int rc;
2844
Avi Kivity9dac77f2011-06-01 15:34:25 +03002845 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002846 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002847 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002848 if (rc != X86EMUL_CONTINUE)
2849 return rc;
2850 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2851 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002852 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002853 return X86EMUL_CONTINUE;
2854}
2855
Avi Kivity5ef39c72011-04-21 12:21:50 +03002856static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002857{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002858 int rc;
2859
Avi Kivity5ef39c72011-04-21 12:21:50 +03002860 rc = ctxt->ops->fix_hypercall(ctxt);
2861
Avi Kivity26d05cc2011-04-21 12:07:59 +03002862 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002863 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002864 return rc;
2865}
2866
2867static int em_lidt(struct x86_emulate_ctxt *ctxt)
2868{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002869 struct desc_ptr desc_ptr;
2870 int rc;
2871
Avi Kivity9dac77f2011-06-01 15:34:25 +03002872 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002873 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002874 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002875 if (rc != X86EMUL_CONTINUE)
2876 return rc;
2877 ctxt->ops->set_idt(ctxt, &desc_ptr);
2878 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002879 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002880 return X86EMUL_CONTINUE;
2881}
2882
2883static int em_smsw(struct x86_emulate_ctxt *ctxt)
2884{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002885 ctxt->dst.bytes = 2;
2886 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002887 return X86EMUL_CONTINUE;
2888}
2889
2890static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2891{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002892 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002893 | (ctxt->src.val & 0x0f));
2894 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002895 return X86EMUL_CONTINUE;
2896}
2897
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002898static int em_loop(struct x86_emulate_ctxt *ctxt)
2899{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002900 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2901 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2902 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2903 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002904
2905 return X86EMUL_CONTINUE;
2906}
2907
2908static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2909{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002910 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2911 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002912
2913 return X86EMUL_CONTINUE;
2914}
2915
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09002916static int em_in(struct x86_emulate_ctxt *ctxt)
2917{
2918 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
2919 &ctxt->dst.val))
2920 return X86EMUL_IO_NEEDED;
2921
2922 return X86EMUL_CONTINUE;
2923}
2924
2925static int em_out(struct x86_emulate_ctxt *ctxt)
2926{
2927 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
2928 &ctxt->src.val, 1);
2929 /* Disable writeback. */
2930 ctxt->dst.type = OP_NONE;
2931 return X86EMUL_CONTINUE;
2932}
2933
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002934static int em_cli(struct x86_emulate_ctxt *ctxt)
2935{
2936 if (emulator_bad_iopl(ctxt))
2937 return emulate_gp(ctxt, 0);
2938
2939 ctxt->eflags &= ~X86_EFLAGS_IF;
2940 return X86EMUL_CONTINUE;
2941}
2942
2943static int em_sti(struct x86_emulate_ctxt *ctxt)
2944{
2945 if (emulator_bad_iopl(ctxt))
2946 return emulate_gp(ctxt, 0);
2947
2948 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2949 ctxt->eflags |= X86_EFLAGS_IF;
2950 return X86EMUL_CONTINUE;
2951}
2952
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09002953static int em_bt(struct x86_emulate_ctxt *ctxt)
2954{
2955 /* Disable writeback. */
2956 ctxt->dst.type = OP_NONE;
2957 /* only subword offset */
2958 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
2959
2960 emulate_2op_SrcV_nobyte(ctxt, "bt");
2961 return X86EMUL_CONTINUE;
2962}
2963
2964static int em_bts(struct x86_emulate_ctxt *ctxt)
2965{
2966 emulate_2op_SrcV_nobyte(ctxt, "bts");
2967 return X86EMUL_CONTINUE;
2968}
2969
2970static int em_btr(struct x86_emulate_ctxt *ctxt)
2971{
2972 emulate_2op_SrcV_nobyte(ctxt, "btr");
2973 return X86EMUL_CONTINUE;
2974}
2975
2976static int em_btc(struct x86_emulate_ctxt *ctxt)
2977{
2978 emulate_2op_SrcV_nobyte(ctxt, "btc");
2979 return X86EMUL_CONTINUE;
2980}
2981
Takuya Yoshikawaff227392011-11-22 15:21:33 +09002982static int em_bsf(struct x86_emulate_ctxt *ctxt)
2983{
2984 u8 zf;
2985
2986 __asm__ ("bsf %2, %0; setz %1"
2987 : "=r"(ctxt->dst.val), "=q"(zf)
2988 : "r"(ctxt->src.val));
2989
2990 ctxt->eflags &= ~X86_EFLAGS_ZF;
2991 if (zf) {
2992 ctxt->eflags |= X86_EFLAGS_ZF;
2993 /* Disable writeback. */
2994 ctxt->dst.type = OP_NONE;
2995 }
2996 return X86EMUL_CONTINUE;
2997}
2998
2999static int em_bsr(struct x86_emulate_ctxt *ctxt)
3000{
3001 u8 zf;
3002
3003 __asm__ ("bsr %2, %0; setz %1"
3004 : "=r"(ctxt->dst.val), "=q"(zf)
3005 : "r"(ctxt->src.val));
3006
3007 ctxt->eflags &= ~X86_EFLAGS_ZF;
3008 if (zf) {
3009 ctxt->eflags |= X86_EFLAGS_ZF;
3010 /* Disable writeback. */
3011 ctxt->dst.type = OP_NONE;
3012 }
3013 return X86EMUL_CONTINUE;
3014}
3015
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003016static bool valid_cr(int nr)
3017{
3018 switch (nr) {
3019 case 0:
3020 case 2 ... 4:
3021 case 8:
3022 return true;
3023 default:
3024 return false;
3025 }
3026}
3027
3028static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3029{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003030 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003031 return emulate_ud(ctxt);
3032
3033 return X86EMUL_CONTINUE;
3034}
3035
3036static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3037{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003038 u64 new_val = ctxt->src.val64;
3039 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003040 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003041
3042 static u64 cr_reserved_bits[] = {
3043 0xffffffff00000000ULL,
3044 0, 0, 0, /* CR3 checked later */
3045 CR4_RESERVED_BITS,
3046 0, 0, 0,
3047 CR8_RESERVED_BITS,
3048 };
3049
3050 if (!valid_cr(cr))
3051 return emulate_ud(ctxt);
3052
3053 if (new_val & cr_reserved_bits[cr])
3054 return emulate_gp(ctxt, 0);
3055
3056 switch (cr) {
3057 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003058 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003059 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3060 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3061 return emulate_gp(ctxt, 0);
3062
Avi Kivity717746e2011-04-20 13:37:53 +03003063 cr4 = ctxt->ops->get_cr(ctxt, 4);
3064 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003065
3066 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3067 !(cr4 & X86_CR4_PAE))
3068 return emulate_gp(ctxt, 0);
3069
3070 break;
3071 }
3072 case 3: {
3073 u64 rsvd = 0;
3074
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003075 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3076 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003077 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003078 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003079 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003080 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003081 rsvd = CR3_NONPAE_RESERVED_BITS;
3082
3083 if (new_val & rsvd)
3084 return emulate_gp(ctxt, 0);
3085
3086 break;
3087 }
3088 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003089 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003090
3091 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3092 return emulate_gp(ctxt, 0);
3093
3094 break;
3095 }
3096 }
3097
3098 return X86EMUL_CONTINUE;
3099}
3100
Joerg Roedel3b88e412011-04-04 12:39:29 +02003101static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3102{
3103 unsigned long dr7;
3104
Avi Kivity717746e2011-04-20 13:37:53 +03003105 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003106
3107 /* Check if DR7.Global_Enable is set */
3108 return dr7 & (1 << 13);
3109}
3110
3111static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3112{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003113 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003114 u64 cr4;
3115
3116 if (dr > 7)
3117 return emulate_ud(ctxt);
3118
Avi Kivity717746e2011-04-20 13:37:53 +03003119 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003120 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3121 return emulate_ud(ctxt);
3122
3123 if (check_dr7_gd(ctxt))
3124 return emulate_db(ctxt);
3125
3126 return X86EMUL_CONTINUE;
3127}
3128
3129static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3130{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003131 u64 new_val = ctxt->src.val64;
3132 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003133
3134 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3135 return emulate_gp(ctxt, 0);
3136
3137 return check_dr_read(ctxt);
3138}
3139
Joerg Roedel01de8b02011-04-04 12:39:31 +02003140static int check_svme(struct x86_emulate_ctxt *ctxt)
3141{
3142 u64 efer;
3143
Avi Kivity717746e2011-04-20 13:37:53 +03003144 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003145
3146 if (!(efer & EFER_SVME))
3147 return emulate_ud(ctxt);
3148
3149 return X86EMUL_CONTINUE;
3150}
3151
3152static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3153{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003154 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003155
3156 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003157 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003158 return emulate_gp(ctxt, 0);
3159
3160 return check_svme(ctxt);
3161}
3162
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003163static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3164{
Avi Kivity717746e2011-04-20 13:37:53 +03003165 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003166
Avi Kivity717746e2011-04-20 13:37:53 +03003167 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003168 return emulate_ud(ctxt);
3169
3170 return X86EMUL_CONTINUE;
3171}
3172
Joerg Roedel80612522011-04-04 12:39:33 +02003173static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3174{
Avi Kivity717746e2011-04-20 13:37:53 +03003175 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003176 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003177
Avi Kivity717746e2011-04-20 13:37:53 +03003178 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003179 (rcx > 3))
3180 return emulate_gp(ctxt, 0);
3181
3182 return X86EMUL_CONTINUE;
3183}
3184
Joerg Roedelf6511932011-04-04 12:39:35 +02003185static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3186{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003187 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3188 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003189 return emulate_gp(ctxt, 0);
3190
3191 return X86EMUL_CONTINUE;
3192}
3193
3194static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3195{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003196 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3197 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003198 return emulate_gp(ctxt, 0);
3199
3200 return X86EMUL_CONTINUE;
3201}
3202
Avi Kivity73fba5f2010-07-29 15:11:53 +03003203#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003204#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003205#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3206 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003207#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003208#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003209#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03003210#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003211#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003212#define II(_f, _e, _i) \
3213 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003214#define IIP(_f, _e, _i, _p) \
3215 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3216 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003217#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003218
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003219#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003220#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003221#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003222#define I2bvIP(_f, _e, _i, _p) \
3223 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003224
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003225#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3226 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3227 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003228
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003229static struct opcode group7_rm1[] = {
3230 DI(SrcNone | ModRM | Priv, monitor),
3231 DI(SrcNone | ModRM | Priv, mwait),
3232 N, N, N, N, N, N,
3233};
3234
Joerg Roedel01de8b02011-04-04 12:39:31 +02003235static struct opcode group7_rm3[] = {
3236 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003237 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003238 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3239 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3240 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3241 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3242 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3243 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3244};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003245
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003246static struct opcode group7_rm7[] = {
3247 N,
3248 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3249 N, N, N, N, N, N,
3250};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003251
Avi Kivity73fba5f2010-07-29 15:11:53 +03003252static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003253 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003254 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003255 I(Lock, em_adc),
3256 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003257 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003258 I(Lock, em_sub),
3259 I(Lock, em_xor),
3260 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003261};
3262
3263static struct opcode group1A[] = {
Takuya Yoshikawac15af352011-12-06 18:06:02 +09003264 I(DstMem | SrcNone | ModRM | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003265};
3266
3267static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003268 I(DstMem | SrcImm | ModRM, em_test),
3269 I(DstMem | SrcImm | ModRM, em_test),
3270 I(DstMem | SrcNone | ModRM | Lock, em_not),
3271 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3272 I(SrcMem | ModRM, em_mul_ex),
3273 I(SrcMem | ModRM, em_imul_ex),
3274 I(SrcMem | ModRM, em_div_ex),
3275 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003276};
3277
3278static struct opcode group4[] = {
Takuya Yoshikawac04ec832011-12-06 18:06:44 +09003279 I(ByteOp | DstMem | SrcNone | ModRM | Lock, em_grp45),
3280 I(ByteOp | DstMem | SrcNone | ModRM | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003281 N, N, N, N, N, N,
3282};
3283
3284static struct opcode group5[] = {
Takuya Yoshikawac04ec832011-12-06 18:06:44 +09003285 I(DstMem | SrcNone | ModRM | Lock, em_grp45),
3286 I(DstMem | SrcNone | ModRM | Lock, em_grp45),
3287 I(SrcMem | ModRM | Stack, em_grp45),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003288 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Takuya Yoshikawac04ec832011-12-06 18:06:44 +09003289 I(SrcMem | ModRM | Stack, em_grp45),
3290 I(SrcMemFAddr | ModRM | ImplicitOps, em_grp45),
3291 I(SrcMem | ModRM | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003292};
3293
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003294static struct opcode group6[] = {
3295 DI(ModRM | Prot, sldt),
3296 DI(ModRM | Prot, str),
3297 DI(ModRM | Prot | Priv, lldt),
3298 DI(ModRM | Prot | Priv, ltr),
3299 N, N, N, N,
3300};
3301
Avi Kivity73fba5f2010-07-29 15:11:53 +03003302static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003303 DI(ModRM | Mov | DstMem | Priv, sgdt),
3304 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003305 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3306 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3307 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3308 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3309 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003310}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003311 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3312 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003313 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003314 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3315 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003316} };
3317
3318static struct opcode group8[] = {
3319 N, N, N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003320 I(DstMem | SrcImmByte | ModRM, em_bt),
3321 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_bts),
3322 I(DstMem | SrcImmByte | ModRM | Lock, em_btr),
3323 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003324};
3325
3326static struct group_dual group9 = { {
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09003327 N, I(DstMem64 | ModRM | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003328}, {
3329 N, N, N, N, N, N, N, N,
3330} };
3331
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003332static struct opcode group11[] = {
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003333 I(DstMem | SrcImm | ModRM | Mov | PageTable, em_mov),
3334 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003335};
3336
Avi Kivityaa97bb42010-01-20 18:09:23 +02003337static struct gprefix pfx_0f_6f_0f_7f = {
3338 N, N, N, I(Sse, em_movdqu),
3339};
3340
Avi Kivity73fba5f2010-07-29 15:11:53 +03003341static struct opcode opcode_table[256] = {
3342 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003343 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003344 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3345 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003346 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003347 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003348 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3349 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003350 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003351 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003352 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3353 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003354 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003355 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003356 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3357 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003358 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003359 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003360 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003361 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003362 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003363 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003364 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003365 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003366 /* 0x40 - 0x4F */
3367 X16(D(DstReg)),
3368 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003369 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003370 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003371 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003372 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003373 I(ImplicitOps | Stack | No64, em_pusha),
3374 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003375 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3376 N, N, N, N,
3377 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003378 I(SrcImm | Mov | Stack, em_push),
3379 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003380 I(SrcImmByte | Mov | Stack, em_push),
3381 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003382 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3383 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003384 /* 0x70 - 0x7F */
3385 X16(D(SrcImmByte)),
3386 /* 0x80 - 0x87 */
3387 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3388 G(DstMem | SrcImm | ModRM | Group, group1),
3389 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3390 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003391 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003392 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003393 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003394 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003395 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003396 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003397 D(ModRM | SrcMem | NoAccess | DstReg),
3398 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3399 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003400 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003401 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003402 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003403 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003404 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003405 II(ImplicitOps | Stack, em_pushf, pushf),
3406 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003407 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003408 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003409 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003410 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003411 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003412 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003413 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003414 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3415 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003416 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003417 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003418 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003419 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003420 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003421 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003422 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003423 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003424 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003425 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3426 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003427 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003428 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003429 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003430 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003431 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003432 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003433 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003434 N, N, N, N,
3435 /* 0xD8 - 0xDF */
3436 N, N, N, N, N, N, N, N,
3437 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003438 X3(I(SrcImmByte, em_loop)),
3439 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003440 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3441 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003442 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003443 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003444 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003445 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3446 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003447 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003448 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003449 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3450 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003451 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003452 D(ImplicitOps), D(ImplicitOps),
3453 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003454 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3455};
3456
3457static struct opcode twobyte_table[256] = {
3458 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003459 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003460 N, I(ImplicitOps | VendorSpecific, em_syscall),
3461 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003462 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003463 N, D(ImplicitOps | ModRM), N, N,
3464 /* 0x10 - 0x1F */
3465 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3466 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003467 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003468 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003469 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3470 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003471 N, N, N, N,
3472 N, N, N, N, N, N, N, N,
3473 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003474 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003475 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003476 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003477 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003478 I(ImplicitOps | VendorSpecific, em_sysenter),
3479 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003480 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003481 N, N, N, N, N, N, N, N,
3482 /* 0x40 - 0x4F */
3483 X16(D(DstReg | SrcMem | ModRM | Mov)),
3484 /* 0x50 - 0x5F */
3485 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3486 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003487 N, N, N, N,
3488 N, N, N, N,
3489 N, N, N, N,
3490 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003491 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003492 N, N, N, N,
3493 N, N, N, N,
3494 N, N, N, N,
3495 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003496 /* 0x80 - 0x8F */
3497 X16(D(SrcImm)),
3498 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003499 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003500 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003501 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003502 DI(ImplicitOps, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003503 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3504 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3505 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003506 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003507 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003508 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003509 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3510 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003511 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003512 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003513 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003514 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003515 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003516 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3517 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003518 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003519 /* 0xB8 - 0xBF */
3520 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003521 G(BitOp, group8),
3522 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003523 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003524 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003525 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003526 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003527 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003528 N, N, N, GD(0, &group9),
3529 N, N, N, N, N, N, N, N,
3530 /* 0xD0 - 0xDF */
3531 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3532 /* 0xE0 - 0xEF */
3533 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3534 /* 0xF0 - 0xFF */
3535 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3536};
3537
3538#undef D
3539#undef N
3540#undef G
3541#undef GD
3542#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003543#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003544#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003545
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003546#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003547#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003548#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003549#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003550#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003551
Avi Kivity9dac77f2011-06-01 15:34:25 +03003552static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003553{
3554 unsigned size;
3555
Avi Kivity9dac77f2011-06-01 15:34:25 +03003556 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003557 if (size == 8)
3558 size = 4;
3559 return size;
3560}
3561
3562static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3563 unsigned size, bool sign_extension)
3564{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003565 int rc = X86EMUL_CONTINUE;
3566
3567 op->type = OP_IMM;
3568 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003569 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003570 /* NB. Immediates are sign-extended as necessary. */
3571 switch (op->bytes) {
3572 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003573 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003574 break;
3575 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003576 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003577 break;
3578 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003579 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003580 break;
3581 }
3582 if (!sign_extension) {
3583 switch (op->bytes) {
3584 case 1:
3585 op->val &= 0xff;
3586 break;
3587 case 2:
3588 op->val &= 0xffff;
3589 break;
3590 case 4:
3591 op->val &= 0xffffffff;
3592 break;
3593 }
3594 }
3595done:
3596 return rc;
3597}
3598
Avi Kivitya99455492011-09-13 10:45:41 +03003599static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3600 unsigned d)
3601{
3602 int rc = X86EMUL_CONTINUE;
3603
3604 switch (d) {
3605 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003606 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03003607 break;
3608 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003609 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03003610 break;
3611 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003612 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003613 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03003614 *op = ctxt->memop;
3615 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003616 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03003617 fetch_bit_operand(ctxt);
3618 op->orig_val = op->val;
3619 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003620 case OpMem64:
3621 ctxt->memop.bytes = 8;
3622 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03003623 case OpAcc:
3624 op->type = OP_REG;
3625 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3626 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3627 fetch_register_operand(op);
3628 op->orig_val = op->val;
3629 break;
3630 case OpDI:
3631 op->type = OP_MEM;
3632 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3633 op->addr.mem.ea =
3634 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3635 op->addr.mem.seg = VCPU_SREG_ES;
3636 op->val = 0;
3637 break;
3638 case OpDX:
3639 op->type = OP_REG;
3640 op->bytes = 2;
3641 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3642 fetch_register_operand(op);
3643 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003644 case OpCL:
3645 op->bytes = 1;
3646 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3647 break;
3648 case OpImmByte:
3649 rc = decode_imm(ctxt, op, 1, true);
3650 break;
3651 case OpOne:
3652 op->bytes = 1;
3653 op->val = 1;
3654 break;
3655 case OpImm:
3656 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3657 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003658 case OpMem8:
3659 ctxt->memop.bytes = 1;
3660 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003661 case OpMem16:
3662 ctxt->memop.bytes = 2;
3663 goto mem_common;
3664 case OpMem32:
3665 ctxt->memop.bytes = 4;
3666 goto mem_common;
3667 case OpImmU16:
3668 rc = decode_imm(ctxt, op, 2, false);
3669 break;
3670 case OpImmU:
3671 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3672 break;
3673 case OpSI:
3674 op->type = OP_MEM;
3675 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3676 op->addr.mem.ea =
3677 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3678 op->addr.mem.seg = seg_override(ctxt);
3679 op->val = 0;
3680 break;
3681 case OpImmFAddr:
3682 op->type = OP_IMM;
3683 op->addr.mem.ea = ctxt->_eip;
3684 op->bytes = ctxt->op_bytes + 2;
3685 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3686 break;
3687 case OpMemFAddr:
3688 ctxt->memop.bytes = ctxt->op_bytes + 2;
3689 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003690 case OpES:
3691 op->val = VCPU_SREG_ES;
3692 break;
3693 case OpCS:
3694 op->val = VCPU_SREG_CS;
3695 break;
3696 case OpSS:
3697 op->val = VCPU_SREG_SS;
3698 break;
3699 case OpDS:
3700 op->val = VCPU_SREG_DS;
3701 break;
3702 case OpFS:
3703 op->val = VCPU_SREG_FS;
3704 break;
3705 case OpGS:
3706 op->val = VCPU_SREG_GS;
3707 break;
Avi Kivitya99455492011-09-13 10:45:41 +03003708 case OpImplicit:
3709 /* Special instructions do their own operand decoding. */
3710 default:
3711 op->type = OP_NONE; /* Disable writeback. */
3712 break;
3713 }
3714
3715done:
3716 return rc;
3717}
3718
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003719int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003720{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003721 int rc = X86EMUL_CONTINUE;
3722 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003723 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003724 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003725 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003726
Avi Kivityf09ed832011-09-13 10:45:40 +03003727 ctxt->memop.type = OP_NONE;
3728 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003729 ctxt->_eip = ctxt->eip;
3730 ctxt->fetch.start = ctxt->_eip;
3731 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003732 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003733 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003734
3735 switch (mode) {
3736 case X86EMUL_MODE_REAL:
3737 case X86EMUL_MODE_VM86:
3738 case X86EMUL_MODE_PROT16:
3739 def_op_bytes = def_ad_bytes = 2;
3740 break;
3741 case X86EMUL_MODE_PROT32:
3742 def_op_bytes = def_ad_bytes = 4;
3743 break;
3744#ifdef CONFIG_X86_64
3745 case X86EMUL_MODE_PROT64:
3746 def_op_bytes = 4;
3747 def_ad_bytes = 8;
3748 break;
3749#endif
3750 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003751 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003752 }
3753
Avi Kivity9dac77f2011-06-01 15:34:25 +03003754 ctxt->op_bytes = def_op_bytes;
3755 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003756
3757 /* Legacy prefixes. */
3758 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003759 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003760 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003761 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003762 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003763 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003764 break;
3765 case 0x67: /* address-size override */
3766 if (mode == X86EMUL_MODE_PROT64)
3767 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003768 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003769 else
3770 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003771 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003772 break;
3773 case 0x26: /* ES override */
3774 case 0x2e: /* CS override */
3775 case 0x36: /* SS override */
3776 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003777 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003778 break;
3779 case 0x64: /* FS override */
3780 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003781 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003782 break;
3783 case 0x40 ... 0x4f: /* REX */
3784 if (mode != X86EMUL_MODE_PROT64)
3785 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003786 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003787 continue;
3788 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003789 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003790 break;
3791 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003792 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003793 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003794 break;
3795 default:
3796 goto done_prefixes;
3797 }
3798
3799 /* Any legacy prefix after a REX prefix nullifies its effect. */
3800
Avi Kivity9dac77f2011-06-01 15:34:25 +03003801 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003802 }
3803
3804done_prefixes:
3805
3806 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003807 if (ctxt->rex_prefix & 8)
3808 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003809
3810 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003811 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003812 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003813 if (ctxt->b == 0x0f) {
3814 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003815 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003816 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003817 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003818 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003819
Avi Kivity9dac77f2011-06-01 15:34:25 +03003820 while (ctxt->d & GroupMask) {
3821 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003822 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003823 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003824 --ctxt->_eip;
3825 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003826 opcode = opcode.u.group[goffset];
3827 break;
3828 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003829 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003830 --ctxt->_eip;
3831 goffset = (ctxt->modrm >> 3) & 7;
3832 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003833 opcode = opcode.u.gdual->mod3[goffset];
3834 else
3835 opcode = opcode.u.gdual->mod012[goffset];
3836 break;
3837 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003838 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003839 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003840 break;
3841 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003842 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003843 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003844 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003845 switch (simd_prefix) {
3846 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3847 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3848 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3849 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3850 }
3851 break;
3852 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003853 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003854 }
Avi Kivity46561642011-04-24 14:09:59 +03003855
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003856 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003857 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003858 }
3859
Avi Kivity9dac77f2011-06-01 15:34:25 +03003860 ctxt->execute = opcode.u.execute;
3861 ctxt->check_perm = opcode.check_perm;
3862 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003863
3864 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003865 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003866 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003867
Avi Kivity9dac77f2011-06-01 15:34:25 +03003868 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003869 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003870
Avi Kivity9dac77f2011-06-01 15:34:25 +03003871 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3872 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003873
Avi Kivity9dac77f2011-06-01 15:34:25 +03003874 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003875 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003876 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003877 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003878 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003879 }
3880
Avi Kivity9dac77f2011-06-01 15:34:25 +03003881 if (ctxt->d & Sse)
3882 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003883
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003884 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003885 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003886 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003887 if (!ctxt->has_seg_override)
3888 set_seg_override(ctxt, ctxt->modrm_seg);
3889 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003890 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003891 if (rc != X86EMUL_CONTINUE)
3892 goto done;
3893
Avi Kivity9dac77f2011-06-01 15:34:25 +03003894 if (!ctxt->has_seg_override)
3895 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003896
Avi Kivityf09ed832011-09-13 10:45:40 +03003897 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003898
Avi Kivityf09ed832011-09-13 10:45:40 +03003899 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
3900 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003901
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003902 /*
3903 * Decode and fetch the source operand: register, memory
3904 * or immediate.
3905 */
Avi Kivity0fe59122011-09-13 10:45:47 +03003906 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003907 if (rc != X86EMUL_CONTINUE)
3908 goto done;
3909
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003910 /*
3911 * Decode and fetch the second source operand: register, memory
3912 * or immediate.
3913 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03003914 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003915 if (rc != X86EMUL_CONTINUE)
3916 goto done;
3917
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003918 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03003919 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003920
3921done:
Avi Kivityf09ed832011-09-13 10:45:40 +03003922 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
3923 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003924
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003925 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003926}
3927
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08003928bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
3929{
3930 return ctxt->d & PageTable;
3931}
3932
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003933static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3934{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003935 /* The second termination condition only applies for REPE
3936 * and REPNE. Test if the repeat string operation prefix is
3937 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3938 * corresponding termination condition according to:
3939 * - if REPE/REPZ and ZF = 0 then done
3940 * - if REPNE/REPNZ and ZF = 1 then done
3941 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003942 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3943 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3944 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003945 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003946 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003947 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3948 return true;
3949
3950 return false;
3951}
3952
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003953int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003954{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003955 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003956 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003957 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003958
Avi Kivity9dac77f2011-06-01 15:34:25 +03003959 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003960
Avi Kivity9dac77f2011-06-01 15:34:25 +03003961 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003962 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003963 goto done;
3964 }
3965
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003966 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003967 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003968 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003969 goto done;
3970 }
3971
Avi Kivity9dac77f2011-06-01 15:34:25 +03003972 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003973 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003974 goto done;
3975 }
3976
Avi Kivity9dac77f2011-06-01 15:34:25 +03003977 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003978 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3979 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003980 rc = emulate_ud(ctxt);
3981 goto done;
3982 }
3983
Avi Kivity9dac77f2011-06-01 15:34:25 +03003984 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003985 rc = emulate_nm(ctxt);
3986 goto done;
3987 }
3988
Avi Kivity9dac77f2011-06-01 15:34:25 +03003989 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3990 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003991 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003992 if (rc != X86EMUL_CONTINUE)
3993 goto done;
3994 }
3995
Gleb Natapove92805a2010-02-10 14:21:35 +02003996 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003997 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003998 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003999 goto done;
4000 }
4001
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004002 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004003 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004004 rc = emulate_ud(ctxt);
4005 goto done;
4006 }
4007
Joerg Roedeld09beab2011-04-04 12:39:25 +02004008 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004009 if (ctxt->check_perm) {
4010 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004011 if (rc != X86EMUL_CONTINUE)
4012 goto done;
4013 }
4014
Avi Kivity9dac77f2011-06-01 15:34:25 +03004015 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4016 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004017 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004018 if (rc != X86EMUL_CONTINUE)
4019 goto done;
4020 }
4021
Avi Kivity9dac77f2011-06-01 15:34:25 +03004022 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004023 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004024 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4025 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004026 goto done;
4027 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004028 }
4029
Avi Kivity9dac77f2011-06-01 15:34:25 +03004030 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4031 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4032 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004033 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004034 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004035 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004036 }
4037
Avi Kivity9dac77f2011-06-01 15:34:25 +03004038 if (ctxt->src2.type == OP_MEM) {
4039 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4040 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004041 if (rc != X86EMUL_CONTINUE)
4042 goto done;
4043 }
4044
Avi Kivity9dac77f2011-06-01 15:34:25 +03004045 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004046 goto special_insn;
4047
4048
Avi Kivity9dac77f2011-06-01 15:34:25 +03004049 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004050 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004051 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4052 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004053 if (rc != X86EMUL_CONTINUE)
4054 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004055 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004056 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004057
Avi Kivity018a98d2007-11-27 19:30:56 +02004058special_insn:
4059
Avi Kivity9dac77f2011-06-01 15:34:25 +03004060 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4061 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004062 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004063 if (rc != X86EMUL_CONTINUE)
4064 goto done;
4065 }
4066
Avi Kivity9dac77f2011-06-01 15:34:25 +03004067 if (ctxt->execute) {
4068 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004069 if (rc != X86EMUL_CONTINUE)
4070 goto done;
4071 goto writeback;
4072 }
4073
Avi Kivity9dac77f2011-06-01 15:34:25 +03004074 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075 goto twobyte_insn;
4076
Avi Kivity9dac77f2011-06-01 15:34:25 +03004077 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004078 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004079 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004080 break;
4081 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004082 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004083 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004085 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004087 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004089 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004090 if (test_cc(ctxt->b, ctxt->eflags))
4091 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004092 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004093 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004094 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004095 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004096 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004097 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004098 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004099 rc = em_xchg(ctxt);
4100 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004101 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 switch (ctxt->op_bytes) {
4103 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4104 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4105 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004106 }
4107 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004108 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004109 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004110 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004111 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004112 rc = emulate_int(ctxt, 3);
4113 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004114 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004115 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004116 break;
4117 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004118 if (ctxt->eflags & EFLG_OF)
4119 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004120 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004121 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004122 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004123 break;
4124 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004125 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004126 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004127 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004128 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004129 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004130 jmp_rel(ctxt, ctxt->src.val);
4131 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004132 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004133 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004134 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004135 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004136 case 0xf5: /* cmc */
4137 /* complement carry flag from eflags reg */
4138 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004139 break;
4140 case 0xf8: /* clc */
4141 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004142 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004143 case 0xf9: /* stc */
4144 ctxt->eflags |= EFLG_CF;
4145 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004146 case 0xfc: /* cld */
4147 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004148 break;
4149 case 0xfd: /* std */
4150 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004151 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004152 default:
4153 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004155
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004156 if (rc != X86EMUL_CONTINUE)
4157 goto done;
4158
Avi Kivity018a98d2007-11-27 19:30:56 +02004159writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004160 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004161 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004162 goto done;
4163
Gleb Natapov5cd21912010-03-18 15:20:26 +02004164 /*
4165 * restore dst type in case the decoding will be reused
4166 * (happens for string instruction )
4167 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004168 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004169
Avi Kivity9dac77f2011-06-01 15:34:25 +03004170 if ((ctxt->d & SrcMask) == SrcSI)
4171 string_addr_inc(ctxt, seg_override(ctxt),
4172 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004173
Avi Kivity9dac77f2011-06-01 15:34:25 +03004174 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004175 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004176 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004177
Avi Kivity9dac77f2011-06-01 15:34:25 +03004178 if (ctxt->rep_prefix && (ctxt->d & String)) {
4179 struct read_cache *r = &ctxt->io_read;
4180 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004181
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004182 if (!string_insn_completed(ctxt)) {
4183 /*
4184 * Re-enter guest when pio read ahead buffer is empty
4185 * or, if it is not used, after each 1024 iteration.
4186 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004187 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004188 (r->end == 0 || r->end != r->pos)) {
4189 /*
4190 * Reset read cache. Usually happens before
4191 * decode, but since instruction is restarted
4192 * we have to do it here.
4193 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004194 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004195 return EMULATION_RESTART;
4196 }
4197 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004198 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004199 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004200
Avi Kivity9dac77f2011-06-01 15:34:25 +03004201 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004202
4203done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004204 if (rc == X86EMUL_PROPAGATE_FAULT)
4205 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004206 if (rc == X86EMUL_INTERCEPTED)
4207 return EMULATION_INTERCEPTED;
4208
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004209 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004210
4211twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004212 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004213 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004214 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004215 break;
4216 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004217 case 0x0d: /* GrpP (prefetch) */
4218 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004219 break;
4220 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004221 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004222 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004223 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004224 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004225 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004226 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004227 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4228 if (!test_cc(ctxt->b, ctxt->eflags))
4229 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004231 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004232 if (test_cc(ctxt->b, ctxt->eflags))
4233 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004234 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004235 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004236 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004237 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004238 case 0xa4: /* shld imm8, r, r/m */
4239 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004240 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004241 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004242 case 0xac: /* shrd imm8, r, r/m */
4243 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004244 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004245 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004246 case 0xae: /* clflush */
4247 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004248 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004249 ctxt->dst.bytes = ctxt->op_bytes;
4250 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4251 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004254 ctxt->dst.bytes = ctxt->op_bytes;
4255 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4256 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004257 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004258 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004259 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004260 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004261 ctxt->src.val = ctxt->dst.orig_val;
4262 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004263 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004264 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004265 ctxt->dst.bytes = ctxt->op_bytes;
4266 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4267 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004268 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004269 default:
4270 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004271 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004272
4273 if (rc != X86EMUL_CONTINUE)
4274 goto done;
4275
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276 goto writeback;
4277
4278cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004279 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280}