blob: ab48611b79cf8ce222ade49d55dbca752019ecde [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 Kivityab85b122010-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
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001831static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001832{
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001833 unsigned short sel;
1834 int rc;
1835
Avi Kivity9dac77f2011-06-01 15:34:25 +03001836 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001837
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001838 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001839 if (rc != X86EMUL_CONTINUE)
1840 return rc;
1841
Avi Kivity9dac77f2011-06-01 15:34:25 +03001842 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001843 return rc;
1844}
1845
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001846static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001847setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001848 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001849{
Avi Kivity1aa36612011-04-27 13:20:30 +03001850 u16 selector;
1851
Gleb Natapov79168fd2010-04-28 19:15:30 +03001852 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001853 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001854 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001855
1856 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001857 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001858 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001859 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001860 cs->type = 0x0b; /* Read, Execute, Accessed */
1861 cs->s = 1;
1862 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001863 cs->p = 1;
1864 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001865
Gleb Natapov79168fd2010-04-28 19:15:30 +03001866 set_desc_base(ss, 0); /* flat segment */
1867 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868 ss->g = 1; /* 4kb granularity */
1869 ss->s = 1;
1870 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001871 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001872 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001873 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001874}
1875
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001876static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001878 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001879 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001880 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001881 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001882 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001883
1884 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001885 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001886 ctxt->mode == X86EMUL_MODE_VM86)
1887 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001888
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001889 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001890 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891
Avi Kivity717746e2011-04-20 13:37:53 +03001892 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001893 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001894 cs_sel = (u16)(msr_data & 0xfffc);
1895 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001896
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001897 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001898 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001899 cs.l = 1;
1900 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001901 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1902 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001903
Avi Kivity9dac77f2011-06-01 15:34:25 +03001904 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001905 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001906#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001907 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001908
Avi Kivity717746e2011-04-20 13:37:53 +03001909 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001910 ctxt->mode == X86EMUL_MODE_PROT64 ?
1911 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001912 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001913
Avi Kivity717746e2011-04-20 13:37:53 +03001914 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001915 ctxt->eflags &= ~(msr_data | EFLG_RF);
1916#endif
1917 } else {
1918 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001919 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001920 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001921
1922 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1923 }
1924
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001925 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001926}
1927
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001928static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001929{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001930 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001931 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001932 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001933 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001934 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001935
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001936 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001937 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001938 if (ctxt->mode == X86EMUL_MODE_REAL)
1939 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001940
1941 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1942 * Therefore, we inject an #UD.
1943 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001944 if (ctxt->mode == X86EMUL_MODE_PROT64)
1945 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001946
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001947 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001948
Avi Kivity717746e2011-04-20 13:37:53 +03001949 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001950 switch (ctxt->mode) {
1951 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001952 if ((msr_data & 0xfffc) == 0x0)
1953 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001954 break;
1955 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001956 if (msr_data == 0x0)
1957 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001958 break;
1959 }
1960
1961 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001962 cs_sel = (u16)msr_data;
1963 cs_sel &= ~SELECTOR_RPL_MASK;
1964 ss_sel = cs_sel + 8;
1965 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001966 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001967 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001968 cs.l = 1;
1969 }
1970
Avi Kivity1aa36612011-04-27 13:20:30 +03001971 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1972 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001973
Avi Kivity717746e2011-04-20 13:37:53 +03001974 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001975 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001976
Avi Kivity717746e2011-04-20 13:37:53 +03001977 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001979
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001980 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001981}
1982
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001983static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001984{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001985 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001986 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001987 u64 msr_data;
1988 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001989 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001990
Gleb Natapova0044752010-02-10 14:21:31 +02001991 /* inject #GP if in real mode or Virtual 8086 mode */
1992 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001993 ctxt->mode == X86EMUL_MODE_VM86)
1994 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001995
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001996 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001997
Avi Kivity9dac77f2011-06-01 15:34:25 +03001998 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02001999 usermode = X86EMUL_MODE_PROT64;
2000 else
2001 usermode = X86EMUL_MODE_PROT32;
2002
2003 cs.dpl = 3;
2004 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002005 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002006 switch (usermode) {
2007 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002008 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002009 if ((msr_data & 0xfffc) == 0x0)
2010 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002011 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002012 break;
2013 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002014 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002015 if (msr_data == 0x0)
2016 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002017 ss_sel = cs_sel + 8;
2018 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002019 cs.l = 1;
2020 break;
2021 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002022 cs_sel |= SELECTOR_RPL_MASK;
2023 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002024
Avi Kivity1aa36612011-04-27 13:20:30 +03002025 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2026 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002027
Avi Kivity9dac77f2011-06-01 15:34:25 +03002028 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2029 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002030
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002031 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002032}
2033
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002034static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002035{
2036 int iopl;
2037 if (ctxt->mode == X86EMUL_MODE_REAL)
2038 return false;
2039 if (ctxt->mode == X86EMUL_MODE_VM86)
2040 return true;
2041 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002042 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002043}
2044
2045static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002046 u16 port, u16 len)
2047{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002048 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002049 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002050 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002051 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002052 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002053 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002054 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002055
Avi Kivity1aa36612011-04-27 13:20:30 +03002056 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002057 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002058 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002059 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002060 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002061 base = get_desc_base(&tr_seg);
2062#ifdef CONFIG_X86_64
2063 base |= ((u64)base3) << 32;
2064#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002065 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002066 if (r != X86EMUL_CONTINUE)
2067 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002068 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002069 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002070 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002071 if (r != X86EMUL_CONTINUE)
2072 return false;
2073 if ((perm >> bit_idx) & mask)
2074 return false;
2075 return true;
2076}
2077
2078static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002079 u16 port, u16 len)
2080{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002081 if (ctxt->perm_ok)
2082 return true;
2083
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002084 if (emulator_bad_iopl(ctxt))
2085 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002086 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002087
2088 ctxt->perm_ok = true;
2089
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002090 return true;
2091}
2092
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002093static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002094 struct tss_segment_16 *tss)
2095{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002096 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002097 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002098 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2099 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2100 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2101 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2102 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2103 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2104 tss->si = ctxt->regs[VCPU_REGS_RSI];
2105 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002106
Avi Kivity1aa36612011-04-27 13:20:30 +03002107 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2108 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2109 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2110 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2111 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002112}
2113
2114static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002115 struct tss_segment_16 *tss)
2116{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002117 int ret;
2118
Avi Kivity9dac77f2011-06-01 15:34:25 +03002119 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002120 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002121 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2122 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2123 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2124 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2125 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2126 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2127 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2128 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002129
2130 /*
2131 * SDM says that segment selectors are loaded before segment
2132 * descriptors
2133 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002134 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2135 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2136 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2137 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2138 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002139
2140 /*
2141 * Now load segment descriptors. If fault happenes at this stage
2142 * it is handled in a context of new task
2143 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002144 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002145 if (ret != X86EMUL_CONTINUE)
2146 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002147 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148 if (ret != X86EMUL_CONTINUE)
2149 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002150 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002151 if (ret != X86EMUL_CONTINUE)
2152 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002153 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154 if (ret != X86EMUL_CONTINUE)
2155 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002156 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002157 if (ret != X86EMUL_CONTINUE)
2158 return ret;
2159
2160 return X86EMUL_CONTINUE;
2161}
2162
2163static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002164 u16 tss_selector, u16 old_tss_sel,
2165 ulong old_tss_base, struct desc_struct *new_desc)
2166{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002167 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168 struct tss_segment_16 tss_seg;
2169 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002170 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002171
Avi Kivity0f65dd72011-04-20 13:37:53 +03002172 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002173 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002174 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002175 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002176 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002178 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002179
Avi Kivity0f65dd72011-04-20 13:37:53 +03002180 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002181 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002182 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002183 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002184 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185
Avi Kivity0f65dd72011-04-20 13:37:53 +03002186 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002187 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002188 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002191
2192 if (old_tss_sel != 0xffff) {
2193 tss_seg.prev_task_link = old_tss_sel;
2194
Avi Kivity0f65dd72011-04-20 13:37:53 +03002195 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196 &tss_seg.prev_task_link,
2197 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002198 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002199 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002200 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002201 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002202 }
2203
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002204 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002205}
2206
2207static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002208 struct tss_segment_32 *tss)
2209{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002210 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002211 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002212 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002213 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2214 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2215 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2216 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2217 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2218 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2219 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2220 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002221
Avi Kivity1aa36612011-04-27 13:20:30 +03002222 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2223 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2224 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2225 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2226 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2227 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2228 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002229}
2230
2231static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002232 struct tss_segment_32 *tss)
2233{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002234 int ret;
2235
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002236 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002237 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002238 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002239 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002240 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2241 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2242 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2243 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2244 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2245 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2246 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2247 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002248
2249 /*
2250 * SDM says that segment selectors are loaded before segment
2251 * descriptors
2252 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002253 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2254 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2255 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2256 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2257 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2258 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2259 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002260
2261 /*
2262 * Now load segment descriptors. If fault happenes at this stage
2263 * it is handled in a context of new task
2264 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002265 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002266 if (ret != X86EMUL_CONTINUE)
2267 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002268 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002269 if (ret != X86EMUL_CONTINUE)
2270 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002271 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002272 if (ret != X86EMUL_CONTINUE)
2273 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002274 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002275 if (ret != X86EMUL_CONTINUE)
2276 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002277 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002278 if (ret != X86EMUL_CONTINUE)
2279 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002280 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002281 if (ret != X86EMUL_CONTINUE)
2282 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002283 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002284 if (ret != X86EMUL_CONTINUE)
2285 return ret;
2286
2287 return X86EMUL_CONTINUE;
2288}
2289
2290static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002291 u16 tss_selector, u16 old_tss_sel,
2292 ulong old_tss_base, struct desc_struct *new_desc)
2293{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002294 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002295 struct tss_segment_32 tss_seg;
2296 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002297 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298
Avi Kivity0f65dd72011-04-20 13:37:53 +03002299 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002300 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002301 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002302 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002304
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002305 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002306
Avi Kivity0f65dd72011-04-20 13:37:53 +03002307 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002308 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002309 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002310 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312
Avi Kivity0f65dd72011-04-20 13:37:53 +03002313 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002314 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002315 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002316 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318
2319 if (old_tss_sel != 0xffff) {
2320 tss_seg.prev_task_link = old_tss_sel;
2321
Avi Kivity0f65dd72011-04-20 13:37:53 +03002322 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002323 &tss_seg.prev_task_link,
2324 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002325 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002326 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002329 }
2330
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002331 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002332}
2333
2334static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002335 u16 tss_selector, int reason,
2336 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002338 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339 struct desc_struct curr_tss_desc, next_tss_desc;
2340 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002341 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002342 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002343 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002344 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002345
2346 /* FIXME: old_tss_base == ~0 ? */
2347
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002348 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002349 if (ret != X86EMUL_CONTINUE)
2350 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002351 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002352 if (ret != X86EMUL_CONTINUE)
2353 return ret;
2354
2355 /* FIXME: check that next_tss_desc is tss */
2356
2357 if (reason != TASK_SWITCH_IRET) {
2358 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002359 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002360 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002361 }
2362
Gleb Natapovceffb452010-03-18 15:20:19 +02002363 desc_limit = desc_limit_scaled(&next_tss_desc);
2364 if (!next_tss_desc.p ||
2365 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2366 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002367 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002368 return X86EMUL_PROPAGATE_FAULT;
2369 }
2370
2371 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2372 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002373 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002374 }
2375
2376 if (reason == TASK_SWITCH_IRET)
2377 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2378
2379 /* set back link to prev task only if NT bit is set in eflags
2380 note that old_tss_sel is not used afetr this point */
2381 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2382 old_tss_sel = 0xffff;
2383
2384 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002385 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002386 old_tss_base, &next_tss_desc);
2387 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002388 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002389 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002390 if (ret != X86EMUL_CONTINUE)
2391 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002392
2393 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2394 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2395
2396 if (reason != TASK_SWITCH_IRET) {
2397 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002398 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399 }
2400
Avi Kivity717746e2011-04-20 13:37:53 +03002401 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002402 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002403
Jan Kiszkae269fb22010-04-14 15:51:09 +02002404 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002405 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2406 ctxt->lock_prefix = 0;
2407 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002408 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002409 }
2410
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411 return ret;
2412}
2413
2414int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002415 u16 tss_selector, int reason,
2416 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002417{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002418 int rc;
2419
Avi Kivity9dac77f2011-06-01 15:34:25 +03002420 ctxt->_eip = ctxt->eip;
2421 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002423 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002424 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002425
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002426 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002427 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002428
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002429 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430}
2431
Avi Kivity90de84f2010-11-17 15:28:21 +02002432static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002433 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002434{
Gleb Natapova682e352010-03-18 15:20:21 +02002435 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2436
Avi Kivity9dac77f2011-06-01 15:34:25 +03002437 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2438 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002439 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002440}
2441
Avi Kivity7af04fc2010-08-18 14:16:35 +03002442static int em_das(struct x86_emulate_ctxt *ctxt)
2443{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002444 u8 al, old_al;
2445 bool af, cf, old_cf;
2446
2447 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002448 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002449
2450 old_al = al;
2451 old_cf = cf;
2452 cf = false;
2453 af = ctxt->eflags & X86_EFLAGS_AF;
2454 if ((al & 0x0f) > 9 || af) {
2455 al -= 6;
2456 cf = old_cf | (al >= 250);
2457 af = true;
2458 } else {
2459 af = false;
2460 }
2461 if (old_al > 0x99 || old_cf) {
2462 al -= 0x60;
2463 cf = true;
2464 }
2465
Avi Kivity9dac77f2011-06-01 15:34:25 +03002466 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002467 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002468 ctxt->src.type = OP_IMM;
2469 ctxt->src.val = 0;
2470 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002471 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002472 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2473 if (cf)
2474 ctxt->eflags |= X86_EFLAGS_CF;
2475 if (af)
2476 ctxt->eflags |= X86_EFLAGS_AF;
2477 return X86EMUL_CONTINUE;
2478}
2479
Avi Kivity0ef753b2010-08-18 14:51:45 +03002480static int em_call_far(struct x86_emulate_ctxt *ctxt)
2481{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002482 u16 sel, old_cs;
2483 ulong old_eip;
2484 int rc;
2485
Avi Kivity1aa36612011-04-27 13:20:30 +03002486 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002487 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002488
Avi Kivity9dac77f2011-06-01 15:34:25 +03002489 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002490 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002491 return X86EMUL_CONTINUE;
2492
Avi Kivity9dac77f2011-06-01 15:34:25 +03002493 ctxt->_eip = 0;
2494 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002495
Avi Kivity9dac77f2011-06-01 15:34:25 +03002496 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002497 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002498 if (rc != X86EMUL_CONTINUE)
2499 return rc;
2500
Avi Kivity9dac77f2011-06-01 15:34:25 +03002501 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002502 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002503}
2504
Avi Kivity40ece7c2010-08-18 15:12:09 +03002505static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2506{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002507 int rc;
2508
Avi Kivity9dac77f2011-06-01 15:34:25 +03002509 ctxt->dst.type = OP_REG;
2510 ctxt->dst.addr.reg = &ctxt->_eip;
2511 ctxt->dst.bytes = ctxt->op_bytes;
2512 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002513 if (rc != X86EMUL_CONTINUE)
2514 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002515 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002516 return X86EMUL_CONTINUE;
2517}
2518
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002519static int em_add(struct x86_emulate_ctxt *ctxt)
2520{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002521 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002522 return X86EMUL_CONTINUE;
2523}
2524
2525static int em_or(struct x86_emulate_ctxt *ctxt)
2526{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002527 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002528 return X86EMUL_CONTINUE;
2529}
2530
2531static int em_adc(struct x86_emulate_ctxt *ctxt)
2532{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002533 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002534 return X86EMUL_CONTINUE;
2535}
2536
2537static int em_sbb(struct x86_emulate_ctxt *ctxt)
2538{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002539 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002540 return X86EMUL_CONTINUE;
2541}
2542
2543static int em_and(struct x86_emulate_ctxt *ctxt)
2544{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002545 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002546 return X86EMUL_CONTINUE;
2547}
2548
2549static int em_sub(struct x86_emulate_ctxt *ctxt)
2550{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002551 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002552 return X86EMUL_CONTINUE;
2553}
2554
2555static int em_xor(struct x86_emulate_ctxt *ctxt)
2556{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002557 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002558 return X86EMUL_CONTINUE;
2559}
2560
2561static int em_cmp(struct x86_emulate_ctxt *ctxt)
2562{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002563 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002564 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002565 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002566 return X86EMUL_CONTINUE;
2567}
2568
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002569static int em_test(struct x86_emulate_ctxt *ctxt)
2570{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002571 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002572 /* Disable writeback. */
2573 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002574 return X86EMUL_CONTINUE;
2575}
2576
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002577static int em_xchg(struct x86_emulate_ctxt *ctxt)
2578{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002579 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002580 ctxt->src.val = ctxt->dst.val;
2581 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002582
2583 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002584 ctxt->dst.val = ctxt->src.orig_val;
2585 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002586 return X86EMUL_CONTINUE;
2587}
2588
Avi Kivity5c82aa22010-08-18 18:31:43 +03002589static int em_imul(struct x86_emulate_ctxt *ctxt)
2590{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002591 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002592 return X86EMUL_CONTINUE;
2593}
2594
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002595static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2596{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002597 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002598 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002599}
2600
Avi Kivity61429142010-08-19 15:13:00 +03002601static int em_cwd(struct x86_emulate_ctxt *ctxt)
2602{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002603 ctxt->dst.type = OP_REG;
2604 ctxt->dst.bytes = ctxt->src.bytes;
2605 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2606 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002607
2608 return X86EMUL_CONTINUE;
2609}
2610
Avi Kivity48bb5d32010-08-18 18:54:34 +03002611static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2612{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002613 u64 tsc = 0;
2614
Avi Kivity717746e2011-04-20 13:37:53 +03002615 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002616 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2617 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002618 return X86EMUL_CONTINUE;
2619}
2620
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002621static int em_mov(struct x86_emulate_ctxt *ctxt)
2622{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002623 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002624 return X86EMUL_CONTINUE;
2625}
2626
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002627static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2628{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002629 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002630 return emulate_ud(ctxt);
2631
Avi Kivity9dac77f2011-06-01 15:34:25 +03002632 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002633 return X86EMUL_CONTINUE;
2634}
2635
2636static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2637{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002638 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002639
Avi Kivity9dac77f2011-06-01 15:34:25 +03002640 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002641 return emulate_ud(ctxt);
2642
Avi Kivity9dac77f2011-06-01 15:34:25 +03002643 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002644 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2645
2646 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002647 ctxt->dst.type = OP_NONE;
2648 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002649}
2650
Avi Kivityaa97bb42010-01-20 18:09:23 +02002651static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2652{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002653 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002654 return X86EMUL_CONTINUE;
2655}
2656
Avi Kivity38503912011-03-31 18:48:09 +02002657static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2658{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002659 int rc;
2660 ulong linear;
2661
Avi Kivity9dac77f2011-06-01 15:34:25 +03002662 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002663 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002664 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002665 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002666 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002667 return X86EMUL_CONTINUE;
2668}
2669
Avi Kivity2d04a052011-04-20 15:32:49 +03002670static int em_clts(struct x86_emulate_ctxt *ctxt)
2671{
2672 ulong cr0;
2673
2674 cr0 = ctxt->ops->get_cr(ctxt, 0);
2675 cr0 &= ~X86_CR0_TS;
2676 ctxt->ops->set_cr(ctxt, 0, cr0);
2677 return X86EMUL_CONTINUE;
2678}
2679
Avi Kivity26d05cc2011-04-21 12:07:59 +03002680static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2681{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002682 int rc;
2683
Avi Kivity9dac77f2011-06-01 15:34:25 +03002684 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002685 return X86EMUL_UNHANDLEABLE;
2686
2687 rc = ctxt->ops->fix_hypercall(ctxt);
2688 if (rc != X86EMUL_CONTINUE)
2689 return rc;
2690
2691 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002692 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002693 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002694 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002695 return X86EMUL_CONTINUE;
2696}
2697
2698static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2699{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002700 struct desc_ptr desc_ptr;
2701 int rc;
2702
Avi Kivity9dac77f2011-06-01 15:34:25 +03002703 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002704 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002705 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002706 if (rc != X86EMUL_CONTINUE)
2707 return rc;
2708 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2709 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002710 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002711 return X86EMUL_CONTINUE;
2712}
2713
Avi Kivity5ef39c72011-04-21 12:21:50 +03002714static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002715{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002716 int rc;
2717
Avi Kivity5ef39c72011-04-21 12:21:50 +03002718 rc = ctxt->ops->fix_hypercall(ctxt);
2719
Avi Kivity26d05cc2011-04-21 12:07:59 +03002720 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002721 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002722 return rc;
2723}
2724
2725static int em_lidt(struct x86_emulate_ctxt *ctxt)
2726{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002727 struct desc_ptr desc_ptr;
2728 int rc;
2729
Avi Kivity9dac77f2011-06-01 15:34:25 +03002730 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002731 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002732 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002733 if (rc != X86EMUL_CONTINUE)
2734 return rc;
2735 ctxt->ops->set_idt(ctxt, &desc_ptr);
2736 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002737 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002738 return X86EMUL_CONTINUE;
2739}
2740
2741static int em_smsw(struct x86_emulate_ctxt *ctxt)
2742{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002743 ctxt->dst.bytes = 2;
2744 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002745 return X86EMUL_CONTINUE;
2746}
2747
2748static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2749{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002750 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002751 | (ctxt->src.val & 0x0f));
2752 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002753 return X86EMUL_CONTINUE;
2754}
2755
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002756static int em_loop(struct x86_emulate_ctxt *ctxt)
2757{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002758 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2759 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2760 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2761 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002762
2763 return X86EMUL_CONTINUE;
2764}
2765
2766static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2767{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002768 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2769 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002770
2771 return X86EMUL_CONTINUE;
2772}
2773
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002774static int em_cli(struct x86_emulate_ctxt *ctxt)
2775{
2776 if (emulator_bad_iopl(ctxt))
2777 return emulate_gp(ctxt, 0);
2778
2779 ctxt->eflags &= ~X86_EFLAGS_IF;
2780 return X86EMUL_CONTINUE;
2781}
2782
2783static int em_sti(struct x86_emulate_ctxt *ctxt)
2784{
2785 if (emulator_bad_iopl(ctxt))
2786 return emulate_gp(ctxt, 0);
2787
2788 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2789 ctxt->eflags |= X86_EFLAGS_IF;
2790 return X86EMUL_CONTINUE;
2791}
2792
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002793static bool valid_cr(int nr)
2794{
2795 switch (nr) {
2796 case 0:
2797 case 2 ... 4:
2798 case 8:
2799 return true;
2800 default:
2801 return false;
2802 }
2803}
2804
2805static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2806{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002807 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002808 return emulate_ud(ctxt);
2809
2810 return X86EMUL_CONTINUE;
2811}
2812
2813static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2814{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002815 u64 new_val = ctxt->src.val64;
2816 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002817 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002818
2819 static u64 cr_reserved_bits[] = {
2820 0xffffffff00000000ULL,
2821 0, 0, 0, /* CR3 checked later */
2822 CR4_RESERVED_BITS,
2823 0, 0, 0,
2824 CR8_RESERVED_BITS,
2825 };
2826
2827 if (!valid_cr(cr))
2828 return emulate_ud(ctxt);
2829
2830 if (new_val & cr_reserved_bits[cr])
2831 return emulate_gp(ctxt, 0);
2832
2833 switch (cr) {
2834 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002835 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002836 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2837 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2838 return emulate_gp(ctxt, 0);
2839
Avi Kivity717746e2011-04-20 13:37:53 +03002840 cr4 = ctxt->ops->get_cr(ctxt, 4);
2841 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002842
2843 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2844 !(cr4 & X86_CR4_PAE))
2845 return emulate_gp(ctxt, 0);
2846
2847 break;
2848 }
2849 case 3: {
2850 u64 rsvd = 0;
2851
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002852 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2853 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002854 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002855 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002856 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002857 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002858 rsvd = CR3_NONPAE_RESERVED_BITS;
2859
2860 if (new_val & rsvd)
2861 return emulate_gp(ctxt, 0);
2862
2863 break;
2864 }
2865 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002866 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002867
Avi Kivity717746e2011-04-20 13:37:53 +03002868 cr4 = ctxt->ops->get_cr(ctxt, 4);
2869 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002870
2871 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2872 return emulate_gp(ctxt, 0);
2873
2874 break;
2875 }
2876 }
2877
2878 return X86EMUL_CONTINUE;
2879}
2880
Joerg Roedel3b88e412011-04-04 12:39:29 +02002881static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2882{
2883 unsigned long dr7;
2884
Avi Kivity717746e2011-04-20 13:37:53 +03002885 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002886
2887 /* Check if DR7.Global_Enable is set */
2888 return dr7 & (1 << 13);
2889}
2890
2891static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2892{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002893 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002894 u64 cr4;
2895
2896 if (dr > 7)
2897 return emulate_ud(ctxt);
2898
Avi Kivity717746e2011-04-20 13:37:53 +03002899 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002900 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2901 return emulate_ud(ctxt);
2902
2903 if (check_dr7_gd(ctxt))
2904 return emulate_db(ctxt);
2905
2906 return X86EMUL_CONTINUE;
2907}
2908
2909static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2910{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002911 u64 new_val = ctxt->src.val64;
2912 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002913
2914 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2915 return emulate_gp(ctxt, 0);
2916
2917 return check_dr_read(ctxt);
2918}
2919
Joerg Roedel01de8b02011-04-04 12:39:31 +02002920static int check_svme(struct x86_emulate_ctxt *ctxt)
2921{
2922 u64 efer;
2923
Avi Kivity717746e2011-04-20 13:37:53 +03002924 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002925
2926 if (!(efer & EFER_SVME))
2927 return emulate_ud(ctxt);
2928
2929 return X86EMUL_CONTINUE;
2930}
2931
2932static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2933{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002934 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002935
2936 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002937 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002938 return emulate_gp(ctxt, 0);
2939
2940 return check_svme(ctxt);
2941}
2942
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002943static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2944{
Avi Kivity717746e2011-04-20 13:37:53 +03002945 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002946
Avi Kivity717746e2011-04-20 13:37:53 +03002947 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002948 return emulate_ud(ctxt);
2949
2950 return X86EMUL_CONTINUE;
2951}
2952
Joerg Roedel80612522011-04-04 12:39:33 +02002953static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2954{
Avi Kivity717746e2011-04-20 13:37:53 +03002955 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002956 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002957
Avi Kivity717746e2011-04-20 13:37:53 +03002958 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002959 (rcx > 3))
2960 return emulate_gp(ctxt, 0);
2961
2962 return X86EMUL_CONTINUE;
2963}
2964
Joerg Roedelf6511932011-04-04 12:39:35 +02002965static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2966{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002967 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2968 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002969 return emulate_gp(ctxt, 0);
2970
2971 return X86EMUL_CONTINUE;
2972}
2973
2974static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2975{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002976 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2977 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002978 return emulate_gp(ctxt, 0);
2979
2980 return X86EMUL_CONTINUE;
2981}
2982
Avi Kivity73fba5f2010-07-29 15:11:53 +03002983#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002984#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002985#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2986 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002987#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002988#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002989#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002990#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002991#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002992#define II(_f, _e, _i) \
2993 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002994#define IIP(_f, _e, _i, _p) \
2995 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2996 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002997#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002998
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002999#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003000#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003001#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
3002
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003003#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3004 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3005 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003006
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003007static struct opcode group7_rm1[] = {
3008 DI(SrcNone | ModRM | Priv, monitor),
3009 DI(SrcNone | ModRM | Priv, mwait),
3010 N, N, N, N, N, N,
3011};
3012
Joerg Roedel01de8b02011-04-04 12:39:31 +02003013static struct opcode group7_rm3[] = {
3014 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003015 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003016 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3017 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3018 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3019 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3020 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3021 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3022};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003023
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003024static struct opcode group7_rm7[] = {
3025 N,
3026 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3027 N, N, N, N, N, N,
3028};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003029
Avi Kivity73fba5f2010-07-29 15:11:53 +03003030static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003031 I(Lock, em_add),
3032 I(Lock, em_or),
3033 I(Lock, em_adc),
3034 I(Lock, em_sbb),
3035 I(Lock, em_and),
3036 I(Lock, em_sub),
3037 I(Lock, em_xor),
3038 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003039};
3040
3041static struct opcode group1A[] = {
3042 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3043};
3044
3045static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003046 I(DstMem | SrcImm | ModRM, em_test),
3047 I(DstMem | SrcImm | ModRM, em_test),
3048 I(DstMem | SrcNone | ModRM | Lock, em_not),
3049 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3050 I(SrcMem | ModRM, em_mul_ex),
3051 I(SrcMem | ModRM, em_imul_ex),
3052 I(SrcMem | ModRM, em_div_ex),
3053 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003054};
3055
3056static struct opcode group4[] = {
3057 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3058 N, N, N, N, N, N,
3059};
3060
3061static struct opcode group5[] = {
3062 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003063 D(SrcMem | ModRM | Stack),
3064 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003065 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3066 D(SrcMem | ModRM | Stack), N,
3067};
3068
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003069static struct opcode group6[] = {
3070 DI(ModRM | Prot, sldt),
3071 DI(ModRM | Prot, str),
3072 DI(ModRM | Prot | Priv, lldt),
3073 DI(ModRM | Prot | Priv, ltr),
3074 N, N, N, N,
3075};
3076
Avi Kivity73fba5f2010-07-29 15:11:53 +03003077static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003078 DI(ModRM | Mov | DstMem | Priv, sgdt),
3079 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003080 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3081 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3082 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3083 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3084 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003085}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003086 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3087 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003088 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003089 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3090 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003091} };
3092
3093static struct opcode group8[] = {
3094 N, N, N, N,
3095 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3096 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3097};
3098
3099static struct group_dual group9 = { {
3100 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3101}, {
3102 N, N, N, N, N, N, N, N,
3103} };
3104
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003105static struct opcode group11[] = {
3106 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3107};
3108
Avi Kivityaa97bb42010-01-20 18:09:23 +02003109static struct gprefix pfx_0f_6f_0f_7f = {
3110 N, N, N, I(Sse, em_movdqu),
3111};
3112
Avi Kivity73fba5f2010-07-29 15:11:53 +03003113static struct opcode opcode_table[256] = {
3114 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003115 I6ALU(Lock, em_add),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003116 D(ImplicitOps | Stack | No64 | Src2ES),
3117 D(ImplicitOps | Stack | No64 | Src2ES),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003118 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003119 I6ALU(Lock, em_or),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003120 D(ImplicitOps | Stack | No64 | Src2CS), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003121 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003122 I6ALU(Lock, em_adc),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003123 D(ImplicitOps | Stack | No64 | Src2SS),
3124 D(ImplicitOps | Stack | No64 | Src2SS),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003125 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003126 I6ALU(Lock, em_sbb),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003127 D(ImplicitOps | Stack | No64 | Src2DS),
3128 D(ImplicitOps | Stack | No64 | Src2DS),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003129 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003130 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003131 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003132 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003133 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003134 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003135 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003136 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003137 /* 0x40 - 0x4F */
3138 X16(D(DstReg)),
3139 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003140 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003141 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003142 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003143 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003144 I(ImplicitOps | Stack | No64, em_pusha),
3145 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003146 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3147 N, N, N, N,
3148 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003149 I(SrcImm | Mov | Stack, em_push),
3150 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003151 I(SrcImmByte | Mov | Stack, em_push),
3152 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003153 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3154 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003155 /* 0x70 - 0x7F */
3156 X16(D(SrcImmByte)),
3157 /* 0x80 - 0x87 */
3158 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3159 G(DstMem | SrcImm | ModRM | Group, group1),
3160 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3161 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003162 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003163 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003164 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003165 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3166 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003167 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3168 D(ModRM | SrcMem | NoAccess | DstReg),
3169 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3170 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003171 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003172 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003173 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003174 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003175 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003176 II(ImplicitOps | Stack, em_pushf, pushf),
3177 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003178 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003179 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3180 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3181 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003182 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003183 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003184 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003185 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3186 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003187 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003188 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003189 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003190 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003191 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003192 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003193 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003194 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003195 I(ImplicitOps | Stack, em_ret),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003196 D(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES),
3197 D(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003198 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003199 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003200 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003201 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003202 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003203 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003204 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003205 N, N, N, N,
3206 /* 0xD8 - 0xDF */
3207 N, N, N, N, N, N, N, N,
3208 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003209 X3(I(SrcImmByte, em_loop)),
3210 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003211 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3212 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003213 /* 0xE8 - 0xEF */
3214 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003215 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003216 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3217 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003218 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003219 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003220 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3221 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003222 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003223 D(ImplicitOps), D(ImplicitOps),
3224 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003225 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3226};
3227
3228static struct opcode twobyte_table[256] = {
3229 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003230 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003231 N, I(ImplicitOps | VendorSpecific, em_syscall),
3232 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003233 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003234 N, D(ImplicitOps | ModRM), N, N,
3235 /* 0x10 - 0x1F */
3236 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3237 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003238 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003239 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003240 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003241 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003242 N, N, N, N,
3243 N, N, N, N, N, N, N, N,
3244 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003245 DI(ImplicitOps | Priv, wrmsr),
3246 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3247 DI(ImplicitOps | Priv, rdmsr),
3248 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003249 I(ImplicitOps | VendorSpecific, em_sysenter),
3250 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003251 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003252 N, N, N, N, N, N, N, N,
3253 /* 0x40 - 0x4F */
3254 X16(D(DstReg | SrcMem | ModRM | Mov)),
3255 /* 0x50 - 0x5F */
3256 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3257 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003258 N, N, N, N,
3259 N, N, N, N,
3260 N, N, N, N,
3261 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003262 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003263 N, N, N, N,
3264 N, N, N, N,
3265 N, N, N, N,
3266 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003267 /* 0x80 - 0x8F */
3268 X16(D(SrcImm)),
3269 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003270 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003271 /* 0xA0 - 0xA7 */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003272 D(Stack | Src2FS), D(Stack | Src2FS),
Joerg Roedel80612522011-04-04 12:39:33 +02003273 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003274 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3275 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3276 /* 0xA8 - 0xAF */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003277 D(Stack | Src2GS), D(Stack | Src2GS),
Joerg Roedel80612522011-04-04 12:39:33 +02003278 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003279 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3280 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003281 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003282 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003283 D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivityc191a7a2011-09-13 10:45:49 +03003284 D(DstReg | SrcMemFAddr | ModRM | Src2SS),
3285 D(DstMem | SrcReg | ModRM | BitOp | Lock),
3286 D(DstReg | SrcMemFAddr | ModRM | Src2FS),
3287 D(DstReg | SrcMemFAddr | ModRM | Src2GS),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003288 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003289 /* 0xB8 - 0xBF */
3290 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003291 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003292 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3293 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003294 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003295 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003296 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003297 N, N, N, GD(0, &group9),
3298 N, N, N, N, N, N, N, N,
3299 /* 0xD0 - 0xDF */
3300 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3301 /* 0xE0 - 0xEF */
3302 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3303 /* 0xF0 - 0xFF */
3304 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3305};
3306
3307#undef D
3308#undef N
3309#undef G
3310#undef GD
3311#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003312#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003313#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003314
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003315#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003316#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003317#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003318#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003319
Avi Kivity9dac77f2011-06-01 15:34:25 +03003320static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003321{
3322 unsigned size;
3323
Avi Kivity9dac77f2011-06-01 15:34:25 +03003324 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003325 if (size == 8)
3326 size = 4;
3327 return size;
3328}
3329
3330static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3331 unsigned size, bool sign_extension)
3332{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003333 int rc = X86EMUL_CONTINUE;
3334
3335 op->type = OP_IMM;
3336 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003337 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003338 /* NB. Immediates are sign-extended as necessary. */
3339 switch (op->bytes) {
3340 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003341 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003342 break;
3343 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003344 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003345 break;
3346 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003347 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003348 break;
3349 }
3350 if (!sign_extension) {
3351 switch (op->bytes) {
3352 case 1:
3353 op->val &= 0xff;
3354 break;
3355 case 2:
3356 op->val &= 0xffff;
3357 break;
3358 case 4:
3359 op->val &= 0xffffffff;
3360 break;
3361 }
3362 }
3363done:
3364 return rc;
3365}
3366
Avi Kivitya9945542011-09-13 10:45:41 +03003367static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3368 unsigned d)
3369{
3370 int rc = X86EMUL_CONTINUE;
3371
3372 switch (d) {
3373 case OpReg:
3374 decode_register_operand(ctxt, op,
Avi Kivity52179732011-09-13 10:45:46 +03003375 op == &ctxt->dst &&
Avi Kivitya9945542011-09-13 10:45:41 +03003376 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
3377 break;
3378 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003379 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03003380 break;
3381 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003382 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003383 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03003384 *op = ctxt->memop;
3385 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003386 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03003387 fetch_bit_operand(ctxt);
3388 op->orig_val = op->val;
3389 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003390 case OpMem64:
3391 ctxt->memop.bytes = 8;
3392 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03003393 case OpAcc:
3394 op->type = OP_REG;
3395 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3396 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3397 fetch_register_operand(op);
3398 op->orig_val = op->val;
3399 break;
3400 case OpDI:
3401 op->type = OP_MEM;
3402 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3403 op->addr.mem.ea =
3404 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3405 op->addr.mem.seg = VCPU_SREG_ES;
3406 op->val = 0;
3407 break;
3408 case OpDX:
3409 op->type = OP_REG;
3410 op->bytes = 2;
3411 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3412 fetch_register_operand(op);
3413 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003414 case OpCL:
3415 op->bytes = 1;
3416 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3417 break;
3418 case OpImmByte:
3419 rc = decode_imm(ctxt, op, 1, true);
3420 break;
3421 case OpOne:
3422 op->bytes = 1;
3423 op->val = 1;
3424 break;
3425 case OpImm:
3426 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3427 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03003428 case OpMem16:
3429 ctxt->memop.bytes = 2;
3430 goto mem_common;
3431 case OpMem32:
3432 ctxt->memop.bytes = 4;
3433 goto mem_common;
3434 case OpImmU16:
3435 rc = decode_imm(ctxt, op, 2, false);
3436 break;
3437 case OpImmU:
3438 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3439 break;
3440 case OpSI:
3441 op->type = OP_MEM;
3442 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3443 op->addr.mem.ea =
3444 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3445 op->addr.mem.seg = seg_override(ctxt);
3446 op->val = 0;
3447 break;
3448 case OpImmFAddr:
3449 op->type = OP_IMM;
3450 op->addr.mem.ea = ctxt->_eip;
3451 op->bytes = ctxt->op_bytes + 2;
3452 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3453 break;
3454 case OpMemFAddr:
3455 ctxt->memop.bytes = ctxt->op_bytes + 2;
3456 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003457 case OpES:
3458 op->val = VCPU_SREG_ES;
3459 break;
3460 case OpCS:
3461 op->val = VCPU_SREG_CS;
3462 break;
3463 case OpSS:
3464 op->val = VCPU_SREG_SS;
3465 break;
3466 case OpDS:
3467 op->val = VCPU_SREG_DS;
3468 break;
3469 case OpFS:
3470 op->val = VCPU_SREG_FS;
3471 break;
3472 case OpGS:
3473 op->val = VCPU_SREG_GS;
3474 break;
Avi Kivitya9945542011-09-13 10:45:41 +03003475 case OpImplicit:
3476 /* Special instructions do their own operand decoding. */
3477 default:
3478 op->type = OP_NONE; /* Disable writeback. */
3479 break;
3480 }
3481
3482done:
3483 return rc;
3484}
3485
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003486int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003487{
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003488 int rc = X86EMUL_CONTINUE;
3489 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003490 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003491 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003492 struct opcode opcode;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003493
Avi Kivityf09ed832011-09-13 10:45:40 +03003494 ctxt->memop.type = OP_NONE;
3495 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003496 ctxt->_eip = ctxt->eip;
3497 ctxt->fetch.start = ctxt->_eip;
3498 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003499 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003500 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003501
3502 switch (mode) {
3503 case X86EMUL_MODE_REAL:
3504 case X86EMUL_MODE_VM86:
3505 case X86EMUL_MODE_PROT16:
3506 def_op_bytes = def_ad_bytes = 2;
3507 break;
3508 case X86EMUL_MODE_PROT32:
3509 def_op_bytes = def_ad_bytes = 4;
3510 break;
3511#ifdef CONFIG_X86_64
3512 case X86EMUL_MODE_PROT64:
3513 def_op_bytes = 4;
3514 def_ad_bytes = 8;
3515 break;
3516#endif
3517 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003518 return EMULATION_FAILED;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003519 }
3520
Avi Kivity9dac77f2011-06-01 15:34:25 +03003521 ctxt->op_bytes = def_op_bytes;
3522 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003523
3524 /* Legacy prefixes. */
3525 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003526 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003527 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003528 op_prefix = true;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003529 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003530 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003531 break;
3532 case 0x67: /* address-size override */
3533 if (mode == X86EMUL_MODE_PROT64)
3534 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003535 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003536 else
3537 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003538 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003539 break;
3540 case 0x26: /* ES override */
3541 case 0x2e: /* CS override */
3542 case 0x36: /* SS override */
3543 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003544 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003545 break;
3546 case 0x64: /* FS override */
3547 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003548 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003549 break;
3550 case 0x40 ... 0x4f: /* REX */
3551 if (mode != X86EMUL_MODE_PROT64)
3552 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003553 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003554 continue;
3555 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003556 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003557 break;
3558 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003559 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003560 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003561 break;
3562 default:
3563 goto done_prefixes;
3564 }
3565
3566 /* Any legacy prefix after a REX prefix nullifies its effect. */
3567
Avi Kivity9dac77f2011-06-01 15:34:25 +03003568 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003569 }
3570
3571done_prefixes:
3572
3573 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003574 if (ctxt->rex_prefix & 8)
3575 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003576
3577 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003578 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003579 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003580 if (ctxt->b == 0x0f) {
3581 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003582 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003583 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003584 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003585 ctxt->d = opcode.flags;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003586
Avi Kivity9dac77f2011-06-01 15:34:25 +03003587 while (ctxt->d & GroupMask) {
3588 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003589 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003590 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003591 --ctxt->_eip;
3592 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003593 opcode = opcode.u.group[goffset];
3594 break;
3595 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003596 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003597 --ctxt->_eip;
3598 goffset = (ctxt->modrm >> 3) & 7;
3599 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003600 opcode = opcode.u.gdual->mod3[goffset];
3601 else
3602 opcode = opcode.u.gdual->mod012[goffset];
3603 break;
3604 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003605 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003606 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003607 break;
3608 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003609 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003610 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003611 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003612 switch (simd_prefix) {
3613 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3614 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3615 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3616 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3617 }
3618 break;
3619 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003620 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003621 }
Avi Kivity46561642011-04-24 14:09:59 +03003622
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003623 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003624 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003625 }
3626
Avi Kivity9dac77f2011-06-01 15:34:25 +03003627 ctxt->execute = opcode.u.execute;
3628 ctxt->check_perm = opcode.check_perm;
3629 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003630
3631 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003632 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003633 return EMULATION_FAILED;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003634
Avi Kivity9dac77f2011-06-01 15:34:25 +03003635 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003636 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003637
Avi Kivity9dac77f2011-06-01 15:34:25 +03003638 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3639 ctxt->op_bytes = 8;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003640
Avi Kivity9dac77f2011-06-01 15:34:25 +03003641 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003642 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003643 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003644 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003645 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003646 }
3647
Avi Kivity9dac77f2011-06-01 15:34:25 +03003648 if (ctxt->d & Sse)
3649 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003650
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003651 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003652 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003653 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003654 if (!ctxt->has_seg_override)
3655 set_seg_override(ctxt, ctxt->modrm_seg);
3656 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003657 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003658 if (rc != X86EMUL_CONTINUE)
3659 goto done;
3660
Avi Kivity9dac77f2011-06-01 15:34:25 +03003661 if (!ctxt->has_seg_override)
3662 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003663
Avi Kivityf09ed832011-09-13 10:45:40 +03003664 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003665
Avi Kivityf09ed832011-09-13 10:45:40 +03003666 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
3667 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003668
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003669 /*
3670 * Decode and fetch the source operand: register, memory
3671 * or immediate.
3672 */
Avi Kivity0fe59122011-09-13 10:45:47 +03003673 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003674 if (rc != X86EMUL_CONTINUE)
3675 goto done;
3676
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003677 /*
3678 * Decode and fetch the second source operand: register, memory
3679 * or immediate.
3680 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03003681 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003682 if (rc != X86EMUL_CONTINUE)
3683 goto done;
3684
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003685 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03003686 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003687
3688done:
Avi Kivityf09ed832011-09-13 10:45:40 +03003689 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
3690 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003691
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003692 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003693}
3694
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003695static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3696{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003697 /* The second termination condition only applies for REPE
3698 * and REPNE. Test if the repeat string operation prefix is
3699 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3700 * corresponding termination condition according to:
3701 * - if REPE/REPZ and ZF = 0 then done
3702 * - if REPNE/REPNZ and ZF = 1 then done
3703 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003704 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3705 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3706 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003707 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003708 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003709 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3710 return true;
3711
3712 return false;
3713}
3714
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003715int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003716{
Avi Kivity9aabc882010-07-29 15:11:50 +03003717 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003718 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003719 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003720 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003721
Avi Kivity9dac77f2011-06-01 15:34:25 +03003722 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003723
Avi Kivity9dac77f2011-06-01 15:34:25 +03003724 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003725 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003726 goto done;
3727 }
3728
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003729 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003730 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003731 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003732 goto done;
3733 }
3734
Avi Kivity9dac77f2011-06-01 15:34:25 +03003735 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003736 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003737 goto done;
3738 }
3739
Avi Kivity9dac77f2011-06-01 15:34:25 +03003740 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003741 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3742 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003743 rc = emulate_ud(ctxt);
3744 goto done;
3745 }
3746
Avi Kivity9dac77f2011-06-01 15:34:25 +03003747 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003748 rc = emulate_nm(ctxt);
3749 goto done;
3750 }
3751
Avi Kivity9dac77f2011-06-01 15:34:25 +03003752 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3753 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003754 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003755 if (rc != X86EMUL_CONTINUE)
3756 goto done;
3757 }
3758
Gleb Natapove92805a2010-02-10 14:21:35 +02003759 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003760 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003761 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003762 goto done;
3763 }
3764
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003765 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003766 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003767 rc = emulate_ud(ctxt);
3768 goto done;
3769 }
3770
Joerg Roedeld09beab2011-04-04 12:39:25 +02003771 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003772 if (ctxt->check_perm) {
3773 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003774 if (rc != X86EMUL_CONTINUE)
3775 goto done;
3776 }
3777
Avi Kivity9dac77f2011-06-01 15:34:25 +03003778 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3779 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003780 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003781 if (rc != X86EMUL_CONTINUE)
3782 goto done;
3783 }
3784
Avi Kivity9dac77f2011-06-01 15:34:25 +03003785 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003786 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003787 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3788 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003789 goto done;
3790 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003791 }
3792
Avi Kivity9dac77f2011-06-01 15:34:25 +03003793 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3794 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3795 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003796 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003797 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003798 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003799 }
3800
Avi Kivity9dac77f2011-06-01 15:34:25 +03003801 if (ctxt->src2.type == OP_MEM) {
3802 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3803 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003804 if (rc != X86EMUL_CONTINUE)
3805 goto done;
3806 }
3807
Avi Kivity9dac77f2011-06-01 15:34:25 +03003808 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003809 goto special_insn;
3810
3811
Avi Kivity9dac77f2011-06-01 15:34:25 +03003812 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003813 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003814 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3815 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003816 if (rc != X86EMUL_CONTINUE)
3817 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003818 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003819 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003820
Avi Kivity018a98d2007-11-27 19:30:56 +02003821special_insn:
3822
Avi Kivity9dac77f2011-06-01 15:34:25 +03003823 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3824 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003825 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003826 if (rc != X86EMUL_CONTINUE)
3827 goto done;
3828 }
3829
Avi Kivity9dac77f2011-06-01 15:34:25 +03003830 if (ctxt->execute) {
3831 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003832 if (rc != X86EMUL_CONTINUE)
3833 goto done;
3834 goto writeback;
3835 }
3836
Avi Kivity9dac77f2011-06-01 15:34:25 +03003837 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003838 goto twobyte_insn;
3839
Avi Kivity9dac77f2011-06-01 15:34:25 +03003840 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003841 case 0x06: /* push es */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003842 case 0x0e: /* push cs */
3843 case 0x16: /* push ss */
3844 case 0x1e: /* push ds */
3845 rc = emulate_push_sreg(ctxt, ctxt->src2.val);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003846 break;
3847 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003848 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003849 case 0x1f: /* pop ds */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003850 rc = emulate_pop_sreg(ctxt, ctxt->src2.val);
Avi Kivity33615aa2007-10-31 11:15:56 +02003851 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003852 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02003853 break;
3854 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003855 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02003856 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003858 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003860 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003862 case 0x6c: /* insb */
3863 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003864 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003865 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003866 case 0x6e: /* outsb */
3867 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003868 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003869 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003870 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003871 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003872 if (test_cc(ctxt->b, ctxt->eflags))
3873 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003874 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003875 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003876 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003877 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003879 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003881 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003882 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003883 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003884 rc = em_xchg(ctxt);
3885 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003886 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003887 switch (ctxt->op_bytes) {
3888 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
3889 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
3890 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003891 }
3892 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003893 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003894 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003895 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003896 case 0xc4: /* les */
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003897 case 0xc5: /* lds */
Avi Kivityc191a7a2011-09-13 10:45:49 +03003898 rc = emulate_load_segment(ctxt, ctxt->src2.val);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003899 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003900 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003901 rc = emulate_int(ctxt, 3);
3902 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003903 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003904 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003905 break;
3906 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003907 if (ctxt->eflags & EFLG_OF)
3908 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003909 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003910 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003911 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003912 break;
3913 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003914 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003915 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003916 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003917 case 0xe4: /* inb */
3918 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003919 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003920 case 0xe6: /* outb */
3921 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003922 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003923 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003924 long int rel = ctxt->src.val;
3925 ctxt->src.val = (unsigned long) ctxt->_eip;
3926 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003927 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003928 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003929 }
3930 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003931 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003932 jmp_rel(ctxt, ctxt->src.val);
3933 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003934 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003935 case 0xec: /* in al,dx */
3936 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003937 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003938 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3939 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003940 goto done; /* IO is needed */
3941 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003942 case 0xee: /* out dx,al */
3943 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003944 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003945 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3946 &ctxt->src.val, 1);
3947 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003948 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003949 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003950 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003951 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003952 case 0xf5: /* cmc */
3953 /* complement carry flag from eflags reg */
3954 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003955 break;
3956 case 0xf8: /* clc */
3957 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003958 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003959 case 0xf9: /* stc */
3960 ctxt->eflags |= EFLG_CF;
3961 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003962 case 0xfc: /* cld */
3963 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003964 break;
3965 case 0xfd: /* std */
3966 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003967 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003968 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003969 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003970 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003971 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003972 rc = em_grp45(ctxt);
3973 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003974 default:
3975 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003976 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003977
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003978 if (rc != X86EMUL_CONTINUE)
3979 goto done;
3980
Avi Kivity018a98d2007-11-27 19:30:56 +02003981writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003982 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003983 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003984 goto done;
3985
Gleb Natapov5cd21912010-03-18 15:20:26 +02003986 /*
3987 * restore dst type in case the decoding will be reused
3988 * (happens for string instruction )
3989 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003990 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003991
Avi Kivity9dac77f2011-06-01 15:34:25 +03003992 if ((ctxt->d & SrcMask) == SrcSI)
3993 string_addr_inc(ctxt, seg_override(ctxt),
3994 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003995
Avi Kivity9dac77f2011-06-01 15:34:25 +03003996 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003997 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003998 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003999
Avi Kivity9dac77f2011-06-01 15:34:25 +03004000 if (ctxt->rep_prefix && (ctxt->d & String)) {
4001 struct read_cache *r = &ctxt->io_read;
4002 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004003
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004004 if (!string_insn_completed(ctxt)) {
4005 /*
4006 * Re-enter guest when pio read ahead buffer is empty
4007 * or, if it is not used, after each 1024 iteration.
4008 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004009 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004010 (r->end == 0 || r->end != r->pos)) {
4011 /*
4012 * Reset read cache. Usually happens before
4013 * decode, but since instruction is restarted
4014 * we have to do it here.
4015 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004016 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004017 return EMULATION_RESTART;
4018 }
4019 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004020 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004021 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004022
Avi Kivity9dac77f2011-06-01 15:34:25 +03004023 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004024
4025done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004026 if (rc == X86EMUL_PROPAGATE_FAULT)
4027 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004028 if (rc == X86EMUL_INTERCEPTED)
4029 return EMULATION_INTERCEPTED;
4030
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004031 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004032
4033twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004034 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004035 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004036 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004037 break;
4038 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004039 case 0x0d: /* GrpP (prefetch) */
4040 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004041 break;
4042 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004043 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004044 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004045 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004046 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004047 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004048 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004049 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004050 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004051 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004052 goto done;
4053 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004054 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004055 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004057 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004058 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004059 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004060 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004061 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004062 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004063 goto done;
4064 }
4065
Avi Kivity9dac77f2011-06-01 15:34:25 +03004066 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004068 case 0x30:
4069 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004070 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4071 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4072 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004073 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004074 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004075 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004076 }
4077 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004078 break;
4079 case 0x32:
4080 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004081 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004082 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004083 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004084 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004085 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004086 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4087 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004088 }
4089 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004090 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004092 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4093 if (!test_cc(ctxt->b, ctxt->eflags))
4094 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004096 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004097 if (test_cc(ctxt->b, ctxt->eflags))
4098 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004099 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004100 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004101 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004102 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004103 case 0xa0: /* push fs */
Avi Kivityc191a7a2011-09-13 10:45:49 +03004104 case 0xa8: /* push gs */
4105 rc = emulate_push_sreg(ctxt, ctxt->src2.val);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004106 break;
4107 case 0xa1: /* pop fs */
Avi Kivityc191a7a2011-09-13 10:45:49 +03004108 case 0xa9: /* pop gs */
4109 rc = emulate_pop_sreg(ctxt, ctxt->src2.val);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004110 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004111 case 0xa3:
4112 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004113 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004114 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004115 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004116 emulate_2op_SrcV_nobyte(ctxt, "bt");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004117 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004118 case 0xa4: /* shld imm8, r, r/m */
4119 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004120 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004121 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004122 case 0xab:
4123 bts: /* bts */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004124 emulate_2op_SrcV_nobyte(ctxt, "bts");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004125 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004126 case 0xac: /* shrd imm8, r, r/m */
4127 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004128 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004129 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004130 case 0xae: /* clflush */
4131 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132 case 0xb0 ... 0xb1: /* cmpxchg */
4133 /*
4134 * Save real source value, then compare EAX against
4135 * destination.
4136 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004137 ctxt->src.orig_val = ctxt->src.val;
4138 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004139 emulate_2op_SrcV(ctxt, "cmp");
Laurent Vivier05f086f2007-09-24 11:10:55 +02004140 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004142 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143 } else {
4144 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004145 ctxt->dst.type = OP_REG;
4146 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 }
4148 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004149 case 0xb2: /* lss */
Avi Kivityc191a7a2011-09-13 10:45:49 +03004150 case 0xb4: /* lfs */
4151 case 0xb5: /* lgs */
4152 rc = emulate_load_segment(ctxt, ctxt->src2.val);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004153 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154 case 0xb3:
4155 btr: /* btr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004156 emulate_2op_SrcV_nobyte(ctxt, "btr");
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004159 ctxt->dst.bytes = ctxt->op_bytes;
4160 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4161 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004162 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004164 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 case 0:
4166 goto bt;
4167 case 1:
4168 goto bts;
4169 case 2:
4170 goto btr;
4171 case 3:
4172 goto btc;
4173 }
4174 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004175 case 0xbb:
4176 btc: /* btc */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004177 emulate_2op_SrcV_nobyte(ctxt, "btc");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004178 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004179 case 0xbc: { /* bsf */
4180 u8 zf;
4181 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004182 : "=r"(ctxt->dst.val), "=q"(zf)
4183 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004184 ctxt->eflags &= ~X86_EFLAGS_ZF;
4185 if (zf) {
4186 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004187 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004188 }
4189 break;
4190 }
4191 case 0xbd: { /* bsr */
4192 u8 zf;
4193 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004194 : "=r"(ctxt->dst.val), "=q"(zf)
4195 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004196 ctxt->eflags &= ~X86_EFLAGS_ZF;
4197 if (zf) {
4198 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004199 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004200 }
4201 break;
4202 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004204 ctxt->dst.bytes = ctxt->op_bytes;
4205 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4206 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004208 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004209 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004210 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004211 ctxt->src.val = ctxt->dst.orig_val;
4212 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004213 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004214 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004215 ctxt->dst.bytes = ctxt->op_bytes;
4216 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4217 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004218 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004220 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004221 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004222 default:
4223 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004224 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004225
4226 if (rc != X86EMUL_CONTINUE)
4227 goto done;
4228
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 goto writeback;
4230
4231cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004232 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004233}