blob: fb39e0b32ed173003dcdc0276337428fc482119d [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya9945542011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020060#define OpMem8 26ull /* 8-bit zero extended memory operand */
Avi Kivitya9945542011-09-13 10:45:41 +030061
Avi Kivity0fe59122011-09-13 10:45:47 +030062#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030063#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030064
65/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080066 * Opcode effective-address decode tables.
67 * Note that we only emulate instructions that have at least one memory
68 * operand (excluding implicit stack references). We assume that stack
69 * references and instruction fetches will never occur in special memory
70 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
71 * not be handled.
72 */
73
74/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030075#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030077#define DstShift 1
78#define ImplicitOps (OpImplicit << DstShift)
79#define DstReg (OpReg << DstShift)
80#define DstMem (OpMem << DstShift)
81#define DstAcc (OpAcc << DstShift)
82#define DstDI (OpDI << DstShift)
83#define DstMem64 (OpMem64 << DstShift)
84#define DstImmUByte (OpImmUByte << DstShift)
85#define DstDX (OpDX << DstShift)
86#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030088#define SrcShift 6
89#define SrcNone (OpNone << SrcShift)
90#define SrcReg (OpReg << SrcShift)
91#define SrcMem (OpMem << SrcShift)
92#define SrcMem16 (OpMem16 << SrcShift)
93#define SrcMem32 (OpMem32 << SrcShift)
94#define SrcImm (OpImm << SrcShift)
95#define SrcImmByte (OpImmByte << SrcShift)
96#define SrcOne (OpOne << SrcShift)
97#define SrcImmUByte (OpImmUByte << SrcShift)
98#define SrcImmU (OpImmU << SrcShift)
99#define SrcSI (OpSI << SrcShift)
100#define SrcImmFAddr (OpImmFAddr << SrcShift)
101#define SrcMemFAddr (OpMemFAddr << SrcShift)
102#define SrcAcc (OpAcc << SrcShift)
103#define SrcImmU16 (OpImmU16 << SrcShift)
104#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200105#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300106#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300107#define BitOp (1<<11)
108#define MemAbs (1<<12) /* Memory operand is absolute displacement */
109#define String (1<<13) /* String instruction (rep capable) */
110#define Stack (1<<14) /* Stack instruction (push/pop) */
111#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
112#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
113#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
114#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
115#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
116#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300117/* Generic ModRM decode. */
118#define ModRM (1<<19)
119/* Destination is only written; never read. */
120#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300121/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200122#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200123#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300124#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300125#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300126#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200127#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200128#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300129#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800130#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100131/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800132#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300133#define Src2None (OpNone << Src2Shift)
134#define Src2CL (OpCL << Src2Shift)
135#define Src2ImmByte (OpImmByte << Src2Shift)
136#define Src2One (OpOne << Src2Shift)
137#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300138#define Src2ES (OpES << Src2Shift)
139#define Src2CS (OpCS << Src2Shift)
140#define Src2SS (OpSS << Src2Shift)
141#define Src2DS (OpDS << Src2Shift)
142#define Src2FS (OpFS << Src2Shift)
143#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2Mask (OpMask << Src2Shift)
Avi Kivity1c11b372012-04-09 18:39:59 +0300145#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
146#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
147#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148
Avi Kivityd0e53322010-07-29 15:11:54 +0300149#define X2(x...) x, x
150#define X3(x...) X2(x), x
151#define X4(x...) X2(x), X2(x)
152#define X5(x...) X4(x), x
153#define X6(x...) X4(x), X2(x)
154#define X7(x...) X4(x), X3(x)
155#define X8(x...) X4(x), X4(x)
156#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300157
Avi Kivityd65b1de2010-07-29 15:11:35 +0300158struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300159 u64 flags : 56;
160 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300161 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300162 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300163 struct opcode *group;
164 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200165 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300166 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200167 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300168};
169
170struct group_dual {
171 struct opcode mod012[8];
172 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300173};
174
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200175struct gprefix {
176 struct opcode pfx_no;
177 struct opcode pfx_66;
178 struct opcode pfx_f2;
179 struct opcode pfx_f3;
180};
181
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200183#define EFLG_ID (1<<21)
184#define EFLG_VIP (1<<20)
185#define EFLG_VIF (1<<19)
186#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200187#define EFLG_VM (1<<17)
188#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200189#define EFLG_IOPL (3<<12)
190#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191#define EFLG_OF (1<<11)
192#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200193#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200194#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800195#define EFLG_SF (1<<7)
196#define EFLG_ZF (1<<6)
197#define EFLG_AF (1<<4)
198#define EFLG_PF (1<<2)
199#define EFLG_CF (1<<0)
200
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300201#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
202#define EFLG_RESERVED_ONE_MASK 2
203
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204/*
205 * Instruction emulation:
206 * Most instructions are emulated directly via a fragment of inline assembly
207 * code. This allows us to save/restore EFLAGS and thus very easily pick up
208 * any modified flags.
209 */
210
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800211#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212#define _LO32 "k" /* force 32-bit operand */
213#define _STK "%%rsp" /* stack pointer */
214#elif defined(__i386__)
215#define _LO32 "" /* force 32-bit operand */
216#define _STK "%%esp" /* stack pointer */
217#endif
218
219/*
220 * These EFLAGS bits are restored from saved value during emulation, and
221 * any changes are written back to the saved value after emulation.
222 */
223#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
224
225/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200226#define _PRE_EFLAGS(_sav, _msk, _tmp) \
227 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
228 "movl %"_sav",%"_LO32 _tmp"; " \
229 "push %"_tmp"; " \
230 "push %"_tmp"; " \
231 "movl %"_msk",%"_LO32 _tmp"; " \
232 "andl %"_LO32 _tmp",("_STK"); " \
233 "pushf; " \
234 "notl %"_LO32 _tmp"; " \
235 "andl %"_LO32 _tmp",("_STK"); " \
236 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
237 "pop %"_tmp"; " \
238 "orl %"_LO32 _tmp",("_STK"); " \
239 "popf; " \
240 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241
242/* After executing instruction: write-back necessary bits in EFLAGS. */
243#define _POST_EFLAGS(_sav, _msk, _tmp) \
244 /* _sav |= EFLAGS & _msk; */ \
245 "pushf; " \
246 "pop %"_tmp"; " \
247 "andl %"_msk",%"_LO32 _tmp"; " \
248 "orl %"_LO32 _tmp",%"_sav"; "
249
Avi Kivitydda96d82008-11-26 15:14:10 +0200250#ifdef CONFIG_X86_64
251#define ON64(x) x
252#else
253#define ON64(x)
254#endif
255
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300256#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200257 do { \
258 __asm__ __volatile__ ( \
259 _PRE_EFLAGS("0", "4", "2") \
260 _op _suffix " %"_x"3,%1; " \
261 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300262 : "=m" ((ctxt)->eflags), \
263 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200264 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300265 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200266 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200267
268
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300270#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200271 do { \
272 unsigned long _tmp; \
273 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300274 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200275 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300276 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200277 break; \
278 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300279 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200280 break; \
281 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300282 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200283 break; \
284 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 } while (0)
286
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300287#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200289 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300290 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300292 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 break; \
294 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300295 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 _wx, _wy, _lx, _ly, _qx, _qy); \
297 break; \
298 } \
299 } while (0)
300
301/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300302#define emulate_2op_SrcB(ctxt, _op) \
303 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304
305/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300306#define emulate_2op_SrcV(ctxt, _op) \
307 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308
309/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300310#define emulate_2op_SrcV_nobyte(ctxt, _op) \
311 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100313/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300314#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300315 do { \
316 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300317 _type _clv = (ctxt)->src2.val; \
318 _type _srcv = (ctxt)->src.val; \
319 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300320 \
321 __asm__ __volatile__ ( \
322 _PRE_EFLAGS("0", "5", "2") \
323 _op _suffix " %4,%1 \n" \
324 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300325 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300326 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
327 ); \
328 \
Avi Kivity761441b2011-09-07 16:41:36 +0300329 (ctxt)->src2.val = (unsigned long) _clv; \
330 (ctxt)->src2.val = (unsigned long) _srcv; \
331 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100332 } while (0)
333
Avi Kivity761441b2011-09-07 16:41:36 +0300334#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300335 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300336 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300337 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300338 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300339 break; \
340 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300341 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300342 break; \
343 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300344 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300345 break; \
346 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100347 } while (0)
348
Avi Kivityd1eef452011-09-07 16:41:38 +0300349#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350 do { \
351 unsigned long _tmp; \
352 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200353 __asm__ __volatile__ ( \
354 _PRE_EFLAGS("0", "3", "2") \
355 _op _suffix " %1; " \
356 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300357 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200358 "=&r" (_tmp) \
359 : "i" (EFLAGS_MASK)); \
360 } while (0)
361
362/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300363#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200364 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300365 switch ((ctxt)->dst.bytes) { \
366 case 1: __emulate_1op(ctxt, _op, "b"); break; \
367 case 2: __emulate_1op(ctxt, _op, "w"); break; \
368 case 4: __emulate_1op(ctxt, _op, "l"); break; \
369 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370 } \
371 } while (0)
372
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300373#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300374 do { \
375 unsigned long _tmp; \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300376 ulong *rax = &(ctxt)->regs[VCPU_REGS_RAX]; \
377 ulong *rdx = &(ctxt)->regs[VCPU_REGS_RDX]; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300378 \
379 __asm__ __volatile__ ( \
380 _PRE_EFLAGS("0", "5", "1") \
381 "1: \n\t" \
382 _op _suffix " %6; " \
383 "2: \n\t" \
384 _POST_EFLAGS("0", "5", "1") \
385 ".pushsection .fixup,\"ax\" \n\t" \
386 "3: movb $1, %4 \n\t" \
387 "jmp 2b \n\t" \
388 ".popsection \n\t" \
389 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300390 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
391 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
392 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
393 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300394 } while (0)
395
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300396/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300397#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300398 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300399 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300400 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300401 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300402 break; \
403 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300404 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300405 break; \
406 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300407 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300408 break; \
409 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300410 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300411 break; \
412 } \
413 } while (0)
414
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200415static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
416 enum x86_intercept intercept,
417 enum x86_intercept_stage stage)
418{
419 struct x86_instruction_info info = {
420 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300421 .rep_prefix = ctxt->rep_prefix,
422 .modrm_mod = ctxt->modrm_mod,
423 .modrm_reg = ctxt->modrm_reg,
424 .modrm_rm = ctxt->modrm_rm,
425 .src_val = ctxt->src.val64,
426 .src_bytes = ctxt->src.bytes,
427 .dst_bytes = ctxt->dst.bytes,
428 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200429 .next_rip = ctxt->eip,
430 };
431
Avi Kivity29535382011-04-20 13:37:53 +0300432 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200433}
434
Avi Kivity9dac77f2011-06-01 15:34:25 +0300435static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800436{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300437 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800438}
439
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800441static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300442address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800443{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300444 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800445 return reg;
446 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300447 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800448}
449
450static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300451register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800452{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300453 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800454}
455
Harvey Harrison7a9572752008-02-19 07:40:41 -0800456static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300457register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800458{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300459 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800460 *reg += inc;
461 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300462 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800463}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464
Avi Kivity9dac77f2011-06-01 15:34:25 +0300465static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800466{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300467 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800468}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300469
Avi Kivity56697682011-04-03 14:08:51 +0300470static u32 desc_limit_scaled(struct desc_struct *desc)
471{
472 u32 limit = get_desc_limit(desc);
473
474 return desc->g ? (limit << 12) | 0xfff : limit;
475}
476
Avi Kivity9dac77f2011-06-01 15:34:25 +0300477static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300478{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300479 ctxt->has_seg_override = true;
480 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300481}
482
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900483static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300484{
485 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
486 return 0;
487
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900488 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300489}
490
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300492{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300493 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300494 return 0;
495
Avi Kivity9dac77f2011-06-01 15:34:25 +0300496 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300497}
498
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200499static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
500 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300501{
Avi Kivityda9cb572010-11-22 17:53:21 +0200502 ctxt->exception.vector = vec;
503 ctxt->exception.error_code = error;
504 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200505 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300506}
507
Joerg Roedel3b88e412011-04-04 12:39:29 +0200508static int emulate_db(struct x86_emulate_ctxt *ctxt)
509{
510 return emulate_exception(ctxt, DB_VECTOR, 0, false);
511}
512
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200513static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300514{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200515 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300516}
517
Avi Kivity618ff152011-04-03 12:32:09 +0300518static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
519{
520 return emulate_exception(ctxt, SS_VECTOR, err, true);
521}
522
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200523static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300524{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300526}
527
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200528static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300529{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200530 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300531}
532
Avi Kivity34d1f492010-08-26 11:59:01 +0300533static int emulate_de(struct x86_emulate_ctxt *ctxt)
534{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200535 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300536}
537
Avi Kivity12537912011-03-29 11:41:27 +0200538static int emulate_nm(struct x86_emulate_ctxt *ctxt)
539{
540 return emulate_exception(ctxt, NM_VECTOR, 0, false);
541}
542
Avi Kivity1aa36612011-04-27 13:20:30 +0300543static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
544{
545 u16 selector;
546 struct desc_struct desc;
547
548 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
549 return selector;
550}
551
552static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
553 unsigned seg)
554{
555 u16 dummy;
556 u32 base3;
557 struct desc_struct desc;
558
559 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
560 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
561}
562
Avi Kivity1c11b372012-04-09 18:39:59 +0300563/*
564 * x86 defines three classes of vector instructions: explicitly
565 * aligned, explicitly unaligned, and the rest, which change behaviour
566 * depending on whether they're AVX encoded or not.
567 *
568 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
569 * subject to the same check.
570 */
571static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
572{
573 if (likely(size < 16))
574 return false;
575
576 if (ctxt->d & Aligned)
577 return true;
578 else if (ctxt->d & Unaligned)
579 return false;
580 else if (ctxt->d & Avx)
581 return false;
582 else
583 return true;
584}
585
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400586static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300587 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400588 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300589 ulong *linear)
590{
Avi Kivity618ff152011-04-03 12:32:09 +0300591 struct desc_struct desc;
592 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300593 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300594 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300595 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300596 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300597
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900598 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300599 switch (ctxt->mode) {
600 case X86EMUL_MODE_REAL:
601 break;
602 case X86EMUL_MODE_PROT64:
603 if (((signed long)la << 16) >> 16 != la)
604 return emulate_gp(ctxt, 0);
605 break;
606 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300607 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
608 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300609 if (!usable)
610 goto bad;
611 /* code segment or read-only data segment */
612 if (((desc.type & 8) || !(desc.type & 2)) && write)
613 goto bad;
614 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400615 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300616 goto bad;
617 lim = desc_limit_scaled(&desc);
618 if ((desc.type & 8) || !(desc.type & 4)) {
619 /* expand-up segment */
620 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
621 goto bad;
622 } else {
623 /* exapand-down segment */
624 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
625 goto bad;
626 lim = desc.d ? 0xffffffff : 0xffff;
627 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
628 goto bad;
629 }
Avi Kivity717746e2011-04-20 13:37:53 +0300630 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300631 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300632 cpl = max(cpl, rpl);
633 if (!(desc.type & 8)) {
634 /* data segment */
635 if (cpl > desc.dpl)
636 goto bad;
637 } else if ((desc.type & 8) && !(desc.type & 4)) {
638 /* nonconforming code segment */
639 if (cpl != desc.dpl)
640 goto bad;
641 } else if ((desc.type & 8) && (desc.type & 4)) {
642 /* conforming code segment */
643 if (cpl < desc.dpl)
644 goto bad;
645 }
646 break;
647 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300648 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300649 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300650 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
651 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300652 *linear = la;
653 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300654bad:
655 if (addr.seg == VCPU_SREG_SS)
656 return emulate_ss(ctxt, addr.seg);
657 else
658 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300659}
660
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400661static int linearize(struct x86_emulate_ctxt *ctxt,
662 struct segmented_address addr,
663 unsigned size, bool write,
664 ulong *linear)
665{
666 return __linearize(ctxt, addr, size, write, false, linear);
667}
668
669
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200670static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
671 struct segmented_address addr,
672 void *data,
673 unsigned size)
674{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200675 int rc;
676 ulong linear;
677
Avi Kivity83b87952011-04-03 11:31:19 +0300678 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200679 if (rc != X86EMUL_CONTINUE)
680 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300681 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200682}
683
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900684/*
685 * Fetch the next byte of the instruction being emulated which is pointed to
686 * by ctxt->_eip, then increment ctxt->_eip.
687 *
688 * Also prefetch the remaining bytes of the instruction without crossing page
689 * boundary if they are not in fetch_cache yet.
690 */
691static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200692{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300693 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200694 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300695 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200696
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900697 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400698 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900699 struct segmented_address addr = { .seg = VCPU_SREG_CS,
700 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300701 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900702 size = min(15UL - cur_size,
703 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400704 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900705 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400706 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900707 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
708 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900709 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200710 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300711 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200712 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900713 *dest = fc->data[ctxt->_eip - fc->start];
714 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900715 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200716}
717
718static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900719 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200720{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900721 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200722
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200723 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900724 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200725 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200726 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900727 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900728 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200729 return rc;
730 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900731 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200732}
733
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900734/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900735#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900736({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900737 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900738 if (rc != X86EMUL_CONTINUE) \
739 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900740 (_type)_x; \
741})
742
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900743#define insn_fetch_arr(_arr, _size, _ctxt) \
744({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900745 if (rc != X86EMUL_CONTINUE) \
746 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900747})
748
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000749/*
750 * Given the 'reg' portion of a ModRM byte, and a register block, return a
751 * pointer into the block that addresses the relevant register.
752 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
753 */
754static void *decode_register(u8 modrm_reg, unsigned long *regs,
755 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756{
757 void *p;
758
759 p = &regs[modrm_reg];
760 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
761 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
762 return p;
763}
764
765static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200766 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 u16 *size, unsigned long *address, int op_bytes)
768{
769 int rc;
770
771 if (op_bytes == 2)
772 op_bytes = 3;
773 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200774 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900775 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200777 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200778 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 return rc;
780}
781
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300782static int test_cc(unsigned int condition, unsigned int flags)
783{
784 int rc = 0;
785
786 switch ((condition & 15) >> 1) {
787 case 0: /* o */
788 rc |= (flags & EFLG_OF);
789 break;
790 case 1: /* b/c/nae */
791 rc |= (flags & EFLG_CF);
792 break;
793 case 2: /* z/e */
794 rc |= (flags & EFLG_ZF);
795 break;
796 case 3: /* be/na */
797 rc |= (flags & (EFLG_CF|EFLG_ZF));
798 break;
799 case 4: /* s */
800 rc |= (flags & EFLG_SF);
801 break;
802 case 5: /* p/pe */
803 rc |= (flags & EFLG_PF);
804 break;
805 case 7: /* le/ng */
806 rc |= (flags & EFLG_ZF);
807 /* fall through */
808 case 6: /* l/nge */
809 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
810 break;
811 }
812
813 /* Odd condition identifiers (lsb == 1) have inverted sense. */
814 return (!!rc ^ (condition & 1));
815}
816
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300817static void fetch_register_operand(struct operand *op)
818{
819 switch (op->bytes) {
820 case 1:
821 op->val = *(u8 *)op->addr.reg;
822 break;
823 case 2:
824 op->val = *(u16 *)op->addr.reg;
825 break;
826 case 4:
827 op->val = *(u32 *)op->addr.reg;
828 break;
829 case 8:
830 op->val = *(u64 *)op->addr.reg;
831 break;
832 }
833}
834
Avi Kivity12537912011-03-29 11:41:27 +0200835static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
836{
837 ctxt->ops->get_fpu(ctxt);
838 switch (reg) {
839 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
840 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
841 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
842 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
843 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
844 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
845 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
846 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
847#ifdef CONFIG_X86_64
848 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
849 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
850 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
851 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
852 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
853 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
854 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
855 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
856#endif
857 default: BUG();
858 }
859 ctxt->ops->put_fpu(ctxt);
860}
861
862static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
863 int reg)
864{
865 ctxt->ops->get_fpu(ctxt);
866 switch (reg) {
867 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
868 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
869 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
870 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
871 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
872 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
873 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
874 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
875#ifdef CONFIG_X86_64
876 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
877 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
878 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
879 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
880 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
881 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
882 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
883 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
884#endif
885 default: BUG();
886 }
887 ctxt->ops->put_fpu(ctxt);
888}
889
890static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200891 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200892{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300893 unsigned reg = ctxt->modrm_reg;
894 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200895
Avi Kivity9dac77f2011-06-01 15:34:25 +0300896 if (!(ctxt->d & ModRM))
897 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200898
Avi Kivity9dac77f2011-06-01 15:34:25 +0300899 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200900 op->type = OP_XMM;
901 op->bytes = 16;
902 op->addr.xmm = reg;
903 read_sse_reg(ctxt, &op->vec_val, reg);
904 return;
905 }
906
Avi Kivity3c118e22007-10-31 10:27:04 +0200907 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200908 if (ctxt->d & ByteOp) {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300909 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200910 op->bytes = 1;
911 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300912 op->addr.reg = decode_register(reg, ctxt->regs, 0);
913 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200914 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300915 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200916 op->orig_val = op->val;
917}
918
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200919static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300920 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200921{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200922 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700923 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900924 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300925 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200926
Avi Kivity9dac77f2011-06-01 15:34:25 +0300927 if (ctxt->rex_prefix) {
928 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
929 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
930 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200931 }
932
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900933 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300934 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
935 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
936 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
937 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200938
Avi Kivity9dac77f2011-06-01 15:34:25 +0300939 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300940 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300941 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
942 op->addr.reg = decode_register(ctxt->modrm_rm,
943 ctxt->regs, ctxt->d & ByteOp);
944 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200945 op->type = OP_XMM;
946 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300947 op->addr.xmm = ctxt->modrm_rm;
948 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200949 return rc;
950 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300951 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200952 return rc;
953 }
954
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300955 op->type = OP_MEM;
956
Avi Kivity9dac77f2011-06-01 15:34:25 +0300957 if (ctxt->ad_bytes == 2) {
958 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
959 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
960 unsigned si = ctxt->regs[VCPU_REGS_RSI];
961 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200962
963 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300964 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200965 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300966 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900967 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200968 break;
969 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900970 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200971 break;
972 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900973 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200974 break;
975 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300976 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200977 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300978 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200979 break;
980 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300981 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200982 break;
983 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300984 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200985 break;
986 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300987 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200988 break;
989 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300990 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200991 break;
992 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300993 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200994 break;
995 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300996 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300997 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200998 break;
999 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001000 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001 break;
1002 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001003 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1004 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1005 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001006 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001007 } else {
1008 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001009 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001010 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001011 index_reg |= (sib >> 3) & 7;
1012 base_reg |= sib & 7;
1013 scale = sib >> 6;
1014
Avi Kivity9dac77f2011-06-01 15:34:25 +03001015 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001016 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001017 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03001018 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001019 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001020 modrm_ea += ctxt->regs[index_reg] << scale;
1021 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001022 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001023 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -07001024 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +03001025 modrm_ea += ctxt->regs[ctxt->modrm_rm];
1026 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001027 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001028 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001029 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001030 break;
1031 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001032 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001033 break;
1034 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001035 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001036 break;
1037 }
1038 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001039 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001040done:
1041 return rc;
1042}
1043
1044static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001045 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001046{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001047 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001048
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001049 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001050 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001051 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001052 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001053 break;
1054 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001055 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001056 break;
1057 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001058 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001059 break;
1060 }
1061done:
1062 return rc;
1063}
1064
Avi Kivity9dac77f2011-06-01 15:34:25 +03001065static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001066{
Sheng Yang7129eec2010-09-28 16:33:32 +08001067 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001068
Avi Kivity9dac77f2011-06-01 15:34:25 +03001069 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1070 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001071
Avi Kivity9dac77f2011-06-01 15:34:25 +03001072 if (ctxt->src.bytes == 2)
1073 sv = (s16)ctxt->src.val & (s16)mask;
1074 else if (ctxt->src.bytes == 4)
1075 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001076
Avi Kivity9dac77f2011-06-01 15:34:25 +03001077 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001078 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001079
1080 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001081 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001082}
1083
Gleb Natapov9de41572010-04-28 19:15:22 +03001084static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001085 unsigned long addr, void *dest, unsigned size)
1086{
1087 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001089
1090 while (size) {
1091 int n = min(size, 8u);
1092 size -= n;
1093 if (mc->pos < mc->end)
1094 goto read_cached;
1095
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001096 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1097 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001098 if (rc != X86EMUL_CONTINUE)
1099 return rc;
1100 mc->end += n;
1101
1102 read_cached:
1103 memcpy(dest, mc->data + mc->pos, n);
1104 mc->pos += n;
1105 dest += n;
1106 addr += n;
1107 }
1108 return X86EMUL_CONTINUE;
1109}
1110
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001111static int segmented_read(struct x86_emulate_ctxt *ctxt,
1112 struct segmented_address addr,
1113 void *data,
1114 unsigned size)
1115{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001116 int rc;
1117 ulong linear;
1118
Avi Kivity83b87952011-04-03 11:31:19 +03001119 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001120 if (rc != X86EMUL_CONTINUE)
1121 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001122 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001123}
1124
1125static int segmented_write(struct x86_emulate_ctxt *ctxt,
1126 struct segmented_address addr,
1127 const void *data,
1128 unsigned size)
1129{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001130 int rc;
1131 ulong linear;
1132
Avi Kivity83b87952011-04-03 11:31:19 +03001133 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001134 if (rc != X86EMUL_CONTINUE)
1135 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001136 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1137 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001138}
1139
1140static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1141 struct segmented_address addr,
1142 const void *orig_data, const void *data,
1143 unsigned size)
1144{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001145 int rc;
1146 ulong linear;
1147
Avi Kivity83b87952011-04-03 11:31:19 +03001148 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001149 if (rc != X86EMUL_CONTINUE)
1150 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001151 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1152 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001153}
1154
Gleb Natapov7b262e92010-03-18 15:20:27 +02001155static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001156 unsigned int size, unsigned short port,
1157 void *dest)
1158{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001159 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001160
1161 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001162 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001163 unsigned int count = ctxt->rep_prefix ?
1164 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001165 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001166 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1167 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001168 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1169 count);
1170 if (n == 0)
1171 n = 1;
1172 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001173 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001174 return 0;
1175 rc->end = n * size;
1176 }
1177
1178 memcpy(dest, rc->data + rc->pos, size);
1179 rc->pos += size;
1180 return 1;
1181}
1182
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001183static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1184 u16 index, struct desc_struct *desc)
1185{
1186 struct desc_ptr dt;
1187 ulong addr;
1188
1189 ctxt->ops->get_idt(ctxt, &dt);
1190
1191 if (dt.size < index * 8 + 7)
1192 return emulate_gp(ctxt, index << 3 | 0x2);
1193
1194 addr = dt.address + index * 8;
1195 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1196 &ctxt->exception);
1197}
1198
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001199static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001200 u16 selector, struct desc_ptr *dt)
1201{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001202 struct x86_emulate_ops *ops = ctxt->ops;
1203
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001204 if (selector & 1 << 2) {
1205 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001206 u16 sel;
1207
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001208 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001209 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001210 return;
1211
1212 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1213 dt->address = get_desc_base(&desc);
1214 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001215 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001216}
1217
1218/* allowed just for 8 bytes segments */
1219static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001220 u16 selector, struct desc_struct *desc)
1221{
1222 struct desc_ptr dt;
1223 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001224 ulong addr;
1225
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001226 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001227
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001228 if (dt.size < index * 8 + 7)
1229 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001230
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001231 addr = dt.address + index * 8;
1232 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1233 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001234}
1235
1236/* allowed just for 8 bytes segments */
1237static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001238 u16 selector, struct desc_struct *desc)
1239{
1240 struct desc_ptr dt;
1241 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001242 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001243
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001244 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001245
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001246 if (dt.size < index * 8 + 7)
1247 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001248
1249 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001250 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1251 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001252}
1253
Gleb Natapov5601d052011-03-07 14:55:06 +02001254/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001255static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001256 u16 selector, int seg)
1257{
1258 struct desc_struct seg_desc;
1259 u8 dpl, rpl, cpl;
1260 unsigned err_vec = GP_VECTOR;
1261 u32 err_code = 0;
1262 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1263 int ret;
1264
1265 memset(&seg_desc, 0, sizeof seg_desc);
1266
1267 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1268 || ctxt->mode == X86EMUL_MODE_REAL) {
1269 /* set real mode segment descriptor */
1270 set_desc_base(&seg_desc, selector << 4);
1271 set_desc_limit(&seg_desc, 0xffff);
1272 seg_desc.type = 3;
1273 seg_desc.p = 1;
1274 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001275 if (ctxt->mode == X86EMUL_MODE_VM86)
1276 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001277 goto load;
1278 }
1279
1280 /* NULL selector is not valid for TR, CS and SS */
1281 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1282 && null_selector)
1283 goto exception;
1284
1285 /* TR should be in GDT only */
1286 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1287 goto exception;
1288
1289 if (null_selector) /* for NULL selector skip all following checks */
1290 goto load;
1291
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001292 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001293 if (ret != X86EMUL_CONTINUE)
1294 return ret;
1295
1296 err_code = selector & 0xfffc;
1297 err_vec = GP_VECTOR;
1298
1299 /* can't load system descriptor into segment selecor */
1300 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1301 goto exception;
1302
1303 if (!seg_desc.p) {
1304 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1305 goto exception;
1306 }
1307
1308 rpl = selector & 3;
1309 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001310 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001311
1312 switch (seg) {
1313 case VCPU_SREG_SS:
1314 /*
1315 * segment is not a writable data segment or segment
1316 * selector's RPL != CPL or segment selector's RPL != CPL
1317 */
1318 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1319 goto exception;
1320 break;
1321 case VCPU_SREG_CS:
1322 if (!(seg_desc.type & 8))
1323 goto exception;
1324
1325 if (seg_desc.type & 4) {
1326 /* conforming */
1327 if (dpl > cpl)
1328 goto exception;
1329 } else {
1330 /* nonconforming */
1331 if (rpl > cpl || dpl != cpl)
1332 goto exception;
1333 }
1334 /* CS(RPL) <- CPL */
1335 selector = (selector & 0xfffc) | cpl;
1336 break;
1337 case VCPU_SREG_TR:
1338 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1339 goto exception;
1340 break;
1341 case VCPU_SREG_LDTR:
1342 if (seg_desc.s || seg_desc.type != 2)
1343 goto exception;
1344 break;
1345 default: /* DS, ES, FS, or GS */
1346 /*
1347 * segment is not a data or readable code segment or
1348 * ((segment is a data or nonconforming code segment)
1349 * and (both RPL and CPL > DPL))
1350 */
1351 if ((seg_desc.type & 0xa) == 0x8 ||
1352 (((seg_desc.type & 0xc) != 0xc) &&
1353 (rpl > dpl && cpl > dpl)))
1354 goto exception;
1355 break;
1356 }
1357
1358 if (seg_desc.s) {
1359 /* mark segment as accessed */
1360 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001361 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001362 if (ret != X86EMUL_CONTINUE)
1363 return ret;
1364 }
1365load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001366 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001367 return X86EMUL_CONTINUE;
1368exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001369 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001370 return X86EMUL_PROPAGATE_FAULT;
1371}
1372
Wei Yongjun31be40b2010-08-17 09:17:30 +08001373static void write_register_operand(struct operand *op)
1374{
1375 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1376 switch (op->bytes) {
1377 case 1:
1378 *(u8 *)op->addr.reg = (u8)op->val;
1379 break;
1380 case 2:
1381 *(u16 *)op->addr.reg = (u16)op->val;
1382 break;
1383 case 4:
1384 *op->addr.reg = (u32)op->val;
1385 break; /* 64b: zero-extend */
1386 case 8:
1387 *op->addr.reg = op->val;
1388 break;
1389 }
1390}
1391
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001392static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001393{
1394 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001395
Avi Kivity9dac77f2011-06-01 15:34:25 +03001396 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001397 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001398 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001399 break;
1400 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001401 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001402 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001403 ctxt->dst.addr.mem,
1404 &ctxt->dst.orig_val,
1405 &ctxt->dst.val,
1406 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001407 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001408 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001409 ctxt->dst.addr.mem,
1410 &ctxt->dst.val,
1411 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001412 if (rc != X86EMUL_CONTINUE)
1413 return rc;
1414 break;
Avi Kivity12537912011-03-29 11:41:27 +02001415 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001416 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001417 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001418 case OP_NONE:
1419 /* no writeback */
1420 break;
1421 default:
1422 break;
1423 }
1424 return X86EMUL_CONTINUE;
1425}
1426
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001427static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001428{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001429 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001430
Avi Kivity9dac77f2011-06-01 15:34:25 +03001431 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1432 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001433 addr.seg = VCPU_SREG_SS;
1434
1435 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001436 ctxt->dst.type = OP_NONE;
1437 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001438}
1439
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001440static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001441 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001442{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001443 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001444 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001445
Avi Kivity9dac77f2011-06-01 15:34:25 +03001446 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001447 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001448 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001449 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001450 return rc;
1451
Avi Kivity9dac77f2011-06-01 15:34:25 +03001452 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001453 return rc;
1454}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001455
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001456static int em_pop(struct x86_emulate_ctxt *ctxt)
1457{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001458 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001459}
1460
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001461static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001462 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001463{
1464 int rc;
1465 unsigned long val, change_mask;
1466 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001467 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001468
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001469 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001470 if (rc != X86EMUL_CONTINUE)
1471 return rc;
1472
1473 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1474 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1475
1476 switch(ctxt->mode) {
1477 case X86EMUL_MODE_PROT64:
1478 case X86EMUL_MODE_PROT32:
1479 case X86EMUL_MODE_PROT16:
1480 if (cpl == 0)
1481 change_mask |= EFLG_IOPL;
1482 if (cpl <= iopl)
1483 change_mask |= EFLG_IF;
1484 break;
1485 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001486 if (iopl < 3)
1487 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001488 change_mask |= EFLG_IF;
1489 break;
1490 default: /* real mode */
1491 change_mask |= (EFLG_IOPL | EFLG_IF);
1492 break;
1493 }
1494
1495 *(unsigned long *)dest =
1496 (ctxt->eflags & ~change_mask) | (val & change_mask);
1497
1498 return rc;
1499}
1500
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001501static int em_popf(struct x86_emulate_ctxt *ctxt)
1502{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001503 ctxt->dst.type = OP_REG;
1504 ctxt->dst.addr.reg = &ctxt->eflags;
1505 ctxt->dst.bytes = ctxt->op_bytes;
1506 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001507}
1508
Avi Kivity1cd196e2011-09-13 10:45:51 +03001509static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001510{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001511 int seg = ctxt->src2.val;
1512
Avi Kivity9dac77f2011-06-01 15:34:25 +03001513 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001514
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001515 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001516}
1517
Avi Kivity1cd196e2011-09-13 10:45:51 +03001518static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001519{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001520 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001521 unsigned long selector;
1522 int rc;
1523
Avi Kivity9dac77f2011-06-01 15:34:25 +03001524 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001525 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001526 return rc;
1527
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001528 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001529 return rc;
1530}
1531
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001532static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001533{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001534 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001535 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001536 int reg = VCPU_REGS_RAX;
1537
1538 while (reg <= VCPU_REGS_RDI) {
1539 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001540 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001541
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001542 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001543 if (rc != X86EMUL_CONTINUE)
1544 return rc;
1545
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001546 ++reg;
1547 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001548
Wei Yongjunc37eda12010-06-15 09:03:33 +08001549 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001550}
1551
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001552static int em_pushf(struct x86_emulate_ctxt *ctxt)
1553{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001554 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001555 return em_push(ctxt);
1556}
1557
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001558static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001559{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001560 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001561 int reg = VCPU_REGS_RDI;
1562
1563 while (reg >= VCPU_REGS_RAX) {
1564 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001565 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1566 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001567 --reg;
1568 }
1569
Avi Kivity9dac77f2011-06-01 15:34:25 +03001570 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001571 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001572 break;
1573 --reg;
1574 }
1575 return rc;
1576}
1577
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001578int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001579{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001580 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001581 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001582 struct desc_ptr dt;
1583 gva_t cs_addr;
1584 gva_t eip_addr;
1585 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001586
1587 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001588 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001589 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001590 if (rc != X86EMUL_CONTINUE)
1591 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001592
1593 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1594
Avi Kivity9dac77f2011-06-01 15:34:25 +03001595 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001596 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001597 if (rc != X86EMUL_CONTINUE)
1598 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001599
Avi Kivity9dac77f2011-06-01 15:34:25 +03001600 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001601 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001602 if (rc != X86EMUL_CONTINUE)
1603 return rc;
1604
Avi Kivity4bff1e862011-04-20 13:37:53 +03001605 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001606
1607 eip_addr = dt.address + (irq << 2);
1608 cs_addr = dt.address + (irq << 2) + 2;
1609
Avi Kivity0f65dd72011-04-20 13:37:53 +03001610 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001611 if (rc != X86EMUL_CONTINUE)
1612 return rc;
1613
Avi Kivity0f65dd72011-04-20 13:37:53 +03001614 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001615 if (rc != X86EMUL_CONTINUE)
1616 return rc;
1617
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001618 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001619 if (rc != X86EMUL_CONTINUE)
1620 return rc;
1621
Avi Kivity9dac77f2011-06-01 15:34:25 +03001622 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001623
1624 return rc;
1625}
1626
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001627static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001628{
1629 switch(ctxt->mode) {
1630 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001631 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001632 case X86EMUL_MODE_VM86:
1633 case X86EMUL_MODE_PROT16:
1634 case X86EMUL_MODE_PROT32:
1635 case X86EMUL_MODE_PROT64:
1636 default:
1637 /* Protected mode interrupts unimplemented yet */
1638 return X86EMUL_UNHANDLEABLE;
1639 }
1640}
1641
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001642static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001643{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001644 int rc = X86EMUL_CONTINUE;
1645 unsigned long temp_eip = 0;
1646 unsigned long temp_eflags = 0;
1647 unsigned long cs = 0;
1648 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1649 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1650 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1651 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1652
1653 /* TODO: Add stack limit check */
1654
Avi Kivity9dac77f2011-06-01 15:34:25 +03001655 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001656
1657 if (rc != X86EMUL_CONTINUE)
1658 return rc;
1659
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001660 if (temp_eip & ~0xffff)
1661 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001662
Avi Kivity9dac77f2011-06-01 15:34:25 +03001663 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001664
1665 if (rc != X86EMUL_CONTINUE)
1666 return rc;
1667
Avi Kivity9dac77f2011-06-01 15:34:25 +03001668 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001669
1670 if (rc != X86EMUL_CONTINUE)
1671 return rc;
1672
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001673 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001674
1675 if (rc != X86EMUL_CONTINUE)
1676 return rc;
1677
Avi Kivity9dac77f2011-06-01 15:34:25 +03001678 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001679
1680
Avi Kivity9dac77f2011-06-01 15:34:25 +03001681 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001682 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001683 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001684 ctxt->eflags &= ~0xffff;
1685 ctxt->eflags |= temp_eflags;
1686 }
1687
1688 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1689 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1690
1691 return rc;
1692}
1693
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001694static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001695{
1696 switch(ctxt->mode) {
1697 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001698 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001699 case X86EMUL_MODE_VM86:
1700 case X86EMUL_MODE_PROT16:
1701 case X86EMUL_MODE_PROT32:
1702 case X86EMUL_MODE_PROT64:
1703 default:
1704 /* iret from protected mode unimplemented yet */
1705 return X86EMUL_UNHANDLEABLE;
1706 }
1707}
1708
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001709static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1710{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001711 int rc;
1712 unsigned short sel;
1713
Avi Kivity9dac77f2011-06-01 15:34:25 +03001714 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001715
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001716 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001717 if (rc != X86EMUL_CONTINUE)
1718 return rc;
1719
Avi Kivity9dac77f2011-06-01 15:34:25 +03001720 ctxt->_eip = 0;
1721 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001722 return X86EMUL_CONTINUE;
1723}
1724
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001725static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001727 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001728 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001729 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001730 break;
1731 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001732 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001733 break;
1734 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001735 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001736 break;
1737 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001738 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739 break;
1740 case 4: /* sal/shl */
1741 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001742 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743 break;
1744 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001745 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001746 break;
1747 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001748 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001749 break;
1750 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001751 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001752}
1753
Avi Kivity3329ece2011-09-13 10:45:39 +03001754static int em_not(struct x86_emulate_ctxt *ctxt)
1755{
1756 ctxt->dst.val = ~ctxt->dst.val;
1757 return X86EMUL_CONTINUE;
1758}
1759
1760static int em_neg(struct x86_emulate_ctxt *ctxt)
1761{
1762 emulate_1op(ctxt, "neg");
1763 return X86EMUL_CONTINUE;
1764}
1765
1766static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1767{
1768 u8 ex = 0;
1769
1770 emulate_1op_rax_rdx(ctxt, "mul", ex);
1771 return X86EMUL_CONTINUE;
1772}
1773
1774static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1775{
1776 u8 ex = 0;
1777
1778 emulate_1op_rax_rdx(ctxt, "imul", ex);
1779 return X86EMUL_CONTINUE;
1780}
1781
1782static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001783{
Avi Kivity34d1f492010-08-26 11:59:01 +03001784 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001785
Avi Kivity3329ece2011-09-13 10:45:39 +03001786 emulate_1op_rax_rdx(ctxt, "div", de);
1787 if (de)
1788 return emulate_de(ctxt);
1789 return X86EMUL_CONTINUE;
1790}
1791
1792static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1793{
1794 u8 de = 0;
1795
1796 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001797 if (de)
1798 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001799 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800}
1801
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001802static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001803{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001804 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001805
Avi Kivity9dac77f2011-06-01 15:34:25 +03001806 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001807 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001808 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001809 break;
1810 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001811 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001812 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001813 case 2: /* call near abs */ {
1814 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001815 old_eip = ctxt->_eip;
1816 ctxt->_eip = ctxt->src.val;
1817 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001818 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001819 break;
1820 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001821 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001822 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001823 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001824 case 5: /* jmp far */
1825 rc = em_jmp_far(ctxt);
1826 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001827 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001828 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001829 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001830 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001831 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001832}
1833
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09001834static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001835{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001836 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001837
Avi Kivity9dac77f2011-06-01 15:34:25 +03001838 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1839 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1840 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1841 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001842 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001843 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001844 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1845 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001846
Laurent Vivier05f086f2007-09-24 11:10:55 +02001847 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001848 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001849 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001850}
1851
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001852static int em_ret(struct x86_emulate_ctxt *ctxt)
1853{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001854 ctxt->dst.type = OP_REG;
1855 ctxt->dst.addr.reg = &ctxt->_eip;
1856 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001857 return em_pop(ctxt);
1858}
1859
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001860static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001861{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001862 int rc;
1863 unsigned long cs;
1864
Avi Kivity9dac77f2011-06-01 15:34:25 +03001865 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001866 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001867 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001868 if (ctxt->op_bytes == 4)
1869 ctxt->_eip = (u32)ctxt->_eip;
1870 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001871 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001872 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001873 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001874 return rc;
1875}
1876
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09001877static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
1878{
1879 /* Save real source value, then compare EAX against destination. */
1880 ctxt->src.orig_val = ctxt->src.val;
1881 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
1882 emulate_2op_SrcV(ctxt, "cmp");
1883
1884 if (ctxt->eflags & EFLG_ZF) {
1885 /* Success: write back to memory. */
1886 ctxt->dst.val = ctxt->src.orig_val;
1887 } else {
1888 /* Failure: write the value we saw to EAX. */
1889 ctxt->dst.type = OP_REG;
1890 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
1891 }
1892 return X86EMUL_CONTINUE;
1893}
1894
Avi Kivityd4b43252011-09-13 10:45:50 +03001895static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001896{
Avi Kivityd4b43252011-09-13 10:45:50 +03001897 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001898 unsigned short sel;
1899 int rc;
1900
Avi Kivity9dac77f2011-06-01 15:34:25 +03001901 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001902
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001903 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001904 if (rc != X86EMUL_CONTINUE)
1905 return rc;
1906
Avi Kivity9dac77f2011-06-01 15:34:25 +03001907 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001908 return rc;
1909}
1910
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001911static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001912setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001913 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001914{
Avi Kivity1aa36612011-04-27 13:20:30 +03001915 u16 selector;
1916
Gleb Natapov79168fd2010-04-28 19:15:30 +03001917 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001918 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001919 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001920
1921 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001922 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001923 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001924 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001925 cs->type = 0x0b; /* Read, Execute, Accessed */
1926 cs->s = 1;
1927 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001928 cs->p = 1;
1929 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001930
Gleb Natapov79168fd2010-04-28 19:15:30 +03001931 set_desc_base(ss, 0); /* flat segment */
1932 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001933 ss->g = 1; /* 4kb granularity */
1934 ss->s = 1;
1935 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001936 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001937 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001938 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001939}
1940
Avi Kivity1a18a692012-02-01 12:23:21 +02001941static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
1942{
1943 u32 eax, ebx, ecx, edx;
1944
1945 eax = ecx = 0;
1946 return ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx)
1947 && ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
1948 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
1949 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
1950}
1951
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01001952static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
1953{
1954 struct x86_emulate_ops *ops = ctxt->ops;
1955 u32 eax, ebx, ecx, edx;
1956
1957 /*
1958 * syscall should always be enabled in longmode - so only become
1959 * vendor specific (cpuid) if other modes are active...
1960 */
1961 if (ctxt->mode == X86EMUL_MODE_PROT64)
1962 return true;
1963
1964 eax = 0x00000000;
1965 ecx = 0x00000000;
1966 if (ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx)) {
1967 /*
1968 * Intel ("GenuineIntel")
1969 * remark: Intel CPUs only support "syscall" in 64bit
1970 * longmode. Also an 64bit guest with a
1971 * 32bit compat-app running will #UD !! While this
1972 * behaviour can be fixed (by emulating) into AMD
1973 * response - CPUs of AMD can't behave like Intel.
1974 */
1975 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
1976 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
1977 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
1978 return false;
1979
1980 /* AMD ("AuthenticAMD") */
1981 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
1982 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
1983 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
1984 return true;
1985
1986 /* AMD ("AMDisbetter!") */
1987 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
1988 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
1989 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
1990 return true;
1991 }
1992
1993 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
1994 return false;
1995}
1996
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001997static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001998{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001999 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002000 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002001 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002002 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002003 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002004
2005 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002006 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002007 ctxt->mode == X86EMUL_MODE_VM86)
2008 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002009
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002010 if (!(em_syscall_is_enabled(ctxt)))
2011 return emulate_ud(ctxt);
2012
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002013 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002014 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002015
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002016 if (!(efer & EFER_SCE))
2017 return emulate_ud(ctxt);
2018
Avi Kivity717746e2011-04-20 13:37:53 +03002019 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002020 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002021 cs_sel = (u16)(msr_data & 0xfffc);
2022 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002023
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002024 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002025 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002026 cs.l = 1;
2027 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002028 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2029 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002030
Avi Kivity9dac77f2011-06-01 15:34:25 +03002031 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002032 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002033#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03002034 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002035
Avi Kivity717746e2011-04-20 13:37:53 +03002036 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002037 ctxt->mode == X86EMUL_MODE_PROT64 ?
2038 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002039 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002040
Avi Kivity717746e2011-04-20 13:37:53 +03002041 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002042 ctxt->eflags &= ~(msr_data | EFLG_RF);
2043#endif
2044 } else {
2045 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002046 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002047 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002048
2049 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2050 }
2051
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002052 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002053}
2054
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002055static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002056{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002057 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002058 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002059 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002060 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002061 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002062
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002063 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002064 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002065 if (ctxt->mode == X86EMUL_MODE_REAL)
2066 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002067
Avi Kivity1a18a692012-02-01 12:23:21 +02002068 /*
2069 * Not recognized on AMD in compat mode (but is recognized in legacy
2070 * mode).
2071 */
2072 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2073 && !vendor_intel(ctxt))
2074 return emulate_ud(ctxt);
2075
Andre Przywara8c604352009-06-18 12:56:01 +02002076 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2077 * Therefore, we inject an #UD.
2078 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002079 if (ctxt->mode == X86EMUL_MODE_PROT64)
2080 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002081
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002082 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002083
Avi Kivity717746e2011-04-20 13:37:53 +03002084 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002085 switch (ctxt->mode) {
2086 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002087 if ((msr_data & 0xfffc) == 0x0)
2088 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002089 break;
2090 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002091 if (msr_data == 0x0)
2092 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002093 break;
2094 }
2095
2096 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002097 cs_sel = (u16)msr_data;
2098 cs_sel &= ~SELECTOR_RPL_MASK;
2099 ss_sel = cs_sel + 8;
2100 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002101 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002103 cs.l = 1;
2104 }
2105
Avi Kivity1aa36612011-04-27 13:20:30 +03002106 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2107 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002108
Avi Kivity717746e2011-04-20 13:37:53 +03002109 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002111
Avi Kivity717746e2011-04-20 13:37:53 +03002112 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002113 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002114
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002115 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002116}
2117
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002118static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002119{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002120 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002121 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002122 u64 msr_data;
2123 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002124 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002125
Gleb Natapova0044752010-02-10 14:21:31 +02002126 /* inject #GP if in real mode or Virtual 8086 mode */
2127 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002128 ctxt->mode == X86EMUL_MODE_VM86)
2129 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002130
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002131 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002132
Avi Kivity9dac77f2011-06-01 15:34:25 +03002133 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002134 usermode = X86EMUL_MODE_PROT64;
2135 else
2136 usermode = X86EMUL_MODE_PROT32;
2137
2138 cs.dpl = 3;
2139 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002140 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002141 switch (usermode) {
2142 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002143 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002144 if ((msr_data & 0xfffc) == 0x0)
2145 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002146 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002147 break;
2148 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002149 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002150 if (msr_data == 0x0)
2151 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002152 ss_sel = cs_sel + 8;
2153 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002154 cs.l = 1;
2155 break;
2156 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002157 cs_sel |= SELECTOR_RPL_MASK;
2158 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002159
Avi Kivity1aa36612011-04-27 13:20:30 +03002160 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2161 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002162
Avi Kivity9dac77f2011-06-01 15:34:25 +03002163 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2164 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002165
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002166 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002167}
2168
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002169static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002170{
2171 int iopl;
2172 if (ctxt->mode == X86EMUL_MODE_REAL)
2173 return false;
2174 if (ctxt->mode == X86EMUL_MODE_VM86)
2175 return true;
2176 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002177 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002178}
2179
2180static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002181 u16 port, u16 len)
2182{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002183 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002184 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002185 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002186 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002187 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002188 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002189 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002190
Avi Kivity1aa36612011-04-27 13:20:30 +03002191 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002192 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002193 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002194 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002195 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002196 base = get_desc_base(&tr_seg);
2197#ifdef CONFIG_X86_64
2198 base |= ((u64)base3) << 32;
2199#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002200 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002201 if (r != X86EMUL_CONTINUE)
2202 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002203 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002204 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002205 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002206 if (r != X86EMUL_CONTINUE)
2207 return false;
2208 if ((perm >> bit_idx) & mask)
2209 return false;
2210 return true;
2211}
2212
2213static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002214 u16 port, u16 len)
2215{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002216 if (ctxt->perm_ok)
2217 return true;
2218
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002219 if (emulator_bad_iopl(ctxt))
2220 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002221 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002222
2223 ctxt->perm_ok = true;
2224
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002225 return true;
2226}
2227
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002228static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002229 struct tss_segment_16 *tss)
2230{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002231 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002232 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002233 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2234 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2235 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2236 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2237 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2238 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2239 tss->si = ctxt->regs[VCPU_REGS_RSI];
2240 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002241
Avi Kivity1aa36612011-04-27 13:20:30 +03002242 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2243 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2244 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2245 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2246 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002247}
2248
2249static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002250 struct tss_segment_16 *tss)
2251{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002252 int ret;
2253
Avi Kivity9dac77f2011-06-01 15:34:25 +03002254 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002255 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002256 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2257 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2258 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2259 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2260 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2261 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2262 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2263 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002264
2265 /*
2266 * SDM says that segment selectors are loaded before segment
2267 * descriptors
2268 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002269 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2270 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2271 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2272 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2273 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002274
2275 /*
2276 * Now load segment descriptors. If fault happenes at this stage
2277 * it is handled in a context of new task
2278 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002279 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002280 if (ret != X86EMUL_CONTINUE)
2281 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002282 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002283 if (ret != X86EMUL_CONTINUE)
2284 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002285 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286 if (ret != X86EMUL_CONTINUE)
2287 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002288 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002289 if (ret != X86EMUL_CONTINUE)
2290 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002291 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292 if (ret != X86EMUL_CONTINUE)
2293 return ret;
2294
2295 return X86EMUL_CONTINUE;
2296}
2297
2298static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002299 u16 tss_selector, u16 old_tss_sel,
2300 ulong old_tss_base, struct desc_struct *new_desc)
2301{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002302 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303 struct tss_segment_16 tss_seg;
2304 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002305 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002306
Avi Kivity0f65dd72011-04-20 13:37:53 +03002307 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002308 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002309 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002310 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002313 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002314
Avi Kivity0f65dd72011-04-20 13:37:53 +03002315 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002316 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002317 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002319 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002320
Avi Kivity0f65dd72011-04-20 13:37:53 +03002321 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002322 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002323 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002324 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002325 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326
2327 if (old_tss_sel != 0xffff) {
2328 tss_seg.prev_task_link = old_tss_sel;
2329
Avi Kivity0f65dd72011-04-20 13:37:53 +03002330 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002331 &tss_seg.prev_task_link,
2332 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002333 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002334 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002335 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002336 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337 }
2338
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002339 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002340}
2341
2342static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002343 struct tss_segment_32 *tss)
2344{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002345 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002346 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002347 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002348 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2349 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2350 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2351 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2352 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2353 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2354 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2355 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002356
Avi Kivity1aa36612011-04-27 13:20:30 +03002357 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2358 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2359 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2360 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2361 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2362 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2363 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002364}
2365
2366static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002367 struct tss_segment_32 *tss)
2368{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002369 int ret;
2370
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002371 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002372 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002373 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002374 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002375
2376 /* General purpose registers */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002377 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2378 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2379 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2380 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2381 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2382 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2383 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2384 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002385
2386 /*
2387 * SDM says that segment selectors are loaded before segment
2388 * descriptors
2389 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002390 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2391 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2392 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2393 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2394 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2395 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2396 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397
2398 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002399 * If we're switching between Protected Mode and VM86, we need to make
2400 * sure to update the mode before loading the segment descriptors so
2401 * that the selectors are interpreted correctly.
2402 *
2403 * Need to get rflags to the vcpu struct immediately because it
2404 * influences the CPL which is checked at least when loading the segment
2405 * descriptors and when pushing an error code to the new kernel stack.
2406 *
2407 * TODO Introduce a separate ctxt->ops->set_cpl callback
2408 */
2409 if (ctxt->eflags & X86_EFLAGS_VM)
2410 ctxt->mode = X86EMUL_MODE_VM86;
2411 else
2412 ctxt->mode = X86EMUL_MODE_PROT32;
2413
2414 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2415
2416 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002417 * Now load segment descriptors. If fault happenes at this stage
2418 * it is handled in a context of new task
2419 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002420 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421 if (ret != X86EMUL_CONTINUE)
2422 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002423 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002424 if (ret != X86EMUL_CONTINUE)
2425 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002426 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427 if (ret != X86EMUL_CONTINUE)
2428 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002429 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 if (ret != X86EMUL_CONTINUE)
2431 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002432 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002433 if (ret != X86EMUL_CONTINUE)
2434 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002435 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002436 if (ret != X86EMUL_CONTINUE)
2437 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002438 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002439 if (ret != X86EMUL_CONTINUE)
2440 return ret;
2441
2442 return X86EMUL_CONTINUE;
2443}
2444
2445static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002446 u16 tss_selector, u16 old_tss_sel,
2447 ulong old_tss_base, struct desc_struct *new_desc)
2448{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002449 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002450 struct tss_segment_32 tss_seg;
2451 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002452 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002453
Avi Kivity0f65dd72011-04-20 13:37:53 +03002454 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002455 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002456 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002458 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002459
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002460 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002461
Avi Kivity0f65dd72011-04-20 13:37:53 +03002462 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002463 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002464 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002465 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002466 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467
Avi Kivity0f65dd72011-04-20 13:37:53 +03002468 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002469 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002470 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002471 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473
2474 if (old_tss_sel != 0xffff) {
2475 tss_seg.prev_task_link = old_tss_sel;
2476
Avi Kivity0f65dd72011-04-20 13:37:53 +03002477 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002478 &tss_seg.prev_task_link,
2479 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002480 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002481 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002482 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002484 }
2485
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002486 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487}
2488
2489static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002490 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002491 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002493 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002494 struct desc_struct curr_tss_desc, next_tss_desc;
2495 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002496 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002498 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002499 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500
2501 /* FIXME: old_tss_base == ~0 ? */
2502
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002503 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504 if (ret != X86EMUL_CONTINUE)
2505 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002506 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507 if (ret != X86EMUL_CONTINUE)
2508 return ret;
2509
2510 /* FIXME: check that next_tss_desc is tss */
2511
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002512 /*
2513 * Check privileges. The three cases are task switch caused by...
2514 *
2515 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2516 * 2. Exception/IRQ/iret: No check is performed
2517 * 3. jmp/call to TSS: Check agains DPL of the TSS
2518 */
2519 if (reason == TASK_SWITCH_GATE) {
2520 if (idt_index != -1) {
2521 /* Software interrupts */
2522 struct desc_struct task_gate_desc;
2523 int dpl;
2524
2525 ret = read_interrupt_descriptor(ctxt, idt_index,
2526 &task_gate_desc);
2527 if (ret != X86EMUL_CONTINUE)
2528 return ret;
2529
2530 dpl = task_gate_desc.dpl;
2531 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2532 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2533 }
2534 } else if (reason != TASK_SWITCH_IRET) {
2535 int dpl = next_tss_desc.dpl;
2536 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2537 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002538 }
2539
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002540
Gleb Natapovceffb452010-03-18 15:20:19 +02002541 desc_limit = desc_limit_scaled(&next_tss_desc);
2542 if (!next_tss_desc.p ||
2543 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2544 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002545 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546 return X86EMUL_PROPAGATE_FAULT;
2547 }
2548
2549 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2550 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002551 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 }
2553
2554 if (reason == TASK_SWITCH_IRET)
2555 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2556
2557 /* set back link to prev task only if NT bit is set in eflags
2558 note that old_tss_sel is not used afetr this point */
2559 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2560 old_tss_sel = 0xffff;
2561
2562 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002563 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002564 old_tss_base, &next_tss_desc);
2565 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002566 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002568 if (ret != X86EMUL_CONTINUE)
2569 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570
2571 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2572 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2573
2574 if (reason != TASK_SWITCH_IRET) {
2575 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002576 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 }
2578
Avi Kivity717746e2011-04-20 13:37:53 +03002579 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002580 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581
Jan Kiszkae269fb22010-04-14 15:51:09 +02002582 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002583 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2584 ctxt->lock_prefix = 0;
2585 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002586 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002587 }
2588
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 return ret;
2590}
2591
2592int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002593 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002594 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002595{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 int rc;
2597
Avi Kivity9dac77f2011-06-01 15:34:25 +03002598 ctxt->_eip = ctxt->eip;
2599 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002601 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002602 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002604 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002605 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002607 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608}
2609
Avi Kivity90de84f2010-11-17 15:28:21 +02002610static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002611 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002612{
Gleb Natapova682e352010-03-18 15:20:21 +02002613 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2614
Avi Kivity9dac77f2011-06-01 15:34:25 +03002615 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2616 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002617 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002618}
2619
Avi Kivity7af04fc2010-08-18 14:16:35 +03002620static int em_das(struct x86_emulate_ctxt *ctxt)
2621{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002622 u8 al, old_al;
2623 bool af, cf, old_cf;
2624
2625 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002626 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002627
2628 old_al = al;
2629 old_cf = cf;
2630 cf = false;
2631 af = ctxt->eflags & X86_EFLAGS_AF;
2632 if ((al & 0x0f) > 9 || af) {
2633 al -= 6;
2634 cf = old_cf | (al >= 250);
2635 af = true;
2636 } else {
2637 af = false;
2638 }
2639 if (old_al > 0x99 || old_cf) {
2640 al -= 0x60;
2641 cf = true;
2642 }
2643
Avi Kivity9dac77f2011-06-01 15:34:25 +03002644 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002645 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002646 ctxt->src.type = OP_IMM;
2647 ctxt->src.val = 0;
2648 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002649 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002650 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2651 if (cf)
2652 ctxt->eflags |= X86_EFLAGS_CF;
2653 if (af)
2654 ctxt->eflags |= X86_EFLAGS_AF;
2655 return X86EMUL_CONTINUE;
2656}
2657
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002658static int em_call(struct x86_emulate_ctxt *ctxt)
2659{
2660 long rel = ctxt->src.val;
2661
2662 ctxt->src.val = (unsigned long)ctxt->_eip;
2663 jmp_rel(ctxt, rel);
2664 return em_push(ctxt);
2665}
2666
Avi Kivity0ef753b2010-08-18 14:51:45 +03002667static int em_call_far(struct x86_emulate_ctxt *ctxt)
2668{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002669 u16 sel, old_cs;
2670 ulong old_eip;
2671 int rc;
2672
Avi Kivity1aa36612011-04-27 13:20:30 +03002673 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002674 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002675
Avi Kivity9dac77f2011-06-01 15:34:25 +03002676 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002677 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002678 return X86EMUL_CONTINUE;
2679
Avi Kivity9dac77f2011-06-01 15:34:25 +03002680 ctxt->_eip = 0;
2681 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002682
Avi Kivity9dac77f2011-06-01 15:34:25 +03002683 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002684 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002685 if (rc != X86EMUL_CONTINUE)
2686 return rc;
2687
Avi Kivity9dac77f2011-06-01 15:34:25 +03002688 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002689 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002690}
2691
Avi Kivity40ece7c2010-08-18 15:12:09 +03002692static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2693{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002694 int rc;
2695
Avi Kivity9dac77f2011-06-01 15:34:25 +03002696 ctxt->dst.type = OP_REG;
2697 ctxt->dst.addr.reg = &ctxt->_eip;
2698 ctxt->dst.bytes = ctxt->op_bytes;
2699 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002700 if (rc != X86EMUL_CONTINUE)
2701 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002702 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002703 return X86EMUL_CONTINUE;
2704}
2705
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002706static int em_add(struct x86_emulate_ctxt *ctxt)
2707{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002708 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002709 return X86EMUL_CONTINUE;
2710}
2711
2712static int em_or(struct x86_emulate_ctxt *ctxt)
2713{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002714 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002715 return X86EMUL_CONTINUE;
2716}
2717
2718static int em_adc(struct x86_emulate_ctxt *ctxt)
2719{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002720 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002721 return X86EMUL_CONTINUE;
2722}
2723
2724static int em_sbb(struct x86_emulate_ctxt *ctxt)
2725{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002726 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002727 return X86EMUL_CONTINUE;
2728}
2729
2730static int em_and(struct x86_emulate_ctxt *ctxt)
2731{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002732 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002733 return X86EMUL_CONTINUE;
2734}
2735
2736static int em_sub(struct x86_emulate_ctxt *ctxt)
2737{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002738 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002739 return X86EMUL_CONTINUE;
2740}
2741
2742static int em_xor(struct x86_emulate_ctxt *ctxt)
2743{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002744 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002745 return X86EMUL_CONTINUE;
2746}
2747
2748static int em_cmp(struct x86_emulate_ctxt *ctxt)
2749{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002750 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002751 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002752 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002753 return X86EMUL_CONTINUE;
2754}
2755
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002756static int em_test(struct x86_emulate_ctxt *ctxt)
2757{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002758 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002759 /* Disable writeback. */
2760 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002761 return X86EMUL_CONTINUE;
2762}
2763
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002764static int em_xchg(struct x86_emulate_ctxt *ctxt)
2765{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002766 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002767 ctxt->src.val = ctxt->dst.val;
2768 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002769
2770 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002771 ctxt->dst.val = ctxt->src.orig_val;
2772 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002773 return X86EMUL_CONTINUE;
2774}
2775
Avi Kivity5c82aa22010-08-18 18:31:43 +03002776static int em_imul(struct x86_emulate_ctxt *ctxt)
2777{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002778 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002779 return X86EMUL_CONTINUE;
2780}
2781
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002782static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2783{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002784 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002785 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002786}
2787
Avi Kivity61429142010-08-19 15:13:00 +03002788static int em_cwd(struct x86_emulate_ctxt *ctxt)
2789{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002790 ctxt->dst.type = OP_REG;
2791 ctxt->dst.bytes = ctxt->src.bytes;
2792 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2793 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002794
2795 return X86EMUL_CONTINUE;
2796}
2797
Avi Kivity48bb5d32010-08-18 18:54:34 +03002798static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2799{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002800 u64 tsc = 0;
2801
Avi Kivity717746e2011-04-20 13:37:53 +03002802 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002803 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2804 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002805 return X86EMUL_CONTINUE;
2806}
2807
Avi Kivity222d21a2011-11-10 14:57:30 +02002808static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2809{
2810 u64 pmc;
2811
2812 if (ctxt->ops->read_pmc(ctxt, ctxt->regs[VCPU_REGS_RCX], &pmc))
2813 return emulate_gp(ctxt, 0);
2814 ctxt->regs[VCPU_REGS_RAX] = (u32)pmc;
2815 ctxt->regs[VCPU_REGS_RDX] = pmc >> 32;
2816 return X86EMUL_CONTINUE;
2817}
2818
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002819static int em_mov(struct x86_emulate_ctxt *ctxt)
2820{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03002821 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002822 return X86EMUL_CONTINUE;
2823}
2824
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002825static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2826{
2827 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2828 return emulate_gp(ctxt, 0);
2829
2830 /* Disable writeback. */
2831 ctxt->dst.type = OP_NONE;
2832 return X86EMUL_CONTINUE;
2833}
2834
2835static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2836{
2837 unsigned long val;
2838
2839 if (ctxt->mode == X86EMUL_MODE_PROT64)
2840 val = ctxt->src.val & ~0ULL;
2841 else
2842 val = ctxt->src.val & ~0U;
2843
2844 /* #UD condition is already handled. */
2845 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2846 return emulate_gp(ctxt, 0);
2847
2848 /* Disable writeback. */
2849 ctxt->dst.type = OP_NONE;
2850 return X86EMUL_CONTINUE;
2851}
2852
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002853static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2854{
2855 u64 msr_data;
2856
2857 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2858 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2859 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2860 return emulate_gp(ctxt, 0);
2861
2862 return X86EMUL_CONTINUE;
2863}
2864
2865static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2866{
2867 u64 msr_data;
2868
2869 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2870 return emulate_gp(ctxt, 0);
2871
2872 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2873 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2874 return X86EMUL_CONTINUE;
2875}
2876
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002877static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2878{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002879 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002880 return emulate_ud(ctxt);
2881
Avi Kivity9dac77f2011-06-01 15:34:25 +03002882 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002883 return X86EMUL_CONTINUE;
2884}
2885
2886static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2887{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002888 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002889
Avi Kivity9dac77f2011-06-01 15:34:25 +03002890 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002891 return emulate_ud(ctxt);
2892
Avi Kivity9dac77f2011-06-01 15:34:25 +03002893 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002894 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2895
2896 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002897 ctxt->dst.type = OP_NONE;
2898 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002899}
2900
Avi Kivity38503912011-03-31 18:48:09 +02002901static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2902{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002903 int rc;
2904 ulong linear;
2905
Avi Kivity9dac77f2011-06-01 15:34:25 +03002906 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002907 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002908 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002909 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002910 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002911 return X86EMUL_CONTINUE;
2912}
2913
Avi Kivity2d04a052011-04-20 15:32:49 +03002914static int em_clts(struct x86_emulate_ctxt *ctxt)
2915{
2916 ulong cr0;
2917
2918 cr0 = ctxt->ops->get_cr(ctxt, 0);
2919 cr0 &= ~X86_CR0_TS;
2920 ctxt->ops->set_cr(ctxt, 0, cr0);
2921 return X86EMUL_CONTINUE;
2922}
2923
Avi Kivity26d05cc2011-04-21 12:07:59 +03002924static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2925{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002926 int rc;
2927
Avi Kivity9dac77f2011-06-01 15:34:25 +03002928 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002929 return X86EMUL_UNHANDLEABLE;
2930
2931 rc = ctxt->ops->fix_hypercall(ctxt);
2932 if (rc != X86EMUL_CONTINUE)
2933 return rc;
2934
2935 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002936 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002937 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002938 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002939 return X86EMUL_CONTINUE;
2940}
2941
2942static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2943{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002944 struct desc_ptr desc_ptr;
2945 int rc;
2946
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002948 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002949 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002950 if (rc != X86EMUL_CONTINUE)
2951 return rc;
2952 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2953 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002954 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002955 return X86EMUL_CONTINUE;
2956}
2957
Avi Kivity5ef39c72011-04-21 12:21:50 +03002958static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002959{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002960 int rc;
2961
Avi Kivity5ef39c72011-04-21 12:21:50 +03002962 rc = ctxt->ops->fix_hypercall(ctxt);
2963
Avi Kivity26d05cc2011-04-21 12:07:59 +03002964 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002965 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002966 return rc;
2967}
2968
2969static int em_lidt(struct x86_emulate_ctxt *ctxt)
2970{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002971 struct desc_ptr desc_ptr;
2972 int rc;
2973
Avi Kivity9dac77f2011-06-01 15:34:25 +03002974 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002975 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002976 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002977 if (rc != X86EMUL_CONTINUE)
2978 return rc;
2979 ctxt->ops->set_idt(ctxt, &desc_ptr);
2980 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002981 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002982 return X86EMUL_CONTINUE;
2983}
2984
2985static int em_smsw(struct x86_emulate_ctxt *ctxt)
2986{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002987 ctxt->dst.bytes = 2;
2988 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002989 return X86EMUL_CONTINUE;
2990}
2991
2992static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2993{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002994 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002995 | (ctxt->src.val & 0x0f));
2996 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002997 return X86EMUL_CONTINUE;
2998}
2999
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003000static int em_loop(struct x86_emulate_ctxt *ctxt)
3001{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003002 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
3003 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
3004 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3005 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003006
3007 return X86EMUL_CONTINUE;
3008}
3009
3010static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3011{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003012 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
3013 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003014
3015 return X86EMUL_CONTINUE;
3016}
3017
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003018static int em_in(struct x86_emulate_ctxt *ctxt)
3019{
3020 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3021 &ctxt->dst.val))
3022 return X86EMUL_IO_NEEDED;
3023
3024 return X86EMUL_CONTINUE;
3025}
3026
3027static int em_out(struct x86_emulate_ctxt *ctxt)
3028{
3029 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3030 &ctxt->src.val, 1);
3031 /* Disable writeback. */
3032 ctxt->dst.type = OP_NONE;
3033 return X86EMUL_CONTINUE;
3034}
3035
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003036static int em_cli(struct x86_emulate_ctxt *ctxt)
3037{
3038 if (emulator_bad_iopl(ctxt))
3039 return emulate_gp(ctxt, 0);
3040
3041 ctxt->eflags &= ~X86_EFLAGS_IF;
3042 return X86EMUL_CONTINUE;
3043}
3044
3045static int em_sti(struct x86_emulate_ctxt *ctxt)
3046{
3047 if (emulator_bad_iopl(ctxt))
3048 return emulate_gp(ctxt, 0);
3049
3050 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3051 ctxt->eflags |= X86_EFLAGS_IF;
3052 return X86EMUL_CONTINUE;
3053}
3054
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003055static int em_bt(struct x86_emulate_ctxt *ctxt)
3056{
3057 /* Disable writeback. */
3058 ctxt->dst.type = OP_NONE;
3059 /* only subword offset */
3060 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3061
3062 emulate_2op_SrcV_nobyte(ctxt, "bt");
3063 return X86EMUL_CONTINUE;
3064}
3065
3066static int em_bts(struct x86_emulate_ctxt *ctxt)
3067{
3068 emulate_2op_SrcV_nobyte(ctxt, "bts");
3069 return X86EMUL_CONTINUE;
3070}
3071
3072static int em_btr(struct x86_emulate_ctxt *ctxt)
3073{
3074 emulate_2op_SrcV_nobyte(ctxt, "btr");
3075 return X86EMUL_CONTINUE;
3076}
3077
3078static int em_btc(struct x86_emulate_ctxt *ctxt)
3079{
3080 emulate_2op_SrcV_nobyte(ctxt, "btc");
3081 return X86EMUL_CONTINUE;
3082}
3083
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003084static int em_bsf(struct x86_emulate_ctxt *ctxt)
3085{
3086 u8 zf;
3087
3088 __asm__ ("bsf %2, %0; setz %1"
3089 : "=r"(ctxt->dst.val), "=q"(zf)
3090 : "r"(ctxt->src.val));
3091
3092 ctxt->eflags &= ~X86_EFLAGS_ZF;
3093 if (zf) {
3094 ctxt->eflags |= X86_EFLAGS_ZF;
3095 /* Disable writeback. */
3096 ctxt->dst.type = OP_NONE;
3097 }
3098 return X86EMUL_CONTINUE;
3099}
3100
3101static int em_bsr(struct x86_emulate_ctxt *ctxt)
3102{
3103 u8 zf;
3104
3105 __asm__ ("bsr %2, %0; setz %1"
3106 : "=r"(ctxt->dst.val), "=q"(zf)
3107 : "r"(ctxt->src.val));
3108
3109 ctxt->eflags &= ~X86_EFLAGS_ZF;
3110 if (zf) {
3111 ctxt->eflags |= X86_EFLAGS_ZF;
3112 /* Disable writeback. */
3113 ctxt->dst.type = OP_NONE;
3114 }
3115 return X86EMUL_CONTINUE;
3116}
3117
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003118static bool valid_cr(int nr)
3119{
3120 switch (nr) {
3121 case 0:
3122 case 2 ... 4:
3123 case 8:
3124 return true;
3125 default:
3126 return false;
3127 }
3128}
3129
3130static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3131{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003132 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003133 return emulate_ud(ctxt);
3134
3135 return X86EMUL_CONTINUE;
3136}
3137
3138static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3139{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003140 u64 new_val = ctxt->src.val64;
3141 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003142 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003143
3144 static u64 cr_reserved_bits[] = {
3145 0xffffffff00000000ULL,
3146 0, 0, 0, /* CR3 checked later */
3147 CR4_RESERVED_BITS,
3148 0, 0, 0,
3149 CR8_RESERVED_BITS,
3150 };
3151
3152 if (!valid_cr(cr))
3153 return emulate_ud(ctxt);
3154
3155 if (new_val & cr_reserved_bits[cr])
3156 return emulate_gp(ctxt, 0);
3157
3158 switch (cr) {
3159 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003160 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003161 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3162 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3163 return emulate_gp(ctxt, 0);
3164
Avi Kivity717746e2011-04-20 13:37:53 +03003165 cr4 = ctxt->ops->get_cr(ctxt, 4);
3166 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003167
3168 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3169 !(cr4 & X86_CR4_PAE))
3170 return emulate_gp(ctxt, 0);
3171
3172 break;
3173 }
3174 case 3: {
3175 u64 rsvd = 0;
3176
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003177 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3178 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003179 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003180 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003181 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003182 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003183 rsvd = CR3_NONPAE_RESERVED_BITS;
3184
3185 if (new_val & rsvd)
3186 return emulate_gp(ctxt, 0);
3187
3188 break;
3189 }
3190 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003191 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003192
3193 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3194 return emulate_gp(ctxt, 0);
3195
3196 break;
3197 }
3198 }
3199
3200 return X86EMUL_CONTINUE;
3201}
3202
Joerg Roedel3b88e412011-04-04 12:39:29 +02003203static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3204{
3205 unsigned long dr7;
3206
Avi Kivity717746e2011-04-20 13:37:53 +03003207 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003208
3209 /* Check if DR7.Global_Enable is set */
3210 return dr7 & (1 << 13);
3211}
3212
3213static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3214{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003215 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003216 u64 cr4;
3217
3218 if (dr > 7)
3219 return emulate_ud(ctxt);
3220
Avi Kivity717746e2011-04-20 13:37:53 +03003221 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003222 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3223 return emulate_ud(ctxt);
3224
3225 if (check_dr7_gd(ctxt))
3226 return emulate_db(ctxt);
3227
3228 return X86EMUL_CONTINUE;
3229}
3230
3231static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3232{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003233 u64 new_val = ctxt->src.val64;
3234 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003235
3236 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3237 return emulate_gp(ctxt, 0);
3238
3239 return check_dr_read(ctxt);
3240}
3241
Joerg Roedel01de8b02011-04-04 12:39:31 +02003242static int check_svme(struct x86_emulate_ctxt *ctxt)
3243{
3244 u64 efer;
3245
Avi Kivity717746e2011-04-20 13:37:53 +03003246 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003247
3248 if (!(efer & EFER_SVME))
3249 return emulate_ud(ctxt);
3250
3251 return X86EMUL_CONTINUE;
3252}
3253
3254static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3255{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003256 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003257
3258 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003259 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003260 return emulate_gp(ctxt, 0);
3261
3262 return check_svme(ctxt);
3263}
3264
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003265static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3266{
Avi Kivity717746e2011-04-20 13:37:53 +03003267 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003268
Avi Kivity717746e2011-04-20 13:37:53 +03003269 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003270 return emulate_ud(ctxt);
3271
3272 return X86EMUL_CONTINUE;
3273}
3274
Joerg Roedel80612522011-04-04 12:39:33 +02003275static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3276{
Avi Kivity717746e2011-04-20 13:37:53 +03003277 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003278 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003279
Avi Kivity717746e2011-04-20 13:37:53 +03003280 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003281 (rcx > 3))
3282 return emulate_gp(ctxt, 0);
3283
3284 return X86EMUL_CONTINUE;
3285}
3286
Joerg Roedelf6511932011-04-04 12:39:35 +02003287static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3288{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003289 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3290 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003291 return emulate_gp(ctxt, 0);
3292
3293 return X86EMUL_CONTINUE;
3294}
3295
3296static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3297{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003298 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3299 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003300 return emulate_gp(ctxt, 0);
3301
3302 return X86EMUL_CONTINUE;
3303}
3304
Avi Kivity73fba5f2010-07-29 15:11:53 +03003305#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003306#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003307#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3308 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003309#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003310#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003311#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03003312#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003313#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003314#define II(_f, _e, _i) \
3315 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003316#define IIP(_f, _e, _i, _p) \
3317 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3318 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003319#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003320
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003321#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003322#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003323#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003324#define I2bvIP(_f, _e, _i, _p) \
3325 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003326
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003327#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3328 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3329 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003330
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003331static struct opcode group7_rm1[] = {
3332 DI(SrcNone | ModRM | Priv, monitor),
3333 DI(SrcNone | ModRM | Priv, mwait),
3334 N, N, N, N, N, N,
3335};
3336
Joerg Roedel01de8b02011-04-04 12:39:31 +02003337static struct opcode group7_rm3[] = {
3338 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003339 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003340 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3341 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3342 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3343 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3344 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3345 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3346};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003347
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003348static struct opcode group7_rm7[] = {
3349 N,
3350 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3351 N, N, N, N, N, N,
3352};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003353
Avi Kivity73fba5f2010-07-29 15:11:53 +03003354static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003355 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003356 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003357 I(Lock, em_adc),
3358 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003359 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003360 I(Lock, em_sub),
3361 I(Lock, em_xor),
3362 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003363};
3364
3365static struct opcode group1A[] = {
Takuya Yoshikawac15af352011-12-06 18:06:02 +09003366 I(DstMem | SrcNone | ModRM | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003367};
3368
3369static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003370 I(DstMem | SrcImm | ModRM, em_test),
3371 I(DstMem | SrcImm | ModRM, em_test),
3372 I(DstMem | SrcNone | ModRM | Lock, em_not),
3373 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3374 I(SrcMem | ModRM, em_mul_ex),
3375 I(SrcMem | ModRM, em_imul_ex),
3376 I(SrcMem | ModRM, em_div_ex),
3377 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003378};
3379
3380static struct opcode group4[] = {
Takuya Yoshikawac04ec832011-12-06 18:06:44 +09003381 I(ByteOp | DstMem | SrcNone | ModRM | Lock, em_grp45),
3382 I(ByteOp | DstMem | SrcNone | ModRM | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003383 N, N, N, N, N, N,
3384};
3385
3386static struct opcode group5[] = {
Takuya Yoshikawac04ec832011-12-06 18:06:44 +09003387 I(DstMem | SrcNone | ModRM | Lock, em_grp45),
3388 I(DstMem | SrcNone | ModRM | Lock, em_grp45),
3389 I(SrcMem | ModRM | Stack, em_grp45),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003390 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Takuya Yoshikawac04ec832011-12-06 18:06:44 +09003391 I(SrcMem | ModRM | Stack, em_grp45),
3392 I(SrcMemFAddr | ModRM | ImplicitOps, em_grp45),
3393 I(SrcMem | ModRM | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003394};
3395
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003396static struct opcode group6[] = {
3397 DI(ModRM | Prot, sldt),
3398 DI(ModRM | Prot, str),
3399 DI(ModRM | Prot | Priv, lldt),
3400 DI(ModRM | Prot | Priv, ltr),
3401 N, N, N, N,
3402};
3403
Avi Kivity73fba5f2010-07-29 15:11:53 +03003404static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003405 DI(ModRM | Mov | DstMem | Priv, sgdt),
3406 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003407 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3408 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3409 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3410 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3411 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003412}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003413 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3414 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003415 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003416 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3417 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003418} };
3419
3420static struct opcode group8[] = {
3421 N, N, N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003422 I(DstMem | SrcImmByte | ModRM, em_bt),
3423 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_bts),
3424 I(DstMem | SrcImmByte | ModRM | Lock, em_btr),
3425 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003426};
3427
3428static struct group_dual group9 = { {
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09003429 N, I(DstMem64 | ModRM | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003430}, {
3431 N, N, N, N, N, N, N, N,
3432} };
3433
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003434static struct opcode group11[] = {
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003435 I(DstMem | SrcImm | ModRM | Mov | PageTable, em_mov),
3436 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003437};
3438
Avi Kivityaa97bb42010-01-20 18:09:23 +02003439static struct gprefix pfx_0f_6f_0f_7f = {
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003440 N, I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003441};
3442
Avi Kivity3e114eb2012-04-09 18:40:01 +03003443static struct gprefix pfx_vmovntpx = {
3444 I(0, em_mov), N, N, N,
3445};
3446
Avi Kivity73fba5f2010-07-29 15:11:53 +03003447static struct opcode opcode_table[256] = {
3448 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003449 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003450 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3451 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003452 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003453 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003454 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3455 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003456 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003457 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003458 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3459 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003460 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003461 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003462 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3463 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003464 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003465 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003466 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003467 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003468 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003469 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003470 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003471 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003472 /* 0x40 - 0x4F */
3473 X16(D(DstReg)),
3474 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003475 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003476 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003477 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003478 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003479 I(ImplicitOps | Stack | No64, em_pusha),
3480 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003481 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3482 N, N, N, N,
3483 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003484 I(SrcImm | Mov | Stack, em_push),
3485 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003486 I(SrcImmByte | Mov | Stack, em_push),
3487 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003488 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3489 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003490 /* 0x70 - 0x7F */
3491 X16(D(SrcImmByte)),
3492 /* 0x80 - 0x87 */
3493 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3494 G(DstMem | SrcImm | ModRM | Group, group1),
3495 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3496 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003497 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003498 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003499 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003500 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003501 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003502 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003503 D(ModRM | SrcMem | NoAccess | DstReg),
3504 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3505 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003506 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003507 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003508 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003509 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003510 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003511 II(ImplicitOps | Stack, em_pushf, pushf),
3512 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003513 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003514 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003515 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003516 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003517 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003518 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003519 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003520 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3521 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003522 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003523 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003524 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003525 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003526 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003527 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003528 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003529 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003530 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003531 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3532 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003533 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003534 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003535 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003536 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003537 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003538 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003539 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003540 N, N, N, N,
3541 /* 0xD8 - 0xDF */
3542 N, N, N, N, N, N, N, N,
3543 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003544 X3(I(SrcImmByte, em_loop)),
3545 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003546 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3547 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003548 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003549 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003550 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003551 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3552 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003553 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003554 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003555 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3556 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003557 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003558 D(ImplicitOps), D(ImplicitOps),
3559 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003560 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3561};
3562
3563static struct opcode twobyte_table[256] = {
3564 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003565 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003566 N, I(ImplicitOps | VendorSpecific, em_syscall),
3567 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003568 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003569 N, D(ImplicitOps | ModRM), N, N,
3570 /* 0x10 - 0x1F */
3571 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3572 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003573 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003574 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003575 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3576 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003577 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003578 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3579 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003580 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003581 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003582 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003583 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003584 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003585 I(ImplicitOps | VendorSpecific, em_sysenter),
3586 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003587 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003588 N, N, N, N, N, N, N, N,
3589 /* 0x40 - 0x4F */
3590 X16(D(DstReg | SrcMem | ModRM | Mov)),
3591 /* 0x50 - 0x5F */
3592 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3593 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003594 N, N, N, N,
3595 N, N, N, N,
3596 N, N, N, N,
3597 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003598 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003599 N, N, N, N,
3600 N, N, N, N,
3601 N, N, N, N,
3602 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003603 /* 0x80 - 0x8F */
3604 X16(D(SrcImm)),
3605 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003606 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003607 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003608 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003609 DI(ImplicitOps, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003610 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3611 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3612 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003613 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003614 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003615 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003616 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3617 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003618 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003619 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003620 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003621 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003622 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003623 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3624 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003625 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003626 /* 0xB8 - 0xBF */
3627 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003628 G(BitOp, group8),
3629 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003630 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003631 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003632 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003633 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003634 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003635 N, N, N, GD(0, &group9),
3636 N, N, N, N, N, N, N, N,
3637 /* 0xD0 - 0xDF */
3638 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3639 /* 0xE0 - 0xEF */
3640 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3641 /* 0xF0 - 0xFF */
3642 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3643};
3644
3645#undef D
3646#undef N
3647#undef G
3648#undef GD
3649#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003650#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003651#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003652
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003653#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003654#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003655#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003656#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003657#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003658
Avi Kivity9dac77f2011-06-01 15:34:25 +03003659static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003660{
3661 unsigned size;
3662
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003664 if (size == 8)
3665 size = 4;
3666 return size;
3667}
3668
3669static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3670 unsigned size, bool sign_extension)
3671{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003672 int rc = X86EMUL_CONTINUE;
3673
3674 op->type = OP_IMM;
3675 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003676 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003677 /* NB. Immediates are sign-extended as necessary. */
3678 switch (op->bytes) {
3679 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003680 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003681 break;
3682 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003683 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003684 break;
3685 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003686 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003687 break;
3688 }
3689 if (!sign_extension) {
3690 switch (op->bytes) {
3691 case 1:
3692 op->val &= 0xff;
3693 break;
3694 case 2:
3695 op->val &= 0xffff;
3696 break;
3697 case 4:
3698 op->val &= 0xffffffff;
3699 break;
3700 }
3701 }
3702done:
3703 return rc;
3704}
3705
Avi Kivitya9945542011-09-13 10:45:41 +03003706static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3707 unsigned d)
3708{
3709 int rc = X86EMUL_CONTINUE;
3710
3711 switch (d) {
3712 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003713 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03003714 break;
3715 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003716 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003717 break;
3718 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003719 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003720 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003721 *op = ctxt->memop;
3722 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003723 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003724 fetch_bit_operand(ctxt);
3725 op->orig_val = op->val;
3726 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003727 case OpMem64:
3728 ctxt->memop.bytes = 8;
3729 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003730 case OpAcc:
3731 op->type = OP_REG;
3732 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3733 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3734 fetch_register_operand(op);
3735 op->orig_val = op->val;
3736 break;
3737 case OpDI:
3738 op->type = OP_MEM;
3739 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3740 op->addr.mem.ea =
3741 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3742 op->addr.mem.seg = VCPU_SREG_ES;
3743 op->val = 0;
3744 break;
3745 case OpDX:
3746 op->type = OP_REG;
3747 op->bytes = 2;
3748 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3749 fetch_register_operand(op);
3750 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003751 case OpCL:
3752 op->bytes = 1;
3753 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3754 break;
3755 case OpImmByte:
3756 rc = decode_imm(ctxt, op, 1, true);
3757 break;
3758 case OpOne:
3759 op->bytes = 1;
3760 op->val = 1;
3761 break;
3762 case OpImm:
3763 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3764 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02003765 case OpMem8:
3766 ctxt->memop.bytes = 1;
3767 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03003768 case OpMem16:
3769 ctxt->memop.bytes = 2;
3770 goto mem_common;
3771 case OpMem32:
3772 ctxt->memop.bytes = 4;
3773 goto mem_common;
3774 case OpImmU16:
3775 rc = decode_imm(ctxt, op, 2, false);
3776 break;
3777 case OpImmU:
3778 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3779 break;
3780 case OpSI:
3781 op->type = OP_MEM;
3782 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3783 op->addr.mem.ea =
3784 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3785 op->addr.mem.seg = seg_override(ctxt);
3786 op->val = 0;
3787 break;
3788 case OpImmFAddr:
3789 op->type = OP_IMM;
3790 op->addr.mem.ea = ctxt->_eip;
3791 op->bytes = ctxt->op_bytes + 2;
3792 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3793 break;
3794 case OpMemFAddr:
3795 ctxt->memop.bytes = ctxt->op_bytes + 2;
3796 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003797 case OpES:
3798 op->val = VCPU_SREG_ES;
3799 break;
3800 case OpCS:
3801 op->val = VCPU_SREG_CS;
3802 break;
3803 case OpSS:
3804 op->val = VCPU_SREG_SS;
3805 break;
3806 case OpDS:
3807 op->val = VCPU_SREG_DS;
3808 break;
3809 case OpFS:
3810 op->val = VCPU_SREG_FS;
3811 break;
3812 case OpGS:
3813 op->val = VCPU_SREG_GS;
3814 break;
Avi Kivitya9945542011-09-13 10:45:41 +03003815 case OpImplicit:
3816 /* Special instructions do their own operand decoding. */
3817 default:
3818 op->type = OP_NONE; /* Disable writeback. */
3819 break;
3820 }
3821
3822done:
3823 return rc;
3824}
3825
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003826int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003827{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003828 int rc = X86EMUL_CONTINUE;
3829 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003830 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003831 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003832 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003833
Avi Kivityf09ed832011-09-13 10:45:40 +03003834 ctxt->memop.type = OP_NONE;
3835 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003836 ctxt->_eip = ctxt->eip;
3837 ctxt->fetch.start = ctxt->_eip;
3838 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003839 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003840 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003841
3842 switch (mode) {
3843 case X86EMUL_MODE_REAL:
3844 case X86EMUL_MODE_VM86:
3845 case X86EMUL_MODE_PROT16:
3846 def_op_bytes = def_ad_bytes = 2;
3847 break;
3848 case X86EMUL_MODE_PROT32:
3849 def_op_bytes = def_ad_bytes = 4;
3850 break;
3851#ifdef CONFIG_X86_64
3852 case X86EMUL_MODE_PROT64:
3853 def_op_bytes = 4;
3854 def_ad_bytes = 8;
3855 break;
3856#endif
3857 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003858 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003859 }
3860
Avi Kivity9dac77f2011-06-01 15:34:25 +03003861 ctxt->op_bytes = def_op_bytes;
3862 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003863
3864 /* Legacy prefixes. */
3865 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003866 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003867 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003868 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003869 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003870 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003871 break;
3872 case 0x67: /* address-size override */
3873 if (mode == X86EMUL_MODE_PROT64)
3874 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003875 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003876 else
3877 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003878 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003879 break;
3880 case 0x26: /* ES override */
3881 case 0x2e: /* CS override */
3882 case 0x36: /* SS override */
3883 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003884 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003885 break;
3886 case 0x64: /* FS override */
3887 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003888 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003889 break;
3890 case 0x40 ... 0x4f: /* REX */
3891 if (mode != X86EMUL_MODE_PROT64)
3892 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003893 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003894 continue;
3895 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003896 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003897 break;
3898 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003899 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003900 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003901 break;
3902 default:
3903 goto done_prefixes;
3904 }
3905
3906 /* Any legacy prefix after a REX prefix nullifies its effect. */
3907
Avi Kivity9dac77f2011-06-01 15:34:25 +03003908 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003909 }
3910
3911done_prefixes:
3912
3913 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003914 if (ctxt->rex_prefix & 8)
3915 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003916
3917 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003918 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003919 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003920 if (ctxt->b == 0x0f) {
3921 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003922 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003923 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003924 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003925 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003926
Avi Kivity9dac77f2011-06-01 15:34:25 +03003927 while (ctxt->d & GroupMask) {
3928 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003929 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003930 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003931 --ctxt->_eip;
3932 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003933 opcode = opcode.u.group[goffset];
3934 break;
3935 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003936 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003937 --ctxt->_eip;
3938 goffset = (ctxt->modrm >> 3) & 7;
3939 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003940 opcode = opcode.u.gdual->mod3[goffset];
3941 else
3942 opcode = opcode.u.gdual->mod012[goffset];
3943 break;
3944 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003945 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003946 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003947 break;
3948 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003949 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003950 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003951 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003952 switch (simd_prefix) {
3953 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3954 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3955 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3956 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3957 }
3958 break;
3959 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003960 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003961 }
Avi Kivity46561642011-04-24 14:09:59 +03003962
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003963 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003964 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003965 }
3966
Avi Kivity9dac77f2011-06-01 15:34:25 +03003967 ctxt->execute = opcode.u.execute;
3968 ctxt->check_perm = opcode.check_perm;
3969 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003970
3971 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003972 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003973 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003974
Avi Kivity9dac77f2011-06-01 15:34:25 +03003975 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003976 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003977
Avi Kivity9dac77f2011-06-01 15:34:25 +03003978 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3979 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003980
Avi Kivity9dac77f2011-06-01 15:34:25 +03003981 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003982 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003983 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003984 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003985 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003986 }
3987
Avi Kivity9dac77f2011-06-01 15:34:25 +03003988 if (ctxt->d & Sse)
3989 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003990
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003991 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003992 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003993 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003994 if (!ctxt->has_seg_override)
3995 set_seg_override(ctxt, ctxt->modrm_seg);
3996 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003997 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003998 if (rc != X86EMUL_CONTINUE)
3999 goto done;
4000
Avi Kivity9dac77f2011-06-01 15:34:25 +03004001 if (!ctxt->has_seg_override)
4002 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004003
Avi Kivityf09ed832011-09-13 10:45:40 +03004004 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004005
Avi Kivityf09ed832011-09-13 10:45:40 +03004006 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4007 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004008
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004009 /*
4010 * Decode and fetch the source operand: register, memory
4011 * or immediate.
4012 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004013 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004014 if (rc != X86EMUL_CONTINUE)
4015 goto done;
4016
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004017 /*
4018 * Decode and fetch the second source operand: register, memory
4019 * or immediate.
4020 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004021 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004022 if (rc != X86EMUL_CONTINUE)
4023 goto done;
4024
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004025 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004026 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004027
4028done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004029 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4030 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004031
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004032 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004033}
4034
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004035bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4036{
4037 return ctxt->d & PageTable;
4038}
4039
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004040static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4041{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004042 /* The second termination condition only applies for REPE
4043 * and REPNE. Test if the repeat string operation prefix is
4044 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4045 * corresponding termination condition according to:
4046 * - if REPE/REPZ and ZF = 0 then done
4047 * - if REPNE/REPNZ and ZF = 1 then done
4048 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004049 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4050 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4051 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004052 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004053 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004054 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4055 return true;
4056
4057 return false;
4058}
4059
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004060int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004061{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004062 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004063 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004064 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004065
Avi Kivity9dac77f2011-06-01 15:34:25 +03004066 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004067
Avi Kivity9dac77f2011-06-01 15:34:25 +03004068 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004069 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004070 goto done;
4071 }
4072
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004073 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004074 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004075 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004076 goto done;
4077 }
4078
Avi Kivity9dac77f2011-06-01 15:34:25 +03004079 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004080 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004081 goto done;
4082 }
4083
Avi Kivity9dac77f2011-06-01 15:34:25 +03004084 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03004085 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
4086 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004087 rc = emulate_ud(ctxt);
4088 goto done;
4089 }
4090
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004092 rc = emulate_nm(ctxt);
4093 goto done;
4094 }
4095
Avi Kivity9dac77f2011-06-01 15:34:25 +03004096 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4097 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004098 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004099 if (rc != X86EMUL_CONTINUE)
4100 goto done;
4101 }
4102
Gleb Natapove92805a2010-02-10 14:21:35 +02004103 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004104 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004105 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004106 goto done;
4107 }
4108
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004109 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004110 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004111 rc = emulate_ud(ctxt);
4112 goto done;
4113 }
4114
Joerg Roedeld09beab2011-04-04 12:39:25 +02004115 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004116 if (ctxt->check_perm) {
4117 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004118 if (rc != X86EMUL_CONTINUE)
4119 goto done;
4120 }
4121
Avi Kivity9dac77f2011-06-01 15:34:25 +03004122 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4123 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004124 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004125 if (rc != X86EMUL_CONTINUE)
4126 goto done;
4127 }
4128
Avi Kivity9dac77f2011-06-01 15:34:25 +03004129 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004130 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004131 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
4132 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004133 goto done;
4134 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004135 }
4136
Avi Kivity9dac77f2011-06-01 15:34:25 +03004137 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4138 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4139 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004140 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004141 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004142 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004143 }
4144
Avi Kivity9dac77f2011-06-01 15:34:25 +03004145 if (ctxt->src2.type == OP_MEM) {
4146 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4147 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004148 if (rc != X86EMUL_CONTINUE)
4149 goto done;
4150 }
4151
Avi Kivity9dac77f2011-06-01 15:34:25 +03004152 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004153 goto special_insn;
4154
4155
Avi Kivity9dac77f2011-06-01 15:34:25 +03004156 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004157 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004158 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4159 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004160 if (rc != X86EMUL_CONTINUE)
4161 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004162 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004163 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004164
Avi Kivity018a98d2007-11-27 19:30:56 +02004165special_insn:
4166
Avi Kivity9dac77f2011-06-01 15:34:25 +03004167 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4168 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004169 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004170 if (rc != X86EMUL_CONTINUE)
4171 goto done;
4172 }
4173
Avi Kivity9dac77f2011-06-01 15:34:25 +03004174 if (ctxt->execute) {
4175 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004176 if (rc != X86EMUL_CONTINUE)
4177 goto done;
4178 goto writeback;
4179 }
4180
Avi Kivity9dac77f2011-06-01 15:34:25 +03004181 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182 goto twobyte_insn;
4183
Avi Kivity9dac77f2011-06-01 15:34:25 +03004184 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004185 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004186 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004187 break;
4188 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004189 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004190 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004191 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004192 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004194 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004196 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004197 if (test_cc(ctxt->b, ctxt->eflags))
4198 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004199 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004200 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004201 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004202 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004203 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004204 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004205 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004206 rc = em_xchg(ctxt);
4207 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004208 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004209 switch (ctxt->op_bytes) {
4210 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4211 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4212 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004213 }
4214 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004215 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004216 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004217 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004218 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004219 rc = emulate_int(ctxt, 3);
4220 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004221 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004222 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004223 break;
4224 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004225 if (ctxt->eflags & EFLG_OF)
4226 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004227 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004228 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004229 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004230 break;
4231 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004232 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004233 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004234 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004235 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004236 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004237 jmp_rel(ctxt, ctxt->src.val);
4238 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004239 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004240 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004241 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004242 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004243 case 0xf5: /* cmc */
4244 /* complement carry flag from eflags reg */
4245 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004246 break;
4247 case 0xf8: /* clc */
4248 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004249 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004250 case 0xf9: /* stc */
4251 ctxt->eflags |= EFLG_CF;
4252 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004253 case 0xfc: /* cld */
4254 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004255 break;
4256 case 0xfd: /* std */
4257 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004258 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004259 default:
4260 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004262
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004263 if (rc != X86EMUL_CONTINUE)
4264 goto done;
4265
Avi Kivity018a98d2007-11-27 19:30:56 +02004266writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004267 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004268 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004269 goto done;
4270
Gleb Natapov5cd21912010-03-18 15:20:26 +02004271 /*
4272 * restore dst type in case the decoding will be reused
4273 * (happens for string instruction )
4274 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004275 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004276
Avi Kivity9dac77f2011-06-01 15:34:25 +03004277 if ((ctxt->d & SrcMask) == SrcSI)
4278 string_addr_inc(ctxt, seg_override(ctxt),
4279 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004280
Avi Kivity9dac77f2011-06-01 15:34:25 +03004281 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004282 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004283 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004284
Avi Kivity9dac77f2011-06-01 15:34:25 +03004285 if (ctxt->rep_prefix && (ctxt->d & String)) {
4286 struct read_cache *r = &ctxt->io_read;
4287 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004288
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004289 if (!string_insn_completed(ctxt)) {
4290 /*
4291 * Re-enter guest when pio read ahead buffer is empty
4292 * or, if it is not used, after each 1024 iteration.
4293 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004294 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004295 (r->end == 0 || r->end != r->pos)) {
4296 /*
4297 * Reset read cache. Usually happens before
4298 * decode, but since instruction is restarted
4299 * we have to do it here.
4300 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004301 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004302 return EMULATION_RESTART;
4303 }
4304 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004305 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004306 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004307
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004309
4310done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004311 if (rc == X86EMUL_PROPAGATE_FAULT)
4312 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004313 if (rc == X86EMUL_INTERCEPTED)
4314 return EMULATION_INTERCEPTED;
4315
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004316 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004317
4318twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004320 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004321 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004322 break;
4323 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004324 case 0x0d: /* GrpP (prefetch) */
4325 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004326 break;
4327 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004328 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004329 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004330 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004331 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004334 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4335 if (!test_cc(ctxt->b, ctxt->eflags))
4336 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004337 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004338 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004339 if (test_cc(ctxt->b, ctxt->eflags))
4340 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004341 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004342 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004343 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004344 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004345 case 0xa4: /* shld imm8, r, r/m */
4346 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004347 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004348 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004349 case 0xac: /* shrd imm8, r, r/m */
4350 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004351 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004352 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004353 case 0xae: /* clflush */
4354 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004355 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 ctxt->dst.bytes = ctxt->op_bytes;
4357 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4358 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004361 ctxt->dst.bytes = ctxt->op_bytes;
4362 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4363 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004364 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004365 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004366 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004367 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004368 ctxt->src.val = ctxt->dst.orig_val;
4369 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004370 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004371 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004372 ctxt->dst.bytes = ctxt->op_bytes;
4373 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4374 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004375 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004376 default:
4377 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004379
4380 if (rc != X86EMUL_CONTINUE)
4381 goto done;
4382
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383 goto writeback;
4384
4385cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004386 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387}