blob: bd3e488c734e1688c6bb8dd6ba386f0a6a822c96 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya9945542011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivitya9945542011-09-13 10:45:41 +030060
Avi Kivity0fe59122011-09-13 10:45:47 +030061#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030062#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030063
64/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080065 * Opcode effective-address decode tables.
66 * Note that we only emulate instructions that have at least one memory
67 * operand (excluding implicit stack references). We assume that stack
68 * references and instruction fetches will never occur in special memory
69 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
70 * not be handled.
71 */
72
73/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030074#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030076#define DstShift 1
77#define ImplicitOps (OpImplicit << DstShift)
78#define DstReg (OpReg << DstShift)
79#define DstMem (OpMem << DstShift)
80#define DstAcc (OpAcc << DstShift)
81#define DstDI (OpDI << DstShift)
82#define DstMem64 (OpMem64 << DstShift)
83#define DstImmUByte (OpImmUByte << DstShift)
84#define DstDX (OpDX << DstShift)
85#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080086/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030087#define SrcShift 6
88#define SrcNone (OpNone << SrcShift)
89#define SrcReg (OpReg << SrcShift)
90#define SrcMem (OpMem << SrcShift)
91#define SrcMem16 (OpMem16 << SrcShift)
92#define SrcMem32 (OpMem32 << SrcShift)
93#define SrcImm (OpImm << SrcShift)
94#define SrcImmByte (OpImmByte << SrcShift)
95#define SrcOne (OpOne << SrcShift)
96#define SrcImmUByte (OpImmUByte << SrcShift)
97#define SrcImmU (OpImmU << SrcShift)
98#define SrcSI (OpSI << SrcShift)
99#define SrcImmFAddr (OpImmFAddr << SrcShift)
100#define SrcMemFAddr (OpMemFAddr << SrcShift)
101#define SrcAcc (OpAcc << SrcShift)
102#define SrcImmU16 (OpImmU16 << SrcShift)
103#define SrcDX (OpDX << SrcShift)
104#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300105#define BitOp (1<<11)
106#define MemAbs (1<<12) /* Memory operand is absolute displacement */
107#define String (1<<13) /* String instruction (rep capable) */
108#define Stack (1<<14) /* Stack instruction (push/pop) */
109#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
110#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
111#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
112#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
113#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
114#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300115/* Generic ModRM decode. */
116#define ModRM (1<<19)
117/* Destination is only written; never read. */
118#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300119/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200120#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200121#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300122#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300123#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300124#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200125#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200126#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300127#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100128/* Source 2 operand type */
Avi Kivity4dd6a572011-09-13 10:45:43 +0300129#define Src2Shift (29)
130#define Src2None (OpNone << Src2Shift)
131#define Src2CL (OpCL << Src2Shift)
132#define Src2ImmByte (OpImmByte << Src2Shift)
133#define Src2One (OpOne << Src2Shift)
134#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300135#define Src2ES (OpES << Src2Shift)
136#define Src2CS (OpCS << Src2Shift)
137#define Src2SS (OpSS << Src2Shift)
138#define Src2DS (OpDS << Src2Shift)
139#define Src2FS (OpFS << Src2Shift)
140#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300141#define Src2Mask (OpMask << Src2Shift)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142
Avi Kivityd0e53322010-07-29 15:11:54 +0300143#define X2(x...) x, x
144#define X3(x...) X2(x), x
145#define X4(x...) X2(x), X2(x)
146#define X5(x...) X4(x), x
147#define X6(x...) X4(x), X2(x)
148#define X7(x...) X4(x), X3(x)
149#define X8(x...) X4(x), X4(x)
150#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300151
Avi Kivityd65b1de2010-07-29 15:11:35 +0300152struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300153 u64 flags : 56;
154 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300155 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300156 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300157 struct opcode *group;
158 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200159 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300160 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200161 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300162};
163
164struct group_dual {
165 struct opcode mod012[8];
166 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300167};
168
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200169struct gprefix {
170 struct opcode pfx_no;
171 struct opcode pfx_66;
172 struct opcode pfx_f2;
173 struct opcode pfx_f3;
174};
175
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200177#define EFLG_ID (1<<21)
178#define EFLG_VIP (1<<20)
179#define EFLG_VIF (1<<19)
180#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200181#define EFLG_VM (1<<17)
182#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200183#define EFLG_IOPL (3<<12)
184#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185#define EFLG_OF (1<<11)
186#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200187#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200188#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189#define EFLG_SF (1<<7)
190#define EFLG_ZF (1<<6)
191#define EFLG_AF (1<<4)
192#define EFLG_PF (1<<2)
193#define EFLG_CF (1<<0)
194
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300195#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
196#define EFLG_RESERVED_ONE_MASK 2
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198/*
199 * Instruction emulation:
200 * Most instructions are emulated directly via a fragment of inline assembly
201 * code. This allows us to save/restore EFLAGS and thus very easily pick up
202 * any modified flags.
203 */
204
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800205#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206#define _LO32 "k" /* force 32-bit operand */
207#define _STK "%%rsp" /* stack pointer */
208#elif defined(__i386__)
209#define _LO32 "" /* force 32-bit operand */
210#define _STK "%%esp" /* stack pointer */
211#endif
212
213/*
214 * These EFLAGS bits are restored from saved value during emulation, and
215 * any changes are written back to the saved value after emulation.
216 */
217#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
218
219/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200220#define _PRE_EFLAGS(_sav, _msk, _tmp) \
221 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
222 "movl %"_sav",%"_LO32 _tmp"; " \
223 "push %"_tmp"; " \
224 "push %"_tmp"; " \
225 "movl %"_msk",%"_LO32 _tmp"; " \
226 "andl %"_LO32 _tmp",("_STK"); " \
227 "pushf; " \
228 "notl %"_LO32 _tmp"; " \
229 "andl %"_LO32 _tmp",("_STK"); " \
230 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
231 "pop %"_tmp"; " \
232 "orl %"_LO32 _tmp",("_STK"); " \
233 "popf; " \
234 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235
236/* After executing instruction: write-back necessary bits in EFLAGS. */
237#define _POST_EFLAGS(_sav, _msk, _tmp) \
238 /* _sav |= EFLAGS & _msk; */ \
239 "pushf; " \
240 "pop %"_tmp"; " \
241 "andl %"_msk",%"_LO32 _tmp"; " \
242 "orl %"_LO32 _tmp",%"_sav"; "
243
Avi Kivitydda96d82008-11-26 15:14:10 +0200244#ifdef CONFIG_X86_64
245#define ON64(x) x
246#else
247#define ON64(x)
248#endif
249
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300250#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200251 do { \
252 __asm__ __volatile__ ( \
253 _PRE_EFLAGS("0", "4", "2") \
254 _op _suffix " %"_x"3,%1; " \
255 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300256 : "=m" ((ctxt)->eflags), \
257 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200258 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300259 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200260 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200261
262
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300264#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200265 do { \
266 unsigned long _tmp; \
267 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300268 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200269 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300270 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200271 break; \
272 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300273 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200274 break; \
275 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300276 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200277 break; \
278 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279 } while (0)
280
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300281#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200283 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300284 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300286 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 break; \
288 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300289 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290 _wx, _wy, _lx, _ly, _qx, _qy); \
291 break; \
292 } \
293 } while (0)
294
295/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300296#define emulate_2op_SrcB(ctxt, _op) \
297 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298
299/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300300#define emulate_2op_SrcV(ctxt, _op) \
301 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302
303/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300304#define emulate_2op_SrcV_nobyte(ctxt, _op) \
305 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100307/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300308#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300309 do { \
310 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300311 _type _clv = (ctxt)->src2.val; \
312 _type _srcv = (ctxt)->src.val; \
313 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300314 \
315 __asm__ __volatile__ ( \
316 _PRE_EFLAGS("0", "5", "2") \
317 _op _suffix " %4,%1 \n" \
318 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300319 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300320 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
321 ); \
322 \
Avi Kivity761441b2011-09-07 16:41:36 +0300323 (ctxt)->src2.val = (unsigned long) _clv; \
324 (ctxt)->src2.val = (unsigned long) _srcv; \
325 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100326 } while (0)
327
Avi Kivity761441b2011-09-07 16:41:36 +0300328#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300329 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300330 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300331 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300332 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300333 break; \
334 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300335 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300336 break; \
337 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300338 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300339 break; \
340 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100341 } while (0)
342
Avi Kivityd1eef452011-09-07 16:41:38 +0300343#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344 do { \
345 unsigned long _tmp; \
346 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200347 __asm__ __volatile__ ( \
348 _PRE_EFLAGS("0", "3", "2") \
349 _op _suffix " %1; " \
350 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300351 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200352 "=&r" (_tmp) \
353 : "i" (EFLAGS_MASK)); \
354 } while (0)
355
356/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300357#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200358 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300359 switch ((ctxt)->dst.bytes) { \
360 case 1: __emulate_1op(ctxt, _op, "b"); break; \
361 case 2: __emulate_1op(ctxt, _op, "w"); break; \
362 case 4: __emulate_1op(ctxt, _op, "l"); break; \
363 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 } \
365 } while (0)
366
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300367#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300368 do { \
369 unsigned long _tmp; \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300370 ulong *rax = &(ctxt)->regs[VCPU_REGS_RAX]; \
371 ulong *rdx = &(ctxt)->regs[VCPU_REGS_RDX]; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300372 \
373 __asm__ __volatile__ ( \
374 _PRE_EFLAGS("0", "5", "1") \
375 "1: \n\t" \
376 _op _suffix " %6; " \
377 "2: \n\t" \
378 _POST_EFLAGS("0", "5", "1") \
379 ".pushsection .fixup,\"ax\" \n\t" \
380 "3: movb $1, %4 \n\t" \
381 "jmp 2b \n\t" \
382 ".popsection \n\t" \
383 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300384 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
385 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
386 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
387 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300388 } while (0)
389
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300390/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300391#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300392 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300393 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300394 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300395 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300396 break; \
397 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300398 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300399 break; \
400 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300401 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300402 break; \
403 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300404 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300405 break; \
406 } \
407 } while (0)
408
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200409static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
410 enum x86_intercept intercept,
411 enum x86_intercept_stage stage)
412{
413 struct x86_instruction_info info = {
414 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300415 .rep_prefix = ctxt->rep_prefix,
416 .modrm_mod = ctxt->modrm_mod,
417 .modrm_reg = ctxt->modrm_reg,
418 .modrm_rm = ctxt->modrm_rm,
419 .src_val = ctxt->src.val64,
420 .src_bytes = ctxt->src.bytes,
421 .dst_bytes = ctxt->dst.bytes,
422 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200423 .next_rip = ctxt->eip,
424 };
425
Avi Kivity29535382011-04-20 13:37:53 +0300426 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200427}
428
Avi Kivity9dac77f2011-06-01 15:34:25 +0300429static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800430{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300431 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800432}
433
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800435static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300436address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800437{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300438 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800439 return reg;
440 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300441 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800442}
443
444static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300445register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800446{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300447 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800448}
449
Harvey Harrison7a9572752008-02-19 07:40:41 -0800450static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300451register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800452{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300453 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800454 *reg += inc;
455 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300456 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800457}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458
Avi Kivity9dac77f2011-06-01 15:34:25 +0300459static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800460{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300461 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800462}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300463
Avi Kivity56697682011-04-03 14:08:51 +0300464static u32 desc_limit_scaled(struct desc_struct *desc)
465{
466 u32 limit = get_desc_limit(desc);
467
468 return desc->g ? (limit << 12) | 0xfff : limit;
469}
470
Avi Kivity9dac77f2011-06-01 15:34:25 +0300471static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300472{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300473 ctxt->has_seg_override = true;
474 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300475}
476
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900477static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300478{
479 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
480 return 0;
481
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900482 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300483}
484
Avi Kivity9dac77f2011-06-01 15:34:25 +0300485static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300486{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300487 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300488 return 0;
489
Avi Kivity9dac77f2011-06-01 15:34:25 +0300490 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300491}
492
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200493static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
494 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300495{
Avi Kivityda9cb572010-11-22 17:53:21 +0200496 ctxt->exception.vector = vec;
497 ctxt->exception.error_code = error;
498 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200499 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300500}
501
Joerg Roedel3b88e412011-04-04 12:39:29 +0200502static int emulate_db(struct x86_emulate_ctxt *ctxt)
503{
504 return emulate_exception(ctxt, DB_VECTOR, 0, false);
505}
506
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200507static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300508{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200509 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300510}
511
Avi Kivity618ff152011-04-03 12:32:09 +0300512static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
513{
514 return emulate_exception(ctxt, SS_VECTOR, err, true);
515}
516
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200517static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300518{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200519 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300520}
521
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200522static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300523{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200524 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300525}
526
Avi Kivity34d1f492010-08-26 11:59:01 +0300527static int emulate_de(struct x86_emulate_ctxt *ctxt)
528{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200529 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300530}
531
Avi Kivity12537912011-03-29 11:41:27 +0200532static int emulate_nm(struct x86_emulate_ctxt *ctxt)
533{
534 return emulate_exception(ctxt, NM_VECTOR, 0, false);
535}
536
Avi Kivity1aa36612011-04-27 13:20:30 +0300537static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
538{
539 u16 selector;
540 struct desc_struct desc;
541
542 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
543 return selector;
544}
545
546static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
547 unsigned seg)
548{
549 u16 dummy;
550 u32 base3;
551 struct desc_struct desc;
552
553 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
554 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
555}
556
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400557static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300558 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400559 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300560 ulong *linear)
561{
Avi Kivity618ff152011-04-03 12:32:09 +0300562 struct desc_struct desc;
563 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300564 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300565 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300566 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300567 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300568
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900569 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300570 switch (ctxt->mode) {
571 case X86EMUL_MODE_REAL:
572 break;
573 case X86EMUL_MODE_PROT64:
574 if (((signed long)la << 16) >> 16 != la)
575 return emulate_gp(ctxt, 0);
576 break;
577 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300578 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
579 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300580 if (!usable)
581 goto bad;
582 /* code segment or read-only data segment */
583 if (((desc.type & 8) || !(desc.type & 2)) && write)
584 goto bad;
585 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400586 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300587 goto bad;
588 lim = desc_limit_scaled(&desc);
589 if ((desc.type & 8) || !(desc.type & 4)) {
590 /* expand-up segment */
591 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
592 goto bad;
593 } else {
594 /* exapand-down segment */
595 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
596 goto bad;
597 lim = desc.d ? 0xffffffff : 0xffff;
598 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
599 goto bad;
600 }
Avi Kivity717746e2011-04-20 13:37:53 +0300601 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300602 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300603 cpl = max(cpl, rpl);
604 if (!(desc.type & 8)) {
605 /* data segment */
606 if (cpl > desc.dpl)
607 goto bad;
608 } else if ((desc.type & 8) && !(desc.type & 4)) {
609 /* nonconforming code segment */
610 if (cpl != desc.dpl)
611 goto bad;
612 } else if ((desc.type & 8) && (desc.type & 4)) {
613 /* conforming code segment */
614 if (cpl < desc.dpl)
615 goto bad;
616 }
617 break;
618 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300619 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300620 la &= (u32)-1;
621 *linear = la;
622 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300623bad:
624 if (addr.seg == VCPU_SREG_SS)
625 return emulate_ss(ctxt, addr.seg);
626 else
627 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300628}
629
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400630static int linearize(struct x86_emulate_ctxt *ctxt,
631 struct segmented_address addr,
632 unsigned size, bool write,
633 ulong *linear)
634{
635 return __linearize(ctxt, addr, size, write, false, linear);
636}
637
638
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200639static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
640 struct segmented_address addr,
641 void *data,
642 unsigned size)
643{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200644 int rc;
645 ulong linear;
646
Avi Kivity83b87952011-04-03 11:31:19 +0300647 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200648 if (rc != X86EMUL_CONTINUE)
649 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300650 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200651}
652
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900653/*
654 * Fetch the next byte of the instruction being emulated which is pointed to
655 * by ctxt->_eip, then increment ctxt->_eip.
656 *
657 * Also prefetch the remaining bytes of the instruction without crossing page
658 * boundary if they are not in fetch_cache yet.
659 */
660static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200661{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300662 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200663 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300664 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200665
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900666 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400667 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900668 struct segmented_address addr = { .seg = VCPU_SREG_CS,
669 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300670 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900671 size = min(15UL - cur_size,
672 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400673 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900674 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400675 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900676 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
677 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900678 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200679 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300680 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200681 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900682 *dest = fc->data[ctxt->_eip - fc->start];
683 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900684 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200685}
686
687static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900688 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200689{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900690 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200691
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200692 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900693 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200694 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200695 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900696 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900697 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200698 return rc;
699 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900700 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200701}
702
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900703/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900704#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900705({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900706 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900707 if (rc != X86EMUL_CONTINUE) \
708 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900709 (_type)_x; \
710})
711
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900712#define insn_fetch_arr(_arr, _size, _ctxt) \
713({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900714 if (rc != X86EMUL_CONTINUE) \
715 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900716})
717
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000718/*
719 * Given the 'reg' portion of a ModRM byte, and a register block, return a
720 * pointer into the block that addresses the relevant register.
721 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
722 */
723static void *decode_register(u8 modrm_reg, unsigned long *regs,
724 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725{
726 void *p;
727
728 p = &regs[modrm_reg];
729 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
730 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
731 return p;
732}
733
734static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200735 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 u16 *size, unsigned long *address, int op_bytes)
737{
738 int rc;
739
740 if (op_bytes == 2)
741 op_bytes = 3;
742 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200743 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900744 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200746 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200747 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748 return rc;
749}
750
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300751static int test_cc(unsigned int condition, unsigned int flags)
752{
753 int rc = 0;
754
755 switch ((condition & 15) >> 1) {
756 case 0: /* o */
757 rc |= (flags & EFLG_OF);
758 break;
759 case 1: /* b/c/nae */
760 rc |= (flags & EFLG_CF);
761 break;
762 case 2: /* z/e */
763 rc |= (flags & EFLG_ZF);
764 break;
765 case 3: /* be/na */
766 rc |= (flags & (EFLG_CF|EFLG_ZF));
767 break;
768 case 4: /* s */
769 rc |= (flags & EFLG_SF);
770 break;
771 case 5: /* p/pe */
772 rc |= (flags & EFLG_PF);
773 break;
774 case 7: /* le/ng */
775 rc |= (flags & EFLG_ZF);
776 /* fall through */
777 case 6: /* l/nge */
778 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
779 break;
780 }
781
782 /* Odd condition identifiers (lsb == 1) have inverted sense. */
783 return (!!rc ^ (condition & 1));
784}
785
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300786static void fetch_register_operand(struct operand *op)
787{
788 switch (op->bytes) {
789 case 1:
790 op->val = *(u8 *)op->addr.reg;
791 break;
792 case 2:
793 op->val = *(u16 *)op->addr.reg;
794 break;
795 case 4:
796 op->val = *(u32 *)op->addr.reg;
797 break;
798 case 8:
799 op->val = *(u64 *)op->addr.reg;
800 break;
801 }
802}
803
Avi Kivity12537912011-03-29 11:41:27 +0200804static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
805{
806 ctxt->ops->get_fpu(ctxt);
807 switch (reg) {
808 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
809 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
810 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
811 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
812 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
813 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
814 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
815 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
816#ifdef CONFIG_X86_64
817 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
818 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
819 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
820 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
821 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
822 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
823 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
824 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
825#endif
826 default: BUG();
827 }
828 ctxt->ops->put_fpu(ctxt);
829}
830
831static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
832 int reg)
833{
834 ctxt->ops->get_fpu(ctxt);
835 switch (reg) {
836 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
837 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
838 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
839 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
840 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
841 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
842 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
843 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
844#ifdef CONFIG_X86_64
845 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
846 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
847 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
848 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
849 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
850 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
851 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
852 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
853#endif
854 default: BUG();
855 }
856 ctxt->ops->put_fpu(ctxt);
857}
858
859static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
860 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200861 int inhibit_bytereg)
862{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300863 unsigned reg = ctxt->modrm_reg;
864 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200865
Avi Kivity9dac77f2011-06-01 15:34:25 +0300866 if (!(ctxt->d & ModRM))
867 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200868
Avi Kivity9dac77f2011-06-01 15:34:25 +0300869 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200870 op->type = OP_XMM;
871 op->bytes = 16;
872 op->addr.xmm = reg;
873 read_sse_reg(ctxt, &op->vec_val, reg);
874 return;
875 }
876
Avi Kivity3c118e22007-10-31 10:27:04 +0200877 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300878 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
879 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200880 op->bytes = 1;
881 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300882 op->addr.reg = decode_register(reg, ctxt->regs, 0);
883 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200884 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300885 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200886 op->orig_val = op->val;
887}
888
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200889static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300890 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200891{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200892 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700893 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900894 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300895 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200896
Avi Kivity9dac77f2011-06-01 15:34:25 +0300897 if (ctxt->rex_prefix) {
898 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
899 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
900 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200901 }
902
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900903 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300904 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
905 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
906 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
907 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200908
Avi Kivity9dac77f2011-06-01 15:34:25 +0300909 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300910 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300911 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
912 op->addr.reg = decode_register(ctxt->modrm_rm,
913 ctxt->regs, ctxt->d & ByteOp);
914 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200915 op->type = OP_XMM;
916 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300917 op->addr.xmm = ctxt->modrm_rm;
918 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200919 return rc;
920 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300921 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200922 return rc;
923 }
924
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300925 op->type = OP_MEM;
926
Avi Kivity9dac77f2011-06-01 15:34:25 +0300927 if (ctxt->ad_bytes == 2) {
928 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
929 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
930 unsigned si = ctxt->regs[VCPU_REGS_RSI];
931 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932
933 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300934 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300936 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900937 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200938 break;
939 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900940 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200941 break;
942 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900943 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200944 break;
945 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300946 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200947 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300948 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200949 break;
950 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300951 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200952 break;
953 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300954 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200955 break;
956 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300957 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200958 break;
959 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300960 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200961 break;
962 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300963 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200964 break;
965 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300966 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300967 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200968 break;
969 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300970 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200971 break;
972 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300973 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
974 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
975 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300976 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200977 } else {
978 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300979 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900980 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200981 index_reg |= (sib >> 3) & 7;
982 base_reg |= sib & 7;
983 scale = sib >> 6;
984
Avi Kivity9dac77f2011-06-01 15:34:25 +0300985 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900986 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700987 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300988 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700989 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300990 modrm_ea += ctxt->regs[index_reg] << scale;
991 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700992 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300993 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700994 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300995 modrm_ea += ctxt->regs[ctxt->modrm_rm];
996 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200997 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300998 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900999 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001000 break;
1001 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001002 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001003 break;
1004 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001005 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006 break;
1007 }
1008 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001009 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001010done:
1011 return rc;
1012}
1013
1014static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001015 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001016{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001017 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001018
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001019 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001020 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001021 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001022 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001023 break;
1024 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001025 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001026 break;
1027 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001028 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001029 break;
1030 }
1031done:
1032 return rc;
1033}
1034
Avi Kivity9dac77f2011-06-01 15:34:25 +03001035static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001036{
Sheng Yang7129eec2010-09-28 16:33:32 +08001037 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001038
Avi Kivity9dac77f2011-06-01 15:34:25 +03001039 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1040 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001041
Avi Kivity9dac77f2011-06-01 15:34:25 +03001042 if (ctxt->src.bytes == 2)
1043 sv = (s16)ctxt->src.val & (s16)mask;
1044 else if (ctxt->src.bytes == 4)
1045 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001046
Avi Kivity9dac77f2011-06-01 15:34:25 +03001047 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001048 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001049
1050 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001051 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001052}
1053
Gleb Natapov9de41572010-04-28 19:15:22 +03001054static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001055 unsigned long addr, void *dest, unsigned size)
1056{
1057 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001058 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001059
1060 while (size) {
1061 int n = min(size, 8u);
1062 size -= n;
1063 if (mc->pos < mc->end)
1064 goto read_cached;
1065
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001066 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1067 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001068 if (rc != X86EMUL_CONTINUE)
1069 return rc;
1070 mc->end += n;
1071
1072 read_cached:
1073 memcpy(dest, mc->data + mc->pos, n);
1074 mc->pos += n;
1075 dest += n;
1076 addr += n;
1077 }
1078 return X86EMUL_CONTINUE;
1079}
1080
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001081static int segmented_read(struct x86_emulate_ctxt *ctxt,
1082 struct segmented_address addr,
1083 void *data,
1084 unsigned size)
1085{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001086 int rc;
1087 ulong linear;
1088
Avi Kivity83b87952011-04-03 11:31:19 +03001089 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001090 if (rc != X86EMUL_CONTINUE)
1091 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001092 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001093}
1094
1095static int segmented_write(struct x86_emulate_ctxt *ctxt,
1096 struct segmented_address addr,
1097 const void *data,
1098 unsigned size)
1099{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001100 int rc;
1101 ulong linear;
1102
Avi Kivity83b87952011-04-03 11:31:19 +03001103 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001104 if (rc != X86EMUL_CONTINUE)
1105 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001106 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1107 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001108}
1109
1110static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1111 struct segmented_address addr,
1112 const void *orig_data, const void *data,
1113 unsigned size)
1114{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001115 int rc;
1116 ulong linear;
1117
Avi Kivity83b87952011-04-03 11:31:19 +03001118 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001119 if (rc != X86EMUL_CONTINUE)
1120 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001121 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1122 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001123}
1124
Gleb Natapov7b262e92010-03-18 15:20:27 +02001125static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001126 unsigned int size, unsigned short port,
1127 void *dest)
1128{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001130
1131 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001132 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001133 unsigned int count = ctxt->rep_prefix ?
1134 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001135 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001136 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1137 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001138 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1139 count);
1140 if (n == 0)
1141 n = 1;
1142 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001143 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001144 return 0;
1145 rc->end = n * size;
1146 }
1147
1148 memcpy(dest, rc->data + rc->pos, size);
1149 rc->pos += size;
1150 return 1;
1151}
1152
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001153static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001154 u16 selector, struct desc_ptr *dt)
1155{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001156 struct x86_emulate_ops *ops = ctxt->ops;
1157
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001158 if (selector & 1 << 2) {
1159 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001160 u16 sel;
1161
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001162 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001163 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001164 return;
1165
1166 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1167 dt->address = get_desc_base(&desc);
1168 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001169 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001170}
1171
1172/* allowed just for 8 bytes segments */
1173static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001174 u16 selector, struct desc_struct *desc)
1175{
1176 struct desc_ptr dt;
1177 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001178 ulong addr;
1179
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001180 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001181
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001182 if (dt.size < index * 8 + 7)
1183 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001184
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001185 addr = dt.address + index * 8;
1186 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1187 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001188}
1189
1190/* allowed just for 8 bytes segments */
1191static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001192 u16 selector, struct desc_struct *desc)
1193{
1194 struct desc_ptr dt;
1195 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001196 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001197
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001198 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001199
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001200 if (dt.size < index * 8 + 7)
1201 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001202
1203 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001204 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1205 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001206}
1207
Gleb Natapov5601d052011-03-07 14:55:06 +02001208/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001209static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001210 u16 selector, int seg)
1211{
1212 struct desc_struct seg_desc;
1213 u8 dpl, rpl, cpl;
1214 unsigned err_vec = GP_VECTOR;
1215 u32 err_code = 0;
1216 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1217 int ret;
1218
1219 memset(&seg_desc, 0, sizeof seg_desc);
1220
1221 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1222 || ctxt->mode == X86EMUL_MODE_REAL) {
1223 /* set real mode segment descriptor */
1224 set_desc_base(&seg_desc, selector << 4);
1225 set_desc_limit(&seg_desc, 0xffff);
1226 seg_desc.type = 3;
1227 seg_desc.p = 1;
1228 seg_desc.s = 1;
1229 goto load;
1230 }
1231
1232 /* NULL selector is not valid for TR, CS and SS */
1233 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1234 && null_selector)
1235 goto exception;
1236
1237 /* TR should be in GDT only */
1238 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1239 goto exception;
1240
1241 if (null_selector) /* for NULL selector skip all following checks */
1242 goto load;
1243
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001244 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001245 if (ret != X86EMUL_CONTINUE)
1246 return ret;
1247
1248 err_code = selector & 0xfffc;
1249 err_vec = GP_VECTOR;
1250
1251 /* can't load system descriptor into segment selecor */
1252 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1253 goto exception;
1254
1255 if (!seg_desc.p) {
1256 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1257 goto exception;
1258 }
1259
1260 rpl = selector & 3;
1261 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001262 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001263
1264 switch (seg) {
1265 case VCPU_SREG_SS:
1266 /*
1267 * segment is not a writable data segment or segment
1268 * selector's RPL != CPL or segment selector's RPL != CPL
1269 */
1270 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1271 goto exception;
1272 break;
1273 case VCPU_SREG_CS:
1274 if (!(seg_desc.type & 8))
1275 goto exception;
1276
1277 if (seg_desc.type & 4) {
1278 /* conforming */
1279 if (dpl > cpl)
1280 goto exception;
1281 } else {
1282 /* nonconforming */
1283 if (rpl > cpl || dpl != cpl)
1284 goto exception;
1285 }
1286 /* CS(RPL) <- CPL */
1287 selector = (selector & 0xfffc) | cpl;
1288 break;
1289 case VCPU_SREG_TR:
1290 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1291 goto exception;
1292 break;
1293 case VCPU_SREG_LDTR:
1294 if (seg_desc.s || seg_desc.type != 2)
1295 goto exception;
1296 break;
1297 default: /* DS, ES, FS, or GS */
1298 /*
1299 * segment is not a data or readable code segment or
1300 * ((segment is a data or nonconforming code segment)
1301 * and (both RPL and CPL > DPL))
1302 */
1303 if ((seg_desc.type & 0xa) == 0x8 ||
1304 (((seg_desc.type & 0xc) != 0xc) &&
1305 (rpl > dpl && cpl > dpl)))
1306 goto exception;
1307 break;
1308 }
1309
1310 if (seg_desc.s) {
1311 /* mark segment as accessed */
1312 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001313 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001314 if (ret != X86EMUL_CONTINUE)
1315 return ret;
1316 }
1317load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001318 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001319 return X86EMUL_CONTINUE;
1320exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001321 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001322 return X86EMUL_PROPAGATE_FAULT;
1323}
1324
Wei Yongjun31be40b2010-08-17 09:17:30 +08001325static void write_register_operand(struct operand *op)
1326{
1327 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1328 switch (op->bytes) {
1329 case 1:
1330 *(u8 *)op->addr.reg = (u8)op->val;
1331 break;
1332 case 2:
1333 *(u16 *)op->addr.reg = (u16)op->val;
1334 break;
1335 case 4:
1336 *op->addr.reg = (u32)op->val;
1337 break; /* 64b: zero-extend */
1338 case 8:
1339 *op->addr.reg = op->val;
1340 break;
1341 }
1342}
1343
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001344static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001345{
1346 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001347
Avi Kivity9dac77f2011-06-01 15:34:25 +03001348 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001349 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001350 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001351 break;
1352 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001353 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001354 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001355 ctxt->dst.addr.mem,
1356 &ctxt->dst.orig_val,
1357 &ctxt->dst.val,
1358 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001359 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001360 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001361 ctxt->dst.addr.mem,
1362 &ctxt->dst.val,
1363 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001364 if (rc != X86EMUL_CONTINUE)
1365 return rc;
1366 break;
Avi Kivity12537912011-03-29 11:41:27 +02001367 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001368 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001369 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001370 case OP_NONE:
1371 /* no writeback */
1372 break;
1373 default:
1374 break;
1375 }
1376 return X86EMUL_CONTINUE;
1377}
1378
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001379static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001380{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001381 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382
Avi Kivity9dac77f2011-06-01 15:34:25 +03001383 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1384 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001385 addr.seg = VCPU_SREG_SS;
1386
1387 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001388 ctxt->dst.type = OP_NONE;
1389 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001390}
1391
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001392static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001393 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001394{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001395 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001396 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001397
Avi Kivity9dac77f2011-06-01 15:34:25 +03001398 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001399 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001400 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001401 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001402 return rc;
1403
Avi Kivity9dac77f2011-06-01 15:34:25 +03001404 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001405 return rc;
1406}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001407
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001408static int em_pop(struct x86_emulate_ctxt *ctxt)
1409{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001410 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001411}
1412
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001413static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001414 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001415{
1416 int rc;
1417 unsigned long val, change_mask;
1418 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001419 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001420
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001421 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001422 if (rc != X86EMUL_CONTINUE)
1423 return rc;
1424
1425 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1426 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1427
1428 switch(ctxt->mode) {
1429 case X86EMUL_MODE_PROT64:
1430 case X86EMUL_MODE_PROT32:
1431 case X86EMUL_MODE_PROT16:
1432 if (cpl == 0)
1433 change_mask |= EFLG_IOPL;
1434 if (cpl <= iopl)
1435 change_mask |= EFLG_IF;
1436 break;
1437 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001438 if (iopl < 3)
1439 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001440 change_mask |= EFLG_IF;
1441 break;
1442 default: /* real mode */
1443 change_mask |= (EFLG_IOPL | EFLG_IF);
1444 break;
1445 }
1446
1447 *(unsigned long *)dest =
1448 (ctxt->eflags & ~change_mask) | (val & change_mask);
1449
1450 return rc;
1451}
1452
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001453static int em_popf(struct x86_emulate_ctxt *ctxt)
1454{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001455 ctxt->dst.type = OP_REG;
1456 ctxt->dst.addr.reg = &ctxt->eflags;
1457 ctxt->dst.bytes = ctxt->op_bytes;
1458 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001459}
1460
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001461static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001462{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001463 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001464
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001465 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001466}
1467
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001468static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001469{
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001470 unsigned long selector;
1471 int rc;
1472
Avi Kivity9dac77f2011-06-01 15:34:25 +03001473 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001474 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001475 return rc;
1476
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001477 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001478 return rc;
1479}
1480
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001481static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001482{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001483 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001484 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001485 int reg = VCPU_REGS_RAX;
1486
1487 while (reg <= VCPU_REGS_RDI) {
1488 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001489 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001490
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001491 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001492 if (rc != X86EMUL_CONTINUE)
1493 return rc;
1494
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001495 ++reg;
1496 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001497
Wei Yongjunc37eda12010-06-15 09:03:33 +08001498 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001499}
1500
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001501static int em_pushf(struct x86_emulate_ctxt *ctxt)
1502{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001503 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001504 return em_push(ctxt);
1505}
1506
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001507static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001508{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001509 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001510 int reg = VCPU_REGS_RDI;
1511
1512 while (reg >= VCPU_REGS_RAX) {
1513 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001514 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1515 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001516 --reg;
1517 }
1518
Avi Kivity9dac77f2011-06-01 15:34:25 +03001519 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001520 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001521 break;
1522 --reg;
1523 }
1524 return rc;
1525}
1526
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001527int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001528{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001529 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001530 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001531 struct desc_ptr dt;
1532 gva_t cs_addr;
1533 gva_t eip_addr;
1534 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001535
1536 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001537 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001538 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001539 if (rc != X86EMUL_CONTINUE)
1540 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001541
1542 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1543
Avi Kivity9dac77f2011-06-01 15:34:25 +03001544 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001545 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001546 if (rc != X86EMUL_CONTINUE)
1547 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001548
Avi Kivity9dac77f2011-06-01 15:34:25 +03001549 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001550 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001551 if (rc != X86EMUL_CONTINUE)
1552 return rc;
1553
Avi Kivity4bff1e862011-04-20 13:37:53 +03001554 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001555
1556 eip_addr = dt.address + (irq << 2);
1557 cs_addr = dt.address + (irq << 2) + 2;
1558
Avi Kivity0f65dd72011-04-20 13:37:53 +03001559 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001560 if (rc != X86EMUL_CONTINUE)
1561 return rc;
1562
Avi Kivity0f65dd72011-04-20 13:37:53 +03001563 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001564 if (rc != X86EMUL_CONTINUE)
1565 return rc;
1566
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001567 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001568 if (rc != X86EMUL_CONTINUE)
1569 return rc;
1570
Avi Kivity9dac77f2011-06-01 15:34:25 +03001571 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001572
1573 return rc;
1574}
1575
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001576static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001577{
1578 switch(ctxt->mode) {
1579 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001580 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001581 case X86EMUL_MODE_VM86:
1582 case X86EMUL_MODE_PROT16:
1583 case X86EMUL_MODE_PROT32:
1584 case X86EMUL_MODE_PROT64:
1585 default:
1586 /* Protected mode interrupts unimplemented yet */
1587 return X86EMUL_UNHANDLEABLE;
1588 }
1589}
1590
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001591static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001592{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001593 int rc = X86EMUL_CONTINUE;
1594 unsigned long temp_eip = 0;
1595 unsigned long temp_eflags = 0;
1596 unsigned long cs = 0;
1597 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1598 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1599 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1600 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1601
1602 /* TODO: Add stack limit check */
1603
Avi Kivity9dac77f2011-06-01 15:34:25 +03001604 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001605
1606 if (rc != X86EMUL_CONTINUE)
1607 return rc;
1608
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001609 if (temp_eip & ~0xffff)
1610 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001611
Avi Kivity9dac77f2011-06-01 15:34:25 +03001612 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001613
1614 if (rc != X86EMUL_CONTINUE)
1615 return rc;
1616
Avi Kivity9dac77f2011-06-01 15:34:25 +03001617 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001618
1619 if (rc != X86EMUL_CONTINUE)
1620 return rc;
1621
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001622 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001623
1624 if (rc != X86EMUL_CONTINUE)
1625 return rc;
1626
Avi Kivity9dac77f2011-06-01 15:34:25 +03001627 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001628
1629
Avi Kivity9dac77f2011-06-01 15:34:25 +03001630 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001631 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001632 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001633 ctxt->eflags &= ~0xffff;
1634 ctxt->eflags |= temp_eflags;
1635 }
1636
1637 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1638 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1639
1640 return rc;
1641}
1642
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001643static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001644{
1645 switch(ctxt->mode) {
1646 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001647 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001648 case X86EMUL_MODE_VM86:
1649 case X86EMUL_MODE_PROT16:
1650 case X86EMUL_MODE_PROT32:
1651 case X86EMUL_MODE_PROT64:
1652 default:
1653 /* iret from protected mode unimplemented yet */
1654 return X86EMUL_UNHANDLEABLE;
1655 }
1656}
1657
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001658static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1659{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001660 int rc;
1661 unsigned short sel;
1662
Avi Kivity9dac77f2011-06-01 15:34:25 +03001663 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001664
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001665 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001666 if (rc != X86EMUL_CONTINUE)
1667 return rc;
1668
Avi Kivity9dac77f2011-06-01 15:34:25 +03001669 ctxt->_eip = 0;
1670 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001671 return X86EMUL_CONTINUE;
1672}
1673
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001674static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001675{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001676 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001677}
1678
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001679static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001680{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001681 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001682 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001683 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001684 break;
1685 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001686 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001687 break;
1688 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001689 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001690 break;
1691 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001692 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001693 break;
1694 case 4: /* sal/shl */
1695 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001696 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697 break;
1698 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001699 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001700 break;
1701 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001702 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001703 break;
1704 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001705 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706}
1707
Avi Kivity3329ece2011-09-13 10:45:39 +03001708static int em_not(struct x86_emulate_ctxt *ctxt)
1709{
1710 ctxt->dst.val = ~ctxt->dst.val;
1711 return X86EMUL_CONTINUE;
1712}
1713
1714static int em_neg(struct x86_emulate_ctxt *ctxt)
1715{
1716 emulate_1op(ctxt, "neg");
1717 return X86EMUL_CONTINUE;
1718}
1719
1720static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1721{
1722 u8 ex = 0;
1723
1724 emulate_1op_rax_rdx(ctxt, "mul", ex);
1725 return X86EMUL_CONTINUE;
1726}
1727
1728static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1729{
1730 u8 ex = 0;
1731
1732 emulate_1op_rax_rdx(ctxt, "imul", ex);
1733 return X86EMUL_CONTINUE;
1734}
1735
1736static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737{
Avi Kivity34d1f492010-08-26 11:59:01 +03001738 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739
Avi Kivity3329ece2011-09-13 10:45:39 +03001740 emulate_1op_rax_rdx(ctxt, "div", de);
1741 if (de)
1742 return emulate_de(ctxt);
1743 return X86EMUL_CONTINUE;
1744}
1745
1746static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1747{
1748 u8 de = 0;
1749
1750 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001751 if (de)
1752 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001753 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754}
1755
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001756static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001758 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001759
Avi Kivity9dac77f2011-06-01 15:34:25 +03001760 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001762 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763 break;
1764 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001765 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001766 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001767 case 2: /* call near abs */ {
1768 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001769 old_eip = ctxt->_eip;
1770 ctxt->_eip = ctxt->src.val;
1771 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001772 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001773 break;
1774 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001776 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001778 case 5: /* jmp far */
1779 rc = em_jmp_far(ctxt);
1780 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001782 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001783 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001785 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001786}
1787
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001788static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001790 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791
Avi Kivity9dac77f2011-06-01 15:34:25 +03001792 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1793 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1794 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1795 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001796 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001797 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001798 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1799 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001800
Laurent Vivier05f086f2007-09-24 11:10:55 +02001801 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001802 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001803 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001804}
1805
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001806static int em_ret(struct x86_emulate_ctxt *ctxt)
1807{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001808 ctxt->dst.type = OP_REG;
1809 ctxt->dst.addr.reg = &ctxt->_eip;
1810 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001811 return em_pop(ctxt);
1812}
1813
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001814static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001815{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001816 int rc;
1817 unsigned long cs;
1818
Avi Kivity9dac77f2011-06-01 15:34:25 +03001819 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001820 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001821 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001822 if (ctxt->op_bytes == 4)
1823 ctxt->_eip = (u32)ctxt->_eip;
1824 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001825 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001826 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001827 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001828 return rc;
1829}
1830
Avi Kivityd4b43252011-09-13 10:45:50 +03001831static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001832{
Avi Kivityd4b43252011-09-13 10:45:50 +03001833 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001834 unsigned short sel;
1835 int rc;
1836
Avi Kivity9dac77f2011-06-01 15:34:25 +03001837 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001838
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001839 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001840 if (rc != X86EMUL_CONTINUE)
1841 return rc;
1842
Avi Kivity9dac77f2011-06-01 15:34:25 +03001843 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001844 return rc;
1845}
1846
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001847static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001848setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001849 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001850{
Avi Kivity1aa36612011-04-27 13:20:30 +03001851 u16 selector;
1852
Gleb Natapov79168fd2010-04-28 19:15:30 +03001853 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001854 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001855 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001856
1857 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001858 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001859 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001860 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001861 cs->type = 0x0b; /* Read, Execute, Accessed */
1862 cs->s = 1;
1863 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001864 cs->p = 1;
1865 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001866
Gleb Natapov79168fd2010-04-28 19:15:30 +03001867 set_desc_base(ss, 0); /* flat segment */
1868 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001869 ss->g = 1; /* 4kb granularity */
1870 ss->s = 1;
1871 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001872 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001873 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001874 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001875}
1876
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001877static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001878{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001879 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001880 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001881 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001883 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001884
1885 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001886 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001887 ctxt->mode == X86EMUL_MODE_VM86)
1888 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001889
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001890 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001891 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001892
Avi Kivity717746e2011-04-20 13:37:53 +03001893 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001894 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001895 cs_sel = (u16)(msr_data & 0xfffc);
1896 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001897
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001898 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001899 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001900 cs.l = 1;
1901 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001902 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1903 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001904
Avi Kivity9dac77f2011-06-01 15:34:25 +03001905 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001906 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001907#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001909
Avi Kivity717746e2011-04-20 13:37:53 +03001910 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001911 ctxt->mode == X86EMUL_MODE_PROT64 ?
1912 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001913 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001914
Avi Kivity717746e2011-04-20 13:37:53 +03001915 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001916 ctxt->eflags &= ~(msr_data | EFLG_RF);
1917#endif
1918 } else {
1919 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001920 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001921 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001922
1923 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1924 }
1925
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001926 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001927}
1928
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001929static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001930{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001931 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001932 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001933 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001934 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001935 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001936
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001937 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001938 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001939 if (ctxt->mode == X86EMUL_MODE_REAL)
1940 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001941
1942 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1943 * Therefore, we inject an #UD.
1944 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001945 if (ctxt->mode == X86EMUL_MODE_PROT64)
1946 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001947
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001948 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001949
Avi Kivity717746e2011-04-20 13:37:53 +03001950 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001951 switch (ctxt->mode) {
1952 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001953 if ((msr_data & 0xfffc) == 0x0)
1954 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001955 break;
1956 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001957 if (msr_data == 0x0)
1958 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001959 break;
1960 }
1961
1962 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001963 cs_sel = (u16)msr_data;
1964 cs_sel &= ~SELECTOR_RPL_MASK;
1965 ss_sel = cs_sel + 8;
1966 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001967 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001968 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001969 cs.l = 1;
1970 }
1971
Avi Kivity1aa36612011-04-27 13:20:30 +03001972 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1973 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001974
Avi Kivity717746e2011-04-20 13:37:53 +03001975 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001976 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001977
Avi Kivity717746e2011-04-20 13:37:53 +03001978 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001979 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001980
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001981 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001982}
1983
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001984static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001985{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001986 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001987 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001988 u64 msr_data;
1989 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001990 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001991
Gleb Natapova0044752010-02-10 14:21:31 +02001992 /* inject #GP if in real mode or Virtual 8086 mode */
1993 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001994 ctxt->mode == X86EMUL_MODE_VM86)
1995 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001996
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001997 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001998
Avi Kivity9dac77f2011-06-01 15:34:25 +03001999 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002000 usermode = X86EMUL_MODE_PROT64;
2001 else
2002 usermode = X86EMUL_MODE_PROT32;
2003
2004 cs.dpl = 3;
2005 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002006 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002007 switch (usermode) {
2008 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002009 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002010 if ((msr_data & 0xfffc) == 0x0)
2011 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002012 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002013 break;
2014 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002015 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002016 if (msr_data == 0x0)
2017 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002018 ss_sel = cs_sel + 8;
2019 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002020 cs.l = 1;
2021 break;
2022 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002023 cs_sel |= SELECTOR_RPL_MASK;
2024 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002025
Avi Kivity1aa36612011-04-27 13:20:30 +03002026 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2027 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002028
Avi Kivity9dac77f2011-06-01 15:34:25 +03002029 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2030 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002031
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002032 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002033}
2034
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002035static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002036{
2037 int iopl;
2038 if (ctxt->mode == X86EMUL_MODE_REAL)
2039 return false;
2040 if (ctxt->mode == X86EMUL_MODE_VM86)
2041 return true;
2042 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002043 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002044}
2045
2046static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002047 u16 port, u16 len)
2048{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002049 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002050 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002051 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002052 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002053 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002054 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002055 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002056
Avi Kivity1aa36612011-04-27 13:20:30 +03002057 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002058 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002059 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002060 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002061 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002062 base = get_desc_base(&tr_seg);
2063#ifdef CONFIG_X86_64
2064 base |= ((u64)base3) << 32;
2065#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002066 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002067 if (r != X86EMUL_CONTINUE)
2068 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002069 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002070 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002071 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002072 if (r != X86EMUL_CONTINUE)
2073 return false;
2074 if ((perm >> bit_idx) & mask)
2075 return false;
2076 return true;
2077}
2078
2079static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002080 u16 port, u16 len)
2081{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002082 if (ctxt->perm_ok)
2083 return true;
2084
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002085 if (emulator_bad_iopl(ctxt))
2086 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002087 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002088
2089 ctxt->perm_ok = true;
2090
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002091 return true;
2092}
2093
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002094static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002095 struct tss_segment_16 *tss)
2096{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002097 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002098 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002099 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2100 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2101 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2102 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2103 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2104 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2105 tss->si = ctxt->regs[VCPU_REGS_RSI];
2106 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002107
Avi Kivity1aa36612011-04-27 13:20:30 +03002108 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2109 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2110 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2111 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2112 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002113}
2114
2115static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002116 struct tss_segment_16 *tss)
2117{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002118 int ret;
2119
Avi Kivity9dac77f2011-06-01 15:34:25 +03002120 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002121 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002122 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2123 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2124 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2125 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2126 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2127 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2128 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2129 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002130
2131 /*
2132 * SDM says that segment selectors are loaded before segment
2133 * descriptors
2134 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002135 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2136 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2137 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2138 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2139 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002140
2141 /*
2142 * Now load segment descriptors. If fault happenes at this stage
2143 * it is handled in a context of new task
2144 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002145 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002146 if (ret != X86EMUL_CONTINUE)
2147 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002148 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002149 if (ret != X86EMUL_CONTINUE)
2150 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002151 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 if (ret != X86EMUL_CONTINUE)
2153 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002154 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002155 if (ret != X86EMUL_CONTINUE)
2156 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002157 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002158 if (ret != X86EMUL_CONTINUE)
2159 return ret;
2160
2161 return X86EMUL_CONTINUE;
2162}
2163
2164static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165 u16 tss_selector, u16 old_tss_sel,
2166 ulong old_tss_base, struct desc_struct *new_desc)
2167{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002168 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002169 struct tss_segment_16 tss_seg;
2170 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002171 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002172
Avi Kivity0f65dd72011-04-20 13:37:53 +03002173 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002174 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002175 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002176 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002179 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180
Avi Kivity0f65dd72011-04-20 13:37:53 +03002181 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002182 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002183 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002184 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002186
Avi Kivity0f65dd72011-04-20 13:37:53 +03002187 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002188 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002189 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002191 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002192
2193 if (old_tss_sel != 0xffff) {
2194 tss_seg.prev_task_link = old_tss_sel;
2195
Avi Kivity0f65dd72011-04-20 13:37:53 +03002196 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002197 &tss_seg.prev_task_link,
2198 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002199 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002200 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002201 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002202 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002203 }
2204
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002205 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002206}
2207
2208static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002209 struct tss_segment_32 *tss)
2210{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002211 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002212 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002213 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002214 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2215 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2216 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2217 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2218 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2219 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2220 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2221 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002222
Avi Kivity1aa36612011-04-27 13:20:30 +03002223 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2224 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2225 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2226 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2227 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2228 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2229 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002230}
2231
2232static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002233 struct tss_segment_32 *tss)
2234{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002235 int ret;
2236
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002237 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002238 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002239 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002240 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002241 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2242 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2243 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2244 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2245 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2246 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2247 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2248 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002249
2250 /*
2251 * SDM says that segment selectors are loaded before segment
2252 * descriptors
2253 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002254 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2255 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2256 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2257 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2258 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2259 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2260 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002261
2262 /*
2263 * Now load segment descriptors. If fault happenes at this stage
2264 * it is handled in a context of new task
2265 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002266 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002267 if (ret != X86EMUL_CONTINUE)
2268 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002269 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002270 if (ret != X86EMUL_CONTINUE)
2271 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002272 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002273 if (ret != X86EMUL_CONTINUE)
2274 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002275 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002276 if (ret != X86EMUL_CONTINUE)
2277 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002278 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002279 if (ret != X86EMUL_CONTINUE)
2280 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002281 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282 if (ret != X86EMUL_CONTINUE)
2283 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002284 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002285 if (ret != X86EMUL_CONTINUE)
2286 return ret;
2287
2288 return X86EMUL_CONTINUE;
2289}
2290
2291static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292 u16 tss_selector, u16 old_tss_sel,
2293 ulong old_tss_base, struct desc_struct *new_desc)
2294{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002295 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002296 struct tss_segment_32 tss_seg;
2297 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002298 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002299
Avi Kivity0f65dd72011-04-20 13:37:53 +03002300 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002301 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002302 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002304 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002305
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002306 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002307
Avi Kivity0f65dd72011-04-20 13:37:53 +03002308 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002309 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002310 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002313
Avi Kivity0f65dd72011-04-20 13:37:53 +03002314 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002315 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002316 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002319
2320 if (old_tss_sel != 0xffff) {
2321 tss_seg.prev_task_link = old_tss_sel;
2322
Avi Kivity0f65dd72011-04-20 13:37:53 +03002323 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002324 &tss_seg.prev_task_link,
2325 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002326 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002327 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002329 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002330 }
2331
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002332 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333}
2334
2335static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002336 u16 tss_selector, int reason,
2337 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002338{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002339 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002340 struct desc_struct curr_tss_desc, next_tss_desc;
2341 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002342 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002343 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002344 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002345 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002346
2347 /* FIXME: old_tss_base == ~0 ? */
2348
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002349 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350 if (ret != X86EMUL_CONTINUE)
2351 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002352 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002353 if (ret != X86EMUL_CONTINUE)
2354 return ret;
2355
2356 /* FIXME: check that next_tss_desc is tss */
2357
2358 if (reason != TASK_SWITCH_IRET) {
2359 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002360 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002361 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002362 }
2363
Gleb Natapovceffb452010-03-18 15:20:19 +02002364 desc_limit = desc_limit_scaled(&next_tss_desc);
2365 if (!next_tss_desc.p ||
2366 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2367 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002368 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002369 return X86EMUL_PROPAGATE_FAULT;
2370 }
2371
2372 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2373 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002374 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375 }
2376
2377 if (reason == TASK_SWITCH_IRET)
2378 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2379
2380 /* set back link to prev task only if NT bit is set in eflags
2381 note that old_tss_sel is not used afetr this point */
2382 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2383 old_tss_sel = 0xffff;
2384
2385 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002386 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387 old_tss_base, &next_tss_desc);
2388 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002389 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002390 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002391 if (ret != X86EMUL_CONTINUE)
2392 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002393
2394 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2395 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2396
2397 if (reason != TASK_SWITCH_IRET) {
2398 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002399 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002400 }
2401
Avi Kivity717746e2011-04-20 13:37:53 +03002402 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002403 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002404
Jan Kiszkae269fb22010-04-14 15:51:09 +02002405 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002406 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2407 ctxt->lock_prefix = 0;
2408 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002409 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002410 }
2411
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 return ret;
2413}
2414
2415int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002416 u16 tss_selector, int reason,
2417 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419 int rc;
2420
Avi Kivity9dac77f2011-06-01 15:34:25 +03002421 ctxt->_eip = ctxt->eip;
2422 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002423
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002424 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002425 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002426
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002427 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002428 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002429
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002430 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002431}
2432
Avi Kivity90de84f2010-11-17 15:28:21 +02002433static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002434 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002435{
Gleb Natapova682e352010-03-18 15:20:21 +02002436 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2437
Avi Kivity9dac77f2011-06-01 15:34:25 +03002438 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2439 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002440 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002441}
2442
Avi Kivity7af04fc2010-08-18 14:16:35 +03002443static int em_das(struct x86_emulate_ctxt *ctxt)
2444{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002445 u8 al, old_al;
2446 bool af, cf, old_cf;
2447
2448 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002449 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002450
2451 old_al = al;
2452 old_cf = cf;
2453 cf = false;
2454 af = ctxt->eflags & X86_EFLAGS_AF;
2455 if ((al & 0x0f) > 9 || af) {
2456 al -= 6;
2457 cf = old_cf | (al >= 250);
2458 af = true;
2459 } else {
2460 af = false;
2461 }
2462 if (old_al > 0x99 || old_cf) {
2463 al -= 0x60;
2464 cf = true;
2465 }
2466
Avi Kivity9dac77f2011-06-01 15:34:25 +03002467 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002468 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002469 ctxt->src.type = OP_IMM;
2470 ctxt->src.val = 0;
2471 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002472 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002473 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2474 if (cf)
2475 ctxt->eflags |= X86_EFLAGS_CF;
2476 if (af)
2477 ctxt->eflags |= X86_EFLAGS_AF;
2478 return X86EMUL_CONTINUE;
2479}
2480
Avi Kivity0ef753b2010-08-18 14:51:45 +03002481static int em_call_far(struct x86_emulate_ctxt *ctxt)
2482{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002483 u16 sel, old_cs;
2484 ulong old_eip;
2485 int rc;
2486
Avi Kivity1aa36612011-04-27 13:20:30 +03002487 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002488 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002489
Avi Kivity9dac77f2011-06-01 15:34:25 +03002490 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002491 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002492 return X86EMUL_CONTINUE;
2493
Avi Kivity9dac77f2011-06-01 15:34:25 +03002494 ctxt->_eip = 0;
2495 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002496
Avi Kivity9dac77f2011-06-01 15:34:25 +03002497 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002498 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002499 if (rc != X86EMUL_CONTINUE)
2500 return rc;
2501
Avi Kivity9dac77f2011-06-01 15:34:25 +03002502 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002503 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002504}
2505
Avi Kivity40ece7c2010-08-18 15:12:09 +03002506static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2507{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002508 int rc;
2509
Avi Kivity9dac77f2011-06-01 15:34:25 +03002510 ctxt->dst.type = OP_REG;
2511 ctxt->dst.addr.reg = &ctxt->_eip;
2512 ctxt->dst.bytes = ctxt->op_bytes;
2513 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002514 if (rc != X86EMUL_CONTINUE)
2515 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002516 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002517 return X86EMUL_CONTINUE;
2518}
2519
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002520static int em_add(struct x86_emulate_ctxt *ctxt)
2521{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002522 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002523 return X86EMUL_CONTINUE;
2524}
2525
2526static int em_or(struct x86_emulate_ctxt *ctxt)
2527{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002528 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002529 return X86EMUL_CONTINUE;
2530}
2531
2532static int em_adc(struct x86_emulate_ctxt *ctxt)
2533{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002534 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002535 return X86EMUL_CONTINUE;
2536}
2537
2538static int em_sbb(struct x86_emulate_ctxt *ctxt)
2539{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002540 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002541 return X86EMUL_CONTINUE;
2542}
2543
2544static int em_and(struct x86_emulate_ctxt *ctxt)
2545{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002546 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002547 return X86EMUL_CONTINUE;
2548}
2549
2550static int em_sub(struct x86_emulate_ctxt *ctxt)
2551{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002552 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002553 return X86EMUL_CONTINUE;
2554}
2555
2556static int em_xor(struct x86_emulate_ctxt *ctxt)
2557{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002558 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002559 return X86EMUL_CONTINUE;
2560}
2561
2562static int em_cmp(struct x86_emulate_ctxt *ctxt)
2563{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002564 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002565 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002566 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002567 return X86EMUL_CONTINUE;
2568}
2569
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002570static int em_test(struct x86_emulate_ctxt *ctxt)
2571{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002572 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002573 /* Disable writeback. */
2574 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002575 return X86EMUL_CONTINUE;
2576}
2577
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002578static int em_xchg(struct x86_emulate_ctxt *ctxt)
2579{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002580 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002581 ctxt->src.val = ctxt->dst.val;
2582 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002583
2584 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002585 ctxt->dst.val = ctxt->src.orig_val;
2586 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002587 return X86EMUL_CONTINUE;
2588}
2589
Avi Kivity5c82aa22010-08-18 18:31:43 +03002590static int em_imul(struct x86_emulate_ctxt *ctxt)
2591{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002592 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002593 return X86EMUL_CONTINUE;
2594}
2595
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002596static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2597{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002598 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002599 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002600}
2601
Avi Kivity61429142010-08-19 15:13:00 +03002602static int em_cwd(struct x86_emulate_ctxt *ctxt)
2603{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002604 ctxt->dst.type = OP_REG;
2605 ctxt->dst.bytes = ctxt->src.bytes;
2606 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2607 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002608
2609 return X86EMUL_CONTINUE;
2610}
2611
Avi Kivity48bb5d32010-08-18 18:54:34 +03002612static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2613{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002614 u64 tsc = 0;
2615
Avi Kivity717746e2011-04-20 13:37:53 +03002616 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002617 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2618 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002619 return X86EMUL_CONTINUE;
2620}
2621
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002622static int em_mov(struct x86_emulate_ctxt *ctxt)
2623{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002624 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002625 return X86EMUL_CONTINUE;
2626}
2627
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002628static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2629{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002630 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002631 return emulate_ud(ctxt);
2632
Avi Kivity9dac77f2011-06-01 15:34:25 +03002633 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002634 return X86EMUL_CONTINUE;
2635}
2636
2637static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2638{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002639 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002640
Avi Kivity9dac77f2011-06-01 15:34:25 +03002641 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002642 return emulate_ud(ctxt);
2643
Avi Kivity9dac77f2011-06-01 15:34:25 +03002644 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002645 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2646
2647 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002648 ctxt->dst.type = OP_NONE;
2649 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002650}
2651
Avi Kivityaa97bb42010-01-20 18:09:23 +02002652static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2653{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002654 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002655 return X86EMUL_CONTINUE;
2656}
2657
Avi Kivity38503912011-03-31 18:48:09 +02002658static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2659{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002660 int rc;
2661 ulong linear;
2662
Avi Kivity9dac77f2011-06-01 15:34:25 +03002663 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002664 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002665 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002666 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002667 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002668 return X86EMUL_CONTINUE;
2669}
2670
Avi Kivity2d04a052011-04-20 15:32:49 +03002671static int em_clts(struct x86_emulate_ctxt *ctxt)
2672{
2673 ulong cr0;
2674
2675 cr0 = ctxt->ops->get_cr(ctxt, 0);
2676 cr0 &= ~X86_CR0_TS;
2677 ctxt->ops->set_cr(ctxt, 0, cr0);
2678 return X86EMUL_CONTINUE;
2679}
2680
Avi Kivity26d05cc2011-04-21 12:07:59 +03002681static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2682{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002683 int rc;
2684
Avi Kivity9dac77f2011-06-01 15:34:25 +03002685 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002686 return X86EMUL_UNHANDLEABLE;
2687
2688 rc = ctxt->ops->fix_hypercall(ctxt);
2689 if (rc != X86EMUL_CONTINUE)
2690 return rc;
2691
2692 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002693 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002694 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002695 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002696 return X86EMUL_CONTINUE;
2697}
2698
2699static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2700{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002701 struct desc_ptr desc_ptr;
2702 int rc;
2703
Avi Kivity9dac77f2011-06-01 15:34:25 +03002704 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002705 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002706 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002707 if (rc != X86EMUL_CONTINUE)
2708 return rc;
2709 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2710 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002711 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002712 return X86EMUL_CONTINUE;
2713}
2714
Avi Kivity5ef39c72011-04-21 12:21:50 +03002715static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002716{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002717 int rc;
2718
Avi Kivity5ef39c72011-04-21 12:21:50 +03002719 rc = ctxt->ops->fix_hypercall(ctxt);
2720
Avi Kivity26d05cc2011-04-21 12:07:59 +03002721 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002722 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002723 return rc;
2724}
2725
2726static int em_lidt(struct x86_emulate_ctxt *ctxt)
2727{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002728 struct desc_ptr desc_ptr;
2729 int rc;
2730
Avi Kivity9dac77f2011-06-01 15:34:25 +03002731 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002732 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002733 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002734 if (rc != X86EMUL_CONTINUE)
2735 return rc;
2736 ctxt->ops->set_idt(ctxt, &desc_ptr);
2737 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002738 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002739 return X86EMUL_CONTINUE;
2740}
2741
2742static int em_smsw(struct x86_emulate_ctxt *ctxt)
2743{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002744 ctxt->dst.bytes = 2;
2745 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002746 return X86EMUL_CONTINUE;
2747}
2748
2749static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2750{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002751 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002752 | (ctxt->src.val & 0x0f));
2753 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002754 return X86EMUL_CONTINUE;
2755}
2756
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002757static int em_loop(struct x86_emulate_ctxt *ctxt)
2758{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002759 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2760 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2761 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2762 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002763
2764 return X86EMUL_CONTINUE;
2765}
2766
2767static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2768{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002769 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2770 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002771
2772 return X86EMUL_CONTINUE;
2773}
2774
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002775static int em_cli(struct x86_emulate_ctxt *ctxt)
2776{
2777 if (emulator_bad_iopl(ctxt))
2778 return emulate_gp(ctxt, 0);
2779
2780 ctxt->eflags &= ~X86_EFLAGS_IF;
2781 return X86EMUL_CONTINUE;
2782}
2783
2784static int em_sti(struct x86_emulate_ctxt *ctxt)
2785{
2786 if (emulator_bad_iopl(ctxt))
2787 return emulate_gp(ctxt, 0);
2788
2789 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2790 ctxt->eflags |= X86_EFLAGS_IF;
2791 return X86EMUL_CONTINUE;
2792}
2793
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002794static bool valid_cr(int nr)
2795{
2796 switch (nr) {
2797 case 0:
2798 case 2 ... 4:
2799 case 8:
2800 return true;
2801 default:
2802 return false;
2803 }
2804}
2805
2806static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2807{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002808 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002809 return emulate_ud(ctxt);
2810
2811 return X86EMUL_CONTINUE;
2812}
2813
2814static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2815{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002816 u64 new_val = ctxt->src.val64;
2817 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002818 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002819
2820 static u64 cr_reserved_bits[] = {
2821 0xffffffff00000000ULL,
2822 0, 0, 0, /* CR3 checked later */
2823 CR4_RESERVED_BITS,
2824 0, 0, 0,
2825 CR8_RESERVED_BITS,
2826 };
2827
2828 if (!valid_cr(cr))
2829 return emulate_ud(ctxt);
2830
2831 if (new_val & cr_reserved_bits[cr])
2832 return emulate_gp(ctxt, 0);
2833
2834 switch (cr) {
2835 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002836 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002837 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2838 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2839 return emulate_gp(ctxt, 0);
2840
Avi Kivity717746e2011-04-20 13:37:53 +03002841 cr4 = ctxt->ops->get_cr(ctxt, 4);
2842 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002843
2844 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2845 !(cr4 & X86_CR4_PAE))
2846 return emulate_gp(ctxt, 0);
2847
2848 break;
2849 }
2850 case 3: {
2851 u64 rsvd = 0;
2852
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002853 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2854 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002855 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002856 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002857 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002858 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002859 rsvd = CR3_NONPAE_RESERVED_BITS;
2860
2861 if (new_val & rsvd)
2862 return emulate_gp(ctxt, 0);
2863
2864 break;
2865 }
2866 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002867 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002868
Avi Kivity717746e2011-04-20 13:37:53 +03002869 cr4 = ctxt->ops->get_cr(ctxt, 4);
2870 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002871
2872 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2873 return emulate_gp(ctxt, 0);
2874
2875 break;
2876 }
2877 }
2878
2879 return X86EMUL_CONTINUE;
2880}
2881
Joerg Roedel3b88e412011-04-04 12:39:29 +02002882static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2883{
2884 unsigned long dr7;
2885
Avi Kivity717746e2011-04-20 13:37:53 +03002886 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002887
2888 /* Check if DR7.Global_Enable is set */
2889 return dr7 & (1 << 13);
2890}
2891
2892static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2893{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002894 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002895 u64 cr4;
2896
2897 if (dr > 7)
2898 return emulate_ud(ctxt);
2899
Avi Kivity717746e2011-04-20 13:37:53 +03002900 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002901 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2902 return emulate_ud(ctxt);
2903
2904 if (check_dr7_gd(ctxt))
2905 return emulate_db(ctxt);
2906
2907 return X86EMUL_CONTINUE;
2908}
2909
2910static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2911{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002912 u64 new_val = ctxt->src.val64;
2913 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002914
2915 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2916 return emulate_gp(ctxt, 0);
2917
2918 return check_dr_read(ctxt);
2919}
2920
Joerg Roedel01de8b02011-04-04 12:39:31 +02002921static int check_svme(struct x86_emulate_ctxt *ctxt)
2922{
2923 u64 efer;
2924
Avi Kivity717746e2011-04-20 13:37:53 +03002925 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002926
2927 if (!(efer & EFER_SVME))
2928 return emulate_ud(ctxt);
2929
2930 return X86EMUL_CONTINUE;
2931}
2932
2933static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2934{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002935 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002936
2937 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002938 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002939 return emulate_gp(ctxt, 0);
2940
2941 return check_svme(ctxt);
2942}
2943
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002944static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2945{
Avi Kivity717746e2011-04-20 13:37:53 +03002946 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002947
Avi Kivity717746e2011-04-20 13:37:53 +03002948 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002949 return emulate_ud(ctxt);
2950
2951 return X86EMUL_CONTINUE;
2952}
2953
Joerg Roedel80612522011-04-04 12:39:33 +02002954static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2955{
Avi Kivity717746e2011-04-20 13:37:53 +03002956 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002957 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002958
Avi Kivity717746e2011-04-20 13:37:53 +03002959 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002960 (rcx > 3))
2961 return emulate_gp(ctxt, 0);
2962
2963 return X86EMUL_CONTINUE;
2964}
2965
Joerg Roedelf6511932011-04-04 12:39:35 +02002966static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2967{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002968 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2969 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002970 return emulate_gp(ctxt, 0);
2971
2972 return X86EMUL_CONTINUE;
2973}
2974
2975static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2976{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002977 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2978 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002979 return emulate_gp(ctxt, 0);
2980
2981 return X86EMUL_CONTINUE;
2982}
2983
Avi Kivity73fba5f2010-07-29 15:11:53 +03002984#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002985#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002986#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2987 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002988#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002989#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002990#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002991#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002992#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002993#define II(_f, _e, _i) \
2994 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002995#define IIP(_f, _e, _i, _p) \
2996 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2997 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002998#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002999
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003000#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003001#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003002#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
3003
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003004#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3005 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3006 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003007
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003008static struct opcode group7_rm1[] = {
3009 DI(SrcNone | ModRM | Priv, monitor),
3010 DI(SrcNone | ModRM | Priv, mwait),
3011 N, N, N, N, N, N,
3012};
3013
Joerg Roedel01de8b02011-04-04 12:39:31 +02003014static struct opcode group7_rm3[] = {
3015 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003016 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003017 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3018 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3019 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3020 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3021 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3022 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3023};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003024
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003025static struct opcode group7_rm7[] = {
3026 N,
3027 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3028 N, N, N, N, N, N,
3029};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003030
Avi Kivity73fba5f2010-07-29 15:11:53 +03003031static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003032 I(Lock, em_add),
3033 I(Lock, em_or),
3034 I(Lock, em_adc),
3035 I(Lock, em_sbb),
3036 I(Lock, em_and),
3037 I(Lock, em_sub),
3038 I(Lock, em_xor),
3039 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003040};
3041
3042static struct opcode group1A[] = {
3043 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3044};
3045
3046static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003047 I(DstMem | SrcImm | ModRM, em_test),
3048 I(DstMem | SrcImm | ModRM, em_test),
3049 I(DstMem | SrcNone | ModRM | Lock, em_not),
3050 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3051 I(SrcMem | ModRM, em_mul_ex),
3052 I(SrcMem | ModRM, em_imul_ex),
3053 I(SrcMem | ModRM, em_div_ex),
3054 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003055};
3056
3057static struct opcode group4[] = {
3058 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3059 N, N, N, N, N, N,
3060};
3061
3062static struct opcode group5[] = {
3063 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003064 D(SrcMem | ModRM | Stack),
3065 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003066 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3067 D(SrcMem | ModRM | Stack), N,
3068};
3069
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003070static struct opcode group6[] = {
3071 DI(ModRM | Prot, sldt),
3072 DI(ModRM | Prot, str),
3073 DI(ModRM | Prot | Priv, lldt),
3074 DI(ModRM | Prot | Priv, ltr),
3075 N, N, N, N,
3076};
3077
Avi Kivity73fba5f2010-07-29 15:11:53 +03003078static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003079 DI(ModRM | Mov | DstMem | Priv, sgdt),
3080 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003081 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3082 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3083 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3084 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3085 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003086}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003087 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3088 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003089 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003090 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3091 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003092} };
3093
3094static struct opcode group8[] = {
3095 N, N, N, N,
3096 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3097 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3098};
3099
3100static struct group_dual group9 = { {
3101 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3102}, {
3103 N, N, N, N, N, N, N, N,
3104} };
3105
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003106static struct opcode group11[] = {
3107 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3108};
3109
Avi Kivityaa97bb42010-01-20 18:09:23 +02003110static struct gprefix pfx_0f_6f_0f_7f = {
3111 N, N, N, I(Sse, em_movdqu),
3112};
3113
Avi Kivity73fba5f2010-07-29 15:11:53 +03003114static struct opcode opcode_table[256] = {
3115 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003116 I6ALU(Lock, em_add),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003117 D(ImplicitOps | Stack | No64 | Src2ES),
3118 D(ImplicitOps | Stack | No64 | Src2ES),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003119 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003120 I6ALU(Lock, em_or),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003121 D(ImplicitOps | Stack | No64 | Src2CS), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003122 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003123 I6ALU(Lock, em_adc),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003124 D(ImplicitOps | Stack | No64 | Src2SS),
3125 D(ImplicitOps | Stack | No64 | Src2SS),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003126 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003127 I6ALU(Lock, em_sbb),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003128 D(ImplicitOps | Stack | No64 | Src2DS),
3129 D(ImplicitOps | Stack | No64 | Src2DS),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003130 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003131 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003132 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003133 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003134 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003135 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003136 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003137 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003138 /* 0x40 - 0x4F */
3139 X16(D(DstReg)),
3140 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003141 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003142 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003143 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003144 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003145 I(ImplicitOps | Stack | No64, em_pusha),
3146 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003147 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3148 N, N, N, N,
3149 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003150 I(SrcImm | Mov | Stack, em_push),
3151 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003152 I(SrcImmByte | Mov | Stack, em_push),
3153 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003154 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3155 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003156 /* 0x70 - 0x7F */
3157 X16(D(SrcImmByte)),
3158 /* 0x80 - 0x87 */
3159 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3160 G(DstMem | SrcImm | ModRM | Group, group1),
3161 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3162 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003163 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003164 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003165 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003166 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3167 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003168 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3169 D(ModRM | SrcMem | NoAccess | DstReg),
3170 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3171 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003172 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003173 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003174 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003175 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003176 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003177 II(ImplicitOps | Stack, em_pushf, pushf),
3178 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003179 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003180 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3181 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3182 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003183 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003184 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003185 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003186 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3187 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003188 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003189 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003190 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003191 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003192 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003193 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003194 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003195 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003196 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003197 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3198 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003199 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003200 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003201 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003202 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003203 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003204 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003205 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003206 N, N, N, N,
3207 /* 0xD8 - 0xDF */
3208 N, N, N, N, N, N, N, N,
3209 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003210 X3(I(SrcImmByte, em_loop)),
3211 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003212 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3213 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003214 /* 0xE8 - 0xEF */
3215 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003216 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003217 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3218 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003219 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003220 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003221 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3222 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003223 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003224 D(ImplicitOps), D(ImplicitOps),
3225 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003226 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3227};
3228
3229static struct opcode twobyte_table[256] = {
3230 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003231 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003232 N, I(ImplicitOps | VendorSpecific, em_syscall),
3233 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003234 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003235 N, D(ImplicitOps | ModRM), N, N,
3236 /* 0x10 - 0x1F */
3237 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3238 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003239 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003240 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003241 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003242 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003243 N, N, N, N,
3244 N, N, N, N, N, N, N, N,
3245 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003246 DI(ImplicitOps | Priv, wrmsr),
3247 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3248 DI(ImplicitOps | Priv, rdmsr),
3249 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003250 I(ImplicitOps | VendorSpecific, em_sysenter),
3251 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003252 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003253 N, N, N, N, N, N, N, N,
3254 /* 0x40 - 0x4F */
3255 X16(D(DstReg | SrcMem | ModRM | Mov)),
3256 /* 0x50 - 0x5F */
3257 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3258 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003259 N, N, N, N,
3260 N, N, N, N,
3261 N, N, N, N,
3262 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003263 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003264 N, N, N, N,
3265 N, N, N, N,
3266 N, N, N, N,
3267 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003268 /* 0x80 - 0x8F */
3269 X16(D(SrcImm)),
3270 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003271 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003272 /* 0xA0 - 0xA7 */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003273 D(Stack | Src2FS), D(Stack | Src2FS),
Joerg Roedel80612522011-04-04 12:39:33 +02003274 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003275 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3276 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3277 /* 0xA8 - 0xAF */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003278 D(Stack | Src2GS), D(Stack | Src2GS),
Joerg Roedel80612522011-04-04 12:39:33 +02003279 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003280 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3281 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003282 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003283 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003284 D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivityd4b43252011-09-13 10:45:50 +03003285 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003286 D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivityd4b43252011-09-13 10:45:50 +03003287 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3288 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003289 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003290 /* 0xB8 - 0xBF */
3291 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003292 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003293 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3294 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003295 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003296 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003297 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003298 N, N, N, GD(0, &group9),
3299 N, N, N, N, N, N, N, N,
3300 /* 0xD0 - 0xDF */
3301 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3302 /* 0xE0 - 0xEF */
3303 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3304 /* 0xF0 - 0xFF */
3305 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3306};
3307
3308#undef D
3309#undef N
3310#undef G
3311#undef GD
3312#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003313#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003314#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003315
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003316#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003317#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003318#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003319#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003320
Avi Kivity9dac77f2011-06-01 15:34:25 +03003321static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003322{
3323 unsigned size;
3324
Avi Kivity9dac77f2011-06-01 15:34:25 +03003325 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003326 if (size == 8)
3327 size = 4;
3328 return size;
3329}
3330
3331static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3332 unsigned size, bool sign_extension)
3333{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003334 int rc = X86EMUL_CONTINUE;
3335
3336 op->type = OP_IMM;
3337 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003338 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003339 /* NB. Immediates are sign-extended as necessary. */
3340 switch (op->bytes) {
3341 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003342 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003343 break;
3344 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003345 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003346 break;
3347 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003348 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003349 break;
3350 }
3351 if (!sign_extension) {
3352 switch (op->bytes) {
3353 case 1:
3354 op->val &= 0xff;
3355 break;
3356 case 2:
3357 op->val &= 0xffff;
3358 break;
3359 case 4:
3360 op->val &= 0xffffffff;
3361 break;
3362 }
3363 }
3364done:
3365 return rc;
3366}
3367
Avi Kivitya9945542011-09-13 10:45:41 +03003368static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3369 unsigned d)
3370{
3371 int rc = X86EMUL_CONTINUE;
3372
3373 switch (d) {
3374 case OpReg:
3375 decode_register_operand(ctxt, op,
Avi Kivity52179732011-09-13 10:45:46 +03003376 op == &ctxt->dst &&
Avi Kivitya9945542011-09-13 10:45:41 +03003377 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
3378 break;
3379 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003380 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003381 break;
3382 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003383 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003384 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003385 *op = ctxt->memop;
3386 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003387 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003388 fetch_bit_operand(ctxt);
3389 op->orig_val = op->val;
3390 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003391 case OpMem64:
3392 ctxt->memop.bytes = 8;
3393 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003394 case OpAcc:
3395 op->type = OP_REG;
3396 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3397 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3398 fetch_register_operand(op);
3399 op->orig_val = op->val;
3400 break;
3401 case OpDI:
3402 op->type = OP_MEM;
3403 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3404 op->addr.mem.ea =
3405 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3406 op->addr.mem.seg = VCPU_SREG_ES;
3407 op->val = 0;
3408 break;
3409 case OpDX:
3410 op->type = OP_REG;
3411 op->bytes = 2;
3412 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3413 fetch_register_operand(op);
3414 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003415 case OpCL:
3416 op->bytes = 1;
3417 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3418 break;
3419 case OpImmByte:
3420 rc = decode_imm(ctxt, op, 1, true);
3421 break;
3422 case OpOne:
3423 op->bytes = 1;
3424 op->val = 1;
3425 break;
3426 case OpImm:
3427 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3428 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03003429 case OpMem16:
3430 ctxt->memop.bytes = 2;
3431 goto mem_common;
3432 case OpMem32:
3433 ctxt->memop.bytes = 4;
3434 goto mem_common;
3435 case OpImmU16:
3436 rc = decode_imm(ctxt, op, 2, false);
3437 break;
3438 case OpImmU:
3439 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3440 break;
3441 case OpSI:
3442 op->type = OP_MEM;
3443 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3444 op->addr.mem.ea =
3445 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3446 op->addr.mem.seg = seg_override(ctxt);
3447 op->val = 0;
3448 break;
3449 case OpImmFAddr:
3450 op->type = OP_IMM;
3451 op->addr.mem.ea = ctxt->_eip;
3452 op->bytes = ctxt->op_bytes + 2;
3453 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3454 break;
3455 case OpMemFAddr:
3456 ctxt->memop.bytes = ctxt->op_bytes + 2;
3457 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003458 case OpES:
3459 op->val = VCPU_SREG_ES;
3460 break;
3461 case OpCS:
3462 op->val = VCPU_SREG_CS;
3463 break;
3464 case OpSS:
3465 op->val = VCPU_SREG_SS;
3466 break;
3467 case OpDS:
3468 op->val = VCPU_SREG_DS;
3469 break;
3470 case OpFS:
3471 op->val = VCPU_SREG_FS;
3472 break;
3473 case OpGS:
3474 op->val = VCPU_SREG_GS;
3475 break;
Avi Kivitya9945542011-09-13 10:45:41 +03003476 case OpImplicit:
3477 /* Special instructions do their own operand decoding. */
3478 default:
3479 op->type = OP_NONE; /* Disable writeback. */
3480 break;
3481 }
3482
3483done:
3484 return rc;
3485}
3486
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003487int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003488{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003489 int rc = X86EMUL_CONTINUE;
3490 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003491 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003492 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003493 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003494
Avi Kivityf09ed832011-09-13 10:45:40 +03003495 ctxt->memop.type = OP_NONE;
3496 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003497 ctxt->_eip = ctxt->eip;
3498 ctxt->fetch.start = ctxt->_eip;
3499 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003500 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003501 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003502
3503 switch (mode) {
3504 case X86EMUL_MODE_REAL:
3505 case X86EMUL_MODE_VM86:
3506 case X86EMUL_MODE_PROT16:
3507 def_op_bytes = def_ad_bytes = 2;
3508 break;
3509 case X86EMUL_MODE_PROT32:
3510 def_op_bytes = def_ad_bytes = 4;
3511 break;
3512#ifdef CONFIG_X86_64
3513 case X86EMUL_MODE_PROT64:
3514 def_op_bytes = 4;
3515 def_ad_bytes = 8;
3516 break;
3517#endif
3518 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003519 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003520 }
3521
Avi Kivity9dac77f2011-06-01 15:34:25 +03003522 ctxt->op_bytes = def_op_bytes;
3523 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003524
3525 /* Legacy prefixes. */
3526 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003527 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003528 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003529 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003530 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003531 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003532 break;
3533 case 0x67: /* address-size override */
3534 if (mode == X86EMUL_MODE_PROT64)
3535 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003536 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003537 else
3538 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003539 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003540 break;
3541 case 0x26: /* ES override */
3542 case 0x2e: /* CS override */
3543 case 0x36: /* SS override */
3544 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003545 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003546 break;
3547 case 0x64: /* FS override */
3548 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003549 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003550 break;
3551 case 0x40 ... 0x4f: /* REX */
3552 if (mode != X86EMUL_MODE_PROT64)
3553 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003554 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003555 continue;
3556 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003557 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003558 break;
3559 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003560 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003561 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003562 break;
3563 default:
3564 goto done_prefixes;
3565 }
3566
3567 /* Any legacy prefix after a REX prefix nullifies its effect. */
3568
Avi Kivity9dac77f2011-06-01 15:34:25 +03003569 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003570 }
3571
3572done_prefixes:
3573
3574 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003575 if (ctxt->rex_prefix & 8)
3576 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003577
3578 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003579 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003580 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003581 if (ctxt->b == 0x0f) {
3582 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003583 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003584 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003585 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003586 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003587
Avi Kivity9dac77f2011-06-01 15:34:25 +03003588 while (ctxt->d & GroupMask) {
3589 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003590 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003591 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003592 --ctxt->_eip;
3593 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003594 opcode = opcode.u.group[goffset];
3595 break;
3596 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003597 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003598 --ctxt->_eip;
3599 goffset = (ctxt->modrm >> 3) & 7;
3600 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003601 opcode = opcode.u.gdual->mod3[goffset];
3602 else
3603 opcode = opcode.u.gdual->mod012[goffset];
3604 break;
3605 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003606 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003607 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003608 break;
3609 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003610 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003611 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003612 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003613 switch (simd_prefix) {
3614 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3615 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3616 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3617 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3618 }
3619 break;
3620 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003621 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003622 }
Avi Kivity46561642011-04-24 14:09:59 +03003623
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003624 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003625 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003626 }
3627
Avi Kivity9dac77f2011-06-01 15:34:25 +03003628 ctxt->execute = opcode.u.execute;
3629 ctxt->check_perm = opcode.check_perm;
3630 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003631
3632 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003633 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003634 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003635
Avi Kivity9dac77f2011-06-01 15:34:25 +03003636 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003637 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003638
Avi Kivity9dac77f2011-06-01 15:34:25 +03003639 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3640 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003641
Avi Kivity9dac77f2011-06-01 15:34:25 +03003642 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003643 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003644 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003645 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003646 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003647 }
3648
Avi Kivity9dac77f2011-06-01 15:34:25 +03003649 if (ctxt->d & Sse)
3650 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003651
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003652 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003653 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003654 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003655 if (!ctxt->has_seg_override)
3656 set_seg_override(ctxt, ctxt->modrm_seg);
3657 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003658 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003659 if (rc != X86EMUL_CONTINUE)
3660 goto done;
3661
Avi Kivity9dac77f2011-06-01 15:34:25 +03003662 if (!ctxt->has_seg_override)
3663 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003664
Avi Kivityf09ed832011-09-13 10:45:40 +03003665 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003666
Avi Kivityf09ed832011-09-13 10:45:40 +03003667 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
3668 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003669
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003670 /*
3671 * Decode and fetch the source operand: register, memory
3672 * or immediate.
3673 */
Avi Kivity0fe59122011-09-13 10:45:47 +03003674 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003675 if (rc != X86EMUL_CONTINUE)
3676 goto done;
3677
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003678 /*
3679 * Decode and fetch the second source operand: register, memory
3680 * or immediate.
3681 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03003682 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003683 if (rc != X86EMUL_CONTINUE)
3684 goto done;
3685
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003686 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03003687 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003688
3689done:
Avi Kivityf09ed832011-09-13 10:45:40 +03003690 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
3691 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003692
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003693 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003694}
3695
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003696static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3697{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003698 /* The second termination condition only applies for REPE
3699 * and REPNE. Test if the repeat string operation prefix is
3700 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3701 * corresponding termination condition according to:
3702 * - if REPE/REPZ and ZF = 0 then done
3703 * - if REPNE/REPNZ and ZF = 1 then done
3704 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003705 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3706 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3707 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003708 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003709 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003710 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3711 return true;
3712
3713 return false;
3714}
3715
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003716int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003717{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003718 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003719 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003720 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003721 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003722
Avi Kivity9dac77f2011-06-01 15:34:25 +03003723 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003724
Avi Kivity9dac77f2011-06-01 15:34:25 +03003725 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003726 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003727 goto done;
3728 }
3729
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003730 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003731 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003732 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003733 goto done;
3734 }
3735
Avi Kivity9dac77f2011-06-01 15:34:25 +03003736 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003737 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003738 goto done;
3739 }
3740
Avi Kivity9dac77f2011-06-01 15:34:25 +03003741 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003742 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3743 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003744 rc = emulate_ud(ctxt);
3745 goto done;
3746 }
3747
Avi Kivity9dac77f2011-06-01 15:34:25 +03003748 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003749 rc = emulate_nm(ctxt);
3750 goto done;
3751 }
3752
Avi Kivity9dac77f2011-06-01 15:34:25 +03003753 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3754 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003755 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003756 if (rc != X86EMUL_CONTINUE)
3757 goto done;
3758 }
3759
Gleb Natapove92805a2010-02-10 14:21:35 +02003760 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003761 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003762 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003763 goto done;
3764 }
3765
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003766 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003767 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003768 rc = emulate_ud(ctxt);
3769 goto done;
3770 }
3771
Joerg Roedeld09beab2011-04-04 12:39:25 +02003772 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003773 if (ctxt->check_perm) {
3774 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003775 if (rc != X86EMUL_CONTINUE)
3776 goto done;
3777 }
3778
Avi Kivity9dac77f2011-06-01 15:34:25 +03003779 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3780 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003781 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003782 if (rc != X86EMUL_CONTINUE)
3783 goto done;
3784 }
3785
Avi Kivity9dac77f2011-06-01 15:34:25 +03003786 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003787 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003788 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3789 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003790 goto done;
3791 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003792 }
3793
Avi Kivity9dac77f2011-06-01 15:34:25 +03003794 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3795 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3796 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003797 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003798 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003799 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003800 }
3801
Avi Kivity9dac77f2011-06-01 15:34:25 +03003802 if (ctxt->src2.type == OP_MEM) {
3803 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3804 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003805 if (rc != X86EMUL_CONTINUE)
3806 goto done;
3807 }
3808
Avi Kivity9dac77f2011-06-01 15:34:25 +03003809 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003810 goto special_insn;
3811
3812
Avi Kivity9dac77f2011-06-01 15:34:25 +03003813 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003814 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003815 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3816 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003817 if (rc != X86EMUL_CONTINUE)
3818 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003819 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003820 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003821
Avi Kivity018a98d2007-11-27 19:30:56 +02003822special_insn:
3823
Avi Kivity9dac77f2011-06-01 15:34:25 +03003824 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3825 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003826 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003827 if (rc != X86EMUL_CONTINUE)
3828 goto done;
3829 }
3830
Avi Kivity9dac77f2011-06-01 15:34:25 +03003831 if (ctxt->execute) {
3832 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003833 if (rc != X86EMUL_CONTINUE)
3834 goto done;
3835 goto writeback;
3836 }
3837
Avi Kivity9dac77f2011-06-01 15:34:25 +03003838 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839 goto twobyte_insn;
3840
Avi Kivity9dac77f2011-06-01 15:34:25 +03003841 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003842 case 0x06: /* push es */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003843 case 0x0e: /* push cs */
3844 case 0x16: /* push ss */
3845 case 0x1e: /* push ds */
3846 rc = emulate_push_sreg(ctxt, ctxt->src2.val);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003847 break;
3848 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003849 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003850 case 0x1f: /* pop ds */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003851 rc = emulate_pop_sreg(ctxt, ctxt->src2.val);
Avi Kivity33615aa2007-10-31 11:15:56 +02003852 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003853 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02003854 break;
3855 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003856 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02003857 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003859 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003861 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003863 case 0x6c: /* insb */
3864 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003865 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003866 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003867 case 0x6e: /* outsb */
3868 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003869 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003870 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003871 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003872 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003873 if (test_cc(ctxt->b, ctxt->eflags))
3874 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003875 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003876 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003877 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003878 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003880 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003882 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003883 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003884 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003885 rc = em_xchg(ctxt);
3886 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003887 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003888 switch (ctxt->op_bytes) {
3889 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
3890 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
3891 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003892 }
3893 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003894 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003895 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003896 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003897 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003898 rc = emulate_int(ctxt, 3);
3899 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003900 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003901 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003902 break;
3903 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003904 if (ctxt->eflags & EFLG_OF)
3905 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003906 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003907 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003908 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003909 break;
3910 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003911 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003912 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003913 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003914 case 0xe4: /* inb */
3915 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003916 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003917 case 0xe6: /* outb */
3918 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003919 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003920 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003921 long int rel = ctxt->src.val;
3922 ctxt->src.val = (unsigned long) ctxt->_eip;
3923 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003924 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003925 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003926 }
3927 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003928 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003929 jmp_rel(ctxt, ctxt->src.val);
3930 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003931 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003932 case 0xec: /* in al,dx */
3933 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003934 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003935 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3936 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003937 goto done; /* IO is needed */
3938 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003939 case 0xee: /* out dx,al */
3940 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003941 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003942 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3943 &ctxt->src.val, 1);
3944 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003945 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003946 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003947 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003948 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003949 case 0xf5: /* cmc */
3950 /* complement carry flag from eflags reg */
3951 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003952 break;
3953 case 0xf8: /* clc */
3954 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003955 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003956 case 0xf9: /* stc */
3957 ctxt->eflags |= EFLG_CF;
3958 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003959 case 0xfc: /* cld */
3960 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003961 break;
3962 case 0xfd: /* std */
3963 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003964 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003965 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003966 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003967 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003968 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003969 rc = em_grp45(ctxt);
3970 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003971 default:
3972 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003973 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003974
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003975 if (rc != X86EMUL_CONTINUE)
3976 goto done;
3977
Avi Kivity018a98d2007-11-27 19:30:56 +02003978writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003979 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003980 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003981 goto done;
3982
Gleb Natapov5cd21912010-03-18 15:20:26 +02003983 /*
3984 * restore dst type in case the decoding will be reused
3985 * (happens for string instruction )
3986 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003987 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003988
Avi Kivity9dac77f2011-06-01 15:34:25 +03003989 if ((ctxt->d & SrcMask) == SrcSI)
3990 string_addr_inc(ctxt, seg_override(ctxt),
3991 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003992
Avi Kivity9dac77f2011-06-01 15:34:25 +03003993 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003994 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003995 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003996
Avi Kivity9dac77f2011-06-01 15:34:25 +03003997 if (ctxt->rep_prefix && (ctxt->d & String)) {
3998 struct read_cache *r = &ctxt->io_read;
3999 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004000
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004001 if (!string_insn_completed(ctxt)) {
4002 /*
4003 * Re-enter guest when pio read ahead buffer is empty
4004 * or, if it is not used, after each 1024 iteration.
4005 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004006 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004007 (r->end == 0 || r->end != r->pos)) {
4008 /*
4009 * Reset read cache. Usually happens before
4010 * decode, but since instruction is restarted
4011 * we have to do it here.
4012 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004013 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004014 return EMULATION_RESTART;
4015 }
4016 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004017 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004018 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004019
Avi Kivity9dac77f2011-06-01 15:34:25 +03004020 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004021
4022done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004023 if (rc == X86EMUL_PROPAGATE_FAULT)
4024 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004025 if (rc == X86EMUL_INTERCEPTED)
4026 return EMULATION_INTERCEPTED;
4027
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004028 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004029
4030twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004031 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004032 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004033 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004034 break;
4035 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004036 case 0x0d: /* GrpP (prefetch) */
4037 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004038 break;
4039 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004040 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004041 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004043 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004045 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004046 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004047 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004048 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004049 goto done;
4050 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004051 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004052 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004054 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004055 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004056 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004057 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004058 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004059 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004060 goto done;
4061 }
4062
Avi Kivity9dac77f2011-06-01 15:34:25 +03004063 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004065 case 0x30:
4066 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004067 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4068 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4069 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004070 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004071 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004072 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004073 }
4074 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004075 break;
4076 case 0x32:
4077 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004078 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004079 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004080 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004081 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004082 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004083 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4084 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004085 }
4086 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004087 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004089 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4090 if (!test_cc(ctxt->b, ctxt->eflags))
4091 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004093 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004094 if (test_cc(ctxt->b, ctxt->eflags))
4095 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004096 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004097 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004098 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004099 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004100 case 0xa0: /* push fs */
Avi Kivityc191a7a2011-09-13 10:45:49 +03004101 case 0xa8: /* push gs */
4102 rc = emulate_push_sreg(ctxt, ctxt->src2.val);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004103 break;
4104 case 0xa1: /* pop fs */
Avi Kivityc191a7a2011-09-13 10:45:49 +03004105 case 0xa9: /* pop gs */
4106 rc = emulate_pop_sreg(ctxt, ctxt->src2.val);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004107 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004108 case 0xa3:
4109 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004110 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004111 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004112 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004113 emulate_2op_SrcV_nobyte(ctxt, "bt");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004114 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004115 case 0xa4: /* shld imm8, r, r/m */
4116 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004117 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004118 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004119 case 0xab:
4120 bts: /* bts */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004121 emulate_2op_SrcV_nobyte(ctxt, "bts");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004122 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004123 case 0xac: /* shrd imm8, r, r/m */
4124 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004125 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004126 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004127 case 0xae: /* clflush */
4128 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129 case 0xb0 ... 0xb1: /* cmpxchg */
4130 /*
4131 * Save real source value, then compare EAX against
4132 * destination.
4133 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004134 ctxt->src.orig_val = ctxt->src.val;
4135 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004136 emulate_2op_SrcV(ctxt, "cmp");
Laurent Vivier05f086f2007-09-24 11:10:55 +02004137 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004139 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140 } else {
4141 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004142 ctxt->dst.type = OP_REG;
4143 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 }
4145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 case 0xb3:
4147 btr: /* btr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004148 emulate_2op_SrcV_nobyte(ctxt, "btr");
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004151 ctxt->dst.bytes = ctxt->op_bytes;
4152 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4153 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004156 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 case 0:
4158 goto bt;
4159 case 1:
4160 goto bts;
4161 case 2:
4162 goto btr;
4163 case 3:
4164 goto btc;
4165 }
4166 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004167 case 0xbb:
4168 btc: /* btc */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004169 emulate_2op_SrcV_nobyte(ctxt, "btc");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004170 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004171 case 0xbc: { /* bsf */
4172 u8 zf;
4173 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004174 : "=r"(ctxt->dst.val), "=q"(zf)
4175 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004176 ctxt->eflags &= ~X86_EFLAGS_ZF;
4177 if (zf) {
4178 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004179 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004180 }
4181 break;
4182 }
4183 case 0xbd: { /* bsr */
4184 u8 zf;
4185 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004186 : "=r"(ctxt->dst.val), "=q"(zf)
4187 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004188 ctxt->eflags &= ~X86_EFLAGS_ZF;
4189 if (zf) {
4190 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004191 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004192 }
4193 break;
4194 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004196 ctxt->dst.bytes = ctxt->op_bytes;
4197 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4198 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004199 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004200 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004201 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004202 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004203 ctxt->src.val = ctxt->dst.orig_val;
4204 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004205 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004206 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004207 ctxt->dst.bytes = ctxt->op_bytes;
4208 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4209 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004210 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004211 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004212 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004213 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004214 default:
4215 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004217
4218 if (rc != X86EMUL_CONTINUE)
4219 goto done;
4220
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221 goto writeback;
4222
4223cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004224 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004225}