blob: 5e27ba532613ea6e5963289c33784f19c9e243b0 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya99455492011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020060#define OpMem8 26ull /* 8-bit zero extended memory operand */
Avi Kivitya99455492011-09-13 10:45:41 +030061
Avi Kivity0fe59122011-09-13 10:45:47 +030062#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030063#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030064
65/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080066 * Opcode effective-address decode tables.
67 * Note that we only emulate instructions that have at least one memory
68 * operand (excluding implicit stack references). We assume that stack
69 * references and instruction fetches will never occur in special memory
70 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
71 * not be handled.
72 */
73
74/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030075#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030077#define DstShift 1
78#define ImplicitOps (OpImplicit << DstShift)
79#define DstReg (OpReg << DstShift)
80#define DstMem (OpMem << DstShift)
81#define DstAcc (OpAcc << DstShift)
82#define DstDI (OpDI << DstShift)
83#define DstMem64 (OpMem64 << DstShift)
84#define DstImmUByte (OpImmUByte << DstShift)
85#define DstDX (OpDX << DstShift)
86#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030088#define SrcShift 6
89#define SrcNone (OpNone << SrcShift)
90#define SrcReg (OpReg << SrcShift)
91#define SrcMem (OpMem << SrcShift)
92#define SrcMem16 (OpMem16 << SrcShift)
93#define SrcMem32 (OpMem32 << SrcShift)
94#define SrcImm (OpImm << SrcShift)
95#define SrcImmByte (OpImmByte << SrcShift)
96#define SrcOne (OpOne << SrcShift)
97#define SrcImmUByte (OpImmUByte << SrcShift)
98#define SrcImmU (OpImmU << SrcShift)
99#define SrcSI (OpSI << SrcShift)
100#define SrcImmFAddr (OpImmFAddr << SrcShift)
101#define SrcMemFAddr (OpMemFAddr << SrcShift)
102#define SrcAcc (OpAcc << SrcShift)
103#define SrcImmU16 (OpImmU16 << SrcShift)
104#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200105#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300106#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300107#define BitOp (1<<11)
108#define MemAbs (1<<12) /* Memory operand is absolute displacement */
109#define String (1<<13) /* String instruction (rep capable) */
110#define Stack (1<<14) /* Stack instruction (push/pop) */
111#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
112#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
113#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
114#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
115#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
116#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300117/* Generic ModRM decode. */
118#define ModRM (1<<19)
119/* Destination is only written; never read. */
120#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300121/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200122#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200123#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300124#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300125#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300126#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200127#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200128#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300129#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800130#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100131/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800132#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300133#define Src2None (OpNone << Src2Shift)
134#define Src2CL (OpCL << Src2Shift)
135#define Src2ImmByte (OpImmByte << Src2Shift)
136#define Src2One (OpOne << Src2Shift)
137#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300138#define Src2ES (OpES << Src2Shift)
139#define Src2CS (OpCS << Src2Shift)
140#define Src2SS (OpSS << Src2Shift)
141#define Src2DS (OpDS << Src2Shift)
142#define Src2FS (OpFS << Src2Shift)
143#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300144#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300145#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300146#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
147#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
148#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149
Avi Kivityd0e53322010-07-29 15:11:54 +0300150#define X2(x...) x, x
151#define X3(x...) X2(x), x
152#define X4(x...) X2(x), X2(x)
153#define X5(x...) X4(x), x
154#define X6(x...) X4(x), X2(x)
155#define X7(x...) X4(x), X3(x)
156#define X8(x...) X4(x), X4(x)
157#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300158
Avi Kivityd65b1de2010-07-29 15:11:35 +0300159struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300160 u64 flags : 56;
161 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300162 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300163 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300164 struct opcode *group;
165 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200166 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300167 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200168 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300169};
170
171struct group_dual {
172 struct opcode mod012[8];
173 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300174};
175
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200176struct gprefix {
177 struct opcode pfx_no;
178 struct opcode pfx_66;
179 struct opcode pfx_f2;
180 struct opcode pfx_f3;
181};
182
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200184#define EFLG_ID (1<<21)
185#define EFLG_VIP (1<<20)
186#define EFLG_VIF (1<<19)
187#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200188#define EFLG_VM (1<<17)
189#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200190#define EFLG_IOPL (3<<12)
191#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192#define EFLG_OF (1<<11)
193#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200194#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200195#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196#define EFLG_SF (1<<7)
197#define EFLG_ZF (1<<6)
198#define EFLG_AF (1<<4)
199#define EFLG_PF (1<<2)
200#define EFLG_CF (1<<0)
201
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300202#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
203#define EFLG_RESERVED_ONE_MASK 2
204
Avi 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) {
671 case X86EMUL_MODE_REAL:
672 break;
673 case X86EMUL_MODE_PROT64:
674 if (((signed long)la << 16) >> 16 != la)
675 return emulate_gp(ctxt, 0);
676 break;
677 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300678 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
679 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300680 if (!usable)
681 goto bad;
682 /* code segment or read-only data segment */
683 if (((desc.type & 8) || !(desc.type & 2)) && write)
684 goto bad;
685 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400686 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300687 goto bad;
688 lim = desc_limit_scaled(&desc);
689 if ((desc.type & 8) || !(desc.type & 4)) {
690 /* expand-up segment */
691 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
692 goto bad;
693 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800694 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300695 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
696 goto bad;
697 lim = desc.d ? 0xffffffff : 0xffff;
698 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
699 goto bad;
700 }
Avi Kivity717746e2011-04-20 13:37:53 +0300701 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300702 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300703 cpl = max(cpl, rpl);
704 if (!(desc.type & 8)) {
705 /* data segment */
706 if (cpl > desc.dpl)
707 goto bad;
708 } else if ((desc.type & 8) && !(desc.type & 4)) {
709 /* nonconforming code segment */
710 if (cpl != desc.dpl)
711 goto bad;
712 } else if ((desc.type & 8) && (desc.type & 4)) {
713 /* conforming code segment */
714 if (cpl < desc.dpl)
715 goto bad;
716 }
717 break;
718 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300719 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300720 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300721 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
722 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300723 *linear = la;
724 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300725bad:
726 if (addr.seg == VCPU_SREG_SS)
727 return emulate_ss(ctxt, addr.seg);
728 else
729 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300730}
731
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400732static int linearize(struct x86_emulate_ctxt *ctxt,
733 struct segmented_address addr,
734 unsigned size, bool write,
735 ulong *linear)
736{
737 return __linearize(ctxt, addr, size, write, false, linear);
738}
739
740
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200741static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
742 struct segmented_address addr,
743 void *data,
744 unsigned size)
745{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200746 int rc;
747 ulong linear;
748
Avi Kivity83b87952011-04-03 11:31:19 +0300749 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200750 if (rc != X86EMUL_CONTINUE)
751 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300752 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200753}
754
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900755/*
756 * Fetch the next byte of the instruction being emulated which is pointed to
757 * by ctxt->_eip, then increment ctxt->_eip.
758 *
759 * Also prefetch the remaining bytes of the instruction without crossing page
760 * boundary if they are not in fetch_cache yet.
761 */
762static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200763{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300764 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200765 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300766 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200767
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900768 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400769 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900770 struct segmented_address addr = { .seg = VCPU_SREG_CS,
771 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300772 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900773 size = min(15UL - cur_size,
774 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400775 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900776 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400777 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900778 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
779 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900780 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200781 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300782 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200783 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900784 *dest = fc->data[ctxt->_eip - fc->start];
785 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900786 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200787}
788
789static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900790 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200791{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900792 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200793
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200794 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900795 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200796 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200797 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900798 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900799 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200800 return rc;
801 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900802 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200803}
804
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900805/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900806#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900807({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900808 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900809 if (rc != X86EMUL_CONTINUE) \
810 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900811 (_type)_x; \
812})
813
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900814#define insn_fetch_arr(_arr, _size, _ctxt) \
815({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900816 if (rc != X86EMUL_CONTINUE) \
817 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900818})
819
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000820/*
821 * Given the 'reg' portion of a ModRM byte, and a register block, return a
822 * pointer into the block that addresses the relevant register.
823 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
824 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300825static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000826 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827{
828 void *p;
829
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300831 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
832 else
833 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 return p;
835}
836
837static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200838 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 u16 *size, unsigned long *address, int op_bytes)
840{
841 int rc;
842
843 if (op_bytes == 2)
844 op_bytes = 3;
845 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200846 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900847 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200849 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200850 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851 return rc;
852}
853
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300854static int test_cc(unsigned int condition, unsigned int flags)
855{
856 int rc = 0;
857
858 switch ((condition & 15) >> 1) {
859 case 0: /* o */
860 rc |= (flags & EFLG_OF);
861 break;
862 case 1: /* b/c/nae */
863 rc |= (flags & EFLG_CF);
864 break;
865 case 2: /* z/e */
866 rc |= (flags & EFLG_ZF);
867 break;
868 case 3: /* be/na */
869 rc |= (flags & (EFLG_CF|EFLG_ZF));
870 break;
871 case 4: /* s */
872 rc |= (flags & EFLG_SF);
873 break;
874 case 5: /* p/pe */
875 rc |= (flags & EFLG_PF);
876 break;
877 case 7: /* le/ng */
878 rc |= (flags & EFLG_ZF);
879 /* fall through */
880 case 6: /* l/nge */
881 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
882 break;
883 }
884
885 /* Odd condition identifiers (lsb == 1) have inverted sense. */
886 return (!!rc ^ (condition & 1));
887}
888
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300889static void fetch_register_operand(struct operand *op)
890{
891 switch (op->bytes) {
892 case 1:
893 op->val = *(u8 *)op->addr.reg;
894 break;
895 case 2:
896 op->val = *(u16 *)op->addr.reg;
897 break;
898 case 4:
899 op->val = *(u32 *)op->addr.reg;
900 break;
901 case 8:
902 op->val = *(u64 *)op->addr.reg;
903 break;
904 }
905}
906
Avi Kivity12537912011-03-29 11:41:27 +0200907static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
908{
909 ctxt->ops->get_fpu(ctxt);
910 switch (reg) {
911 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
912 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
913 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
914 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
915 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
916 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
917 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
918 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
919#ifdef CONFIG_X86_64
920 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
921 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
922 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
923 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
924 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
925 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
926 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
927 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
928#endif
929 default: BUG();
930 }
931 ctxt->ops->put_fpu(ctxt);
932}
933
934static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
935 int reg)
936{
937 ctxt->ops->get_fpu(ctxt);
938 switch (reg) {
939 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
940 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
941 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
942 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
943 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
944 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
945 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
946 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
947#ifdef CONFIG_X86_64
948 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
949 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
950 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
951 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
952 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
953 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
954 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
955 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
956#endif
957 default: BUG();
958 }
959 ctxt->ops->put_fpu(ctxt);
960}
961
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300962static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
963{
964 ctxt->ops->get_fpu(ctxt);
965 switch (reg) {
966 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
967 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
968 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
969 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
970 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
971 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
972 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
973 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
974 default: BUG();
975 }
976 ctxt->ops->put_fpu(ctxt);
977}
978
979static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
980{
981 ctxt->ops->get_fpu(ctxt);
982 switch (reg) {
983 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
984 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
985 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
986 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
987 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
988 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
989 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
990 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
991 default: BUG();
992 }
993 ctxt->ops->put_fpu(ctxt);
994}
995
Avi Kivity12537912011-03-29 11:41:27 +0200996static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +0200997 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +0200998{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300999 unsigned reg = ctxt->modrm_reg;
1000 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001001
Avi Kivity9dac77f2011-06-01 15:34:25 +03001002 if (!(ctxt->d & ModRM))
1003 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001004
Avi Kivity9dac77f2011-06-01 15:34:25 +03001005 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001006 op->type = OP_XMM;
1007 op->bytes = 16;
1008 op->addr.xmm = reg;
1009 read_sse_reg(ctxt, &op->vec_val, reg);
1010 return;
1011 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001012 if (ctxt->d & Mmx) {
1013 reg &= 7;
1014 op->type = OP_MM;
1015 op->bytes = 8;
1016 op->addr.mm = reg;
1017 return;
1018 }
Avi Kivity12537912011-03-29 11:41:27 +02001019
Avi Kivity3c118e22007-10-31 10:27:04 +02001020 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001021 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001022 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001023 op->bytes = 1;
1024 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001025 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001026 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001027 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001028 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001029 op->orig_val = op->val;
1030}
1031
Avi Kivitya6e34072012-06-10 17:15:39 +03001032static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1033{
1034 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1035 ctxt->modrm_seg = VCPU_SREG_SS;
1036}
1037
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001038static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001039 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001040{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001041 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001042 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001043 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001044 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001045
Avi Kivity9dac77f2011-06-01 15:34:25 +03001046 if (ctxt->rex_prefix) {
1047 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1048 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1049 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001050 }
1051
Avi Kivity9dac77f2011-06-01 15:34:25 +03001052 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1053 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1054 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1055 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001056
Avi Kivity9dac77f2011-06-01 15:34:25 +03001057 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001058 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001059 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001060 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001061 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001062 op->type = OP_XMM;
1063 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001064 op->addr.xmm = ctxt->modrm_rm;
1065 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001066 return rc;
1067 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001068 if (ctxt->d & Mmx) {
1069 op->type = OP_MM;
1070 op->bytes = 8;
1071 op->addr.xmm = ctxt->modrm_rm & 7;
1072 return rc;
1073 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001074 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001075 return rc;
1076 }
1077
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001078 op->type = OP_MEM;
1079
Avi Kivity9dac77f2011-06-01 15:34:25 +03001080 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001081 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1082 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1083 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1084 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001085
1086 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001087 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001088 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001089 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001090 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001091 break;
1092 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001093 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001094 break;
1095 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001096 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001097 break;
1098 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001099 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001100 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001101 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001102 break;
1103 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001104 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001105 break;
1106 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001107 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001108 break;
1109 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001110 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001111 break;
1112 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001113 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001114 break;
1115 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001116 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001117 break;
1118 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001119 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001120 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001121 break;
1122 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001123 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001124 break;
1125 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001126 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1127 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1128 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001129 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001130 } else {
1131 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001133 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001134 index_reg |= (sib >> 3) & 7;
1135 base_reg |= sib & 7;
1136 scale = sib >> 6;
1137
Avi Kivity9dac77f2011-06-01 15:34:25 +03001138 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001139 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001140 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001141 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001142 adjust_modrm_seg(ctxt, base_reg);
1143 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001144 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001145 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001146 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001147 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001148 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001149 } else {
1150 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001151 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001152 adjust_modrm_seg(ctxt, base_reg);
1153 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001154 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001155 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001156 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001157 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001158 break;
1159 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001160 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001161 break;
1162 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001163 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001164 break;
1165 }
1166 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001167 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001168done:
1169 return rc;
1170}
1171
1172static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001173 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001175 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001177 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001179 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001180 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001181 break;
1182 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001183 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001184 break;
1185 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001186 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001187 break;
1188 }
1189done:
1190 return rc;
1191}
1192
Avi Kivity9dac77f2011-06-01 15:34:25 +03001193static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001194{
Sheng Yang7129eec2010-09-28 16:33:32 +08001195 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001196
Avi Kivity9dac77f2011-06-01 15:34:25 +03001197 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1198 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001199
Avi Kivity9dac77f2011-06-01 15:34:25 +03001200 if (ctxt->src.bytes == 2)
1201 sv = (s16)ctxt->src.val & (s16)mask;
1202 else if (ctxt->src.bytes == 4)
1203 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001204
Avi Kivity9dac77f2011-06-01 15:34:25 +03001205 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001206 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001207
1208 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001209 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001210}
1211
Gleb Natapov9de41572010-04-28 19:15:22 +03001212static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001213 unsigned long addr, void *dest, unsigned size)
1214{
1215 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001216 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001217
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001218 if (mc->pos < mc->end)
1219 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001220
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001221 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001222
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001223 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1224 &ctxt->exception);
1225 if (rc != X86EMUL_CONTINUE)
1226 return rc;
1227
1228 mc->end += size;
1229
1230read_cached:
1231 memcpy(dest, mc->data + mc->pos, size);
1232 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001233 return X86EMUL_CONTINUE;
1234}
1235
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001236static int segmented_read(struct x86_emulate_ctxt *ctxt,
1237 struct segmented_address addr,
1238 void *data,
1239 unsigned size)
1240{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001241 int rc;
1242 ulong linear;
1243
Avi Kivity83b87952011-04-03 11:31:19 +03001244 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001245 if (rc != X86EMUL_CONTINUE)
1246 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001247 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001248}
1249
1250static int segmented_write(struct x86_emulate_ctxt *ctxt,
1251 struct segmented_address addr,
1252 const void *data,
1253 unsigned size)
1254{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001255 int rc;
1256 ulong linear;
1257
Avi Kivity83b87952011-04-03 11:31:19 +03001258 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001259 if (rc != X86EMUL_CONTINUE)
1260 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001261 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1262 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001263}
1264
1265static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1266 struct segmented_address addr,
1267 const void *orig_data, const void *data,
1268 unsigned size)
1269{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001270 int rc;
1271 ulong linear;
1272
Avi Kivity83b87952011-04-03 11:31:19 +03001273 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001274 if (rc != X86EMUL_CONTINUE)
1275 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001276 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1277 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001278}
1279
Gleb Natapov7b262e92010-03-18 15:20:27 +02001280static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001281 unsigned int size, unsigned short port,
1282 void *dest)
1283{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001284 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001285
1286 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001287 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001288 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001289 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001290 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001291 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1292 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001293 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1294 count);
1295 if (n == 0)
1296 n = 1;
1297 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001298 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001299 return 0;
1300 rc->end = n * size;
1301 }
1302
1303 memcpy(dest, rc->data + rc->pos, size);
1304 rc->pos += size;
1305 return 1;
1306}
1307
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001308static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1309 u16 index, struct desc_struct *desc)
1310{
1311 struct desc_ptr dt;
1312 ulong addr;
1313
1314 ctxt->ops->get_idt(ctxt, &dt);
1315
1316 if (dt.size < index * 8 + 7)
1317 return emulate_gp(ctxt, index << 3 | 0x2);
1318
1319 addr = dt.address + index * 8;
1320 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1321 &ctxt->exception);
1322}
1323
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001324static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001325 u16 selector, struct desc_ptr *dt)
1326{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001327 struct x86_emulate_ops *ops = ctxt->ops;
1328
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001329 if (selector & 1 << 2) {
1330 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001331 u16 sel;
1332
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001333 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001334 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001335 return;
1336
1337 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1338 dt->address = get_desc_base(&desc);
1339 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001340 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001341}
1342
1343/* allowed just for 8 bytes segments */
1344static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001345 u16 selector, struct desc_struct *desc,
1346 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001347{
1348 struct desc_ptr dt;
1349 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001350 ulong addr;
1351
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001352 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001353
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001354 if (dt.size < index * 8 + 7)
1355 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001356
Avi Kivitye9194642012-06-13 16:29:39 +03001357 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001358 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1359 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001360}
1361
1362/* allowed just for 8 bytes segments */
1363static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001364 u16 selector, struct desc_struct *desc)
1365{
1366 struct desc_ptr dt;
1367 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001368 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001369
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001370 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001371
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001372 if (dt.size < index * 8 + 7)
1373 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001374
1375 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001376 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1377 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001378}
1379
Gleb Natapov5601d052011-03-07 14:55:06 +02001380/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001381static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001382 u16 selector, int seg)
1383{
Avi Kivity869be992012-06-13 16:30:53 +03001384 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001385 u8 dpl, rpl, cpl;
1386 unsigned err_vec = GP_VECTOR;
1387 u32 err_code = 0;
1388 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001389 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001390 int ret;
1391
1392 memset(&seg_desc, 0, sizeof seg_desc);
1393
1394 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1395 || ctxt->mode == X86EMUL_MODE_REAL) {
1396 /* set real mode segment descriptor */
1397 set_desc_base(&seg_desc, selector << 4);
1398 set_desc_limit(&seg_desc, 0xffff);
1399 seg_desc.type = 3;
1400 seg_desc.p = 1;
1401 seg_desc.s = 1;
Kevin Wolf66b0ab82012-02-08 14:34:39 +01001402 if (ctxt->mode == X86EMUL_MODE_VM86)
1403 seg_desc.dpl = 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001404 goto load;
1405 }
1406
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001407 rpl = selector & 3;
1408 cpl = ctxt->ops->cpl(ctxt);
1409
1410 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1411 if ((seg == VCPU_SREG_CS
1412 || (seg == VCPU_SREG_SS
1413 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1414 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415 && null_selector)
1416 goto exception;
1417
1418 /* TR should be in GDT only */
1419 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1420 goto exception;
1421
1422 if (null_selector) /* for NULL selector skip all following checks */
1423 goto load;
1424
Avi Kivitye9194642012-06-13 16:29:39 +03001425 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001426 if (ret != X86EMUL_CONTINUE)
1427 return ret;
1428
1429 err_code = selector & 0xfffc;
1430 err_vec = GP_VECTOR;
1431
Guo Chaofc058682012-06-28 15:19:51 +08001432 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001433 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1434 goto exception;
1435
1436 if (!seg_desc.p) {
1437 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1438 goto exception;
1439 }
1440
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001441 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442
1443 switch (seg) {
1444 case VCPU_SREG_SS:
1445 /*
1446 * segment is not a writable data segment or segment
1447 * selector's RPL != CPL or segment selector's RPL != CPL
1448 */
1449 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1450 goto exception;
1451 break;
1452 case VCPU_SREG_CS:
1453 if (!(seg_desc.type & 8))
1454 goto exception;
1455
1456 if (seg_desc.type & 4) {
1457 /* conforming */
1458 if (dpl > cpl)
1459 goto exception;
1460 } else {
1461 /* nonconforming */
1462 if (rpl > cpl || dpl != cpl)
1463 goto exception;
1464 }
1465 /* CS(RPL) <- CPL */
1466 selector = (selector & 0xfffc) | cpl;
1467 break;
1468 case VCPU_SREG_TR:
1469 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1470 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001471 old_desc = seg_desc;
1472 seg_desc.type |= 2; /* busy */
1473 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1474 sizeof(seg_desc), &ctxt->exception);
1475 if (ret != X86EMUL_CONTINUE)
1476 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477 break;
1478 case VCPU_SREG_LDTR:
1479 if (seg_desc.s || seg_desc.type != 2)
1480 goto exception;
1481 break;
1482 default: /* DS, ES, FS, or GS */
1483 /*
1484 * segment is not a data or readable code segment or
1485 * ((segment is a data or nonconforming code segment)
1486 * and (both RPL and CPL > DPL))
1487 */
1488 if ((seg_desc.type & 0xa) == 0x8 ||
1489 (((seg_desc.type & 0xc) != 0xc) &&
1490 (rpl > dpl && cpl > dpl)))
1491 goto exception;
1492 break;
1493 }
1494
1495 if (seg_desc.s) {
1496 /* mark segment as accessed */
1497 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001498 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001499 if (ret != X86EMUL_CONTINUE)
1500 return ret;
1501 }
1502load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001503 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 return X86EMUL_CONTINUE;
1505exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001506 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001507 return X86EMUL_PROPAGATE_FAULT;
1508}
1509
Wei Yongjun31be40b2010-08-17 09:17:30 +08001510static void write_register_operand(struct operand *op)
1511{
1512 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1513 switch (op->bytes) {
1514 case 1:
1515 *(u8 *)op->addr.reg = (u8)op->val;
1516 break;
1517 case 2:
1518 *(u16 *)op->addr.reg = (u16)op->val;
1519 break;
1520 case 4:
1521 *op->addr.reg = (u32)op->val;
1522 break; /* 64b: zero-extend */
1523 case 8:
1524 *op->addr.reg = op->val;
1525 break;
1526 }
1527}
1528
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001529static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001530{
1531 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001532
Avi Kivity9dac77f2011-06-01 15:34:25 +03001533 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001534 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001535 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001536 break;
1537 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001538 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001539 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001540 ctxt->dst.addr.mem,
1541 &ctxt->dst.orig_val,
1542 &ctxt->dst.val,
1543 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001544 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001545 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001546 ctxt->dst.addr.mem,
1547 &ctxt->dst.val,
1548 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001549 if (rc != X86EMUL_CONTINUE)
1550 return rc;
1551 break;
Avi Kivity12537912011-03-29 11:41:27 +02001552 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001553 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001554 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001555 case OP_MM:
1556 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1557 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001558 case OP_NONE:
1559 /* no writeback */
1560 break;
1561 default:
1562 break;
1563 }
1564 return X86EMUL_CONTINUE;
1565}
1566
Avi Kivity51ddff52012-06-12 20:19:40 +03001567static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001568{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001569 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001570
Avi Kivity5ad105e2012-08-19 14:34:31 +03001571 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001572 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001573 addr.seg = VCPU_SREG_SS;
1574
Avi Kivity51ddff52012-06-12 20:19:40 +03001575 return segmented_write(ctxt, addr, data, bytes);
1576}
1577
1578static int em_push(struct x86_emulate_ctxt *ctxt)
1579{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001580 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001581 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001582 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001583}
1584
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001585static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001586 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001587{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001588 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001589 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001590
Avi Kivitydd856ef2012-08-27 23:46:17 +03001591 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001592 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001593 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001594 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001595 return rc;
1596
Avi Kivity5ad105e2012-08-19 14:34:31 +03001597 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001598 return rc;
1599}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001600
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001601static int em_pop(struct x86_emulate_ctxt *ctxt)
1602{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001603 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001604}
1605
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001606static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001607 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001608{
1609 int rc;
1610 unsigned long val, change_mask;
1611 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001612 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001613
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001614 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001615 if (rc != X86EMUL_CONTINUE)
1616 return rc;
1617
1618 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1619 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1620
1621 switch(ctxt->mode) {
1622 case X86EMUL_MODE_PROT64:
1623 case X86EMUL_MODE_PROT32:
1624 case X86EMUL_MODE_PROT16:
1625 if (cpl == 0)
1626 change_mask |= EFLG_IOPL;
1627 if (cpl <= iopl)
1628 change_mask |= EFLG_IF;
1629 break;
1630 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001631 if (iopl < 3)
1632 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001633 change_mask |= EFLG_IF;
1634 break;
1635 default: /* real mode */
1636 change_mask |= (EFLG_IOPL | EFLG_IF);
1637 break;
1638 }
1639
1640 *(unsigned long *)dest =
1641 (ctxt->eflags & ~change_mask) | (val & change_mask);
1642
1643 return rc;
1644}
1645
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001646static int em_popf(struct x86_emulate_ctxt *ctxt)
1647{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001648 ctxt->dst.type = OP_REG;
1649 ctxt->dst.addr.reg = &ctxt->eflags;
1650 ctxt->dst.bytes = ctxt->op_bytes;
1651 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001652}
1653
Avi Kivity612e89f2012-06-12 20:03:23 +03001654static int em_enter(struct x86_emulate_ctxt *ctxt)
1655{
1656 int rc;
1657 unsigned frame_size = ctxt->src.val;
1658 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001659 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001660
1661 if (nesting_level)
1662 return X86EMUL_UNHANDLEABLE;
1663
Avi Kivitydd856ef2012-08-27 23:46:17 +03001664 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1665 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001666 if (rc != X86EMUL_CONTINUE)
1667 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001668 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001669 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001670 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1671 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001672 stack_mask(ctxt));
1673 return X86EMUL_CONTINUE;
1674}
1675
Avi Kivityf47cfa32012-06-07 17:49:24 +03001676static int em_leave(struct x86_emulate_ctxt *ctxt)
1677{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001678 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001679 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001680 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001681}
1682
Avi Kivity1cd196e2011-09-13 10:45:51 +03001683static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001684{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001685 int seg = ctxt->src2.val;
1686
Avi Kivity9dac77f2011-06-01 15:34:25 +03001687 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001688
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001689 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001690}
1691
Avi Kivity1cd196e2011-09-13 10:45:51 +03001692static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001693{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001694 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001695 unsigned long selector;
1696 int rc;
1697
Avi Kivity9dac77f2011-06-01 15:34:25 +03001698 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001699 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001700 return rc;
1701
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001702 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001703 return rc;
1704}
1705
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001706static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001707{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001708 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001709 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001710 int reg = VCPU_REGS_RAX;
1711
1712 while (reg <= VCPU_REGS_RDI) {
1713 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001714 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001715
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001716 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001717 if (rc != X86EMUL_CONTINUE)
1718 return rc;
1719
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001720 ++reg;
1721 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001722
Wei Yongjunc37eda12010-06-15 09:03:33 +08001723 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001724}
1725
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001726static int em_pushf(struct x86_emulate_ctxt *ctxt)
1727{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001728 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001729 return em_push(ctxt);
1730}
1731
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001732static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001733{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001734 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001735 int reg = VCPU_REGS_RDI;
1736
1737 while (reg >= VCPU_REGS_RAX) {
1738 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001739 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001740 --reg;
1741 }
1742
Avi Kivitydd856ef2012-08-27 23:46:17 +03001743 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001744 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001745 break;
1746 --reg;
1747 }
1748 return rc;
1749}
1750
Avi Kivitydd856ef2012-08-27 23:46:17 +03001751static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001752{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001753 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001754 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001755 struct desc_ptr dt;
1756 gva_t cs_addr;
1757 gva_t eip_addr;
1758 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001759
1760 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001761 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001762 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001763 if (rc != X86EMUL_CONTINUE)
1764 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001765
1766 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1767
Avi Kivity9dac77f2011-06-01 15:34:25 +03001768 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001769 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001770 if (rc != X86EMUL_CONTINUE)
1771 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001772
Avi Kivity9dac77f2011-06-01 15:34:25 +03001773 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001774 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001775 if (rc != X86EMUL_CONTINUE)
1776 return rc;
1777
Avi Kivity4bff1e862011-04-20 13:37:53 +03001778 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001779
1780 eip_addr = dt.address + (irq << 2);
1781 cs_addr = dt.address + (irq << 2) + 2;
1782
Avi Kivity0f65dd72011-04-20 13:37:53 +03001783 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001784 if (rc != X86EMUL_CONTINUE)
1785 return rc;
1786
Avi Kivity0f65dd72011-04-20 13:37:53 +03001787 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001788 if (rc != X86EMUL_CONTINUE)
1789 return rc;
1790
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001791 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001792 if (rc != X86EMUL_CONTINUE)
1793 return rc;
1794
Avi Kivity9dac77f2011-06-01 15:34:25 +03001795 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001796
1797 return rc;
1798}
1799
Avi Kivitydd856ef2012-08-27 23:46:17 +03001800int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1801{
1802 int rc;
1803
1804 invalidate_registers(ctxt);
1805 rc = __emulate_int_real(ctxt, irq);
1806 if (rc == X86EMUL_CONTINUE)
1807 writeback_registers(ctxt);
1808 return rc;
1809}
1810
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001811static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001812{
1813 switch(ctxt->mode) {
1814 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001815 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001816 case X86EMUL_MODE_VM86:
1817 case X86EMUL_MODE_PROT16:
1818 case X86EMUL_MODE_PROT32:
1819 case X86EMUL_MODE_PROT64:
1820 default:
1821 /* Protected mode interrupts unimplemented yet */
1822 return X86EMUL_UNHANDLEABLE;
1823 }
1824}
1825
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001826static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001827{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001828 int rc = X86EMUL_CONTINUE;
1829 unsigned long temp_eip = 0;
1830 unsigned long temp_eflags = 0;
1831 unsigned long cs = 0;
1832 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1833 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1834 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1835 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1836
1837 /* TODO: Add stack limit check */
1838
Avi Kivity9dac77f2011-06-01 15:34:25 +03001839 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001840
1841 if (rc != X86EMUL_CONTINUE)
1842 return rc;
1843
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001844 if (temp_eip & ~0xffff)
1845 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001846
Avi Kivity9dac77f2011-06-01 15:34:25 +03001847 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001848
1849 if (rc != X86EMUL_CONTINUE)
1850 return rc;
1851
Avi Kivity9dac77f2011-06-01 15:34:25 +03001852 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001853
1854 if (rc != X86EMUL_CONTINUE)
1855 return rc;
1856
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001857 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001858
1859 if (rc != X86EMUL_CONTINUE)
1860 return rc;
1861
Avi Kivity9dac77f2011-06-01 15:34:25 +03001862 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001863
1864
Avi Kivity9dac77f2011-06-01 15:34:25 +03001865 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001866 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001867 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001868 ctxt->eflags &= ~0xffff;
1869 ctxt->eflags |= temp_eflags;
1870 }
1871
1872 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1873 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1874
1875 return rc;
1876}
1877
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001878static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001879{
1880 switch(ctxt->mode) {
1881 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001882 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001883 case X86EMUL_MODE_VM86:
1884 case X86EMUL_MODE_PROT16:
1885 case X86EMUL_MODE_PROT32:
1886 case X86EMUL_MODE_PROT64:
1887 default:
1888 /* iret from protected mode unimplemented yet */
1889 return X86EMUL_UNHANDLEABLE;
1890 }
1891}
1892
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001893static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1894{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001895 int rc;
1896 unsigned short sel;
1897
Avi Kivity9dac77f2011-06-01 15:34:25 +03001898 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001899
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001900 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001901 if (rc != X86EMUL_CONTINUE)
1902 return rc;
1903
Avi Kivity9dac77f2011-06-01 15:34:25 +03001904 ctxt->_eip = 0;
1905 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001906 return X86EMUL_CONTINUE;
1907}
1908
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001909static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001910{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001911 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001912 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001913 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001914 break;
1915 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001916 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001917 break;
1918 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001919 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001920 break;
1921 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001922 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923 break;
1924 case 4: /* sal/shl */
1925 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001926 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001927 break;
1928 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001929 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001930 break;
1931 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001932 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001933 break;
1934 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001935 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001936}
1937
Avi Kivity3329ece2011-09-13 10:45:39 +03001938static int em_not(struct x86_emulate_ctxt *ctxt)
1939{
1940 ctxt->dst.val = ~ctxt->dst.val;
1941 return X86EMUL_CONTINUE;
1942}
1943
1944static int em_neg(struct x86_emulate_ctxt *ctxt)
1945{
1946 emulate_1op(ctxt, "neg");
1947 return X86EMUL_CONTINUE;
1948}
1949
1950static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1951{
1952 u8 ex = 0;
1953
1954 emulate_1op_rax_rdx(ctxt, "mul", ex);
1955 return X86EMUL_CONTINUE;
1956}
1957
1958static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1959{
1960 u8 ex = 0;
1961
1962 emulate_1op_rax_rdx(ctxt, "imul", ex);
1963 return X86EMUL_CONTINUE;
1964}
1965
1966static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001967{
Avi Kivity34d1f492010-08-26 11:59:01 +03001968 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001969
Avi Kivity3329ece2011-09-13 10:45:39 +03001970 emulate_1op_rax_rdx(ctxt, "div", de);
1971 if (de)
1972 return emulate_de(ctxt);
1973 return X86EMUL_CONTINUE;
1974}
1975
1976static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1977{
1978 u8 de = 0;
1979
1980 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001981 if (de)
1982 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001983 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001984}
1985
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001986static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001987{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001988 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001989
Avi Kivity9dac77f2011-06-01 15:34:25 +03001990 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001991 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001992 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001993 break;
1994 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001995 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001996 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001997 case 2: /* call near abs */ {
1998 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001999 old_eip = ctxt->_eip;
2000 ctxt->_eip = ctxt->src.val;
2001 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002002 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002003 break;
2004 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002005 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002006 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002007 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002008 case 5: /* jmp far */
2009 rc = em_jmp_far(ctxt);
2010 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002011 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002012 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002013 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002014 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002015 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002016}
2017
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002018static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002019{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002020 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002021
Avi Kivitydd856ef2012-08-27 23:46:17 +03002022 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2023 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2024 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2025 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002026 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002027 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002028 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2029 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002030
Laurent Vivier05f086f2007-09-24 11:10:55 +02002031 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002032 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002033 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002034}
2035
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002036static int em_ret(struct x86_emulate_ctxt *ctxt)
2037{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002038 ctxt->dst.type = OP_REG;
2039 ctxt->dst.addr.reg = &ctxt->_eip;
2040 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002041 return em_pop(ctxt);
2042}
2043
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002044static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002045{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002046 int rc;
2047 unsigned long cs;
2048
Avi Kivity9dac77f2011-06-01 15:34:25 +03002049 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002050 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002051 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002052 if (ctxt->op_bytes == 4)
2053 ctxt->_eip = (u32)ctxt->_eip;
2054 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002055 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002056 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002057 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002058 return rc;
2059}
2060
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002061static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2062{
2063 /* Save real source value, then compare EAX against destination. */
2064 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002065 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002066 emulate_2op_SrcV(ctxt, "cmp");
2067
2068 if (ctxt->eflags & EFLG_ZF) {
2069 /* Success: write back to memory. */
2070 ctxt->dst.val = ctxt->src.orig_val;
2071 } else {
2072 /* Failure: write the value we saw to EAX. */
2073 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002074 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002075 }
2076 return X86EMUL_CONTINUE;
2077}
2078
Avi Kivityd4b43252011-09-13 10:45:50 +03002079static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002080{
Avi Kivityd4b43252011-09-13 10:45:50 +03002081 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002082 unsigned short sel;
2083 int rc;
2084
Avi Kivity9dac77f2011-06-01 15:34:25 +03002085 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002086
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002087 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002088 if (rc != X86EMUL_CONTINUE)
2089 return rc;
2090
Avi Kivity9dac77f2011-06-01 15:34:25 +03002091 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002092 return rc;
2093}
2094
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002095static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002096setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002097 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002098{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002099 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002100 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002101 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002102 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002103 cs->type = 0x0b; /* Read, Execute, Accessed */
2104 cs->s = 1;
2105 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002106 cs->p = 1;
2107 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002108 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002109
Gleb Natapov79168fd2010-04-28 19:15:30 +03002110 set_desc_base(ss, 0); /* flat segment */
2111 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002112 ss->g = 1; /* 4kb granularity */
2113 ss->s = 1;
2114 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002115 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002116 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002117 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002118 ss->l = 0;
2119 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002120}
2121
Avi Kivity1a18a692012-02-01 12:23:21 +02002122static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2123{
2124 u32 eax, ebx, ecx, edx;
2125
2126 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002127 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2128 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002129 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2130 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2131}
2132
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002133static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2134{
2135 struct x86_emulate_ops *ops = ctxt->ops;
2136 u32 eax, ebx, ecx, edx;
2137
2138 /*
2139 * syscall should always be enabled in longmode - so only become
2140 * vendor specific (cpuid) if other modes are active...
2141 */
2142 if (ctxt->mode == X86EMUL_MODE_PROT64)
2143 return true;
2144
2145 eax = 0x00000000;
2146 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002147 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2148 /*
2149 * Intel ("GenuineIntel")
2150 * remark: Intel CPUs only support "syscall" in 64bit
2151 * longmode. Also an 64bit guest with a
2152 * 32bit compat-app running will #UD !! While this
2153 * behaviour can be fixed (by emulating) into AMD
2154 * response - CPUs of AMD can't behave like Intel.
2155 */
2156 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2157 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2158 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2159 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002160
Avi Kivity0017f932012-06-07 14:10:16 +03002161 /* AMD ("AuthenticAMD") */
2162 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2163 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2164 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2165 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002166
Avi Kivity0017f932012-06-07 14:10:16 +03002167 /* AMD ("AMDisbetter!") */
2168 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2169 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2170 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2171 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002172
2173 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2174 return false;
2175}
2176
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002177static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002178{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002179 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002180 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002181 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002182 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002183 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002184
2185 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002186 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002187 ctxt->mode == X86EMUL_MODE_VM86)
2188 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002189
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002190 if (!(em_syscall_is_enabled(ctxt)))
2191 return emulate_ud(ctxt);
2192
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002193 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002194 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002196 if (!(efer & EFER_SCE))
2197 return emulate_ud(ctxt);
2198
Avi Kivity717746e2011-04-20 13:37:53 +03002199 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002200 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002201 cs_sel = (u16)(msr_data & 0xfffc);
2202 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002203
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002204 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002205 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206 cs.l = 1;
2207 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002208 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2209 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002210
Avi Kivitydd856ef2012-08-27 23:46:17 +03002211 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002212 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002213#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002214 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002215
Avi Kivity717746e2011-04-20 13:37:53 +03002216 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002217 ctxt->mode == X86EMUL_MODE_PROT64 ?
2218 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002219 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220
Avi Kivity717746e2011-04-20 13:37:53 +03002221 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002222 ctxt->eflags &= ~(msr_data | EFLG_RF);
2223#endif
2224 } else {
2225 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002226 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002227 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002228
2229 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2230 }
2231
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002232 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002233}
2234
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002235static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002236{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002237 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002238 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002239 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002240 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002241 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002242
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002243 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002244 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002245 if (ctxt->mode == X86EMUL_MODE_REAL)
2246 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002247
Avi Kivity1a18a692012-02-01 12:23:21 +02002248 /*
2249 * Not recognized on AMD in compat mode (but is recognized in legacy
2250 * mode).
2251 */
2252 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2253 && !vendor_intel(ctxt))
2254 return emulate_ud(ctxt);
2255
Andre Przywara8c604352009-06-18 12:56:01 +02002256 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2257 * Therefore, we inject an #UD.
2258 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002259 if (ctxt->mode == X86EMUL_MODE_PROT64)
2260 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002261
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002262 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002263
Avi Kivity717746e2011-04-20 13:37:53 +03002264 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002265 switch (ctxt->mode) {
2266 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002267 if ((msr_data & 0xfffc) == 0x0)
2268 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002269 break;
2270 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002271 if (msr_data == 0x0)
2272 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002273 break;
2274 }
2275
2276 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002277 cs_sel = (u16)msr_data;
2278 cs_sel &= ~SELECTOR_RPL_MASK;
2279 ss_sel = cs_sel + 8;
2280 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002281 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002282 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002283 cs.l = 1;
2284 }
2285
Avi Kivity1aa36612011-04-27 13:20:30 +03002286 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2287 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002288
Avi Kivity717746e2011-04-20 13:37:53 +03002289 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002290 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002291
Avi Kivity717746e2011-04-20 13:37:53 +03002292 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002293 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002294
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002295 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002296}
2297
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002298static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002299{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002300 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002301 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002302 u64 msr_data;
2303 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002304 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002305
Gleb Natapova0044752010-02-10 14:21:31 +02002306 /* inject #GP if in real mode or Virtual 8086 mode */
2307 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002308 ctxt->mode == X86EMUL_MODE_VM86)
2309 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002310
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002311 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002312
Avi Kivity9dac77f2011-06-01 15:34:25 +03002313 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002314 usermode = X86EMUL_MODE_PROT64;
2315 else
2316 usermode = X86EMUL_MODE_PROT32;
2317
2318 cs.dpl = 3;
2319 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002320 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002321 switch (usermode) {
2322 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002323 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002324 if ((msr_data & 0xfffc) == 0x0)
2325 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002326 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002327 break;
2328 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002329 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002330 if (msr_data == 0x0)
2331 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002332 ss_sel = cs_sel + 8;
2333 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002334 cs.l = 1;
2335 break;
2336 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002337 cs_sel |= SELECTOR_RPL_MASK;
2338 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002339
Avi Kivity1aa36612011-04-27 13:20:30 +03002340 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2341 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002342
Avi Kivitydd856ef2012-08-27 23:46:17 +03002343 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2344 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002345
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002346 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002347}
2348
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002349static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002350{
2351 int iopl;
2352 if (ctxt->mode == X86EMUL_MODE_REAL)
2353 return false;
2354 if (ctxt->mode == X86EMUL_MODE_VM86)
2355 return true;
2356 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002357 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002358}
2359
2360static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002361 u16 port, u16 len)
2362{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002363 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002364 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002365 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002366 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002367 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002368 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002369 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002370
Avi Kivity1aa36612011-04-27 13:20:30 +03002371 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002372 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002373 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002374 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002375 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002376 base = get_desc_base(&tr_seg);
2377#ifdef CONFIG_X86_64
2378 base |= ((u64)base3) << 32;
2379#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002380 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002381 if (r != X86EMUL_CONTINUE)
2382 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002383 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002384 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002385 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002386 if (r != X86EMUL_CONTINUE)
2387 return false;
2388 if ((perm >> bit_idx) & mask)
2389 return false;
2390 return true;
2391}
2392
2393static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002394 u16 port, u16 len)
2395{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002396 if (ctxt->perm_ok)
2397 return true;
2398
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002399 if (emulator_bad_iopl(ctxt))
2400 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002401 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002402
2403 ctxt->perm_ok = true;
2404
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002405 return true;
2406}
2407
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 struct tss_segment_16 *tss)
2410{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002411 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002413 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2414 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2415 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2416 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2417 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2418 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2419 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2420 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421
Avi Kivity1aa36612011-04-27 13:20:30 +03002422 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2423 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2424 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2425 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2426 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427}
2428
2429static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430 struct tss_segment_16 *tss)
2431{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002432 int ret;
2433
Avi Kivity9dac77f2011-06-01 15:34:25 +03002434 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002435 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002436 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2437 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2438 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2439 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2440 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2441 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2442 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2443 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002444
2445 /*
2446 * SDM says that segment selectors are loaded before segment
2447 * descriptors
2448 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002449 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2450 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2451 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2452 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2453 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002454
2455 /*
Guo Chaofc058682012-06-28 15:19:51 +08002456 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457 * it is handled in a context of new task
2458 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002459 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
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->es, VCPU_SREG_ES);
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->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002469 if (ret != X86EMUL_CONTINUE)
2470 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002471 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002472 if (ret != X86EMUL_CONTINUE)
2473 return ret;
2474
2475 return X86EMUL_CONTINUE;
2476}
2477
2478static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002479 u16 tss_selector, u16 old_tss_sel,
2480 ulong old_tss_base, struct desc_struct *new_desc)
2481{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002482 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002483 struct tss_segment_16 tss_seg;
2484 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002485 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002486
Avi Kivity0f65dd72011-04-20 13:37:53 +03002487 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002488 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002489 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002490 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002491 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002493 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002494
Avi Kivity0f65dd72011-04-20 13:37:53 +03002495 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002496 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002497 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002498 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002499 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500
Avi Kivity0f65dd72011-04-20 13:37:53 +03002501 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002502 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002503 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002505 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002506
2507 if (old_tss_sel != 0xffff) {
2508 tss_seg.prev_task_link = old_tss_sel;
2509
Avi Kivity0f65dd72011-04-20 13:37:53 +03002510 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 &tss_seg.prev_task_link,
2512 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002513 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002514 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002515 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002516 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517 }
2518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002519 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520}
2521
2522static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523 struct tss_segment_32 *tss)
2524{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002525 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002526 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002528 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2529 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2530 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2531 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2532 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2533 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2534 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2535 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002536
Avi Kivity1aa36612011-04-27 13:20:30 +03002537 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2538 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2539 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2540 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2541 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2542 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2543 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544}
2545
2546static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547 struct tss_segment_32 *tss)
2548{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 int ret;
2550
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002551 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002552 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002553 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002555
2556 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002557 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2558 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2559 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2560 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2561 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2562 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2563 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2564 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565
2566 /*
2567 * SDM says that segment selectors are loaded before segment
2568 * descriptors
2569 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002570 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2571 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2572 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2573 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2574 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2575 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2576 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577
2578 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002579 * If we're switching between Protected Mode and VM86, we need to make
2580 * sure to update the mode before loading the segment descriptors so
2581 * that the selectors are interpreted correctly.
2582 *
2583 * Need to get rflags to the vcpu struct immediately because it
2584 * influences the CPL which is checked at least when loading the segment
2585 * descriptors and when pushing an error code to the new kernel stack.
2586 *
2587 * TODO Introduce a separate ctxt->ops->set_cpl callback
2588 */
2589 if (ctxt->eflags & X86_EFLAGS_VM)
2590 ctxt->mode = X86EMUL_MODE_VM86;
2591 else
2592 ctxt->mode = X86EMUL_MODE_PROT32;
2593
2594 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2595
2596 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597 * Now load segment descriptors. If fault happenes at this stage
2598 * it is handled in a context of new task
2599 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002600 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
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->es, VCPU_SREG_ES);
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->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
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->ds, VCPU_SREG_DS);
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->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 if (ret != X86EMUL_CONTINUE)
2617 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002618 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 if (ret != X86EMUL_CONTINUE)
2620 return ret;
2621
2622 return X86EMUL_CONTINUE;
2623}
2624
2625static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 u16 tss_selector, u16 old_tss_sel,
2627 ulong old_tss_base, struct desc_struct *new_desc)
2628{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002629 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 struct tss_segment_32 tss_seg;
2631 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002632 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633
Avi Kivity0f65dd72011-04-20 13:37:53 +03002634 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002635 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002636 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002640 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002641
Avi Kivity0f65dd72011-04-20 13:37:53 +03002642 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002643 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002644 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002645 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647
Avi Kivity0f65dd72011-04-20 13:37:53 +03002648 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002649 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002650 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
2654 if (old_tss_sel != 0xffff) {
2655 tss_seg.prev_task_link = old_tss_sel;
2656
Avi Kivity0f65dd72011-04-20 13:37:53 +03002657 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 &tss_seg.prev_task_link,
2659 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002660 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002661 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002663 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 }
2665
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002666 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667}
2668
2669static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002670 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002671 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002673 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002674 struct desc_struct curr_tss_desc, next_tss_desc;
2675 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002676 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002678 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002679 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002680 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681
2682 /* FIXME: old_tss_base == ~0 ? */
2683
Avi Kivitye9194642012-06-13 16:29:39 +03002684 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 if (ret != X86EMUL_CONTINUE)
2686 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002687 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002688 if (ret != X86EMUL_CONTINUE)
2689 return ret;
2690
2691 /* FIXME: check that next_tss_desc is tss */
2692
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002693 /*
2694 * Check privileges. The three cases are task switch caused by...
2695 *
2696 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2697 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002698 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002699 */
2700 if (reason == TASK_SWITCH_GATE) {
2701 if (idt_index != -1) {
2702 /* Software interrupts */
2703 struct desc_struct task_gate_desc;
2704 int dpl;
2705
2706 ret = read_interrupt_descriptor(ctxt, idt_index,
2707 &task_gate_desc);
2708 if (ret != X86EMUL_CONTINUE)
2709 return ret;
2710
2711 dpl = task_gate_desc.dpl;
2712 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2713 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2714 }
2715 } else if (reason != TASK_SWITCH_IRET) {
2716 int dpl = next_tss_desc.dpl;
2717 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2718 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719 }
2720
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002721
Gleb Natapovceffb452010-03-18 15:20:19 +02002722 desc_limit = desc_limit_scaled(&next_tss_desc);
2723 if (!next_tss_desc.p ||
2724 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2725 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002726 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 return X86EMUL_PROPAGATE_FAULT;
2728 }
2729
2730 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2731 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002732 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733 }
2734
2735 if (reason == TASK_SWITCH_IRET)
2736 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2737
2738 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002739 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2741 old_tss_sel = 0xffff;
2742
2743 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002744 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 old_tss_base, &next_tss_desc);
2746 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002747 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002749 if (ret != X86EMUL_CONTINUE)
2750 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751
2752 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2753 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2754
2755 if (reason != TASK_SWITCH_IRET) {
2756 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002757 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758 }
2759
Avi Kivity717746e2011-04-20 13:37:53 +03002760 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002761 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762
Jan Kiszkae269fb22010-04-14 15:51:09 +02002763 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002764 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2765 ctxt->lock_prefix = 0;
2766 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002767 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002768 }
2769
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 return ret;
2771}
2772
2773int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002774 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002775 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 int rc;
2778
Avi Kivitydd856ef2012-08-27 23:46:17 +03002779 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002780 ctxt->_eip = ctxt->eip;
2781 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002783 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002784 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785
Avi Kivitydd856ef2012-08-27 23:46:17 +03002786 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002787 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002788 writeback_registers(ctxt);
2789 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002791 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792}
2793
Avi Kivity90de84f2010-11-17 15:28:21 +02002794static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002795 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002796{
Gleb Natapova682e352010-03-18 15:20:21 +02002797 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2798
Avi Kivitydd856ef2012-08-27 23:46:17 +03002799 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2800 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Avi Kivity90de84f2010-11-17 15:28:21 +02002801 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002802}
2803
Avi Kivity7af04fc2010-08-18 14:16:35 +03002804static int em_das(struct x86_emulate_ctxt *ctxt)
2805{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002806 u8 al, old_al;
2807 bool af, cf, old_cf;
2808
2809 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002810 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002811
2812 old_al = al;
2813 old_cf = cf;
2814 cf = false;
2815 af = ctxt->eflags & X86_EFLAGS_AF;
2816 if ((al & 0x0f) > 9 || af) {
2817 al -= 6;
2818 cf = old_cf | (al >= 250);
2819 af = true;
2820 } else {
2821 af = false;
2822 }
2823 if (old_al > 0x99 || old_cf) {
2824 al -= 0x60;
2825 cf = true;
2826 }
2827
Avi Kivity9dac77f2011-06-01 15:34:25 +03002828 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002829 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002830 ctxt->src.type = OP_IMM;
2831 ctxt->src.val = 0;
2832 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002833 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002834 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2835 if (cf)
2836 ctxt->eflags |= X86_EFLAGS_CF;
2837 if (af)
2838 ctxt->eflags |= X86_EFLAGS_AF;
2839 return X86EMUL_CONTINUE;
2840}
2841
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002842static int em_call(struct x86_emulate_ctxt *ctxt)
2843{
2844 long rel = ctxt->src.val;
2845
2846 ctxt->src.val = (unsigned long)ctxt->_eip;
2847 jmp_rel(ctxt, rel);
2848 return em_push(ctxt);
2849}
2850
Avi Kivity0ef753b2010-08-18 14:51:45 +03002851static int em_call_far(struct x86_emulate_ctxt *ctxt)
2852{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002853 u16 sel, old_cs;
2854 ulong old_eip;
2855 int rc;
2856
Avi Kivity1aa36612011-04-27 13:20:30 +03002857 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002858 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002859
Avi Kivity9dac77f2011-06-01 15:34:25 +03002860 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002861 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002862 return X86EMUL_CONTINUE;
2863
Avi Kivity9dac77f2011-06-01 15:34:25 +03002864 ctxt->_eip = 0;
2865 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002866
Avi Kivity9dac77f2011-06-01 15:34:25 +03002867 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002868 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002869 if (rc != X86EMUL_CONTINUE)
2870 return rc;
2871
Avi Kivity9dac77f2011-06-01 15:34:25 +03002872 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002873 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002874}
2875
Avi Kivity40ece7c2010-08-18 15:12:09 +03002876static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2877{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002878 int rc;
2879
Avi Kivity9dac77f2011-06-01 15:34:25 +03002880 ctxt->dst.type = OP_REG;
2881 ctxt->dst.addr.reg = &ctxt->_eip;
2882 ctxt->dst.bytes = ctxt->op_bytes;
2883 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002884 if (rc != X86EMUL_CONTINUE)
2885 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002886 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002887 return X86EMUL_CONTINUE;
2888}
2889
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002890static int em_add(struct x86_emulate_ctxt *ctxt)
2891{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002892 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002893 return X86EMUL_CONTINUE;
2894}
2895
2896static int em_or(struct x86_emulate_ctxt *ctxt)
2897{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002898 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002899 return X86EMUL_CONTINUE;
2900}
2901
2902static int em_adc(struct x86_emulate_ctxt *ctxt)
2903{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002904 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002905 return X86EMUL_CONTINUE;
2906}
2907
2908static int em_sbb(struct x86_emulate_ctxt *ctxt)
2909{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002910 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002911 return X86EMUL_CONTINUE;
2912}
2913
2914static int em_and(struct x86_emulate_ctxt *ctxt)
2915{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002916 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002917 return X86EMUL_CONTINUE;
2918}
2919
2920static int em_sub(struct x86_emulate_ctxt *ctxt)
2921{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002922 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002923 return X86EMUL_CONTINUE;
2924}
2925
2926static int em_xor(struct x86_emulate_ctxt *ctxt)
2927{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002928 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002929 return X86EMUL_CONTINUE;
2930}
2931
2932static int em_cmp(struct x86_emulate_ctxt *ctxt)
2933{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002934 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002935 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002936 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002937 return X86EMUL_CONTINUE;
2938}
2939
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002940static int em_test(struct x86_emulate_ctxt *ctxt)
2941{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002942 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002943 /* Disable writeback. */
2944 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002945 return X86EMUL_CONTINUE;
2946}
2947
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002948static int em_xchg(struct x86_emulate_ctxt *ctxt)
2949{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002950 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->src.val = ctxt->dst.val;
2952 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002953
2954 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002955 ctxt->dst.val = ctxt->src.orig_val;
2956 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002957 return X86EMUL_CONTINUE;
2958}
2959
Avi Kivity5c82aa22010-08-18 18:31:43 +03002960static int em_imul(struct x86_emulate_ctxt *ctxt)
2961{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002962 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002963 return X86EMUL_CONTINUE;
2964}
2965
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002966static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2967{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002968 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002969 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002970}
2971
Avi Kivity61429142010-08-19 15:13:00 +03002972static int em_cwd(struct x86_emulate_ctxt *ctxt)
2973{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002974 ctxt->dst.type = OP_REG;
2975 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002976 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002977 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002978
2979 return X86EMUL_CONTINUE;
2980}
2981
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002982static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2983{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002984 u64 tsc = 0;
2985
Avi Kivity717746e2011-04-20 13:37:53 +03002986 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002987 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
2988 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002989 return X86EMUL_CONTINUE;
2990}
2991
Avi Kivity222d21a2011-11-10 14:57:30 +02002992static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
2993{
2994 u64 pmc;
2995
Avi Kivitydd856ef2012-08-27 23:46:17 +03002996 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02002997 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002998 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
2999 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003000 return X86EMUL_CONTINUE;
3001}
3002
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003003static int em_mov(struct x86_emulate_ctxt *ctxt)
3004{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003005 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003006 return X86EMUL_CONTINUE;
3007}
3008
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003009static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3010{
3011 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3012 return emulate_gp(ctxt, 0);
3013
3014 /* Disable writeback. */
3015 ctxt->dst.type = OP_NONE;
3016 return X86EMUL_CONTINUE;
3017}
3018
3019static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3020{
3021 unsigned long val;
3022
3023 if (ctxt->mode == X86EMUL_MODE_PROT64)
3024 val = ctxt->src.val & ~0ULL;
3025 else
3026 val = ctxt->src.val & ~0U;
3027
3028 /* #UD condition is already handled. */
3029 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3030 return emulate_gp(ctxt, 0);
3031
3032 /* Disable writeback. */
3033 ctxt->dst.type = OP_NONE;
3034 return X86EMUL_CONTINUE;
3035}
3036
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003037static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3038{
3039 u64 msr_data;
3040
Avi Kivitydd856ef2012-08-27 23:46:17 +03003041 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3042 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3043 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003044 return emulate_gp(ctxt, 0);
3045
3046 return X86EMUL_CONTINUE;
3047}
3048
3049static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3050{
3051 u64 msr_data;
3052
Avi Kivitydd856ef2012-08-27 23:46:17 +03003053 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003054 return emulate_gp(ctxt, 0);
3055
Avi Kivitydd856ef2012-08-27 23:46:17 +03003056 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3057 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003058 return X86EMUL_CONTINUE;
3059}
3060
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003061static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3062{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003064 return emulate_ud(ctxt);
3065
Avi Kivity9dac77f2011-06-01 15:34:25 +03003066 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003067 return X86EMUL_CONTINUE;
3068}
3069
3070static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3071{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003073
Avi Kivity9dac77f2011-06-01 15:34:25 +03003074 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003075 return emulate_ud(ctxt);
3076
Avi Kivity9dac77f2011-06-01 15:34:25 +03003077 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003078 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3079
3080 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003081 ctxt->dst.type = OP_NONE;
3082 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003083}
3084
Avi Kivitya14e5792012-06-13 12:28:33 +03003085static int em_lldt(struct x86_emulate_ctxt *ctxt)
3086{
3087 u16 sel = ctxt->src.val;
3088
3089 /* Disable writeback. */
3090 ctxt->dst.type = OP_NONE;
3091 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3092}
3093
Avi Kivity80890002012-06-13 16:33:29 +03003094static int em_ltr(struct x86_emulate_ctxt *ctxt)
3095{
3096 u16 sel = ctxt->src.val;
3097
3098 /* Disable writeback. */
3099 ctxt->dst.type = OP_NONE;
3100 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3101}
3102
Avi Kivity38503912011-03-31 18:48:09 +02003103static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3104{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003105 int rc;
3106 ulong linear;
3107
Avi Kivity9dac77f2011-06-01 15:34:25 +03003108 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003109 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003110 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003111 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003112 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003113 return X86EMUL_CONTINUE;
3114}
3115
Avi Kivity2d04a052011-04-20 15:32:49 +03003116static int em_clts(struct x86_emulate_ctxt *ctxt)
3117{
3118 ulong cr0;
3119
3120 cr0 = ctxt->ops->get_cr(ctxt, 0);
3121 cr0 &= ~X86_CR0_TS;
3122 ctxt->ops->set_cr(ctxt, 0, cr0);
3123 return X86EMUL_CONTINUE;
3124}
3125
Avi Kivity26d05cc2011-04-21 12:07:59 +03003126static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3127{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003128 int rc;
3129
Avi Kivity9dac77f2011-06-01 15:34:25 +03003130 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003131 return X86EMUL_UNHANDLEABLE;
3132
3133 rc = ctxt->ops->fix_hypercall(ctxt);
3134 if (rc != X86EMUL_CONTINUE)
3135 return rc;
3136
3137 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003138 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003139 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003140 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003141 return X86EMUL_CONTINUE;
3142}
3143
Avi Kivity96051572012-06-10 17:21:18 +03003144static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3145 void (*get)(struct x86_emulate_ctxt *ctxt,
3146 struct desc_ptr *ptr))
3147{
3148 struct desc_ptr desc_ptr;
3149
3150 if (ctxt->mode == X86EMUL_MODE_PROT64)
3151 ctxt->op_bytes = 8;
3152 get(ctxt, &desc_ptr);
3153 if (ctxt->op_bytes == 2) {
3154 ctxt->op_bytes = 4;
3155 desc_ptr.address &= 0x00ffffff;
3156 }
3157 /* Disable writeback. */
3158 ctxt->dst.type = OP_NONE;
3159 return segmented_write(ctxt, ctxt->dst.addr.mem,
3160 &desc_ptr, 2 + ctxt->op_bytes);
3161}
3162
3163static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3164{
3165 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3166}
3167
3168static int em_sidt(struct x86_emulate_ctxt *ctxt)
3169{
3170 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3171}
3172
Avi Kivity26d05cc2011-04-21 12:07:59 +03003173static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3174{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003175 struct desc_ptr desc_ptr;
3176 int rc;
3177
Avi Kivity510425f2012-06-07 17:04:36 +03003178 if (ctxt->mode == X86EMUL_MODE_PROT64)
3179 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003180 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003181 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003182 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003183 if (rc != X86EMUL_CONTINUE)
3184 return rc;
3185 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3186 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003187 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003188 return X86EMUL_CONTINUE;
3189}
3190
Avi Kivity5ef39c72011-04-21 12:21:50 +03003191static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003192{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003193 int rc;
3194
Avi Kivity5ef39c72011-04-21 12:21:50 +03003195 rc = ctxt->ops->fix_hypercall(ctxt);
3196
Avi Kivity26d05cc2011-04-21 12:07:59 +03003197 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003199 return rc;
3200}
3201
3202static int em_lidt(struct x86_emulate_ctxt *ctxt)
3203{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003204 struct desc_ptr desc_ptr;
3205 int rc;
3206
Avi Kivity510425f2012-06-07 17:04:36 +03003207 if (ctxt->mode == X86EMUL_MODE_PROT64)
3208 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003209 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003210 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003211 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003212 if (rc != X86EMUL_CONTINUE)
3213 return rc;
3214 ctxt->ops->set_idt(ctxt, &desc_ptr);
3215 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003217 return X86EMUL_CONTINUE;
3218}
3219
3220static int em_smsw(struct x86_emulate_ctxt *ctxt)
3221{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003222 ctxt->dst.bytes = 2;
3223 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003224 return X86EMUL_CONTINUE;
3225}
3226
3227static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3228{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003229 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003230 | (ctxt->src.val & 0x0f));
3231 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003232 return X86EMUL_CONTINUE;
3233}
3234
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003235static int em_loop(struct x86_emulate_ctxt *ctxt)
3236{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003237 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3238 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003239 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3240 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003241
3242 return X86EMUL_CONTINUE;
3243}
3244
3245static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3246{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003247 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003248 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003249
3250 return X86EMUL_CONTINUE;
3251}
3252
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003253static int em_in(struct x86_emulate_ctxt *ctxt)
3254{
3255 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3256 &ctxt->dst.val))
3257 return X86EMUL_IO_NEEDED;
3258
3259 return X86EMUL_CONTINUE;
3260}
3261
3262static int em_out(struct x86_emulate_ctxt *ctxt)
3263{
3264 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3265 &ctxt->src.val, 1);
3266 /* Disable writeback. */
3267 ctxt->dst.type = OP_NONE;
3268 return X86EMUL_CONTINUE;
3269}
3270
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003271static int em_cli(struct x86_emulate_ctxt *ctxt)
3272{
3273 if (emulator_bad_iopl(ctxt))
3274 return emulate_gp(ctxt, 0);
3275
3276 ctxt->eflags &= ~X86_EFLAGS_IF;
3277 return X86EMUL_CONTINUE;
3278}
3279
3280static int em_sti(struct x86_emulate_ctxt *ctxt)
3281{
3282 if (emulator_bad_iopl(ctxt))
3283 return emulate_gp(ctxt, 0);
3284
3285 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3286 ctxt->eflags |= X86_EFLAGS_IF;
3287 return X86EMUL_CONTINUE;
3288}
3289
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003290static int em_bt(struct x86_emulate_ctxt *ctxt)
3291{
3292 /* Disable writeback. */
3293 ctxt->dst.type = OP_NONE;
3294 /* only subword offset */
3295 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3296
3297 emulate_2op_SrcV_nobyte(ctxt, "bt");
3298 return X86EMUL_CONTINUE;
3299}
3300
3301static int em_bts(struct x86_emulate_ctxt *ctxt)
3302{
3303 emulate_2op_SrcV_nobyte(ctxt, "bts");
3304 return X86EMUL_CONTINUE;
3305}
3306
3307static int em_btr(struct x86_emulate_ctxt *ctxt)
3308{
3309 emulate_2op_SrcV_nobyte(ctxt, "btr");
3310 return X86EMUL_CONTINUE;
3311}
3312
3313static int em_btc(struct x86_emulate_ctxt *ctxt)
3314{
3315 emulate_2op_SrcV_nobyte(ctxt, "btc");
3316 return X86EMUL_CONTINUE;
3317}
3318
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003319static int em_bsf(struct x86_emulate_ctxt *ctxt)
3320{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003321 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003322 return X86EMUL_CONTINUE;
3323}
3324
3325static int em_bsr(struct x86_emulate_ctxt *ctxt)
3326{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003327 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003328 return X86EMUL_CONTINUE;
3329}
3330
Avi Kivity6d6eede2012-06-07 14:11:36 +03003331static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3332{
3333 u32 eax, ebx, ecx, edx;
3334
Avi Kivitydd856ef2012-08-27 23:46:17 +03003335 eax = reg_read(ctxt, VCPU_REGS_RAX);
3336 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003337 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003338 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3339 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3340 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3341 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003342 return X86EMUL_CONTINUE;
3343}
3344
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003345static int em_lahf(struct x86_emulate_ctxt *ctxt)
3346{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003347 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3348 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003349 return X86EMUL_CONTINUE;
3350}
3351
Avi Kivity92998362012-06-13 12:25:06 +03003352static int em_bswap(struct x86_emulate_ctxt *ctxt)
3353{
3354 switch (ctxt->op_bytes) {
3355#ifdef CONFIG_X86_64
3356 case 8:
3357 asm("bswap %0" : "+r"(ctxt->dst.val));
3358 break;
3359#endif
3360 default:
3361 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3362 break;
3363 }
3364 return X86EMUL_CONTINUE;
3365}
3366
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003367static bool valid_cr(int nr)
3368{
3369 switch (nr) {
3370 case 0:
3371 case 2 ... 4:
3372 case 8:
3373 return true;
3374 default:
3375 return false;
3376 }
3377}
3378
3379static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3380{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003381 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003382 return emulate_ud(ctxt);
3383
3384 return X86EMUL_CONTINUE;
3385}
3386
3387static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3388{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003389 u64 new_val = ctxt->src.val64;
3390 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003391 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003392
3393 static u64 cr_reserved_bits[] = {
3394 0xffffffff00000000ULL,
3395 0, 0, 0, /* CR3 checked later */
3396 CR4_RESERVED_BITS,
3397 0, 0, 0,
3398 CR8_RESERVED_BITS,
3399 };
3400
3401 if (!valid_cr(cr))
3402 return emulate_ud(ctxt);
3403
3404 if (new_val & cr_reserved_bits[cr])
3405 return emulate_gp(ctxt, 0);
3406
3407 switch (cr) {
3408 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003409 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003410 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3411 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3412 return emulate_gp(ctxt, 0);
3413
Avi Kivity717746e2011-04-20 13:37:53 +03003414 cr4 = ctxt->ops->get_cr(ctxt, 4);
3415 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003416
3417 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3418 !(cr4 & X86_CR4_PAE))
3419 return emulate_gp(ctxt, 0);
3420
3421 break;
3422 }
3423 case 3: {
3424 u64 rsvd = 0;
3425
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003426 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3427 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003428 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003429 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003430 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003431 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003432 rsvd = CR3_NONPAE_RESERVED_BITS;
3433
3434 if (new_val & rsvd)
3435 return emulate_gp(ctxt, 0);
3436
3437 break;
3438 }
3439 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003440 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003441
3442 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3443 return emulate_gp(ctxt, 0);
3444
3445 break;
3446 }
3447 }
3448
3449 return X86EMUL_CONTINUE;
3450}
3451
Joerg Roedel3b88e412011-04-04 12:39:29 +02003452static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3453{
3454 unsigned long dr7;
3455
Avi Kivity717746e2011-04-20 13:37:53 +03003456 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003457
3458 /* Check if DR7.Global_Enable is set */
3459 return dr7 & (1 << 13);
3460}
3461
3462static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3463{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003464 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003465 u64 cr4;
3466
3467 if (dr > 7)
3468 return emulate_ud(ctxt);
3469
Avi Kivity717746e2011-04-20 13:37:53 +03003470 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003471 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3472 return emulate_ud(ctxt);
3473
3474 if (check_dr7_gd(ctxt))
3475 return emulate_db(ctxt);
3476
3477 return X86EMUL_CONTINUE;
3478}
3479
3480static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3481{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003482 u64 new_val = ctxt->src.val64;
3483 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003484
3485 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3486 return emulate_gp(ctxt, 0);
3487
3488 return check_dr_read(ctxt);
3489}
3490
Joerg Roedel01de8b02011-04-04 12:39:31 +02003491static int check_svme(struct x86_emulate_ctxt *ctxt)
3492{
3493 u64 efer;
3494
Avi Kivity717746e2011-04-20 13:37:53 +03003495 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003496
3497 if (!(efer & EFER_SVME))
3498 return emulate_ud(ctxt);
3499
3500 return X86EMUL_CONTINUE;
3501}
3502
3503static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3504{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003505 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003506
3507 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003508 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003509 return emulate_gp(ctxt, 0);
3510
3511 return check_svme(ctxt);
3512}
3513
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003514static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3515{
Avi Kivity717746e2011-04-20 13:37:53 +03003516 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003517
Avi Kivity717746e2011-04-20 13:37:53 +03003518 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003519 return emulate_ud(ctxt);
3520
3521 return X86EMUL_CONTINUE;
3522}
3523
Joerg Roedel80612522011-04-04 12:39:33 +02003524static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3525{
Avi Kivity717746e2011-04-20 13:37:53 +03003526 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003527 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003528
Avi Kivity717746e2011-04-20 13:37:53 +03003529 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003530 (rcx > 3))
3531 return emulate_gp(ctxt, 0);
3532
3533 return X86EMUL_CONTINUE;
3534}
3535
Joerg Roedelf6511932011-04-04 12:39:35 +02003536static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3537{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003538 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3539 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003540 return emulate_gp(ctxt, 0);
3541
3542 return X86EMUL_CONTINUE;
3543}
3544
3545static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3546{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003547 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3548 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003549 return emulate_gp(ctxt, 0);
3550
3551 return X86EMUL_CONTINUE;
3552}
3553
Avi Kivity73fba5f2010-07-29 15:11:53 +03003554#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003555#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003556#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3557 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003558#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003559#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003560#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3561#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003562#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003563#define II(_f, _e, _i) \
3564 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003565#define IIP(_f, _e, _i, _p) \
3566 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3567 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003568#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003569
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003570#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003571#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003572#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003573#define I2bvIP(_f, _e, _i, _p) \
3574 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003575
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003576#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3577 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3578 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003579
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003580static struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003581 DI(SrcNone | Priv, monitor),
3582 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003583 N, N, N, N, N, N,
3584};
3585
Joerg Roedel01de8b02011-04-04 12:39:31 +02003586static struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003587 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3588 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3589 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3590 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3591 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3592 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3593 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3594 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003595};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003596
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003597static struct opcode group7_rm7[] = {
3598 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003599 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003600 N, N, N, N, N, N,
3601};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003602
Avi Kivity73fba5f2010-07-29 15:11:53 +03003603static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003604 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003605 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003606 I(Lock, em_adc),
3607 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003608 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003609 I(Lock, em_sub),
3610 I(Lock, em_xor),
3611 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003612};
3613
3614static struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003615 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003616};
3617
3618static struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003619 I(DstMem | SrcImm, em_test),
3620 I(DstMem | SrcImm, em_test),
3621 I(DstMem | SrcNone | Lock, em_not),
3622 I(DstMem | SrcNone | Lock, em_neg),
3623 I(SrcMem, em_mul_ex),
3624 I(SrcMem, em_imul_ex),
3625 I(SrcMem, em_div_ex),
3626 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003627};
3628
3629static struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003630 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3631 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003632 N, N, N, N, N, N,
3633};
3634
3635static struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003636 I(DstMem | SrcNone | Lock, em_grp45),
3637 I(DstMem | SrcNone | Lock, em_grp45),
3638 I(SrcMem | Stack, em_grp45),
3639 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3640 I(SrcMem | Stack, em_grp45),
3641 I(SrcMemFAddr | ImplicitOps, em_grp45),
3642 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003643};
3644
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003645static struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003646 DI(Prot, sldt),
3647 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003648 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003649 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003650 N, N, N, N,
3651};
3652
Avi Kivity73fba5f2010-07-29 15:11:53 +03003653static struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003654 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3655 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003656 II(SrcMem | Priv, em_lgdt, lgdt),
3657 II(SrcMem | Priv, em_lidt, lidt),
3658 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3659 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3660 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003662 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003663 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003664 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003665 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3666 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3667 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003668} };
3669
3670static struct opcode group8[] = {
3671 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003672 I(DstMem | SrcImmByte, em_bt),
3673 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3674 I(DstMem | SrcImmByte | Lock, em_btr),
3675 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003676};
3677
3678static struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003679 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003680}, {
3681 N, N, N, N, N, N, N, N,
3682} };
3683
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003684static struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003685 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003686 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003687};
3688
Avi Kivityaa97bb42010-01-20 18:09:23 +02003689static struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003690 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003691};
3692
Avi Kivity3e114eb2012-04-09 18:40:01 +03003693static struct gprefix pfx_vmovntpx = {
3694 I(0, em_mov), N, N, N,
3695};
3696
Avi Kivity73fba5f2010-07-29 15:11:53 +03003697static struct opcode opcode_table[256] = {
3698 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003699 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003700 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3701 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003702 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003703 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003704 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3705 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003706 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003707 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003708 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3709 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003710 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003711 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003712 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3713 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003714 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003715 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003716 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003717 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003718 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003719 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003720 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003721 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003722 /* 0x40 - 0x4F */
3723 X16(D(DstReg)),
3724 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003725 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003726 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003727 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003728 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003729 I(ImplicitOps | Stack | No64, em_pusha),
3730 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003731 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3732 N, N, N, N,
3733 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003734 I(SrcImm | Mov | Stack, em_push),
3735 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003736 I(SrcImmByte | Mov | Stack, em_push),
3737 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003738 I2bvIP(DstDI | SrcDX | Mov | String, em_in, ins, check_perm_in), /* insb, insw/insd */
3739 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003740 /* 0x70 - 0x7F */
3741 X16(D(SrcImmByte)),
3742 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003743 G(ByteOp | DstMem | SrcImm, group1),
3744 G(DstMem | SrcImm, group1),
3745 G(ByteOp | DstMem | SrcImm | No64, group1),
3746 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003747 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003748 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003749 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003750 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003751 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003752 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003753 D(ModRM | SrcMem | NoAccess | DstReg),
3754 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3755 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003756 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003757 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003758 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003759 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003760 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003761 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003762 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003763 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003764 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003765 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003766 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003767 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003768 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003769 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003770 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3771 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003772 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003773 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003774 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003775 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003776 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003777 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003778 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003779 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003780 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003781 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3782 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003783 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003784 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003785 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3786 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003787 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003788 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003790 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791 N, N, N, N,
3792 /* 0xD8 - 0xDF */
3793 N, N, N, N, N, N, N, N,
3794 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003795 X3(I(SrcImmByte, em_loop)),
3796 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003797 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3798 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003799 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003800 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003801 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003802 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3803 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003804 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003805 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003806 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3807 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003808 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003809 D(ImplicitOps), D(ImplicitOps),
3810 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003811 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3812};
3813
3814static struct opcode twobyte_table[256] = {
3815 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003816 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003817 N, I(ImplicitOps | VendorSpecific, em_syscall),
3818 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003819 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003820 N, D(ImplicitOps | ModRM), N, N,
3821 /* 0x10 - 0x1F */
3822 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3823 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003824 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003825 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003826 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3827 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003828 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003829 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3830 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003831 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003832 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003833 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003834 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003835 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003836 I(ImplicitOps | VendorSpecific, em_sysenter),
3837 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003838 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003839 N, N, N, N, N, N, N, N,
3840 /* 0x40 - 0x4F */
3841 X16(D(DstReg | SrcMem | ModRM | Mov)),
3842 /* 0x50 - 0x5F */
3843 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3844 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003845 N, N, N, N,
3846 N, N, N, N,
3847 N, N, N, N,
3848 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003849 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003850 N, N, N, N,
3851 N, N, N, N,
3852 N, N, N, N,
3853 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003854 /* 0x80 - 0x8F */
3855 X16(D(SrcImm)),
3856 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003857 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003858 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003859 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003860 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003861 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3862 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3863 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003864 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003865 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003866 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003867 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3868 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003869 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003871 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003872 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003873 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003874 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3875 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003876 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003877 /* 0xB8 - 0xBF */
3878 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003879 G(BitOp, group8),
3880 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003881 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003882 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003883 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003884 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003885 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003887 /* 0xC8 - 0xCF */
3888 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0xD0 - 0xDF */
3890 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3891 /* 0xE0 - 0xEF */
3892 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3893 /* 0xF0 - 0xFF */
3894 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3895};
3896
3897#undef D
3898#undef N
3899#undef G
3900#undef GD
3901#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003902#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003903#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003905#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003906#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003907#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003908#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003909#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003910
Avi Kivity9dac77f2011-06-01 15:34:25 +03003911static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003912{
3913 unsigned size;
3914
Avi Kivity9dac77f2011-06-01 15:34:25 +03003915 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003916 if (size == 8)
3917 size = 4;
3918 return size;
3919}
3920
3921static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3922 unsigned size, bool sign_extension)
3923{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003924 int rc = X86EMUL_CONTINUE;
3925
3926 op->type = OP_IMM;
3927 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003928 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003929 /* NB. Immediates are sign-extended as necessary. */
3930 switch (op->bytes) {
3931 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003932 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003933 break;
3934 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003935 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003936 break;
3937 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003938 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003939 break;
3940 }
3941 if (!sign_extension) {
3942 switch (op->bytes) {
3943 case 1:
3944 op->val &= 0xff;
3945 break;
3946 case 2:
3947 op->val &= 0xffff;
3948 break;
3949 case 4:
3950 op->val &= 0xffffffff;
3951 break;
3952 }
3953 }
3954done:
3955 return rc;
3956}
3957
Avi Kivitya99455492011-09-13 10:45:41 +03003958static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3959 unsigned d)
3960{
3961 int rc = X86EMUL_CONTINUE;
3962
3963 switch (d) {
3964 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003965 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03003966 break;
3967 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003968 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03003969 break;
3970 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003971 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003972 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03003973 *op = ctxt->memop;
3974 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003975 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03003976 fetch_bit_operand(ctxt);
3977 op->orig_val = op->val;
3978 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003979 case OpMem64:
3980 ctxt->memop.bytes = 8;
3981 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03003982 case OpAcc:
3983 op->type = OP_REG;
3984 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003985 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03003986 fetch_register_operand(op);
3987 op->orig_val = op->val;
3988 break;
3989 case OpDI:
3990 op->type = OP_MEM;
3991 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3992 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03003993 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03003994 op->addr.mem.seg = VCPU_SREG_ES;
3995 op->val = 0;
3996 break;
3997 case OpDX:
3998 op->type = OP_REG;
3999 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004000 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004001 fetch_register_operand(op);
4002 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004003 case OpCL:
4004 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004005 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004006 break;
4007 case OpImmByte:
4008 rc = decode_imm(ctxt, op, 1, true);
4009 break;
4010 case OpOne:
4011 op->bytes = 1;
4012 op->val = 1;
4013 break;
4014 case OpImm:
4015 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4016 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004017 case OpMem8:
4018 ctxt->memop.bytes = 1;
4019 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004020 case OpMem16:
4021 ctxt->memop.bytes = 2;
4022 goto mem_common;
4023 case OpMem32:
4024 ctxt->memop.bytes = 4;
4025 goto mem_common;
4026 case OpImmU16:
4027 rc = decode_imm(ctxt, op, 2, false);
4028 break;
4029 case OpImmU:
4030 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4031 break;
4032 case OpSI:
4033 op->type = OP_MEM;
4034 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4035 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004036 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004037 op->addr.mem.seg = seg_override(ctxt);
4038 op->val = 0;
4039 break;
4040 case OpImmFAddr:
4041 op->type = OP_IMM;
4042 op->addr.mem.ea = ctxt->_eip;
4043 op->bytes = ctxt->op_bytes + 2;
4044 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4045 break;
4046 case OpMemFAddr:
4047 ctxt->memop.bytes = ctxt->op_bytes + 2;
4048 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004049 case OpES:
4050 op->val = VCPU_SREG_ES;
4051 break;
4052 case OpCS:
4053 op->val = VCPU_SREG_CS;
4054 break;
4055 case OpSS:
4056 op->val = VCPU_SREG_SS;
4057 break;
4058 case OpDS:
4059 op->val = VCPU_SREG_DS;
4060 break;
4061 case OpFS:
4062 op->val = VCPU_SREG_FS;
4063 break;
4064 case OpGS:
4065 op->val = VCPU_SREG_GS;
4066 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004067 case OpImplicit:
4068 /* Special instructions do their own operand decoding. */
4069 default:
4070 op->type = OP_NONE; /* Disable writeback. */
4071 break;
4072 }
4073
4074done:
4075 return rc;
4076}
4077
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004078int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004079{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004080 int rc = X86EMUL_CONTINUE;
4081 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004082 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004083 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004084 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004085
Avi Kivityf09ed832011-09-13 10:45:40 +03004086 ctxt->memop.type = OP_NONE;
4087 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004088 ctxt->_eip = ctxt->eip;
4089 ctxt->fetch.start = ctxt->_eip;
4090 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004091 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004092 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004093
4094 switch (mode) {
4095 case X86EMUL_MODE_REAL:
4096 case X86EMUL_MODE_VM86:
4097 case X86EMUL_MODE_PROT16:
4098 def_op_bytes = def_ad_bytes = 2;
4099 break;
4100 case X86EMUL_MODE_PROT32:
4101 def_op_bytes = def_ad_bytes = 4;
4102 break;
4103#ifdef CONFIG_X86_64
4104 case X86EMUL_MODE_PROT64:
4105 def_op_bytes = 4;
4106 def_ad_bytes = 8;
4107 break;
4108#endif
4109 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004110 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004111 }
4112
Avi Kivity9dac77f2011-06-01 15:34:25 +03004113 ctxt->op_bytes = def_op_bytes;
4114 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004115
4116 /* Legacy prefixes. */
4117 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004118 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004119 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004120 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004121 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004122 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004123 break;
4124 case 0x67: /* address-size override */
4125 if (mode == X86EMUL_MODE_PROT64)
4126 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004127 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004128 else
4129 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004130 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004131 break;
4132 case 0x26: /* ES override */
4133 case 0x2e: /* CS override */
4134 case 0x36: /* SS override */
4135 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004136 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004137 break;
4138 case 0x64: /* FS override */
4139 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004140 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004141 break;
4142 case 0x40 ... 0x4f: /* REX */
4143 if (mode != X86EMUL_MODE_PROT64)
4144 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004145 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004146 continue;
4147 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004148 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004149 break;
4150 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004151 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004152 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004153 break;
4154 default:
4155 goto done_prefixes;
4156 }
4157
4158 /* Any legacy prefix after a REX prefix nullifies its effect. */
4159
Avi Kivity9dac77f2011-06-01 15:34:25 +03004160 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004161 }
4162
4163done_prefixes:
4164
4165 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004166 if (ctxt->rex_prefix & 8)
4167 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004168
4169 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004170 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004171 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004172 if (ctxt->b == 0x0f) {
4173 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004174 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004176 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004177 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004178
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004179 if (ctxt->d & ModRM)
4180 ctxt->modrm = insn_fetch(u8, ctxt);
4181
Avi Kivity9dac77f2011-06-01 15:34:25 +03004182 while (ctxt->d & GroupMask) {
4183 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004184 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004185 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004186 opcode = opcode.u.group[goffset];
4187 break;
4188 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004189 goffset = (ctxt->modrm >> 3) & 7;
4190 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004191 opcode = opcode.u.gdual->mod3[goffset];
4192 else
4193 opcode = opcode.u.gdual->mod012[goffset];
4194 break;
4195 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004196 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004197 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004198 break;
4199 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004200 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004201 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004202 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004203 switch (simd_prefix) {
4204 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4205 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4206 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4207 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4208 }
4209 break;
4210 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004211 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004212 }
Avi Kivity46561642011-04-24 14:09:59 +03004213
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004214 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004215 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004216 }
4217
Avi Kivity9dac77f2011-06-01 15:34:25 +03004218 ctxt->execute = opcode.u.execute;
4219 ctxt->check_perm = opcode.check_perm;
4220 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004221
4222 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004223 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004224 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004225
Avi Kivity9dac77f2011-06-01 15:34:25 +03004226 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004227 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004228
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4230 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004231
Avi Kivity9dac77f2011-06-01 15:34:25 +03004232 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004233 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004234 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004235 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004236 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004237 }
4238
Avi Kivity9dac77f2011-06-01 15:34:25 +03004239 if (ctxt->d & Sse)
4240 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004241 else if (ctxt->d & Mmx)
4242 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004243
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004244 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004245 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004246 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004247 if (!ctxt->has_seg_override)
4248 set_seg_override(ctxt, ctxt->modrm_seg);
4249 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004250 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004251 if (rc != X86EMUL_CONTINUE)
4252 goto done;
4253
Avi Kivity9dac77f2011-06-01 15:34:25 +03004254 if (!ctxt->has_seg_override)
4255 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004256
Avi Kivityf09ed832011-09-13 10:45:40 +03004257 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004258
Avi Kivityf09ed832011-09-13 10:45:40 +03004259 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4260 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004261
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004262 /*
4263 * Decode and fetch the source operand: register, memory
4264 * or immediate.
4265 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004266 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004267 if (rc != X86EMUL_CONTINUE)
4268 goto done;
4269
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004270 /*
4271 * Decode and fetch the second source operand: register, memory
4272 * or immediate.
4273 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004274 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004275 if (rc != X86EMUL_CONTINUE)
4276 goto done;
4277
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004278 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004279 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004280
4281done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004282 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4283 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004284
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004285 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004286}
4287
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004288bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4289{
4290 return ctxt->d & PageTable;
4291}
4292
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004293static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4294{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004295 /* The second termination condition only applies for REPE
4296 * and REPNE. Test if the repeat string operation prefix is
4297 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4298 * corresponding termination condition according to:
4299 * - if REPE/REPZ and ZF = 0 then done
4300 * - if REPNE/REPNZ and ZF = 1 then done
4301 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004302 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4303 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4304 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004305 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004306 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004307 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4308 return true;
4309
4310 return false;
4311}
4312
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004313static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4314{
4315 bool fault = false;
4316
4317 ctxt->ops->get_fpu(ctxt);
4318 asm volatile("1: fwait \n\t"
4319 "2: \n\t"
4320 ".pushsection .fixup,\"ax\" \n\t"
4321 "3: \n\t"
4322 "movb $1, %[fault] \n\t"
4323 "jmp 2b \n\t"
4324 ".popsection \n\t"
4325 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004326 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004327 ctxt->ops->put_fpu(ctxt);
4328
4329 if (unlikely(fault))
4330 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4331
4332 return X86EMUL_CONTINUE;
4333}
4334
4335static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4336 struct operand *op)
4337{
4338 if (op->type == OP_MM)
4339 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4340}
4341
Avi Kivitydd856ef2012-08-27 23:46:17 +03004342
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004343int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004344{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03004345 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004346 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004347 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004348
Avi Kivity9dac77f2011-06-01 15:34:25 +03004349 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004350
Avi Kivity9dac77f2011-06-01 15:34:25 +03004351 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004352 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004353 goto done;
4354 }
4355
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004356 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004358 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004359 goto done;
4360 }
4361
Avi Kivity9dac77f2011-06-01 15:34:25 +03004362 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004363 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004364 goto done;
4365 }
4366
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004367 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4368 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004369 rc = emulate_ud(ctxt);
4370 goto done;
4371 }
4372
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004373 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004374 rc = emulate_nm(ctxt);
4375 goto done;
4376 }
4377
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004378 if (ctxt->d & Mmx) {
4379 rc = flush_pending_x87_faults(ctxt);
4380 if (rc != X86EMUL_CONTINUE)
4381 goto done;
4382 /*
4383 * Now that we know the fpu is exception safe, we can fetch
4384 * operands from it.
4385 */
4386 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4387 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4388 if (!(ctxt->d & Mov))
4389 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4390 }
4391
Avi Kivity9dac77f2011-06-01 15:34:25 +03004392 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4393 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004394 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004395 if (rc != X86EMUL_CONTINUE)
4396 goto done;
4397 }
4398
Gleb Natapove92805a2010-02-10 14:21:35 +02004399 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004400 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004401 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004402 goto done;
4403 }
4404
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004405 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004406 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004407 rc = emulate_ud(ctxt);
4408 goto done;
4409 }
4410
Joerg Roedeld09beab2011-04-04 12:39:25 +02004411 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 if (ctxt->check_perm) {
4413 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004414 if (rc != X86EMUL_CONTINUE)
4415 goto done;
4416 }
4417
Avi Kivity9dac77f2011-06-01 15:34:25 +03004418 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4419 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004420 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004421 if (rc != X86EMUL_CONTINUE)
4422 goto done;
4423 }
4424
Avi Kivity9dac77f2011-06-01 15:34:25 +03004425 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004426 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004427 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004428 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004429 goto done;
4430 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004431 }
4432
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4434 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4435 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004436 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004437 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004438 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004439 }
4440
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 if (ctxt->src2.type == OP_MEM) {
4442 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4443 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004444 if (rc != X86EMUL_CONTINUE)
4445 goto done;
4446 }
4447
Avi Kivity9dac77f2011-06-01 15:34:25 +03004448 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004449 goto special_insn;
4450
4451
Avi Kivity9dac77f2011-06-01 15:34:25 +03004452 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004453 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004454 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4455 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004456 if (rc != X86EMUL_CONTINUE)
4457 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004458 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004459 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004460
Avi Kivity018a98d2007-11-27 19:30:56 +02004461special_insn:
4462
Avi Kivity9dac77f2011-06-01 15:34:25 +03004463 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4464 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004465 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004466 if (rc != X86EMUL_CONTINUE)
4467 goto done;
4468 }
4469
Avi Kivity9dac77f2011-06-01 15:34:25 +03004470 if (ctxt->execute) {
4471 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004472 if (rc != X86EMUL_CONTINUE)
4473 goto done;
4474 goto writeback;
4475 }
4476
Avi Kivity9dac77f2011-06-01 15:34:25 +03004477 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478 goto twobyte_insn;
4479
Avi Kivity9dac77f2011-06-01 15:34:25 +03004480 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004481 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004482 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004483 break;
4484 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004485 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004486 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004488 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004490 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004491 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004492 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004493 if (test_cc(ctxt->b, ctxt->eflags))
4494 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004495 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004496 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004497 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004498 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004499 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004500 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004501 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004502 rc = em_xchg(ctxt);
4503 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004504 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004505 switch (ctxt->op_bytes) {
4506 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4507 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4508 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004509 }
4510 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004511 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004512 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004513 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004514 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004515 rc = emulate_int(ctxt, 3);
4516 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004517 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004518 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004519 break;
4520 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004521 if (ctxt->eflags & EFLG_OF)
4522 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004523 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004524 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004525 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004526 break;
4527 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004528 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004529 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004530 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004531 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004532 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004533 jmp_rel(ctxt, ctxt->src.val);
4534 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004535 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004536 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004537 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004538 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004539 case 0xf5: /* cmc */
4540 /* complement carry flag from eflags reg */
4541 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004542 break;
4543 case 0xf8: /* clc */
4544 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004545 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004546 case 0xf9: /* stc */
4547 ctxt->eflags |= EFLG_CF;
4548 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004549 case 0xfc: /* cld */
4550 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004551 break;
4552 case 0xfd: /* std */
4553 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004554 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004555 default:
4556 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004557 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004558
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004559 if (rc != X86EMUL_CONTINUE)
4560 goto done;
4561
Avi Kivity018a98d2007-11-27 19:30:56 +02004562writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004563 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004564 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004565 goto done;
4566
Gleb Natapov5cd21912010-03-18 15:20:26 +02004567 /*
4568 * restore dst type in case the decoding will be reused
4569 * (happens for string instruction )
4570 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004571 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004572
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 if ((ctxt->d & SrcMask) == SrcSI)
4574 string_addr_inc(ctxt, seg_override(ctxt),
4575 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004576
Avi Kivity9dac77f2011-06-01 15:34:25 +03004577 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004578 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004579 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004580
Avi Kivity9dac77f2011-06-01 15:34:25 +03004581 if (ctxt->rep_prefix && (ctxt->d & String)) {
4582 struct read_cache *r = &ctxt->io_read;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004583 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004584
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004585 if (!string_insn_completed(ctxt)) {
4586 /*
4587 * Re-enter guest when pio read ahead buffer is empty
4588 * or, if it is not used, after each 1024 iteration.
4589 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004590 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004591 (r->end == 0 || r->end != r->pos)) {
4592 /*
4593 * Reset read cache. Usually happens before
4594 * decode, but since instruction is restarted
4595 * we have to do it here.
4596 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004597 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004598 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004599 return EMULATION_RESTART;
4600 }
4601 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004602 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004603 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004604
Avi Kivity9dac77f2011-06-01 15:34:25 +03004605 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004606
4607done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004608 if (rc == X86EMUL_PROPAGATE_FAULT)
4609 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004610 if (rc == X86EMUL_INTERCEPTED)
4611 return EMULATION_INTERCEPTED;
4612
Avi Kivitydd856ef2012-08-27 23:46:17 +03004613 if (rc == X86EMUL_CONTINUE)
4614 writeback_registers(ctxt);
4615
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004616 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004617
4618twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004619 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004620 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004621 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004622 break;
4623 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004624 case 0x0d: /* GrpP (prefetch) */
4625 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004626 break;
4627 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004628 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004629 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004634 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4635 if (!test_cc(ctxt->b, ctxt->eflags))
4636 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004637 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004638 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004639 if (test_cc(ctxt->b, ctxt->eflags))
4640 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004641 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004642 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004643 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004644 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004645 case 0xa4: /* shld imm8, r, r/m */
4646 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004647 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004648 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004649 case 0xac: /* shrd imm8, r, r/m */
4650 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004651 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004652 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004653 case 0xae: /* clflush */
4654 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004655 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004657 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004658 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004659 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004661 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004662 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004664 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004665 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004666 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004667 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004668 ctxt->src.val = ctxt->dst.orig_val;
4669 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004670 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004671 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004672 ctxt->dst.bytes = ctxt->op_bytes;
4673 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4674 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004675 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004676 default:
4677 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004679
4680 if (rc != X86EMUL_CONTINUE)
4681 goto done;
4682
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683 goto writeback;
4684
4685cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004686 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004687}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004688
4689void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4690{
4691 invalidate_registers(ctxt);
4692}
4693
4694void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4695{
4696 writeback_registers(ctxt);
4697}