blob: 663e95881bdba8c49527e2f4828bd7eaf565fa53 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya9945542011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020060#define OpMem8 26ull /* 8-bit zero extended memory operand */
Avi Kivitya9945542011-09-13 10:45:41 +030061
Avi Kivity0fe59122011-09-13 10:45:47 +030062#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030063#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030064
65/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080066 * Opcode effective-address decode tables.
67 * Note that we only emulate instructions that have at least one memory
68 * operand (excluding implicit stack references). We assume that stack
69 * references and instruction fetches will never occur in special memory
70 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
71 * not be handled.
72 */
73
74/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030075#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030077#define DstShift 1
78#define ImplicitOps (OpImplicit << DstShift)
79#define DstReg (OpReg << DstShift)
80#define DstMem (OpMem << DstShift)
81#define DstAcc (OpAcc << DstShift)
82#define DstDI (OpDI << DstShift)
83#define DstMem64 (OpMem64 << DstShift)
84#define DstImmUByte (OpImmUByte << DstShift)
85#define DstDX (OpDX << DstShift)
86#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030088#define SrcShift 6
89#define SrcNone (OpNone << SrcShift)
90#define SrcReg (OpReg << SrcShift)
91#define SrcMem (OpMem << SrcShift)
92#define SrcMem16 (OpMem16 << SrcShift)
93#define SrcMem32 (OpMem32 << SrcShift)
94#define SrcImm (OpImm << SrcShift)
95#define SrcImmByte (OpImmByte << SrcShift)
96#define SrcOne (OpOne << SrcShift)
97#define SrcImmUByte (OpImmUByte << SrcShift)
98#define SrcImmU (OpImmU << SrcShift)
99#define SrcSI (OpSI << SrcShift)
100#define SrcImmFAddr (OpImmFAddr << SrcShift)
101#define SrcMemFAddr (OpMemFAddr << SrcShift)
102#define SrcAcc (OpAcc << SrcShift)
103#define SrcImmU16 (OpImmU16 << SrcShift)
104#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200105#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300106#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300107#define BitOp (1<<11)
108#define MemAbs (1<<12) /* Memory operand is absolute displacement */
109#define String (1<<13) /* String instruction (rep capable) */
110#define Stack (1<<14) /* Stack instruction (push/pop) */
111#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
112#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
113#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
114#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
115#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
116#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300117/* Generic ModRM decode. */
118#define ModRM (1<<19)
119/* Destination is only written; never read. */
120#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300121/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200122#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200123#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300124#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300125#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300126#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200127#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200128#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300129#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800130#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100131/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800132#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300133#define Src2None (OpNone << Src2Shift)
134#define Src2CL (OpCL << Src2Shift)
135#define Src2ImmByte (OpImmByte << Src2Shift)
136#define Src2One (OpOne << Src2Shift)
137#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300138#define Src2ES (OpES << Src2Shift)
139#define Src2CS (OpCS << Src2Shift)
140#define Src2SS (OpSS << Src2Shift)
141#define Src2DS (OpDS << Src2Shift)
142#define Src2FS (OpFS << Src2Shift)
143#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300145#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300146#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
147#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
148#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Avi Kivityd0e53322010-07-29 15:11:54 +0300150#define X2(x...) x, x
151#define X3(x...) X2(x), x
152#define X4(x...) X2(x), X2(x)
153#define X5(x...) X4(x), x
154#define X6(x...) X4(x), X2(x)
155#define X7(x...) X4(x), X3(x)
156#define X8(x...) X4(x), X4(x)
157#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300158
Avi Kivityd65b1de2010-07-29 15:11:35 +0300159struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300160 u64 flags : 56;
161 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300162 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300163 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200164 const struct opcode *group;
165 const struct group_dual *gdual;
166 const struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300167 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200168 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300169};
170
171struct group_dual {
172 struct opcode mod012[8];
173 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300174};
175
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200176struct gprefix {
177 struct opcode pfx_no;
178 struct opcode pfx_66;
179 struct opcode pfx_f2;
180 struct opcode pfx_f3;
181};
182
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200184#define EFLG_ID (1<<21)
185#define EFLG_VIP (1<<20)
186#define EFLG_VIF (1<<19)
187#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200188#define EFLG_VM (1<<17)
189#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200190#define EFLG_IOPL (3<<12)
191#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192#define EFLG_OF (1<<11)
193#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200194#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200195#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196#define EFLG_SF (1<<7)
197#define EFLG_ZF (1<<6)
198#define EFLG_AF (1<<4)
199#define EFLG_PF (1<<2)
200#define EFLG_CF (1<<0)
201
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300202#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
203#define EFLG_RESERVED_ONE_MASK 2
204
Avi Kivitydd856ef2012-08-27 23:46:17 +0300205static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
206{
207 if (!(ctxt->regs_valid & (1 << nr))) {
208 ctxt->regs_valid |= 1 << nr;
209 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
210 }
211 return ctxt->_regs[nr];
212}
213
214static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
215{
216 ctxt->regs_valid |= 1 << nr;
217 ctxt->regs_dirty |= 1 << nr;
218 return &ctxt->_regs[nr];
219}
220
221static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
222{
223 reg_read(ctxt, nr);
224 return reg_write(ctxt, nr);
225}
226
227static void writeback_registers(struct x86_emulate_ctxt *ctxt)
228{
229 unsigned reg;
230
231 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
232 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
233}
234
235static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
236{
237 ctxt->regs_dirty = 0;
238 ctxt->regs_valid = 0;
239}
240
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241/*
242 * Instruction emulation:
243 * Most instructions are emulated directly via a fragment of inline assembly
244 * code. This allows us to save/restore EFLAGS and thus very easily pick up
245 * any modified flags.
246 */
247
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800248#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249#define _LO32 "k" /* force 32-bit operand */
250#define _STK "%%rsp" /* stack pointer */
251#elif defined(__i386__)
252#define _LO32 "" /* force 32-bit operand */
253#define _STK "%%esp" /* stack pointer */
254#endif
255
256/*
257 * These EFLAGS bits are restored from saved value during emulation, and
258 * any changes are written back to the saved value after emulation.
259 */
260#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
261
262/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200263#define _PRE_EFLAGS(_sav, _msk, _tmp) \
264 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
265 "movl %"_sav",%"_LO32 _tmp"; " \
266 "push %"_tmp"; " \
267 "push %"_tmp"; " \
268 "movl %"_msk",%"_LO32 _tmp"; " \
269 "andl %"_LO32 _tmp",("_STK"); " \
270 "pushf; " \
271 "notl %"_LO32 _tmp"; " \
272 "andl %"_LO32 _tmp",("_STK"); " \
273 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
274 "pop %"_tmp"; " \
275 "orl %"_LO32 _tmp",("_STK"); " \
276 "popf; " \
277 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278
279/* After executing instruction: write-back necessary bits in EFLAGS. */
280#define _POST_EFLAGS(_sav, _msk, _tmp) \
281 /* _sav |= EFLAGS & _msk; */ \
282 "pushf; " \
283 "pop %"_tmp"; " \
284 "andl %"_msk",%"_LO32 _tmp"; " \
285 "orl %"_LO32 _tmp",%"_sav"; "
286
Avi Kivitydda96d82008-11-26 15:14:10 +0200287#ifdef CONFIG_X86_64
288#define ON64(x) x
289#else
290#define ON64(x)
291#endif
292
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300293#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200294 do { \
295 __asm__ __volatile__ ( \
296 _PRE_EFLAGS("0", "4", "2") \
297 _op _suffix " %"_x"3,%1; " \
298 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300299 : "=m" ((ctxt)->eflags), \
300 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200301 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300302 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200303 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200304
305
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300307#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200308 do { \
309 unsigned long _tmp; \
310 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300311 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200312 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300313 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200314 break; \
315 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300316 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200317 break; \
318 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300319 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200320 break; \
321 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 } while (0)
323
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300324#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200326 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300327 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300329 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 break; \
331 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300332 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 _wx, _wy, _lx, _ly, _qx, _qy); \
334 break; \
335 } \
336 } while (0)
337
338/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300339#define emulate_2op_SrcB(ctxt, _op) \
340 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341
342/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300343#define emulate_2op_SrcV(ctxt, _op) \
344 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345
346/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300347#define emulate_2op_SrcV_nobyte(ctxt, _op) \
348 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100350/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300351#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300352 do { \
353 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300354 _type _clv = (ctxt)->src2.val; \
355 _type _srcv = (ctxt)->src.val; \
356 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300357 \
358 __asm__ __volatile__ ( \
359 _PRE_EFLAGS("0", "5", "2") \
360 _op _suffix " %4,%1 \n" \
361 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300362 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300363 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
364 ); \
365 \
Avi Kivity761441b2011-09-07 16:41:36 +0300366 (ctxt)->src2.val = (unsigned long) _clv; \
367 (ctxt)->src2.val = (unsigned long) _srcv; \
368 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100369 } while (0)
370
Avi Kivity761441b2011-09-07 16:41:36 +0300371#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300372 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300373 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300374 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300375 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300376 break; \
377 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300378 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300379 break; \
380 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300381 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300382 break; \
383 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100384 } while (0)
385
Avi Kivityd1eef452011-09-07 16:41:38 +0300386#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387 do { \
388 unsigned long _tmp; \
389 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200390 __asm__ __volatile__ ( \
391 _PRE_EFLAGS("0", "3", "2") \
392 _op _suffix " %1; " \
393 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300394 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200395 "=&r" (_tmp) \
396 : "i" (EFLAGS_MASK)); \
397 } while (0)
398
399/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300400#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200401 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300402 switch ((ctxt)->dst.bytes) { \
403 case 1: __emulate_1op(ctxt, _op, "b"); break; \
404 case 2: __emulate_1op(ctxt, _op, "w"); break; \
405 case 4: __emulate_1op(ctxt, _op, "l"); break; \
406 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 } \
408 } while (0)
409
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300410#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300411 do { \
412 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300413 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
414 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300415 \
416 __asm__ __volatile__ ( \
417 _PRE_EFLAGS("0", "5", "1") \
418 "1: \n\t" \
419 _op _suffix " %6; " \
420 "2: \n\t" \
421 _POST_EFLAGS("0", "5", "1") \
422 ".pushsection .fixup,\"ax\" \n\t" \
423 "3: movb $1, %4 \n\t" \
424 "jmp 2b \n\t" \
425 ".popsection \n\t" \
426 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300427 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
428 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
429 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
430 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300431 } while (0)
432
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300433/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300434#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300435 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300436 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300437 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300438 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300439 break; \
440 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300441 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300442 break; \
443 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300444 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300445 break; \
446 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300447 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300448 break; \
449 } \
450 } while (0)
451
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200452static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
453 enum x86_intercept intercept,
454 enum x86_intercept_stage stage)
455{
456 struct x86_instruction_info info = {
457 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300458 .rep_prefix = ctxt->rep_prefix,
459 .modrm_mod = ctxt->modrm_mod,
460 .modrm_reg = ctxt->modrm_reg,
461 .modrm_rm = ctxt->modrm_rm,
462 .src_val = ctxt->src.val64,
463 .src_bytes = ctxt->src.bytes,
464 .dst_bytes = ctxt->dst.bytes,
465 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200466 .next_rip = ctxt->eip,
467 };
468
Avi Kivity29535382011-04-20 13:37:53 +0300469 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200470}
471
Avi Kivityf47cfa32012-06-07 17:49:24 +0300472static void assign_masked(ulong *dest, ulong src, ulong mask)
473{
474 *dest = (*dest & ~mask) | (src & mask);
475}
476
Avi Kivity9dac77f2011-06-01 15:34:25 +0300477static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800478{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300479 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800480}
481
Avi Kivityf47cfa32012-06-07 17:49:24 +0300482static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
483{
484 u16 sel;
485 struct desc_struct ss;
486
487 if (ctxt->mode == X86EMUL_MODE_PROT64)
488 return ~0UL;
489 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
490 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
491}
492
Avi Kivity612e89f2012-06-12 20:03:23 +0300493static int stack_size(struct x86_emulate_ctxt *ctxt)
494{
495 return (__fls(stack_mask(ctxt)) + 1) >> 3;
496}
497
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800499static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300500address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800501{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300502 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800503 return reg;
504 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300505 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800506}
507
508static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300509register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800510{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300511 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800512}
513
Avi Kivity5ad105e2012-08-19 14:34:31 +0300514static void masked_increment(ulong *reg, ulong mask, int inc)
515{
516 assign_masked(reg, *reg + inc, mask);
517}
518
Harvey Harrison7a9572752008-02-19 07:40:41 -0800519static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300520register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800521{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300522 ulong mask;
523
Avi Kivity9dac77f2011-06-01 15:34:25 +0300524 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300525 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800526 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300527 mask = ad_mask(ctxt);
528 masked_increment(reg, mask, inc);
529}
530
531static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
532{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300533 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800534}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535
Avi Kivity9dac77f2011-06-01 15:34:25 +0300536static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800537{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300538 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800539}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300540
Avi Kivity56697682011-04-03 14:08:51 +0300541static u32 desc_limit_scaled(struct desc_struct *desc)
542{
543 u32 limit = get_desc_limit(desc);
544
545 return desc->g ? (limit << 12) | 0xfff : limit;
546}
547
Avi Kivity9dac77f2011-06-01 15:34:25 +0300548static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300549{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300550 ctxt->has_seg_override = true;
551 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300552}
553
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900554static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300555{
556 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
557 return 0;
558
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900559 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300560}
561
Avi Kivity9dac77f2011-06-01 15:34:25 +0300562static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300563{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300564 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300565 return 0;
566
Avi Kivity9dac77f2011-06-01 15:34:25 +0300567 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300568}
569
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200570static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
571 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300572{
Avi Kivityda9cb572010-11-22 17:53:21 +0200573 ctxt->exception.vector = vec;
574 ctxt->exception.error_code = error;
575 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200576 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300577}
578
Joerg Roedel3b88e412011-04-04 12:39:29 +0200579static int emulate_db(struct x86_emulate_ctxt *ctxt)
580{
581 return emulate_exception(ctxt, DB_VECTOR, 0, false);
582}
583
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200584static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300585{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200586 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300587}
588
Avi Kivity618ff152011-04-03 12:32:09 +0300589static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
590{
591 return emulate_exception(ctxt, SS_VECTOR, err, true);
592}
593
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200594static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300595{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200596 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300597}
598
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200599static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300600{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200601 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300602}
603
Avi Kivity34d1f492010-08-26 11:59:01 +0300604static int emulate_de(struct x86_emulate_ctxt *ctxt)
605{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200606 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300607}
608
Avi Kivity12537912011-03-29 11:41:27 +0200609static int emulate_nm(struct x86_emulate_ctxt *ctxt)
610{
611 return emulate_exception(ctxt, NM_VECTOR, 0, false);
612}
613
Avi Kivity1aa36612011-04-27 13:20:30 +0300614static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
615{
616 u16 selector;
617 struct desc_struct desc;
618
619 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
620 return selector;
621}
622
623static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
624 unsigned seg)
625{
626 u16 dummy;
627 u32 base3;
628 struct desc_struct desc;
629
630 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
631 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
632}
633
Avi Kivity1c11b372012-04-09 18:39:59 +0300634/*
635 * x86 defines three classes of vector instructions: explicitly
636 * aligned, explicitly unaligned, and the rest, which change behaviour
637 * depending on whether they're AVX encoded or not.
638 *
639 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
640 * subject to the same check.
641 */
642static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
643{
644 if (likely(size < 16))
645 return false;
646
647 if (ctxt->d & Aligned)
648 return true;
649 else if (ctxt->d & Unaligned)
650 return false;
651 else if (ctxt->d & Avx)
652 return false;
653 else
654 return true;
655}
656
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400657static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300658 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400659 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300660 ulong *linear)
661{
Avi Kivity618ff152011-04-03 12:32:09 +0300662 struct desc_struct desc;
663 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300664 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300665 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300666 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300667 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300668
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900669 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300670 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300671 case X86EMUL_MODE_PROT64:
672 if (((signed long)la << 16) >> 16 != la)
673 return emulate_gp(ctxt, 0);
674 break;
675 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300676 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
677 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300678 if (!usable)
679 goto bad;
680 /* code segment or read-only data segment */
681 if (((desc.type & 8) || !(desc.type & 2)) && write)
682 goto bad;
683 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400684 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300685 goto bad;
686 lim = desc_limit_scaled(&desc);
687 if ((desc.type & 8) || !(desc.type & 4)) {
688 /* expand-up segment */
689 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
690 goto bad;
691 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800692 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300693 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
694 goto bad;
695 lim = desc.d ? 0xffffffff : 0xffff;
696 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
697 goto bad;
698 }
Avi Kivity717746e2011-04-20 13:37:53 +0300699 cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya5625182012-08-21 17:07:05 +0300700 if (ctxt->mode == X86EMUL_MODE_REAL)
701 rpl = 0;
702 else
703 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300704 cpl = max(cpl, rpl);
705 if (!(desc.type & 8)) {
706 /* data segment */
707 if (cpl > desc.dpl)
708 goto bad;
709 } else if ((desc.type & 8) && !(desc.type & 4)) {
710 /* nonconforming code segment */
711 if (cpl != desc.dpl)
712 goto bad;
713 } else if ((desc.type & 8) && (desc.type & 4)) {
714 /* conforming code segment */
715 if (cpl < desc.dpl)
716 goto bad;
717 }
718 break;
719 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300720 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300721 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300722 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
723 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300724 *linear = la;
725 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300726bad:
727 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300728 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300729 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300730 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300731}
732
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400733static int linearize(struct x86_emulate_ctxt *ctxt,
734 struct segmented_address addr,
735 unsigned size, bool write,
736 ulong *linear)
737{
738 return __linearize(ctxt, addr, size, write, false, linear);
739}
740
741
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200742static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
743 struct segmented_address addr,
744 void *data,
745 unsigned size)
746{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200747 int rc;
748 ulong linear;
749
Avi Kivity83b87952011-04-03 11:31:19 +0300750 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200751 if (rc != X86EMUL_CONTINUE)
752 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300753 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200754}
755
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900756/*
757 * Fetch the next byte of the instruction being emulated which is pointed to
758 * by ctxt->_eip, then increment ctxt->_eip.
759 *
760 * Also prefetch the remaining bytes of the instruction without crossing page
761 * boundary if they are not in fetch_cache yet.
762 */
763static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200764{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300765 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200766 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300767 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200768
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900769 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400770 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900771 struct segmented_address addr = { .seg = VCPU_SREG_CS,
772 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300773 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900774 size = min(15UL - cur_size,
775 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400776 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900777 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400778 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900779 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
780 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900781 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200782 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300783 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200784 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900785 *dest = fc->data[ctxt->_eip - fc->start];
786 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900787 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200788}
789
790static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900791 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200792{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900793 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200794
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200795 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900796 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200797 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200798 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900799 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900800 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200801 return rc;
802 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900803 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200804}
805
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900806/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900807#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900808({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900809 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900810 if (rc != X86EMUL_CONTINUE) \
811 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900812 (_type)_x; \
813})
814
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900815#define insn_fetch_arr(_arr, _size, _ctxt) \
816({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900817 if (rc != X86EMUL_CONTINUE) \
818 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900819})
820
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000821/*
822 * Given the 'reg' portion of a ModRM byte, and a register block, return a
823 * pointer into the block that addresses the relevant register.
824 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
825 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300826static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000827 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828{
829 void *p;
830
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300832 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
833 else
834 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 return p;
836}
837
838static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200839 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 u16 *size, unsigned long *address, int op_bytes)
841{
842 int rc;
843
844 if (op_bytes == 2)
845 op_bytes = 3;
846 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200847 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900848 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200850 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200851 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 return rc;
853}
854
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300855static int test_cc(unsigned int condition, unsigned int flags)
856{
857 int rc = 0;
858
859 switch ((condition & 15) >> 1) {
860 case 0: /* o */
861 rc |= (flags & EFLG_OF);
862 break;
863 case 1: /* b/c/nae */
864 rc |= (flags & EFLG_CF);
865 break;
866 case 2: /* z/e */
867 rc |= (flags & EFLG_ZF);
868 break;
869 case 3: /* be/na */
870 rc |= (flags & (EFLG_CF|EFLG_ZF));
871 break;
872 case 4: /* s */
873 rc |= (flags & EFLG_SF);
874 break;
875 case 5: /* p/pe */
876 rc |= (flags & EFLG_PF);
877 break;
878 case 7: /* le/ng */
879 rc |= (flags & EFLG_ZF);
880 /* fall through */
881 case 6: /* l/nge */
882 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
883 break;
884 }
885
886 /* Odd condition identifiers (lsb == 1) have inverted sense. */
887 return (!!rc ^ (condition & 1));
888}
889
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300890static void fetch_register_operand(struct operand *op)
891{
892 switch (op->bytes) {
893 case 1:
894 op->val = *(u8 *)op->addr.reg;
895 break;
896 case 2:
897 op->val = *(u16 *)op->addr.reg;
898 break;
899 case 4:
900 op->val = *(u32 *)op->addr.reg;
901 break;
902 case 8:
903 op->val = *(u64 *)op->addr.reg;
904 break;
905 }
906}
907
Avi Kivity12537912011-03-29 11:41:27 +0200908static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
909{
910 ctxt->ops->get_fpu(ctxt);
911 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200912 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
913 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
914 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
915 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
916 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
917 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
918 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
919 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200920#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200921 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
922 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
923 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
924 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
925 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
926 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
927 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
928 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200929#endif
930 default: BUG();
931 }
932 ctxt->ops->put_fpu(ctxt);
933}
934
935static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
936 int reg)
937{
938 ctxt->ops->get_fpu(ctxt);
939 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200940 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
941 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
942 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
943 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
944 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
945 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
946 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
947 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200948#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200949 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
950 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
951 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
952 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
953 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
954 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
955 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
956 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200957#endif
958 default: BUG();
959 }
960 ctxt->ops->put_fpu(ctxt);
961}
962
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300963static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
964{
965 ctxt->ops->get_fpu(ctxt);
966 switch (reg) {
967 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
968 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
969 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
970 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
971 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
972 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
973 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
974 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
975 default: BUG();
976 }
977 ctxt->ops->put_fpu(ctxt);
978}
979
980static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
981{
982 ctxt->ops->get_fpu(ctxt);
983 switch (reg) {
984 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
985 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
986 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
987 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
988 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
989 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
990 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
991 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
992 default: BUG();
993 }
994 ctxt->ops->put_fpu(ctxt);
995}
996
Avi Kivity12537912011-03-29 11:41:27 +0200997static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200998 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200999{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001000 unsigned reg = ctxt->modrm_reg;
1001 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001002
Avi Kivity9dac77f2011-06-01 15:34:25 +03001003 if (!(ctxt->d & ModRM))
1004 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001005
Avi Kivity9dac77f2011-06-01 15:34:25 +03001006 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001007 op->type = OP_XMM;
1008 op->bytes = 16;
1009 op->addr.xmm = reg;
1010 read_sse_reg(ctxt, &op->vec_val, reg);
1011 return;
1012 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001013 if (ctxt->d & Mmx) {
1014 reg &= 7;
1015 op->type = OP_MM;
1016 op->bytes = 8;
1017 op->addr.mm = reg;
1018 return;
1019 }
Avi Kivity12537912011-03-29 11:41:27 +02001020
Avi Kivity3c118e22007-10-31 10:27:04 +02001021 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001022 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001023 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001024 op->bytes = 1;
1025 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001026 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001027 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001028 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001029 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001030 op->orig_val = op->val;
1031}
1032
Avi Kivitya6e34072012-06-10 17:15:39 +03001033static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1034{
1035 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1036 ctxt->modrm_seg = VCPU_SREG_SS;
1037}
1038
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001039static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001040 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001041{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001042 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001043 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001044 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001045 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001046
Avi Kivity9dac77f2011-06-01 15:34:25 +03001047 if (ctxt->rex_prefix) {
1048 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1049 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1050 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001051 }
1052
Avi Kivity9dac77f2011-06-01 15:34:25 +03001053 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1054 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1055 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1056 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001057
Avi Kivity9dac77f2011-06-01 15:34:25 +03001058 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001059 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001060 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001061 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001062 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001063 op->type = OP_XMM;
1064 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001065 op->addr.xmm = ctxt->modrm_rm;
1066 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001067 return rc;
1068 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001069 if (ctxt->d & Mmx) {
1070 op->type = OP_MM;
1071 op->bytes = 8;
1072 op->addr.xmm = ctxt->modrm_rm & 7;
1073 return rc;
1074 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001075 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001076 return rc;
1077 }
1078
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001079 op->type = OP_MEM;
1080
Avi Kivity9dac77f2011-06-01 15:34:25 +03001081 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001082 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1083 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1084 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1085 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001086
1087 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001088 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001089 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001090 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001091 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001092 break;
1093 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001094 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001095 break;
1096 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001097 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001098 break;
1099 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001100 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001101 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001102 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001103 break;
1104 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001105 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001106 break;
1107 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001108 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001109 break;
1110 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001111 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001112 break;
1113 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001114 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001115 break;
1116 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001117 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001118 break;
1119 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001120 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001121 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001122 break;
1123 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001124 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 break;
1126 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001127 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1128 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1129 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001130 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001131 } else {
1132 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001133 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001134 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001135 index_reg |= (sib >> 3) & 7;
1136 base_reg |= sib & 7;
1137 scale = sib >> 6;
1138
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001140 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001141 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001142 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001143 adjust_modrm_seg(ctxt, base_reg);
1144 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001145 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001146 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001147 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001148 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001149 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001150 } else {
1151 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001152 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001153 adjust_modrm_seg(ctxt, base_reg);
1154 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001155 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001156 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001157 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001158 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001159 break;
1160 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001161 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001162 break;
1163 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001164 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001165 break;
1166 }
1167 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001168 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001169done:
1170 return rc;
1171}
1172
1173static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001174 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001176 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001177
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001178 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001179 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001181 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182 break;
1183 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001184 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 break;
1186 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001187 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 break;
1189 }
1190done:
1191 return rc;
1192}
1193
Avi Kivity9dac77f2011-06-01 15:34:25 +03001194static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001195{
Sheng Yang7129eec2010-09-28 16:33:32 +08001196 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001197
Avi Kivity9dac77f2011-06-01 15:34:25 +03001198 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1199 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001200
Avi Kivity9dac77f2011-06-01 15:34:25 +03001201 if (ctxt->src.bytes == 2)
1202 sv = (s16)ctxt->src.val & (s16)mask;
1203 else if (ctxt->src.bytes == 4)
1204 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001205
Avi Kivity9dac77f2011-06-01 15:34:25 +03001206 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001207 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001208
1209 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001210 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001211}
1212
Gleb Natapov9de41572010-04-28 19:15:22 +03001213static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001214 unsigned long addr, void *dest, unsigned size)
1215{
1216 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001217 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001218
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001219 if (mc->pos < mc->end)
1220 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001221
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001222 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001223
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001224 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1225 &ctxt->exception);
1226 if (rc != X86EMUL_CONTINUE)
1227 return rc;
1228
1229 mc->end += size;
1230
1231read_cached:
1232 memcpy(dest, mc->data + mc->pos, size);
1233 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001234 return X86EMUL_CONTINUE;
1235}
1236
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001237static int segmented_read(struct x86_emulate_ctxt *ctxt,
1238 struct segmented_address addr,
1239 void *data,
1240 unsigned size)
1241{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001242 int rc;
1243 ulong linear;
1244
Avi Kivity83b87952011-04-03 11:31:19 +03001245 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001246 if (rc != X86EMUL_CONTINUE)
1247 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001248 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001249}
1250
1251static int segmented_write(struct x86_emulate_ctxt *ctxt,
1252 struct segmented_address addr,
1253 const void *data,
1254 unsigned size)
1255{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001256 int rc;
1257 ulong linear;
1258
Avi Kivity83b87952011-04-03 11:31:19 +03001259 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001260 if (rc != X86EMUL_CONTINUE)
1261 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001262 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1263 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001264}
1265
1266static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1267 struct segmented_address addr,
1268 const void *orig_data, const void *data,
1269 unsigned size)
1270{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001271 int rc;
1272 ulong linear;
1273
Avi Kivity83b87952011-04-03 11:31:19 +03001274 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001275 if (rc != X86EMUL_CONTINUE)
1276 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001277 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1278 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001279}
1280
Gleb Natapov7b262e92010-03-18 15:20:27 +02001281static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001282 unsigned int size, unsigned short port,
1283 void *dest)
1284{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001285 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001286
1287 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001288 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001289 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001290 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001291 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001292 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1293 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001294 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1295 count);
1296 if (n == 0)
1297 n = 1;
1298 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001299 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001300 return 0;
1301 rc->end = n * size;
1302 }
1303
1304 memcpy(dest, rc->data + rc->pos, size);
1305 rc->pos += size;
1306 return 1;
1307}
1308
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001309static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1310 u16 index, struct desc_struct *desc)
1311{
1312 struct desc_ptr dt;
1313 ulong addr;
1314
1315 ctxt->ops->get_idt(ctxt, &dt);
1316
1317 if (dt.size < index * 8 + 7)
1318 return emulate_gp(ctxt, index << 3 | 0x2);
1319
1320 addr = dt.address + index * 8;
1321 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1322 &ctxt->exception);
1323}
1324
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001325static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001326 u16 selector, struct desc_ptr *dt)
1327{
Mathias Krause0225fb52012-08-30 01:30:16 +02001328 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001329
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001330 if (selector & 1 << 2) {
1331 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001332 u16 sel;
1333
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001334 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001335 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001336 return;
1337
1338 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1339 dt->address = get_desc_base(&desc);
1340 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001341 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001342}
1343
1344/* allowed just for 8 bytes segments */
1345static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001346 u16 selector, struct desc_struct *desc,
1347 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001348{
1349 struct desc_ptr dt;
1350 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001351 ulong addr;
1352
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001353 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001354
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001355 if (dt.size < index * 8 + 7)
1356 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001357
Avi Kivitye9194642012-06-13 16:29:39 +03001358 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001359 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1360 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001361}
1362
1363/* allowed just for 8 bytes segments */
1364static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001365 u16 selector, struct desc_struct *desc)
1366{
1367 struct desc_ptr dt;
1368 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001369 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001370
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001371 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001372
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001373 if (dt.size < index * 8 + 7)
1374 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001375
1376 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001377 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1378 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001379}
1380
Gleb Natapov5601d052011-03-07 14:55:06 +02001381/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001382static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001383 u16 selector, int seg)
1384{
Avi Kivity869be992012-06-13 16:30:53 +03001385 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001386 u8 dpl, rpl, cpl;
1387 unsigned err_vec = GP_VECTOR;
1388 u32 err_code = 0;
1389 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001390 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001391 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001392 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001393
1394 memset(&seg_desc, 0, sizeof seg_desc);
1395
1396 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1397 || ctxt->mode == X86EMUL_MODE_REAL) {
1398 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001399 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001400 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001401 goto load;
1402 }
1403
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001404 rpl = selector & 3;
1405 cpl = ctxt->ops->cpl(ctxt);
1406
1407 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1408 if ((seg == VCPU_SREG_CS
1409 || (seg == VCPU_SREG_SS
1410 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1411 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001412 && null_selector)
1413 goto exception;
1414
1415 /* TR should be in GDT only */
1416 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1417 goto exception;
1418
1419 if (null_selector) /* for NULL selector skip all following checks */
1420 goto load;
1421
Avi Kivitye9194642012-06-13 16:29:39 +03001422 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423 if (ret != X86EMUL_CONTINUE)
1424 return ret;
1425
1426 err_code = selector & 0xfffc;
1427 err_vec = GP_VECTOR;
1428
Guo Chaofc058682012-06-28 15:19:51 +08001429 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001430 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1431 goto exception;
1432
1433 if (!seg_desc.p) {
1434 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1435 goto exception;
1436 }
1437
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001439
1440 switch (seg) {
1441 case VCPU_SREG_SS:
1442 /*
1443 * segment is not a writable data segment or segment
1444 * selector's RPL != CPL or segment selector's RPL != CPL
1445 */
1446 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1447 goto exception;
1448 break;
1449 case VCPU_SREG_CS:
1450 if (!(seg_desc.type & 8))
1451 goto exception;
1452
1453 if (seg_desc.type & 4) {
1454 /* conforming */
1455 if (dpl > cpl)
1456 goto exception;
1457 } else {
1458 /* nonconforming */
1459 if (rpl > cpl || dpl != cpl)
1460 goto exception;
1461 }
1462 /* CS(RPL) <- CPL */
1463 selector = (selector & 0xfffc) | cpl;
1464 break;
1465 case VCPU_SREG_TR:
1466 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1467 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001468 old_desc = seg_desc;
1469 seg_desc.type |= 2; /* busy */
1470 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1471 sizeof(seg_desc), &ctxt->exception);
1472 if (ret != X86EMUL_CONTINUE)
1473 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001474 break;
1475 case VCPU_SREG_LDTR:
1476 if (seg_desc.s || seg_desc.type != 2)
1477 goto exception;
1478 break;
1479 default: /* DS, ES, FS, or GS */
1480 /*
1481 * segment is not a data or readable code segment or
1482 * ((segment is a data or nonconforming code segment)
1483 * and (both RPL and CPL > DPL))
1484 */
1485 if ((seg_desc.type & 0xa) == 0x8 ||
1486 (((seg_desc.type & 0xc) != 0xc) &&
1487 (rpl > dpl && cpl > dpl)))
1488 goto exception;
1489 break;
1490 }
1491
1492 if (seg_desc.s) {
1493 /* mark segment as accessed */
1494 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001495 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001496 if (ret != X86EMUL_CONTINUE)
1497 return ret;
1498 }
1499load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001500 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001501 return X86EMUL_CONTINUE;
1502exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001503 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 return X86EMUL_PROPAGATE_FAULT;
1505}
1506
Wei Yongjun31be40b2010-08-17 09:17:30 +08001507static void write_register_operand(struct operand *op)
1508{
1509 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1510 switch (op->bytes) {
1511 case 1:
1512 *(u8 *)op->addr.reg = (u8)op->val;
1513 break;
1514 case 2:
1515 *(u16 *)op->addr.reg = (u16)op->val;
1516 break;
1517 case 4:
1518 *op->addr.reg = (u32)op->val;
1519 break; /* 64b: zero-extend */
1520 case 8:
1521 *op->addr.reg = op->val;
1522 break;
1523 }
1524}
1525
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001526static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001527{
1528 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001529
Avi Kivity9dac77f2011-06-01 15:34:25 +03001530 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001531 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001532 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001533 break;
1534 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001535 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001536 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001537 ctxt->dst.addr.mem,
1538 &ctxt->dst.orig_val,
1539 &ctxt->dst.val,
1540 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001541 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001542 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001543 ctxt->dst.addr.mem,
1544 &ctxt->dst.val,
1545 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001546 if (rc != X86EMUL_CONTINUE)
1547 return rc;
1548 break;
Avi Kivity12537912011-03-29 11:41:27 +02001549 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001550 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001551 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001552 case OP_MM:
1553 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1554 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001555 case OP_NONE:
1556 /* no writeback */
1557 break;
1558 default:
1559 break;
1560 }
1561 return X86EMUL_CONTINUE;
1562}
1563
Avi Kivity51ddff52012-06-12 20:19:40 +03001564static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001565{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001566 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001567
Avi Kivity5ad105e2012-08-19 14:34:31 +03001568 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001569 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001570 addr.seg = VCPU_SREG_SS;
1571
Avi Kivity51ddff52012-06-12 20:19:40 +03001572 return segmented_write(ctxt, addr, data, bytes);
1573}
1574
1575static int em_push(struct x86_emulate_ctxt *ctxt)
1576{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001577 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001578 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001579 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001580}
1581
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001582static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001583 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001584{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001585 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001586 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001587
Avi Kivitydd856ef2012-08-27 23:46:17 +03001588 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001589 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001590 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001591 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001592 return rc;
1593
Avi Kivity5ad105e2012-08-19 14:34:31 +03001594 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001595 return rc;
1596}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001597
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001598static int em_pop(struct x86_emulate_ctxt *ctxt)
1599{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001600 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001601}
1602
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001603static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001604 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001605{
1606 int rc;
1607 unsigned long val, change_mask;
1608 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001609 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001610
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001611 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001612 if (rc != X86EMUL_CONTINUE)
1613 return rc;
1614
1615 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1616 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1617
1618 switch(ctxt->mode) {
1619 case X86EMUL_MODE_PROT64:
1620 case X86EMUL_MODE_PROT32:
1621 case X86EMUL_MODE_PROT16:
1622 if (cpl == 0)
1623 change_mask |= EFLG_IOPL;
1624 if (cpl <= iopl)
1625 change_mask |= EFLG_IF;
1626 break;
1627 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001628 if (iopl < 3)
1629 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001630 change_mask |= EFLG_IF;
1631 break;
1632 default: /* real mode */
1633 change_mask |= (EFLG_IOPL | EFLG_IF);
1634 break;
1635 }
1636
1637 *(unsigned long *)dest =
1638 (ctxt->eflags & ~change_mask) | (val & change_mask);
1639
1640 return rc;
1641}
1642
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001643static int em_popf(struct x86_emulate_ctxt *ctxt)
1644{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001645 ctxt->dst.type = OP_REG;
1646 ctxt->dst.addr.reg = &ctxt->eflags;
1647 ctxt->dst.bytes = ctxt->op_bytes;
1648 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001649}
1650
Avi Kivity612e89f2012-06-12 20:03:23 +03001651static int em_enter(struct x86_emulate_ctxt *ctxt)
1652{
1653 int rc;
1654 unsigned frame_size = ctxt->src.val;
1655 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001656 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001657
1658 if (nesting_level)
1659 return X86EMUL_UNHANDLEABLE;
1660
Avi Kivitydd856ef2012-08-27 23:46:17 +03001661 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1662 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001663 if (rc != X86EMUL_CONTINUE)
1664 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001665 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001666 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001667 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1668 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001669 stack_mask(ctxt));
1670 return X86EMUL_CONTINUE;
1671}
1672
Avi Kivityf47cfa32012-06-07 17:49:24 +03001673static int em_leave(struct x86_emulate_ctxt *ctxt)
1674{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001675 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001676 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001677 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001678}
1679
Avi Kivity1cd196e2011-09-13 10:45:51 +03001680static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001681{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001682 int seg = ctxt->src2.val;
1683
Avi Kivity9dac77f2011-06-01 15:34:25 +03001684 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001685
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001686 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001687}
1688
Avi Kivity1cd196e2011-09-13 10:45:51 +03001689static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001690{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001691 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001692 unsigned long selector;
1693 int rc;
1694
Avi Kivity9dac77f2011-06-01 15:34:25 +03001695 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001696 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001697 return rc;
1698
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001699 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001700 return rc;
1701}
1702
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001703static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001704{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001705 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001706 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001707 int reg = VCPU_REGS_RAX;
1708
1709 while (reg <= VCPU_REGS_RDI) {
1710 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001711 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001712
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001713 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001714 if (rc != X86EMUL_CONTINUE)
1715 return rc;
1716
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001717 ++reg;
1718 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001719
Wei Yongjunc37eda12010-06-15 09:03:33 +08001720 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001721}
1722
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001723static int em_pushf(struct x86_emulate_ctxt *ctxt)
1724{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001725 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001726 return em_push(ctxt);
1727}
1728
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001729static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001730{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001731 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001732 int reg = VCPU_REGS_RDI;
1733
1734 while (reg >= VCPU_REGS_RAX) {
1735 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001736 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001737 --reg;
1738 }
1739
Avi Kivitydd856ef2012-08-27 23:46:17 +03001740 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001741 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001742 break;
1743 --reg;
1744 }
1745 return rc;
1746}
1747
Avi Kivitydd856ef2012-08-27 23:46:17 +03001748static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001749{
Mathias Krause0225fb52012-08-30 01:30:16 +02001750 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001751 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001752 struct desc_ptr dt;
1753 gva_t cs_addr;
1754 gva_t eip_addr;
1755 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001756
1757 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001758 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001759 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001760 if (rc != X86EMUL_CONTINUE)
1761 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001762
1763 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1764
Avi Kivity9dac77f2011-06-01 15:34:25 +03001765 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001766 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001767 if (rc != X86EMUL_CONTINUE)
1768 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001769
Avi Kivity9dac77f2011-06-01 15:34:25 +03001770 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001771 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001772 if (rc != X86EMUL_CONTINUE)
1773 return rc;
1774
Avi Kivity4bff1e862011-04-20 13:37:53 +03001775 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001776
1777 eip_addr = dt.address + (irq << 2);
1778 cs_addr = dt.address + (irq << 2) + 2;
1779
Avi Kivity0f65dd72011-04-20 13:37:53 +03001780 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001781 if (rc != X86EMUL_CONTINUE)
1782 return rc;
1783
Avi Kivity0f65dd72011-04-20 13:37:53 +03001784 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001785 if (rc != X86EMUL_CONTINUE)
1786 return rc;
1787
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001788 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001789 if (rc != X86EMUL_CONTINUE)
1790 return rc;
1791
Avi Kivity9dac77f2011-06-01 15:34:25 +03001792 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001793
1794 return rc;
1795}
1796
Avi Kivitydd856ef2012-08-27 23:46:17 +03001797int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1798{
1799 int rc;
1800
1801 invalidate_registers(ctxt);
1802 rc = __emulate_int_real(ctxt, irq);
1803 if (rc == X86EMUL_CONTINUE)
1804 writeback_registers(ctxt);
1805 return rc;
1806}
1807
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001808static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001809{
1810 switch(ctxt->mode) {
1811 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001812 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001813 case X86EMUL_MODE_VM86:
1814 case X86EMUL_MODE_PROT16:
1815 case X86EMUL_MODE_PROT32:
1816 case X86EMUL_MODE_PROT64:
1817 default:
1818 /* Protected mode interrupts unimplemented yet */
1819 return X86EMUL_UNHANDLEABLE;
1820 }
1821}
1822
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001823static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001824{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001825 int rc = X86EMUL_CONTINUE;
1826 unsigned long temp_eip = 0;
1827 unsigned long temp_eflags = 0;
1828 unsigned long cs = 0;
1829 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1830 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1831 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1832 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1833
1834 /* TODO: Add stack limit check */
1835
Avi Kivity9dac77f2011-06-01 15:34:25 +03001836 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001837
1838 if (rc != X86EMUL_CONTINUE)
1839 return rc;
1840
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001841 if (temp_eip & ~0xffff)
1842 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001843
Avi Kivity9dac77f2011-06-01 15:34:25 +03001844 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001845
1846 if (rc != X86EMUL_CONTINUE)
1847 return rc;
1848
Avi Kivity9dac77f2011-06-01 15:34:25 +03001849 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001850
1851 if (rc != X86EMUL_CONTINUE)
1852 return rc;
1853
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001854 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001855
1856 if (rc != X86EMUL_CONTINUE)
1857 return rc;
1858
Avi Kivity9dac77f2011-06-01 15:34:25 +03001859 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001860
1861
Avi Kivity9dac77f2011-06-01 15:34:25 +03001862 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001863 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001864 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001865 ctxt->eflags &= ~0xffff;
1866 ctxt->eflags |= temp_eflags;
1867 }
1868
1869 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1870 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1871
1872 return rc;
1873}
1874
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001875static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001876{
1877 switch(ctxt->mode) {
1878 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001879 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001880 case X86EMUL_MODE_VM86:
1881 case X86EMUL_MODE_PROT16:
1882 case X86EMUL_MODE_PROT32:
1883 case X86EMUL_MODE_PROT64:
1884 default:
1885 /* iret from protected mode unimplemented yet */
1886 return X86EMUL_UNHANDLEABLE;
1887 }
1888}
1889
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001890static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1891{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001892 int rc;
1893 unsigned short sel;
1894
Avi Kivity9dac77f2011-06-01 15:34:25 +03001895 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001896
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001897 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001898 if (rc != X86EMUL_CONTINUE)
1899 return rc;
1900
Avi Kivity9dac77f2011-06-01 15:34:25 +03001901 ctxt->_eip = 0;
1902 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001903 return X86EMUL_CONTINUE;
1904}
1905
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001906static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001907{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001909 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001910 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001911 break;
1912 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001913 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001914 break;
1915 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001916 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001917 break;
1918 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001919 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001920 break;
1921 case 4: /* sal/shl */
1922 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001923 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001924 break;
1925 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001926 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001927 break;
1928 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001929 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001930 break;
1931 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001932 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001933}
1934
Avi Kivity3329ece2011-09-13 10:45:39 +03001935static int em_not(struct x86_emulate_ctxt *ctxt)
1936{
1937 ctxt->dst.val = ~ctxt->dst.val;
1938 return X86EMUL_CONTINUE;
1939}
1940
1941static int em_neg(struct x86_emulate_ctxt *ctxt)
1942{
1943 emulate_1op(ctxt, "neg");
1944 return X86EMUL_CONTINUE;
1945}
1946
1947static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1948{
1949 u8 ex = 0;
1950
1951 emulate_1op_rax_rdx(ctxt, "mul", ex);
1952 return X86EMUL_CONTINUE;
1953}
1954
1955static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1956{
1957 u8 ex = 0;
1958
1959 emulate_1op_rax_rdx(ctxt, "imul", ex);
1960 return X86EMUL_CONTINUE;
1961}
1962
1963static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001964{
Avi Kivity34d1f492010-08-26 11:59:01 +03001965 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001966
Avi Kivity3329ece2011-09-13 10:45:39 +03001967 emulate_1op_rax_rdx(ctxt, "div", de);
1968 if (de)
1969 return emulate_de(ctxt);
1970 return X86EMUL_CONTINUE;
1971}
1972
1973static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1974{
1975 u8 de = 0;
1976
1977 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001978 if (de)
1979 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001980 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001981}
1982
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001983static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001984{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001985 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001986
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001988 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001989 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001990 break;
1991 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001992 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001993 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001994 case 2: /* call near abs */ {
1995 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001996 old_eip = ctxt->_eip;
1997 ctxt->_eip = ctxt->src.val;
1998 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001999 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002000 break;
2001 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002002 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002003 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002004 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002005 case 5: /* jmp far */
2006 rc = em_jmp_far(ctxt);
2007 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002008 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002009 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002010 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002011 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002012 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002013}
2014
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002015static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002016{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002017 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002018
Avi Kivitydd856ef2012-08-27 23:46:17 +03002019 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2020 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2021 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2022 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002023 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002024 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002025 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2026 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002027
Laurent Vivier05f086f2007-09-24 11:10:55 +02002028 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002029 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002030 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002031}
2032
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002033static int em_ret(struct x86_emulate_ctxt *ctxt)
2034{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002035 ctxt->dst.type = OP_REG;
2036 ctxt->dst.addr.reg = &ctxt->_eip;
2037 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002038 return em_pop(ctxt);
2039}
2040
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002041static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002042{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002043 int rc;
2044 unsigned long cs;
2045
Avi Kivity9dac77f2011-06-01 15:34:25 +03002046 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002047 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002048 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002049 if (ctxt->op_bytes == 4)
2050 ctxt->_eip = (u32)ctxt->_eip;
2051 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002052 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002053 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002054 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002055 return rc;
2056}
2057
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002058static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2059{
2060 /* Save real source value, then compare EAX against destination. */
2061 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002062 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002063 emulate_2op_SrcV(ctxt, "cmp");
2064
2065 if (ctxt->eflags & EFLG_ZF) {
2066 /* Success: write back to memory. */
2067 ctxt->dst.val = ctxt->src.orig_val;
2068 } else {
2069 /* Failure: write the value we saw to EAX. */
2070 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002071 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002072 }
2073 return X86EMUL_CONTINUE;
2074}
2075
Avi Kivityd4b43252011-09-13 10:45:50 +03002076static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002077{
Avi Kivityd4b43252011-09-13 10:45:50 +03002078 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002079 unsigned short sel;
2080 int rc;
2081
Avi Kivity9dac77f2011-06-01 15:34:25 +03002082 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002083
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002084 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002085 if (rc != X86EMUL_CONTINUE)
2086 return rc;
2087
Avi Kivity9dac77f2011-06-01 15:34:25 +03002088 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002089 return rc;
2090}
2091
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002092static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002093setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002094 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002095{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002096 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002097 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002098 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002099 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002100 cs->type = 0x0b; /* Read, Execute, Accessed */
2101 cs->s = 1;
2102 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002103 cs->p = 1;
2104 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002105 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002106
Gleb Natapov79168fd2010-04-28 19:15:30 +03002107 set_desc_base(ss, 0); /* flat segment */
2108 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002109 ss->g = 1; /* 4kb granularity */
2110 ss->s = 1;
2111 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002112 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002113 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002114 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002115 ss->l = 0;
2116 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002117}
2118
Avi Kivity1a18a692012-02-01 12:23:21 +02002119static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2120{
2121 u32 eax, ebx, ecx, edx;
2122
2123 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002124 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2125 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002126 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2127 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2128}
2129
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002130static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2131{
Mathias Krause0225fb52012-08-30 01:30:16 +02002132 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002133 u32 eax, ebx, ecx, edx;
2134
2135 /*
2136 * syscall should always be enabled in longmode - so only become
2137 * vendor specific (cpuid) if other modes are active...
2138 */
2139 if (ctxt->mode == X86EMUL_MODE_PROT64)
2140 return true;
2141
2142 eax = 0x00000000;
2143 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002144 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2145 /*
2146 * Intel ("GenuineIntel")
2147 * remark: Intel CPUs only support "syscall" in 64bit
2148 * longmode. Also an 64bit guest with a
2149 * 32bit compat-app running will #UD !! While this
2150 * behaviour can be fixed (by emulating) into AMD
2151 * response - CPUs of AMD can't behave like Intel.
2152 */
2153 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2154 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2155 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2156 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002157
Avi Kivity0017f932012-06-07 14:10:16 +03002158 /* AMD ("AuthenticAMD") */
2159 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2160 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2161 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2162 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002163
Avi Kivity0017f932012-06-07 14:10:16 +03002164 /* AMD ("AMDisbetter!") */
2165 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2166 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2167 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2168 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002169
2170 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2171 return false;
2172}
2173
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002174static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002175{
Mathias Krause0225fb52012-08-30 01:30:16 +02002176 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002177 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002178 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002179 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002180 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002181
2182 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002183 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002184 ctxt->mode == X86EMUL_MODE_VM86)
2185 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002186
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002187 if (!(em_syscall_is_enabled(ctxt)))
2188 return emulate_ud(ctxt);
2189
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002190 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002191 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002192
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002193 if (!(efer & EFER_SCE))
2194 return emulate_ud(ctxt);
2195
Avi Kivity717746e2011-04-20 13:37:53 +03002196 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002197 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002198 cs_sel = (u16)(msr_data & 0xfffc);
2199 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002200
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002201 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002202 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002203 cs.l = 1;
2204 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002205 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2206 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002207
Avi Kivitydd856ef2012-08-27 23:46:17 +03002208 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002209 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002210#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002211 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212
Avi Kivity717746e2011-04-20 13:37:53 +03002213 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002214 ctxt->mode == X86EMUL_MODE_PROT64 ?
2215 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002216 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002217
Avi Kivity717746e2011-04-20 13:37:53 +03002218 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002219 ctxt->eflags &= ~(msr_data | EFLG_RF);
2220#endif
2221 } else {
2222 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002223 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002224 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002225
2226 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2227 }
2228
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002229 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002230}
2231
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002232static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002233{
Mathias Krause0225fb52012-08-30 01:30:16 +02002234 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002235 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002236 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002237 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002238 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002239
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002240 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002241 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002242 if (ctxt->mode == X86EMUL_MODE_REAL)
2243 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002244
Avi Kivity1a18a692012-02-01 12:23:21 +02002245 /*
2246 * Not recognized on AMD in compat mode (but is recognized in legacy
2247 * mode).
2248 */
2249 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2250 && !vendor_intel(ctxt))
2251 return emulate_ud(ctxt);
2252
Andre Przywara8c604352009-06-18 12:56:01 +02002253 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2254 * Therefore, we inject an #UD.
2255 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002256 if (ctxt->mode == X86EMUL_MODE_PROT64)
2257 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002258
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002259 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002260
Avi Kivity717746e2011-04-20 13:37:53 +03002261 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002262 switch (ctxt->mode) {
2263 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002264 if ((msr_data & 0xfffc) == 0x0)
2265 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002266 break;
2267 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002268 if (msr_data == 0x0)
2269 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002270 break;
2271 }
2272
2273 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002274 cs_sel = (u16)msr_data;
2275 cs_sel &= ~SELECTOR_RPL_MASK;
2276 ss_sel = cs_sel + 8;
2277 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002278 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002279 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002280 cs.l = 1;
2281 }
2282
Avi Kivity1aa36612011-04-27 13:20:30 +03002283 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2284 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002285
Avi Kivity717746e2011-04-20 13:37:53 +03002286 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002287 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002288
Avi Kivity717746e2011-04-20 13:37:53 +03002289 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002290 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002291
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002292 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002293}
2294
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002295static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002296{
Mathias Krause0225fb52012-08-30 01:30:16 +02002297 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002298 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002299 u64 msr_data;
2300 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002301 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002302
Gleb Natapova0044752010-02-10 14:21:31 +02002303 /* inject #GP if in real mode or Virtual 8086 mode */
2304 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002305 ctxt->mode == X86EMUL_MODE_VM86)
2306 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002307
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002308 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002309
Avi Kivity9dac77f2011-06-01 15:34:25 +03002310 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002311 usermode = X86EMUL_MODE_PROT64;
2312 else
2313 usermode = X86EMUL_MODE_PROT32;
2314
2315 cs.dpl = 3;
2316 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002317 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002318 switch (usermode) {
2319 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002320 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002321 if ((msr_data & 0xfffc) == 0x0)
2322 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002323 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002324 break;
2325 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002326 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002327 if (msr_data == 0x0)
2328 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002329 ss_sel = cs_sel + 8;
2330 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002331 cs.l = 1;
2332 break;
2333 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002334 cs_sel |= SELECTOR_RPL_MASK;
2335 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002336
Avi Kivity1aa36612011-04-27 13:20:30 +03002337 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2338 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002339
Avi Kivitydd856ef2012-08-27 23:46:17 +03002340 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2341 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002342
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002343 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002344}
2345
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002346static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002347{
2348 int iopl;
2349 if (ctxt->mode == X86EMUL_MODE_REAL)
2350 return false;
2351 if (ctxt->mode == X86EMUL_MODE_VM86)
2352 return true;
2353 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002354 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002355}
2356
2357static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002358 u16 port, u16 len)
2359{
Mathias Krause0225fb52012-08-30 01:30:16 +02002360 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002361 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002362 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002363 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002364 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002365 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002366 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002367
Avi Kivity1aa36612011-04-27 13:20:30 +03002368 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002369 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002370 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002371 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002372 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002373 base = get_desc_base(&tr_seg);
2374#ifdef CONFIG_X86_64
2375 base |= ((u64)base3) << 32;
2376#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002377 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002378 if (r != X86EMUL_CONTINUE)
2379 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002380 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002381 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002382 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002383 if (r != X86EMUL_CONTINUE)
2384 return false;
2385 if ((perm >> bit_idx) & mask)
2386 return false;
2387 return true;
2388}
2389
2390static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002391 u16 port, u16 len)
2392{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002393 if (ctxt->perm_ok)
2394 return true;
2395
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002396 if (emulator_bad_iopl(ctxt))
2397 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002398 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002399
2400 ctxt->perm_ok = true;
2401
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002402 return true;
2403}
2404
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406 struct tss_segment_16 *tss)
2407{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002408 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002410 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2411 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2412 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2413 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2414 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2415 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2416 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2417 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418
Avi Kivity1aa36612011-04-27 13:20:30 +03002419 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2420 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2421 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2422 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2423 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002424}
2425
2426static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427 struct tss_segment_16 *tss)
2428{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002429 int ret;
2430
Avi Kivity9dac77f2011-06-01 15:34:25 +03002431 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002433 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2434 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2435 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2436 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2437 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2438 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2439 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2440 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002441
2442 /*
2443 * SDM says that segment selectors are loaded before segment
2444 * descriptors
2445 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002446 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2447 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2448 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2449 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2450 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002451
2452 /*
Guo Chaofc058682012-06-28 15:19:51 +08002453 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454 * it is handled in a context of new task
2455 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002456 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457 if (ret != X86EMUL_CONTINUE)
2458 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002459 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002460 if (ret != X86EMUL_CONTINUE)
2461 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002462 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002463 if (ret != X86EMUL_CONTINUE)
2464 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002465 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002466 if (ret != X86EMUL_CONTINUE)
2467 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002468 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 if (ret != X86EMUL_CONTINUE)
2470 return ret;
2471
2472 return X86EMUL_CONTINUE;
2473}
2474
2475static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002476 u16 tss_selector, u16 old_tss_sel,
2477 ulong old_tss_base, struct desc_struct *new_desc)
2478{
Mathias Krause0225fb52012-08-30 01:30:16 +02002479 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002480 struct tss_segment_16 tss_seg;
2481 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002482 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483
Avi Kivity0f65dd72011-04-20 13:37:53 +03002484 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002485 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002486 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002487 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002488 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002489
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002490 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491
Avi Kivity0f65dd72011-04-20 13:37:53 +03002492 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002493 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002494 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002495 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497
Avi Kivity0f65dd72011-04-20 13:37:53 +03002498 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002499 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002500 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002501 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002502 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503
2504 if (old_tss_sel != 0xffff) {
2505 tss_seg.prev_task_link = old_tss_sel;
2506
Avi Kivity0f65dd72011-04-20 13:37:53 +03002507 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002508 &tss_seg.prev_task_link,
2509 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002510 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002511 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002514 }
2515
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002516 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517}
2518
2519static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520 struct tss_segment_32 *tss)
2521{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002522 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002523 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002525 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2526 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2527 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2528 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2529 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2530 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2531 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2532 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002533
Avi Kivity1aa36612011-04-27 13:20:30 +03002534 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2535 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2536 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2537 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2538 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2539 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2540 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541}
2542
2543static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544 struct tss_segment_32 *tss)
2545{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002546 int ret;
2547
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002548 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002549 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002550 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002552
2553 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002554 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2555 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2556 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2557 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2558 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2559 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2560 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2561 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002562
2563 /*
2564 * SDM says that segment selectors are loaded before segment
2565 * descriptors
2566 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002567 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2568 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2569 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2570 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2571 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2572 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2573 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574
2575 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002576 * If we're switching between Protected Mode and VM86, we need to make
2577 * sure to update the mode before loading the segment descriptors so
2578 * that the selectors are interpreted correctly.
2579 *
2580 * Need to get rflags to the vcpu struct immediately because it
2581 * influences the CPL which is checked at least when loading the segment
2582 * descriptors and when pushing an error code to the new kernel stack.
2583 *
2584 * TODO Introduce a separate ctxt->ops->set_cpl callback
2585 */
2586 if (ctxt->eflags & X86_EFLAGS_VM)
2587 ctxt->mode = X86EMUL_MODE_VM86;
2588 else
2589 ctxt->mode = X86EMUL_MODE_PROT32;
2590
2591 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2592
2593 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 * Now load segment descriptors. If fault happenes at this stage
2595 * it is handled in a context of new task
2596 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002597 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 if (ret != X86EMUL_CONTINUE)
2599 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002600 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601 if (ret != X86EMUL_CONTINUE)
2602 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002603 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 if (ret != X86EMUL_CONTINUE)
2605 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002606 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 if (ret != X86EMUL_CONTINUE)
2608 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002609 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 if (ret != X86EMUL_CONTINUE)
2611 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002612 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 if (ret != X86EMUL_CONTINUE)
2614 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002615 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 if (ret != X86EMUL_CONTINUE)
2617 return ret;
2618
2619 return X86EMUL_CONTINUE;
2620}
2621
2622static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 u16 tss_selector, u16 old_tss_sel,
2624 ulong old_tss_base, struct desc_struct *new_desc)
2625{
Mathias Krause0225fb52012-08-30 01:30:16 +02002626 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 struct tss_segment_32 tss_seg;
2628 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002629 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630
Avi Kivity0f65dd72011-04-20 13:37:53 +03002631 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002632 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002633 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002637 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638
Avi Kivity0f65dd72011-04-20 13:37:53 +03002639 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002640 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002641 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644
Avi Kivity0f65dd72011-04-20 13:37:53 +03002645 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002646 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002647 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650
2651 if (old_tss_sel != 0xffff) {
2652 tss_seg.prev_task_link = old_tss_sel;
2653
Avi Kivity0f65dd72011-04-20 13:37:53 +03002654 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655 &tss_seg.prev_task_link,
2656 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002657 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002658 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661 }
2662
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002663 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664}
2665
2666static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002667 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002668 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669{
Mathias Krause0225fb52012-08-30 01:30:16 +02002670 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 struct desc_struct curr_tss_desc, next_tss_desc;
2672 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002673 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002675 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002676 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002677 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678
2679 /* FIXME: old_tss_base == ~0 ? */
2680
Avi Kivitye9194642012-06-13 16:29:39 +03002681 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 if (ret != X86EMUL_CONTINUE)
2683 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002684 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 if (ret != X86EMUL_CONTINUE)
2686 return ret;
2687
2688 /* FIXME: check that next_tss_desc is tss */
2689
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002690 /*
2691 * Check privileges. The three cases are task switch caused by...
2692 *
2693 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2694 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002695 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002696 */
2697 if (reason == TASK_SWITCH_GATE) {
2698 if (idt_index != -1) {
2699 /* Software interrupts */
2700 struct desc_struct task_gate_desc;
2701 int dpl;
2702
2703 ret = read_interrupt_descriptor(ctxt, idt_index,
2704 &task_gate_desc);
2705 if (ret != X86EMUL_CONTINUE)
2706 return ret;
2707
2708 dpl = task_gate_desc.dpl;
2709 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2710 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2711 }
2712 } else if (reason != TASK_SWITCH_IRET) {
2713 int dpl = next_tss_desc.dpl;
2714 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2715 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 }
2717
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002718
Gleb Natapovceffb452010-03-18 15:20:19 +02002719 desc_limit = desc_limit_scaled(&next_tss_desc);
2720 if (!next_tss_desc.p ||
2721 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2722 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002723 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002724 return X86EMUL_PROPAGATE_FAULT;
2725 }
2726
2727 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2728 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002729 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 }
2731
2732 if (reason == TASK_SWITCH_IRET)
2733 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2734
2735 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002736 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2738 old_tss_sel = 0xffff;
2739
2740 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002741 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002742 old_tss_base, &next_tss_desc);
2743 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002744 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002746 if (ret != X86EMUL_CONTINUE)
2747 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748
2749 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2750 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2751
2752 if (reason != TASK_SWITCH_IRET) {
2753 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002754 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 }
2756
Avi Kivity717746e2011-04-20 13:37:53 +03002757 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002758 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759
Jan Kiszkae269fb22010-04-14 15:51:09 +02002760 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002761 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2762 ctxt->lock_prefix = 0;
2763 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002764 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002765 }
2766
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 return ret;
2768}
2769
2770int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002771 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002772 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 int rc;
2775
Avi Kivitydd856ef2012-08-27 23:46:17 +03002776 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002777 ctxt->_eip = ctxt->eip;
2778 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002780 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002781 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782
Avi Kivitydd856ef2012-08-27 23:46:17 +03002783 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002784 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002785 writeback_registers(ctxt);
2786 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002788 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789}
2790
Avi Kivity90de84f2010-11-17 15:28:21 +02002791static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002792 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002793{
Gleb Natapova682e352010-03-18 15:20:21 +02002794 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2795
Avi Kivitydd856ef2012-08-27 23:46:17 +03002796 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2797 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Avi Kivity90de84f2010-11-17 15:28:21 +02002798 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002799}
2800
Avi Kivity7af04fc2010-08-18 14:16:35 +03002801static int em_das(struct x86_emulate_ctxt *ctxt)
2802{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002803 u8 al, old_al;
2804 bool af, cf, old_cf;
2805
2806 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002807 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002808
2809 old_al = al;
2810 old_cf = cf;
2811 cf = false;
2812 af = ctxt->eflags & X86_EFLAGS_AF;
2813 if ((al & 0x0f) > 9 || af) {
2814 al -= 6;
2815 cf = old_cf | (al >= 250);
2816 af = true;
2817 } else {
2818 af = false;
2819 }
2820 if (old_al > 0x99 || old_cf) {
2821 al -= 0x60;
2822 cf = true;
2823 }
2824
Avi Kivity9dac77f2011-06-01 15:34:25 +03002825 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002826 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002827 ctxt->src.type = OP_IMM;
2828 ctxt->src.val = 0;
2829 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002830 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002831 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2832 if (cf)
2833 ctxt->eflags |= X86_EFLAGS_CF;
2834 if (af)
2835 ctxt->eflags |= X86_EFLAGS_AF;
2836 return X86EMUL_CONTINUE;
2837}
2838
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002839static int em_call(struct x86_emulate_ctxt *ctxt)
2840{
2841 long rel = ctxt->src.val;
2842
2843 ctxt->src.val = (unsigned long)ctxt->_eip;
2844 jmp_rel(ctxt, rel);
2845 return em_push(ctxt);
2846}
2847
Avi Kivity0ef753b2010-08-18 14:51:45 +03002848static int em_call_far(struct x86_emulate_ctxt *ctxt)
2849{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002850 u16 sel, old_cs;
2851 ulong old_eip;
2852 int rc;
2853
Avi Kivity1aa36612011-04-27 13:20:30 +03002854 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002855 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002856
Avi Kivity9dac77f2011-06-01 15:34:25 +03002857 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002858 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002859 return X86EMUL_CONTINUE;
2860
Avi Kivity9dac77f2011-06-01 15:34:25 +03002861 ctxt->_eip = 0;
2862 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002863
Avi Kivity9dac77f2011-06-01 15:34:25 +03002864 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002865 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002866 if (rc != X86EMUL_CONTINUE)
2867 return rc;
2868
Avi Kivity9dac77f2011-06-01 15:34:25 +03002869 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002870 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002871}
2872
Avi Kivity40ece7c2010-08-18 15:12:09 +03002873static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2874{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002875 int rc;
2876
Avi Kivity9dac77f2011-06-01 15:34:25 +03002877 ctxt->dst.type = OP_REG;
2878 ctxt->dst.addr.reg = &ctxt->_eip;
2879 ctxt->dst.bytes = ctxt->op_bytes;
2880 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002881 if (rc != X86EMUL_CONTINUE)
2882 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002883 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002884 return X86EMUL_CONTINUE;
2885}
2886
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002887static int em_add(struct x86_emulate_ctxt *ctxt)
2888{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002889 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002890 return X86EMUL_CONTINUE;
2891}
2892
2893static int em_or(struct x86_emulate_ctxt *ctxt)
2894{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002895 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002896 return X86EMUL_CONTINUE;
2897}
2898
2899static int em_adc(struct x86_emulate_ctxt *ctxt)
2900{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002901 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002902 return X86EMUL_CONTINUE;
2903}
2904
2905static int em_sbb(struct x86_emulate_ctxt *ctxt)
2906{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002907 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002908 return X86EMUL_CONTINUE;
2909}
2910
2911static int em_and(struct x86_emulate_ctxt *ctxt)
2912{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002913 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002914 return X86EMUL_CONTINUE;
2915}
2916
2917static int em_sub(struct x86_emulate_ctxt *ctxt)
2918{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002919 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002920 return X86EMUL_CONTINUE;
2921}
2922
2923static int em_xor(struct x86_emulate_ctxt *ctxt)
2924{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002925 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002926 return X86EMUL_CONTINUE;
2927}
2928
2929static int em_cmp(struct x86_emulate_ctxt *ctxt)
2930{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002931 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002932 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002933 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002934 return X86EMUL_CONTINUE;
2935}
2936
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002937static int em_test(struct x86_emulate_ctxt *ctxt)
2938{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002939 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002940 /* Disable writeback. */
2941 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002942 return X86EMUL_CONTINUE;
2943}
2944
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002945static int em_xchg(struct x86_emulate_ctxt *ctxt)
2946{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002947 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002948 ctxt->src.val = ctxt->dst.val;
2949 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002950
2951 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002952 ctxt->dst.val = ctxt->src.orig_val;
2953 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002954 return X86EMUL_CONTINUE;
2955}
2956
Avi Kivity5c82aa22010-08-18 18:31:43 +03002957static int em_imul(struct x86_emulate_ctxt *ctxt)
2958{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002959 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002960 return X86EMUL_CONTINUE;
2961}
2962
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002963static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2964{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002965 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002966 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002967}
2968
Avi Kivity61429142010-08-19 15:13:00 +03002969static int em_cwd(struct x86_emulate_ctxt *ctxt)
2970{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002971 ctxt->dst.type = OP_REG;
2972 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002973 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002974 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002975
2976 return X86EMUL_CONTINUE;
2977}
2978
Avi Kivity48bb5d32010-08-18 18:54:34 +03002979static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2980{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002981 u64 tsc = 0;
2982
Avi Kivity717746e2011-04-20 13:37:53 +03002983 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002984 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2985 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002986 return X86EMUL_CONTINUE;
2987}
2988
Avi Kivity222d21a2011-11-10 14:57:30 +02002989static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2990{
2991 u64 pmc;
2992
Avi Kivitydd856ef2012-08-27 23:46:17 +03002993 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002994 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002995 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2996 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02002997 return X86EMUL_CONTINUE;
2998}
2999
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003000static int em_mov(struct x86_emulate_ctxt *ctxt)
3001{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003002 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003003 return X86EMUL_CONTINUE;
3004}
3005
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003006static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3007{
3008 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3009 return emulate_gp(ctxt, 0);
3010
3011 /* Disable writeback. */
3012 ctxt->dst.type = OP_NONE;
3013 return X86EMUL_CONTINUE;
3014}
3015
3016static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3017{
3018 unsigned long val;
3019
3020 if (ctxt->mode == X86EMUL_MODE_PROT64)
3021 val = ctxt->src.val & ~0ULL;
3022 else
3023 val = ctxt->src.val & ~0U;
3024
3025 /* #UD condition is already handled. */
3026 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3027 return emulate_gp(ctxt, 0);
3028
3029 /* Disable writeback. */
3030 ctxt->dst.type = OP_NONE;
3031 return X86EMUL_CONTINUE;
3032}
3033
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003034static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3035{
3036 u64 msr_data;
3037
Avi Kivitydd856ef2012-08-27 23:46:17 +03003038 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3039 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3040 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003041 return emulate_gp(ctxt, 0);
3042
3043 return X86EMUL_CONTINUE;
3044}
3045
3046static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3047{
3048 u64 msr_data;
3049
Avi Kivitydd856ef2012-08-27 23:46:17 +03003050 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003051 return emulate_gp(ctxt, 0);
3052
Avi Kivitydd856ef2012-08-27 23:46:17 +03003053 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3054 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003055 return X86EMUL_CONTINUE;
3056}
3057
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003058static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3059{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003060 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003061 return emulate_ud(ctxt);
3062
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003064 return X86EMUL_CONTINUE;
3065}
3066
3067static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3068{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003070
Avi Kivity9dac77f2011-06-01 15:34:25 +03003071 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003072 return emulate_ud(ctxt);
3073
Avi Kivity9dac77f2011-06-01 15:34:25 +03003074 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003075 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3076
3077 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003078 ctxt->dst.type = OP_NONE;
3079 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003080}
3081
Avi Kivitya14e5792012-06-13 12:28:33 +03003082static int em_lldt(struct x86_emulate_ctxt *ctxt)
3083{
3084 u16 sel = ctxt->src.val;
3085
3086 /* Disable writeback. */
3087 ctxt->dst.type = OP_NONE;
3088 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3089}
3090
Avi Kivity80890002012-06-13 16:33:29 +03003091static int em_ltr(struct x86_emulate_ctxt *ctxt)
3092{
3093 u16 sel = ctxt->src.val;
3094
3095 /* Disable writeback. */
3096 ctxt->dst.type = OP_NONE;
3097 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3098}
3099
Avi Kivity38503912011-03-31 18:48:09 +02003100static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3101{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003102 int rc;
3103 ulong linear;
3104
Avi Kivity9dac77f2011-06-01 15:34:25 +03003105 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003106 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003107 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003108 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003109 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003110 return X86EMUL_CONTINUE;
3111}
3112
Avi Kivity2d04a052011-04-20 15:32:49 +03003113static int em_clts(struct x86_emulate_ctxt *ctxt)
3114{
3115 ulong cr0;
3116
3117 cr0 = ctxt->ops->get_cr(ctxt, 0);
3118 cr0 &= ~X86_CR0_TS;
3119 ctxt->ops->set_cr(ctxt, 0, cr0);
3120 return X86EMUL_CONTINUE;
3121}
3122
Avi Kivity26d05cc2011-04-21 12:07:59 +03003123static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3124{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003125 int rc;
3126
Avi Kivity9dac77f2011-06-01 15:34:25 +03003127 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003128 return X86EMUL_UNHANDLEABLE;
3129
3130 rc = ctxt->ops->fix_hypercall(ctxt);
3131 if (rc != X86EMUL_CONTINUE)
3132 return rc;
3133
3134 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003135 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003136 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003137 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003138 return X86EMUL_CONTINUE;
3139}
3140
Avi Kivity96051572012-06-10 17:21:18 +03003141static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3142 void (*get)(struct x86_emulate_ctxt *ctxt,
3143 struct desc_ptr *ptr))
3144{
3145 struct desc_ptr desc_ptr;
3146
3147 if (ctxt->mode == X86EMUL_MODE_PROT64)
3148 ctxt->op_bytes = 8;
3149 get(ctxt, &desc_ptr);
3150 if (ctxt->op_bytes == 2) {
3151 ctxt->op_bytes = 4;
3152 desc_ptr.address &= 0x00ffffff;
3153 }
3154 /* Disable writeback. */
3155 ctxt->dst.type = OP_NONE;
3156 return segmented_write(ctxt, ctxt->dst.addr.mem,
3157 &desc_ptr, 2 + ctxt->op_bytes);
3158}
3159
3160static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3161{
3162 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3163}
3164
3165static int em_sidt(struct x86_emulate_ctxt *ctxt)
3166{
3167 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3168}
3169
Avi Kivity26d05cc2011-04-21 12:07:59 +03003170static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3171{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003172 struct desc_ptr desc_ptr;
3173 int rc;
3174
Avi Kivity510425f2012-06-07 17:04:36 +03003175 if (ctxt->mode == X86EMUL_MODE_PROT64)
3176 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003177 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003178 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003179 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003180 if (rc != X86EMUL_CONTINUE)
3181 return rc;
3182 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3183 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003184 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003185 return X86EMUL_CONTINUE;
3186}
3187
Avi Kivity5ef39c72011-04-21 12:21:50 +03003188static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003189{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003190 int rc;
3191
Avi Kivity5ef39c72011-04-21 12:21:50 +03003192 rc = ctxt->ops->fix_hypercall(ctxt);
3193
Avi Kivity26d05cc2011-04-21 12:07:59 +03003194 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003195 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003196 return rc;
3197}
3198
3199static int em_lidt(struct x86_emulate_ctxt *ctxt)
3200{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003201 struct desc_ptr desc_ptr;
3202 int rc;
3203
Avi Kivity510425f2012-06-07 17:04:36 +03003204 if (ctxt->mode == X86EMUL_MODE_PROT64)
3205 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003206 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003207 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209 if (rc != X86EMUL_CONTINUE)
3210 return rc;
3211 ctxt->ops->set_idt(ctxt, &desc_ptr);
3212 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003213 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003214 return X86EMUL_CONTINUE;
3215}
3216
3217static int em_smsw(struct x86_emulate_ctxt *ctxt)
3218{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003219 ctxt->dst.bytes = 2;
3220 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003221 return X86EMUL_CONTINUE;
3222}
3223
3224static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3225{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003226 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003227 | (ctxt->src.val & 0x0f));
3228 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003229 return X86EMUL_CONTINUE;
3230}
3231
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003232static int em_loop(struct x86_emulate_ctxt *ctxt)
3233{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003234 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3235 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003236 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3237 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003238
3239 return X86EMUL_CONTINUE;
3240}
3241
3242static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3243{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003244 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003245 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003246
3247 return X86EMUL_CONTINUE;
3248}
3249
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003250static int em_in(struct x86_emulate_ctxt *ctxt)
3251{
3252 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3253 &ctxt->dst.val))
3254 return X86EMUL_IO_NEEDED;
3255
3256 return X86EMUL_CONTINUE;
3257}
3258
3259static int em_out(struct x86_emulate_ctxt *ctxt)
3260{
3261 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3262 &ctxt->src.val, 1);
3263 /* Disable writeback. */
3264 ctxt->dst.type = OP_NONE;
3265 return X86EMUL_CONTINUE;
3266}
3267
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003268static int em_cli(struct x86_emulate_ctxt *ctxt)
3269{
3270 if (emulator_bad_iopl(ctxt))
3271 return emulate_gp(ctxt, 0);
3272
3273 ctxt->eflags &= ~X86_EFLAGS_IF;
3274 return X86EMUL_CONTINUE;
3275}
3276
3277static int em_sti(struct x86_emulate_ctxt *ctxt)
3278{
3279 if (emulator_bad_iopl(ctxt))
3280 return emulate_gp(ctxt, 0);
3281
3282 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3283 ctxt->eflags |= X86_EFLAGS_IF;
3284 return X86EMUL_CONTINUE;
3285}
3286
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003287static int em_bt(struct x86_emulate_ctxt *ctxt)
3288{
3289 /* Disable writeback. */
3290 ctxt->dst.type = OP_NONE;
3291 /* only subword offset */
3292 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3293
3294 emulate_2op_SrcV_nobyte(ctxt, "bt");
3295 return X86EMUL_CONTINUE;
3296}
3297
3298static int em_bts(struct x86_emulate_ctxt *ctxt)
3299{
3300 emulate_2op_SrcV_nobyte(ctxt, "bts");
3301 return X86EMUL_CONTINUE;
3302}
3303
3304static int em_btr(struct x86_emulate_ctxt *ctxt)
3305{
3306 emulate_2op_SrcV_nobyte(ctxt, "btr");
3307 return X86EMUL_CONTINUE;
3308}
3309
3310static int em_btc(struct x86_emulate_ctxt *ctxt)
3311{
3312 emulate_2op_SrcV_nobyte(ctxt, "btc");
3313 return X86EMUL_CONTINUE;
3314}
3315
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003316static int em_bsf(struct x86_emulate_ctxt *ctxt)
3317{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003318 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003319 return X86EMUL_CONTINUE;
3320}
3321
3322static int em_bsr(struct x86_emulate_ctxt *ctxt)
3323{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003324 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003325 return X86EMUL_CONTINUE;
3326}
3327
Avi Kivity6d6eede2012-06-07 14:11:36 +03003328static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3329{
3330 u32 eax, ebx, ecx, edx;
3331
Avi Kivitydd856ef2012-08-27 23:46:17 +03003332 eax = reg_read(ctxt, VCPU_REGS_RAX);
3333 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003334 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003335 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3336 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3337 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3338 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003339 return X86EMUL_CONTINUE;
3340}
3341
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003342static int em_lahf(struct x86_emulate_ctxt *ctxt)
3343{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003344 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3345 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003346 return X86EMUL_CONTINUE;
3347}
3348
Avi Kivity92998362012-06-13 12:25:06 +03003349static int em_bswap(struct x86_emulate_ctxt *ctxt)
3350{
3351 switch (ctxt->op_bytes) {
3352#ifdef CONFIG_X86_64
3353 case 8:
3354 asm("bswap %0" : "+r"(ctxt->dst.val));
3355 break;
3356#endif
3357 default:
3358 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3359 break;
3360 }
3361 return X86EMUL_CONTINUE;
3362}
3363
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003364static bool valid_cr(int nr)
3365{
3366 switch (nr) {
3367 case 0:
3368 case 2 ... 4:
3369 case 8:
3370 return true;
3371 default:
3372 return false;
3373 }
3374}
3375
3376static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3377{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003378 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003379 return emulate_ud(ctxt);
3380
3381 return X86EMUL_CONTINUE;
3382}
3383
3384static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3385{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003386 u64 new_val = ctxt->src.val64;
3387 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003388 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003389
3390 static u64 cr_reserved_bits[] = {
3391 0xffffffff00000000ULL,
3392 0, 0, 0, /* CR3 checked later */
3393 CR4_RESERVED_BITS,
3394 0, 0, 0,
3395 CR8_RESERVED_BITS,
3396 };
3397
3398 if (!valid_cr(cr))
3399 return emulate_ud(ctxt);
3400
3401 if (new_val & cr_reserved_bits[cr])
3402 return emulate_gp(ctxt, 0);
3403
3404 switch (cr) {
3405 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003406 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003407 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3408 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3409 return emulate_gp(ctxt, 0);
3410
Avi Kivity717746e2011-04-20 13:37:53 +03003411 cr4 = ctxt->ops->get_cr(ctxt, 4);
3412 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003413
3414 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3415 !(cr4 & X86_CR4_PAE))
3416 return emulate_gp(ctxt, 0);
3417
3418 break;
3419 }
3420 case 3: {
3421 u64 rsvd = 0;
3422
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003423 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3424 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003425 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003426 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003427 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003428 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003429 rsvd = CR3_NONPAE_RESERVED_BITS;
3430
3431 if (new_val & rsvd)
3432 return emulate_gp(ctxt, 0);
3433
3434 break;
3435 }
3436 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003437 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003438
3439 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3440 return emulate_gp(ctxt, 0);
3441
3442 break;
3443 }
3444 }
3445
3446 return X86EMUL_CONTINUE;
3447}
3448
Joerg Roedel3b88e412011-04-04 12:39:29 +02003449static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3450{
3451 unsigned long dr7;
3452
Avi Kivity717746e2011-04-20 13:37:53 +03003453 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003454
3455 /* Check if DR7.Global_Enable is set */
3456 return dr7 & (1 << 13);
3457}
3458
3459static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3460{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003461 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003462 u64 cr4;
3463
3464 if (dr > 7)
3465 return emulate_ud(ctxt);
3466
Avi Kivity717746e2011-04-20 13:37:53 +03003467 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003468 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3469 return emulate_ud(ctxt);
3470
3471 if (check_dr7_gd(ctxt))
3472 return emulate_db(ctxt);
3473
3474 return X86EMUL_CONTINUE;
3475}
3476
3477static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3478{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003479 u64 new_val = ctxt->src.val64;
3480 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003481
3482 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3483 return emulate_gp(ctxt, 0);
3484
3485 return check_dr_read(ctxt);
3486}
3487
Joerg Roedel01de8b02011-04-04 12:39:31 +02003488static int check_svme(struct x86_emulate_ctxt *ctxt)
3489{
3490 u64 efer;
3491
Avi Kivity717746e2011-04-20 13:37:53 +03003492 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003493
3494 if (!(efer & EFER_SVME))
3495 return emulate_ud(ctxt);
3496
3497 return X86EMUL_CONTINUE;
3498}
3499
3500static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3501{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003502 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003503
3504 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003505 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003506 return emulate_gp(ctxt, 0);
3507
3508 return check_svme(ctxt);
3509}
3510
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003511static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3512{
Avi Kivity717746e2011-04-20 13:37:53 +03003513 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003514
Avi Kivity717746e2011-04-20 13:37:53 +03003515 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003516 return emulate_ud(ctxt);
3517
3518 return X86EMUL_CONTINUE;
3519}
3520
Joerg Roedel80612522011-04-04 12:39:33 +02003521static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3522{
Avi Kivity717746e2011-04-20 13:37:53 +03003523 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003524 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003525
Avi Kivity717746e2011-04-20 13:37:53 +03003526 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003527 (rcx > 3))
3528 return emulate_gp(ctxt, 0);
3529
3530 return X86EMUL_CONTINUE;
3531}
3532
Joerg Roedelf6511932011-04-04 12:39:35 +02003533static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3534{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003535 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3536 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003537 return emulate_gp(ctxt, 0);
3538
3539 return X86EMUL_CONTINUE;
3540}
3541
3542static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3543{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003544 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3545 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003546 return emulate_gp(ctxt, 0);
3547
3548 return X86EMUL_CONTINUE;
3549}
3550
Avi Kivity73fba5f2010-07-29 15:11:53 +03003551#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003552#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003553#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3554 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003555#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003556#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003557#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3558#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003559#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003560#define II(_f, _e, _i) \
3561 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003562#define IIP(_f, _e, _i, _p) \
3563 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3564 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003565#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003566
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003567#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003568#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003569#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003570#define I2bvIP(_f, _e, _i, _p) \
3571 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003572
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003573#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3574 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3575 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003576
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003577static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003578 DI(SrcNone | Priv, monitor),
3579 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003580 N, N, N, N, N, N,
3581};
3582
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003583static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003584 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3585 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3586 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3587 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3588 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3589 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3590 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3591 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003592};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003593
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003594static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003595 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003596 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003597 N, N, N, N, N, N,
3598};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003599
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003600static const struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003601 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003602 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003603 I(Lock, em_adc),
3604 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003605 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003606 I(Lock, em_sub),
3607 I(Lock, em_xor),
3608 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003609};
3610
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003611static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003612 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003613};
3614
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003615static const struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003616 I(DstMem | SrcImm, em_test),
3617 I(DstMem | SrcImm, em_test),
3618 I(DstMem | SrcNone | Lock, em_not),
3619 I(DstMem | SrcNone | Lock, em_neg),
3620 I(SrcMem, em_mul_ex),
3621 I(SrcMem, em_imul_ex),
3622 I(SrcMem, em_div_ex),
3623 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003624};
3625
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003626static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003627 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3628 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003629 N, N, N, N, N, N,
3630};
3631
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003632static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003633 I(DstMem | SrcNone | Lock, em_grp45),
3634 I(DstMem | SrcNone | Lock, em_grp45),
3635 I(SrcMem | Stack, em_grp45),
3636 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3637 I(SrcMem | Stack, em_grp45),
3638 I(SrcMemFAddr | ImplicitOps, em_grp45),
3639 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003640};
3641
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003642static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003643 DI(Prot, sldt),
3644 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003645 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003646 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003647 N, N, N, N,
3648};
3649
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003650static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003651 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3652 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003653 II(SrcMem | Priv, em_lgdt, lgdt),
3654 II(SrcMem | Priv, em_lidt, lidt),
3655 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3656 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3657 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003658}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003659 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003660 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003661 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003662 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3663 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3664 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003665} };
3666
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003667static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003668 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003669 I(DstMem | SrcImmByte, em_bt),
3670 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3671 I(DstMem | SrcImmByte | Lock, em_btr),
3672 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003673};
3674
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003675static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003676 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003677}, {
3678 N, N, N, N, N, N, N, N,
3679} };
3680
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003681static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003682 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003683 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003684};
3685
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003686static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003687 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003688};
3689
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003690static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003691 I(0, em_mov), N, N, N,
3692};
3693
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003694static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003695 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003696 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003697 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3698 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003699 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003700 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003701 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3702 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003703 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003704 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003705 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3706 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003707 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003708 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003709 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3710 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003711 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003712 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003713 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003714 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003715 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003716 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003717 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003718 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003719 /* 0x40 - 0x4F */
3720 X16(D(DstReg)),
3721 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003722 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003723 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003724 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003725 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003726 I(ImplicitOps | Stack | No64, em_pusha),
3727 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003728 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3729 N, N, N, N,
3730 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003731 I(SrcImm | Mov | Stack, em_push),
3732 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003733 I(SrcImmByte | Mov | Stack, em_push),
3734 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003735 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3736 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003737 /* 0x70 - 0x7F */
3738 X16(D(SrcImmByte)),
3739 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003740 G(ByteOp | DstMem | SrcImm, group1),
3741 G(DstMem | SrcImm, group1),
3742 G(ByteOp | DstMem | SrcImm | No64, group1),
3743 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003744 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003745 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003746 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003747 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003748 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003749 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003750 D(ModRM | SrcMem | NoAccess | DstReg),
3751 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3752 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003753 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003754 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003755 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003756 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003757 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003758 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003759 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003760 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003761 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003762 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003763 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003764 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003765 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003766 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003767 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3768 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003769 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003770 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003771 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003772 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003773 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003774 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003775 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003776 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003777 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003778 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3779 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003780 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003782 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3783 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003784 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003785 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003786 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003787 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003788 N, N, N, N,
3789 /* 0xD8 - 0xDF */
3790 N, N, N, N, N, N, N, N,
3791 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003792 X3(I(SrcImmByte, em_loop)),
3793 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003794 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3795 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003796 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003797 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003798 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003799 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3800 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003801 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003802 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003803 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3804 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003805 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003806 D(ImplicitOps), D(ImplicitOps),
3807 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003808 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3809};
3810
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003811static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003812 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003813 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003814 N, I(ImplicitOps | VendorSpecific, em_syscall),
3815 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003816 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003817 N, D(ImplicitOps | ModRM), N, N,
3818 /* 0x10 - 0x1F */
3819 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3820 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003821 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003822 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003823 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3824 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003825 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003826 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3827 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003828 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003829 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003830 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003831 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003832 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003833 I(ImplicitOps | VendorSpecific, em_sysenter),
3834 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003835 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003836 N, N, N, N, N, N, N, N,
3837 /* 0x40 - 0x4F */
3838 X16(D(DstReg | SrcMem | ModRM | Mov)),
3839 /* 0x50 - 0x5F */
3840 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3841 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003842 N, N, N, N,
3843 N, N, N, N,
3844 N, N, N, N,
3845 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003846 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003847 N, N, N, N,
3848 N, N, N, N,
3849 N, N, N, N,
3850 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003851 /* 0x80 - 0x8F */
3852 X16(D(SrcImm)),
3853 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003854 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003855 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003856 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003857 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003858 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3859 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3860 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003861 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003862 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003863 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003864 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3865 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003866 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003867 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003868 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003869 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003870 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003871 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3872 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003873 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003874 /* 0xB8 - 0xBF */
3875 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003876 G(BitOp, group8),
3877 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003878 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003879 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003880 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003881 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003882 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003884 /* 0xC8 - 0xCF */
3885 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886 /* 0xD0 - 0xDF */
3887 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3888 /* 0xE0 - 0xEF */
3889 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3890 /* 0xF0 - 0xFF */
3891 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3892};
3893
3894#undef D
3895#undef N
3896#undef G
3897#undef GD
3898#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003899#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003900#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003902#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003903#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003904#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003905#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003906#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003907
Avi Kivity9dac77f2011-06-01 15:34:25 +03003908static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003909{
3910 unsigned size;
3911
Avi Kivity9dac77f2011-06-01 15:34:25 +03003912 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003913 if (size == 8)
3914 size = 4;
3915 return size;
3916}
3917
3918static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3919 unsigned size, bool sign_extension)
3920{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003921 int rc = X86EMUL_CONTINUE;
3922
3923 op->type = OP_IMM;
3924 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003925 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003926 /* NB. Immediates are sign-extended as necessary. */
3927 switch (op->bytes) {
3928 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003929 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003930 break;
3931 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003932 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003933 break;
3934 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003935 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003936 break;
3937 }
3938 if (!sign_extension) {
3939 switch (op->bytes) {
3940 case 1:
3941 op->val &= 0xff;
3942 break;
3943 case 2:
3944 op->val &= 0xffff;
3945 break;
3946 case 4:
3947 op->val &= 0xffffffff;
3948 break;
3949 }
3950 }
3951done:
3952 return rc;
3953}
3954
Avi Kivitya9945542011-09-13 10:45:41 +03003955static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3956 unsigned d)
3957{
3958 int rc = X86EMUL_CONTINUE;
3959
3960 switch (d) {
3961 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003962 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03003963 break;
3964 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003965 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003966 break;
3967 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003968 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003969 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003970 *op = ctxt->memop;
3971 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003972 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003973 fetch_bit_operand(ctxt);
3974 op->orig_val = op->val;
3975 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003976 case OpMem64:
3977 ctxt->memop.bytes = 8;
3978 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003979 case OpAcc:
3980 op->type = OP_REG;
3981 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003982 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03003983 fetch_register_operand(op);
3984 op->orig_val = op->val;
3985 break;
3986 case OpDI:
3987 op->type = OP_MEM;
3988 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3989 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03003990 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03003991 op->addr.mem.seg = VCPU_SREG_ES;
3992 op->val = 0;
3993 break;
3994 case OpDX:
3995 op->type = OP_REG;
3996 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003997 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03003998 fetch_register_operand(op);
3999 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004000 case OpCL:
4001 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004002 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004003 break;
4004 case OpImmByte:
4005 rc = decode_imm(ctxt, op, 1, true);
4006 break;
4007 case OpOne:
4008 op->bytes = 1;
4009 op->val = 1;
4010 break;
4011 case OpImm:
4012 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4013 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004014 case OpMem8:
4015 ctxt->memop.bytes = 1;
4016 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004017 case OpMem16:
4018 ctxt->memop.bytes = 2;
4019 goto mem_common;
4020 case OpMem32:
4021 ctxt->memop.bytes = 4;
4022 goto mem_common;
4023 case OpImmU16:
4024 rc = decode_imm(ctxt, op, 2, false);
4025 break;
4026 case OpImmU:
4027 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4028 break;
4029 case OpSI:
4030 op->type = OP_MEM;
4031 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4032 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004033 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004034 op->addr.mem.seg = seg_override(ctxt);
4035 op->val = 0;
4036 break;
4037 case OpImmFAddr:
4038 op->type = OP_IMM;
4039 op->addr.mem.ea = ctxt->_eip;
4040 op->bytes = ctxt->op_bytes + 2;
4041 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4042 break;
4043 case OpMemFAddr:
4044 ctxt->memop.bytes = ctxt->op_bytes + 2;
4045 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004046 case OpES:
4047 op->val = VCPU_SREG_ES;
4048 break;
4049 case OpCS:
4050 op->val = VCPU_SREG_CS;
4051 break;
4052 case OpSS:
4053 op->val = VCPU_SREG_SS;
4054 break;
4055 case OpDS:
4056 op->val = VCPU_SREG_DS;
4057 break;
4058 case OpFS:
4059 op->val = VCPU_SREG_FS;
4060 break;
4061 case OpGS:
4062 op->val = VCPU_SREG_GS;
4063 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004064 case OpImplicit:
4065 /* Special instructions do their own operand decoding. */
4066 default:
4067 op->type = OP_NONE; /* Disable writeback. */
4068 break;
4069 }
4070
4071done:
4072 return rc;
4073}
4074
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004075int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004076{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004077 int rc = X86EMUL_CONTINUE;
4078 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004079 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004080 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004081 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004082
Avi Kivityf09ed832011-09-13 10:45:40 +03004083 ctxt->memop.type = OP_NONE;
4084 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004085 ctxt->_eip = ctxt->eip;
4086 ctxt->fetch.start = ctxt->_eip;
4087 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004088 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004089 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004090
4091 switch (mode) {
4092 case X86EMUL_MODE_REAL:
4093 case X86EMUL_MODE_VM86:
4094 case X86EMUL_MODE_PROT16:
4095 def_op_bytes = def_ad_bytes = 2;
4096 break;
4097 case X86EMUL_MODE_PROT32:
4098 def_op_bytes = def_ad_bytes = 4;
4099 break;
4100#ifdef CONFIG_X86_64
4101 case X86EMUL_MODE_PROT64:
4102 def_op_bytes = 4;
4103 def_ad_bytes = 8;
4104 break;
4105#endif
4106 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004107 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004108 }
4109
Avi Kivity9dac77f2011-06-01 15:34:25 +03004110 ctxt->op_bytes = def_op_bytes;
4111 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004112
4113 /* Legacy prefixes. */
4114 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004115 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004116 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004117 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004118 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004119 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004120 break;
4121 case 0x67: /* address-size override */
4122 if (mode == X86EMUL_MODE_PROT64)
4123 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004124 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004125 else
4126 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004127 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004128 break;
4129 case 0x26: /* ES override */
4130 case 0x2e: /* CS override */
4131 case 0x36: /* SS override */
4132 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004133 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004134 break;
4135 case 0x64: /* FS override */
4136 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004137 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004138 break;
4139 case 0x40 ... 0x4f: /* REX */
4140 if (mode != X86EMUL_MODE_PROT64)
4141 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004142 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004143 continue;
4144 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004145 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004146 break;
4147 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004148 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004149 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004150 break;
4151 default:
4152 goto done_prefixes;
4153 }
4154
4155 /* Any legacy prefix after a REX prefix nullifies its effect. */
4156
Avi Kivity9dac77f2011-06-01 15:34:25 +03004157 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004158 }
4159
4160done_prefixes:
4161
4162 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004163 if (ctxt->rex_prefix & 8)
4164 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004165
4166 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004167 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004168 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004169 if (ctxt->b == 0x0f) {
4170 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004171 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004172 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004173 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004174 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004175
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004176 if (ctxt->d & ModRM)
4177 ctxt->modrm = insn_fetch(u8, ctxt);
4178
Avi Kivity9dac77f2011-06-01 15:34:25 +03004179 while (ctxt->d & GroupMask) {
4180 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004181 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004182 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004183 opcode = opcode.u.group[goffset];
4184 break;
4185 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004186 goffset = (ctxt->modrm >> 3) & 7;
4187 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004188 opcode = opcode.u.gdual->mod3[goffset];
4189 else
4190 opcode = opcode.u.gdual->mod012[goffset];
4191 break;
4192 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004193 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004194 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004195 break;
4196 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004197 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004198 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004199 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004200 switch (simd_prefix) {
4201 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4202 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4203 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4204 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4205 }
4206 break;
4207 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004208 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004209 }
Avi Kivity46561642011-04-24 14:09:59 +03004210
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004211 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004212 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004213 }
4214
Avi Kivity9dac77f2011-06-01 15:34:25 +03004215 ctxt->execute = opcode.u.execute;
4216 ctxt->check_perm = opcode.check_perm;
4217 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004218
4219 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004220 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004221 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004222
Avi Kivity9dac77f2011-06-01 15:34:25 +03004223 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004224 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004225
Avi Kivity9dac77f2011-06-01 15:34:25 +03004226 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4227 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004228
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004230 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004231 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004232 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004233 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004234 }
4235
Avi Kivity9dac77f2011-06-01 15:34:25 +03004236 if (ctxt->d & Sse)
4237 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004238 else if (ctxt->d & Mmx)
4239 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004240
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004241 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004242 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004243 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004244 if (!ctxt->has_seg_override)
4245 set_seg_override(ctxt, ctxt->modrm_seg);
4246 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004247 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004248 if (rc != X86EMUL_CONTINUE)
4249 goto done;
4250
Avi Kivity9dac77f2011-06-01 15:34:25 +03004251 if (!ctxt->has_seg_override)
4252 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004253
Avi Kivityf09ed832011-09-13 10:45:40 +03004254 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004255
Avi Kivityf09ed832011-09-13 10:45:40 +03004256 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4257 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004258
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004259 /*
4260 * Decode and fetch the source operand: register, memory
4261 * or immediate.
4262 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004263 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004264 if (rc != X86EMUL_CONTINUE)
4265 goto done;
4266
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267 /*
4268 * Decode and fetch the second source operand: register, memory
4269 * or immediate.
4270 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004271 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004272 if (rc != X86EMUL_CONTINUE)
4273 goto done;
4274
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004276 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004277
4278done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004279 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4280 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004281
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004282 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004283}
4284
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004285bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4286{
4287 return ctxt->d & PageTable;
4288}
4289
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004290static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4291{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004292 /* The second termination condition only applies for REPE
4293 * and REPNE. Test if the repeat string operation prefix is
4294 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4295 * corresponding termination condition according to:
4296 * - if REPE/REPZ and ZF = 0 then done
4297 * - if REPNE/REPNZ and ZF = 1 then done
4298 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004299 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4300 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4301 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004302 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004303 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004304 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4305 return true;
4306
4307 return false;
4308}
4309
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004310static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4311{
4312 bool fault = false;
4313
4314 ctxt->ops->get_fpu(ctxt);
4315 asm volatile("1: fwait \n\t"
4316 "2: \n\t"
4317 ".pushsection .fixup,\"ax\" \n\t"
4318 "3: \n\t"
4319 "movb $1, %[fault] \n\t"
4320 "jmp 2b \n\t"
4321 ".popsection \n\t"
4322 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004323 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004324 ctxt->ops->put_fpu(ctxt);
4325
4326 if (unlikely(fault))
4327 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4328
4329 return X86EMUL_CONTINUE;
4330}
4331
4332static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4333 struct operand *op)
4334{
4335 if (op->type == OP_MM)
4336 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4337}
4338
Avi Kivitydd856ef2012-08-27 23:46:17 +03004339
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004340int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004341{
Mathias Krause0225fb52012-08-30 01:30:16 +02004342 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004343 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004344 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004345
Avi Kivity9dac77f2011-06-01 15:34:25 +03004346 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004347
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004349 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004350 goto done;
4351 }
4352
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004353 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004354 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004355 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004356 goto done;
4357 }
4358
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004360 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004361 goto done;
4362 }
4363
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004364 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4365 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004366 rc = emulate_ud(ctxt);
4367 goto done;
4368 }
4369
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004370 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004371 rc = emulate_nm(ctxt);
4372 goto done;
4373 }
4374
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004375 if (ctxt->d & Mmx) {
4376 rc = flush_pending_x87_faults(ctxt);
4377 if (rc != X86EMUL_CONTINUE)
4378 goto done;
4379 /*
4380 * Now that we know the fpu is exception safe, we can fetch
4381 * operands from it.
4382 */
4383 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4384 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4385 if (!(ctxt->d & Mov))
4386 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4387 }
4388
Avi Kivity9dac77f2011-06-01 15:34:25 +03004389 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4390 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004391 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004392 if (rc != X86EMUL_CONTINUE)
4393 goto done;
4394 }
4395
Gleb Natapove92805a2010-02-10 14:21:35 +02004396 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004398 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004399 goto done;
4400 }
4401
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004402 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004403 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004404 rc = emulate_ud(ctxt);
4405 goto done;
4406 }
4407
Joerg Roedeld09beab2011-04-04 12:39:25 +02004408 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004409 if (ctxt->check_perm) {
4410 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004411 if (rc != X86EMUL_CONTINUE)
4412 goto done;
4413 }
4414
Avi Kivity9dac77f2011-06-01 15:34:25 +03004415 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4416 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004417 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004418 if (rc != X86EMUL_CONTINUE)
4419 goto done;
4420 }
4421
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004423 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004424 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004425 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004426 goto done;
4427 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004428 }
4429
Avi Kivity9dac77f2011-06-01 15:34:25 +03004430 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4431 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4432 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004433 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004434 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004436 }
4437
Avi Kivity9dac77f2011-06-01 15:34:25 +03004438 if (ctxt->src2.type == OP_MEM) {
4439 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4440 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004441 if (rc != X86EMUL_CONTINUE)
4442 goto done;
4443 }
4444
Avi Kivity9dac77f2011-06-01 15:34:25 +03004445 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004446 goto special_insn;
4447
4448
Avi Kivity9dac77f2011-06-01 15:34:25 +03004449 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004450 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004451 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4452 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004453 if (rc != X86EMUL_CONTINUE)
4454 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004455 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004456 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004457
Avi Kivity018a98d2007-11-27 19:30:56 +02004458special_insn:
4459
Avi Kivity9dac77f2011-06-01 15:34:25 +03004460 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4461 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004462 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004463 if (rc != X86EMUL_CONTINUE)
4464 goto done;
4465 }
4466
Avi Kivity9dac77f2011-06-01 15:34:25 +03004467 if (ctxt->execute) {
4468 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004469 if (rc != X86EMUL_CONTINUE)
4470 goto done;
4471 goto writeback;
4472 }
4473
Avi Kivity9dac77f2011-06-01 15:34:25 +03004474 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475 goto twobyte_insn;
4476
Avi Kivity9dac77f2011-06-01 15:34:25 +03004477 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004478 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004479 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004480 break;
4481 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004482 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004483 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004485 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004486 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004487 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004488 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004489 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004490 if (test_cc(ctxt->b, ctxt->eflags))
4491 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004492 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004493 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004495 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004496 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004497 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004498 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004499 rc = em_xchg(ctxt);
4500 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004501 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004502 switch (ctxt->op_bytes) {
4503 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4504 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4505 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004506 }
4507 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004508 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004509 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004510 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004511 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004512 rc = emulate_int(ctxt, 3);
4513 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004514 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004515 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004516 break;
4517 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004518 if (ctxt->eflags & EFLG_OF)
4519 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004520 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004521 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004522 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004523 break;
4524 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004525 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004526 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004527 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004528 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004529 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 jmp_rel(ctxt, ctxt->src.val);
4531 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004532 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004533 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004534 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004535 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004536 case 0xf5: /* cmc */
4537 /* complement carry flag from eflags reg */
4538 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004539 break;
4540 case 0xf8: /* clc */
4541 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004542 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004543 case 0xf9: /* stc */
4544 ctxt->eflags |= EFLG_CF;
4545 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004546 case 0xfc: /* cld */
4547 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004548 break;
4549 case 0xfd: /* std */
4550 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004551 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004552 default:
4553 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004554 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004555
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004556 if (rc != X86EMUL_CONTINUE)
4557 goto done;
4558
Avi Kivity018a98d2007-11-27 19:30:56 +02004559writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004560 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004561 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004562 goto done;
4563
Gleb Natapov5cd21912010-03-18 15:20:26 +02004564 /*
4565 * restore dst type in case the decoding will be reused
4566 * (happens for string instruction )
4567 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004568 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004569
Avi Kivity9dac77f2011-06-01 15:34:25 +03004570 if ((ctxt->d & SrcMask) == SrcSI)
4571 string_addr_inc(ctxt, seg_override(ctxt),
4572 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004573
Avi Kivity9dac77f2011-06-01 15:34:25 +03004574 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004575 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004576 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004577
Avi Kivity9dac77f2011-06-01 15:34:25 +03004578 if (ctxt->rep_prefix && (ctxt->d & String)) {
4579 struct read_cache *r = &ctxt->io_read;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004580 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004581
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004582 if (!string_insn_completed(ctxt)) {
4583 /*
4584 * Re-enter guest when pio read ahead buffer is empty
4585 * or, if it is not used, after each 1024 iteration.
4586 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004587 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004588 (r->end == 0 || r->end != r->pos)) {
4589 /*
4590 * Reset read cache. Usually happens before
4591 * decode, but since instruction is restarted
4592 * we have to do it here.
4593 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004594 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004595 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004596 return EMULATION_RESTART;
4597 }
4598 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004599 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004600 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004601
Avi Kivity9dac77f2011-06-01 15:34:25 +03004602 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004603
4604done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004605 if (rc == X86EMUL_PROPAGATE_FAULT)
4606 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004607 if (rc == X86EMUL_INTERCEPTED)
4608 return EMULATION_INTERCEPTED;
4609
Avi Kivitydd856ef2012-08-27 23:46:17 +03004610 if (rc == X86EMUL_CONTINUE)
4611 writeback_registers(ctxt);
4612
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004613 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614
4615twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004617 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004618 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004619 break;
4620 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004621 case 0x0d: /* GrpP (prefetch) */
4622 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004623 break;
4624 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004625 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004626 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004627 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004628 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4632 if (!test_cc(ctxt->b, ctxt->eflags))
4633 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004635 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004636 if (test_cc(ctxt->b, ctxt->eflags))
4637 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004638 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004639 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004640 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004641 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004642 case 0xa4: /* shld imm8, r, r/m */
4643 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004644 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004645 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004646 case 0xac: /* shrd imm8, r, r/m */
4647 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004648 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004649 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004650 case 0xae: /* clflush */
4651 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004652 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004653 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004654 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004655 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004657 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004659 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004660 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004661 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004662 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004663 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004664 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004665 ctxt->src.val = ctxt->dst.orig_val;
4666 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004667 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004668 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004669 ctxt->dst.bytes = ctxt->op_bytes;
4670 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4671 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004672 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004673 default:
4674 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004676
4677 if (rc != X86EMUL_CONTINUE)
4678 goto done;
4679
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680 goto writeback;
4681
4682cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004683 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004684}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004685
4686void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4687{
4688 invalidate_registers(ctxt);
4689}
4690
4691void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4692{
4693 writeback_registers(ctxt);
4694}