blob: 00e0904fab220343ec59a725a72ea40828a65792 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya99455492011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivitya99455492011-09-13 10:45:41 +030047
48#define OpBits 4 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030049#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030050
51/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080052 * Opcode effective-address decode tables.
53 * Note that we only emulate instructions that have at least one memory
54 * operand (excluding implicit stack references). We assume that stack
55 * references and instruction fetches will never occur in special memory
56 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
57 * not be handled.
58 */
59
60/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030061#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080062/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030063#define DstShift 1
64#define ImplicitOps (OpImplicit << DstShift)
65#define DstReg (OpReg << DstShift)
66#define DstMem (OpMem << DstShift)
67#define DstAcc (OpAcc << DstShift)
68#define DstDI (OpDI << DstShift)
69#define DstMem64 (OpMem64 << DstShift)
70#define DstImmUByte (OpImmUByte << DstShift)
71#define DstDX (OpDX << DstShift)
72#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080073/* Source operand type. */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030074#define SrcNone (0<<5) /* No source operand. */
75#define SrcReg (1<<5) /* Register operand. */
76#define SrcMem (2<<5) /* Memory operand. */
77#define SrcMem16 (3<<5) /* Memory operand (16-bit). */
78#define SrcMem32 (4<<5) /* Memory operand (32-bit). */
79#define SrcImm (5<<5) /* Immediate operand. */
80#define SrcImmByte (6<<5) /* 8-bit sign-extended immediate operand. */
81#define SrcOne (7<<5) /* Implied '1' */
82#define SrcImmUByte (8<<5) /* 8-bit unsigned immediate operand. */
83#define SrcImmU (9<<5) /* Immediate operand, unsigned */
84#define SrcSI (0xa<<5) /* Source is in the DS:RSI */
85#define SrcImmFAddr (0xb<<5) /* Source is immediate far address */
86#define SrcMemFAddr (0xc<<5) /* Source is far address in memory */
87#define SrcAcc (0xd<<5) /* Source Accumulator */
88#define SrcImmU16 (0xe<<5) /* Immediate operand, unsigned, 16 bits */
89#define SrcDX (0xf<<5) /* Source is in DX register */
90#define SrcMask (0xf<<5)
Marcelo Tosatti221192b2011-05-30 15:23:14 -030091#define BitOp (1<<11)
92#define MemAbs (1<<12) /* Memory operand is absolute displacement */
93#define String (1<<13) /* String instruction (rep capable) */
94#define Stack (1<<14) /* Stack instruction (push/pop) */
95#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
96#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
97#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
98#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
99#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
100#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300101/* Generic ModRM decode. */
102#define ModRM (1<<19)
103/* Destination is only written; never read. */
104#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300105/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200106#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200107#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300108#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300109#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300110#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200111#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200112#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300113#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100114/* Source 2 operand type */
Avi Kivity4dd6a572011-09-13 10:45:43 +0300115#define Src2Shift (29)
116#define Src2None (OpNone << Src2Shift)
117#define Src2CL (OpCL << Src2Shift)
118#define Src2ImmByte (OpImmByte << Src2Shift)
119#define Src2One (OpOne << Src2Shift)
120#define Src2Imm (OpImm << Src2Shift)
121#define Src2Mask (OpMask << Src2Shift)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
Avi Kivityd0e53322010-07-29 15:11:54 +0300123#define X2(x...) x, x
124#define X3(x...) X2(x), x
125#define X4(x...) X2(x), X2(x)
126#define X5(x...) X4(x), x
127#define X6(x...) X4(x), X2(x)
128#define X7(x...) X4(x), X3(x)
129#define X8(x...) X4(x), X4(x)
130#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300131
Avi Kivityd65b1de2010-07-29 15:11:35 +0300132struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300133 u64 flags : 56;
134 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300135 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300136 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300137 struct opcode *group;
138 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200139 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300140 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200141 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300142};
143
144struct group_dual {
145 struct opcode mod012[8];
146 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300147};
148
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200149struct gprefix {
150 struct opcode pfx_no;
151 struct opcode pfx_66;
152 struct opcode pfx_f2;
153 struct opcode pfx_f3;
154};
155
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200157#define EFLG_ID (1<<21)
158#define EFLG_VIP (1<<20)
159#define EFLG_VIF (1<<19)
160#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200161#define EFLG_VM (1<<17)
162#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200163#define EFLG_IOPL (3<<12)
164#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165#define EFLG_OF (1<<11)
166#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200167#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200168#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169#define EFLG_SF (1<<7)
170#define EFLG_ZF (1<<6)
171#define EFLG_AF (1<<4)
172#define EFLG_PF (1<<2)
173#define EFLG_CF (1<<0)
174
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300175#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
176#define EFLG_RESERVED_ONE_MASK 2
177
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178/*
179 * Instruction emulation:
180 * Most instructions are emulated directly via a fragment of inline assembly
181 * code. This allows us to save/restore EFLAGS and thus very easily pick up
182 * any modified flags.
183 */
184
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800185#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186#define _LO32 "k" /* force 32-bit operand */
187#define _STK "%%rsp" /* stack pointer */
188#elif defined(__i386__)
189#define _LO32 "" /* force 32-bit operand */
190#define _STK "%%esp" /* stack pointer */
191#endif
192
193/*
194 * These EFLAGS bits are restored from saved value during emulation, and
195 * any changes are written back to the saved value after emulation.
196 */
197#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
198
199/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200200#define _PRE_EFLAGS(_sav, _msk, _tmp) \
201 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
202 "movl %"_sav",%"_LO32 _tmp"; " \
203 "push %"_tmp"; " \
204 "push %"_tmp"; " \
205 "movl %"_msk",%"_LO32 _tmp"; " \
206 "andl %"_LO32 _tmp",("_STK"); " \
207 "pushf; " \
208 "notl %"_LO32 _tmp"; " \
209 "andl %"_LO32 _tmp",("_STK"); " \
210 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
211 "pop %"_tmp"; " \
212 "orl %"_LO32 _tmp",("_STK"); " \
213 "popf; " \
214 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215
216/* After executing instruction: write-back necessary bits in EFLAGS. */
217#define _POST_EFLAGS(_sav, _msk, _tmp) \
218 /* _sav |= EFLAGS & _msk; */ \
219 "pushf; " \
220 "pop %"_tmp"; " \
221 "andl %"_msk",%"_LO32 _tmp"; " \
222 "orl %"_LO32 _tmp",%"_sav"; "
223
Avi Kivitydda96d82008-11-26 15:14:10 +0200224#ifdef CONFIG_X86_64
225#define ON64(x) x
226#else
227#define ON64(x)
228#endif
229
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300230#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200231 do { \
232 __asm__ __volatile__ ( \
233 _PRE_EFLAGS("0", "4", "2") \
234 _op _suffix " %"_x"3,%1; " \
235 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300236 : "=m" ((ctxt)->eflags), \
237 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200238 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300239 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200240 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200241
242
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300244#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200245 do { \
246 unsigned long _tmp; \
247 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300248 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200249 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300250 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200251 break; \
252 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300253 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200254 break; \
255 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300256 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200257 break; \
258 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259 } while (0)
260
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300261#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200263 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300264 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300266 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267 break; \
268 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300269 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270 _wx, _wy, _lx, _ly, _qx, _qy); \
271 break; \
272 } \
273 } while (0)
274
275/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300276#define emulate_2op_SrcB(ctxt, _op) \
277 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278
279/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300280#define emulate_2op_SrcV(ctxt, _op) \
281 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282
283/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300284#define emulate_2op_SrcV_nobyte(ctxt, _op) \
285 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100287/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300288#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300289 do { \
290 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300291 _type _clv = (ctxt)->src2.val; \
292 _type _srcv = (ctxt)->src.val; \
293 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300294 \
295 __asm__ __volatile__ ( \
296 _PRE_EFLAGS("0", "5", "2") \
297 _op _suffix " %4,%1 \n" \
298 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300299 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300300 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
301 ); \
302 \
Avi Kivity761441b2011-09-07 16:41:36 +0300303 (ctxt)->src2.val = (unsigned long) _clv; \
304 (ctxt)->src2.val = (unsigned long) _srcv; \
305 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100306 } while (0)
307
Avi Kivity761441b2011-09-07 16:41:36 +0300308#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300309 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300310 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300311 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300312 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300313 break; \
314 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300315 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300316 break; \
317 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300318 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300319 break; \
320 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100321 } while (0)
322
Avi Kivityd1eef452011-09-07 16:41:38 +0300323#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324 do { \
325 unsigned long _tmp; \
326 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200327 __asm__ __volatile__ ( \
328 _PRE_EFLAGS("0", "3", "2") \
329 _op _suffix " %1; " \
330 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300331 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200332 "=&r" (_tmp) \
333 : "i" (EFLAGS_MASK)); \
334 } while (0)
335
336/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300337#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200338 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300339 switch ((ctxt)->dst.bytes) { \
340 case 1: __emulate_1op(ctxt, _op, "b"); break; \
341 case 2: __emulate_1op(ctxt, _op, "w"); break; \
342 case 4: __emulate_1op(ctxt, _op, "l"); break; \
343 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344 } \
345 } while (0)
346
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300347#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300348 do { \
349 unsigned long _tmp; \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300350 ulong *rax = &(ctxt)->regs[VCPU_REGS_RAX]; \
351 ulong *rdx = &(ctxt)->regs[VCPU_REGS_RDX]; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300352 \
353 __asm__ __volatile__ ( \
354 _PRE_EFLAGS("0", "5", "1") \
355 "1: \n\t" \
356 _op _suffix " %6; " \
357 "2: \n\t" \
358 _POST_EFLAGS("0", "5", "1") \
359 ".pushsection .fixup,\"ax\" \n\t" \
360 "3: movb $1, %4 \n\t" \
361 "jmp 2b \n\t" \
362 ".popsection \n\t" \
363 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300364 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
365 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
366 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
367 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300368 } while (0)
369
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300370/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300371#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300372 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300373 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300374 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300375 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300376 break; \
377 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300378 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300379 break; \
380 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300381 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300382 break; \
383 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300384 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300385 break; \
386 } \
387 } while (0)
388
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200389static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
390 enum x86_intercept intercept,
391 enum x86_intercept_stage stage)
392{
393 struct x86_instruction_info info = {
394 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300395 .rep_prefix = ctxt->rep_prefix,
396 .modrm_mod = ctxt->modrm_mod,
397 .modrm_reg = ctxt->modrm_reg,
398 .modrm_rm = ctxt->modrm_rm,
399 .src_val = ctxt->src.val64,
400 .src_bytes = ctxt->src.bytes,
401 .dst_bytes = ctxt->dst.bytes,
402 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200403 .next_rip = ctxt->eip,
404 };
405
Avi Kivity29535382011-04-20 13:37:53 +0300406 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200407}
408
Avi Kivity9dac77f2011-06-01 15:34:25 +0300409static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800410{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300411 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800412}
413
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800415static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300416address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800417{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300418 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800419 return reg;
420 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300421 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800422}
423
424static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300425register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800426{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300427 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800428}
429
Harvey Harrison7a9572752008-02-19 07:40:41 -0800430static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300431register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800432{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300433 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800434 *reg += inc;
435 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300436 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800437}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438
Avi Kivity9dac77f2011-06-01 15:34:25 +0300439static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800440{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300441 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800442}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300443
Avi Kivity56697682011-04-03 14:08:51 +0300444static u32 desc_limit_scaled(struct desc_struct *desc)
445{
446 u32 limit = get_desc_limit(desc);
447
448 return desc->g ? (limit << 12) | 0xfff : limit;
449}
450
Avi Kivity9dac77f2011-06-01 15:34:25 +0300451static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300452{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300453 ctxt->has_seg_override = true;
454 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300455}
456
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900457static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300458{
459 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
460 return 0;
461
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900462 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300463}
464
Avi Kivity9dac77f2011-06-01 15:34:25 +0300465static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300466{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300467 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300468 return 0;
469
Avi Kivity9dac77f2011-06-01 15:34:25 +0300470 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300471}
472
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200473static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
474 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300475{
Avi Kivityda9cb572010-11-22 17:53:21 +0200476 ctxt->exception.vector = vec;
477 ctxt->exception.error_code = error;
478 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200479 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300480}
481
Joerg Roedel3b88e412011-04-04 12:39:29 +0200482static int emulate_db(struct x86_emulate_ctxt *ctxt)
483{
484 return emulate_exception(ctxt, DB_VECTOR, 0, false);
485}
486
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200487static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300488{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200489 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300490}
491
Avi Kivity618ff152011-04-03 12:32:09 +0300492static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
493{
494 return emulate_exception(ctxt, SS_VECTOR, err, true);
495}
496
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200497static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300498{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200499 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300500}
501
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200502static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300503{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200504 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300505}
506
Avi Kivity34d1f492010-08-26 11:59:01 +0300507static int emulate_de(struct x86_emulate_ctxt *ctxt)
508{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200509 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300510}
511
Avi Kivity12537912011-03-29 11:41:27 +0200512static int emulate_nm(struct x86_emulate_ctxt *ctxt)
513{
514 return emulate_exception(ctxt, NM_VECTOR, 0, false);
515}
516
Avi Kivity1aa36612011-04-27 13:20:30 +0300517static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
518{
519 u16 selector;
520 struct desc_struct desc;
521
522 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
523 return selector;
524}
525
526static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
527 unsigned seg)
528{
529 u16 dummy;
530 u32 base3;
531 struct desc_struct desc;
532
533 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
534 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
535}
536
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400537static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300538 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400539 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300540 ulong *linear)
541{
Avi Kivity618ff152011-04-03 12:32:09 +0300542 struct desc_struct desc;
543 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300544 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300545 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300546 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300547 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300548
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900549 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300550 switch (ctxt->mode) {
551 case X86EMUL_MODE_REAL:
552 break;
553 case X86EMUL_MODE_PROT64:
554 if (((signed long)la << 16) >> 16 != la)
555 return emulate_gp(ctxt, 0);
556 break;
557 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300558 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
559 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300560 if (!usable)
561 goto bad;
562 /* code segment or read-only data segment */
563 if (((desc.type & 8) || !(desc.type & 2)) && write)
564 goto bad;
565 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400566 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300567 goto bad;
568 lim = desc_limit_scaled(&desc);
569 if ((desc.type & 8) || !(desc.type & 4)) {
570 /* expand-up segment */
571 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
572 goto bad;
573 } else {
574 /* exapand-down segment */
575 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
576 goto bad;
577 lim = desc.d ? 0xffffffff : 0xffff;
578 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
579 goto bad;
580 }
Avi Kivity717746e2011-04-20 13:37:53 +0300581 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300582 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300583 cpl = max(cpl, rpl);
584 if (!(desc.type & 8)) {
585 /* data segment */
586 if (cpl > desc.dpl)
587 goto bad;
588 } else if ((desc.type & 8) && !(desc.type & 4)) {
589 /* nonconforming code segment */
590 if (cpl != desc.dpl)
591 goto bad;
592 } else if ((desc.type & 8) && (desc.type & 4)) {
593 /* conforming code segment */
594 if (cpl < desc.dpl)
595 goto bad;
596 }
597 break;
598 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300599 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300600 la &= (u32)-1;
601 *linear = la;
602 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300603bad:
604 if (addr.seg == VCPU_SREG_SS)
605 return emulate_ss(ctxt, addr.seg);
606 else
607 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300608}
609
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400610static int linearize(struct x86_emulate_ctxt *ctxt,
611 struct segmented_address addr,
612 unsigned size, bool write,
613 ulong *linear)
614{
615 return __linearize(ctxt, addr, size, write, false, linear);
616}
617
618
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200619static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
620 struct segmented_address addr,
621 void *data,
622 unsigned size)
623{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200624 int rc;
625 ulong linear;
626
Avi Kivity83b87952011-04-03 11:31:19 +0300627 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200628 if (rc != X86EMUL_CONTINUE)
629 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300630 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200631}
632
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900633/*
634 * Fetch the next byte of the instruction being emulated which is pointed to
635 * by ctxt->_eip, then increment ctxt->_eip.
636 *
637 * Also prefetch the remaining bytes of the instruction without crossing page
638 * boundary if they are not in fetch_cache yet.
639 */
640static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200641{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300642 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200643 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300644 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200645
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900646 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400647 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900648 struct segmented_address addr = { .seg = VCPU_SREG_CS,
649 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300650 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900651 size = min(15UL - cur_size,
652 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400653 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900654 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400655 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900656 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
657 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900658 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200659 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300660 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200661 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900662 *dest = fc->data[ctxt->_eip - fc->start];
663 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900664 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200665}
666
667static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900668 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200669{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900670 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200671
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200672 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900673 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200674 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200675 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900676 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900677 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200678 return rc;
679 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900680 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200681}
682
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900683/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900684#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900685({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900686 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900687 if (rc != X86EMUL_CONTINUE) \
688 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900689 (_type)_x; \
690})
691
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900692#define insn_fetch_arr(_arr, _size, _ctxt) \
693({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900694 if (rc != X86EMUL_CONTINUE) \
695 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900696})
697
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000698/*
699 * Given the 'reg' portion of a ModRM byte, and a register block, return a
700 * pointer into the block that addresses the relevant register.
701 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
702 */
703static void *decode_register(u8 modrm_reg, unsigned long *regs,
704 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705{
706 void *p;
707
708 p = &regs[modrm_reg];
709 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
710 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
711 return p;
712}
713
714static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200715 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 u16 *size, unsigned long *address, int op_bytes)
717{
718 int rc;
719
720 if (op_bytes == 2)
721 op_bytes = 3;
722 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200723 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900724 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200726 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200727 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 return rc;
729}
730
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300731static int test_cc(unsigned int condition, unsigned int flags)
732{
733 int rc = 0;
734
735 switch ((condition & 15) >> 1) {
736 case 0: /* o */
737 rc |= (flags & EFLG_OF);
738 break;
739 case 1: /* b/c/nae */
740 rc |= (flags & EFLG_CF);
741 break;
742 case 2: /* z/e */
743 rc |= (flags & EFLG_ZF);
744 break;
745 case 3: /* be/na */
746 rc |= (flags & (EFLG_CF|EFLG_ZF));
747 break;
748 case 4: /* s */
749 rc |= (flags & EFLG_SF);
750 break;
751 case 5: /* p/pe */
752 rc |= (flags & EFLG_PF);
753 break;
754 case 7: /* le/ng */
755 rc |= (flags & EFLG_ZF);
756 /* fall through */
757 case 6: /* l/nge */
758 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
759 break;
760 }
761
762 /* Odd condition identifiers (lsb == 1) have inverted sense. */
763 return (!!rc ^ (condition & 1));
764}
765
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300766static void fetch_register_operand(struct operand *op)
767{
768 switch (op->bytes) {
769 case 1:
770 op->val = *(u8 *)op->addr.reg;
771 break;
772 case 2:
773 op->val = *(u16 *)op->addr.reg;
774 break;
775 case 4:
776 op->val = *(u32 *)op->addr.reg;
777 break;
778 case 8:
779 op->val = *(u64 *)op->addr.reg;
780 break;
781 }
782}
783
Avi Kivity12537912011-03-29 11:41:27 +0200784static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
785{
786 ctxt->ops->get_fpu(ctxt);
787 switch (reg) {
788 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
789 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
790 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
791 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
792 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
793 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
794 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
795 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
796#ifdef CONFIG_X86_64
797 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
798 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
799 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
800 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
801 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
802 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
803 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
804 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
805#endif
806 default: BUG();
807 }
808 ctxt->ops->put_fpu(ctxt);
809}
810
811static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
812 int reg)
813{
814 ctxt->ops->get_fpu(ctxt);
815 switch (reg) {
816 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
817 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
818 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
819 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
820 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
821 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
822 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
823 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
824#ifdef CONFIG_X86_64
825 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
826 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
827 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
828 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
829 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
830 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
831 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
832 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
833#endif
834 default: BUG();
835 }
836 ctxt->ops->put_fpu(ctxt);
837}
838
839static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
840 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200841 int inhibit_bytereg)
842{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300843 unsigned reg = ctxt->modrm_reg;
844 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200845
Avi Kivity9dac77f2011-06-01 15:34:25 +0300846 if (!(ctxt->d & ModRM))
847 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200848
Avi Kivity9dac77f2011-06-01 15:34:25 +0300849 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200850 op->type = OP_XMM;
851 op->bytes = 16;
852 op->addr.xmm = reg;
853 read_sse_reg(ctxt, &op->vec_val, reg);
854 return;
855 }
856
Avi Kivity3c118e22007-10-31 10:27:04 +0200857 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300858 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
859 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200860 op->bytes = 1;
861 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300862 op->addr.reg = decode_register(reg, ctxt->regs, 0);
863 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200864 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300865 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200866 op->orig_val = op->val;
867}
868
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200869static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300870 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200872 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700873 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900874 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300875 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876
Avi Kivity9dac77f2011-06-01 15:34:25 +0300877 if (ctxt->rex_prefix) {
878 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
879 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
880 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200881 }
882
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900883 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300884 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
885 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
886 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
887 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888
Avi Kivity9dac77f2011-06-01 15:34:25 +0300889 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300890 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300891 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
892 op->addr.reg = decode_register(ctxt->modrm_rm,
893 ctxt->regs, ctxt->d & ByteOp);
894 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200895 op->type = OP_XMM;
896 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300897 op->addr.xmm = ctxt->modrm_rm;
898 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200899 return rc;
900 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300901 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902 return rc;
903 }
904
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300905 op->type = OP_MEM;
906
Avi Kivity9dac77f2011-06-01 15:34:25 +0300907 if (ctxt->ad_bytes == 2) {
908 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
909 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
910 unsigned si = ctxt->regs[VCPU_REGS_RSI];
911 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200912
913 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300914 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200915 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300916 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900917 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200918 break;
919 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900920 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200921 break;
922 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900923 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200924 break;
925 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300926 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200927 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300928 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200929 break;
930 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300931 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932 break;
933 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300934 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 break;
936 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300937 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200938 break;
939 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300940 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200941 break;
942 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300943 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200944 break;
945 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300946 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300947 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200948 break;
949 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300950 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200951 break;
952 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300953 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
954 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
955 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300956 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200957 } else {
958 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300959 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900960 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200961 index_reg |= (sib >> 3) & 7;
962 base_reg |= sib & 7;
963 scale = sib >> 6;
964
Avi Kivity9dac77f2011-06-01 15:34:25 +0300965 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900966 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700967 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300968 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700969 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300970 modrm_ea += ctxt->regs[index_reg] << scale;
971 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700972 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300973 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700974 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300975 modrm_ea += ctxt->regs[ctxt->modrm_rm];
976 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200977 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300978 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900979 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200980 break;
981 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900982 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200983 break;
984 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900985 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200986 break;
987 }
988 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200989 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200990done:
991 return rc;
992}
993
994static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300995 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200996{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900997 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200998
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300999 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001000 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001002 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001003 break;
1004 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001005 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001006 break;
1007 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001008 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001009 break;
1010 }
1011done:
1012 return rc;
1013}
1014
Avi Kivity9dac77f2011-06-01 15:34:25 +03001015static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001016{
Sheng Yang7129eec2010-09-28 16:33:32 +08001017 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001018
Avi Kivity9dac77f2011-06-01 15:34:25 +03001019 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1020 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001021
Avi Kivity9dac77f2011-06-01 15:34:25 +03001022 if (ctxt->src.bytes == 2)
1023 sv = (s16)ctxt->src.val & (s16)mask;
1024 else if (ctxt->src.bytes == 4)
1025 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001026
Avi Kivity9dac77f2011-06-01 15:34:25 +03001027 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001028 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001029
1030 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001031 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001032}
1033
Gleb Natapov9de41572010-04-28 19:15:22 +03001034static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001035 unsigned long addr, void *dest, unsigned size)
1036{
1037 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001038 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001039
1040 while (size) {
1041 int n = min(size, 8u);
1042 size -= n;
1043 if (mc->pos < mc->end)
1044 goto read_cached;
1045
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001046 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1047 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001048 if (rc != X86EMUL_CONTINUE)
1049 return rc;
1050 mc->end += n;
1051
1052 read_cached:
1053 memcpy(dest, mc->data + mc->pos, n);
1054 mc->pos += n;
1055 dest += n;
1056 addr += n;
1057 }
1058 return X86EMUL_CONTINUE;
1059}
1060
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001061static int segmented_read(struct x86_emulate_ctxt *ctxt,
1062 struct segmented_address addr,
1063 void *data,
1064 unsigned size)
1065{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001066 int rc;
1067 ulong linear;
1068
Avi Kivity83b87952011-04-03 11:31:19 +03001069 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001070 if (rc != X86EMUL_CONTINUE)
1071 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001072 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001073}
1074
1075static int segmented_write(struct x86_emulate_ctxt *ctxt,
1076 struct segmented_address addr,
1077 const void *data,
1078 unsigned size)
1079{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001080 int rc;
1081 ulong linear;
1082
Avi Kivity83b87952011-04-03 11:31:19 +03001083 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001084 if (rc != X86EMUL_CONTINUE)
1085 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001086 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1087 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001088}
1089
1090static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1091 struct segmented_address addr,
1092 const void *orig_data, const void *data,
1093 unsigned size)
1094{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001095 int rc;
1096 ulong linear;
1097
Avi Kivity83b87952011-04-03 11:31:19 +03001098 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001099 if (rc != X86EMUL_CONTINUE)
1100 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001101 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1102 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001103}
1104
Gleb Natapov7b262e92010-03-18 15:20:27 +02001105static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001106 unsigned int size, unsigned short port,
1107 void *dest)
1108{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001109 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001110
1111 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001112 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001113 unsigned int count = ctxt->rep_prefix ?
1114 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001115 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001116 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1117 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001118 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1119 count);
1120 if (n == 0)
1121 n = 1;
1122 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001123 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001124 return 0;
1125 rc->end = n * size;
1126 }
1127
1128 memcpy(dest, rc->data + rc->pos, size);
1129 rc->pos += size;
1130 return 1;
1131}
1132
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001133static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001134 u16 selector, struct desc_ptr *dt)
1135{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001136 struct x86_emulate_ops *ops = ctxt->ops;
1137
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001138 if (selector & 1 << 2) {
1139 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001140 u16 sel;
1141
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001142 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001143 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001144 return;
1145
1146 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1147 dt->address = get_desc_base(&desc);
1148 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001149 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001150}
1151
1152/* allowed just for 8 bytes segments */
1153static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001154 u16 selector, struct desc_struct *desc)
1155{
1156 struct desc_ptr dt;
1157 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001158 ulong addr;
1159
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001160 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001161
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001162 if (dt.size < index * 8 + 7)
1163 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001164
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001165 addr = dt.address + index * 8;
1166 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1167 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001168}
1169
1170/* allowed just for 8 bytes segments */
1171static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001172 u16 selector, struct desc_struct *desc)
1173{
1174 struct desc_ptr dt;
1175 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001176 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001177
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001178 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001179
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001180 if (dt.size < index * 8 + 7)
1181 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001182
1183 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001184 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1185 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001186}
1187
Gleb Natapov5601d052011-03-07 14:55:06 +02001188/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001189static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001190 u16 selector, int seg)
1191{
1192 struct desc_struct seg_desc;
1193 u8 dpl, rpl, cpl;
1194 unsigned err_vec = GP_VECTOR;
1195 u32 err_code = 0;
1196 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1197 int ret;
1198
1199 memset(&seg_desc, 0, sizeof seg_desc);
1200
1201 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1202 || ctxt->mode == X86EMUL_MODE_REAL) {
1203 /* set real mode segment descriptor */
1204 set_desc_base(&seg_desc, selector << 4);
1205 set_desc_limit(&seg_desc, 0xffff);
1206 seg_desc.type = 3;
1207 seg_desc.p = 1;
1208 seg_desc.s = 1;
1209 goto load;
1210 }
1211
1212 /* NULL selector is not valid for TR, CS and SS */
1213 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1214 && null_selector)
1215 goto exception;
1216
1217 /* TR should be in GDT only */
1218 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1219 goto exception;
1220
1221 if (null_selector) /* for NULL selector skip all following checks */
1222 goto load;
1223
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001224 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001225 if (ret != X86EMUL_CONTINUE)
1226 return ret;
1227
1228 err_code = selector & 0xfffc;
1229 err_vec = GP_VECTOR;
1230
1231 /* can't load system descriptor into segment selecor */
1232 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1233 goto exception;
1234
1235 if (!seg_desc.p) {
1236 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1237 goto exception;
1238 }
1239
1240 rpl = selector & 3;
1241 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001242 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001243
1244 switch (seg) {
1245 case VCPU_SREG_SS:
1246 /*
1247 * segment is not a writable data segment or segment
1248 * selector's RPL != CPL or segment selector's RPL != CPL
1249 */
1250 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1251 goto exception;
1252 break;
1253 case VCPU_SREG_CS:
1254 if (!(seg_desc.type & 8))
1255 goto exception;
1256
1257 if (seg_desc.type & 4) {
1258 /* conforming */
1259 if (dpl > cpl)
1260 goto exception;
1261 } else {
1262 /* nonconforming */
1263 if (rpl > cpl || dpl != cpl)
1264 goto exception;
1265 }
1266 /* CS(RPL) <- CPL */
1267 selector = (selector & 0xfffc) | cpl;
1268 break;
1269 case VCPU_SREG_TR:
1270 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1271 goto exception;
1272 break;
1273 case VCPU_SREG_LDTR:
1274 if (seg_desc.s || seg_desc.type != 2)
1275 goto exception;
1276 break;
1277 default: /* DS, ES, FS, or GS */
1278 /*
1279 * segment is not a data or readable code segment or
1280 * ((segment is a data or nonconforming code segment)
1281 * and (both RPL and CPL > DPL))
1282 */
1283 if ((seg_desc.type & 0xa) == 0x8 ||
1284 (((seg_desc.type & 0xc) != 0xc) &&
1285 (rpl > dpl && cpl > dpl)))
1286 goto exception;
1287 break;
1288 }
1289
1290 if (seg_desc.s) {
1291 /* mark segment as accessed */
1292 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001293 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001294 if (ret != X86EMUL_CONTINUE)
1295 return ret;
1296 }
1297load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001298 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001299 return X86EMUL_CONTINUE;
1300exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001301 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001302 return X86EMUL_PROPAGATE_FAULT;
1303}
1304
Wei Yongjun31be40b2010-08-17 09:17:30 +08001305static void write_register_operand(struct operand *op)
1306{
1307 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1308 switch (op->bytes) {
1309 case 1:
1310 *(u8 *)op->addr.reg = (u8)op->val;
1311 break;
1312 case 2:
1313 *(u16 *)op->addr.reg = (u16)op->val;
1314 break;
1315 case 4:
1316 *op->addr.reg = (u32)op->val;
1317 break; /* 64b: zero-extend */
1318 case 8:
1319 *op->addr.reg = op->val;
1320 break;
1321 }
1322}
1323
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001324static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001325{
1326 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001327
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001329 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001330 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001331 break;
1332 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001333 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001334 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001335 ctxt->dst.addr.mem,
1336 &ctxt->dst.orig_val,
1337 &ctxt->dst.val,
1338 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001339 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001340 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001341 ctxt->dst.addr.mem,
1342 &ctxt->dst.val,
1343 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001344 if (rc != X86EMUL_CONTINUE)
1345 return rc;
1346 break;
Avi Kivity12537912011-03-29 11:41:27 +02001347 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001348 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001349 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001350 case OP_NONE:
1351 /* no writeback */
1352 break;
1353 default:
1354 break;
1355 }
1356 return X86EMUL_CONTINUE;
1357}
1358
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001359static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001360{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001361 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001362
Avi Kivity9dac77f2011-06-01 15:34:25 +03001363 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1364 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001365 addr.seg = VCPU_SREG_SS;
1366
1367 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001368 ctxt->dst.type = OP_NONE;
1369 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001370}
1371
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001372static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001373 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001374{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001375 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001376 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001377
Avi Kivity9dac77f2011-06-01 15:34:25 +03001378 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001379 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001380 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001381 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382 return rc;
1383
Avi Kivity9dac77f2011-06-01 15:34:25 +03001384 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001385 return rc;
1386}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001387
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001388static int em_pop(struct x86_emulate_ctxt *ctxt)
1389{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001390 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001391}
1392
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001393static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001394 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001395{
1396 int rc;
1397 unsigned long val, change_mask;
1398 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001399 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001400
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001401 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001402 if (rc != X86EMUL_CONTINUE)
1403 return rc;
1404
1405 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1406 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1407
1408 switch(ctxt->mode) {
1409 case X86EMUL_MODE_PROT64:
1410 case X86EMUL_MODE_PROT32:
1411 case X86EMUL_MODE_PROT16:
1412 if (cpl == 0)
1413 change_mask |= EFLG_IOPL;
1414 if (cpl <= iopl)
1415 change_mask |= EFLG_IF;
1416 break;
1417 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001418 if (iopl < 3)
1419 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001420 change_mask |= EFLG_IF;
1421 break;
1422 default: /* real mode */
1423 change_mask |= (EFLG_IOPL | EFLG_IF);
1424 break;
1425 }
1426
1427 *(unsigned long *)dest =
1428 (ctxt->eflags & ~change_mask) | (val & change_mask);
1429
1430 return rc;
1431}
1432
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001433static int em_popf(struct x86_emulate_ctxt *ctxt)
1434{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001435 ctxt->dst.type = OP_REG;
1436 ctxt->dst.addr.reg = &ctxt->eflags;
1437 ctxt->dst.bytes = ctxt->op_bytes;
1438 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001439}
1440
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001441static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001442{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001443 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001444
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001445 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001446}
1447
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001448static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001449{
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001450 unsigned long selector;
1451 int rc;
1452
Avi Kivity9dac77f2011-06-01 15:34:25 +03001453 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001454 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001455 return rc;
1456
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001457 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001458 return rc;
1459}
1460
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001461static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001462{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001463 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001464 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001465 int reg = VCPU_REGS_RAX;
1466
1467 while (reg <= VCPU_REGS_RDI) {
1468 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001469 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001470
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001471 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001472 if (rc != X86EMUL_CONTINUE)
1473 return rc;
1474
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001475 ++reg;
1476 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001477
Wei Yongjunc37eda12010-06-15 09:03:33 +08001478 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001479}
1480
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001481static int em_pushf(struct x86_emulate_ctxt *ctxt)
1482{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001483 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001484 return em_push(ctxt);
1485}
1486
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001487static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001488{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001489 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001490 int reg = VCPU_REGS_RDI;
1491
1492 while (reg >= VCPU_REGS_RAX) {
1493 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001494 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1495 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001496 --reg;
1497 }
1498
Avi Kivity9dac77f2011-06-01 15:34:25 +03001499 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001500 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001501 break;
1502 --reg;
1503 }
1504 return rc;
1505}
1506
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001507int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001508{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001509 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001510 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001511 struct desc_ptr dt;
1512 gva_t cs_addr;
1513 gva_t eip_addr;
1514 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001515
1516 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001517 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001518 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001519 if (rc != X86EMUL_CONTINUE)
1520 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001521
1522 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1523
Avi Kivity9dac77f2011-06-01 15:34:25 +03001524 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001525 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001526 if (rc != X86EMUL_CONTINUE)
1527 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001528
Avi Kivity9dac77f2011-06-01 15:34:25 +03001529 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001530 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001531 if (rc != X86EMUL_CONTINUE)
1532 return rc;
1533
Avi Kivity4bff1e862011-04-20 13:37:53 +03001534 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001535
1536 eip_addr = dt.address + (irq << 2);
1537 cs_addr = dt.address + (irq << 2) + 2;
1538
Avi Kivity0f65dd72011-04-20 13:37:53 +03001539 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001540 if (rc != X86EMUL_CONTINUE)
1541 return rc;
1542
Avi Kivity0f65dd72011-04-20 13:37:53 +03001543 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001544 if (rc != X86EMUL_CONTINUE)
1545 return rc;
1546
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001547 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001548 if (rc != X86EMUL_CONTINUE)
1549 return rc;
1550
Avi Kivity9dac77f2011-06-01 15:34:25 +03001551 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001552
1553 return rc;
1554}
1555
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001556static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001557{
1558 switch(ctxt->mode) {
1559 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001560 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001561 case X86EMUL_MODE_VM86:
1562 case X86EMUL_MODE_PROT16:
1563 case X86EMUL_MODE_PROT32:
1564 case X86EMUL_MODE_PROT64:
1565 default:
1566 /* Protected mode interrupts unimplemented yet */
1567 return X86EMUL_UNHANDLEABLE;
1568 }
1569}
1570
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001571static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001572{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001573 int rc = X86EMUL_CONTINUE;
1574 unsigned long temp_eip = 0;
1575 unsigned long temp_eflags = 0;
1576 unsigned long cs = 0;
1577 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1578 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1579 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1580 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1581
1582 /* TODO: Add stack limit check */
1583
Avi Kivity9dac77f2011-06-01 15:34:25 +03001584 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001585
1586 if (rc != X86EMUL_CONTINUE)
1587 return rc;
1588
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001589 if (temp_eip & ~0xffff)
1590 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001591
Avi Kivity9dac77f2011-06-01 15:34:25 +03001592 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001593
1594 if (rc != X86EMUL_CONTINUE)
1595 return rc;
1596
Avi Kivity9dac77f2011-06-01 15:34:25 +03001597 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001598
1599 if (rc != X86EMUL_CONTINUE)
1600 return rc;
1601
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001602 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001603
1604 if (rc != X86EMUL_CONTINUE)
1605 return rc;
1606
Avi Kivity9dac77f2011-06-01 15:34:25 +03001607 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001608
1609
Avi Kivity9dac77f2011-06-01 15:34:25 +03001610 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001611 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001612 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001613 ctxt->eflags &= ~0xffff;
1614 ctxt->eflags |= temp_eflags;
1615 }
1616
1617 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1618 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1619
1620 return rc;
1621}
1622
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001623static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001624{
1625 switch(ctxt->mode) {
1626 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001627 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001628 case X86EMUL_MODE_VM86:
1629 case X86EMUL_MODE_PROT16:
1630 case X86EMUL_MODE_PROT32:
1631 case X86EMUL_MODE_PROT64:
1632 default:
1633 /* iret from protected mode unimplemented yet */
1634 return X86EMUL_UNHANDLEABLE;
1635 }
1636}
1637
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001638static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1639{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001640 int rc;
1641 unsigned short sel;
1642
Avi Kivity9dac77f2011-06-01 15:34:25 +03001643 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001644
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001645 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001646 if (rc != X86EMUL_CONTINUE)
1647 return rc;
1648
Avi Kivity9dac77f2011-06-01 15:34:25 +03001649 ctxt->_eip = 0;
1650 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001651 return X86EMUL_CONTINUE;
1652}
1653
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001654static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001655{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001656 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001657}
1658
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001659static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001660{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001661 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001662 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001663 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001664 break;
1665 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001666 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001667 break;
1668 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001669 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001670 break;
1671 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001672 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001673 break;
1674 case 4: /* sal/shl */
1675 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001676 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001677 break;
1678 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001679 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001680 break;
1681 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001682 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 break;
1684 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001685 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001686}
1687
Avi Kivity3329ece2011-09-13 10:45:39 +03001688static int em_not(struct x86_emulate_ctxt *ctxt)
1689{
1690 ctxt->dst.val = ~ctxt->dst.val;
1691 return X86EMUL_CONTINUE;
1692}
1693
1694static int em_neg(struct x86_emulate_ctxt *ctxt)
1695{
1696 emulate_1op(ctxt, "neg");
1697 return X86EMUL_CONTINUE;
1698}
1699
1700static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1701{
1702 u8 ex = 0;
1703
1704 emulate_1op_rax_rdx(ctxt, "mul", ex);
1705 return X86EMUL_CONTINUE;
1706}
1707
1708static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1709{
1710 u8 ex = 0;
1711
1712 emulate_1op_rax_rdx(ctxt, "imul", ex);
1713 return X86EMUL_CONTINUE;
1714}
1715
1716static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717{
Avi Kivity34d1f492010-08-26 11:59:01 +03001718 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001719
Avi Kivity3329ece2011-09-13 10:45:39 +03001720 emulate_1op_rax_rdx(ctxt, "div", de);
1721 if (de)
1722 return emulate_de(ctxt);
1723 return X86EMUL_CONTINUE;
1724}
1725
1726static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1727{
1728 u8 de = 0;
1729
1730 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001731 if (de)
1732 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001733 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001734}
1735
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001736static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001738 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739
Avi Kivity9dac77f2011-06-01 15:34:25 +03001740 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001741 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001742 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743 break;
1744 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001745 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001746 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001747 case 2: /* call near abs */ {
1748 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001749 old_eip = ctxt->_eip;
1750 ctxt->_eip = ctxt->src.val;
1751 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001752 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001753 break;
1754 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001755 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001756 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001758 case 5: /* jmp far */
1759 rc = em_jmp_far(ctxt);
1760 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001762 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001764 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001765 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001766}
1767
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001768static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001769{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001770 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771
Avi Kivity9dac77f2011-06-01 15:34:25 +03001772 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1773 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1774 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1775 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001776 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001778 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1779 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001780
Laurent Vivier05f086f2007-09-24 11:10:55 +02001781 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001783 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784}
1785
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001786static int em_ret(struct x86_emulate_ctxt *ctxt)
1787{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001788 ctxt->dst.type = OP_REG;
1789 ctxt->dst.addr.reg = &ctxt->_eip;
1790 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001791 return em_pop(ctxt);
1792}
1793
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001794static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001795{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001796 int rc;
1797 unsigned long cs;
1798
Avi Kivity9dac77f2011-06-01 15:34:25 +03001799 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001800 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001801 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001802 if (ctxt->op_bytes == 4)
1803 ctxt->_eip = (u32)ctxt->_eip;
1804 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001805 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001806 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001807 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001808 return rc;
1809}
1810
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001811static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001812{
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001813 unsigned short sel;
1814 int rc;
1815
Avi Kivity9dac77f2011-06-01 15:34:25 +03001816 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001817
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001818 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001819 if (rc != X86EMUL_CONTINUE)
1820 return rc;
1821
Avi Kivity9dac77f2011-06-01 15:34:25 +03001822 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001823 return rc;
1824}
1825
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001826static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001827setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001828 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001829{
Avi Kivity1aa36612011-04-27 13:20:30 +03001830 u16 selector;
1831
Gleb Natapov79168fd2010-04-28 19:15:30 +03001832 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001833 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001834 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001835
1836 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001837 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001838 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001839 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001840 cs->type = 0x0b; /* Read, Execute, Accessed */
1841 cs->s = 1;
1842 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 cs->p = 1;
1844 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001845
Gleb Natapov79168fd2010-04-28 19:15:30 +03001846 set_desc_base(ss, 0); /* flat segment */
1847 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001848 ss->g = 1; /* 4kb granularity */
1849 ss->s = 1;
1850 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001851 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001852 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001853 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001854}
1855
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001856static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001857{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001858 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001859 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001860 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001861 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001862 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001863
1864 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001865 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001866 ctxt->mode == X86EMUL_MODE_VM86)
1867 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001869 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001870 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001871
Avi Kivity717746e2011-04-20 13:37:53 +03001872 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001873 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001874 cs_sel = (u16)(msr_data & 0xfffc);
1875 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001876
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001877 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001878 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879 cs.l = 1;
1880 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001881 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1882 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001883
Avi Kivity9dac77f2011-06-01 15:34:25 +03001884 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001885 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001886#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001887 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001888
Avi Kivity717746e2011-04-20 13:37:53 +03001889 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001890 ctxt->mode == X86EMUL_MODE_PROT64 ?
1891 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001892 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001893
Avi Kivity717746e2011-04-20 13:37:53 +03001894 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001895 ctxt->eflags &= ~(msr_data | EFLG_RF);
1896#endif
1897 } else {
1898 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001899 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001900 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001901
1902 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1903 }
1904
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001905 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001906}
1907
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001908static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001909{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001910 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001911 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001912 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001913 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001914 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001915
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001916 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001917 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001918 if (ctxt->mode == X86EMUL_MODE_REAL)
1919 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001920
1921 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1922 * Therefore, we inject an #UD.
1923 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001924 if (ctxt->mode == X86EMUL_MODE_PROT64)
1925 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001926
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001927 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001928
Avi Kivity717746e2011-04-20 13:37:53 +03001929 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001930 switch (ctxt->mode) {
1931 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001932 if ((msr_data & 0xfffc) == 0x0)
1933 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001934 break;
1935 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001936 if (msr_data == 0x0)
1937 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001938 break;
1939 }
1940
1941 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001942 cs_sel = (u16)msr_data;
1943 cs_sel &= ~SELECTOR_RPL_MASK;
1944 ss_sel = cs_sel + 8;
1945 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001946 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001947 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001948 cs.l = 1;
1949 }
1950
Avi Kivity1aa36612011-04-27 13:20:30 +03001951 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1952 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001953
Avi Kivity717746e2011-04-20 13:37:53 +03001954 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001955 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001956
Avi Kivity717746e2011-04-20 13:37:53 +03001957 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001958 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001959
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001960 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001961}
1962
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001963static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001964{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001965 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001966 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001967 u64 msr_data;
1968 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001969 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001970
Gleb Natapova0044752010-02-10 14:21:31 +02001971 /* inject #GP if in real mode or Virtual 8086 mode */
1972 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001973 ctxt->mode == X86EMUL_MODE_VM86)
1974 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001975
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001976 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001977
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02001979 usermode = X86EMUL_MODE_PROT64;
1980 else
1981 usermode = X86EMUL_MODE_PROT32;
1982
1983 cs.dpl = 3;
1984 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03001985 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001986 switch (usermode) {
1987 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001988 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001989 if ((msr_data & 0xfffc) == 0x0)
1990 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001991 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001992 break;
1993 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001994 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001995 if (msr_data == 0x0)
1996 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001997 ss_sel = cs_sel + 8;
1998 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001999 cs.l = 1;
2000 break;
2001 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002002 cs_sel |= SELECTOR_RPL_MASK;
2003 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002004
Avi Kivity1aa36612011-04-27 13:20:30 +03002005 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2006 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002007
Avi Kivity9dac77f2011-06-01 15:34:25 +03002008 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2009 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002010
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002011 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002012}
2013
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002014static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002015{
2016 int iopl;
2017 if (ctxt->mode == X86EMUL_MODE_REAL)
2018 return false;
2019 if (ctxt->mode == X86EMUL_MODE_VM86)
2020 return true;
2021 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002022 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002023}
2024
2025static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002026 u16 port, u16 len)
2027{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002028 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002029 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002030 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002031 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002032 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002033 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002034 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002035
Avi Kivity1aa36612011-04-27 13:20:30 +03002036 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002037 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002038 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002039 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002040 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002041 base = get_desc_base(&tr_seg);
2042#ifdef CONFIG_X86_64
2043 base |= ((u64)base3) << 32;
2044#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002045 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002046 if (r != X86EMUL_CONTINUE)
2047 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002048 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002049 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002050 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002051 if (r != X86EMUL_CONTINUE)
2052 return false;
2053 if ((perm >> bit_idx) & mask)
2054 return false;
2055 return true;
2056}
2057
2058static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002059 u16 port, u16 len)
2060{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002061 if (ctxt->perm_ok)
2062 return true;
2063
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002064 if (emulator_bad_iopl(ctxt))
2065 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002066 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002067
2068 ctxt->perm_ok = true;
2069
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002070 return true;
2071}
2072
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002073static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002074 struct tss_segment_16 *tss)
2075{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002076 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002077 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002078 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2079 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2080 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2081 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2082 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2083 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2084 tss->si = ctxt->regs[VCPU_REGS_RSI];
2085 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002086
Avi Kivity1aa36612011-04-27 13:20:30 +03002087 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2088 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2089 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2090 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2091 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002092}
2093
2094static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002095 struct tss_segment_16 *tss)
2096{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002097 int ret;
2098
Avi Kivity9dac77f2011-06-01 15:34:25 +03002099 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002100 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002101 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2102 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2103 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2104 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2105 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2106 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2107 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2108 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002109
2110 /*
2111 * SDM says that segment selectors are loaded before segment
2112 * descriptors
2113 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002114 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2115 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2116 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2117 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2118 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002119
2120 /*
2121 * Now load segment descriptors. If fault happenes at this stage
2122 * it is handled in a context of new task
2123 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002124 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002125 if (ret != X86EMUL_CONTINUE)
2126 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002127 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002128 if (ret != X86EMUL_CONTINUE)
2129 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002130 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
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->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002137 if (ret != X86EMUL_CONTINUE)
2138 return ret;
2139
2140 return X86EMUL_CONTINUE;
2141}
2142
2143static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002144 u16 tss_selector, u16 old_tss_sel,
2145 ulong old_tss_base, struct desc_struct *new_desc)
2146{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002147 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148 struct tss_segment_16 tss_seg;
2149 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002150 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002151
Avi Kivity0f65dd72011-04-20 13:37:53 +03002152 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002153 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002154 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002155 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002156 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002157
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002158 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002159
Avi Kivity0f65dd72011-04-20 13:37:53 +03002160 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002161 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002162 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002164 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165
Avi Kivity0f65dd72011-04-20 13:37:53 +03002166 ret = ops->read_std(ctxt, new_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
2172 if (old_tss_sel != 0xffff) {
2173 tss_seg.prev_task_link = old_tss_sel;
2174
Avi Kivity0f65dd72011-04-20 13:37:53 +03002175 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002176 &tss_seg.prev_task_link,
2177 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002178 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002179 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002181 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002182 }
2183
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002184 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185}
2186
2187static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002188 struct tss_segment_32 *tss)
2189{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002190 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002191 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002192 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002193 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2194 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2195 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2196 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2197 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2198 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2199 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2200 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002201
Avi Kivity1aa36612011-04-27 13:20:30 +03002202 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2203 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2204 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2205 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2206 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2207 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2208 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002209}
2210
2211static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002212 struct tss_segment_32 *tss)
2213{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002214 int ret;
2215
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002216 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002217 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002218 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002219 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002220 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2221 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2222 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2223 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2224 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2225 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2226 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2227 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002228
2229 /*
2230 * SDM says that segment selectors are loaded before segment
2231 * descriptors
2232 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002233 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2234 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2235 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2236 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2237 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2238 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2239 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002240
2241 /*
2242 * Now load segment descriptors. If fault happenes at this stage
2243 * it is handled in a context of new task
2244 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002245 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002246 if (ret != X86EMUL_CONTINUE)
2247 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002248 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002249 if (ret != X86EMUL_CONTINUE)
2250 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002251 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
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->ss, VCPU_SREG_SS);
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->ds, VCPU_SREG_DS);
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->fs, VCPU_SREG_FS);
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->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002264 if (ret != X86EMUL_CONTINUE)
2265 return ret;
2266
2267 return X86EMUL_CONTINUE;
2268}
2269
2270static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002271 u16 tss_selector, u16 old_tss_sel,
2272 ulong old_tss_base, struct desc_struct *new_desc)
2273{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002274 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002275 struct tss_segment_32 tss_seg;
2276 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002277 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002278
Avi Kivity0f65dd72011-04-20 13:37:53 +03002279 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002280 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002281 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002283 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002284
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002285 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286
Avi Kivity0f65dd72011-04-20 13:37:53 +03002287 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002288 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002289 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002290 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002291 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292
Avi Kivity0f65dd72011-04-20 13:37:53 +03002293 ret = ops->read_std(ctxt, new_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
2299 if (old_tss_sel != 0xffff) {
2300 tss_seg.prev_task_link = old_tss_sel;
2301
Avi Kivity0f65dd72011-04-20 13:37:53 +03002302 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303 &tss_seg.prev_task_link,
2304 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002305 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002306 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002307 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002308 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002309 }
2310
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002311 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312}
2313
2314static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002315 u16 tss_selector, int reason,
2316 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002318 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002319 struct desc_struct curr_tss_desc, next_tss_desc;
2320 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002321 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002322 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002323 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002324 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002325
2326 /* FIXME: old_tss_base == ~0 ? */
2327
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002328 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002329 if (ret != X86EMUL_CONTINUE)
2330 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002331 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002332 if (ret != X86EMUL_CONTINUE)
2333 return ret;
2334
2335 /* FIXME: check that next_tss_desc is tss */
2336
2337 if (reason != TASK_SWITCH_IRET) {
2338 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002339 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002340 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002341 }
2342
Gleb Natapovceffb452010-03-18 15:20:19 +02002343 desc_limit = desc_limit_scaled(&next_tss_desc);
2344 if (!next_tss_desc.p ||
2345 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2346 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002347 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002348 return X86EMUL_PROPAGATE_FAULT;
2349 }
2350
2351 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2352 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002353 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002354 }
2355
2356 if (reason == TASK_SWITCH_IRET)
2357 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2358
2359 /* set back link to prev task only if NT bit is set in eflags
2360 note that old_tss_sel is not used afetr this point */
2361 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2362 old_tss_sel = 0xffff;
2363
2364 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002365 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002366 old_tss_base, &next_tss_desc);
2367 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002368 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002369 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002370 if (ret != X86EMUL_CONTINUE)
2371 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002372
2373 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2374 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2375
2376 if (reason != TASK_SWITCH_IRET) {
2377 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002378 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002379 }
2380
Avi Kivity717746e2011-04-20 13:37:53 +03002381 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002382 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002383
Jan Kiszkae269fb22010-04-14 15:51:09 +02002384 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002385 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2386 ctxt->lock_prefix = 0;
2387 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002388 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002389 }
2390
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002391 return ret;
2392}
2393
2394int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002395 u16 tss_selector, int reason,
2396 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002398 int rc;
2399
Avi Kivity9dac77f2011-06-01 15:34:25 +03002400 ctxt->_eip = ctxt->eip;
2401 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002403 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002404 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002406 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002407 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002409 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410}
2411
Avi Kivity90de84f2010-11-17 15:28:21 +02002412static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002413 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002414{
Gleb Natapova682e352010-03-18 15:20:21 +02002415 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2416
Avi Kivity9dac77f2011-06-01 15:34:25 +03002417 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2418 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002419 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002420}
2421
Avi Kivity7af04fc2010-08-18 14:16:35 +03002422static int em_das(struct x86_emulate_ctxt *ctxt)
2423{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002424 u8 al, old_al;
2425 bool af, cf, old_cf;
2426
2427 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002428 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002429
2430 old_al = al;
2431 old_cf = cf;
2432 cf = false;
2433 af = ctxt->eflags & X86_EFLAGS_AF;
2434 if ((al & 0x0f) > 9 || af) {
2435 al -= 6;
2436 cf = old_cf | (al >= 250);
2437 af = true;
2438 } else {
2439 af = false;
2440 }
2441 if (old_al > 0x99 || old_cf) {
2442 al -= 0x60;
2443 cf = true;
2444 }
2445
Avi Kivity9dac77f2011-06-01 15:34:25 +03002446 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002447 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002448 ctxt->src.type = OP_IMM;
2449 ctxt->src.val = 0;
2450 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002451 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002452 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2453 if (cf)
2454 ctxt->eflags |= X86_EFLAGS_CF;
2455 if (af)
2456 ctxt->eflags |= X86_EFLAGS_AF;
2457 return X86EMUL_CONTINUE;
2458}
2459
Avi Kivity0ef753b2010-08-18 14:51:45 +03002460static int em_call_far(struct x86_emulate_ctxt *ctxt)
2461{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002462 u16 sel, old_cs;
2463 ulong old_eip;
2464 int rc;
2465
Avi Kivity1aa36612011-04-27 13:20:30 +03002466 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002467 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002468
Avi Kivity9dac77f2011-06-01 15:34:25 +03002469 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002470 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002471 return X86EMUL_CONTINUE;
2472
Avi Kivity9dac77f2011-06-01 15:34:25 +03002473 ctxt->_eip = 0;
2474 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002475
Avi Kivity9dac77f2011-06-01 15:34:25 +03002476 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002477 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002478 if (rc != X86EMUL_CONTINUE)
2479 return rc;
2480
Avi Kivity9dac77f2011-06-01 15:34:25 +03002481 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002482 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002483}
2484
Avi Kivity40ece7c2010-08-18 15:12:09 +03002485static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2486{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002487 int rc;
2488
Avi Kivity9dac77f2011-06-01 15:34:25 +03002489 ctxt->dst.type = OP_REG;
2490 ctxt->dst.addr.reg = &ctxt->_eip;
2491 ctxt->dst.bytes = ctxt->op_bytes;
2492 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002493 if (rc != X86EMUL_CONTINUE)
2494 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002495 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002496 return X86EMUL_CONTINUE;
2497}
2498
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002499static int em_add(struct x86_emulate_ctxt *ctxt)
2500{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002501 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002502 return X86EMUL_CONTINUE;
2503}
2504
2505static int em_or(struct x86_emulate_ctxt *ctxt)
2506{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002507 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002508 return X86EMUL_CONTINUE;
2509}
2510
2511static int em_adc(struct x86_emulate_ctxt *ctxt)
2512{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002513 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002514 return X86EMUL_CONTINUE;
2515}
2516
2517static int em_sbb(struct x86_emulate_ctxt *ctxt)
2518{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002519 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002520 return X86EMUL_CONTINUE;
2521}
2522
2523static int em_and(struct x86_emulate_ctxt *ctxt)
2524{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002525 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002526 return X86EMUL_CONTINUE;
2527}
2528
2529static int em_sub(struct x86_emulate_ctxt *ctxt)
2530{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002531 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002532 return X86EMUL_CONTINUE;
2533}
2534
2535static int em_xor(struct x86_emulate_ctxt *ctxt)
2536{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002537 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002538 return X86EMUL_CONTINUE;
2539}
2540
2541static int em_cmp(struct x86_emulate_ctxt *ctxt)
2542{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002543 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002544 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002545 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002546 return X86EMUL_CONTINUE;
2547}
2548
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002549static int em_test(struct x86_emulate_ctxt *ctxt)
2550{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002551 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002552 /* Disable writeback. */
2553 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002554 return X86EMUL_CONTINUE;
2555}
2556
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002557static int em_xchg(struct x86_emulate_ctxt *ctxt)
2558{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002559 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002560 ctxt->src.val = ctxt->dst.val;
2561 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002562
2563 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002564 ctxt->dst.val = ctxt->src.orig_val;
2565 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002566 return X86EMUL_CONTINUE;
2567}
2568
Avi Kivity5c82aa22010-08-18 18:31:43 +03002569static int em_imul(struct x86_emulate_ctxt *ctxt)
2570{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002571 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002572 return X86EMUL_CONTINUE;
2573}
2574
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002575static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2576{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002577 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002578 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002579}
2580
Avi Kivity61429142010-08-19 15:13:00 +03002581static int em_cwd(struct x86_emulate_ctxt *ctxt)
2582{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002583 ctxt->dst.type = OP_REG;
2584 ctxt->dst.bytes = ctxt->src.bytes;
2585 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2586 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002587
2588 return X86EMUL_CONTINUE;
2589}
2590
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002591static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2592{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002593 u64 tsc = 0;
2594
Avi Kivity717746e2011-04-20 13:37:53 +03002595 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002596 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2597 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002598 return X86EMUL_CONTINUE;
2599}
2600
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002601static int em_mov(struct x86_emulate_ctxt *ctxt)
2602{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002603 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002604 return X86EMUL_CONTINUE;
2605}
2606
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002607static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2608{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002609 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002610 return emulate_ud(ctxt);
2611
Avi Kivity9dac77f2011-06-01 15:34:25 +03002612 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002613 return X86EMUL_CONTINUE;
2614}
2615
2616static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2617{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002618 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002619
Avi Kivity9dac77f2011-06-01 15:34:25 +03002620 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002621 return emulate_ud(ctxt);
2622
Avi Kivity9dac77f2011-06-01 15:34:25 +03002623 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002624 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2625
2626 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002627 ctxt->dst.type = OP_NONE;
2628 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002629}
2630
Avi Kivityaa97bb42010-01-20 18:09:23 +02002631static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2632{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002633 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002634 return X86EMUL_CONTINUE;
2635}
2636
Avi Kivity38503912011-03-31 18:48:09 +02002637static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2638{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002639 int rc;
2640 ulong linear;
2641
Avi Kivity9dac77f2011-06-01 15:34:25 +03002642 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002643 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002644 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002645 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002646 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002647 return X86EMUL_CONTINUE;
2648}
2649
Avi Kivity2d04a052011-04-20 15:32:49 +03002650static int em_clts(struct x86_emulate_ctxt *ctxt)
2651{
2652 ulong cr0;
2653
2654 cr0 = ctxt->ops->get_cr(ctxt, 0);
2655 cr0 &= ~X86_CR0_TS;
2656 ctxt->ops->set_cr(ctxt, 0, cr0);
2657 return X86EMUL_CONTINUE;
2658}
2659
Avi Kivity26d05cc2011-04-21 12:07:59 +03002660static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2661{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002662 int rc;
2663
Avi Kivity9dac77f2011-06-01 15:34:25 +03002664 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002665 return X86EMUL_UNHANDLEABLE;
2666
2667 rc = ctxt->ops->fix_hypercall(ctxt);
2668 if (rc != X86EMUL_CONTINUE)
2669 return rc;
2670
2671 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002672 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002673 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002674 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002675 return X86EMUL_CONTINUE;
2676}
2677
2678static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2679{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002680 struct desc_ptr desc_ptr;
2681 int rc;
2682
Avi Kivity9dac77f2011-06-01 15:34:25 +03002683 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002684 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002685 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002686 if (rc != X86EMUL_CONTINUE)
2687 return rc;
2688 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2689 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002690 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002691 return X86EMUL_CONTINUE;
2692}
2693
Avi Kivity5ef39c72011-04-21 12:21:50 +03002694static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002695{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002696 int rc;
2697
Avi Kivity5ef39c72011-04-21 12:21:50 +03002698 rc = ctxt->ops->fix_hypercall(ctxt);
2699
Avi Kivity26d05cc2011-04-21 12:07:59 +03002700 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002701 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002702 return rc;
2703}
2704
2705static int em_lidt(struct x86_emulate_ctxt *ctxt)
2706{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002707 struct desc_ptr desc_ptr;
2708 int rc;
2709
Avi Kivity9dac77f2011-06-01 15:34:25 +03002710 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002711 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002712 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002713 if (rc != X86EMUL_CONTINUE)
2714 return rc;
2715 ctxt->ops->set_idt(ctxt, &desc_ptr);
2716 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002717 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002718 return X86EMUL_CONTINUE;
2719}
2720
2721static int em_smsw(struct x86_emulate_ctxt *ctxt)
2722{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002723 ctxt->dst.bytes = 2;
2724 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002725 return X86EMUL_CONTINUE;
2726}
2727
2728static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2729{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002730 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002731 | (ctxt->src.val & 0x0f));
2732 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002733 return X86EMUL_CONTINUE;
2734}
2735
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002736static int em_loop(struct x86_emulate_ctxt *ctxt)
2737{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002738 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2739 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2740 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2741 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002742
2743 return X86EMUL_CONTINUE;
2744}
2745
2746static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2747{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002748 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2749 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002750
2751 return X86EMUL_CONTINUE;
2752}
2753
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002754static int em_cli(struct x86_emulate_ctxt *ctxt)
2755{
2756 if (emulator_bad_iopl(ctxt))
2757 return emulate_gp(ctxt, 0);
2758
2759 ctxt->eflags &= ~X86_EFLAGS_IF;
2760 return X86EMUL_CONTINUE;
2761}
2762
2763static int em_sti(struct x86_emulate_ctxt *ctxt)
2764{
2765 if (emulator_bad_iopl(ctxt))
2766 return emulate_gp(ctxt, 0);
2767
2768 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2769 ctxt->eflags |= X86_EFLAGS_IF;
2770 return X86EMUL_CONTINUE;
2771}
2772
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002773static bool valid_cr(int nr)
2774{
2775 switch (nr) {
2776 case 0:
2777 case 2 ... 4:
2778 case 8:
2779 return true;
2780 default:
2781 return false;
2782 }
2783}
2784
2785static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2786{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002787 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002788 return emulate_ud(ctxt);
2789
2790 return X86EMUL_CONTINUE;
2791}
2792
2793static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2794{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002795 u64 new_val = ctxt->src.val64;
2796 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002797 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002798
2799 static u64 cr_reserved_bits[] = {
2800 0xffffffff00000000ULL,
2801 0, 0, 0, /* CR3 checked later */
2802 CR4_RESERVED_BITS,
2803 0, 0, 0,
2804 CR8_RESERVED_BITS,
2805 };
2806
2807 if (!valid_cr(cr))
2808 return emulate_ud(ctxt);
2809
2810 if (new_val & cr_reserved_bits[cr])
2811 return emulate_gp(ctxt, 0);
2812
2813 switch (cr) {
2814 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002815 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002816 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2817 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2818 return emulate_gp(ctxt, 0);
2819
Avi Kivity717746e2011-04-20 13:37:53 +03002820 cr4 = ctxt->ops->get_cr(ctxt, 4);
2821 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002822
2823 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2824 !(cr4 & X86_CR4_PAE))
2825 return emulate_gp(ctxt, 0);
2826
2827 break;
2828 }
2829 case 3: {
2830 u64 rsvd = 0;
2831
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002832 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2833 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002834 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002835 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002836 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002837 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002838 rsvd = CR3_NONPAE_RESERVED_BITS;
2839
2840 if (new_val & rsvd)
2841 return emulate_gp(ctxt, 0);
2842
2843 break;
2844 }
2845 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002846 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002847
Avi Kivity717746e2011-04-20 13:37:53 +03002848 cr4 = ctxt->ops->get_cr(ctxt, 4);
2849 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002850
2851 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2852 return emulate_gp(ctxt, 0);
2853
2854 break;
2855 }
2856 }
2857
2858 return X86EMUL_CONTINUE;
2859}
2860
Joerg Roedel3b88e412011-04-04 12:39:29 +02002861static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2862{
2863 unsigned long dr7;
2864
Avi Kivity717746e2011-04-20 13:37:53 +03002865 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002866
2867 /* Check if DR7.Global_Enable is set */
2868 return dr7 & (1 << 13);
2869}
2870
2871static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2872{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002873 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002874 u64 cr4;
2875
2876 if (dr > 7)
2877 return emulate_ud(ctxt);
2878
Avi Kivity717746e2011-04-20 13:37:53 +03002879 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002880 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2881 return emulate_ud(ctxt);
2882
2883 if (check_dr7_gd(ctxt))
2884 return emulate_db(ctxt);
2885
2886 return X86EMUL_CONTINUE;
2887}
2888
2889static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2890{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002891 u64 new_val = ctxt->src.val64;
2892 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002893
2894 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2895 return emulate_gp(ctxt, 0);
2896
2897 return check_dr_read(ctxt);
2898}
2899
Joerg Roedel01de8b02011-04-04 12:39:31 +02002900static int check_svme(struct x86_emulate_ctxt *ctxt)
2901{
2902 u64 efer;
2903
Avi Kivity717746e2011-04-20 13:37:53 +03002904 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002905
2906 if (!(efer & EFER_SVME))
2907 return emulate_ud(ctxt);
2908
2909 return X86EMUL_CONTINUE;
2910}
2911
2912static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2913{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002914 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002915
2916 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002917 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002918 return emulate_gp(ctxt, 0);
2919
2920 return check_svme(ctxt);
2921}
2922
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002923static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2924{
Avi Kivity717746e2011-04-20 13:37:53 +03002925 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002926
Avi Kivity717746e2011-04-20 13:37:53 +03002927 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002928 return emulate_ud(ctxt);
2929
2930 return X86EMUL_CONTINUE;
2931}
2932
Joerg Roedel80612522011-04-04 12:39:33 +02002933static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2934{
Avi Kivity717746e2011-04-20 13:37:53 +03002935 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002936 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002937
Avi Kivity717746e2011-04-20 13:37:53 +03002938 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002939 (rcx > 3))
2940 return emulate_gp(ctxt, 0);
2941
2942 return X86EMUL_CONTINUE;
2943}
2944
Joerg Roedelf6511932011-04-04 12:39:35 +02002945static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2946{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002947 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2948 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002949 return emulate_gp(ctxt, 0);
2950
2951 return X86EMUL_CONTINUE;
2952}
2953
2954static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2955{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002956 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2957 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002958 return emulate_gp(ctxt, 0);
2959
2960 return X86EMUL_CONTINUE;
2961}
2962
Avi Kivity73fba5f2010-07-29 15:11:53 +03002963#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002964#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002965#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2966 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002967#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002968#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002969#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002970#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002971#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002972#define II(_f, _e, _i) \
2973 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002974#define IIP(_f, _e, _i, _p) \
2975 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2976 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002977#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002978
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002979#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002980#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002981#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2982
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002983#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2984 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2985 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002986
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002987static struct opcode group7_rm1[] = {
2988 DI(SrcNone | ModRM | Priv, monitor),
2989 DI(SrcNone | ModRM | Priv, mwait),
2990 N, N, N, N, N, N,
2991};
2992
Joerg Roedel01de8b02011-04-04 12:39:31 +02002993static struct opcode group7_rm3[] = {
2994 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03002995 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002996 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
2997 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
2998 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
2999 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3000 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3001 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3002};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003003
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003004static struct opcode group7_rm7[] = {
3005 N,
3006 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3007 N, N, N, N, N, N,
3008};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003009
Avi Kivity73fba5f2010-07-29 15:11:53 +03003010static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003011 I(Lock, em_add),
3012 I(Lock, em_or),
3013 I(Lock, em_adc),
3014 I(Lock, em_sbb),
3015 I(Lock, em_and),
3016 I(Lock, em_sub),
3017 I(Lock, em_xor),
3018 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003019};
3020
3021static struct opcode group1A[] = {
3022 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3023};
3024
3025static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003026 I(DstMem | SrcImm | ModRM, em_test),
3027 I(DstMem | SrcImm | ModRM, em_test),
3028 I(DstMem | SrcNone | ModRM | Lock, em_not),
3029 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3030 I(SrcMem | ModRM, em_mul_ex),
3031 I(SrcMem | ModRM, em_imul_ex),
3032 I(SrcMem | ModRM, em_div_ex),
3033 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003034};
3035
3036static struct opcode group4[] = {
3037 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3038 N, N, N, N, N, N,
3039};
3040
3041static struct opcode group5[] = {
3042 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003043 D(SrcMem | ModRM | Stack),
3044 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003045 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3046 D(SrcMem | ModRM | Stack), N,
3047};
3048
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003049static struct opcode group6[] = {
3050 DI(ModRM | Prot, sldt),
3051 DI(ModRM | Prot, str),
3052 DI(ModRM | Prot | Priv, lldt),
3053 DI(ModRM | Prot | Priv, ltr),
3054 N, N, N, N,
3055};
3056
Avi Kivity73fba5f2010-07-29 15:11:53 +03003057static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003058 DI(ModRM | Mov | DstMem | Priv, sgdt),
3059 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003060 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3061 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3062 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3063 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3064 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003065}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003066 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3067 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003068 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003069 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3070 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003071} };
3072
3073static struct opcode group8[] = {
3074 N, N, N, N,
3075 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3076 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3077};
3078
3079static struct group_dual group9 = { {
3080 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3081}, {
3082 N, N, N, N, N, N, N, N,
3083} };
3084
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003085static struct opcode group11[] = {
3086 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3087};
3088
Avi Kivityaa97bb42010-01-20 18:09:23 +02003089static struct gprefix pfx_0f_6f_0f_7f = {
3090 N, N, N, I(Sse, em_movdqu),
3091};
3092
Avi Kivity73fba5f2010-07-29 15:11:53 +03003093static struct opcode opcode_table[256] = {
3094 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003095 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003096 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3097 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003098 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003099 D(ImplicitOps | Stack | No64), N,
3100 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003101 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003102 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3103 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003104 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003105 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3106 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003107 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003108 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003109 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003110 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003111 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003112 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003113 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003114 /* 0x40 - 0x4F */
3115 X16(D(DstReg)),
3116 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003117 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003118 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003119 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003120 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003121 I(ImplicitOps | Stack | No64, em_pusha),
3122 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003123 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3124 N, N, N, N,
3125 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003126 I(SrcImm | Mov | Stack, em_push),
3127 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003128 I(SrcImmByte | Mov | Stack, em_push),
3129 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003130 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3131 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003132 /* 0x70 - 0x7F */
3133 X16(D(SrcImmByte)),
3134 /* 0x80 - 0x87 */
3135 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3136 G(DstMem | SrcImm | ModRM | Group, group1),
3137 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3138 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003139 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003140 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003141 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003142 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3143 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003144 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3145 D(ModRM | SrcMem | NoAccess | DstReg),
3146 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3147 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003148 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003149 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003150 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003151 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003152 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003153 II(ImplicitOps | Stack, em_pushf, pushf),
3154 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003155 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003156 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3157 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3158 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003159 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003160 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003161 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003162 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3163 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003164 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003165 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003166 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003167 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003168 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003169 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003170 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003171 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003172 I(ImplicitOps | Stack, em_ret),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003173 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003174 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003175 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003176 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003177 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003178 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003179 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003180 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003181 N, N, N, N,
3182 /* 0xD8 - 0xDF */
3183 N, N, N, N, N, N, N, N,
3184 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003185 X3(I(SrcImmByte, em_loop)),
3186 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003187 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3188 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003189 /* 0xE8 - 0xEF */
3190 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003191 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003192 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3193 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003194 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003195 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003196 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3197 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003198 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003199 D(ImplicitOps), D(ImplicitOps),
3200 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003201 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3202};
3203
3204static struct opcode twobyte_table[256] = {
3205 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003206 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003207 N, I(ImplicitOps | VendorSpecific, em_syscall),
3208 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003209 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003210 N, D(ImplicitOps | ModRM), N, N,
3211 /* 0x10 - 0x1F */
3212 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3213 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003214 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003215 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003216 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003217 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003218 N, N, N, N,
3219 N, N, N, N, N, N, N, N,
3220 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003221 DI(ImplicitOps | Priv, wrmsr),
3222 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3223 DI(ImplicitOps | Priv, rdmsr),
3224 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003225 I(ImplicitOps | VendorSpecific, em_sysenter),
3226 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003227 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003228 N, N, N, N, N, N, N, N,
3229 /* 0x40 - 0x4F */
3230 X16(D(DstReg | SrcMem | ModRM | Mov)),
3231 /* 0x50 - 0x5F */
3232 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3233 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003234 N, N, N, N,
3235 N, N, N, N,
3236 N, N, N, N,
3237 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003238 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003239 N, N, N, N,
3240 N, N, N, N,
3241 N, N, N, N,
3242 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003243 /* 0x80 - 0x8F */
3244 X16(D(SrcImm)),
3245 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003246 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003247 /* 0xA0 - 0xA7 */
3248 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003249 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003250 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3251 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3252 /* 0xA8 - 0xAF */
3253 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003254 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003255 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3256 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003257 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003258 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003259 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003260 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3261 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3262 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003263 /* 0xB8 - 0xBF */
3264 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003265 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003266 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3267 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003268 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003269 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003270 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003271 N, N, N, GD(0, &group9),
3272 N, N, N, N, N, N, N, N,
3273 /* 0xD0 - 0xDF */
3274 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3275 /* 0xE0 - 0xEF */
3276 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3277 /* 0xF0 - 0xFF */
3278 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3279};
3280
3281#undef D
3282#undef N
3283#undef G
3284#undef GD
3285#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003286#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003287#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003288
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003289#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003290#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003291#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003292#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003293
Avi Kivity9dac77f2011-06-01 15:34:25 +03003294static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003295{
3296 unsigned size;
3297
Avi Kivity9dac77f2011-06-01 15:34:25 +03003298 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003299 if (size == 8)
3300 size = 4;
3301 return size;
3302}
3303
3304static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3305 unsigned size, bool sign_extension)
3306{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003307 int rc = X86EMUL_CONTINUE;
3308
3309 op->type = OP_IMM;
3310 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003311 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003312 /* NB. Immediates are sign-extended as necessary. */
3313 switch (op->bytes) {
3314 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003315 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003316 break;
3317 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003318 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003319 break;
3320 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003321 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003322 break;
3323 }
3324 if (!sign_extension) {
3325 switch (op->bytes) {
3326 case 1:
3327 op->val &= 0xff;
3328 break;
3329 case 2:
3330 op->val &= 0xffff;
3331 break;
3332 case 4:
3333 op->val &= 0xffffffff;
3334 break;
3335 }
3336 }
3337done:
3338 return rc;
3339}
3340
Avi Kivitya99455492011-09-13 10:45:41 +03003341static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3342 unsigned d)
3343{
3344 int rc = X86EMUL_CONTINUE;
3345
3346 switch (d) {
3347 case OpReg:
3348 decode_register_operand(ctxt, op,
3349 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
3350 break;
3351 case OpImmUByte:
3352 op->type = OP_IMM;
3353 op->addr.mem.ea = ctxt->_eip;
3354 op->bytes = 1;
3355 op->val = insn_fetch(u8, ctxt);
3356 break;
3357 case OpMem:
3358 case OpMem64:
3359 *op = ctxt->memop;
3360 ctxt->memopp = op;
3361 if (d == OpMem64)
3362 op->bytes = 8;
3363 else
3364 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3365 if (ctxt->d & BitOp)
3366 fetch_bit_operand(ctxt);
3367 op->orig_val = op->val;
3368 break;
3369 case OpAcc:
3370 op->type = OP_REG;
3371 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3372 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3373 fetch_register_operand(op);
3374 op->orig_val = op->val;
3375 break;
3376 case OpDI:
3377 op->type = OP_MEM;
3378 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3379 op->addr.mem.ea =
3380 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3381 op->addr.mem.seg = VCPU_SREG_ES;
3382 op->val = 0;
3383 break;
3384 case OpDX:
3385 op->type = OP_REG;
3386 op->bytes = 2;
3387 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3388 fetch_register_operand(op);
3389 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003390 case OpCL:
3391 op->bytes = 1;
3392 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3393 break;
3394 case OpImmByte:
3395 rc = decode_imm(ctxt, op, 1, true);
3396 break;
3397 case OpOne:
3398 op->bytes = 1;
3399 op->val = 1;
3400 break;
3401 case OpImm:
3402 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3403 break;
Avi Kivitya99455492011-09-13 10:45:41 +03003404 case OpImplicit:
3405 /* Special instructions do their own operand decoding. */
3406 default:
3407 op->type = OP_NONE; /* Disable writeback. */
3408 break;
3409 }
3410
3411done:
3412 return rc;
3413}
3414
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003415int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003416{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003417 int rc = X86EMUL_CONTINUE;
3418 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003419 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003420 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003421 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003422
Avi Kivityf09ed832011-09-13 10:45:40 +03003423 ctxt->memop.type = OP_NONE;
3424 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003425 ctxt->_eip = ctxt->eip;
3426 ctxt->fetch.start = ctxt->_eip;
3427 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003428 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003429 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003430
3431 switch (mode) {
3432 case X86EMUL_MODE_REAL:
3433 case X86EMUL_MODE_VM86:
3434 case X86EMUL_MODE_PROT16:
3435 def_op_bytes = def_ad_bytes = 2;
3436 break;
3437 case X86EMUL_MODE_PROT32:
3438 def_op_bytes = def_ad_bytes = 4;
3439 break;
3440#ifdef CONFIG_X86_64
3441 case X86EMUL_MODE_PROT64:
3442 def_op_bytes = 4;
3443 def_ad_bytes = 8;
3444 break;
3445#endif
3446 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003447 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003448 }
3449
Avi Kivity9dac77f2011-06-01 15:34:25 +03003450 ctxt->op_bytes = def_op_bytes;
3451 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003452
3453 /* Legacy prefixes. */
3454 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003455 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003456 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003457 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003458 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003459 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003460 break;
3461 case 0x67: /* address-size override */
3462 if (mode == X86EMUL_MODE_PROT64)
3463 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003464 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003465 else
3466 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003467 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003468 break;
3469 case 0x26: /* ES override */
3470 case 0x2e: /* CS override */
3471 case 0x36: /* SS override */
3472 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003473 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003474 break;
3475 case 0x64: /* FS override */
3476 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003477 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003478 break;
3479 case 0x40 ... 0x4f: /* REX */
3480 if (mode != X86EMUL_MODE_PROT64)
3481 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003482 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003483 continue;
3484 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003485 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003486 break;
3487 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003488 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003489 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003490 break;
3491 default:
3492 goto done_prefixes;
3493 }
3494
3495 /* Any legacy prefix after a REX prefix nullifies its effect. */
3496
Avi Kivity9dac77f2011-06-01 15:34:25 +03003497 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003498 }
3499
3500done_prefixes:
3501
3502 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003503 if (ctxt->rex_prefix & 8)
3504 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003505
3506 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003507 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003508 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003509 if (ctxt->b == 0x0f) {
3510 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003511 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003512 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003513 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003514 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003515
Avi Kivity9dac77f2011-06-01 15:34:25 +03003516 while (ctxt->d & GroupMask) {
3517 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003518 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003519 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003520 --ctxt->_eip;
3521 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003522 opcode = opcode.u.group[goffset];
3523 break;
3524 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003525 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003526 --ctxt->_eip;
3527 goffset = (ctxt->modrm >> 3) & 7;
3528 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003529 opcode = opcode.u.gdual->mod3[goffset];
3530 else
3531 opcode = opcode.u.gdual->mod012[goffset];
3532 break;
3533 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003534 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003535 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003536 break;
3537 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003538 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003539 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003540 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003541 switch (simd_prefix) {
3542 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3543 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3544 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3545 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3546 }
3547 break;
3548 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003549 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003550 }
Avi Kivity46561642011-04-24 14:09:59 +03003551
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003552 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003553 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003554 }
3555
Avi Kivity9dac77f2011-06-01 15:34:25 +03003556 ctxt->execute = opcode.u.execute;
3557 ctxt->check_perm = opcode.check_perm;
3558 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003559
3560 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003561 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003562 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003563
Avi Kivity9dac77f2011-06-01 15:34:25 +03003564 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003565 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003566
Avi Kivity9dac77f2011-06-01 15:34:25 +03003567 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3568 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003569
Avi Kivity9dac77f2011-06-01 15:34:25 +03003570 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003571 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003572 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003573 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003574 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003575 }
3576
Avi Kivity9dac77f2011-06-01 15:34:25 +03003577 if (ctxt->d & Sse)
3578 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003579
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003580 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003581 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003582 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003583 if (!ctxt->has_seg_override)
3584 set_seg_override(ctxt, ctxt->modrm_seg);
3585 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003586 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003587 if (rc != X86EMUL_CONTINUE)
3588 goto done;
3589
Avi Kivity9dac77f2011-06-01 15:34:25 +03003590 if (!ctxt->has_seg_override)
3591 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003592
Avi Kivityf09ed832011-09-13 10:45:40 +03003593 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003594
Avi Kivityf09ed832011-09-13 10:45:40 +03003595 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
3596 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003597
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003598 /*
3599 * Decode and fetch the source operand: register, memory
3600 * or immediate.
3601 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003602 switch (ctxt->d & SrcMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003603 case SrcNone:
3604 break;
3605 case SrcReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003606 decode_register_operand(ctxt, &ctxt->src, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003607 break;
3608 case SrcMem16:
Avi Kivityf09ed832011-09-13 10:45:40 +03003609 ctxt->memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003610 goto srcmem_common;
3611 case SrcMem32:
Avi Kivityf09ed832011-09-13 10:45:40 +03003612 ctxt->memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003613 goto srcmem_common;
3614 case SrcMem:
Avi Kivityf09ed832011-09-13 10:45:40 +03003615 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003616 srcmem_common:
Avi Kivityf09ed832011-09-13 10:45:40 +03003617 ctxt->src = ctxt->memop;
3618 ctxt->memopp = &ctxt->src;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003619 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003620 case SrcImmU16:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003621 rc = decode_imm(ctxt, &ctxt->src, 2, false);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003622 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003623 case SrcImm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003624 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003625 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003626 case SrcImmU:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003627 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003628 break;
3629 case SrcImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003630 rc = decode_imm(ctxt, &ctxt->src, 1, true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003631 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003632 case SrcImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003633 rc = decode_imm(ctxt, &ctxt->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003634 break;
3635 case SrcAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003636 ctxt->src.type = OP_REG;
3637 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3638 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3639 fetch_register_operand(&ctxt->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003640 break;
3641 case SrcOne:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003642 ctxt->src.bytes = 1;
3643 ctxt->src.val = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003644 break;
3645 case SrcSI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003646 ctxt->src.type = OP_MEM;
3647 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3648 ctxt->src.addr.mem.ea =
3649 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3650 ctxt->src.addr.mem.seg = seg_override(ctxt);
3651 ctxt->src.val = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003652 break;
3653 case SrcImmFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003654 ctxt->src.type = OP_IMM;
3655 ctxt->src.addr.mem.ea = ctxt->_eip;
3656 ctxt->src.bytes = ctxt->op_bytes + 2;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003657 insn_fetch_arr(ctxt->src.valptr, ctxt->src.bytes, ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003658 break;
3659 case SrcMemFAddr:
Avi Kivityf09ed832011-09-13 10:45:40 +03003660 ctxt->memop.bytes = ctxt->op_bytes + 2;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003661 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003662 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003663 case SrcDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003664 ctxt->src.type = OP_REG;
3665 ctxt->src.bytes = 2;
3666 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3667 fetch_register_operand(&ctxt->src);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003668 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003669 }
3670
Avi Kivity39f21ee2010-08-18 19:20:21 +03003671 if (rc != X86EMUL_CONTINUE)
3672 goto done;
3673
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003674 /*
3675 * Decode and fetch the second source operand: register, memory
3676 * or immediate.
3677 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03003678 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003679 if (rc != X86EMUL_CONTINUE)
3680 goto done;
3681
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003682 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03003683 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003684
3685done:
Avi Kivityf09ed832011-09-13 10:45:40 +03003686 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
3687 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003688
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003689 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003690}
3691
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003692static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3693{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003694 /* The second termination condition only applies for REPE
3695 * and REPNE. Test if the repeat string operation prefix is
3696 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3697 * corresponding termination condition according to:
3698 * - if REPE/REPZ and ZF = 0 then done
3699 * - if REPNE/REPNZ and ZF = 1 then done
3700 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003701 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3702 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3703 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003704 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003705 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003706 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3707 return true;
3708
3709 return false;
3710}
3711
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003712int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003713{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003714 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003715 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003716 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003717 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003718
Avi Kivity9dac77f2011-06-01 15:34:25 +03003719 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003720
Avi Kivity9dac77f2011-06-01 15:34:25 +03003721 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003722 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003723 goto done;
3724 }
3725
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003726 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003727 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003728 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003729 goto done;
3730 }
3731
Avi Kivity9dac77f2011-06-01 15:34:25 +03003732 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003733 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003734 goto done;
3735 }
3736
Avi Kivity9dac77f2011-06-01 15:34:25 +03003737 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003738 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3739 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003740 rc = emulate_ud(ctxt);
3741 goto done;
3742 }
3743
Avi Kivity9dac77f2011-06-01 15:34:25 +03003744 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003745 rc = emulate_nm(ctxt);
3746 goto done;
3747 }
3748
Avi Kivity9dac77f2011-06-01 15:34:25 +03003749 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3750 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003751 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003752 if (rc != X86EMUL_CONTINUE)
3753 goto done;
3754 }
3755
Gleb Natapove92805a2010-02-10 14:21:35 +02003756 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003757 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003758 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003759 goto done;
3760 }
3761
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003762 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003763 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003764 rc = emulate_ud(ctxt);
3765 goto done;
3766 }
3767
Joerg Roedeld09beab2011-04-04 12:39:25 +02003768 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003769 if (ctxt->check_perm) {
3770 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003771 if (rc != X86EMUL_CONTINUE)
3772 goto done;
3773 }
3774
Avi Kivity9dac77f2011-06-01 15:34:25 +03003775 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3776 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003777 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003778 if (rc != X86EMUL_CONTINUE)
3779 goto done;
3780 }
3781
Avi Kivity9dac77f2011-06-01 15:34:25 +03003782 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003783 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003784 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3785 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003786 goto done;
3787 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003788 }
3789
Avi Kivity9dac77f2011-06-01 15:34:25 +03003790 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3791 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3792 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003793 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003794 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003795 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003796 }
3797
Avi Kivity9dac77f2011-06-01 15:34:25 +03003798 if (ctxt->src2.type == OP_MEM) {
3799 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3800 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003801 if (rc != X86EMUL_CONTINUE)
3802 goto done;
3803 }
3804
Avi Kivity9dac77f2011-06-01 15:34:25 +03003805 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003806 goto special_insn;
3807
3808
Avi Kivity9dac77f2011-06-01 15:34:25 +03003809 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003810 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003811 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3812 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003813 if (rc != X86EMUL_CONTINUE)
3814 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003815 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003816 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003817
Avi Kivity018a98d2007-11-27 19:30:56 +02003818special_insn:
3819
Avi Kivity9dac77f2011-06-01 15:34:25 +03003820 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3821 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003822 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003823 if (rc != X86EMUL_CONTINUE)
3824 goto done;
3825 }
3826
Avi Kivity9dac77f2011-06-01 15:34:25 +03003827 if (ctxt->execute) {
3828 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003829 if (rc != X86EMUL_CONTINUE)
3830 goto done;
3831 goto writeback;
3832 }
3833
Avi Kivity9dac77f2011-06-01 15:34:25 +03003834 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 goto twobyte_insn;
3836
Avi Kivity9dac77f2011-06-01 15:34:25 +03003837 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003838 case 0x06: /* push es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003839 rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003840 break;
3841 case 0x07: /* pop es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003842 rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003843 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003844 case 0x0e: /* push cs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003845 rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003846 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003847 case 0x16: /* push ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003848 rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003849 break;
3850 case 0x17: /* pop ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003851 rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003852 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003853 case 0x1e: /* push ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003854 rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003855 break;
3856 case 0x1f: /* pop ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003857 rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003858 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003859 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003860 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02003861 break;
3862 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003863 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02003864 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003866 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003868 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003870 case 0x6c: /* insb */
3871 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003872 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003873 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003874 case 0x6e: /* outsb */
3875 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003876 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003877 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003878 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003879 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003880 if (test_cc(ctxt->b, ctxt->eflags))
3881 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003882 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003883 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003884 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003885 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003887 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003889 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003890 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003891 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003892 rc = em_xchg(ctxt);
3893 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003894 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003895 switch (ctxt->op_bytes) {
3896 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
3897 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
3898 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003899 }
3900 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003901 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003902 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003903 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003904 case 0xc4: /* les */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003905 rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003906 break;
3907 case 0xc5: /* lds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003908 rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003909 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003910 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003911 rc = emulate_int(ctxt, 3);
3912 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003913 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003914 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003915 break;
3916 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003917 if (ctxt->eflags & EFLG_OF)
3918 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003919 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003920 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003921 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003922 break;
3923 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003924 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003925 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003926 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003927 case 0xe4: /* inb */
3928 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003929 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003930 case 0xe6: /* outb */
3931 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003932 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003933 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003934 long int rel = ctxt->src.val;
3935 ctxt->src.val = (unsigned long) ctxt->_eip;
3936 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003937 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003938 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003939 }
3940 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003941 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003942 jmp_rel(ctxt, ctxt->src.val);
3943 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003944 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003945 case 0xec: /* in al,dx */
3946 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003947 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003948 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3949 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003950 goto done; /* IO is needed */
3951 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003952 case 0xee: /* out dx,al */
3953 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003954 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003955 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3956 &ctxt->src.val, 1);
3957 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003958 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003959 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003960 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003961 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003962 case 0xf5: /* cmc */
3963 /* complement carry flag from eflags reg */
3964 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003965 break;
3966 case 0xf8: /* clc */
3967 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003968 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003969 case 0xf9: /* stc */
3970 ctxt->eflags |= EFLG_CF;
3971 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003972 case 0xfc: /* cld */
3973 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003974 break;
3975 case 0xfd: /* std */
3976 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003977 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003978 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003979 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003980 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003981 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003982 rc = em_grp45(ctxt);
3983 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003984 default:
3985 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003987
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003988 if (rc != X86EMUL_CONTINUE)
3989 goto done;
3990
Avi Kivity018a98d2007-11-27 19:30:56 +02003991writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003992 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003993 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003994 goto done;
3995
Gleb Natapov5cd21912010-03-18 15:20:26 +02003996 /*
3997 * restore dst type in case the decoding will be reused
3998 * (happens for string instruction )
3999 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004000 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004001
Avi Kivity9dac77f2011-06-01 15:34:25 +03004002 if ((ctxt->d & SrcMask) == SrcSI)
4003 string_addr_inc(ctxt, seg_override(ctxt),
4004 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004005
Avi Kivity9dac77f2011-06-01 15:34:25 +03004006 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004007 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004008 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004009
Avi Kivity9dac77f2011-06-01 15:34:25 +03004010 if (ctxt->rep_prefix && (ctxt->d & String)) {
4011 struct read_cache *r = &ctxt->io_read;
4012 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004013
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004014 if (!string_insn_completed(ctxt)) {
4015 /*
4016 * Re-enter guest when pio read ahead buffer is empty
4017 * or, if it is not used, after each 1024 iteration.
4018 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004019 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004020 (r->end == 0 || r->end != r->pos)) {
4021 /*
4022 * Reset read cache. Usually happens before
4023 * decode, but since instruction is restarted
4024 * we have to do it here.
4025 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004026 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004027 return EMULATION_RESTART;
4028 }
4029 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004030 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004031 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004032
Avi Kivity9dac77f2011-06-01 15:34:25 +03004033 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004034
4035done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004036 if (rc == X86EMUL_PROPAGATE_FAULT)
4037 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004038 if (rc == X86EMUL_INTERCEPTED)
4039 return EMULATION_INTERCEPTED;
4040
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004041 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042
4043twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004044 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004045 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004046 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004047 break;
4048 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004049 case 0x0d: /* GrpP (prefetch) */
4050 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004051 break;
4052 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004053 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004054 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004056 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004058 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004059 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004060 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004061 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004062 goto done;
4063 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004064 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004065 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004067 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004068 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004069 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004070 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004071 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004072 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004073 goto done;
4074 }
4075
Avi Kivity9dac77f2011-06-01 15:34:25 +03004076 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004078 case 0x30:
4079 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004080 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4081 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4082 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004083 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004084 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004085 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004086 }
4087 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004088 break;
4089 case 0x32:
4090 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004092 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004093 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004094 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004095 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004096 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4097 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004098 }
4099 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004100 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4103 if (!test_cc(ctxt->b, ctxt->eflags))
4104 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004106 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004107 if (test_cc(ctxt->b, ctxt->eflags))
4108 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004109 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004110 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004111 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004112 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004113 case 0xa0: /* push fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004114 rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004115 break;
4116 case 0xa1: /* pop fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004117 rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004118 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004119 case 0xa3:
4120 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004121 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004122 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004123 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004124 emulate_2op_SrcV_nobyte(ctxt, "bt");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004125 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004126 case 0xa4: /* shld imm8, r, r/m */
4127 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004128 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004129 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004130 case 0xa8: /* push gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004131 rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004132 break;
4133 case 0xa9: /* pop gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004134 rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004135 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004136 case 0xab:
4137 bts: /* bts */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004138 emulate_2op_SrcV_nobyte(ctxt, "bts");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004139 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004140 case 0xac: /* shrd imm8, r, r/m */
4141 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004142 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004143 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004144 case 0xae: /* clflush */
4145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 case 0xb0 ... 0xb1: /* cmpxchg */
4147 /*
4148 * Save real source value, then compare EAX against
4149 * destination.
4150 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004151 ctxt->src.orig_val = ctxt->src.val;
4152 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004153 emulate_2op_SrcV(ctxt, "cmp");
Laurent Vivier05f086f2007-09-24 11:10:55 +02004154 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004156 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 } else {
4158 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004159 ctxt->dst.type = OP_REG;
4160 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 }
4162 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004163 case 0xb2: /* lss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004164 rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004165 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 case 0xb3:
4167 btr: /* btr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004168 emulate_2op_SrcV_nobyte(ctxt, "btr");
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004170 case 0xb4: /* lfs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004171 rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004172 break;
4173 case 0xb5: /* lgs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004174 rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004175 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004177 ctxt->dst.bytes = ctxt->op_bytes;
4178 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4179 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004182 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 case 0:
4184 goto bt;
4185 case 1:
4186 goto bts;
4187 case 2:
4188 goto btr;
4189 case 3:
4190 goto btc;
4191 }
4192 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004193 case 0xbb:
4194 btc: /* btc */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004195 emulate_2op_SrcV_nobyte(ctxt, "btc");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004196 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004197 case 0xbc: { /* bsf */
4198 u8 zf;
4199 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004200 : "=r"(ctxt->dst.val), "=q"(zf)
4201 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004202 ctxt->eflags &= ~X86_EFLAGS_ZF;
4203 if (zf) {
4204 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004205 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004206 }
4207 break;
4208 }
4209 case 0xbd: { /* bsr */
4210 u8 zf;
4211 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004212 : "=r"(ctxt->dst.val), "=q"(zf)
4213 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004214 ctxt->eflags &= ~X86_EFLAGS_ZF;
4215 if (zf) {
4216 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004217 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004218 }
4219 break;
4220 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004222 ctxt->dst.bytes = ctxt->op_bytes;
4223 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4224 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004225 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004226 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004227 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004228 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 ctxt->src.val = ctxt->dst.orig_val;
4230 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004231 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004232 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004233 ctxt->dst.bytes = ctxt->op_bytes;
4234 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4235 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004236 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004238 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004239 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004240 default:
4241 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004242 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004243
4244 if (rc != X86EMUL_CONTINUE)
4245 goto done;
4246
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247 goto writeback;
4248
4249cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004250 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251}