blob: bba39bfa1c4b03806cf1f42773cd8041f18b8759 [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);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200164 const struct opcode *group;
165 const struct group_dual *gdual;
166 const struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300167 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200168 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300169};
170
171struct group_dual {
172 struct opcode mod012[8];
173 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300174};
175
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200176struct gprefix {
177 struct opcode pfx_no;
178 struct opcode pfx_66;
179 struct opcode pfx_f2;
180 struct opcode pfx_f3;
181};
182
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200184#define EFLG_ID (1<<21)
185#define EFLG_VIP (1<<20)
186#define EFLG_VIF (1<<19)
187#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200188#define EFLG_VM (1<<17)
189#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200190#define EFLG_IOPL (3<<12)
191#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192#define EFLG_OF (1<<11)
193#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200194#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200195#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196#define EFLG_SF (1<<7)
197#define EFLG_ZF (1<<6)
198#define EFLG_AF (1<<4)
199#define EFLG_PF (1<<2)
200#define EFLG_CF (1<<0)
201
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300202#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
203#define EFLG_RESERVED_ONE_MASK 2
204
Avi Kivitydd856ef2012-08-27 23:46:17 +0300205static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
206{
207 if (!(ctxt->regs_valid & (1 << nr))) {
208 ctxt->regs_valid |= 1 << nr;
209 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
210 }
211 return ctxt->_regs[nr];
212}
213
214static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
215{
216 ctxt->regs_valid |= 1 << nr;
217 ctxt->regs_dirty |= 1 << nr;
218 return &ctxt->_regs[nr];
219}
220
221static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
222{
223 reg_read(ctxt, nr);
224 return reg_write(ctxt, nr);
225}
226
227static void writeback_registers(struct x86_emulate_ctxt *ctxt)
228{
229 unsigned reg;
230
231 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
232 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
233}
234
235static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
236{
237 ctxt->regs_dirty = 0;
238 ctxt->regs_valid = 0;
239}
240
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241/*
242 * Instruction emulation:
243 * Most instructions are emulated directly via a fragment of inline assembly
244 * code. This allows us to save/restore EFLAGS and thus very easily pick up
245 * any modified flags.
246 */
247
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800248#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249#define _LO32 "k" /* force 32-bit operand */
250#define _STK "%%rsp" /* stack pointer */
251#elif defined(__i386__)
252#define _LO32 "" /* force 32-bit operand */
253#define _STK "%%esp" /* stack pointer */
254#endif
255
256/*
257 * These EFLAGS bits are restored from saved value during emulation, and
258 * any changes are written back to the saved value after emulation.
259 */
260#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
261
262/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200263#define _PRE_EFLAGS(_sav, _msk, _tmp) \
264 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
265 "movl %"_sav",%"_LO32 _tmp"; " \
266 "push %"_tmp"; " \
267 "push %"_tmp"; " \
268 "movl %"_msk",%"_LO32 _tmp"; " \
269 "andl %"_LO32 _tmp",("_STK"); " \
270 "pushf; " \
271 "notl %"_LO32 _tmp"; " \
272 "andl %"_LO32 _tmp",("_STK"); " \
273 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
274 "pop %"_tmp"; " \
275 "orl %"_LO32 _tmp",("_STK"); " \
276 "popf; " \
277 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278
279/* After executing instruction: write-back necessary bits in EFLAGS. */
280#define _POST_EFLAGS(_sav, _msk, _tmp) \
281 /* _sav |= EFLAGS & _msk; */ \
282 "pushf; " \
283 "pop %"_tmp"; " \
284 "andl %"_msk",%"_LO32 _tmp"; " \
285 "orl %"_LO32 _tmp",%"_sav"; "
286
Avi Kivitydda96d82008-11-26 15:14:10 +0200287#ifdef CONFIG_X86_64
288#define ON64(x) x
289#else
290#define ON64(x)
291#endif
292
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300293#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200294 do { \
295 __asm__ __volatile__ ( \
296 _PRE_EFLAGS("0", "4", "2") \
297 _op _suffix " %"_x"3,%1; " \
298 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300299 : "=m" ((ctxt)->eflags), \
300 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200301 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300302 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200303 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200304
305
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300307#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200308 do { \
309 unsigned long _tmp; \
310 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300311 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200312 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300313 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200314 break; \
315 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300316 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200317 break; \
318 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300319 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200320 break; \
321 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 } while (0)
323
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300324#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200326 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300327 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300329 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 break; \
331 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300332 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 _wx, _wy, _lx, _ly, _qx, _qy); \
334 break; \
335 } \
336 } while (0)
337
338/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300339#define emulate_2op_SrcB(ctxt, _op) \
340 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341
342/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300343#define emulate_2op_SrcV(ctxt, _op) \
344 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345
346/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300347#define emulate_2op_SrcV_nobyte(ctxt, _op) \
348 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100350/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300351#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300352 do { \
353 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300354 _type _clv = (ctxt)->src2.val; \
355 _type _srcv = (ctxt)->src.val; \
356 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300357 \
358 __asm__ __volatile__ ( \
359 _PRE_EFLAGS("0", "5", "2") \
360 _op _suffix " %4,%1 \n" \
361 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300362 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300363 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
364 ); \
365 \
Avi Kivity761441b2011-09-07 16:41:36 +0300366 (ctxt)->src2.val = (unsigned long) _clv; \
367 (ctxt)->src2.val = (unsigned long) _srcv; \
368 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100369 } while (0)
370
Avi Kivity761441b2011-09-07 16:41:36 +0300371#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300372 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300373 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300374 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300375 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300376 break; \
377 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300378 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300379 break; \
380 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300381 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300382 break; \
383 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100384 } while (0)
385
Avi Kivityd1eef452011-09-07 16:41:38 +0300386#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387 do { \
388 unsigned long _tmp; \
389 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200390 __asm__ __volatile__ ( \
391 _PRE_EFLAGS("0", "3", "2") \
392 _op _suffix " %1; " \
393 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300394 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200395 "=&r" (_tmp) \
396 : "i" (EFLAGS_MASK)); \
397 } while (0)
398
399/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300400#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200401 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300402 switch ((ctxt)->dst.bytes) { \
403 case 1: __emulate_1op(ctxt, _op, "b"); break; \
404 case 2: __emulate_1op(ctxt, _op, "w"); break; \
405 case 4: __emulate_1op(ctxt, _op, "l"); break; \
406 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 } \
408 } while (0)
409
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300410#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300411 do { \
412 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300413 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
414 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300415 \
416 __asm__ __volatile__ ( \
417 _PRE_EFLAGS("0", "5", "1") \
418 "1: \n\t" \
419 _op _suffix " %6; " \
420 "2: \n\t" \
421 _POST_EFLAGS("0", "5", "1") \
422 ".pushsection .fixup,\"ax\" \n\t" \
423 "3: movb $1, %4 \n\t" \
424 "jmp 2b \n\t" \
425 ".popsection \n\t" \
426 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300427 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
428 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800429 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300430 } while (0)
431
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300432/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300433#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300434 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300435 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300436 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300437 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300438 break; \
439 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300440 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300441 break; \
442 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300443 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300444 break; \
445 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300446 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300447 break; \
448 } \
449 } while (0)
450
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200451static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
452 enum x86_intercept intercept,
453 enum x86_intercept_stage stage)
454{
455 struct x86_instruction_info info = {
456 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300457 .rep_prefix = ctxt->rep_prefix,
458 .modrm_mod = ctxt->modrm_mod,
459 .modrm_reg = ctxt->modrm_reg,
460 .modrm_rm = ctxt->modrm_rm,
461 .src_val = ctxt->src.val64,
462 .src_bytes = ctxt->src.bytes,
463 .dst_bytes = ctxt->dst.bytes,
464 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200465 .next_rip = ctxt->eip,
466 };
467
Avi Kivity29535382011-04-20 13:37:53 +0300468 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200469}
470
Avi Kivityf47cfa32012-06-07 17:49:24 +0300471static void assign_masked(ulong *dest, ulong src, ulong mask)
472{
473 *dest = (*dest & ~mask) | (src & mask);
474}
475
Avi Kivity9dac77f2011-06-01 15:34:25 +0300476static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800477{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300478 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800479}
480
Avi Kivityf47cfa32012-06-07 17:49:24 +0300481static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
482{
483 u16 sel;
484 struct desc_struct ss;
485
486 if (ctxt->mode == X86EMUL_MODE_PROT64)
487 return ~0UL;
488 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
489 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
490}
491
Avi Kivity612e89f2012-06-12 20:03:23 +0300492static int stack_size(struct x86_emulate_ctxt *ctxt)
493{
494 return (__fls(stack_mask(ctxt)) + 1) >> 3;
495}
496
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800498static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300499address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800500{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300501 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800502 return reg;
503 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300504 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800505}
506
507static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300508register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800509{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300510 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800511}
512
Avi Kivity5ad105e2012-08-19 14:34:31 +0300513static void masked_increment(ulong *reg, ulong mask, int inc)
514{
515 assign_masked(reg, *reg + inc, mask);
516}
517
Harvey Harrison7a9572752008-02-19 07:40:41 -0800518static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300519register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800520{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300521 ulong mask;
522
Avi Kivity9dac77f2011-06-01 15:34:25 +0300523 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300524 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800525 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300526 mask = ad_mask(ctxt);
527 masked_increment(reg, mask, inc);
528}
529
530static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
531{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300532 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800533}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534
Avi Kivity9dac77f2011-06-01 15:34:25 +0300535static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800536{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300537 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800538}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300539
Avi Kivity56697682011-04-03 14:08:51 +0300540static u32 desc_limit_scaled(struct desc_struct *desc)
541{
542 u32 limit = get_desc_limit(desc);
543
544 return desc->g ? (limit << 12) | 0xfff : limit;
545}
546
Avi Kivity9dac77f2011-06-01 15:34:25 +0300547static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300548{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300549 ctxt->has_seg_override = true;
550 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300551}
552
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900553static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300554{
555 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
556 return 0;
557
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900558 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300559}
560
Avi Kivity9dac77f2011-06-01 15:34:25 +0300561static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300562{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300563 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300564 return 0;
565
Avi Kivity9dac77f2011-06-01 15:34:25 +0300566 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300567}
568
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200569static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
570 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300571{
Avi Kivityda9cb572010-11-22 17:53:21 +0200572 ctxt->exception.vector = vec;
573 ctxt->exception.error_code = error;
574 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200575 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300576}
577
Joerg Roedel3b88e412011-04-04 12:39:29 +0200578static int emulate_db(struct x86_emulate_ctxt *ctxt)
579{
580 return emulate_exception(ctxt, DB_VECTOR, 0, false);
581}
582
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200583static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300584{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200585 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300586}
587
Avi Kivity618ff152011-04-03 12:32:09 +0300588static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
589{
590 return emulate_exception(ctxt, SS_VECTOR, err, true);
591}
592
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200593static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300594{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200595 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300596}
597
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200598static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300599{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200600 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300601}
602
Avi Kivity34d1f492010-08-26 11:59:01 +0300603static int emulate_de(struct x86_emulate_ctxt *ctxt)
604{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200605 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300606}
607
Avi Kivity12537912011-03-29 11:41:27 +0200608static int emulate_nm(struct x86_emulate_ctxt *ctxt)
609{
610 return emulate_exception(ctxt, NM_VECTOR, 0, false);
611}
612
Avi Kivity1aa36612011-04-27 13:20:30 +0300613static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
614{
615 u16 selector;
616 struct desc_struct desc;
617
618 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
619 return selector;
620}
621
622static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
623 unsigned seg)
624{
625 u16 dummy;
626 u32 base3;
627 struct desc_struct desc;
628
629 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
630 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
631}
632
Avi Kivity1c11b372012-04-09 18:39:59 +0300633/*
634 * x86 defines three classes of vector instructions: explicitly
635 * aligned, explicitly unaligned, and the rest, which change behaviour
636 * depending on whether they're AVX encoded or not.
637 *
638 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
639 * subject to the same check.
640 */
641static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
642{
643 if (likely(size < 16))
644 return false;
645
646 if (ctxt->d & Aligned)
647 return true;
648 else if (ctxt->d & Unaligned)
649 return false;
650 else if (ctxt->d & Avx)
651 return false;
652 else
653 return true;
654}
655
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400656static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300657 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400658 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300659 ulong *linear)
660{
Avi Kivity618ff152011-04-03 12:32:09 +0300661 struct desc_struct desc;
662 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300663 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300664 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300665 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300666 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300667
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900668 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300669 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300670 case X86EMUL_MODE_PROT64:
671 if (((signed long)la << 16) >> 16 != la)
672 return emulate_gp(ctxt, 0);
673 break;
674 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300675 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
676 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300677 if (!usable)
678 goto bad;
679 /* code segment or read-only data segment */
680 if (((desc.type & 8) || !(desc.type & 2)) && write)
681 goto bad;
682 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400683 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300684 goto bad;
685 lim = desc_limit_scaled(&desc);
686 if ((desc.type & 8) || !(desc.type & 4)) {
687 /* expand-up segment */
688 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
689 goto bad;
690 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800691 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300692 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
693 goto bad;
694 lim = desc.d ? 0xffffffff : 0xffff;
695 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
696 goto bad;
697 }
Avi Kivity717746e2011-04-20 13:37:53 +0300698 cpl = ctxt->ops->cpl(ctxt);
Avi Kivitya5625182012-08-21 17:07:05 +0300699 if (ctxt->mode == X86EMUL_MODE_REAL)
700 rpl = 0;
701 else
702 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)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300727 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300728 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300729 return emulate_gp(ctxt, sel);
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) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200911 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
912 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
913 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
914 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
915 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
916 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
917 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
918 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200919#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200920 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
921 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
922 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
923 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
924 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
925 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
926 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
927 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200928#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) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200939 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
940 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
941 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
942 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
943 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
944 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
945 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
946 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200947#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200948 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
949 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
950 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
951 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
952 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
953 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
954 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
955 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200956#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
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001303 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1304 ctxt->dst.data = rc->data + rc->pos;
1305 ctxt->dst.type = OP_MEM_STR;
1306 ctxt->dst.count = (rc->end - rc->pos) / size;
1307 rc->pos = rc->end;
1308 } else {
1309 memcpy(dest, rc->data + rc->pos, size);
1310 rc->pos += size;
1311 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001312 return 1;
1313}
1314
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001315static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1316 u16 index, struct desc_struct *desc)
1317{
1318 struct desc_ptr dt;
1319 ulong addr;
1320
1321 ctxt->ops->get_idt(ctxt, &dt);
1322
1323 if (dt.size < index * 8 + 7)
1324 return emulate_gp(ctxt, index << 3 | 0x2);
1325
1326 addr = dt.address + index * 8;
1327 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1328 &ctxt->exception);
1329}
1330
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001331static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001332 u16 selector, struct desc_ptr *dt)
1333{
Mathias Krause0225fb52012-08-30 01:30:16 +02001334 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001335
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001336 if (selector & 1 << 2) {
1337 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001338 u16 sel;
1339
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001340 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001341 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001342 return;
1343
1344 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1345 dt->address = get_desc_base(&desc);
1346 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001347 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001348}
1349
1350/* allowed just for 8 bytes segments */
1351static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001352 u16 selector, struct desc_struct *desc,
1353 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001354{
1355 struct desc_ptr dt;
1356 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001357 ulong addr;
1358
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001359 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001360
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001361 if (dt.size < index * 8 + 7)
1362 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001363
Avi Kivitye9194642012-06-13 16:29:39 +03001364 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001365 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1366 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001367}
1368
1369/* allowed just for 8 bytes segments */
1370static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001371 u16 selector, struct desc_struct *desc)
1372{
1373 struct desc_ptr dt;
1374 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001375 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001376
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001377 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001378
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001379 if (dt.size < index * 8 + 7)
1380 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001381
1382 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001383 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1384 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001385}
1386
Gleb Natapov5601d052011-03-07 14:55:06 +02001387/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001388static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001389 u16 selector, int seg)
1390{
Avi Kivity869be992012-06-13 16:30:53 +03001391 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001392 u8 dpl, rpl, cpl;
1393 unsigned err_vec = GP_VECTOR;
1394 u32 err_code = 0;
1395 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001396 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001397 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001398 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001399
1400 memset(&seg_desc, 0, sizeof seg_desc);
1401
1402 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1403 || ctxt->mode == X86EMUL_MODE_REAL) {
1404 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001405 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001407 goto load;
1408 }
1409
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001410 rpl = selector & 3;
1411 cpl = ctxt->ops->cpl(ctxt);
1412
1413 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1414 if ((seg == VCPU_SREG_CS
1415 || (seg == VCPU_SREG_SS
1416 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1417 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001418 && null_selector)
1419 goto exception;
1420
1421 /* TR should be in GDT only */
1422 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1423 goto exception;
1424
1425 if (null_selector) /* for NULL selector skip all following checks */
1426 goto load;
1427
Avi Kivitye9194642012-06-13 16:29:39 +03001428 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001429 if (ret != X86EMUL_CONTINUE)
1430 return ret;
1431
1432 err_code = selector & 0xfffc;
1433 err_vec = GP_VECTOR;
1434
Guo Chaofc058682012-06-28 15:19:51 +08001435 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001436 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1437 goto exception;
1438
1439 if (!seg_desc.p) {
1440 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1441 goto exception;
1442 }
1443
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001444 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001445
1446 switch (seg) {
1447 case VCPU_SREG_SS:
1448 /*
1449 * segment is not a writable data segment or segment
1450 * selector's RPL != CPL or segment selector's RPL != CPL
1451 */
1452 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1453 goto exception;
1454 break;
1455 case VCPU_SREG_CS:
1456 if (!(seg_desc.type & 8))
1457 goto exception;
1458
1459 if (seg_desc.type & 4) {
1460 /* conforming */
1461 if (dpl > cpl)
1462 goto exception;
1463 } else {
1464 /* nonconforming */
1465 if (rpl > cpl || dpl != cpl)
1466 goto exception;
1467 }
1468 /* CS(RPL) <- CPL */
1469 selector = (selector & 0xfffc) | cpl;
1470 break;
1471 case VCPU_SREG_TR:
1472 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1473 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001474 old_desc = seg_desc;
1475 seg_desc.type |= 2; /* busy */
1476 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1477 sizeof(seg_desc), &ctxt->exception);
1478 if (ret != X86EMUL_CONTINUE)
1479 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001480 break;
1481 case VCPU_SREG_LDTR:
1482 if (seg_desc.s || seg_desc.type != 2)
1483 goto exception;
1484 break;
1485 default: /* DS, ES, FS, or GS */
1486 /*
1487 * segment is not a data or readable code segment or
1488 * ((segment is a data or nonconforming code segment)
1489 * and (both RPL and CPL > DPL))
1490 */
1491 if ((seg_desc.type & 0xa) == 0x8 ||
1492 (((seg_desc.type & 0xc) != 0xc) &&
1493 (rpl > dpl && cpl > dpl)))
1494 goto exception;
1495 break;
1496 }
1497
1498 if (seg_desc.s) {
1499 /* mark segment as accessed */
1500 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001501 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001502 if (ret != X86EMUL_CONTINUE)
1503 return ret;
1504 }
1505load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001506 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001507 return X86EMUL_CONTINUE;
1508exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001509 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001510 return X86EMUL_PROPAGATE_FAULT;
1511}
1512
Wei Yongjun31be40b2010-08-17 09:17:30 +08001513static void write_register_operand(struct operand *op)
1514{
1515 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1516 switch (op->bytes) {
1517 case 1:
1518 *(u8 *)op->addr.reg = (u8)op->val;
1519 break;
1520 case 2:
1521 *(u16 *)op->addr.reg = (u16)op->val;
1522 break;
1523 case 4:
1524 *op->addr.reg = (u32)op->val;
1525 break; /* 64b: zero-extend */
1526 case 8:
1527 *op->addr.reg = op->val;
1528 break;
1529 }
1530}
1531
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001532static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001533{
1534 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001535
Avi Kivity9dac77f2011-06-01 15:34:25 +03001536 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001537 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001538 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001539 break;
1540 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001541 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001542 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001543 ctxt->dst.addr.mem,
1544 &ctxt->dst.orig_val,
1545 &ctxt->dst.val,
1546 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001547 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001548 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001549 ctxt->dst.addr.mem,
1550 &ctxt->dst.val,
1551 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001552 if (rc != X86EMUL_CONTINUE)
1553 return rc;
1554 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001555 case OP_MEM_STR:
1556 rc = segmented_write(ctxt,
1557 ctxt->dst.addr.mem,
1558 ctxt->dst.data,
1559 ctxt->dst.bytes * ctxt->dst.count);
1560 if (rc != X86EMUL_CONTINUE)
1561 return rc;
1562 break;
Avi Kivity12537912011-03-29 11:41:27 +02001563 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001564 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001565 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001566 case OP_MM:
1567 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1568 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001569 case OP_NONE:
1570 /* no writeback */
1571 break;
1572 default:
1573 break;
1574 }
1575 return X86EMUL_CONTINUE;
1576}
1577
Avi Kivity51ddff52012-06-12 20:19:40 +03001578static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001579{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001580 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001581
Avi Kivity5ad105e2012-08-19 14:34:31 +03001582 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001583 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001584 addr.seg = VCPU_SREG_SS;
1585
Avi Kivity51ddff52012-06-12 20:19:40 +03001586 return segmented_write(ctxt, addr, data, bytes);
1587}
1588
1589static int em_push(struct x86_emulate_ctxt *ctxt)
1590{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001591 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001592 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001593 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001594}
1595
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001596static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001597 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001598{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001599 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001600 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001601
Avi Kivitydd856ef2012-08-27 23:46:17 +03001602 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001603 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001604 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001605 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001606 return rc;
1607
Avi Kivity5ad105e2012-08-19 14:34:31 +03001608 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001609 return rc;
1610}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001611
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001612static int em_pop(struct x86_emulate_ctxt *ctxt)
1613{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001614 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001615}
1616
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001617static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001618 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001619{
1620 int rc;
1621 unsigned long val, change_mask;
1622 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001623 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001624
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001625 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001626 if (rc != X86EMUL_CONTINUE)
1627 return rc;
1628
1629 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1630 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1631
1632 switch(ctxt->mode) {
1633 case X86EMUL_MODE_PROT64:
1634 case X86EMUL_MODE_PROT32:
1635 case X86EMUL_MODE_PROT16:
1636 if (cpl == 0)
1637 change_mask |= EFLG_IOPL;
1638 if (cpl <= iopl)
1639 change_mask |= EFLG_IF;
1640 break;
1641 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001642 if (iopl < 3)
1643 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001644 change_mask |= EFLG_IF;
1645 break;
1646 default: /* real mode */
1647 change_mask |= (EFLG_IOPL | EFLG_IF);
1648 break;
1649 }
1650
1651 *(unsigned long *)dest =
1652 (ctxt->eflags & ~change_mask) | (val & change_mask);
1653
1654 return rc;
1655}
1656
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001657static int em_popf(struct x86_emulate_ctxt *ctxt)
1658{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001659 ctxt->dst.type = OP_REG;
1660 ctxt->dst.addr.reg = &ctxt->eflags;
1661 ctxt->dst.bytes = ctxt->op_bytes;
1662 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001663}
1664
Avi Kivity612e89f2012-06-12 20:03:23 +03001665static int em_enter(struct x86_emulate_ctxt *ctxt)
1666{
1667 int rc;
1668 unsigned frame_size = ctxt->src.val;
1669 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001670 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001671
1672 if (nesting_level)
1673 return X86EMUL_UNHANDLEABLE;
1674
Avi Kivitydd856ef2012-08-27 23:46:17 +03001675 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1676 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001677 if (rc != X86EMUL_CONTINUE)
1678 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001679 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001680 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001681 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1682 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001683 stack_mask(ctxt));
1684 return X86EMUL_CONTINUE;
1685}
1686
Avi Kivityf47cfa32012-06-07 17:49:24 +03001687static int em_leave(struct x86_emulate_ctxt *ctxt)
1688{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001689 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001690 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001691 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001692}
1693
Avi Kivity1cd196e2011-09-13 10:45:51 +03001694static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001695{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001696 int seg = ctxt->src2.val;
1697
Avi Kivity9dac77f2011-06-01 15:34:25 +03001698 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001699
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001700 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001701}
1702
Avi Kivity1cd196e2011-09-13 10:45:51 +03001703static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001704{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001705 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001706 unsigned long selector;
1707 int rc;
1708
Avi Kivity9dac77f2011-06-01 15:34:25 +03001709 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001710 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001711 return rc;
1712
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001713 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001714 return rc;
1715}
1716
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001717static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001718{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001719 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001720 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001721 int reg = VCPU_REGS_RAX;
1722
1723 while (reg <= VCPU_REGS_RDI) {
1724 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001725 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001726
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001727 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001728 if (rc != X86EMUL_CONTINUE)
1729 return rc;
1730
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001731 ++reg;
1732 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001733
Wei Yongjunc37eda12010-06-15 09:03:33 +08001734 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001735}
1736
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001737static int em_pushf(struct x86_emulate_ctxt *ctxt)
1738{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001739 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001740 return em_push(ctxt);
1741}
1742
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001743static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001744{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001745 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001746 int reg = VCPU_REGS_RDI;
1747
1748 while (reg >= VCPU_REGS_RAX) {
1749 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001750 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001751 --reg;
1752 }
1753
Avi Kivitydd856ef2012-08-27 23:46:17 +03001754 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001755 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001756 break;
1757 --reg;
1758 }
1759 return rc;
1760}
1761
Avi Kivitydd856ef2012-08-27 23:46:17 +03001762static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001763{
Mathias Krause0225fb52012-08-30 01:30:16 +02001764 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001765 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001766 struct desc_ptr dt;
1767 gva_t cs_addr;
1768 gva_t eip_addr;
1769 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001770
1771 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001772 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001773 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001774 if (rc != X86EMUL_CONTINUE)
1775 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001776
1777 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1778
Avi Kivity9dac77f2011-06-01 15:34:25 +03001779 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001780 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001781 if (rc != X86EMUL_CONTINUE)
1782 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001783
Avi Kivity9dac77f2011-06-01 15:34:25 +03001784 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001785 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001786 if (rc != X86EMUL_CONTINUE)
1787 return rc;
1788
Avi Kivity4bff1e862011-04-20 13:37:53 +03001789 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001790
1791 eip_addr = dt.address + (irq << 2);
1792 cs_addr = dt.address + (irq << 2) + 2;
1793
Avi Kivity0f65dd72011-04-20 13:37:53 +03001794 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001795 if (rc != X86EMUL_CONTINUE)
1796 return rc;
1797
Avi Kivity0f65dd72011-04-20 13:37:53 +03001798 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001799 if (rc != X86EMUL_CONTINUE)
1800 return rc;
1801
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001802 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001803 if (rc != X86EMUL_CONTINUE)
1804 return rc;
1805
Avi Kivity9dac77f2011-06-01 15:34:25 +03001806 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001807
1808 return rc;
1809}
1810
Avi Kivitydd856ef2012-08-27 23:46:17 +03001811int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1812{
1813 int rc;
1814
1815 invalidate_registers(ctxt);
1816 rc = __emulate_int_real(ctxt, irq);
1817 if (rc == X86EMUL_CONTINUE)
1818 writeback_registers(ctxt);
1819 return rc;
1820}
1821
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001822static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001823{
1824 switch(ctxt->mode) {
1825 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001826 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001827 case X86EMUL_MODE_VM86:
1828 case X86EMUL_MODE_PROT16:
1829 case X86EMUL_MODE_PROT32:
1830 case X86EMUL_MODE_PROT64:
1831 default:
1832 /* Protected mode interrupts unimplemented yet */
1833 return X86EMUL_UNHANDLEABLE;
1834 }
1835}
1836
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001837static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001838{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001839 int rc = X86EMUL_CONTINUE;
1840 unsigned long temp_eip = 0;
1841 unsigned long temp_eflags = 0;
1842 unsigned long cs = 0;
1843 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1844 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1845 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1846 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1847
1848 /* TODO: Add stack limit check */
1849
Avi Kivity9dac77f2011-06-01 15:34:25 +03001850 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001851
1852 if (rc != X86EMUL_CONTINUE)
1853 return rc;
1854
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001855 if (temp_eip & ~0xffff)
1856 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001857
Avi Kivity9dac77f2011-06-01 15:34:25 +03001858 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001859
1860 if (rc != X86EMUL_CONTINUE)
1861 return rc;
1862
Avi Kivity9dac77f2011-06-01 15:34:25 +03001863 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001864
1865 if (rc != X86EMUL_CONTINUE)
1866 return rc;
1867
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001868 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001869
1870 if (rc != X86EMUL_CONTINUE)
1871 return rc;
1872
Avi Kivity9dac77f2011-06-01 15:34:25 +03001873 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001874
1875
Avi Kivity9dac77f2011-06-01 15:34:25 +03001876 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001877 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001878 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001879 ctxt->eflags &= ~0xffff;
1880 ctxt->eflags |= temp_eflags;
1881 }
1882
1883 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1884 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1885
1886 return rc;
1887}
1888
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001889static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001890{
1891 switch(ctxt->mode) {
1892 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001893 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001894 case X86EMUL_MODE_VM86:
1895 case X86EMUL_MODE_PROT16:
1896 case X86EMUL_MODE_PROT32:
1897 case X86EMUL_MODE_PROT64:
1898 default:
1899 /* iret from protected mode unimplemented yet */
1900 return X86EMUL_UNHANDLEABLE;
1901 }
1902}
1903
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001904static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1905{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001906 int rc;
1907 unsigned short sel;
1908
Avi Kivity9dac77f2011-06-01 15:34:25 +03001909 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001910
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001911 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001912 if (rc != X86EMUL_CONTINUE)
1913 return rc;
1914
Avi Kivity9dac77f2011-06-01 15:34:25 +03001915 ctxt->_eip = 0;
1916 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001917 return X86EMUL_CONTINUE;
1918}
1919
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001920static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001921{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001922 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001923 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001924 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001925 break;
1926 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001927 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001928 break;
1929 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001930 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001931 break;
1932 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001933 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001934 break;
1935 case 4: /* sal/shl */
1936 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001937 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001938 break;
1939 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001940 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001941 break;
1942 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001943 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001944 break;
1945 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001946 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001947}
1948
Avi Kivity3329ece2011-09-13 10:45:39 +03001949static int em_not(struct x86_emulate_ctxt *ctxt)
1950{
1951 ctxt->dst.val = ~ctxt->dst.val;
1952 return X86EMUL_CONTINUE;
1953}
1954
1955static int em_neg(struct x86_emulate_ctxt *ctxt)
1956{
1957 emulate_1op(ctxt, "neg");
1958 return X86EMUL_CONTINUE;
1959}
1960
1961static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1962{
1963 u8 ex = 0;
1964
1965 emulate_1op_rax_rdx(ctxt, "mul", ex);
1966 return X86EMUL_CONTINUE;
1967}
1968
1969static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1970{
1971 u8 ex = 0;
1972
1973 emulate_1op_rax_rdx(ctxt, "imul", ex);
1974 return X86EMUL_CONTINUE;
1975}
1976
1977static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001978{
Avi Kivity34d1f492010-08-26 11:59:01 +03001979 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001980
Avi Kivity3329ece2011-09-13 10:45:39 +03001981 emulate_1op_rax_rdx(ctxt, "div", de);
1982 if (de)
1983 return emulate_de(ctxt);
1984 return X86EMUL_CONTINUE;
1985}
1986
1987static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1988{
1989 u8 de = 0;
1990
1991 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001992 if (de)
1993 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001994 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001995}
1996
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001997static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001998{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001999 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002000
Avi Kivity9dac77f2011-06-01 15:34:25 +03002001 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002002 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03002003 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002004 break;
2005 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03002006 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002007 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03002008 case 2: /* call near abs */ {
2009 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002010 old_eip = ctxt->_eip;
2011 ctxt->_eip = ctxt->src.val;
2012 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002013 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002014 break;
2015 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002016 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002017 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002018 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002019 case 5: /* jmp far */
2020 rc = em_jmp_far(ctxt);
2021 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002022 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002023 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002024 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002025 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002026 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002027}
2028
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002029static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002030{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002031 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002032
Avi Kivitydd856ef2012-08-27 23:46:17 +03002033 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2034 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2035 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2036 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002037 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002038 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002039 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2040 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002041
Laurent Vivier05f086f2007-09-24 11:10:55 +02002042 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002043 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002044 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002045}
2046
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002047static int em_ret(struct x86_emulate_ctxt *ctxt)
2048{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002049 ctxt->dst.type = OP_REG;
2050 ctxt->dst.addr.reg = &ctxt->_eip;
2051 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002052 return em_pop(ctxt);
2053}
2054
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002055static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002056{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002057 int rc;
2058 unsigned long cs;
2059
Avi Kivity9dac77f2011-06-01 15:34:25 +03002060 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002061 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002062 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002063 if (ctxt->op_bytes == 4)
2064 ctxt->_eip = (u32)ctxt->_eip;
2065 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002066 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002067 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002068 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002069 return rc;
2070}
2071
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002072static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2073{
2074 /* Save real source value, then compare EAX against destination. */
2075 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002076 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002077 emulate_2op_SrcV(ctxt, "cmp");
2078
2079 if (ctxt->eflags & EFLG_ZF) {
2080 /* Success: write back to memory. */
2081 ctxt->dst.val = ctxt->src.orig_val;
2082 } else {
2083 /* Failure: write the value we saw to EAX. */
2084 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002085 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002086 }
2087 return X86EMUL_CONTINUE;
2088}
2089
Avi Kivityd4b43252011-09-13 10:45:50 +03002090static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002091{
Avi Kivityd4b43252011-09-13 10:45:50 +03002092 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002093 unsigned short sel;
2094 int rc;
2095
Avi Kivity9dac77f2011-06-01 15:34:25 +03002096 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002097
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002098 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002099 if (rc != X86EMUL_CONTINUE)
2100 return rc;
2101
Avi Kivity9dac77f2011-06-01 15:34:25 +03002102 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002103 return rc;
2104}
2105
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002106static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002107setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002108 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002109{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002110 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002111 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002112 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002113 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002114 cs->type = 0x0b; /* Read, Execute, Accessed */
2115 cs->s = 1;
2116 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002117 cs->p = 1;
2118 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002119 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002120
Gleb Natapov79168fd2010-04-28 19:15:30 +03002121 set_desc_base(ss, 0); /* flat segment */
2122 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002123 ss->g = 1; /* 4kb granularity */
2124 ss->s = 1;
2125 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002126 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002127 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002128 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002129 ss->l = 0;
2130 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002131}
2132
Avi Kivity1a18a692012-02-01 12:23:21 +02002133static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2134{
2135 u32 eax, ebx, ecx, edx;
2136
2137 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002138 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2139 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002140 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2141 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2142}
2143
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002144static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2145{
Mathias Krause0225fb52012-08-30 01:30:16 +02002146 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002147 u32 eax, ebx, ecx, edx;
2148
2149 /*
2150 * syscall should always be enabled in longmode - so only become
2151 * vendor specific (cpuid) if other modes are active...
2152 */
2153 if (ctxt->mode == X86EMUL_MODE_PROT64)
2154 return true;
2155
2156 eax = 0x00000000;
2157 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002158 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2159 /*
2160 * Intel ("GenuineIntel")
2161 * remark: Intel CPUs only support "syscall" in 64bit
2162 * longmode. Also an 64bit guest with a
2163 * 32bit compat-app running will #UD !! While this
2164 * behaviour can be fixed (by emulating) into AMD
2165 * response - CPUs of AMD can't behave like Intel.
2166 */
2167 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2168 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2169 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2170 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002171
Avi Kivity0017f932012-06-07 14:10:16 +03002172 /* AMD ("AuthenticAMD") */
2173 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2174 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2175 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2176 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002177
Avi Kivity0017f932012-06-07 14:10:16 +03002178 /* AMD ("AMDisbetter!") */
2179 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2180 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2181 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2182 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002183
2184 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2185 return false;
2186}
2187
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002188static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002189{
Mathias Krause0225fb52012-08-30 01:30:16 +02002190 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002191 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002192 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002193 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002194 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195
2196 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002197 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002198 ctxt->mode == X86EMUL_MODE_VM86)
2199 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002200
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002201 if (!(em_syscall_is_enabled(ctxt)))
2202 return emulate_ud(ctxt);
2203
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002204 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002205 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002207 if (!(efer & EFER_SCE))
2208 return emulate_ud(ctxt);
2209
Avi Kivity717746e2011-04-20 13:37:53 +03002210 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002211 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002212 cs_sel = (u16)(msr_data & 0xfffc);
2213 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002214
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002215 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002216 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002217 cs.l = 1;
2218 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002219 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2220 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002221
Avi Kivitydd856ef2012-08-27 23:46:17 +03002222 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002223 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002224#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002225 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002226
Avi Kivity717746e2011-04-20 13:37:53 +03002227 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002228 ctxt->mode == X86EMUL_MODE_PROT64 ?
2229 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002230 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002231
Avi Kivity717746e2011-04-20 13:37:53 +03002232 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002233 ctxt->eflags &= ~(msr_data | EFLG_RF);
2234#endif
2235 } else {
2236 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002237 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002238 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002239
2240 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2241 }
2242
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002243 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002244}
2245
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002246static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002247{
Mathias Krause0225fb52012-08-30 01:30:16 +02002248 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002249 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002250 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002251 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002252 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002253
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002254 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002255 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002256 if (ctxt->mode == X86EMUL_MODE_REAL)
2257 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002258
Avi Kivity1a18a692012-02-01 12:23:21 +02002259 /*
2260 * Not recognized on AMD in compat mode (but is recognized in legacy
2261 * mode).
2262 */
2263 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2264 && !vendor_intel(ctxt))
2265 return emulate_ud(ctxt);
2266
Andre Przywara8c604352009-06-18 12:56:01 +02002267 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2268 * Therefore, we inject an #UD.
2269 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002270 if (ctxt->mode == X86EMUL_MODE_PROT64)
2271 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002272
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002273 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002274
Avi Kivity717746e2011-04-20 13:37:53 +03002275 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002276 switch (ctxt->mode) {
2277 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002278 if ((msr_data & 0xfffc) == 0x0)
2279 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002280 break;
2281 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002282 if (msr_data == 0x0)
2283 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002284 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002285 default:
2286 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002287 }
2288
2289 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002290 cs_sel = (u16)msr_data;
2291 cs_sel &= ~SELECTOR_RPL_MASK;
2292 ss_sel = cs_sel + 8;
2293 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002294 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002295 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002296 cs.l = 1;
2297 }
2298
Avi Kivity1aa36612011-04-27 13:20:30 +03002299 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2300 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002301
Avi Kivity717746e2011-04-20 13:37:53 +03002302 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002303 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002304
Avi Kivity717746e2011-04-20 13:37:53 +03002305 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002306 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002307
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002308 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002309}
2310
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002311static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002312{
Mathias Krause0225fb52012-08-30 01:30:16 +02002313 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002314 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002315 u64 msr_data;
2316 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002317 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002318
Gleb Natapova0044752010-02-10 14:21:31 +02002319 /* inject #GP if in real mode or Virtual 8086 mode */
2320 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002321 ctxt->mode == X86EMUL_MODE_VM86)
2322 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002323
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002324 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002325
Avi Kivity9dac77f2011-06-01 15:34:25 +03002326 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002327 usermode = X86EMUL_MODE_PROT64;
2328 else
2329 usermode = X86EMUL_MODE_PROT32;
2330
2331 cs.dpl = 3;
2332 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002333 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002334 switch (usermode) {
2335 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002336 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002337 if ((msr_data & 0xfffc) == 0x0)
2338 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002339 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002340 break;
2341 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002342 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002343 if (msr_data == 0x0)
2344 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002345 ss_sel = cs_sel + 8;
2346 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002347 cs.l = 1;
2348 break;
2349 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002350 cs_sel |= SELECTOR_RPL_MASK;
2351 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002352
Avi Kivity1aa36612011-04-27 13:20:30 +03002353 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2354 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002355
Avi Kivitydd856ef2012-08-27 23:46:17 +03002356 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2357 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002358
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002359 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002360}
2361
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002362static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002363{
2364 int iopl;
2365 if (ctxt->mode == X86EMUL_MODE_REAL)
2366 return false;
2367 if (ctxt->mode == X86EMUL_MODE_VM86)
2368 return true;
2369 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002370 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002371}
2372
2373static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002374 u16 port, u16 len)
2375{
Mathias Krause0225fb52012-08-30 01:30:16 +02002376 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002377 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002378 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002379 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002380 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002381 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002382 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002383
Avi Kivity1aa36612011-04-27 13:20:30 +03002384 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002385 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002386 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002387 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002388 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002389 base = get_desc_base(&tr_seg);
2390#ifdef CONFIG_X86_64
2391 base |= ((u64)base3) << 32;
2392#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002393 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002394 if (r != X86EMUL_CONTINUE)
2395 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002396 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002397 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002398 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002399 if (r != X86EMUL_CONTINUE)
2400 return false;
2401 if ((perm >> bit_idx) & mask)
2402 return false;
2403 return true;
2404}
2405
2406static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002407 u16 port, u16 len)
2408{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002409 if (ctxt->perm_ok)
2410 return true;
2411
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002412 if (emulator_bad_iopl(ctxt))
2413 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002414 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002415
2416 ctxt->perm_ok = true;
2417
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002418 return true;
2419}
2420
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422 struct tss_segment_16 *tss)
2423{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002424 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002425 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002426 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2427 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2428 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2429 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2430 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2431 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2432 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2433 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434
Avi Kivity1aa36612011-04-27 13:20:30 +03002435 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2436 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2437 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2438 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2439 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440}
2441
2442static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002443 struct tss_segment_16 *tss)
2444{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002445 int ret;
2446
Avi Kivity9dac77f2011-06-01 15:34:25 +03002447 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002448 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002449 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2450 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2451 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2452 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2453 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2454 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2455 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2456 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002457
2458 /*
2459 * SDM says that segment selectors are loaded before segment
2460 * descriptors
2461 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002462 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2463 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2464 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2465 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2466 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002467
2468 /*
Guo Chaofc058682012-06-28 15:19:51 +08002469 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002470 * it is handled in a context of new task
2471 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002472 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002473 if (ret != X86EMUL_CONTINUE)
2474 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002475 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002476 if (ret != X86EMUL_CONTINUE)
2477 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002478 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002479 if (ret != X86EMUL_CONTINUE)
2480 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002481 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002482 if (ret != X86EMUL_CONTINUE)
2483 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002484 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002485 if (ret != X86EMUL_CONTINUE)
2486 return ret;
2487
2488 return X86EMUL_CONTINUE;
2489}
2490
2491static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002492 u16 tss_selector, u16 old_tss_sel,
2493 ulong old_tss_base, struct desc_struct *new_desc)
2494{
Mathias Krause0225fb52012-08-30 01:30:16 +02002495 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496 struct tss_segment_16 tss_seg;
2497 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002498 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002499
Avi Kivity0f65dd72011-04-20 13:37:53 +03002500 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002501 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002502 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002503 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002504 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002505
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002506 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002507
Avi Kivity0f65dd72011-04-20 13:37:53 +03002508 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002509 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002510 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002511 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002512 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002513
Avi Kivity0f65dd72011-04-20 13:37:53 +03002514 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002515 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002516 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002517 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002519
2520 if (old_tss_sel != 0xffff) {
2521 tss_seg.prev_task_link = old_tss_sel;
2522
Avi Kivity0f65dd72011-04-20 13:37:53 +03002523 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 &tss_seg.prev_task_link,
2525 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002526 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002527 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002528 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530 }
2531
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002532 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002533}
2534
2535static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002536 struct tss_segment_32 *tss)
2537{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002538 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002539 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002541 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2542 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2543 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2544 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2545 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2546 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2547 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2548 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549
Avi Kivity1aa36612011-04-27 13:20:30 +03002550 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2551 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2552 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2553 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2554 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2555 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2556 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557}
2558
2559static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560 struct tss_segment_32 *tss)
2561{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002562 int ret;
2563
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002564 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002565 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002566 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002568
2569 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002570 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2571 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2572 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2573 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2574 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2575 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2576 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2577 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578
2579 /*
2580 * SDM says that segment selectors are loaded before segment
2581 * descriptors
2582 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002583 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2584 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2585 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2586 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2587 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2588 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2589 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590
2591 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002592 * If we're switching between Protected Mode and VM86, we need to make
2593 * sure to update the mode before loading the segment descriptors so
2594 * that the selectors are interpreted correctly.
2595 *
2596 * Need to get rflags to the vcpu struct immediately because it
2597 * influences the CPL which is checked at least when loading the segment
2598 * descriptors and when pushing an error code to the new kernel stack.
2599 *
2600 * TODO Introduce a separate ctxt->ops->set_cpl callback
2601 */
2602 if (ctxt->eflags & X86_EFLAGS_VM)
2603 ctxt->mode = X86EMUL_MODE_VM86;
2604 else
2605 ctxt->mode = X86EMUL_MODE_PROT32;
2606
2607 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2608
2609 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 * Now load segment descriptors. If fault happenes at this stage
2611 * it is handled in a context of new task
2612 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002613 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 if (ret != X86EMUL_CONTINUE)
2615 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002616 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 if (ret != X86EMUL_CONTINUE)
2618 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002619 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 if (ret != X86EMUL_CONTINUE)
2621 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002622 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 if (ret != X86EMUL_CONTINUE)
2624 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002625 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 if (ret != X86EMUL_CONTINUE)
2627 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002628 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 if (ret != X86EMUL_CONTINUE)
2630 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002631 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 if (ret != X86EMUL_CONTINUE)
2633 return ret;
2634
2635 return X86EMUL_CONTINUE;
2636}
2637
2638static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639 u16 tss_selector, u16 old_tss_sel,
2640 ulong old_tss_base, struct desc_struct *new_desc)
2641{
Mathias Krause0225fb52012-08-30 01:30:16 +02002642 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002643 struct tss_segment_32 tss_seg;
2644 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002645 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646
Avi Kivity0f65dd72011-04-20 13:37:53 +03002647 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002648 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002649 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002653 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654
Avi Kivity0f65dd72011-04-20 13:37:53 +03002655 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002656 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002657 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660
Avi Kivity0f65dd72011-04-20 13:37:53 +03002661 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002662 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002663 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666
2667 if (old_tss_sel != 0xffff) {
2668 tss_seg.prev_task_link = old_tss_sel;
2669
Avi Kivity0f65dd72011-04-20 13:37:53 +03002670 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 &tss_seg.prev_task_link,
2672 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002673 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002674 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677 }
2678
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002679 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680}
2681
2682static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002683 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002684 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685{
Mathias Krause0225fb52012-08-30 01:30:16 +02002686 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 struct desc_struct curr_tss_desc, next_tss_desc;
2688 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002689 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002691 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002692 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002693 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002694
2695 /* FIXME: old_tss_base == ~0 ? */
2696
Avi Kivitye9194642012-06-13 16:29:39 +03002697 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002698 if (ret != X86EMUL_CONTINUE)
2699 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002700 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002701 if (ret != X86EMUL_CONTINUE)
2702 return ret;
2703
2704 /* FIXME: check that next_tss_desc is tss */
2705
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002706 /*
2707 * Check privileges. The three cases are task switch caused by...
2708 *
2709 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2710 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002711 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002712 */
2713 if (reason == TASK_SWITCH_GATE) {
2714 if (idt_index != -1) {
2715 /* Software interrupts */
2716 struct desc_struct task_gate_desc;
2717 int dpl;
2718
2719 ret = read_interrupt_descriptor(ctxt, idt_index,
2720 &task_gate_desc);
2721 if (ret != X86EMUL_CONTINUE)
2722 return ret;
2723
2724 dpl = task_gate_desc.dpl;
2725 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2726 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2727 }
2728 } else if (reason != TASK_SWITCH_IRET) {
2729 int dpl = next_tss_desc.dpl;
2730 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2731 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732 }
2733
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002734
Gleb Natapovceffb452010-03-18 15:20:19 +02002735 desc_limit = desc_limit_scaled(&next_tss_desc);
2736 if (!next_tss_desc.p ||
2737 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2738 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002739 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740 return X86EMUL_PROPAGATE_FAULT;
2741 }
2742
2743 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2744 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002745 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 }
2747
2748 if (reason == TASK_SWITCH_IRET)
2749 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2750
2751 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002752 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002753 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2754 old_tss_sel = 0xffff;
2755
2756 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002757 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758 old_tss_base, &next_tss_desc);
2759 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002760 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002762 if (ret != X86EMUL_CONTINUE)
2763 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764
2765 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2766 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2767
2768 if (reason != TASK_SWITCH_IRET) {
2769 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002770 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 }
2772
Avi Kivity717746e2011-04-20 13:37:53 +03002773 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002774 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002775
Jan Kiszkae269fb22010-04-14 15:51:09 +02002776 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002777 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2778 ctxt->lock_prefix = 0;
2779 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002780 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002781 }
2782
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 return ret;
2784}
2785
2786int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002787 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002788 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790 int rc;
2791
Avi Kivitydd856ef2012-08-27 23:46:17 +03002792 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002793 ctxt->_eip = ctxt->eip;
2794 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002796 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002797 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798
Avi Kivitydd856ef2012-08-27 23:46:17 +03002799 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002800 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002801 writeback_registers(ctxt);
2802 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002804 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805}
2806
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002807static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2808 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002809{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002810 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002811
Avi Kivitydd856ef2012-08-27 23:46:17 +03002812 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2813 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002814}
2815
Avi Kivity7af04fc2010-08-18 14:16:35 +03002816static int em_das(struct x86_emulate_ctxt *ctxt)
2817{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002818 u8 al, old_al;
2819 bool af, cf, old_cf;
2820
2821 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002822 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002823
2824 old_al = al;
2825 old_cf = cf;
2826 cf = false;
2827 af = ctxt->eflags & X86_EFLAGS_AF;
2828 if ((al & 0x0f) > 9 || af) {
2829 al -= 6;
2830 cf = old_cf | (al >= 250);
2831 af = true;
2832 } else {
2833 af = false;
2834 }
2835 if (old_al > 0x99 || old_cf) {
2836 al -= 0x60;
2837 cf = true;
2838 }
2839
Avi Kivity9dac77f2011-06-01 15:34:25 +03002840 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002841 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002842 ctxt->src.type = OP_IMM;
2843 ctxt->src.val = 0;
2844 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002845 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002846 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2847 if (cf)
2848 ctxt->eflags |= X86_EFLAGS_CF;
2849 if (af)
2850 ctxt->eflags |= X86_EFLAGS_AF;
2851 return X86EMUL_CONTINUE;
2852}
2853
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002854static int em_call(struct x86_emulate_ctxt *ctxt)
2855{
2856 long rel = ctxt->src.val;
2857
2858 ctxt->src.val = (unsigned long)ctxt->_eip;
2859 jmp_rel(ctxt, rel);
2860 return em_push(ctxt);
2861}
2862
Avi Kivity0ef753b2010-08-18 14:51:45 +03002863static int em_call_far(struct x86_emulate_ctxt *ctxt)
2864{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002865 u16 sel, old_cs;
2866 ulong old_eip;
2867 int rc;
2868
Avi Kivity1aa36612011-04-27 13:20:30 +03002869 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002870 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002871
Avi Kivity9dac77f2011-06-01 15:34:25 +03002872 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002873 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002874 return X86EMUL_CONTINUE;
2875
Avi Kivity9dac77f2011-06-01 15:34:25 +03002876 ctxt->_eip = 0;
2877 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002878
Avi Kivity9dac77f2011-06-01 15:34:25 +03002879 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002880 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002881 if (rc != X86EMUL_CONTINUE)
2882 return rc;
2883
Avi Kivity9dac77f2011-06-01 15:34:25 +03002884 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002885 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002886}
2887
Avi Kivity40ece7c2010-08-18 15:12:09 +03002888static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2889{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002890 int rc;
2891
Avi Kivity9dac77f2011-06-01 15:34:25 +03002892 ctxt->dst.type = OP_REG;
2893 ctxt->dst.addr.reg = &ctxt->_eip;
2894 ctxt->dst.bytes = ctxt->op_bytes;
2895 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002896 if (rc != X86EMUL_CONTINUE)
2897 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002898 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002899 return X86EMUL_CONTINUE;
2900}
2901
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002902static int em_add(struct x86_emulate_ctxt *ctxt)
2903{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002904 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002905 return X86EMUL_CONTINUE;
2906}
2907
2908static int em_or(struct x86_emulate_ctxt *ctxt)
2909{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002910 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002911 return X86EMUL_CONTINUE;
2912}
2913
2914static int em_adc(struct x86_emulate_ctxt *ctxt)
2915{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002916 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002917 return X86EMUL_CONTINUE;
2918}
2919
2920static int em_sbb(struct x86_emulate_ctxt *ctxt)
2921{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002922 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002923 return X86EMUL_CONTINUE;
2924}
2925
2926static int em_and(struct x86_emulate_ctxt *ctxt)
2927{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002928 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002929 return X86EMUL_CONTINUE;
2930}
2931
2932static int em_sub(struct x86_emulate_ctxt *ctxt)
2933{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002934 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002935 return X86EMUL_CONTINUE;
2936}
2937
2938static int em_xor(struct x86_emulate_ctxt *ctxt)
2939{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002940 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002941 return X86EMUL_CONTINUE;
2942}
2943
2944static int em_cmp(struct x86_emulate_ctxt *ctxt)
2945{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002946 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002947 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002948 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002949 return X86EMUL_CONTINUE;
2950}
2951
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002952static int em_test(struct x86_emulate_ctxt *ctxt)
2953{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002954 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002955 /* Disable writeback. */
2956 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002957 return X86EMUL_CONTINUE;
2958}
2959
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002960static int em_xchg(struct x86_emulate_ctxt *ctxt)
2961{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002962 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002963 ctxt->src.val = ctxt->dst.val;
2964 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002965
2966 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002967 ctxt->dst.val = ctxt->src.orig_val;
2968 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002969 return X86EMUL_CONTINUE;
2970}
2971
Avi Kivity5c82aa22010-08-18 18:31:43 +03002972static int em_imul(struct x86_emulate_ctxt *ctxt)
2973{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002974 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002975 return X86EMUL_CONTINUE;
2976}
2977
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002978static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2979{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002980 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002981 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002982}
2983
Avi Kivity61429142010-08-19 15:13:00 +03002984static int em_cwd(struct x86_emulate_ctxt *ctxt)
2985{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002986 ctxt->dst.type = OP_REG;
2987 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002988 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002989 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002990
2991 return X86EMUL_CONTINUE;
2992}
2993
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002994static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2995{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002996 u64 tsc = 0;
2997
Avi Kivity717746e2011-04-20 13:37:53 +03002998 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002999 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3000 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03003001 return X86EMUL_CONTINUE;
3002}
3003
Avi Kivity222d21a2011-11-10 14:57:30 +02003004static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3005{
3006 u64 pmc;
3007
Avi Kivitydd856ef2012-08-27 23:46:17 +03003008 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003009 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003010 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3011 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003012 return X86EMUL_CONTINUE;
3013}
3014
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003015static int em_mov(struct x86_emulate_ctxt *ctxt)
3016{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003017 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003018 return X86EMUL_CONTINUE;
3019}
3020
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003021static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3022{
3023 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3024 return emulate_gp(ctxt, 0);
3025
3026 /* Disable writeback. */
3027 ctxt->dst.type = OP_NONE;
3028 return X86EMUL_CONTINUE;
3029}
3030
3031static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3032{
3033 unsigned long val;
3034
3035 if (ctxt->mode == X86EMUL_MODE_PROT64)
3036 val = ctxt->src.val & ~0ULL;
3037 else
3038 val = ctxt->src.val & ~0U;
3039
3040 /* #UD condition is already handled. */
3041 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3042 return emulate_gp(ctxt, 0);
3043
3044 /* Disable writeback. */
3045 ctxt->dst.type = OP_NONE;
3046 return X86EMUL_CONTINUE;
3047}
3048
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003049static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3050{
3051 u64 msr_data;
3052
Avi Kivitydd856ef2012-08-27 23:46:17 +03003053 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3054 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3055 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003056 return emulate_gp(ctxt, 0);
3057
3058 return X86EMUL_CONTINUE;
3059}
3060
3061static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3062{
3063 u64 msr_data;
3064
Avi Kivitydd856ef2012-08-27 23:46:17 +03003065 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003066 return emulate_gp(ctxt, 0);
3067
Avi Kivitydd856ef2012-08-27 23:46:17 +03003068 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3069 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003070 return X86EMUL_CONTINUE;
3071}
3072
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003073static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3074{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003075 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003076 return emulate_ud(ctxt);
3077
Avi Kivity9dac77f2011-06-01 15:34:25 +03003078 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003079 return X86EMUL_CONTINUE;
3080}
3081
3082static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3083{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003085
Avi Kivity9dac77f2011-06-01 15:34:25 +03003086 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003087 return emulate_ud(ctxt);
3088
Avi Kivity9dac77f2011-06-01 15:34:25 +03003089 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003090 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3091
3092 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003093 ctxt->dst.type = OP_NONE;
3094 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003095}
3096
Avi Kivitya14e5792012-06-13 12:28:33 +03003097static int em_lldt(struct x86_emulate_ctxt *ctxt)
3098{
3099 u16 sel = ctxt->src.val;
3100
3101 /* Disable writeback. */
3102 ctxt->dst.type = OP_NONE;
3103 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3104}
3105
Avi Kivity80890002012-06-13 16:33:29 +03003106static int em_ltr(struct x86_emulate_ctxt *ctxt)
3107{
3108 u16 sel = ctxt->src.val;
3109
3110 /* Disable writeback. */
3111 ctxt->dst.type = OP_NONE;
3112 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3113}
3114
Avi Kivity38503912011-03-31 18:48:09 +02003115static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3116{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003117 int rc;
3118 ulong linear;
3119
Avi Kivity9dac77f2011-06-01 15:34:25 +03003120 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003121 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003122 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003123 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003124 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003125 return X86EMUL_CONTINUE;
3126}
3127
Avi Kivity2d04a052011-04-20 15:32:49 +03003128static int em_clts(struct x86_emulate_ctxt *ctxt)
3129{
3130 ulong cr0;
3131
3132 cr0 = ctxt->ops->get_cr(ctxt, 0);
3133 cr0 &= ~X86_CR0_TS;
3134 ctxt->ops->set_cr(ctxt, 0, cr0);
3135 return X86EMUL_CONTINUE;
3136}
3137
Avi Kivity26d05cc2011-04-21 12:07:59 +03003138static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3139{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003140 int rc;
3141
Avi Kivity9dac77f2011-06-01 15:34:25 +03003142 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003143 return X86EMUL_UNHANDLEABLE;
3144
3145 rc = ctxt->ops->fix_hypercall(ctxt);
3146 if (rc != X86EMUL_CONTINUE)
3147 return rc;
3148
3149 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003150 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003151 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003152 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003153 return X86EMUL_CONTINUE;
3154}
3155
Avi Kivity96051572012-06-10 17:21:18 +03003156static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3157 void (*get)(struct x86_emulate_ctxt *ctxt,
3158 struct desc_ptr *ptr))
3159{
3160 struct desc_ptr desc_ptr;
3161
3162 if (ctxt->mode == X86EMUL_MODE_PROT64)
3163 ctxt->op_bytes = 8;
3164 get(ctxt, &desc_ptr);
3165 if (ctxt->op_bytes == 2) {
3166 ctxt->op_bytes = 4;
3167 desc_ptr.address &= 0x00ffffff;
3168 }
3169 /* Disable writeback. */
3170 ctxt->dst.type = OP_NONE;
3171 return segmented_write(ctxt, ctxt->dst.addr.mem,
3172 &desc_ptr, 2 + ctxt->op_bytes);
3173}
3174
3175static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3176{
3177 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3178}
3179
3180static int em_sidt(struct x86_emulate_ctxt *ctxt)
3181{
3182 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3183}
3184
Avi Kivity26d05cc2011-04-21 12:07:59 +03003185static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3186{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003187 struct desc_ptr desc_ptr;
3188 int rc;
3189
Avi Kivity510425f2012-06-07 17:04:36 +03003190 if (ctxt->mode == X86EMUL_MODE_PROT64)
3191 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003192 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003193 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003194 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003195 if (rc != X86EMUL_CONTINUE)
3196 return rc;
3197 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3198 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003199 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003200 return X86EMUL_CONTINUE;
3201}
3202
Avi Kivity5ef39c72011-04-21 12:21:50 +03003203static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003204{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003205 int rc;
3206
Avi Kivity5ef39c72011-04-21 12:21:50 +03003207 rc = ctxt->ops->fix_hypercall(ctxt);
3208
Avi Kivity26d05cc2011-04-21 12:07:59 +03003209 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003210 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003211 return rc;
3212}
3213
3214static int em_lidt(struct x86_emulate_ctxt *ctxt)
3215{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003216 struct desc_ptr desc_ptr;
3217 int rc;
3218
Avi Kivity510425f2012-06-07 17:04:36 +03003219 if (ctxt->mode == X86EMUL_MODE_PROT64)
3220 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003221 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003222 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003223 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003224 if (rc != X86EMUL_CONTINUE)
3225 return rc;
3226 ctxt->ops->set_idt(ctxt, &desc_ptr);
3227 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003228 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003229 return X86EMUL_CONTINUE;
3230}
3231
3232static int em_smsw(struct x86_emulate_ctxt *ctxt)
3233{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003234 ctxt->dst.bytes = 2;
3235 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003236 return X86EMUL_CONTINUE;
3237}
3238
3239static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3240{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003241 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003242 | (ctxt->src.val & 0x0f));
3243 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003244 return X86EMUL_CONTINUE;
3245}
3246
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003247static int em_loop(struct x86_emulate_ctxt *ctxt)
3248{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003249 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3250 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003251 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3252 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003253
3254 return X86EMUL_CONTINUE;
3255}
3256
3257static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3258{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003259 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003260 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003261
3262 return X86EMUL_CONTINUE;
3263}
3264
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003265static int em_in(struct x86_emulate_ctxt *ctxt)
3266{
3267 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3268 &ctxt->dst.val))
3269 return X86EMUL_IO_NEEDED;
3270
3271 return X86EMUL_CONTINUE;
3272}
3273
3274static int em_out(struct x86_emulate_ctxt *ctxt)
3275{
3276 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3277 &ctxt->src.val, 1);
3278 /* Disable writeback. */
3279 ctxt->dst.type = OP_NONE;
3280 return X86EMUL_CONTINUE;
3281}
3282
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003283static int em_cli(struct x86_emulate_ctxt *ctxt)
3284{
3285 if (emulator_bad_iopl(ctxt))
3286 return emulate_gp(ctxt, 0);
3287
3288 ctxt->eflags &= ~X86_EFLAGS_IF;
3289 return X86EMUL_CONTINUE;
3290}
3291
3292static int em_sti(struct x86_emulate_ctxt *ctxt)
3293{
3294 if (emulator_bad_iopl(ctxt))
3295 return emulate_gp(ctxt, 0);
3296
3297 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3298 ctxt->eflags |= X86_EFLAGS_IF;
3299 return X86EMUL_CONTINUE;
3300}
3301
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003302static int em_bt(struct x86_emulate_ctxt *ctxt)
3303{
3304 /* Disable writeback. */
3305 ctxt->dst.type = OP_NONE;
3306 /* only subword offset */
3307 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3308
3309 emulate_2op_SrcV_nobyte(ctxt, "bt");
3310 return X86EMUL_CONTINUE;
3311}
3312
3313static int em_bts(struct x86_emulate_ctxt *ctxt)
3314{
3315 emulate_2op_SrcV_nobyte(ctxt, "bts");
3316 return X86EMUL_CONTINUE;
3317}
3318
3319static int em_btr(struct x86_emulate_ctxt *ctxt)
3320{
3321 emulate_2op_SrcV_nobyte(ctxt, "btr");
3322 return X86EMUL_CONTINUE;
3323}
3324
3325static int em_btc(struct x86_emulate_ctxt *ctxt)
3326{
3327 emulate_2op_SrcV_nobyte(ctxt, "btc");
3328 return X86EMUL_CONTINUE;
3329}
3330
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003331static int em_bsf(struct x86_emulate_ctxt *ctxt)
3332{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003333 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003334 return X86EMUL_CONTINUE;
3335}
3336
3337static int em_bsr(struct x86_emulate_ctxt *ctxt)
3338{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003339 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003340 return X86EMUL_CONTINUE;
3341}
3342
Avi Kivity6d6eede2012-06-07 14:11:36 +03003343static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3344{
3345 u32 eax, ebx, ecx, edx;
3346
Avi Kivitydd856ef2012-08-27 23:46:17 +03003347 eax = reg_read(ctxt, VCPU_REGS_RAX);
3348 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003349 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003350 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3351 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3352 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3353 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003354 return X86EMUL_CONTINUE;
3355}
3356
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003357static int em_lahf(struct x86_emulate_ctxt *ctxt)
3358{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003359 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3360 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003361 return X86EMUL_CONTINUE;
3362}
3363
Avi Kivity92998362012-06-13 12:25:06 +03003364static int em_bswap(struct x86_emulate_ctxt *ctxt)
3365{
3366 switch (ctxt->op_bytes) {
3367#ifdef CONFIG_X86_64
3368 case 8:
3369 asm("bswap %0" : "+r"(ctxt->dst.val));
3370 break;
3371#endif
3372 default:
3373 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3374 break;
3375 }
3376 return X86EMUL_CONTINUE;
3377}
3378
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003379static bool valid_cr(int nr)
3380{
3381 switch (nr) {
3382 case 0:
3383 case 2 ... 4:
3384 case 8:
3385 return true;
3386 default:
3387 return false;
3388 }
3389}
3390
3391static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3392{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003393 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003394 return emulate_ud(ctxt);
3395
3396 return X86EMUL_CONTINUE;
3397}
3398
3399static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3400{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003401 u64 new_val = ctxt->src.val64;
3402 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003403 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003404
3405 static u64 cr_reserved_bits[] = {
3406 0xffffffff00000000ULL,
3407 0, 0, 0, /* CR3 checked later */
3408 CR4_RESERVED_BITS,
3409 0, 0, 0,
3410 CR8_RESERVED_BITS,
3411 };
3412
3413 if (!valid_cr(cr))
3414 return emulate_ud(ctxt);
3415
3416 if (new_val & cr_reserved_bits[cr])
3417 return emulate_gp(ctxt, 0);
3418
3419 switch (cr) {
3420 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003421 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003422 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3423 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3424 return emulate_gp(ctxt, 0);
3425
Avi Kivity717746e2011-04-20 13:37:53 +03003426 cr4 = ctxt->ops->get_cr(ctxt, 4);
3427 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003428
3429 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3430 !(cr4 & X86_CR4_PAE))
3431 return emulate_gp(ctxt, 0);
3432
3433 break;
3434 }
3435 case 3: {
3436 u64 rsvd = 0;
3437
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003438 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3439 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003440 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003441 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003442 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003443 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003444 rsvd = CR3_NONPAE_RESERVED_BITS;
3445
3446 if (new_val & rsvd)
3447 return emulate_gp(ctxt, 0);
3448
3449 break;
3450 }
3451 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003452 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003453
3454 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3455 return emulate_gp(ctxt, 0);
3456
3457 break;
3458 }
3459 }
3460
3461 return X86EMUL_CONTINUE;
3462}
3463
Joerg Roedel3b88e412011-04-04 12:39:29 +02003464static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3465{
3466 unsigned long dr7;
3467
Avi Kivity717746e2011-04-20 13:37:53 +03003468 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003469
3470 /* Check if DR7.Global_Enable is set */
3471 return dr7 & (1 << 13);
3472}
3473
3474static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3475{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003476 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003477 u64 cr4;
3478
3479 if (dr > 7)
3480 return emulate_ud(ctxt);
3481
Avi Kivity717746e2011-04-20 13:37:53 +03003482 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003483 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3484 return emulate_ud(ctxt);
3485
3486 if (check_dr7_gd(ctxt))
3487 return emulate_db(ctxt);
3488
3489 return X86EMUL_CONTINUE;
3490}
3491
3492static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3493{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003494 u64 new_val = ctxt->src.val64;
3495 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003496
3497 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3498 return emulate_gp(ctxt, 0);
3499
3500 return check_dr_read(ctxt);
3501}
3502
Joerg Roedel01de8b02011-04-04 12:39:31 +02003503static int check_svme(struct x86_emulate_ctxt *ctxt)
3504{
3505 u64 efer;
3506
Avi Kivity717746e2011-04-20 13:37:53 +03003507 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003508
3509 if (!(efer & EFER_SVME))
3510 return emulate_ud(ctxt);
3511
3512 return X86EMUL_CONTINUE;
3513}
3514
3515static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3516{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003517 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003518
3519 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003520 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003521 return emulate_gp(ctxt, 0);
3522
3523 return check_svme(ctxt);
3524}
3525
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003526static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3527{
Avi Kivity717746e2011-04-20 13:37:53 +03003528 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003529
Avi Kivity717746e2011-04-20 13:37:53 +03003530 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003531 return emulate_ud(ctxt);
3532
3533 return X86EMUL_CONTINUE;
3534}
3535
Joerg Roedel80612522011-04-04 12:39:33 +02003536static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3537{
Avi Kivity717746e2011-04-20 13:37:53 +03003538 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003539 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003540
Avi Kivity717746e2011-04-20 13:37:53 +03003541 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003542 (rcx > 3))
3543 return emulate_gp(ctxt, 0);
3544
3545 return X86EMUL_CONTINUE;
3546}
3547
Joerg Roedelf6511932011-04-04 12:39:35 +02003548static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3549{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003550 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3551 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003552 return emulate_gp(ctxt, 0);
3553
3554 return X86EMUL_CONTINUE;
3555}
3556
3557static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3558{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003559 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3560 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003561 return emulate_gp(ctxt, 0);
3562
3563 return X86EMUL_CONTINUE;
3564}
3565
Avi Kivity73fba5f2010-07-29 15:11:53 +03003566#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003567#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003568#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3569 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003570#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003571#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003572#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3573#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003574#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003575#define II(_f, _e, _i) \
3576 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003577#define IIP(_f, _e, _i, _p) \
3578 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3579 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003580#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003581
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003582#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003583#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003584#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003585#define I2bvIP(_f, _e, _i, _p) \
3586 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003587
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003588#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3589 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3590 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003591
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003592static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003593 DI(SrcNone | Priv, monitor),
3594 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003595 N, N, N, N, N, N,
3596};
3597
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003598static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003599 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3600 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3601 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3602 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3603 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3604 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3605 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3606 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003607};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003608
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003609static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003610 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003611 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003612 N, N, N, N, N, N,
3613};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003614
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003615static const struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003616 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003617 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003618 I(Lock, em_adc),
3619 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003620 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003621 I(Lock, em_sub),
3622 I(Lock, em_xor),
3623 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003624};
3625
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003626static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003627 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003628};
3629
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003630static const struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003631 I(DstMem | SrcImm, em_test),
3632 I(DstMem | SrcImm, em_test),
3633 I(DstMem | SrcNone | Lock, em_not),
3634 I(DstMem | SrcNone | Lock, em_neg),
3635 I(SrcMem, em_mul_ex),
3636 I(SrcMem, em_imul_ex),
3637 I(SrcMem, em_div_ex),
3638 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003639};
3640
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003641static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003642 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3643 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003644 N, N, N, N, N, N,
3645};
3646
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003647static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003648 I(DstMem | SrcNone | Lock, em_grp45),
3649 I(DstMem | SrcNone | Lock, em_grp45),
3650 I(SrcMem | Stack, em_grp45),
3651 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3652 I(SrcMem | Stack, em_grp45),
3653 I(SrcMemFAddr | ImplicitOps, em_grp45),
3654 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003655};
3656
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003657static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003658 DI(Prot, sldt),
3659 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003660 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003661 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003662 N, N, N, N,
3663};
3664
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003665static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003666 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3667 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003668 II(SrcMem | Priv, em_lgdt, lgdt),
3669 II(SrcMem | Priv, em_lidt, lidt),
3670 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3671 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3672 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003673}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003674 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003675 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003676 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003677 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3678 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3679 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003680} };
3681
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003682static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003683 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003684 I(DstMem | SrcImmByte, em_bt),
3685 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3686 I(DstMem | SrcImmByte | Lock, em_btr),
3687 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003688};
3689
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003690static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003691 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003692}, {
3693 N, N, N, N, N, N, N, N,
3694} };
3695
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003696static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003697 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003698 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003699};
3700
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003701static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003702 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003703};
3704
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003705static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003706 I(0, em_mov), N, N, N,
3707};
3708
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003709static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003710 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003711 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003712 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3713 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003714 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003715 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003716 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3717 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003718 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003719 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003720 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3721 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003722 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003723 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003724 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3725 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003726 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003727 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003728 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003729 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003730 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003731 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003732 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003733 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003734 /* 0x40 - 0x4F */
3735 X16(D(DstReg)),
3736 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003737 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003738 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003739 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003740 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003741 I(ImplicitOps | Stack | No64, em_pusha),
3742 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003743 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3744 N, N, N, N,
3745 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003746 I(SrcImm | Mov | Stack, em_push),
3747 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003748 I(SrcImmByte | Mov | Stack, em_push),
3749 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003750 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003751 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003752 /* 0x70 - 0x7F */
3753 X16(D(SrcImmByte)),
3754 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003755 G(ByteOp | DstMem | SrcImm, group1),
3756 G(DstMem | SrcImm, group1),
3757 G(ByteOp | DstMem | SrcImm | No64, group1),
3758 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003759 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003760 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003761 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003762 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003763 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003764 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003765 D(ModRM | SrcMem | NoAccess | DstReg),
3766 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3767 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003768 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003769 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003770 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003771 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003772 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003773 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003774 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003775 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003776 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003777 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003778 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003779 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003781 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003782 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3783 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003784 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003786 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003787 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003788 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003790 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003791 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003792 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003793 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3794 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003795 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003796 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003797 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3798 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003799 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003800 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003801 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003802 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003803 N, N, N, N,
3804 /* 0xD8 - 0xDF */
3805 N, N, N, N, N, N, N, N,
3806 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003807 X3(I(SrcImmByte, em_loop)),
3808 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003809 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3810 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003811 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003812 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003813 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003814 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3815 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003816 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003817 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003818 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3819 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003820 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003821 D(ImplicitOps), D(ImplicitOps),
3822 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003823 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3824};
3825
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003826static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003827 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003828 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003829 N, I(ImplicitOps | VendorSpecific, em_syscall),
3830 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003831 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003832 N, D(ImplicitOps | ModRM), N, N,
3833 /* 0x10 - 0x1F */
3834 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3835 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003836 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003837 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003838 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3839 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003840 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003841 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3842 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003843 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003844 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003845 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003846 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003847 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003848 I(ImplicitOps | VendorSpecific, em_sysenter),
3849 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003850 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003851 N, N, N, N, N, N, N, N,
3852 /* 0x40 - 0x4F */
3853 X16(D(DstReg | SrcMem | ModRM | Mov)),
3854 /* 0x50 - 0x5F */
3855 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3856 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003857 N, N, N, N,
3858 N, N, N, N,
3859 N, N, N, N,
3860 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003861 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003862 N, N, N, N,
3863 N, N, N, N,
3864 N, N, N, N,
3865 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003866 /* 0x80 - 0x8F */
3867 X16(D(SrcImm)),
3868 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003869 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003871 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03003872 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003873 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3874 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3875 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003876 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003877 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003878 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3880 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003881 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003882 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003883 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003884 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003885 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003886 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3887 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003888 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0xB8 - 0xBF */
3890 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003891 G(BitOp, group8),
3892 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003893 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003894 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03003895 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003896 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003897 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03003899 /* 0xC8 - 0xCF */
3900 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 /* 0xD0 - 0xDF */
3902 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3903 /* 0xE0 - 0xEF */
3904 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3905 /* 0xF0 - 0xFF */
3906 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3907};
3908
3909#undef D
3910#undef N
3911#undef G
3912#undef GD
3913#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003914#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003915#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003916
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003917#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003918#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003919#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003920#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003921#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003922
Avi Kivity9dac77f2011-06-01 15:34:25 +03003923static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003924{
3925 unsigned size;
3926
Avi Kivity9dac77f2011-06-01 15:34:25 +03003927 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003928 if (size == 8)
3929 size = 4;
3930 return size;
3931}
3932
3933static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3934 unsigned size, bool sign_extension)
3935{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003936 int rc = X86EMUL_CONTINUE;
3937
3938 op->type = OP_IMM;
3939 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003940 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003941 /* NB. Immediates are sign-extended as necessary. */
3942 switch (op->bytes) {
3943 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003944 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003945 break;
3946 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003947 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003948 break;
3949 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003950 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003951 break;
3952 }
3953 if (!sign_extension) {
3954 switch (op->bytes) {
3955 case 1:
3956 op->val &= 0xff;
3957 break;
3958 case 2:
3959 op->val &= 0xffff;
3960 break;
3961 case 4:
3962 op->val &= 0xffffffff;
3963 break;
3964 }
3965 }
3966done:
3967 return rc;
3968}
3969
Avi Kivitya99455492011-09-13 10:45:41 +03003970static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3971 unsigned d)
3972{
3973 int rc = X86EMUL_CONTINUE;
3974
3975 switch (d) {
3976 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02003977 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03003978 break;
3979 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003980 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03003981 break;
3982 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003983 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003984 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03003985 *op = ctxt->memop;
3986 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003987 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03003988 fetch_bit_operand(ctxt);
3989 op->orig_val = op->val;
3990 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003991 case OpMem64:
3992 ctxt->memop.bytes = 8;
3993 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03003994 case OpAcc:
3995 op->type = OP_REG;
3996 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003997 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03003998 fetch_register_operand(op);
3999 op->orig_val = op->val;
4000 break;
4001 case OpDI:
4002 op->type = OP_MEM;
4003 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4004 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004005 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004006 op->addr.mem.seg = VCPU_SREG_ES;
4007 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004008 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004009 break;
4010 case OpDX:
4011 op->type = OP_REG;
4012 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004013 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004014 fetch_register_operand(op);
4015 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004016 case OpCL:
4017 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004018 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004019 break;
4020 case OpImmByte:
4021 rc = decode_imm(ctxt, op, 1, true);
4022 break;
4023 case OpOne:
4024 op->bytes = 1;
4025 op->val = 1;
4026 break;
4027 case OpImm:
4028 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4029 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004030 case OpMem8:
4031 ctxt->memop.bytes = 1;
4032 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004033 case OpMem16:
4034 ctxt->memop.bytes = 2;
4035 goto mem_common;
4036 case OpMem32:
4037 ctxt->memop.bytes = 4;
4038 goto mem_common;
4039 case OpImmU16:
4040 rc = decode_imm(ctxt, op, 2, false);
4041 break;
4042 case OpImmU:
4043 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4044 break;
4045 case OpSI:
4046 op->type = OP_MEM;
4047 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4048 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004049 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004050 op->addr.mem.seg = seg_override(ctxt);
4051 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004052 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004053 break;
4054 case OpImmFAddr:
4055 op->type = OP_IMM;
4056 op->addr.mem.ea = ctxt->_eip;
4057 op->bytes = ctxt->op_bytes + 2;
4058 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4059 break;
4060 case OpMemFAddr:
4061 ctxt->memop.bytes = ctxt->op_bytes + 2;
4062 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004063 case OpES:
4064 op->val = VCPU_SREG_ES;
4065 break;
4066 case OpCS:
4067 op->val = VCPU_SREG_CS;
4068 break;
4069 case OpSS:
4070 op->val = VCPU_SREG_SS;
4071 break;
4072 case OpDS:
4073 op->val = VCPU_SREG_DS;
4074 break;
4075 case OpFS:
4076 op->val = VCPU_SREG_FS;
4077 break;
4078 case OpGS:
4079 op->val = VCPU_SREG_GS;
4080 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004081 case OpImplicit:
4082 /* Special instructions do their own operand decoding. */
4083 default:
4084 op->type = OP_NONE; /* Disable writeback. */
4085 break;
4086 }
4087
4088done:
4089 return rc;
4090}
4091
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004092int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004093{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004094 int rc = X86EMUL_CONTINUE;
4095 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004096 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004097 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004098 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004099
Avi Kivityf09ed832011-09-13 10:45:40 +03004100 ctxt->memop.type = OP_NONE;
4101 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 ctxt->_eip = ctxt->eip;
4103 ctxt->fetch.start = ctxt->_eip;
4104 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004105 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004106 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004107
4108 switch (mode) {
4109 case X86EMUL_MODE_REAL:
4110 case X86EMUL_MODE_VM86:
4111 case X86EMUL_MODE_PROT16:
4112 def_op_bytes = def_ad_bytes = 2;
4113 break;
4114 case X86EMUL_MODE_PROT32:
4115 def_op_bytes = def_ad_bytes = 4;
4116 break;
4117#ifdef CONFIG_X86_64
4118 case X86EMUL_MODE_PROT64:
4119 def_op_bytes = 4;
4120 def_ad_bytes = 8;
4121 break;
4122#endif
4123 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004124 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004125 }
4126
Avi Kivity9dac77f2011-06-01 15:34:25 +03004127 ctxt->op_bytes = def_op_bytes;
4128 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004129
4130 /* Legacy prefixes. */
4131 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004132 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004133 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004134 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004135 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004136 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004137 break;
4138 case 0x67: /* address-size override */
4139 if (mode == X86EMUL_MODE_PROT64)
4140 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004141 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004142 else
4143 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004144 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004145 break;
4146 case 0x26: /* ES override */
4147 case 0x2e: /* CS override */
4148 case 0x36: /* SS override */
4149 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004150 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004151 break;
4152 case 0x64: /* FS override */
4153 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004154 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004155 break;
4156 case 0x40 ... 0x4f: /* REX */
4157 if (mode != X86EMUL_MODE_PROT64)
4158 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004159 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004160 continue;
4161 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004162 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004163 break;
4164 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004165 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004166 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004167 break;
4168 default:
4169 goto done_prefixes;
4170 }
4171
4172 /* Any legacy prefix after a REX prefix nullifies its effect. */
4173
Avi Kivity9dac77f2011-06-01 15:34:25 +03004174 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004175 }
4176
4177done_prefixes:
4178
4179 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004180 if (ctxt->rex_prefix & 8)
4181 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004182
4183 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004184 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004185 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004186 if (ctxt->b == 0x0f) {
4187 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004188 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004189 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004190 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004191 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004192
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004193 if (ctxt->d & ModRM)
4194 ctxt->modrm = insn_fetch(u8, ctxt);
4195
Avi Kivity9dac77f2011-06-01 15:34:25 +03004196 while (ctxt->d & GroupMask) {
4197 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004198 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004199 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004200 opcode = opcode.u.group[goffset];
4201 break;
4202 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004203 goffset = (ctxt->modrm >> 3) & 7;
4204 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004205 opcode = opcode.u.gdual->mod3[goffset];
4206 else
4207 opcode = opcode.u.gdual->mod012[goffset];
4208 break;
4209 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004210 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004211 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004212 break;
4213 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004214 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004215 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004216 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004217 switch (simd_prefix) {
4218 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4219 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4220 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4221 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4222 }
4223 break;
4224 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004225 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004226 }
Avi Kivity46561642011-04-24 14:09:59 +03004227
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004228 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004230 }
4231
Avi Kivity9dac77f2011-06-01 15:34:25 +03004232 ctxt->execute = opcode.u.execute;
4233 ctxt->check_perm = opcode.check_perm;
4234 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004235
4236 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004237 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004238 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004239
Avi Kivity9dac77f2011-06-01 15:34:25 +03004240 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004241 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004242
Avi Kivity9dac77f2011-06-01 15:34:25 +03004243 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4244 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004245
Avi Kivity9dac77f2011-06-01 15:34:25 +03004246 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004247 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004248 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004249 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004250 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004251 }
4252
Avi Kivity9dac77f2011-06-01 15:34:25 +03004253 if (ctxt->d & Sse)
4254 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004255 else if (ctxt->d & Mmx)
4256 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004257
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004258 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004259 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004260 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004261 if (!ctxt->has_seg_override)
4262 set_seg_override(ctxt, ctxt->modrm_seg);
4263 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004264 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004265 if (rc != X86EMUL_CONTINUE)
4266 goto done;
4267
Avi Kivity9dac77f2011-06-01 15:34:25 +03004268 if (!ctxt->has_seg_override)
4269 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004270
Avi Kivityf09ed832011-09-13 10:45:40 +03004271 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004272
Avi Kivityf09ed832011-09-13 10:45:40 +03004273 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4274 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004276 /*
4277 * Decode and fetch the source operand: register, memory
4278 * or immediate.
4279 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004280 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004281 if (rc != X86EMUL_CONTINUE)
4282 goto done;
4283
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004284 /*
4285 * Decode and fetch the second source operand: register, memory
4286 * or immediate.
4287 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004288 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004289 if (rc != X86EMUL_CONTINUE)
4290 goto done;
4291
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004292 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004293 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004294
4295done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004296 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4297 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004298
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004299 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004300}
4301
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004302bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4303{
4304 return ctxt->d & PageTable;
4305}
4306
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004307static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4308{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004309 /* The second termination condition only applies for REPE
4310 * and REPNE. Test if the repeat string operation prefix is
4311 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4312 * corresponding termination condition according to:
4313 * - if REPE/REPZ and ZF = 0 then done
4314 * - if REPNE/REPNZ and ZF = 1 then done
4315 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004316 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4317 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4318 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004319 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004320 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004321 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4322 return true;
4323
4324 return false;
4325}
4326
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004327static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4328{
4329 bool fault = false;
4330
4331 ctxt->ops->get_fpu(ctxt);
4332 asm volatile("1: fwait \n\t"
4333 "2: \n\t"
4334 ".pushsection .fixup,\"ax\" \n\t"
4335 "3: \n\t"
4336 "movb $1, %[fault] \n\t"
4337 "jmp 2b \n\t"
4338 ".popsection \n\t"
4339 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004340 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004341 ctxt->ops->put_fpu(ctxt);
4342
4343 if (unlikely(fault))
4344 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4345
4346 return X86EMUL_CONTINUE;
4347}
4348
4349static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4350 struct operand *op)
4351{
4352 if (op->type == OP_MM)
4353 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4354}
4355
Avi Kivitydd856ef2012-08-27 23:46:17 +03004356
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004357int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004358{
Mathias Krause0225fb52012-08-30 01:30:16 +02004359 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004360 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004361 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004362
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004364
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004366 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004367 goto done;
4368 }
4369
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004370 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004372 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004373 goto done;
4374 }
4375
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004377 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004378 goto done;
4379 }
4380
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004381 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4382 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004383 rc = emulate_ud(ctxt);
4384 goto done;
4385 }
4386
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004387 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004388 rc = emulate_nm(ctxt);
4389 goto done;
4390 }
4391
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004392 if (ctxt->d & Mmx) {
4393 rc = flush_pending_x87_faults(ctxt);
4394 if (rc != X86EMUL_CONTINUE)
4395 goto done;
4396 /*
4397 * Now that we know the fpu is exception safe, we can fetch
4398 * operands from it.
4399 */
4400 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4401 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4402 if (!(ctxt->d & Mov))
4403 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4404 }
4405
Avi Kivity9dac77f2011-06-01 15:34:25 +03004406 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4407 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004408 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004409 if (rc != X86EMUL_CONTINUE)
4410 goto done;
4411 }
4412
Gleb Natapove92805a2010-02-10 14:21:35 +02004413 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004414 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004415 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004416 goto done;
4417 }
4418
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004419 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004420 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004421 rc = emulate_ud(ctxt);
4422 goto done;
4423 }
4424
Joerg Roedeld09beab2011-04-04 12:39:25 +02004425 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004426 if (ctxt->check_perm) {
4427 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004428 if (rc != X86EMUL_CONTINUE)
4429 goto done;
4430 }
4431
Avi Kivity9dac77f2011-06-01 15:34:25 +03004432 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4433 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004434 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004435 if (rc != X86EMUL_CONTINUE)
4436 goto done;
4437 }
4438
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004440 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004441 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004443 goto done;
4444 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004445 }
4446
Avi Kivity9dac77f2011-06-01 15:34:25 +03004447 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4448 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4449 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004450 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004451 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004452 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004453 }
4454
Avi Kivity9dac77f2011-06-01 15:34:25 +03004455 if (ctxt->src2.type == OP_MEM) {
4456 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4457 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004458 if (rc != X86EMUL_CONTINUE)
4459 goto done;
4460 }
4461
Avi Kivity9dac77f2011-06-01 15:34:25 +03004462 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004463 goto special_insn;
4464
4465
Avi Kivity9dac77f2011-06-01 15:34:25 +03004466 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004467 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004468 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4469 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004470 if (rc != X86EMUL_CONTINUE)
4471 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004472 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004473 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004474
Avi Kivity018a98d2007-11-27 19:30:56 +02004475special_insn:
4476
Avi Kivity9dac77f2011-06-01 15:34:25 +03004477 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4478 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004479 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004480 if (rc != X86EMUL_CONTINUE)
4481 goto done;
4482 }
4483
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 if (ctxt->execute) {
4485 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004486 if (rc != X86EMUL_CONTINUE)
4487 goto done;
4488 goto writeback;
4489 }
4490
Avi Kivity9dac77f2011-06-01 15:34:25 +03004491 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004492 goto twobyte_insn;
4493
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004495 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004496 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004497 break;
4498 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004499 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004500 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004501 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004502 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004503 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004504 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004505 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004506 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004507 if (test_cc(ctxt->b, ctxt->eflags))
4508 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004509 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004510 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004511 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004512 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004513 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004514 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004515 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004516 rc = em_xchg(ctxt);
4517 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004518 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004519 switch (ctxt->op_bytes) {
4520 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4521 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4522 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004523 }
4524 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004525 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004526 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004527 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004528 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004529 rc = emulate_int(ctxt, 3);
4530 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004531 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004532 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004533 break;
4534 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004535 if (ctxt->eflags & EFLG_OF)
4536 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004537 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004538 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004539 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004540 break;
4541 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004542 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004543 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004544 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004545 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004546 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004547 jmp_rel(ctxt, ctxt->src.val);
4548 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004549 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004550 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004551 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004552 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004553 case 0xf5: /* cmc */
4554 /* complement carry flag from eflags reg */
4555 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004556 break;
4557 case 0xf8: /* clc */
4558 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004559 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004560 case 0xf9: /* stc */
4561 ctxt->eflags |= EFLG_CF;
4562 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004563 case 0xfc: /* cld */
4564 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004565 break;
4566 case 0xfd: /* std */
4567 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004568 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004569 default:
4570 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004571 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004572
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004573 if (rc != X86EMUL_CONTINUE)
4574 goto done;
4575
Avi Kivity018a98d2007-11-27 19:30:56 +02004576writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004577 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004578 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004579 goto done;
4580
Gleb Natapov5cd21912010-03-18 15:20:26 +02004581 /*
4582 * restore dst type in case the decoding will be reused
4583 * (happens for string instruction )
4584 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004585 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004586
Avi Kivity9dac77f2011-06-01 15:34:25 +03004587 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004588 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004589
Avi Kivity9dac77f2011-06-01 15:34:25 +03004590 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004591 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004592
Avi Kivity9dac77f2011-06-01 15:34:25 +03004593 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004594 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004595 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004596 if ((ctxt->d & SrcMask) == SrcSI)
4597 count = ctxt->src.count;
4598 else
4599 count = ctxt->dst.count;
4600 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4601 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004602
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004603 if (!string_insn_completed(ctxt)) {
4604 /*
4605 * Re-enter guest when pio read ahead buffer is empty
4606 * or, if it is not used, after each 1024 iteration.
4607 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004608 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004609 (r->end == 0 || r->end != r->pos)) {
4610 /*
4611 * Reset read cache. Usually happens before
4612 * decode, but since instruction is restarted
4613 * we have to do it here.
4614 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004615 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004616 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004617 return EMULATION_RESTART;
4618 }
4619 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004620 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004621 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004622
Avi Kivity9dac77f2011-06-01 15:34:25 +03004623 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004624
4625done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004626 if (rc == X86EMUL_PROPAGATE_FAULT)
4627 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004628 if (rc == X86EMUL_INTERCEPTED)
4629 return EMULATION_INTERCEPTED;
4630
Avi Kivitydd856ef2012-08-27 23:46:17 +03004631 if (rc == X86EMUL_CONTINUE)
4632 writeback_registers(ctxt);
4633
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004634 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635
4636twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004637 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004638 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004639 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004640 break;
4641 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004642 case 0x0d: /* GrpP (prefetch) */
4643 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004644 break;
4645 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004646 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004647 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004648 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004649 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004650 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004651 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4653 if (!test_cc(ctxt->b, ctxt->eflags))
4654 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004655 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004656 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004657 if (test_cc(ctxt->b, ctxt->eflags))
4658 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004659 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004660 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004661 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004662 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004663 case 0xa4: /* shld imm8, r, r/m */
4664 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004665 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004666 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004667 case 0xac: /* shrd imm8, r, r/m */
4668 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004669 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004670 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004671 case 0xae: /* clflush */
4672 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004673 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004675 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004679 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004680 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004681 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004683 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004684 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004685 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004686 ctxt->src.val = ctxt->dst.orig_val;
4687 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004688 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004689 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004690 ctxt->dst.bytes = ctxt->op_bytes;
4691 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4692 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004693 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004694 default:
4695 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004697
4698 if (rc != X86EMUL_CONTINUE)
4699 goto done;
4700
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701 goto writeback;
4702
4703cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004704 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004705}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004706
4707void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4708{
4709 invalidate_registers(ctxt);
4710}
4711
4712void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4713{
4714 writeback_registers(ctxt);
4715}