blob: 5b4e1d46aa2d6efeba35c95705f8fbcbb5fe986f [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/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030050#define DstDX (8<<1) /* Destination is in DX register */
51#define DstMask (0xf<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080052/* Source operand type. */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030053#define SrcNone (0<<5) /* No source operand. */
54#define SrcReg (1<<5) /* Register operand. */
55#define SrcMem (2<<5) /* Memory operand. */
56#define SrcMem16 (3<<5) /* Memory operand (16-bit). */
57#define SrcMem32 (4<<5) /* Memory operand (32-bit). */
58#define SrcImm (5<<5) /* Immediate operand. */
59#define SrcImmByte (6<<5) /* 8-bit sign-extended immediate operand. */
60#define SrcOne (7<<5) /* Implied '1' */
61#define SrcImmUByte (8<<5) /* 8-bit unsigned immediate operand. */
62#define SrcImmU (9<<5) /* Immediate operand, unsigned */
63#define SrcSI (0xa<<5) /* Source is in the DS:RSI */
64#define SrcImmFAddr (0xb<<5) /* Source is immediate far address */
65#define SrcMemFAddr (0xc<<5) /* Source is far address in memory */
66#define SrcAcc (0xd<<5) /* Source Accumulator */
67#define SrcImmU16 (0xe<<5) /* Immediate operand, unsigned, 16 bits */
68#define SrcDX (0xf<<5) /* Source is in DX register */
69#define SrcMask (0xf<<5)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Generic ModRM decode. */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030071#define ModRM (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072/* Destination is only written; never read. */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030073#define Mov (1<<10)
74#define BitOp (1<<11)
75#define MemAbs (1<<12) /* Memory operand is absolute displacement */
76#define String (1<<13) /* String instruction (rep capable) */
77#define Stack (1<<14) /* Stack instruction (push/pop) */
78#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
79#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
80#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
81#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
82#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
83#define Sse (1<<18) /* SSE Vector instruction */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030084/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +020085#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +020086#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030087#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030088#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030089#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020090#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020091#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030092#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010093/* Source 2 operand type */
94#define Src2None (0<<29)
95#define Src2CL (1<<29)
96#define Src2ImmByte (2<<29)
97#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030098#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010099#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100
Avi Kivityd0e53322010-07-29 15:11:54 +0300101#define X2(x...) x, x
102#define X3(x...) X2(x), x
103#define X4(x...) X2(x), X2(x)
104#define X5(x...) X4(x), x
105#define X6(x...) X4(x), X2(x)
106#define X7(x...) X4(x), X3(x)
107#define X8(x...) X4(x), X4(x)
108#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300109
Avi Kivityd65b1de2010-07-29 15:11:35 +0300110struct opcode {
111 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200112 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300113 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300114 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300115 struct opcode *group;
116 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200117 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300118 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200119 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300120};
121
122struct group_dual {
123 struct opcode mod012[8];
124 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300125};
126
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200127struct gprefix {
128 struct opcode pfx_no;
129 struct opcode pfx_66;
130 struct opcode pfx_f2;
131 struct opcode pfx_f3;
132};
133
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200135#define EFLG_ID (1<<21)
136#define EFLG_VIP (1<<20)
137#define EFLG_VIF (1<<19)
138#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200139#define EFLG_VM (1<<17)
140#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200141#define EFLG_IOPL (3<<12)
142#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143#define EFLG_OF (1<<11)
144#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200145#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200146#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147#define EFLG_SF (1<<7)
148#define EFLG_ZF (1<<6)
149#define EFLG_AF (1<<4)
150#define EFLG_PF (1<<2)
151#define EFLG_CF (1<<0)
152
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300153#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
154#define EFLG_RESERVED_ONE_MASK 2
155
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156/*
157 * Instruction emulation:
158 * Most instructions are emulated directly via a fragment of inline assembly
159 * code. This allows us to save/restore EFLAGS and thus very easily pick up
160 * any modified flags.
161 */
162
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800163#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164#define _LO32 "k" /* force 32-bit operand */
165#define _STK "%%rsp" /* stack pointer */
166#elif defined(__i386__)
167#define _LO32 "" /* force 32-bit operand */
168#define _STK "%%esp" /* stack pointer */
169#endif
170
171/*
172 * These EFLAGS bits are restored from saved value during emulation, and
173 * any changes are written back to the saved value after emulation.
174 */
175#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
176
177/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200178#define _PRE_EFLAGS(_sav, _msk, _tmp) \
179 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
180 "movl %"_sav",%"_LO32 _tmp"; " \
181 "push %"_tmp"; " \
182 "push %"_tmp"; " \
183 "movl %"_msk",%"_LO32 _tmp"; " \
184 "andl %"_LO32 _tmp",("_STK"); " \
185 "pushf; " \
186 "notl %"_LO32 _tmp"; " \
187 "andl %"_LO32 _tmp",("_STK"); " \
188 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
189 "pop %"_tmp"; " \
190 "orl %"_LO32 _tmp",("_STK"); " \
191 "popf; " \
192 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193
194/* After executing instruction: write-back necessary bits in EFLAGS. */
195#define _POST_EFLAGS(_sav, _msk, _tmp) \
196 /* _sav |= EFLAGS & _msk; */ \
197 "pushf; " \
198 "pop %"_tmp"; " \
199 "andl %"_msk",%"_LO32 _tmp"; " \
200 "orl %"_LO32 _tmp",%"_sav"; "
201
Avi Kivitydda96d82008-11-26 15:14:10 +0200202#ifdef CONFIG_X86_64
203#define ON64(x) x
204#else
205#define ON64(x)
206#endif
207
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300208#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200209 do { \
210 __asm__ __volatile__ ( \
211 _PRE_EFLAGS("0", "4", "2") \
212 _op _suffix " %"_x"3,%1; " \
213 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300214 : "=m" ((ctxt)->eflags), \
215 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200216 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300217 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200218 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200219
220
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300222#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200223 do { \
224 unsigned long _tmp; \
225 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300226 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200227 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300228 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200229 break; \
230 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300231 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200232 break; \
233 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300234 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200235 break; \
236 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 } while (0)
238
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300239#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200241 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300242 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300244 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 break; \
246 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300247 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 _wx, _wy, _lx, _ly, _qx, _qy); \
249 break; \
250 } \
251 } while (0)
252
253/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300254#define emulate_2op_SrcB(ctxt, _op) \
255 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256
257/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300258#define emulate_2op_SrcV(ctxt, _op) \
259 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260
261/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300262#define emulate_2op_SrcV_nobyte(ctxt, _op) \
263 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100265/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300266#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300267 do { \
268 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300269 _type _clv = (ctxt)->src2.val; \
270 _type _srcv = (ctxt)->src.val; \
271 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300272 \
273 __asm__ __volatile__ ( \
274 _PRE_EFLAGS("0", "5", "2") \
275 _op _suffix " %4,%1 \n" \
276 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300277 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300278 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
279 ); \
280 \
Avi Kivity761441b2011-09-07 16:41:36 +0300281 (ctxt)->src2.val = (unsigned long) _clv; \
282 (ctxt)->src2.val = (unsigned long) _srcv; \
283 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100284 } while (0)
285
Avi Kivity761441b2011-09-07 16:41:36 +0300286#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300287 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300288 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300289 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300290 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300291 break; \
292 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300293 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300294 break; \
295 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300296 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300297 break; \
298 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100299 } while (0)
300
Avi Kivityd1eef452011-09-07 16:41:38 +0300301#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 do { \
303 unsigned long _tmp; \
304 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200305 __asm__ __volatile__ ( \
306 _PRE_EFLAGS("0", "3", "2") \
307 _op _suffix " %1; " \
308 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300309 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200310 "=&r" (_tmp) \
311 : "i" (EFLAGS_MASK)); \
312 } while (0)
313
314/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300315#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200316 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300317 switch ((ctxt)->dst.bytes) { \
318 case 1: __emulate_1op(ctxt, _op, "b"); break; \
319 case 2: __emulate_1op(ctxt, _op, "w"); break; \
320 case 4: __emulate_1op(ctxt, _op, "l"); break; \
321 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 } \
323 } while (0)
324
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300325#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
326 do { \
327 unsigned long _tmp; \
328 \
329 __asm__ __volatile__ ( \
330 _PRE_EFLAGS("0", "4", "1") \
331 _op _suffix " %5; " \
332 _POST_EFLAGS("0", "4", "1") \
333 : "=m" (_eflags), "=&r" (_tmp), \
334 "+a" (_rax), "+d" (_rdx) \
335 : "i" (EFLAGS_MASK), "m" ((_src).val), \
336 "a" (_rax), "d" (_rdx)); \
337 } while (0)
338
Avi Kivityf6b35972010-08-26 11:59:00 +0300339#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
340 do { \
341 unsigned long _tmp; \
342 \
343 __asm__ __volatile__ ( \
344 _PRE_EFLAGS("0", "5", "1") \
345 "1: \n\t" \
346 _op _suffix " %6; " \
347 "2: \n\t" \
348 _POST_EFLAGS("0", "5", "1") \
349 ".pushsection .fixup,\"ax\" \n\t" \
350 "3: movb $1, %4 \n\t" \
351 "jmp 2b \n\t" \
352 ".popsection \n\t" \
353 _ASM_EXTABLE(1b, 3b) \
354 : "=m" (_eflags), "=&r" (_tmp), \
355 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
356 : "i" (EFLAGS_MASK), "m" ((_src).val), \
357 "a" (_rax), "d" (_rdx)); \
358 } while (0)
359
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300360/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivity72952612011-04-20 13:12:27 +0300361#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
362 do { \
363 switch((_src).bytes) { \
364 case 1: \
365 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
366 _eflags, "b"); \
367 break; \
368 case 2: \
369 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
370 _eflags, "w"); \
371 break; \
372 case 4: \
373 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
374 _eflags, "l"); \
375 break; \
376 case 8: \
377 ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
378 _eflags, "q")); \
379 break; \
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300380 } \
381 } while (0)
382
Avi Kivityf6b35972010-08-26 11:59:00 +0300383#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
384 do { \
385 switch((_src).bytes) { \
386 case 1: \
387 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
388 _eflags, "b", _ex); \
389 break; \
390 case 2: \
391 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
392 _eflags, "w", _ex); \
393 break; \
394 case 4: \
395 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
396 _eflags, "l", _ex); \
397 break; \
398 case 8: ON64( \
399 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
400 _eflags, "q", _ex)); \
401 break; \
402 } \
403 } while (0)
404
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200405static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
406 enum x86_intercept intercept,
407 enum x86_intercept_stage stage)
408{
409 struct x86_instruction_info info = {
410 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300411 .rep_prefix = ctxt->rep_prefix,
412 .modrm_mod = ctxt->modrm_mod,
413 .modrm_reg = ctxt->modrm_reg,
414 .modrm_rm = ctxt->modrm_rm,
415 .src_val = ctxt->src.val64,
416 .src_bytes = ctxt->src.bytes,
417 .dst_bytes = ctxt->dst.bytes,
418 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200419 .next_rip = ctxt->eip,
420 };
421
Avi Kivity29535382011-04-20 13:37:53 +0300422 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200423}
424
Avi Kivity9dac77f2011-06-01 15:34:25 +0300425static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800426{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300427 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800428}
429
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800431static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300432address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800433{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300434 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800435 return reg;
436 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300437 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800438}
439
440static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300441register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800442{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300443 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800444}
445
Harvey Harrison7a9572752008-02-19 07:40:41 -0800446static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300447register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800448{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300449 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800450 *reg += inc;
451 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300452 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800453}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454
Avi Kivity9dac77f2011-06-01 15:34:25 +0300455static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800456{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300457 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800458}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300459
Avi Kivity56697682011-04-03 14:08:51 +0300460static u32 desc_limit_scaled(struct desc_struct *desc)
461{
462 u32 limit = get_desc_limit(desc);
463
464 return desc->g ? (limit << 12) | 0xfff : limit;
465}
466
Avi Kivity9dac77f2011-06-01 15:34:25 +0300467static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300468{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300469 ctxt->has_seg_override = true;
470 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300471}
472
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900473static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300474{
475 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
476 return 0;
477
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900478 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300479}
480
Avi Kivity9dac77f2011-06-01 15:34:25 +0300481static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300482{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300483 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300484 return 0;
485
Avi Kivity9dac77f2011-06-01 15:34:25 +0300486 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300487}
488
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200489static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
490 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300491{
Avi Kivityda9cb572010-11-22 17:53:21 +0200492 ctxt->exception.vector = vec;
493 ctxt->exception.error_code = error;
494 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200495 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300496}
497
Joerg Roedel3b88e412011-04-04 12:39:29 +0200498static int emulate_db(struct x86_emulate_ctxt *ctxt)
499{
500 return emulate_exception(ctxt, DB_VECTOR, 0, false);
501}
502
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200503static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300504{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200505 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300506}
507
Avi Kivity618ff152011-04-03 12:32:09 +0300508static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
509{
510 return emulate_exception(ctxt, SS_VECTOR, err, true);
511}
512
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200513static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300514{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200515 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300516}
517
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200518static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300519{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200520 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300521}
522
Avi Kivity34d1f492010-08-26 11:59:01 +0300523static int emulate_de(struct x86_emulate_ctxt *ctxt)
524{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300526}
527
Avi Kivity1253791d2011-03-29 11:41:27 +0200528static int emulate_nm(struct x86_emulate_ctxt *ctxt)
529{
530 return emulate_exception(ctxt, NM_VECTOR, 0, false);
531}
532
Avi Kivity1aa36612011-04-27 13:20:30 +0300533static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
534{
535 u16 selector;
536 struct desc_struct desc;
537
538 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
539 return selector;
540}
541
542static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
543 unsigned seg)
544{
545 u16 dummy;
546 u32 base3;
547 struct desc_struct desc;
548
549 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
550 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
551}
552
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400553static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300554 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400555 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300556 ulong *linear)
557{
Avi Kivity618ff152011-04-03 12:32:09 +0300558 struct desc_struct desc;
559 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300560 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300561 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300562 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300563 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300564
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900565 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300566 switch (ctxt->mode) {
567 case X86EMUL_MODE_REAL:
568 break;
569 case X86EMUL_MODE_PROT64:
570 if (((signed long)la << 16) >> 16 != la)
571 return emulate_gp(ctxt, 0);
572 break;
573 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300574 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
575 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300576 if (!usable)
577 goto bad;
578 /* code segment or read-only data segment */
579 if (((desc.type & 8) || !(desc.type & 2)) && write)
580 goto bad;
581 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400582 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300583 goto bad;
584 lim = desc_limit_scaled(&desc);
585 if ((desc.type & 8) || !(desc.type & 4)) {
586 /* expand-up segment */
587 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
588 goto bad;
589 } else {
590 /* exapand-down segment */
591 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
592 goto bad;
593 lim = desc.d ? 0xffffffff : 0xffff;
594 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
595 goto bad;
596 }
Avi Kivity717746e2011-04-20 13:37:53 +0300597 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300598 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300599 cpl = max(cpl, rpl);
600 if (!(desc.type & 8)) {
601 /* data segment */
602 if (cpl > desc.dpl)
603 goto bad;
604 } else if ((desc.type & 8) && !(desc.type & 4)) {
605 /* nonconforming code segment */
606 if (cpl != desc.dpl)
607 goto bad;
608 } else if ((desc.type & 8) && (desc.type & 4)) {
609 /* conforming code segment */
610 if (cpl < desc.dpl)
611 goto bad;
612 }
613 break;
614 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300615 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300616 la &= (u32)-1;
617 *linear = la;
618 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300619bad:
620 if (addr.seg == VCPU_SREG_SS)
621 return emulate_ss(ctxt, addr.seg);
622 else
623 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300624}
625
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400626static int linearize(struct x86_emulate_ctxt *ctxt,
627 struct segmented_address addr,
628 unsigned size, bool write,
629 ulong *linear)
630{
631 return __linearize(ctxt, addr, size, write, false, linear);
632}
633
634
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200635static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
636 struct segmented_address addr,
637 void *data,
638 unsigned size)
639{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200640 int rc;
641 ulong linear;
642
Avi Kivity83b87952011-04-03 11:31:19 +0300643 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200644 if (rc != X86EMUL_CONTINUE)
645 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300646 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200647}
648
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900649/*
650 * Fetch the next byte of the instruction being emulated which is pointed to
651 * by ctxt->_eip, then increment ctxt->_eip.
652 *
653 * Also prefetch the remaining bytes of the instruction without crossing page
654 * boundary if they are not in fetch_cache yet.
655 */
656static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200657{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300658 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200659 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300660 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200661
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900662 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400663 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900664 struct segmented_address addr = { .seg = VCPU_SREG_CS,
665 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300666 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900667 size = min(15UL - cur_size,
668 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400669 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900670 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400671 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900672 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
673 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900674 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200675 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300676 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200677 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900678 *dest = fc->data[ctxt->_eip - fc->start];
679 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900680 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200681}
682
683static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900684 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200685{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900686 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200687
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200688 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900689 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200690 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200691 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900692 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900693 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200694 return rc;
695 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900696 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200697}
698
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900699/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900700#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900701({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900702 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900703 if (rc != X86EMUL_CONTINUE) \
704 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900705 (_type)_x; \
706})
707
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900708#define insn_fetch_arr(_arr, _size, _ctxt) \
709({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900710 if (rc != X86EMUL_CONTINUE) \
711 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900712})
713
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000714/*
715 * Given the 'reg' portion of a ModRM byte, and a register block, return a
716 * pointer into the block that addresses the relevant register.
717 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
718 */
719static void *decode_register(u8 modrm_reg, unsigned long *regs,
720 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721{
722 void *p;
723
724 p = &regs[modrm_reg];
725 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
726 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
727 return p;
728}
729
730static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200731 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732 u16 *size, unsigned long *address, int op_bytes)
733{
734 int rc;
735
736 if (op_bytes == 2)
737 op_bytes = 3;
738 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200739 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900740 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200742 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200743 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 return rc;
745}
746
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300747static int test_cc(unsigned int condition, unsigned int flags)
748{
749 int rc = 0;
750
751 switch ((condition & 15) >> 1) {
752 case 0: /* o */
753 rc |= (flags & EFLG_OF);
754 break;
755 case 1: /* b/c/nae */
756 rc |= (flags & EFLG_CF);
757 break;
758 case 2: /* z/e */
759 rc |= (flags & EFLG_ZF);
760 break;
761 case 3: /* be/na */
762 rc |= (flags & (EFLG_CF|EFLG_ZF));
763 break;
764 case 4: /* s */
765 rc |= (flags & EFLG_SF);
766 break;
767 case 5: /* p/pe */
768 rc |= (flags & EFLG_PF);
769 break;
770 case 7: /* le/ng */
771 rc |= (flags & EFLG_ZF);
772 /* fall through */
773 case 6: /* l/nge */
774 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
775 break;
776 }
777
778 /* Odd condition identifiers (lsb == 1) have inverted sense. */
779 return (!!rc ^ (condition & 1));
780}
781
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300782static void fetch_register_operand(struct operand *op)
783{
784 switch (op->bytes) {
785 case 1:
786 op->val = *(u8 *)op->addr.reg;
787 break;
788 case 2:
789 op->val = *(u16 *)op->addr.reg;
790 break;
791 case 4:
792 op->val = *(u32 *)op->addr.reg;
793 break;
794 case 8:
795 op->val = *(u64 *)op->addr.reg;
796 break;
797 }
798}
799
Avi Kivity1253791d2011-03-29 11:41:27 +0200800static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
801{
802 ctxt->ops->get_fpu(ctxt);
803 switch (reg) {
804 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
805 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
806 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
807 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
808 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
809 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
810 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
811 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
812#ifdef CONFIG_X86_64
813 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
814 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
815 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
816 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
817 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
818 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
819 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
820 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
821#endif
822 default: BUG();
823 }
824 ctxt->ops->put_fpu(ctxt);
825}
826
827static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
828 int reg)
829{
830 ctxt->ops->get_fpu(ctxt);
831 switch (reg) {
832 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
833 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
834 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
835 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
836 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
837 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
838 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
839 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
840#ifdef CONFIG_X86_64
841 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
842 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
843 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
844 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
845 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
846 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
847 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
848 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
849#endif
850 default: BUG();
851 }
852 ctxt->ops->put_fpu(ctxt);
853}
854
855static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
856 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200857 int inhibit_bytereg)
858{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300859 unsigned reg = ctxt->modrm_reg;
860 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200861
Avi Kivity9dac77f2011-06-01 15:34:25 +0300862 if (!(ctxt->d & ModRM))
863 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +0200864
Avi Kivity9dac77f2011-06-01 15:34:25 +0300865 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +0200866 op->type = OP_XMM;
867 op->bytes = 16;
868 op->addr.xmm = reg;
869 read_sse_reg(ctxt, &op->vec_val, reg);
870 return;
871 }
872
Avi Kivity3c118e22007-10-31 10:27:04 +0200873 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300874 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
875 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200876 op->bytes = 1;
877 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300878 op->addr.reg = decode_register(reg, ctxt->regs, 0);
879 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200880 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300881 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200882 op->orig_val = op->val;
883}
884
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200885static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300886 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200887{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700889 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900890 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300891 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200892
Avi Kivity9dac77f2011-06-01 15:34:25 +0300893 if (ctxt->rex_prefix) {
894 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
895 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
896 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200897 }
898
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900899 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300900 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
901 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
902 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
903 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200904
Avi Kivity9dac77f2011-06-01 15:34:25 +0300905 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300906 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300907 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
908 op->addr.reg = decode_register(ctxt->modrm_rm,
909 ctxt->regs, ctxt->d & ByteOp);
910 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +0200911 op->type = OP_XMM;
912 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300913 op->addr.xmm = ctxt->modrm_rm;
914 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +0200915 return rc;
916 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300917 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200918 return rc;
919 }
920
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300921 op->type = OP_MEM;
922
Avi Kivity9dac77f2011-06-01 15:34:25 +0300923 if (ctxt->ad_bytes == 2) {
924 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
925 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
926 unsigned si = ctxt->regs[VCPU_REGS_RSI];
927 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200928
929 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300930 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200931 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300932 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900933 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200934 break;
935 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900936 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200937 break;
938 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900939 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200940 break;
941 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300942 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200943 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300944 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200945 break;
946 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300947 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200948 break;
949 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300950 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200951 break;
952 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300953 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200954 break;
955 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300956 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200957 break;
958 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300959 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200960 break;
961 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300962 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300963 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200964 break;
965 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300966 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200967 break;
968 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300969 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
970 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
971 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300972 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200973 } else {
974 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300975 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900976 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200977 index_reg |= (sib >> 3) & 7;
978 base_reg |= sib & 7;
979 scale = sib >> 6;
980
Avi Kivity9dac77f2011-06-01 15:34:25 +0300981 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900982 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700983 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300984 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700985 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300986 modrm_ea += ctxt->regs[index_reg] << scale;
987 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700988 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300989 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700990 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300991 modrm_ea += ctxt->regs[ctxt->modrm_rm];
992 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200993 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300994 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900995 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200996 break;
997 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900998 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200999 break;
1000 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001001 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001002 break;
1003 }
1004 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001005 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006done:
1007 return rc;
1008}
1009
1010static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001011 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001012{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001013 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001014
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001015 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001016 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001017 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001018 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001019 break;
1020 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001021 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001022 break;
1023 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001024 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001025 break;
1026 }
1027done:
1028 return rc;
1029}
1030
Avi Kivity9dac77f2011-06-01 15:34:25 +03001031static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001032{
Sheng Yang7129eec2010-09-28 16:33:32 +08001033 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001034
Avi Kivity9dac77f2011-06-01 15:34:25 +03001035 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1036 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001037
Avi Kivity9dac77f2011-06-01 15:34:25 +03001038 if (ctxt->src.bytes == 2)
1039 sv = (s16)ctxt->src.val & (s16)mask;
1040 else if (ctxt->src.bytes == 4)
1041 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001042
Avi Kivity9dac77f2011-06-01 15:34:25 +03001043 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001044 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001045
1046 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001047 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001048}
1049
Gleb Natapov9de41572010-04-28 19:15:22 +03001050static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001051 unsigned long addr, void *dest, unsigned size)
1052{
1053 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001054 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001055
1056 while (size) {
1057 int n = min(size, 8u);
1058 size -= n;
1059 if (mc->pos < mc->end)
1060 goto read_cached;
1061
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001062 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1063 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001064 if (rc != X86EMUL_CONTINUE)
1065 return rc;
1066 mc->end += n;
1067
1068 read_cached:
1069 memcpy(dest, mc->data + mc->pos, n);
1070 mc->pos += n;
1071 dest += n;
1072 addr += n;
1073 }
1074 return X86EMUL_CONTINUE;
1075}
1076
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001077static int segmented_read(struct x86_emulate_ctxt *ctxt,
1078 struct segmented_address addr,
1079 void *data,
1080 unsigned size)
1081{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001082 int rc;
1083 ulong linear;
1084
Avi Kivity83b87952011-04-03 11:31:19 +03001085 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001086 if (rc != X86EMUL_CONTINUE)
1087 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001088 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001089}
1090
1091static int segmented_write(struct x86_emulate_ctxt *ctxt,
1092 struct segmented_address addr,
1093 const void *data,
1094 unsigned size)
1095{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001096 int rc;
1097 ulong linear;
1098
Avi Kivity83b87952011-04-03 11:31:19 +03001099 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001100 if (rc != X86EMUL_CONTINUE)
1101 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001102 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1103 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001104}
1105
1106static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1107 struct segmented_address addr,
1108 const void *orig_data, const void *data,
1109 unsigned size)
1110{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001111 int rc;
1112 ulong linear;
1113
Avi Kivity83b87952011-04-03 11:31:19 +03001114 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001115 if (rc != X86EMUL_CONTINUE)
1116 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001117 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1118 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001119}
1120
Gleb Natapov7b262e92010-03-18 15:20:27 +02001121static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001122 unsigned int size, unsigned short port,
1123 void *dest)
1124{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001125 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001126
1127 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001128 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001129 unsigned int count = ctxt->rep_prefix ?
1130 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001131 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1133 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001134 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1135 count);
1136 if (n == 0)
1137 n = 1;
1138 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001139 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001140 return 0;
1141 rc->end = n * size;
1142 }
1143
1144 memcpy(dest, rc->data + rc->pos, size);
1145 rc->pos += size;
1146 return 1;
1147}
1148
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001149static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001150 u16 selector, struct desc_ptr *dt)
1151{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001152 struct x86_emulate_ops *ops = ctxt->ops;
1153
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001154 if (selector & 1 << 2) {
1155 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001156 u16 sel;
1157
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001158 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001159 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001160 return;
1161
1162 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1163 dt->address = get_desc_base(&desc);
1164 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001165 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001166}
1167
1168/* allowed just for 8 bytes segments */
1169static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001170 u16 selector, struct desc_struct *desc)
1171{
1172 struct desc_ptr dt;
1173 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001174 ulong addr;
1175
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001176 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001177
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001178 if (dt.size < index * 8 + 7)
1179 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001180
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001181 addr = dt.address + index * 8;
1182 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1183 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001184}
1185
1186/* allowed just for 8 bytes segments */
1187static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001188 u16 selector, struct desc_struct *desc)
1189{
1190 struct desc_ptr dt;
1191 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001192 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001193
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001194 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001195
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001196 if (dt.size < index * 8 + 7)
1197 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198
1199 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001200 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1201 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001202}
1203
Gleb Natapov5601d052011-03-07 14:55:06 +02001204/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001205static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001206 u16 selector, int seg)
1207{
1208 struct desc_struct seg_desc;
1209 u8 dpl, rpl, cpl;
1210 unsigned err_vec = GP_VECTOR;
1211 u32 err_code = 0;
1212 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1213 int ret;
1214
1215 memset(&seg_desc, 0, sizeof seg_desc);
1216
1217 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1218 || ctxt->mode == X86EMUL_MODE_REAL) {
1219 /* set real mode segment descriptor */
1220 set_desc_base(&seg_desc, selector << 4);
1221 set_desc_limit(&seg_desc, 0xffff);
1222 seg_desc.type = 3;
1223 seg_desc.p = 1;
1224 seg_desc.s = 1;
1225 goto load;
1226 }
1227
1228 /* NULL selector is not valid for TR, CS and SS */
1229 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1230 && null_selector)
1231 goto exception;
1232
1233 /* TR should be in GDT only */
1234 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1235 goto exception;
1236
1237 if (null_selector) /* for NULL selector skip all following checks */
1238 goto load;
1239
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001240 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001241 if (ret != X86EMUL_CONTINUE)
1242 return ret;
1243
1244 err_code = selector & 0xfffc;
1245 err_vec = GP_VECTOR;
1246
1247 /* can't load system descriptor into segment selecor */
1248 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1249 goto exception;
1250
1251 if (!seg_desc.p) {
1252 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1253 goto exception;
1254 }
1255
1256 rpl = selector & 3;
1257 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001258 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001259
1260 switch (seg) {
1261 case VCPU_SREG_SS:
1262 /*
1263 * segment is not a writable data segment or segment
1264 * selector's RPL != CPL or segment selector's RPL != CPL
1265 */
1266 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1267 goto exception;
1268 break;
1269 case VCPU_SREG_CS:
1270 if (!(seg_desc.type & 8))
1271 goto exception;
1272
1273 if (seg_desc.type & 4) {
1274 /* conforming */
1275 if (dpl > cpl)
1276 goto exception;
1277 } else {
1278 /* nonconforming */
1279 if (rpl > cpl || dpl != cpl)
1280 goto exception;
1281 }
1282 /* CS(RPL) <- CPL */
1283 selector = (selector & 0xfffc) | cpl;
1284 break;
1285 case VCPU_SREG_TR:
1286 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1287 goto exception;
1288 break;
1289 case VCPU_SREG_LDTR:
1290 if (seg_desc.s || seg_desc.type != 2)
1291 goto exception;
1292 break;
1293 default: /* DS, ES, FS, or GS */
1294 /*
1295 * segment is not a data or readable code segment or
1296 * ((segment is a data or nonconforming code segment)
1297 * and (both RPL and CPL > DPL))
1298 */
1299 if ((seg_desc.type & 0xa) == 0x8 ||
1300 (((seg_desc.type & 0xc) != 0xc) &&
1301 (rpl > dpl && cpl > dpl)))
1302 goto exception;
1303 break;
1304 }
1305
1306 if (seg_desc.s) {
1307 /* mark segment as accessed */
1308 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001309 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001310 if (ret != X86EMUL_CONTINUE)
1311 return ret;
1312 }
1313load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001314 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001315 return X86EMUL_CONTINUE;
1316exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001317 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001318 return X86EMUL_PROPAGATE_FAULT;
1319}
1320
Wei Yongjun31be40b2010-08-17 09:17:30 +08001321static void write_register_operand(struct operand *op)
1322{
1323 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1324 switch (op->bytes) {
1325 case 1:
1326 *(u8 *)op->addr.reg = (u8)op->val;
1327 break;
1328 case 2:
1329 *(u16 *)op->addr.reg = (u16)op->val;
1330 break;
1331 case 4:
1332 *op->addr.reg = (u32)op->val;
1333 break; /* 64b: zero-extend */
1334 case 8:
1335 *op->addr.reg = op->val;
1336 break;
1337 }
1338}
1339
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001340static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001341{
1342 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001343
Avi Kivity9dac77f2011-06-01 15:34:25 +03001344 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001345 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001346 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001347 break;
1348 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001349 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001350 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001351 ctxt->dst.addr.mem,
1352 &ctxt->dst.orig_val,
1353 &ctxt->dst.val,
1354 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001355 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001356 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001357 ctxt->dst.addr.mem,
1358 &ctxt->dst.val,
1359 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001360 if (rc != X86EMUL_CONTINUE)
1361 return rc;
1362 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001363 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001364 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001365 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001366 case OP_NONE:
1367 /* no writeback */
1368 break;
1369 default:
1370 break;
1371 }
1372 return X86EMUL_CONTINUE;
1373}
1374
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001375static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001376{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001377 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001378
Avi Kivity9dac77f2011-06-01 15:34:25 +03001379 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1380 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001381 addr.seg = VCPU_SREG_SS;
1382
1383 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001384 ctxt->dst.type = OP_NONE;
1385 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001386}
1387
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001388static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001389 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001390{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001391 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001392 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393
Avi Kivity9dac77f2011-06-01 15:34:25 +03001394 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001395 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001396 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001397 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398 return rc;
1399
Avi Kivity9dac77f2011-06-01 15:34:25 +03001400 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001401 return rc;
1402}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001404static int em_pop(struct x86_emulate_ctxt *ctxt)
1405{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001406 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001407}
1408
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001409static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001410 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001411{
1412 int rc;
1413 unsigned long val, change_mask;
1414 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001415 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001416
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001417 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001418 if (rc != X86EMUL_CONTINUE)
1419 return rc;
1420
1421 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1422 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1423
1424 switch(ctxt->mode) {
1425 case X86EMUL_MODE_PROT64:
1426 case X86EMUL_MODE_PROT32:
1427 case X86EMUL_MODE_PROT16:
1428 if (cpl == 0)
1429 change_mask |= EFLG_IOPL;
1430 if (cpl <= iopl)
1431 change_mask |= EFLG_IF;
1432 break;
1433 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001434 if (iopl < 3)
1435 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001436 change_mask |= EFLG_IF;
1437 break;
1438 default: /* real mode */
1439 change_mask |= (EFLG_IOPL | EFLG_IF);
1440 break;
1441 }
1442
1443 *(unsigned long *)dest =
1444 (ctxt->eflags & ~change_mask) | (val & change_mask);
1445
1446 return rc;
1447}
1448
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001449static int em_popf(struct x86_emulate_ctxt *ctxt)
1450{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001451 ctxt->dst.type = OP_REG;
1452 ctxt->dst.addr.reg = &ctxt->eflags;
1453 ctxt->dst.bytes = ctxt->op_bytes;
1454 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001455}
1456
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001457static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001458{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001459 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001460
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001461 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001462}
1463
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001464static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001465{
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001466 unsigned long selector;
1467 int rc;
1468
Avi Kivity9dac77f2011-06-01 15:34:25 +03001469 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001470 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001471 return rc;
1472
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001473 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001474 return rc;
1475}
1476
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001477static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001478{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001479 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001480 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001481 int reg = VCPU_REGS_RAX;
1482
1483 while (reg <= VCPU_REGS_RDI) {
1484 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001485 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001486
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001487 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001488 if (rc != X86EMUL_CONTINUE)
1489 return rc;
1490
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001491 ++reg;
1492 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001493
Wei Yongjunc37eda12010-06-15 09:03:33 +08001494 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001495}
1496
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001497static int em_pushf(struct x86_emulate_ctxt *ctxt)
1498{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001499 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001500 return em_push(ctxt);
1501}
1502
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001503static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001504{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001505 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001506 int reg = VCPU_REGS_RDI;
1507
1508 while (reg >= VCPU_REGS_RAX) {
1509 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001510 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1511 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001512 --reg;
1513 }
1514
Avi Kivity9dac77f2011-06-01 15:34:25 +03001515 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001516 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001517 break;
1518 --reg;
1519 }
1520 return rc;
1521}
1522
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001523int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001524{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001525 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001526 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001527 struct desc_ptr dt;
1528 gva_t cs_addr;
1529 gva_t eip_addr;
1530 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001531
1532 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001533 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001534 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001535 if (rc != X86EMUL_CONTINUE)
1536 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001537
1538 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1539
Avi Kivity9dac77f2011-06-01 15:34:25 +03001540 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001541 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001542 if (rc != X86EMUL_CONTINUE)
1543 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001544
Avi Kivity9dac77f2011-06-01 15:34:25 +03001545 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001546 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001547 if (rc != X86EMUL_CONTINUE)
1548 return rc;
1549
Avi Kivity4bff1e862011-04-20 13:37:53 +03001550 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001551
1552 eip_addr = dt.address + (irq << 2);
1553 cs_addr = dt.address + (irq << 2) + 2;
1554
Avi Kivity0f65dd72011-04-20 13:37:53 +03001555 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001556 if (rc != X86EMUL_CONTINUE)
1557 return rc;
1558
Avi Kivity0f65dd72011-04-20 13:37:53 +03001559 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001560 if (rc != X86EMUL_CONTINUE)
1561 return rc;
1562
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001563 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001564 if (rc != X86EMUL_CONTINUE)
1565 return rc;
1566
Avi Kivity9dac77f2011-06-01 15:34:25 +03001567 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001568
1569 return rc;
1570}
1571
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001572static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001573{
1574 switch(ctxt->mode) {
1575 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001576 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001577 case X86EMUL_MODE_VM86:
1578 case X86EMUL_MODE_PROT16:
1579 case X86EMUL_MODE_PROT32:
1580 case X86EMUL_MODE_PROT64:
1581 default:
1582 /* Protected mode interrupts unimplemented yet */
1583 return X86EMUL_UNHANDLEABLE;
1584 }
1585}
1586
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001587static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001588{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001589 int rc = X86EMUL_CONTINUE;
1590 unsigned long temp_eip = 0;
1591 unsigned long temp_eflags = 0;
1592 unsigned long cs = 0;
1593 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1594 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1595 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1596 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1597
1598 /* TODO: Add stack limit check */
1599
Avi Kivity9dac77f2011-06-01 15:34:25 +03001600 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001601
1602 if (rc != X86EMUL_CONTINUE)
1603 return rc;
1604
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001605 if (temp_eip & ~0xffff)
1606 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001607
Avi Kivity9dac77f2011-06-01 15:34:25 +03001608 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001609
1610 if (rc != X86EMUL_CONTINUE)
1611 return rc;
1612
Avi Kivity9dac77f2011-06-01 15:34:25 +03001613 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001614
1615 if (rc != X86EMUL_CONTINUE)
1616 return rc;
1617
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001618 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001619
1620 if (rc != X86EMUL_CONTINUE)
1621 return rc;
1622
Avi Kivity9dac77f2011-06-01 15:34:25 +03001623 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001624
1625
Avi Kivity9dac77f2011-06-01 15:34:25 +03001626 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001627 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001628 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001629 ctxt->eflags &= ~0xffff;
1630 ctxt->eflags |= temp_eflags;
1631 }
1632
1633 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1634 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1635
1636 return rc;
1637}
1638
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001639static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001640{
1641 switch(ctxt->mode) {
1642 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001643 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001644 case X86EMUL_MODE_VM86:
1645 case X86EMUL_MODE_PROT16:
1646 case X86EMUL_MODE_PROT32:
1647 case X86EMUL_MODE_PROT64:
1648 default:
1649 /* iret from protected mode unimplemented yet */
1650 return X86EMUL_UNHANDLEABLE;
1651 }
1652}
1653
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001654static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1655{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001656 int rc;
1657 unsigned short sel;
1658
Avi Kivity9dac77f2011-06-01 15:34:25 +03001659 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001660
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001661 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001662 if (rc != X86EMUL_CONTINUE)
1663 return rc;
1664
Avi Kivity9dac77f2011-06-01 15:34:25 +03001665 ctxt->_eip = 0;
1666 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001667 return X86EMUL_CONTINUE;
1668}
1669
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001670static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001671{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001672 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001673}
1674
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001675static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001676{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001677 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001678 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001679 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001680 break;
1681 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001682 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 break;
1684 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001685 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001686 break;
1687 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001688 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001689 break;
1690 case 4: /* sal/shl */
1691 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001692 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001693 break;
1694 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001695 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696 break;
1697 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001698 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699 break;
1700 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001701 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001702}
1703
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001704static int em_grp3(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001705{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001706 unsigned long *rax = &ctxt->regs[VCPU_REGS_RAX];
1707 unsigned long *rdx = &ctxt->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001708 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001709
Avi Kivity9dac77f2011-06-01 15:34:25 +03001710 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001711 case 0 ... 1: /* test */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001712 emulate_2op_SrcV(ctxt, "test");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001713 break;
1714 case 2: /* not */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001715 ctxt->dst.val = ~ctxt->dst.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716 break;
1717 case 3: /* neg */
Avi Kivityd1eef452011-09-07 16:41:38 +03001718 emulate_1op(ctxt, "neg");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001719 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001720 case 4: /* mul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001721 emulate_1op_rax_rdx("mul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001722 break;
1723 case 5: /* imul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001724 emulate_1op_rax_rdx("imul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001725 break;
1726 case 6: /* div */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001727 emulate_1op_rax_rdx_ex("div", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001728 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001729 break;
1730 case 7: /* idiv */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001731 emulate_1op_rax_rdx_ex("idiv", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001732 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001733 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001734 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001735 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001736 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001737 if (de)
1738 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001739 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001740}
1741
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001742static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001744 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745
Avi Kivity9dac77f2011-06-01 15:34:25 +03001746 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001748 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001749 break;
1750 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001751 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001752 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001753 case 2: /* call near abs */ {
1754 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001755 old_eip = ctxt->_eip;
1756 ctxt->_eip = ctxt->src.val;
1757 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001758 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001759 break;
1760 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001762 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001764 case 5: /* jmp far */
1765 rc = em_jmp_far(ctxt);
1766 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001768 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001769 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001771 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772}
1773
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001774static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001776 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777
Avi Kivity9dac77f2011-06-01 15:34:25 +03001778 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1779 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1780 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1781 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001782 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001783 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001784 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1785 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001786
Laurent Vivier05f086f2007-09-24 11:10:55 +02001787 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001789 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001790}
1791
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001792static int em_ret(struct x86_emulate_ctxt *ctxt)
1793{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001794 ctxt->dst.type = OP_REG;
1795 ctxt->dst.addr.reg = &ctxt->_eip;
1796 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001797 return em_pop(ctxt);
1798}
1799
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001800static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001801{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001802 int rc;
1803 unsigned long cs;
1804
Avi Kivity9dac77f2011-06-01 15:34:25 +03001805 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001806 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001807 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001808 if (ctxt->op_bytes == 4)
1809 ctxt->_eip = (u32)ctxt->_eip;
1810 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001811 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001812 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001813 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001814 return rc;
1815}
1816
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001817static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001818{
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001819 unsigned short sel;
1820 int rc;
1821
Avi Kivity9dac77f2011-06-01 15:34:25 +03001822 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001823
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001824 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001825 if (rc != X86EMUL_CONTINUE)
1826 return rc;
1827
Avi Kivity9dac77f2011-06-01 15:34:25 +03001828 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001829 return rc;
1830}
1831
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001832static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001833setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001834 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001835{
Avi Kivity1aa36612011-04-27 13:20:30 +03001836 u16 selector;
1837
Gleb Natapov79168fd2010-04-28 19:15:30 +03001838 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001839 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001840 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001841
1842 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001844 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001845 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001846 cs->type = 0x0b; /* Read, Execute, Accessed */
1847 cs->s = 1;
1848 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001849 cs->p = 1;
1850 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001851
Gleb Natapov79168fd2010-04-28 19:15:30 +03001852 set_desc_base(ss, 0); /* flat segment */
1853 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001854 ss->g = 1; /* 4kb granularity */
1855 ss->s = 1;
1856 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001857 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001858 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001859 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001860}
1861
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001862static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001863{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001864 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001865 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001866 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001867 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001868 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001869
1870 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001871 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001872 ctxt->mode == X86EMUL_MODE_VM86)
1873 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001874
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001875 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001876 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877
Avi Kivity717746e2011-04-20 13:37:53 +03001878 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001880 cs_sel = (u16)(msr_data & 0xfffc);
1881 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001882
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001883 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001884 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885 cs.l = 1;
1886 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001887 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1888 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001889
Avi Kivity9dac77f2011-06-01 15:34:25 +03001890 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001891 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001892#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001893 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001894
Avi Kivity717746e2011-04-20 13:37:53 +03001895 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001896 ctxt->mode == X86EMUL_MODE_PROT64 ?
1897 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001898 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001899
Avi Kivity717746e2011-04-20 13:37:53 +03001900 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001901 ctxt->eflags &= ~(msr_data | EFLG_RF);
1902#endif
1903 } else {
1904 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001905 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001906 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001907
1908 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1909 }
1910
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001911 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001912}
1913
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001914static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001915{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001916 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001917 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001918 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001919 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001920 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001921
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001922 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001923 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001924 if (ctxt->mode == X86EMUL_MODE_REAL)
1925 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001926
1927 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1928 * Therefore, we inject an #UD.
1929 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001930 if (ctxt->mode == X86EMUL_MODE_PROT64)
1931 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001932
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001933 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001934
Avi Kivity717746e2011-04-20 13:37:53 +03001935 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001936 switch (ctxt->mode) {
1937 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001938 if ((msr_data & 0xfffc) == 0x0)
1939 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001940 break;
1941 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001942 if (msr_data == 0x0)
1943 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001944 break;
1945 }
1946
1947 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001948 cs_sel = (u16)msr_data;
1949 cs_sel &= ~SELECTOR_RPL_MASK;
1950 ss_sel = cs_sel + 8;
1951 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001952 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001953 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001954 cs.l = 1;
1955 }
1956
Avi Kivity1aa36612011-04-27 13:20:30 +03001957 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1958 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001959
Avi Kivity717746e2011-04-20 13:37:53 +03001960 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001961 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001962
Avi Kivity717746e2011-04-20 13:37:53 +03001963 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001964 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001965
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001966 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001967}
1968
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001969static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001970{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001971 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001972 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001973 u64 msr_data;
1974 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001975 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001976
Gleb Natapova0044752010-02-10 14:21:31 +02001977 /* inject #GP if in real mode or Virtual 8086 mode */
1978 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001979 ctxt->mode == X86EMUL_MODE_VM86)
1980 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001981
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001982 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001983
Avi Kivity9dac77f2011-06-01 15:34:25 +03001984 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02001985 usermode = X86EMUL_MODE_PROT64;
1986 else
1987 usermode = X86EMUL_MODE_PROT32;
1988
1989 cs.dpl = 3;
1990 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03001991 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001992 switch (usermode) {
1993 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001994 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001995 if ((msr_data & 0xfffc) == 0x0)
1996 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001997 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001998 break;
1999 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002000 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002001 if (msr_data == 0x0)
2002 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002003 ss_sel = cs_sel + 8;
2004 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002005 cs.l = 1;
2006 break;
2007 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002008 cs_sel |= SELECTOR_RPL_MASK;
2009 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002010
Avi Kivity1aa36612011-04-27 13:20:30 +03002011 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2012 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002013
Avi Kivity9dac77f2011-06-01 15:34:25 +03002014 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2015 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002016
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002017 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002018}
2019
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002020static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002021{
2022 int iopl;
2023 if (ctxt->mode == X86EMUL_MODE_REAL)
2024 return false;
2025 if (ctxt->mode == X86EMUL_MODE_VM86)
2026 return true;
2027 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002028 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002029}
2030
2031static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002032 u16 port, u16 len)
2033{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002034 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002035 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002036 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002037 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002038 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002039 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002040 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002041
Avi Kivity1aa36612011-04-27 13:20:30 +03002042 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002043 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002044 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002045 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002046 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002047 base = get_desc_base(&tr_seg);
2048#ifdef CONFIG_X86_64
2049 base |= ((u64)base3) << 32;
2050#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002051 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002052 if (r != X86EMUL_CONTINUE)
2053 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002054 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002055 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002056 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002057 if (r != X86EMUL_CONTINUE)
2058 return false;
2059 if ((perm >> bit_idx) & mask)
2060 return false;
2061 return true;
2062}
2063
2064static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002065 u16 port, u16 len)
2066{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002067 if (ctxt->perm_ok)
2068 return true;
2069
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002070 if (emulator_bad_iopl(ctxt))
2071 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002072 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002073
2074 ctxt->perm_ok = true;
2075
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002076 return true;
2077}
2078
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002079static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002080 struct tss_segment_16 *tss)
2081{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002082 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002083 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002084 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2085 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2086 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2087 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2088 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2089 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2090 tss->si = ctxt->regs[VCPU_REGS_RSI];
2091 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002092
Avi Kivity1aa36612011-04-27 13:20:30 +03002093 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2094 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2095 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2096 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2097 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002098}
2099
2100static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002101 struct tss_segment_16 *tss)
2102{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002103 int ret;
2104
Avi Kivity9dac77f2011-06-01 15:34:25 +03002105 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002106 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002107 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2108 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2109 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2110 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2111 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2112 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2113 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2114 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002115
2116 /*
2117 * SDM says that segment selectors are loaded before segment
2118 * descriptors
2119 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002120 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2121 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2122 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2123 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2124 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002125
2126 /*
2127 * Now load segment descriptors. If fault happenes at this stage
2128 * it is handled in a context of new task
2129 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002130 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002131 if (ret != X86EMUL_CONTINUE)
2132 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002133 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002134 if (ret != X86EMUL_CONTINUE)
2135 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002136 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002137 if (ret != X86EMUL_CONTINUE)
2138 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002139 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002140 if (ret != X86EMUL_CONTINUE)
2141 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002142 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002143 if (ret != X86EMUL_CONTINUE)
2144 return ret;
2145
2146 return X86EMUL_CONTINUE;
2147}
2148
2149static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002150 u16 tss_selector, u16 old_tss_sel,
2151 ulong old_tss_base, struct desc_struct *new_desc)
2152{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002153 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154 struct tss_segment_16 tss_seg;
2155 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002156 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002157
Avi Kivity0f65dd72011-04-20 13:37:53 +03002158 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002159 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002160 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002161 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002164 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165
Avi Kivity0f65dd72011-04-20 13:37:53 +03002166 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002167 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002168 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002169 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002170 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002171
Avi Kivity0f65dd72011-04-20 13:37:53 +03002172 ret = ops->read_std(ctxt, new_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
2178 if (old_tss_sel != 0xffff) {
2179 tss_seg.prev_task_link = old_tss_sel;
2180
Avi Kivity0f65dd72011-04-20 13:37:53 +03002181 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002182 &tss_seg.prev_task_link,
2183 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002184 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002185 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002186 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002187 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002188 }
2189
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002190 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002191}
2192
2193static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002194 struct tss_segment_32 *tss)
2195{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002196 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002197 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002198 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002199 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2200 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2201 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2202 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2203 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2204 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2205 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2206 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002207
Avi Kivity1aa36612011-04-27 13:20:30 +03002208 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2209 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2210 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2211 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2212 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2213 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2214 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002215}
2216
2217static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218 struct tss_segment_32 *tss)
2219{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002220 int ret;
2221
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002222 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002223 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002224 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002225 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002226 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2227 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2228 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2229 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2230 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2231 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2232 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2233 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002234
2235 /*
2236 * SDM says that segment selectors are loaded before segment
2237 * descriptors
2238 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002239 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2240 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2241 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2242 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2243 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2244 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2245 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002246
2247 /*
2248 * Now load segment descriptors. If fault happenes at this stage
2249 * it is handled in a context of new task
2250 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002251 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002252 if (ret != X86EMUL_CONTINUE)
2253 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002254 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002255 if (ret != X86EMUL_CONTINUE)
2256 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002257 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002258 if (ret != X86EMUL_CONTINUE)
2259 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002260 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002261 if (ret != X86EMUL_CONTINUE)
2262 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002263 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002264 if (ret != X86EMUL_CONTINUE)
2265 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002266 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002267 if (ret != X86EMUL_CONTINUE)
2268 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002269 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002270 if (ret != X86EMUL_CONTINUE)
2271 return ret;
2272
2273 return X86EMUL_CONTINUE;
2274}
2275
2276static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002277 u16 tss_selector, u16 old_tss_sel,
2278 ulong old_tss_base, struct desc_struct *new_desc)
2279{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002280 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002281 struct tss_segment_32 tss_seg;
2282 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002283 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002284
Avi Kivity0f65dd72011-04-20 13:37:53 +03002285 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002286 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002287 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002288 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002289 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002290
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002291 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292
Avi Kivity0f65dd72011-04-20 13:37:53 +03002293 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002294 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002295 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002296 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002297 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298
Avi Kivity0f65dd72011-04-20 13:37:53 +03002299 ret = ops->read_std(ctxt, new_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
2305 if (old_tss_sel != 0xffff) {
2306 tss_seg.prev_task_link = old_tss_sel;
2307
Avi Kivity0f65dd72011-04-20 13:37:53 +03002308 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002309 &tss_seg.prev_task_link,
2310 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002311 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002312 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002313 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002314 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002315 }
2316
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002317 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318}
2319
2320static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002321 u16 tss_selector, int reason,
2322 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002323{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002324 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002325 struct desc_struct curr_tss_desc, next_tss_desc;
2326 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002327 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002329 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002330 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002331
2332 /* FIXME: old_tss_base == ~0 ? */
2333
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002334 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002335 if (ret != X86EMUL_CONTINUE)
2336 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002337 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002338 if (ret != X86EMUL_CONTINUE)
2339 return ret;
2340
2341 /* FIXME: check that next_tss_desc is tss */
2342
2343 if (reason != TASK_SWITCH_IRET) {
2344 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002345 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002346 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002347 }
2348
Gleb Natapovceffb452010-03-18 15:20:19 +02002349 desc_limit = desc_limit_scaled(&next_tss_desc);
2350 if (!next_tss_desc.p ||
2351 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2352 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002353 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002354 return X86EMUL_PROPAGATE_FAULT;
2355 }
2356
2357 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2358 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002359 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002360 }
2361
2362 if (reason == TASK_SWITCH_IRET)
2363 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2364
2365 /* set back link to prev task only if NT bit is set in eflags
2366 note that old_tss_sel is not used afetr this point */
2367 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2368 old_tss_sel = 0xffff;
2369
2370 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002371 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002372 old_tss_base, &next_tss_desc);
2373 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002374 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002376 if (ret != X86EMUL_CONTINUE)
2377 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002378
2379 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2380 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2381
2382 if (reason != TASK_SWITCH_IRET) {
2383 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002384 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002385 }
2386
Avi Kivity717746e2011-04-20 13:37:53 +03002387 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002388 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002389
Jan Kiszkae269fb22010-04-14 15:51:09 +02002390 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002391 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2392 ctxt->lock_prefix = 0;
2393 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002394 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002395 }
2396
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397 return ret;
2398}
2399
2400int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002401 u16 tss_selector, int reason,
2402 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002403{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002404 int rc;
2405
Avi Kivity9dac77f2011-06-01 15:34:25 +03002406 ctxt->_eip = ctxt->eip;
2407 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002409 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002410 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002412 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002413 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002414
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002415 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416}
2417
Avi Kivity90de84f2010-11-17 15:28:21 +02002418static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002419 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002420{
Gleb Natapova682e352010-03-18 15:20:21 +02002421 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2422
Avi Kivity9dac77f2011-06-01 15:34:25 +03002423 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2424 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002425 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002426}
2427
Avi Kivity7af04fc2010-08-18 14:16:35 +03002428static int em_das(struct x86_emulate_ctxt *ctxt)
2429{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002430 u8 al, old_al;
2431 bool af, cf, old_cf;
2432
2433 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002434 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002435
2436 old_al = al;
2437 old_cf = cf;
2438 cf = false;
2439 af = ctxt->eflags & X86_EFLAGS_AF;
2440 if ((al & 0x0f) > 9 || af) {
2441 al -= 6;
2442 cf = old_cf | (al >= 250);
2443 af = true;
2444 } else {
2445 af = false;
2446 }
2447 if (old_al > 0x99 || old_cf) {
2448 al -= 0x60;
2449 cf = true;
2450 }
2451
Avi Kivity9dac77f2011-06-01 15:34:25 +03002452 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002453 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002454 ctxt->src.type = OP_IMM;
2455 ctxt->src.val = 0;
2456 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002457 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002458 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2459 if (cf)
2460 ctxt->eflags |= X86_EFLAGS_CF;
2461 if (af)
2462 ctxt->eflags |= X86_EFLAGS_AF;
2463 return X86EMUL_CONTINUE;
2464}
2465
Avi Kivity0ef753b2010-08-18 14:51:45 +03002466static int em_call_far(struct x86_emulate_ctxt *ctxt)
2467{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002468 u16 sel, old_cs;
2469 ulong old_eip;
2470 int rc;
2471
Avi Kivity1aa36612011-04-27 13:20:30 +03002472 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002473 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002474
Avi Kivity9dac77f2011-06-01 15:34:25 +03002475 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002476 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002477 return X86EMUL_CONTINUE;
2478
Avi Kivity9dac77f2011-06-01 15:34:25 +03002479 ctxt->_eip = 0;
2480 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002481
Avi Kivity9dac77f2011-06-01 15:34:25 +03002482 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002483 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002484 if (rc != X86EMUL_CONTINUE)
2485 return rc;
2486
Avi Kivity9dac77f2011-06-01 15:34:25 +03002487 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002488 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002489}
2490
Avi Kivity40ece7c2010-08-18 15:12:09 +03002491static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2492{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002493 int rc;
2494
Avi Kivity9dac77f2011-06-01 15:34:25 +03002495 ctxt->dst.type = OP_REG;
2496 ctxt->dst.addr.reg = &ctxt->_eip;
2497 ctxt->dst.bytes = ctxt->op_bytes;
2498 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002499 if (rc != X86EMUL_CONTINUE)
2500 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002501 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002502 return X86EMUL_CONTINUE;
2503}
2504
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002505static int em_add(struct x86_emulate_ctxt *ctxt)
2506{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002507 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002508 return X86EMUL_CONTINUE;
2509}
2510
2511static int em_or(struct x86_emulate_ctxt *ctxt)
2512{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002513 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002514 return X86EMUL_CONTINUE;
2515}
2516
2517static int em_adc(struct x86_emulate_ctxt *ctxt)
2518{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002519 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002520 return X86EMUL_CONTINUE;
2521}
2522
2523static int em_sbb(struct x86_emulate_ctxt *ctxt)
2524{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002525 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002526 return X86EMUL_CONTINUE;
2527}
2528
2529static int em_and(struct x86_emulate_ctxt *ctxt)
2530{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002531 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002532 return X86EMUL_CONTINUE;
2533}
2534
2535static int em_sub(struct x86_emulate_ctxt *ctxt)
2536{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002537 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002538 return X86EMUL_CONTINUE;
2539}
2540
2541static int em_xor(struct x86_emulate_ctxt *ctxt)
2542{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002543 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002544 return X86EMUL_CONTINUE;
2545}
2546
2547static int em_cmp(struct x86_emulate_ctxt *ctxt)
2548{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002549 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002550 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002551 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002552 return X86EMUL_CONTINUE;
2553}
2554
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002555static int em_test(struct x86_emulate_ctxt *ctxt)
2556{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002557 emulate_2op_SrcV(ctxt, "test");
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002558 return X86EMUL_CONTINUE;
2559}
2560
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002561static int em_xchg(struct x86_emulate_ctxt *ctxt)
2562{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002563 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002564 ctxt->src.val = ctxt->dst.val;
2565 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002566
2567 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002568 ctxt->dst.val = ctxt->src.orig_val;
2569 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002570 return X86EMUL_CONTINUE;
2571}
2572
Avi Kivity5c82aa22010-08-18 18:31:43 +03002573static int em_imul(struct x86_emulate_ctxt *ctxt)
2574{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002575 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002576 return X86EMUL_CONTINUE;
2577}
2578
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002579static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2580{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002581 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002582 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002583}
2584
Avi Kivity61429142010-08-19 15:13:00 +03002585static int em_cwd(struct x86_emulate_ctxt *ctxt)
2586{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002587 ctxt->dst.type = OP_REG;
2588 ctxt->dst.bytes = ctxt->src.bytes;
2589 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2590 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002591
2592 return X86EMUL_CONTINUE;
2593}
2594
Avi Kivity48bb5d32010-08-18 18:54:34 +03002595static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2596{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002597 u64 tsc = 0;
2598
Avi Kivity717746e2011-04-20 13:37:53 +03002599 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002600 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2601 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002602 return X86EMUL_CONTINUE;
2603}
2604
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002605static int em_mov(struct x86_emulate_ctxt *ctxt)
2606{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002607 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002608 return X86EMUL_CONTINUE;
2609}
2610
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002611static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2612{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002613 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002614 return emulate_ud(ctxt);
2615
Avi Kivity9dac77f2011-06-01 15:34:25 +03002616 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002617 return X86EMUL_CONTINUE;
2618}
2619
2620static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2621{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002622 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002623
Avi Kivity9dac77f2011-06-01 15:34:25 +03002624 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002625 return emulate_ud(ctxt);
2626
Avi Kivity9dac77f2011-06-01 15:34:25 +03002627 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002628 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2629
2630 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002631 ctxt->dst.type = OP_NONE;
2632 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002633}
2634
Avi Kivityaa97bb42010-01-20 18:09:23 +02002635static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2636{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002637 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002638 return X86EMUL_CONTINUE;
2639}
2640
Avi Kivity38503912011-03-31 18:48:09 +02002641static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2642{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002643 int rc;
2644 ulong linear;
2645
Avi Kivity9dac77f2011-06-01 15:34:25 +03002646 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002647 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002648 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002649 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002650 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002651 return X86EMUL_CONTINUE;
2652}
2653
Avi Kivity2d04a052011-04-20 15:32:49 +03002654static int em_clts(struct x86_emulate_ctxt *ctxt)
2655{
2656 ulong cr0;
2657
2658 cr0 = ctxt->ops->get_cr(ctxt, 0);
2659 cr0 &= ~X86_CR0_TS;
2660 ctxt->ops->set_cr(ctxt, 0, cr0);
2661 return X86EMUL_CONTINUE;
2662}
2663
Avi Kivity26d05cc2011-04-21 12:07:59 +03002664static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2665{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002666 int rc;
2667
Avi Kivity9dac77f2011-06-01 15:34:25 +03002668 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002669 return X86EMUL_UNHANDLEABLE;
2670
2671 rc = ctxt->ops->fix_hypercall(ctxt);
2672 if (rc != X86EMUL_CONTINUE)
2673 return rc;
2674
2675 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002676 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002677 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002678 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002679 return X86EMUL_CONTINUE;
2680}
2681
2682static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2683{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002684 struct desc_ptr desc_ptr;
2685 int rc;
2686
Avi Kivity9dac77f2011-06-01 15:34:25 +03002687 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002688 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002689 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002690 if (rc != X86EMUL_CONTINUE)
2691 return rc;
2692 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2693 /* 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
Avi Kivity5ef39c72011-04-21 12:21:50 +03002698static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002699{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002700 int rc;
2701
Avi Kivity5ef39c72011-04-21 12:21:50 +03002702 rc = ctxt->ops->fix_hypercall(ctxt);
2703
Avi Kivity26d05cc2011-04-21 12:07:59 +03002704 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002705 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002706 return rc;
2707}
2708
2709static int em_lidt(struct x86_emulate_ctxt *ctxt)
2710{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002711 struct desc_ptr desc_ptr;
2712 int rc;
2713
Avi Kivity9dac77f2011-06-01 15:34:25 +03002714 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002715 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002716 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002717 if (rc != X86EMUL_CONTINUE)
2718 return rc;
2719 ctxt->ops->set_idt(ctxt, &desc_ptr);
2720 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002721 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002722 return X86EMUL_CONTINUE;
2723}
2724
2725static int em_smsw(struct x86_emulate_ctxt *ctxt)
2726{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002727 ctxt->dst.bytes = 2;
2728 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002729 return X86EMUL_CONTINUE;
2730}
2731
2732static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2733{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002734 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002735 | (ctxt->src.val & 0x0f));
2736 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002737 return X86EMUL_CONTINUE;
2738}
2739
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002740static int em_loop(struct x86_emulate_ctxt *ctxt)
2741{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002742 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2743 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2744 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2745 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002746
2747 return X86EMUL_CONTINUE;
2748}
2749
2750static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2751{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002752 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2753 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002754
2755 return X86EMUL_CONTINUE;
2756}
2757
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002758static int em_cli(struct x86_emulate_ctxt *ctxt)
2759{
2760 if (emulator_bad_iopl(ctxt))
2761 return emulate_gp(ctxt, 0);
2762
2763 ctxt->eflags &= ~X86_EFLAGS_IF;
2764 return X86EMUL_CONTINUE;
2765}
2766
2767static int em_sti(struct x86_emulate_ctxt *ctxt)
2768{
2769 if (emulator_bad_iopl(ctxt))
2770 return emulate_gp(ctxt, 0);
2771
2772 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2773 ctxt->eflags |= X86_EFLAGS_IF;
2774 return X86EMUL_CONTINUE;
2775}
2776
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002777static bool valid_cr(int nr)
2778{
2779 switch (nr) {
2780 case 0:
2781 case 2 ... 4:
2782 case 8:
2783 return true;
2784 default:
2785 return false;
2786 }
2787}
2788
2789static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2790{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002791 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002792 return emulate_ud(ctxt);
2793
2794 return X86EMUL_CONTINUE;
2795}
2796
2797static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2798{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002799 u64 new_val = ctxt->src.val64;
2800 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002801 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002802
2803 static u64 cr_reserved_bits[] = {
2804 0xffffffff00000000ULL,
2805 0, 0, 0, /* CR3 checked later */
2806 CR4_RESERVED_BITS,
2807 0, 0, 0,
2808 CR8_RESERVED_BITS,
2809 };
2810
2811 if (!valid_cr(cr))
2812 return emulate_ud(ctxt);
2813
2814 if (new_val & cr_reserved_bits[cr])
2815 return emulate_gp(ctxt, 0);
2816
2817 switch (cr) {
2818 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002819 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002820 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2821 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2822 return emulate_gp(ctxt, 0);
2823
Avi Kivity717746e2011-04-20 13:37:53 +03002824 cr4 = ctxt->ops->get_cr(ctxt, 4);
2825 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002826
2827 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2828 !(cr4 & X86_CR4_PAE))
2829 return emulate_gp(ctxt, 0);
2830
2831 break;
2832 }
2833 case 3: {
2834 u64 rsvd = 0;
2835
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002836 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2837 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002838 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002839 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002840 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002841 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002842 rsvd = CR3_NONPAE_RESERVED_BITS;
2843
2844 if (new_val & rsvd)
2845 return emulate_gp(ctxt, 0);
2846
2847 break;
2848 }
2849 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002850 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002851
Avi Kivity717746e2011-04-20 13:37:53 +03002852 cr4 = ctxt->ops->get_cr(ctxt, 4);
2853 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002854
2855 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2856 return emulate_gp(ctxt, 0);
2857
2858 break;
2859 }
2860 }
2861
2862 return X86EMUL_CONTINUE;
2863}
2864
Joerg Roedel3b88e412011-04-04 12:39:29 +02002865static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2866{
2867 unsigned long dr7;
2868
Avi Kivity717746e2011-04-20 13:37:53 +03002869 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002870
2871 /* Check if DR7.Global_Enable is set */
2872 return dr7 & (1 << 13);
2873}
2874
2875static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2876{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002877 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002878 u64 cr4;
2879
2880 if (dr > 7)
2881 return emulate_ud(ctxt);
2882
Avi Kivity717746e2011-04-20 13:37:53 +03002883 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002884 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2885 return emulate_ud(ctxt);
2886
2887 if (check_dr7_gd(ctxt))
2888 return emulate_db(ctxt);
2889
2890 return X86EMUL_CONTINUE;
2891}
2892
2893static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2894{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002895 u64 new_val = ctxt->src.val64;
2896 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002897
2898 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2899 return emulate_gp(ctxt, 0);
2900
2901 return check_dr_read(ctxt);
2902}
2903
Joerg Roedel01de8b02011-04-04 12:39:31 +02002904static int check_svme(struct x86_emulate_ctxt *ctxt)
2905{
2906 u64 efer;
2907
Avi Kivity717746e2011-04-20 13:37:53 +03002908 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002909
2910 if (!(efer & EFER_SVME))
2911 return emulate_ud(ctxt);
2912
2913 return X86EMUL_CONTINUE;
2914}
2915
2916static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2917{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002918 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002919
2920 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002921 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002922 return emulate_gp(ctxt, 0);
2923
2924 return check_svme(ctxt);
2925}
2926
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002927static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2928{
Avi Kivity717746e2011-04-20 13:37:53 +03002929 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002930
Avi Kivity717746e2011-04-20 13:37:53 +03002931 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002932 return emulate_ud(ctxt);
2933
2934 return X86EMUL_CONTINUE;
2935}
2936
Joerg Roedel80612522011-04-04 12:39:33 +02002937static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2938{
Avi Kivity717746e2011-04-20 13:37:53 +03002939 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002940 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002941
Avi Kivity717746e2011-04-20 13:37:53 +03002942 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002943 (rcx > 3))
2944 return emulate_gp(ctxt, 0);
2945
2946 return X86EMUL_CONTINUE;
2947}
2948
Joerg Roedelf6511932011-04-04 12:39:35 +02002949static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2950{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002951 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2952 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002953 return emulate_gp(ctxt, 0);
2954
2955 return X86EMUL_CONTINUE;
2956}
2957
2958static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2959{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002960 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2961 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002962 return emulate_gp(ctxt, 0);
2963
2964 return X86EMUL_CONTINUE;
2965}
2966
Avi Kivity73fba5f2010-07-29 15:11:53 +03002967#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002968#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002969#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2970 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002971#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002972#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002973#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002974#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002975#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002976#define II(_f, _e, _i) \
2977 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002978#define IIP(_f, _e, _i, _p) \
2979 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2980 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002981#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002982
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002983#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002984#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002985#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2986
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002987#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2988 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2989 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002990
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002991static struct opcode group7_rm1[] = {
2992 DI(SrcNone | ModRM | Priv, monitor),
2993 DI(SrcNone | ModRM | Priv, mwait),
2994 N, N, N, N, N, N,
2995};
2996
Joerg Roedel01de8b02011-04-04 12:39:31 +02002997static struct opcode group7_rm3[] = {
2998 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03002999 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003000 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3001 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3002 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3003 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3004 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3005 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3006};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003007
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003008static struct opcode group7_rm7[] = {
3009 N,
3010 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3011 N, N, N, N, N, N,
3012};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003013
Avi Kivity73fba5f2010-07-29 15:11:53 +03003014static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003015 I(Lock, em_add),
3016 I(Lock, em_or),
3017 I(Lock, em_adc),
3018 I(Lock, em_sbb),
3019 I(Lock, em_and),
3020 I(Lock, em_sub),
3021 I(Lock, em_xor),
3022 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003023};
3024
3025static struct opcode group1A[] = {
3026 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3027};
3028
3029static struct opcode group3[] = {
3030 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
3031 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03003032 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003033};
3034
3035static struct opcode group4[] = {
3036 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3037 N, N, N, N, N, N,
3038};
3039
3040static struct opcode group5[] = {
3041 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003042 D(SrcMem | ModRM | Stack),
3043 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003044 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3045 D(SrcMem | ModRM | Stack), N,
3046};
3047
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003048static struct opcode group6[] = {
3049 DI(ModRM | Prot, sldt),
3050 DI(ModRM | Prot, str),
3051 DI(ModRM | Prot | Priv, lldt),
3052 DI(ModRM | Prot | Priv, ltr),
3053 N, N, N, N,
3054};
3055
Avi Kivity73fba5f2010-07-29 15:11:53 +03003056static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003057 DI(ModRM | Mov | DstMem | Priv, sgdt),
3058 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003059 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3060 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3061 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3062 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3063 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003064}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003065 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3066 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003067 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003068 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3069 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003070} };
3071
3072static struct opcode group8[] = {
3073 N, N, N, N,
3074 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3075 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3076};
3077
3078static struct group_dual group9 = { {
3079 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3080}, {
3081 N, N, N, N, N, N, N, N,
3082} };
3083
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003084static struct opcode group11[] = {
3085 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3086};
3087
Avi Kivityaa97bb42010-01-20 18:09:23 +02003088static struct gprefix pfx_0f_6f_0f_7f = {
3089 N, N, N, I(Sse, em_movdqu),
3090};
3091
Avi Kivity73fba5f2010-07-29 15:11:53 +03003092static struct opcode opcode_table[256] = {
3093 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003094 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003095 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3096 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003097 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003098 D(ImplicitOps | Stack | No64), N,
3099 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003100 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003101 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3102 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003103 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003104 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3105 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003106 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003107 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003108 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003109 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003110 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003111 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003112 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003113 /* 0x40 - 0x4F */
3114 X16(D(DstReg)),
3115 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003116 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003117 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003118 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003119 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003120 I(ImplicitOps | Stack | No64, em_pusha),
3121 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003122 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3123 N, N, N, N,
3124 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003125 I(SrcImm | Mov | Stack, em_push),
3126 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003127 I(SrcImmByte | Mov | Stack, em_push),
3128 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003129 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3130 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003131 /* 0x70 - 0x7F */
3132 X16(D(SrcImmByte)),
3133 /* 0x80 - 0x87 */
3134 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3135 G(DstMem | SrcImm | ModRM | Group, group1),
3136 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3137 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003138 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003139 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003140 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003141 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3142 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003143 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3144 D(ModRM | SrcMem | NoAccess | DstReg),
3145 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3146 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003147 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003148 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003149 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003150 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003151 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003152 II(ImplicitOps | Stack, em_pushf, pushf),
3153 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003154 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003155 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3156 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3157 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003158 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003159 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003160 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003161 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3162 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003163 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003164 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003165 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003166 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003167 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003168 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003169 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003170 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003171 I(ImplicitOps | Stack, em_ret),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003172 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003173 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003174 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003175 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003176 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003177 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003178 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003179 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003180 N, N, N, N,
3181 /* 0xD8 - 0xDF */
3182 N, N, N, N, N, N, N, N,
3183 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003184 X3(I(SrcImmByte, em_loop)),
3185 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003186 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3187 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003188 /* 0xE8 - 0xEF */
3189 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003190 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003191 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3192 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003193 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003194 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003195 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3196 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003197 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003198 D(ImplicitOps), D(ImplicitOps),
3199 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003200 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3201};
3202
3203static struct opcode twobyte_table[256] = {
3204 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003205 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003206 N, I(ImplicitOps | VendorSpecific, em_syscall),
3207 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003208 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003209 N, D(ImplicitOps | ModRM), N, N,
3210 /* 0x10 - 0x1F */
3211 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3212 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003213 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003214 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003215 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003216 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003217 N, N, N, N,
3218 N, N, N, N, N, N, N, N,
3219 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003220 DI(ImplicitOps | Priv, wrmsr),
3221 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3222 DI(ImplicitOps | Priv, rdmsr),
3223 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003224 I(ImplicitOps | VendorSpecific, em_sysenter),
3225 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003226 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003227 N, N, N, N, N, N, N, N,
3228 /* 0x40 - 0x4F */
3229 X16(D(DstReg | SrcMem | ModRM | Mov)),
3230 /* 0x50 - 0x5F */
3231 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3232 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003233 N, N, N, N,
3234 N, N, N, N,
3235 N, N, N, N,
3236 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003237 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003238 N, N, N, N,
3239 N, N, N, N,
3240 N, N, N, N,
3241 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003242 /* 0x80 - 0x8F */
3243 X16(D(SrcImm)),
3244 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003245 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003246 /* 0xA0 - 0xA7 */
3247 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003248 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003249 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3250 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3251 /* 0xA8 - 0xAF */
3252 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003253 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003254 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3255 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003256 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003257 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003258 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003259 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3260 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3261 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003262 /* 0xB8 - 0xBF */
3263 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003264 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003265 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3266 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003267 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003268 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a52010-08-17 09:19:34 +08003269 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003270 N, N, N, GD(0, &group9),
3271 N, N, N, N, N, N, N, N,
3272 /* 0xD0 - 0xDF */
3273 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3274 /* 0xE0 - 0xEF */
3275 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3276 /* 0xF0 - 0xFF */
3277 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3278};
3279
3280#undef D
3281#undef N
3282#undef G
3283#undef GD
3284#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003285#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003286#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003287
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003288#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003289#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003290#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003291#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003292
Avi Kivity9dac77f2011-06-01 15:34:25 +03003293static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003294{
3295 unsigned size;
3296
Avi Kivity9dac77f2011-06-01 15:34:25 +03003297 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003298 if (size == 8)
3299 size = 4;
3300 return size;
3301}
3302
3303static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3304 unsigned size, bool sign_extension)
3305{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003306 int rc = X86EMUL_CONTINUE;
3307
3308 op->type = OP_IMM;
3309 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003310 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003311 /* NB. Immediates are sign-extended as necessary. */
3312 switch (op->bytes) {
3313 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003314 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003315 break;
3316 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003317 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003318 break;
3319 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003320 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003321 break;
3322 }
3323 if (!sign_extension) {
3324 switch (op->bytes) {
3325 case 1:
3326 op->val &= 0xff;
3327 break;
3328 case 2:
3329 op->val &= 0xffff;
3330 break;
3331 case 4:
3332 op->val &= 0xffffffff;
3333 break;
3334 }
3335 }
3336done:
3337 return rc;
3338}
3339
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003340int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003341{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003342 int rc = X86EMUL_CONTINUE;
3343 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003344 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003345 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003346 struct opcode opcode;
Avi Kivitycb16c342011-06-19 19:21:11 +03003347 struct operand memop = { .type = OP_NONE }, *memopp = NULL;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003348
Avi Kivity9dac77f2011-06-01 15:34:25 +03003349 ctxt->_eip = ctxt->eip;
3350 ctxt->fetch.start = ctxt->_eip;
3351 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003352 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003353 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003354
3355 switch (mode) {
3356 case X86EMUL_MODE_REAL:
3357 case X86EMUL_MODE_VM86:
3358 case X86EMUL_MODE_PROT16:
3359 def_op_bytes = def_ad_bytes = 2;
3360 break;
3361 case X86EMUL_MODE_PROT32:
3362 def_op_bytes = def_ad_bytes = 4;
3363 break;
3364#ifdef CONFIG_X86_64
3365 case X86EMUL_MODE_PROT64:
3366 def_op_bytes = 4;
3367 def_ad_bytes = 8;
3368 break;
3369#endif
3370 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003371 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003372 }
3373
Avi Kivity9dac77f2011-06-01 15:34:25 +03003374 ctxt->op_bytes = def_op_bytes;
3375 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003376
3377 /* Legacy prefixes. */
3378 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003379 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003380 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003381 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003382 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003383 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003384 break;
3385 case 0x67: /* address-size override */
3386 if (mode == X86EMUL_MODE_PROT64)
3387 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003388 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003389 else
3390 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003391 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003392 break;
3393 case 0x26: /* ES override */
3394 case 0x2e: /* CS override */
3395 case 0x36: /* SS override */
3396 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003397 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003398 break;
3399 case 0x64: /* FS override */
3400 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003401 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003402 break;
3403 case 0x40 ... 0x4f: /* REX */
3404 if (mode != X86EMUL_MODE_PROT64)
3405 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003406 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003407 continue;
3408 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003409 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003410 break;
3411 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003412 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003413 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003414 break;
3415 default:
3416 goto done_prefixes;
3417 }
3418
3419 /* Any legacy prefix after a REX prefix nullifies its effect. */
3420
Avi Kivity9dac77f2011-06-01 15:34:25 +03003421 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003422 }
3423
3424done_prefixes:
3425
3426 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003427 if (ctxt->rex_prefix & 8)
3428 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003429
3430 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003431 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003432 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003433 if (ctxt->b == 0x0f) {
3434 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003435 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003437 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003438 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003439
Avi Kivity9dac77f2011-06-01 15:34:25 +03003440 while (ctxt->d & GroupMask) {
3441 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003442 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003443 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003444 --ctxt->_eip;
3445 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003446 opcode = opcode.u.group[goffset];
3447 break;
3448 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003449 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003450 --ctxt->_eip;
3451 goffset = (ctxt->modrm >> 3) & 7;
3452 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003453 opcode = opcode.u.gdual->mod3[goffset];
3454 else
3455 opcode = opcode.u.gdual->mod012[goffset];
3456 break;
3457 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003458 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003459 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003460 break;
3461 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003462 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003463 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003464 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003465 switch (simd_prefix) {
3466 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3467 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3468 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3469 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3470 }
3471 break;
3472 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003473 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003474 }
Avi Kivity46561642011-04-24 14:09:59 +03003475
Avi Kivity9dac77f2011-06-01 15:34:25 +03003476 ctxt->d &= ~GroupMask;
3477 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003478 }
3479
Avi Kivity9dac77f2011-06-01 15:34:25 +03003480 ctxt->execute = opcode.u.execute;
3481 ctxt->check_perm = opcode.check_perm;
3482 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003483
3484 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003485 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003486 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003487
Avi Kivity9dac77f2011-06-01 15:34:25 +03003488 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003489 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003490
Avi Kivity9dac77f2011-06-01 15:34:25 +03003491 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3492 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003493
Avi Kivity9dac77f2011-06-01 15:34:25 +03003494 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003495 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003496 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003497 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003498 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003499 }
3500
Avi Kivity9dac77f2011-06-01 15:34:25 +03003501 if (ctxt->d & Sse)
3502 ctxt->op_bytes = 16;
Avi Kivity1253791d2011-03-29 11:41:27 +02003503
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003504 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003505 if (ctxt->d & ModRM) {
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003506 rc = decode_modrm(ctxt, &memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003507 if (!ctxt->has_seg_override)
3508 set_seg_override(ctxt, ctxt->modrm_seg);
3509 } else if (ctxt->d & MemAbs)
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003510 rc = decode_abs(ctxt, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003511 if (rc != X86EMUL_CONTINUE)
3512 goto done;
3513
Avi Kivity9dac77f2011-06-01 15:34:25 +03003514 if (!ctxt->has_seg_override)
3515 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003516
Avi Kivity9dac77f2011-06-01 15:34:25 +03003517 memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003518
Avi Kivity9dac77f2011-06-01 15:34:25 +03003519 if (memop.type == OP_MEM && ctxt->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003520 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003521
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003522 /*
3523 * Decode and fetch the source operand: register, memory
3524 * or immediate.
3525 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003526 switch (ctxt->d & SrcMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003527 case SrcNone:
3528 break;
3529 case SrcReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003530 decode_register_operand(ctxt, &ctxt->src, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003531 break;
3532 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003533 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003534 goto srcmem_common;
3535 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003536 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003537 goto srcmem_common;
3538 case SrcMem:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003539 memop.bytes = (ctxt->d & ByteOp) ? 1 :
3540 ctxt->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003541 srcmem_common:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003542 ctxt->src = memop;
3543 memopp = &ctxt->src;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003544 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003545 case SrcImmU16:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003546 rc = decode_imm(ctxt, &ctxt->src, 2, false);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003547 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003548 case SrcImm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003549 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003550 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003551 case SrcImmU:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003552 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003553 break;
3554 case SrcImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003555 rc = decode_imm(ctxt, &ctxt->src, 1, true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003556 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003557 case SrcImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003558 rc = decode_imm(ctxt, &ctxt->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003559 break;
3560 case SrcAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003561 ctxt->src.type = OP_REG;
3562 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3563 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3564 fetch_register_operand(&ctxt->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003565 break;
3566 case SrcOne:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003567 ctxt->src.bytes = 1;
3568 ctxt->src.val = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003569 break;
3570 case SrcSI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003571 ctxt->src.type = OP_MEM;
3572 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3573 ctxt->src.addr.mem.ea =
3574 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3575 ctxt->src.addr.mem.seg = seg_override(ctxt);
3576 ctxt->src.val = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003577 break;
3578 case SrcImmFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003579 ctxt->src.type = OP_IMM;
3580 ctxt->src.addr.mem.ea = ctxt->_eip;
3581 ctxt->src.bytes = ctxt->op_bytes + 2;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003582 insn_fetch_arr(ctxt->src.valptr, ctxt->src.bytes, ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003583 break;
3584 case SrcMemFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003585 memop.bytes = ctxt->op_bytes + 2;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003586 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003587 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003588 case SrcDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003589 ctxt->src.type = OP_REG;
3590 ctxt->src.bytes = 2;
3591 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3592 fetch_register_operand(&ctxt->src);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003593 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003594 }
3595
Avi Kivity39f21ee2010-08-18 19:20:21 +03003596 if (rc != X86EMUL_CONTINUE)
3597 goto done;
3598
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003599 /*
3600 * Decode and fetch the second source operand: register, memory
3601 * or immediate.
3602 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003603 switch (ctxt->d & Src2Mask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003604 case Src2None:
3605 break;
3606 case Src2CL:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003607 ctxt->src2.bytes = 1;
Avi Kivity9be3be12011-09-13 10:45:38 +03003608 ctxt->src2.val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003609 break;
3610 case Src2ImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003611 rc = decode_imm(ctxt, &ctxt->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003612 break;
3613 case Src2One:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003614 ctxt->src2.bytes = 1;
3615 ctxt->src2.val = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003616 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003617 case Src2Imm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003618 rc = decode_imm(ctxt, &ctxt->src2, imm_size(ctxt), true);
Avi Kivity7db41eb2010-08-18 19:25:28 +03003619 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003620 }
3621
Avi Kivity39f21ee2010-08-18 19:20:21 +03003622 if (rc != X86EMUL_CONTINUE)
3623 goto done;
3624
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003625 /* Decode and fetch the destination operand: register or memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003626 switch (ctxt->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003627 case DstReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003628 decode_register_operand(ctxt, &ctxt->dst,
3629 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003630 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003631 case DstImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003632 ctxt->dst.type = OP_IMM;
3633 ctxt->dst.addr.mem.ea = ctxt->_eip;
3634 ctxt->dst.bytes = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003635 ctxt->dst.val = insn_fetch(u8, ctxt);
Wei Yongjun943858e2010-08-06 11:36:51 +08003636 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003637 case DstMem:
3638 case DstMem64:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003639 ctxt->dst = memop;
3640 memopp = &ctxt->dst;
3641 if ((ctxt->d & DstMask) == DstMem64)
3642 ctxt->dst.bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003643 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003644 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3645 if (ctxt->d & BitOp)
3646 fetch_bit_operand(ctxt);
3647 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003648 break;
3649 case DstAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003650 ctxt->dst.type = OP_REG;
3651 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3652 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3653 fetch_register_operand(&ctxt->dst);
3654 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003655 break;
3656 case DstDI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003657 ctxt->dst.type = OP_MEM;
3658 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3659 ctxt->dst.addr.mem.ea =
3660 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3661 ctxt->dst.addr.mem.seg = VCPU_SREG_ES;
3662 ctxt->dst.val = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003663 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003664 case DstDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003665 ctxt->dst.type = OP_REG;
3666 ctxt->dst.bytes = 2;
3667 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3668 fetch_register_operand(&ctxt->dst);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003669 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003670 case ImplicitOps:
3671 /* Special instructions do their own operand decoding. */
3672 default:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003673 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivitycb16c342011-06-19 19:21:11 +03003674 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003675 }
3676
3677done:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003678 if (memopp && memopp->type == OP_MEM && ctxt->rip_relative)
3679 memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003680
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003681 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003682}
3683
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003684static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3685{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003686 /* The second termination condition only applies for REPE
3687 * and REPNE. Test if the repeat string operation prefix is
3688 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3689 * corresponding termination condition according to:
3690 * - if REPE/REPZ and ZF = 0 then done
3691 * - if REPNE/REPNZ and ZF = 1 then done
3692 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003693 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3694 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3695 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003696 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003697 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003698 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3699 return true;
3700
3701 return false;
3702}
3703
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003704int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003705{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003706 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003707 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003708 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003709 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003710
Avi Kivity9dac77f2011-06-01 15:34:25 +03003711 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003712
Avi Kivity9dac77f2011-06-01 15:34:25 +03003713 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003714 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003715 goto done;
3716 }
3717
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003718 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003719 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003720 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003721 goto done;
3722 }
3723
Avi Kivity9dac77f2011-06-01 15:34:25 +03003724 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003725 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003726 goto done;
3727 }
3728
Avi Kivity9dac77f2011-06-01 15:34:25 +03003729 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003730 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3731 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity1253791d2011-03-29 11:41:27 +02003732 rc = emulate_ud(ctxt);
3733 goto done;
3734 }
3735
Avi Kivity9dac77f2011-06-01 15:34:25 +03003736 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity1253791d2011-03-29 11:41:27 +02003737 rc = emulate_nm(ctxt);
3738 goto done;
3739 }
3740
Avi Kivity9dac77f2011-06-01 15:34:25 +03003741 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3742 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003743 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003744 if (rc != X86EMUL_CONTINUE)
3745 goto done;
3746 }
3747
Gleb Natapove92805a2010-02-10 14:21:35 +02003748 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003749 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003750 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003751 goto done;
3752 }
3753
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003754 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003755 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003756 rc = emulate_ud(ctxt);
3757 goto done;
3758 }
3759
Joerg Roedeld09beab2011-04-04 12:39:25 +02003760 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003761 if (ctxt->check_perm) {
3762 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003763 if (rc != X86EMUL_CONTINUE)
3764 goto done;
3765 }
3766
Avi Kivity9dac77f2011-06-01 15:34:25 +03003767 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3768 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003769 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003770 if (rc != X86EMUL_CONTINUE)
3771 goto done;
3772 }
3773
Avi Kivity9dac77f2011-06-01 15:34:25 +03003774 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003775 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003776 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3777 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003778 goto done;
3779 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003780 }
3781
Avi Kivity9dac77f2011-06-01 15:34:25 +03003782 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3783 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3784 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003785 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003786 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003787 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003788 }
3789
Avi Kivity9dac77f2011-06-01 15:34:25 +03003790 if (ctxt->src2.type == OP_MEM) {
3791 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3792 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003793 if (rc != X86EMUL_CONTINUE)
3794 goto done;
3795 }
3796
Avi Kivity9dac77f2011-06-01 15:34:25 +03003797 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003798 goto special_insn;
3799
3800
Avi Kivity9dac77f2011-06-01 15:34:25 +03003801 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003802 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003803 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3804 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003805 if (rc != X86EMUL_CONTINUE)
3806 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003807 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003808 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003809
Avi Kivity018a98d2007-11-27 19:30:56 +02003810special_insn:
3811
Avi Kivity9dac77f2011-06-01 15:34:25 +03003812 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3813 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003814 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003815 if (rc != X86EMUL_CONTINUE)
3816 goto done;
3817 }
3818
Avi Kivity9dac77f2011-06-01 15:34:25 +03003819 if (ctxt->execute) {
3820 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003821 if (rc != X86EMUL_CONTINUE)
3822 goto done;
3823 goto writeback;
3824 }
3825
Avi Kivity9dac77f2011-06-01 15:34:25 +03003826 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003827 goto twobyte_insn;
3828
Avi Kivity9dac77f2011-06-01 15:34:25 +03003829 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003830 case 0x06: /* push es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003831 rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003832 break;
3833 case 0x07: /* pop es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003834 rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003835 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003836 case 0x0e: /* push cs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003837 rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003838 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003839 case 0x16: /* push ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003840 rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003841 break;
3842 case 0x17: /* pop ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003843 rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003844 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003845 case 0x1e: /* push ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003846 rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003847 break;
3848 case 0x1f: /* pop ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003849 rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003850 break;
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 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003897 rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003898 break;
3899 case 0xc5: /* lds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003900 rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003901 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003902 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003903 rc = emulate_int(ctxt, 3);
3904 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003905 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003906 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003907 break;
3908 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003909 if (ctxt->eflags & EFLG_OF)
3910 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003911 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003912 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003913 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003914 break;
3915 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003916 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003917 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003918 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003919 case 0xe4: /* inb */
3920 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003921 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003922 case 0xe6: /* outb */
3923 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003924 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003925 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003926 long int rel = ctxt->src.val;
3927 ctxt->src.val = (unsigned long) ctxt->_eip;
3928 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003929 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003930 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003931 }
3932 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003933 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003934 jmp_rel(ctxt, ctxt->src.val);
3935 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003936 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003937 case 0xec: /* in al,dx */
3938 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003939 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003940 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3941 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003942 goto done; /* IO is needed */
3943 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003944 case 0xee: /* out dx,al */
3945 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003946 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003947 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3948 &ctxt->src.val, 1);
3949 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003950 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003951 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003952 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003953 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003954 case 0xf5: /* cmc */
3955 /* complement carry flag from eflags reg */
3956 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003957 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003958 case 0xf6 ... 0xf7: /* Grp3 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003959 rc = em_grp3(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003960 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003961 case 0xf8: /* clc */
3962 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003963 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003964 case 0xf9: /* stc */
3965 ctxt->eflags |= EFLG_CF;
3966 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003967 case 0xfc: /* cld */
3968 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003969 break;
3970 case 0xfd: /* std */
3971 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003972 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003973 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003974 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003975 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003976 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003977 rc = em_grp45(ctxt);
3978 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003979 default:
3980 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003982
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003983 if (rc != X86EMUL_CONTINUE)
3984 goto done;
3985
Avi Kivity018a98d2007-11-27 19:30:56 +02003986writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003987 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003988 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003989 goto done;
3990
Gleb Natapov5cd21912010-03-18 15:20:26 +02003991 /*
3992 * restore dst type in case the decoding will be reused
3993 * (happens for string instruction )
3994 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003995 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003996
Avi Kivity9dac77f2011-06-01 15:34:25 +03003997 if ((ctxt->d & SrcMask) == SrcSI)
3998 string_addr_inc(ctxt, seg_override(ctxt),
3999 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004000
Avi Kivity9dac77f2011-06-01 15:34:25 +03004001 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004002 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004003 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004004
Avi Kivity9dac77f2011-06-01 15:34:25 +03004005 if (ctxt->rep_prefix && (ctxt->d & String)) {
4006 struct read_cache *r = &ctxt->io_read;
4007 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004008
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004009 if (!string_insn_completed(ctxt)) {
4010 /*
4011 * Re-enter guest when pio read ahead buffer is empty
4012 * or, if it is not used, after each 1024 iteration.
4013 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004014 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004015 (r->end == 0 || r->end != r->pos)) {
4016 /*
4017 * Reset read cache. Usually happens before
4018 * decode, but since instruction is restarted
4019 * we have to do it here.
4020 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004021 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004022 return EMULATION_RESTART;
4023 }
4024 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004025 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004026 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004027
Avi Kivity9dac77f2011-06-01 15:34:25 +03004028 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004029
4030done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004031 if (rc == X86EMUL_PROPAGATE_FAULT)
4032 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004033 if (rc == X86EMUL_INTERCEPTED)
4034 return EMULATION_INTERCEPTED;
4035
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004036 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004037
4038twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004039 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004040 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004041 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004042 break;
4043 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004044 case 0x0d: /* GrpP (prefetch) */
4045 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004046 break;
4047 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004048 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004049 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004050 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004051 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004053 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004054 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004055 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004056 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004057 goto done;
4058 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004059 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004062 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004063 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004064 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004065 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004066 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004067 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004068 goto done;
4069 }
4070
Avi Kivity9dac77f2011-06-01 15:34:25 +03004071 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004073 case 0x30:
4074 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004075 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4076 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4077 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004078 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004079 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004080 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004081 }
4082 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004083 break;
4084 case 0x32:
4085 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004086 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004087 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004088 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004089 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004090 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4092 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004093 }
4094 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004095 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004097 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4098 if (!test_cc(ctxt->b, ctxt->eflags))
4099 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004101 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 if (test_cc(ctxt->b, ctxt->eflags))
4103 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004104 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004105 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004106 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004107 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004108 case 0xa0: /* push fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004109 rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004110 break;
4111 case 0xa1: /* pop fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004112 rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004113 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004114 case 0xa3:
4115 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004116 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004117 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004118 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004119 emulate_2op_SrcV_nobyte(ctxt, "bt");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004120 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004121 case 0xa4: /* shld imm8, r, r/m */
4122 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004123 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004124 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004125 case 0xa8: /* push gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004126 rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004127 break;
4128 case 0xa9: /* pop gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004129 rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004130 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004131 case 0xab:
4132 bts: /* bts */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004133 emulate_2op_SrcV_nobyte(ctxt, "bts");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004134 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004135 case 0xac: /* shrd imm8, r, r/m */
4136 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004137 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004138 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004139 case 0xae: /* clflush */
4140 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 case 0xb0 ... 0xb1: /* cmpxchg */
4142 /*
4143 * Save real source value, then compare EAX against
4144 * destination.
4145 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004146 ctxt->src.orig_val = ctxt->src.val;
4147 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004148 emulate_2op_SrcV(ctxt, "cmp");
Laurent Vivier05f086f2007-09-24 11:10:55 +02004149 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004151 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 } else {
4153 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004154 ctxt->dst.type = OP_REG;
4155 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156 }
4157 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004158 case 0xb2: /* lss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004159 rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004160 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 case 0xb3:
4162 btr: /* btr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004163 emulate_2op_SrcV_nobyte(ctxt, "btr");
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004165 case 0xb4: /* lfs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004166 rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004167 break;
4168 case 0xb5: /* lgs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004169 rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004170 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004172 ctxt->dst.bytes = ctxt->op_bytes;
4173 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4174 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004177 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 case 0:
4179 goto bt;
4180 case 1:
4181 goto bts;
4182 case 2:
4183 goto btr;
4184 case 3:
4185 goto btc;
4186 }
4187 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004188 case 0xbb:
4189 btc: /* btc */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004190 emulate_2op_SrcV_nobyte(ctxt, "btc");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004191 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004192 case 0xbc: { /* bsf */
4193 u8 zf;
4194 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004195 : "=r"(ctxt->dst.val), "=q"(zf)
4196 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004197 ctxt->eflags &= ~X86_EFLAGS_ZF;
4198 if (zf) {
4199 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004200 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004201 }
4202 break;
4203 }
4204 case 0xbd: { /* bsr */
4205 u8 zf;
4206 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004207 : "=r"(ctxt->dst.val), "=q"(zf)
4208 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004209 ctxt->eflags &= ~X86_EFLAGS_ZF;
4210 if (zf) {
4211 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004212 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004213 }
4214 break;
4215 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004217 ctxt->dst.bytes = ctxt->op_bytes;
4218 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4219 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220 break;
Wei Yongjun92f738a52010-08-17 09:19:34 +08004221 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004222 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a52010-08-17 09:19:34 +08004223 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004224 ctxt->src.val = ctxt->dst.orig_val;
4225 write_register_operand(&ctxt->src);
Wei Yongjun92f738a52010-08-17 09:19:34 +08004226 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004227 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004228 ctxt->dst.bytes = ctxt->op_bytes;
4229 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4230 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004231 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004232 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004233 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004234 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004235 default:
4236 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004238
4239 if (rc != X86EMUL_CONTINUE)
4240 goto done;
4241
Avi Kivity6aa8b732006-12-10 02:21:36 -08004242 goto writeback;
4243
4244cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004245 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004246}