blob: 906c5eb34aa7960628d7c0d021a060e94cc9b378 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya99455492011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
46#define OpImm 12ull /* Sign extended immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivitya99455492011-09-13 10:45:41 +030060
Avi Kivity0fe59122011-09-13 10:45:47 +030061#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030062#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030063
64/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080065 * Opcode effective-address decode tables.
66 * Note that we only emulate instructions that have at least one memory
67 * operand (excluding implicit stack references). We assume that stack
68 * references and instruction fetches will never occur in special memory
69 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
70 * not be handled.
71 */
72
73/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030074#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030076#define DstShift 1
77#define ImplicitOps (OpImplicit << DstShift)
78#define DstReg (OpReg << DstShift)
79#define DstMem (OpMem << DstShift)
80#define DstAcc (OpAcc << DstShift)
81#define DstDI (OpDI << DstShift)
82#define DstMem64 (OpMem64 << DstShift)
83#define DstImmUByte (OpImmUByte << DstShift)
84#define DstDX (OpDX << DstShift)
85#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080086/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030087#define SrcShift 6
88#define SrcNone (OpNone << SrcShift)
89#define SrcReg (OpReg << SrcShift)
90#define SrcMem (OpMem << SrcShift)
91#define SrcMem16 (OpMem16 << SrcShift)
92#define SrcMem32 (OpMem32 << SrcShift)
93#define SrcImm (OpImm << SrcShift)
94#define SrcImmByte (OpImmByte << SrcShift)
95#define SrcOne (OpOne << SrcShift)
96#define SrcImmUByte (OpImmUByte << SrcShift)
97#define SrcImmU (OpImmU << SrcShift)
98#define SrcSI (OpSI << SrcShift)
99#define SrcImmFAddr (OpImmFAddr << SrcShift)
100#define SrcMemFAddr (OpMemFAddr << SrcShift)
101#define SrcAcc (OpAcc << SrcShift)
102#define SrcImmU16 (OpImmU16 << SrcShift)
103#define SrcDX (OpDX << SrcShift)
104#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300105#define BitOp (1<<11)
106#define MemAbs (1<<12) /* Memory operand is absolute displacement */
107#define String (1<<13) /* String instruction (rep capable) */
108#define Stack (1<<14) /* Stack instruction (push/pop) */
109#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
110#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
111#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
112#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
113#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
114#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300115/* Generic ModRM decode. */
116#define ModRM (1<<19)
117/* Destination is only written; never read. */
118#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300119/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200120#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200121#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300122#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300123#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300124#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200125#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200126#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300127#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800128#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100129/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800130#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300131#define Src2None (OpNone << Src2Shift)
132#define Src2CL (OpCL << Src2Shift)
133#define Src2ImmByte (OpImmByte << Src2Shift)
134#define Src2One (OpOne << Src2Shift)
135#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300136#define Src2ES (OpES << Src2Shift)
137#define Src2CS (OpCS << Src2Shift)
138#define Src2SS (OpSS << Src2Shift)
139#define Src2DS (OpDS << Src2Shift)
140#define Src2FS (OpFS << Src2Shift)
141#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300142#define Src2Mask (OpMask << Src2Shift)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143
Avi Kivityd0e53322010-07-29 15:11:54 +0300144#define X2(x...) x, x
145#define X3(x...) X2(x), x
146#define X4(x...) X2(x), X2(x)
147#define X5(x...) X4(x), x
148#define X6(x...) X4(x), X2(x)
149#define X7(x...) X4(x), X3(x)
150#define X8(x...) X4(x), X4(x)
151#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300152
Avi Kivityd65b1de2010-07-29 15:11:35 +0300153struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300154 u64 flags : 56;
155 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300156 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300157 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300158 struct opcode *group;
159 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200160 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300161 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200162 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300163};
164
165struct group_dual {
166 struct opcode mod012[8];
167 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300168};
169
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200170struct gprefix {
171 struct opcode pfx_no;
172 struct opcode pfx_66;
173 struct opcode pfx_f2;
174 struct opcode pfx_f3;
175};
176
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200178#define EFLG_ID (1<<21)
179#define EFLG_VIP (1<<20)
180#define EFLG_VIF (1<<19)
181#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200182#define EFLG_VM (1<<17)
183#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200184#define EFLG_IOPL (3<<12)
185#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186#define EFLG_OF (1<<11)
187#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200188#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200189#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190#define EFLG_SF (1<<7)
191#define EFLG_ZF (1<<6)
192#define EFLG_AF (1<<4)
193#define EFLG_PF (1<<2)
194#define EFLG_CF (1<<0)
195
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300196#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
197#define EFLG_RESERVED_ONE_MASK 2
198
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199/*
200 * Instruction emulation:
201 * Most instructions are emulated directly via a fragment of inline assembly
202 * code. This allows us to save/restore EFLAGS and thus very easily pick up
203 * any modified flags.
204 */
205
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800206#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207#define _LO32 "k" /* force 32-bit operand */
208#define _STK "%%rsp" /* stack pointer */
209#elif defined(__i386__)
210#define _LO32 "" /* force 32-bit operand */
211#define _STK "%%esp" /* stack pointer */
212#endif
213
214/*
215 * These EFLAGS bits are restored from saved value during emulation, and
216 * any changes are written back to the saved value after emulation.
217 */
218#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
219
220/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200221#define _PRE_EFLAGS(_sav, _msk, _tmp) \
222 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
223 "movl %"_sav",%"_LO32 _tmp"; " \
224 "push %"_tmp"; " \
225 "push %"_tmp"; " \
226 "movl %"_msk",%"_LO32 _tmp"; " \
227 "andl %"_LO32 _tmp",("_STK"); " \
228 "pushf; " \
229 "notl %"_LO32 _tmp"; " \
230 "andl %"_LO32 _tmp",("_STK"); " \
231 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
232 "pop %"_tmp"; " \
233 "orl %"_LO32 _tmp",("_STK"); " \
234 "popf; " \
235 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236
237/* After executing instruction: write-back necessary bits in EFLAGS. */
238#define _POST_EFLAGS(_sav, _msk, _tmp) \
239 /* _sav |= EFLAGS & _msk; */ \
240 "pushf; " \
241 "pop %"_tmp"; " \
242 "andl %"_msk",%"_LO32 _tmp"; " \
243 "orl %"_LO32 _tmp",%"_sav"; "
244
Avi Kivitydda96d82008-11-26 15:14:10 +0200245#ifdef CONFIG_X86_64
246#define ON64(x) x
247#else
248#define ON64(x)
249#endif
250
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300251#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200252 do { \
253 __asm__ __volatile__ ( \
254 _PRE_EFLAGS("0", "4", "2") \
255 _op _suffix " %"_x"3,%1; " \
256 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300257 : "=m" ((ctxt)->eflags), \
258 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200259 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300260 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200261 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200262
263
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300265#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200266 do { \
267 unsigned long _tmp; \
268 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300269 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200270 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300271 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200272 break; \
273 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300274 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200275 break; \
276 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300277 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200278 break; \
279 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280 } while (0)
281
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300282#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200284 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300285 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300287 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288 break; \
289 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300290 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 _wx, _wy, _lx, _ly, _qx, _qy); \
292 break; \
293 } \
294 } while (0)
295
296/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300297#define emulate_2op_SrcB(ctxt, _op) \
298 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299
300/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300301#define emulate_2op_SrcV(ctxt, _op) \
302 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303
304/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300305#define emulate_2op_SrcV_nobyte(ctxt, _op) \
306 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100308/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300309#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300310 do { \
311 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300312 _type _clv = (ctxt)->src2.val; \
313 _type _srcv = (ctxt)->src.val; \
314 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300315 \
316 __asm__ __volatile__ ( \
317 _PRE_EFLAGS("0", "5", "2") \
318 _op _suffix " %4,%1 \n" \
319 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300320 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300321 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
322 ); \
323 \
Avi Kivity761441b2011-09-07 16:41:36 +0300324 (ctxt)->src2.val = (unsigned long) _clv; \
325 (ctxt)->src2.val = (unsigned long) _srcv; \
326 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100327 } while (0)
328
Avi Kivity761441b2011-09-07 16:41:36 +0300329#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300330 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300331 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300332 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300333 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300334 break; \
335 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300336 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300337 break; \
338 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300339 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300340 break; \
341 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100342 } while (0)
343
Avi Kivityd1eef452011-09-07 16:41:38 +0300344#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345 do { \
346 unsigned long _tmp; \
347 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200348 __asm__ __volatile__ ( \
349 _PRE_EFLAGS("0", "3", "2") \
350 _op _suffix " %1; " \
351 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300352 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200353 "=&r" (_tmp) \
354 : "i" (EFLAGS_MASK)); \
355 } while (0)
356
357/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300358#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200359 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300360 switch ((ctxt)->dst.bytes) { \
361 case 1: __emulate_1op(ctxt, _op, "b"); break; \
362 case 2: __emulate_1op(ctxt, _op, "w"); break; \
363 case 4: __emulate_1op(ctxt, _op, "l"); break; \
364 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365 } \
366 } while (0)
367
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300368#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300369 do { \
370 unsigned long _tmp; \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300371 ulong *rax = &(ctxt)->regs[VCPU_REGS_RAX]; \
372 ulong *rdx = &(ctxt)->regs[VCPU_REGS_RDX]; \
Avi Kivityf6b35972010-08-26 11:59:00 +0300373 \
374 __asm__ __volatile__ ( \
375 _PRE_EFLAGS("0", "5", "1") \
376 "1: \n\t" \
377 _op _suffix " %6; " \
378 "2: \n\t" \
379 _POST_EFLAGS("0", "5", "1") \
380 ".pushsection .fixup,\"ax\" \n\t" \
381 "3: movb $1, %4 \n\t" \
382 "jmp 2b \n\t" \
383 ".popsection \n\t" \
384 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300385 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
386 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
387 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val), \
388 "a" (*rax), "d" (*rdx)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300389 } while (0)
390
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300391/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300392#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300393 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300394 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300395 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300396 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300397 break; \
398 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300399 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300400 break; \
401 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300402 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300403 break; \
404 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300405 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300406 break; \
407 } \
408 } while (0)
409
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200410static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
411 enum x86_intercept intercept,
412 enum x86_intercept_stage stage)
413{
414 struct x86_instruction_info info = {
415 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300416 .rep_prefix = ctxt->rep_prefix,
417 .modrm_mod = ctxt->modrm_mod,
418 .modrm_reg = ctxt->modrm_reg,
419 .modrm_rm = ctxt->modrm_rm,
420 .src_val = ctxt->src.val64,
421 .src_bytes = ctxt->src.bytes,
422 .dst_bytes = ctxt->dst.bytes,
423 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200424 .next_rip = ctxt->eip,
425 };
426
Avi Kivity29535382011-04-20 13:37:53 +0300427 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200428}
429
Avi Kivity9dac77f2011-06-01 15:34:25 +0300430static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800431{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300432 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800433}
434
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800436static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300437address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800438{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300439 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800440 return reg;
441 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300442 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800443}
444
445static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300446register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800447{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800449}
450
Harvey Harrison7a9572752008-02-19 07:40:41 -0800451static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300452register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800453{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300454 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800455 *reg += inc;
456 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300457 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800458}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459
Avi Kivity9dac77f2011-06-01 15:34:25 +0300460static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800461{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300462 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800463}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300464
Avi Kivity56697682011-04-03 14:08:51 +0300465static u32 desc_limit_scaled(struct desc_struct *desc)
466{
467 u32 limit = get_desc_limit(desc);
468
469 return desc->g ? (limit << 12) | 0xfff : limit;
470}
471
Avi Kivity9dac77f2011-06-01 15:34:25 +0300472static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300473{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474 ctxt->has_seg_override = true;
475 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300476}
477
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900478static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300479{
480 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
481 return 0;
482
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900483 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300484}
485
Avi Kivity9dac77f2011-06-01 15:34:25 +0300486static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300487{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300488 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300489 return 0;
490
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300492}
493
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200494static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
495 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300496{
Avi Kivityda9cb572010-11-22 17:53:21 +0200497 ctxt->exception.vector = vec;
498 ctxt->exception.error_code = error;
499 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200500 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300501}
502
Joerg Roedel3b88e412011-04-04 12:39:29 +0200503static int emulate_db(struct x86_emulate_ctxt *ctxt)
504{
505 return emulate_exception(ctxt, DB_VECTOR, 0, false);
506}
507
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200508static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300509{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200510 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300511}
512
Avi Kivity618ff152011-04-03 12:32:09 +0300513static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
514{
515 return emulate_exception(ctxt, SS_VECTOR, err, true);
516}
517
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200518static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300519{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200520 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300521}
522
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200523static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300524{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300526}
527
Avi Kivity34d1f492010-08-26 11:59:01 +0300528static int emulate_de(struct x86_emulate_ctxt *ctxt)
529{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200530 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300531}
532
Avi Kivity12537912011-03-29 11:41:27 +0200533static int emulate_nm(struct x86_emulate_ctxt *ctxt)
534{
535 return emulate_exception(ctxt, NM_VECTOR, 0, false);
536}
537
Avi Kivity1aa36612011-04-27 13:20:30 +0300538static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
539{
540 u16 selector;
541 struct desc_struct desc;
542
543 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
544 return selector;
545}
546
547static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
548 unsigned seg)
549{
550 u16 dummy;
551 u32 base3;
552 struct desc_struct desc;
553
554 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
555 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
556}
557
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400558static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300559 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400560 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300561 ulong *linear)
562{
Avi Kivity618ff152011-04-03 12:32:09 +0300563 struct desc_struct desc;
564 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300565 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300566 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300567 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300568 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300569
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900570 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300571 switch (ctxt->mode) {
572 case X86EMUL_MODE_REAL:
573 break;
574 case X86EMUL_MODE_PROT64:
575 if (((signed long)la << 16) >> 16 != la)
576 return emulate_gp(ctxt, 0);
577 break;
578 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300579 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
580 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300581 if (!usable)
582 goto bad;
583 /* code segment or read-only data segment */
584 if (((desc.type & 8) || !(desc.type & 2)) && write)
585 goto bad;
586 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400587 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300588 goto bad;
589 lim = desc_limit_scaled(&desc);
590 if ((desc.type & 8) || !(desc.type & 4)) {
591 /* expand-up segment */
592 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
593 goto bad;
594 } else {
595 /* exapand-down segment */
596 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
597 goto bad;
598 lim = desc.d ? 0xffffffff : 0xffff;
599 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
600 goto bad;
601 }
Avi Kivity717746e2011-04-20 13:37:53 +0300602 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300603 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300604 cpl = max(cpl, rpl);
605 if (!(desc.type & 8)) {
606 /* data segment */
607 if (cpl > desc.dpl)
608 goto bad;
609 } else if ((desc.type & 8) && !(desc.type & 4)) {
610 /* nonconforming code segment */
611 if (cpl != desc.dpl)
612 goto bad;
613 } else if ((desc.type & 8) && (desc.type & 4)) {
614 /* conforming code segment */
615 if (cpl < desc.dpl)
616 goto bad;
617 }
618 break;
619 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300620 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300621 la &= (u32)-1;
622 *linear = la;
623 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300624bad:
625 if (addr.seg == VCPU_SREG_SS)
626 return emulate_ss(ctxt, addr.seg);
627 else
628 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300629}
630
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400631static int linearize(struct x86_emulate_ctxt *ctxt,
632 struct segmented_address addr,
633 unsigned size, bool write,
634 ulong *linear)
635{
636 return __linearize(ctxt, addr, size, write, false, linear);
637}
638
639
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200640static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
641 struct segmented_address addr,
642 void *data,
643 unsigned size)
644{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200645 int rc;
646 ulong linear;
647
Avi Kivity83b87952011-04-03 11:31:19 +0300648 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200649 if (rc != X86EMUL_CONTINUE)
650 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300651 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200652}
653
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900654/*
655 * Fetch the next byte of the instruction being emulated which is pointed to
656 * by ctxt->_eip, then increment ctxt->_eip.
657 *
658 * Also prefetch the remaining bytes of the instruction without crossing page
659 * boundary if they are not in fetch_cache yet.
660 */
661static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200662{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300663 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200664 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300665 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200666
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900667 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400668 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900669 struct segmented_address addr = { .seg = VCPU_SREG_CS,
670 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300671 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900672 size = min(15UL - cur_size,
673 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400674 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900675 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400676 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900677 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
678 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900679 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200680 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300681 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200682 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900683 *dest = fc->data[ctxt->_eip - fc->start];
684 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900685 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200686}
687
688static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900689 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200690{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900691 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200692
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200693 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900694 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200695 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200696 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900697 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900698 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200699 return rc;
700 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900701 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200702}
703
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900704/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900705#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900706({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900707 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900708 if (rc != X86EMUL_CONTINUE) \
709 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900710 (_type)_x; \
711})
712
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900713#define insn_fetch_arr(_arr, _size, _ctxt) \
714({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900715 if (rc != X86EMUL_CONTINUE) \
716 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900717})
718
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000719/*
720 * Given the 'reg' portion of a ModRM byte, and a register block, return a
721 * pointer into the block that addresses the relevant register.
722 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
723 */
724static void *decode_register(u8 modrm_reg, unsigned long *regs,
725 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726{
727 void *p;
728
729 p = &regs[modrm_reg];
730 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
731 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
732 return p;
733}
734
735static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200736 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 u16 *size, unsigned long *address, int op_bytes)
738{
739 int rc;
740
741 if (op_bytes == 2)
742 op_bytes = 3;
743 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200744 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900745 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200747 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200748 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 return rc;
750}
751
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300752static int test_cc(unsigned int condition, unsigned int flags)
753{
754 int rc = 0;
755
756 switch ((condition & 15) >> 1) {
757 case 0: /* o */
758 rc |= (flags & EFLG_OF);
759 break;
760 case 1: /* b/c/nae */
761 rc |= (flags & EFLG_CF);
762 break;
763 case 2: /* z/e */
764 rc |= (flags & EFLG_ZF);
765 break;
766 case 3: /* be/na */
767 rc |= (flags & (EFLG_CF|EFLG_ZF));
768 break;
769 case 4: /* s */
770 rc |= (flags & EFLG_SF);
771 break;
772 case 5: /* p/pe */
773 rc |= (flags & EFLG_PF);
774 break;
775 case 7: /* le/ng */
776 rc |= (flags & EFLG_ZF);
777 /* fall through */
778 case 6: /* l/nge */
779 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
780 break;
781 }
782
783 /* Odd condition identifiers (lsb == 1) have inverted sense. */
784 return (!!rc ^ (condition & 1));
785}
786
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300787static void fetch_register_operand(struct operand *op)
788{
789 switch (op->bytes) {
790 case 1:
791 op->val = *(u8 *)op->addr.reg;
792 break;
793 case 2:
794 op->val = *(u16 *)op->addr.reg;
795 break;
796 case 4:
797 op->val = *(u32 *)op->addr.reg;
798 break;
799 case 8:
800 op->val = *(u64 *)op->addr.reg;
801 break;
802 }
803}
804
Avi Kivity12537912011-03-29 11:41:27 +0200805static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
806{
807 ctxt->ops->get_fpu(ctxt);
808 switch (reg) {
809 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
810 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
811 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
812 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
813 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
814 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
815 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
816 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
817#ifdef CONFIG_X86_64
818 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
819 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
820 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
821 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
822 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
823 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
824 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
825 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
826#endif
827 default: BUG();
828 }
829 ctxt->ops->put_fpu(ctxt);
830}
831
832static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
833 int reg)
834{
835 ctxt->ops->get_fpu(ctxt);
836 switch (reg) {
837 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
838 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
839 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
840 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
841 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
842 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
843 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
844 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
845#ifdef CONFIG_X86_64
846 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
847 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
848 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
849 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
850 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
851 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
852 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
853 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
854#endif
855 default: BUG();
856 }
857 ctxt->ops->put_fpu(ctxt);
858}
859
860static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
861 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200862 int inhibit_bytereg)
863{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300864 unsigned reg = ctxt->modrm_reg;
865 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200866
Avi Kivity9dac77f2011-06-01 15:34:25 +0300867 if (!(ctxt->d & ModRM))
868 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200869
Avi Kivity9dac77f2011-06-01 15:34:25 +0300870 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200871 op->type = OP_XMM;
872 op->bytes = 16;
873 op->addr.xmm = reg;
874 read_sse_reg(ctxt, &op->vec_val, reg);
875 return;
876 }
877
Avi Kivity3c118e22007-10-31 10:27:04 +0200878 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300879 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
880 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200881 op->bytes = 1;
882 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300883 op->addr.reg = decode_register(reg, ctxt->regs, 0);
884 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200885 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300886 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200887 op->orig_val = op->val;
888}
889
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300891 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200892{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200893 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700894 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900895 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300896 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200897
Avi Kivity9dac77f2011-06-01 15:34:25 +0300898 if (ctxt->rex_prefix) {
899 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
900 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
901 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902 }
903
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900904 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300905 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
906 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
907 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
908 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200909
Avi Kivity9dac77f2011-06-01 15:34:25 +0300910 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300911 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300912 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
913 op->addr.reg = decode_register(ctxt->modrm_rm,
914 ctxt->regs, ctxt->d & ByteOp);
915 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200916 op->type = OP_XMM;
917 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300918 op->addr.xmm = ctxt->modrm_rm;
919 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200920 return rc;
921 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300922 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200923 return rc;
924 }
925
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300926 op->type = OP_MEM;
927
Avi Kivity9dac77f2011-06-01 15:34:25 +0300928 if (ctxt->ad_bytes == 2) {
929 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
930 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
931 unsigned si = ctxt->regs[VCPU_REGS_RSI];
932 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200933
934 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300935 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200936 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300937 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900938 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200939 break;
940 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900941 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200942 break;
943 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900944 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200945 break;
946 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300947 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200948 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300949 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200950 break;
951 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300952 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200953 break;
954 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300955 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200956 break;
957 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300958 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200959 break;
960 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300961 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200962 break;
963 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300964 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200965 break;
966 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300967 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300968 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200969 break;
970 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300971 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200972 break;
973 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300974 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
975 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
976 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300977 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200978 } else {
979 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300980 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900981 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200982 index_reg |= (sib >> 3) & 7;
983 base_reg |= sib & 7;
984 scale = sib >> 6;
985
Avi Kivity9dac77f2011-06-01 15:34:25 +0300986 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900987 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700988 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300989 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700990 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300991 modrm_ea += ctxt->regs[index_reg] << scale;
992 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700993 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300994 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700995 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300996 modrm_ea += ctxt->regs[ctxt->modrm_rm];
997 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200998 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300999 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001000 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001 break;
1002 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001003 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004 break;
1005 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001006 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001007 break;
1008 }
1009 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001010 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001011done:
1012 return rc;
1013}
1014
1015static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001016 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001017{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001018 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001019
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001020 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001021 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001022 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001023 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001024 break;
1025 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001026 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001027 break;
1028 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001029 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001030 break;
1031 }
1032done:
1033 return rc;
1034}
1035
Avi Kivity9dac77f2011-06-01 15:34:25 +03001036static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001037{
Sheng Yang7129eec2010-09-28 16:33:32 +08001038 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001039
Avi Kivity9dac77f2011-06-01 15:34:25 +03001040 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1041 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001042
Avi Kivity9dac77f2011-06-01 15:34:25 +03001043 if (ctxt->src.bytes == 2)
1044 sv = (s16)ctxt->src.val & (s16)mask;
1045 else if (ctxt->src.bytes == 4)
1046 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001047
Avi Kivity9dac77f2011-06-01 15:34:25 +03001048 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001049 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001050
1051 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001052 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001053}
1054
Gleb Natapov9de41572010-04-28 19:15:22 +03001055static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001056 unsigned long addr, void *dest, unsigned size)
1057{
1058 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001059 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001060
1061 while (size) {
1062 int n = min(size, 8u);
1063 size -= n;
1064 if (mc->pos < mc->end)
1065 goto read_cached;
1066
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001067 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1068 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001069 if (rc != X86EMUL_CONTINUE)
1070 return rc;
1071 mc->end += n;
1072
1073 read_cached:
1074 memcpy(dest, mc->data + mc->pos, n);
1075 mc->pos += n;
1076 dest += n;
1077 addr += n;
1078 }
1079 return X86EMUL_CONTINUE;
1080}
1081
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001082static int segmented_read(struct x86_emulate_ctxt *ctxt,
1083 struct segmented_address addr,
1084 void *data,
1085 unsigned size)
1086{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001087 int rc;
1088 ulong linear;
1089
Avi Kivity83b87952011-04-03 11:31:19 +03001090 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001091 if (rc != X86EMUL_CONTINUE)
1092 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001093 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001094}
1095
1096static int segmented_write(struct x86_emulate_ctxt *ctxt,
1097 struct segmented_address addr,
1098 const void *data,
1099 unsigned size)
1100{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001101 int rc;
1102 ulong linear;
1103
Avi Kivity83b87952011-04-03 11:31:19 +03001104 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001105 if (rc != X86EMUL_CONTINUE)
1106 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001107 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1108 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001109}
1110
1111static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1112 struct segmented_address addr,
1113 const void *orig_data, const void *data,
1114 unsigned size)
1115{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001116 int rc;
1117 ulong linear;
1118
Avi Kivity83b87952011-04-03 11:31:19 +03001119 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001120 if (rc != X86EMUL_CONTINUE)
1121 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001122 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1123 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001124}
1125
Gleb Natapov7b262e92010-03-18 15:20:27 +02001126static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001127 unsigned int size, unsigned short port,
1128 void *dest)
1129{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001130 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001131
1132 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001133 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001134 unsigned int count = ctxt->rep_prefix ?
1135 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001136 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001137 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1138 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001139 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1140 count);
1141 if (n == 0)
1142 n = 1;
1143 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001144 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001145 return 0;
1146 rc->end = n * size;
1147 }
1148
1149 memcpy(dest, rc->data + rc->pos, size);
1150 rc->pos += size;
1151 return 1;
1152}
1153
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001154static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001155 u16 selector, struct desc_ptr *dt)
1156{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001157 struct x86_emulate_ops *ops = ctxt->ops;
1158
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001159 if (selector & 1 << 2) {
1160 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001161 u16 sel;
1162
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001163 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001164 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001165 return;
1166
1167 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1168 dt->address = get_desc_base(&desc);
1169 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001170 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001171}
1172
1173/* allowed just for 8 bytes segments */
1174static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001175 u16 selector, struct desc_struct *desc)
1176{
1177 struct desc_ptr dt;
1178 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001179 ulong addr;
1180
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001181 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001182
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001183 if (dt.size < index * 8 + 7)
1184 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001185
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001186 addr = dt.address + index * 8;
1187 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1188 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001189}
1190
1191/* allowed just for 8 bytes segments */
1192static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001193 u16 selector, struct desc_struct *desc)
1194{
1195 struct desc_ptr dt;
1196 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001197 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001199 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001200
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001201 if (dt.size < index * 8 + 7)
1202 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001203
1204 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001205 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1206 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001207}
1208
Gleb Natapov5601d052011-03-07 14:55:06 +02001209/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001210static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001211 u16 selector, int seg)
1212{
1213 struct desc_struct seg_desc;
1214 u8 dpl, rpl, cpl;
1215 unsigned err_vec = GP_VECTOR;
1216 u32 err_code = 0;
1217 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1218 int ret;
1219
1220 memset(&seg_desc, 0, sizeof seg_desc);
1221
1222 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1223 || ctxt->mode == X86EMUL_MODE_REAL) {
1224 /* set real mode segment descriptor */
1225 set_desc_base(&seg_desc, selector << 4);
1226 set_desc_limit(&seg_desc, 0xffff);
1227 seg_desc.type = 3;
1228 seg_desc.p = 1;
1229 seg_desc.s = 1;
1230 goto load;
1231 }
1232
1233 /* NULL selector is not valid for TR, CS and SS */
1234 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1235 && null_selector)
1236 goto exception;
1237
1238 /* TR should be in GDT only */
1239 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1240 goto exception;
1241
1242 if (null_selector) /* for NULL selector skip all following checks */
1243 goto load;
1244
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001245 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001246 if (ret != X86EMUL_CONTINUE)
1247 return ret;
1248
1249 err_code = selector & 0xfffc;
1250 err_vec = GP_VECTOR;
1251
1252 /* can't load system descriptor into segment selecor */
1253 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1254 goto exception;
1255
1256 if (!seg_desc.p) {
1257 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1258 goto exception;
1259 }
1260
1261 rpl = selector & 3;
1262 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001263 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001264
1265 switch (seg) {
1266 case VCPU_SREG_SS:
1267 /*
1268 * segment is not a writable data segment or segment
1269 * selector's RPL != CPL or segment selector's RPL != CPL
1270 */
1271 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1272 goto exception;
1273 break;
1274 case VCPU_SREG_CS:
1275 if (!(seg_desc.type & 8))
1276 goto exception;
1277
1278 if (seg_desc.type & 4) {
1279 /* conforming */
1280 if (dpl > cpl)
1281 goto exception;
1282 } else {
1283 /* nonconforming */
1284 if (rpl > cpl || dpl != cpl)
1285 goto exception;
1286 }
1287 /* CS(RPL) <- CPL */
1288 selector = (selector & 0xfffc) | cpl;
1289 break;
1290 case VCPU_SREG_TR:
1291 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1292 goto exception;
1293 break;
1294 case VCPU_SREG_LDTR:
1295 if (seg_desc.s || seg_desc.type != 2)
1296 goto exception;
1297 break;
1298 default: /* DS, ES, FS, or GS */
1299 /*
1300 * segment is not a data or readable code segment or
1301 * ((segment is a data or nonconforming code segment)
1302 * and (both RPL and CPL > DPL))
1303 */
1304 if ((seg_desc.type & 0xa) == 0x8 ||
1305 (((seg_desc.type & 0xc) != 0xc) &&
1306 (rpl > dpl && cpl > dpl)))
1307 goto exception;
1308 break;
1309 }
1310
1311 if (seg_desc.s) {
1312 /* mark segment as accessed */
1313 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001314 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001315 if (ret != X86EMUL_CONTINUE)
1316 return ret;
1317 }
1318load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001319 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001320 return X86EMUL_CONTINUE;
1321exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001322 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001323 return X86EMUL_PROPAGATE_FAULT;
1324}
1325
Wei Yongjun31be40b2010-08-17 09:17:30 +08001326static void write_register_operand(struct operand *op)
1327{
1328 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1329 switch (op->bytes) {
1330 case 1:
1331 *(u8 *)op->addr.reg = (u8)op->val;
1332 break;
1333 case 2:
1334 *(u16 *)op->addr.reg = (u16)op->val;
1335 break;
1336 case 4:
1337 *op->addr.reg = (u32)op->val;
1338 break; /* 64b: zero-extend */
1339 case 8:
1340 *op->addr.reg = op->val;
1341 break;
1342 }
1343}
1344
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001345static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001346{
1347 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001348
Avi Kivity9dac77f2011-06-01 15:34:25 +03001349 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001350 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001351 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001352 break;
1353 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001354 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001355 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001356 ctxt->dst.addr.mem,
1357 &ctxt->dst.orig_val,
1358 &ctxt->dst.val,
1359 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001360 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001361 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001362 ctxt->dst.addr.mem,
1363 &ctxt->dst.val,
1364 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001365 if (rc != X86EMUL_CONTINUE)
1366 return rc;
1367 break;
Avi Kivity12537912011-03-29 11:41:27 +02001368 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001369 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001370 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001371 case OP_NONE:
1372 /* no writeback */
1373 break;
1374 default:
1375 break;
1376 }
1377 return X86EMUL_CONTINUE;
1378}
1379
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001380static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001381{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001382 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001383
Avi Kivity9dac77f2011-06-01 15:34:25 +03001384 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1385 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001386 addr.seg = VCPU_SREG_SS;
1387
1388 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001389 ctxt->dst.type = OP_NONE;
1390 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001391}
1392
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001393static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001394 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001395{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001397 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398
Avi Kivity9dac77f2011-06-01 15:34:25 +03001399 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001400 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001401 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001402 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403 return rc;
1404
Avi Kivity9dac77f2011-06-01 15:34:25 +03001405 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001406 return rc;
1407}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001408
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001409static int em_pop(struct x86_emulate_ctxt *ctxt)
1410{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001411 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001412}
1413
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001414static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001415 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001416{
1417 int rc;
1418 unsigned long val, change_mask;
1419 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001420 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001421
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001422 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001423 if (rc != X86EMUL_CONTINUE)
1424 return rc;
1425
1426 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1427 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1428
1429 switch(ctxt->mode) {
1430 case X86EMUL_MODE_PROT64:
1431 case X86EMUL_MODE_PROT32:
1432 case X86EMUL_MODE_PROT16:
1433 if (cpl == 0)
1434 change_mask |= EFLG_IOPL;
1435 if (cpl <= iopl)
1436 change_mask |= EFLG_IF;
1437 break;
1438 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001439 if (iopl < 3)
1440 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001441 change_mask |= EFLG_IF;
1442 break;
1443 default: /* real mode */
1444 change_mask |= (EFLG_IOPL | EFLG_IF);
1445 break;
1446 }
1447
1448 *(unsigned long *)dest =
1449 (ctxt->eflags & ~change_mask) | (val & change_mask);
1450
1451 return rc;
1452}
1453
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001454static int em_popf(struct x86_emulate_ctxt *ctxt)
1455{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001456 ctxt->dst.type = OP_REG;
1457 ctxt->dst.addr.reg = &ctxt->eflags;
1458 ctxt->dst.bytes = ctxt->op_bytes;
1459 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001460}
1461
Avi Kivity1cd196e2011-09-13 10:45:51 +03001462static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001463{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001464 int seg = ctxt->src2.val;
1465
Avi Kivity9dac77f2011-06-01 15:34:25 +03001466 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001467
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001468 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001469}
1470
Avi Kivity1cd196e2011-09-13 10:45:51 +03001471static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001472{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001473 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001474 unsigned long selector;
1475 int rc;
1476
Avi Kivity9dac77f2011-06-01 15:34:25 +03001477 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001478 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001479 return rc;
1480
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001481 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001482 return rc;
1483}
1484
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001485static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001486{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001487 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001488 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001489 int reg = VCPU_REGS_RAX;
1490
1491 while (reg <= VCPU_REGS_RDI) {
1492 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001493 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001494
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001495 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001496 if (rc != X86EMUL_CONTINUE)
1497 return rc;
1498
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001499 ++reg;
1500 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001501
Wei Yongjunc37eda12010-06-15 09:03:33 +08001502 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001503}
1504
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001505static int em_pushf(struct x86_emulate_ctxt *ctxt)
1506{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001507 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001508 return em_push(ctxt);
1509}
1510
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001511static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001512{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001513 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001514 int reg = VCPU_REGS_RDI;
1515
1516 while (reg >= VCPU_REGS_RAX) {
1517 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001518 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1519 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001520 --reg;
1521 }
1522
Avi Kivity9dac77f2011-06-01 15:34:25 +03001523 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001524 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001525 break;
1526 --reg;
1527 }
1528 return rc;
1529}
1530
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001531int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001532{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001533 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001534 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001535 struct desc_ptr dt;
1536 gva_t cs_addr;
1537 gva_t eip_addr;
1538 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001539
1540 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001541 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001542 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001543 if (rc != X86EMUL_CONTINUE)
1544 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001545
1546 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1547
Avi Kivity9dac77f2011-06-01 15:34:25 +03001548 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001549 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001550 if (rc != X86EMUL_CONTINUE)
1551 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001552
Avi Kivity9dac77f2011-06-01 15:34:25 +03001553 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001554 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001555 if (rc != X86EMUL_CONTINUE)
1556 return rc;
1557
Avi Kivity4bff1e862011-04-20 13:37:53 +03001558 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001559
1560 eip_addr = dt.address + (irq << 2);
1561 cs_addr = dt.address + (irq << 2) + 2;
1562
Avi Kivity0f65dd72011-04-20 13:37:53 +03001563 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001564 if (rc != X86EMUL_CONTINUE)
1565 return rc;
1566
Avi Kivity0f65dd72011-04-20 13:37:53 +03001567 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001568 if (rc != X86EMUL_CONTINUE)
1569 return rc;
1570
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001571 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001572 if (rc != X86EMUL_CONTINUE)
1573 return rc;
1574
Avi Kivity9dac77f2011-06-01 15:34:25 +03001575 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001576
1577 return rc;
1578}
1579
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001580static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001581{
1582 switch(ctxt->mode) {
1583 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001584 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001585 case X86EMUL_MODE_VM86:
1586 case X86EMUL_MODE_PROT16:
1587 case X86EMUL_MODE_PROT32:
1588 case X86EMUL_MODE_PROT64:
1589 default:
1590 /* Protected mode interrupts unimplemented yet */
1591 return X86EMUL_UNHANDLEABLE;
1592 }
1593}
1594
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001595static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001596{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001597 int rc = X86EMUL_CONTINUE;
1598 unsigned long temp_eip = 0;
1599 unsigned long temp_eflags = 0;
1600 unsigned long cs = 0;
1601 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1602 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1603 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1604 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1605
1606 /* TODO: Add stack limit check */
1607
Avi Kivity9dac77f2011-06-01 15:34:25 +03001608 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001609
1610 if (rc != X86EMUL_CONTINUE)
1611 return rc;
1612
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001613 if (temp_eip & ~0xffff)
1614 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001615
Avi Kivity9dac77f2011-06-01 15:34:25 +03001616 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001617
1618 if (rc != X86EMUL_CONTINUE)
1619 return rc;
1620
Avi Kivity9dac77f2011-06-01 15:34:25 +03001621 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001622
1623 if (rc != X86EMUL_CONTINUE)
1624 return rc;
1625
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001626 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001627
1628 if (rc != X86EMUL_CONTINUE)
1629 return rc;
1630
Avi Kivity9dac77f2011-06-01 15:34:25 +03001631 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001632
1633
Avi Kivity9dac77f2011-06-01 15:34:25 +03001634 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001635 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001636 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001637 ctxt->eflags &= ~0xffff;
1638 ctxt->eflags |= temp_eflags;
1639 }
1640
1641 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1642 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1643
1644 return rc;
1645}
1646
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001647static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001648{
1649 switch(ctxt->mode) {
1650 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001651 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001652 case X86EMUL_MODE_VM86:
1653 case X86EMUL_MODE_PROT16:
1654 case X86EMUL_MODE_PROT32:
1655 case X86EMUL_MODE_PROT64:
1656 default:
1657 /* iret from protected mode unimplemented yet */
1658 return X86EMUL_UNHANDLEABLE;
1659 }
1660}
1661
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001662static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1663{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001664 int rc;
1665 unsigned short sel;
1666
Avi Kivity9dac77f2011-06-01 15:34:25 +03001667 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001668
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001669 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001670 if (rc != X86EMUL_CONTINUE)
1671 return rc;
1672
Avi Kivity9dac77f2011-06-01 15:34:25 +03001673 ctxt->_eip = 0;
1674 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001675 return X86EMUL_CONTINUE;
1676}
1677
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001678static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001679{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001680 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681}
1682
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001683static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001684{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001685 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001686 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001687 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001688 break;
1689 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001690 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001691 break;
1692 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001693 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001694 break;
1695 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001696 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697 break;
1698 case 4: /* sal/shl */
1699 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001700 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001701 break;
1702 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001703 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001704 break;
1705 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001706 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001707 break;
1708 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001709 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710}
1711
Avi Kivity3329ece2011-09-13 10:45:39 +03001712static int em_not(struct x86_emulate_ctxt *ctxt)
1713{
1714 ctxt->dst.val = ~ctxt->dst.val;
1715 return X86EMUL_CONTINUE;
1716}
1717
1718static int em_neg(struct x86_emulate_ctxt *ctxt)
1719{
1720 emulate_1op(ctxt, "neg");
1721 return X86EMUL_CONTINUE;
1722}
1723
1724static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
1725{
1726 u8 ex = 0;
1727
1728 emulate_1op_rax_rdx(ctxt, "mul", ex);
1729 return X86EMUL_CONTINUE;
1730}
1731
1732static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
1733{
1734 u8 ex = 0;
1735
1736 emulate_1op_rax_rdx(ctxt, "imul", ex);
1737 return X86EMUL_CONTINUE;
1738}
1739
1740static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001741{
Avi Kivity34d1f492010-08-26 11:59:01 +03001742 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743
Avi Kivity3329ece2011-09-13 10:45:39 +03001744 emulate_1op_rax_rdx(ctxt, "div", de);
1745 if (de)
1746 return emulate_de(ctxt);
1747 return X86EMUL_CONTINUE;
1748}
1749
1750static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
1751{
1752 u8 de = 0;
1753
1754 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03001755 if (de)
1756 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001757 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001758}
1759
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001760static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001762 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763
Avi Kivity9dac77f2011-06-01 15:34:25 +03001764 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001765 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03001766 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767 break;
1768 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03001769 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001771 case 2: /* call near abs */ {
1772 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001773 old_eip = ctxt->_eip;
1774 ctxt->_eip = ctxt->src.val;
1775 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001776 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001777 break;
1778 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001779 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001780 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001782 case 5: /* jmp far */
1783 rc = em_jmp_far(ctxt);
1784 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001785 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001786 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001787 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001789 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001790}
1791
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001792static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001794 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795
Avi Kivity9dac77f2011-06-01 15:34:25 +03001796 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1797 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1798 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1799 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001800 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001801 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001802 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1803 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001804
Laurent Vivier05f086f2007-09-24 11:10:55 +02001805 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001806 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001807 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001808}
1809
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001810static int em_ret(struct x86_emulate_ctxt *ctxt)
1811{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001812 ctxt->dst.type = OP_REG;
1813 ctxt->dst.addr.reg = &ctxt->_eip;
1814 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001815 return em_pop(ctxt);
1816}
1817
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001818static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001819{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001820 int rc;
1821 unsigned long cs;
1822
Avi Kivity9dac77f2011-06-01 15:34:25 +03001823 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001824 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001825 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001826 if (ctxt->op_bytes == 4)
1827 ctxt->_eip = (u32)ctxt->_eip;
1828 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001829 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001830 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001831 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001832 return rc;
1833}
1834
Avi Kivityd4b43252011-09-13 10:45:50 +03001835static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001836{
Avi Kivityd4b43252011-09-13 10:45:50 +03001837 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001838 unsigned short sel;
1839 int rc;
1840
Avi Kivity9dac77f2011-06-01 15:34:25 +03001841 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001842
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001843 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001844 if (rc != X86EMUL_CONTINUE)
1845 return rc;
1846
Avi Kivity9dac77f2011-06-01 15:34:25 +03001847 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001848 return rc;
1849}
1850
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001851static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001852setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001853 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001854{
Avi Kivity1aa36612011-04-27 13:20:30 +03001855 u16 selector;
1856
Gleb Natapov79168fd2010-04-28 19:15:30 +03001857 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001858 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001859 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001860
1861 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001862 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001863 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001864 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001865 cs->type = 0x0b; /* Read, Execute, Accessed */
1866 cs->s = 1;
1867 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001868 cs->p = 1;
1869 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001870
Gleb Natapov79168fd2010-04-28 19:15:30 +03001871 set_desc_base(ss, 0); /* flat segment */
1872 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001873 ss->g = 1; /* 4kb granularity */
1874 ss->s = 1;
1875 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001876 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001878 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879}
1880
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001881static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001882{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001883 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001884 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001886 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001887 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001888
1889 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001890 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001891 ctxt->mode == X86EMUL_MODE_VM86)
1892 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001893
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001894 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001895 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001896
Avi Kivity717746e2011-04-20 13:37:53 +03001897 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001898 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001899 cs_sel = (u16)(msr_data & 0xfffc);
1900 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001901
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001902 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001903 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001904 cs.l = 1;
1905 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001906 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1907 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001908
Avi Kivity9dac77f2011-06-01 15:34:25 +03001909 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001910 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001911#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001912 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001913
Avi Kivity717746e2011-04-20 13:37:53 +03001914 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001915 ctxt->mode == X86EMUL_MODE_PROT64 ?
1916 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001917 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001918
Avi Kivity717746e2011-04-20 13:37:53 +03001919 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001920 ctxt->eflags &= ~(msr_data | EFLG_RF);
1921#endif
1922 } else {
1923 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001924 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001925 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001926
1927 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1928 }
1929
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001930 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001931}
1932
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001933static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001934{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001935 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001936 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001937 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001938 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001939 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001940
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001941 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001942 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001943 if (ctxt->mode == X86EMUL_MODE_REAL)
1944 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001945
1946 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1947 * Therefore, we inject an #UD.
1948 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001949 if (ctxt->mode == X86EMUL_MODE_PROT64)
1950 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001951
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001952 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001953
Avi Kivity717746e2011-04-20 13:37:53 +03001954 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001955 switch (ctxt->mode) {
1956 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001957 if ((msr_data & 0xfffc) == 0x0)
1958 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001959 break;
1960 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001961 if (msr_data == 0x0)
1962 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001963 break;
1964 }
1965
1966 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001967 cs_sel = (u16)msr_data;
1968 cs_sel &= ~SELECTOR_RPL_MASK;
1969 ss_sel = cs_sel + 8;
1970 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001971 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001972 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001973 cs.l = 1;
1974 }
1975
Avi Kivity1aa36612011-04-27 13:20:30 +03001976 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1977 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001978
Avi Kivity717746e2011-04-20 13:37:53 +03001979 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001980 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001981
Avi Kivity717746e2011-04-20 13:37:53 +03001982 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001983 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001984
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001985 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001986}
1987
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001988static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001989{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001990 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001991 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001992 u64 msr_data;
1993 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001994 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001995
Gleb Natapova0044752010-02-10 14:21:31 +02001996 /* inject #GP if in real mode or Virtual 8086 mode */
1997 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001998 ctxt->mode == X86EMUL_MODE_VM86)
1999 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002000
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002001 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002002
Avi Kivity9dac77f2011-06-01 15:34:25 +03002003 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002004 usermode = X86EMUL_MODE_PROT64;
2005 else
2006 usermode = X86EMUL_MODE_PROT32;
2007
2008 cs.dpl = 3;
2009 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002010 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002011 switch (usermode) {
2012 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002013 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002014 if ((msr_data & 0xfffc) == 0x0)
2015 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002016 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002017 break;
2018 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002019 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002020 if (msr_data == 0x0)
2021 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002022 ss_sel = cs_sel + 8;
2023 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002024 cs.l = 1;
2025 break;
2026 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002027 cs_sel |= SELECTOR_RPL_MASK;
2028 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002029
Avi Kivity1aa36612011-04-27 13:20:30 +03002030 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2031 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002032
Avi Kivity9dac77f2011-06-01 15:34:25 +03002033 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2034 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002035
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002036 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002037}
2038
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002039static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002040{
2041 int iopl;
2042 if (ctxt->mode == X86EMUL_MODE_REAL)
2043 return false;
2044 if (ctxt->mode == X86EMUL_MODE_VM86)
2045 return true;
2046 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002047 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002048}
2049
2050static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002051 u16 port, u16 len)
2052{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002053 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002054 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002055 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002056 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002057 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002058 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002059 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002060
Avi Kivity1aa36612011-04-27 13:20:30 +03002061 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002062 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002063 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002064 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002065 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002066 base = get_desc_base(&tr_seg);
2067#ifdef CONFIG_X86_64
2068 base |= ((u64)base3) << 32;
2069#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002070 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002071 if (r != X86EMUL_CONTINUE)
2072 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002073 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002074 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002075 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002076 if (r != X86EMUL_CONTINUE)
2077 return false;
2078 if ((perm >> bit_idx) & mask)
2079 return false;
2080 return true;
2081}
2082
2083static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002084 u16 port, u16 len)
2085{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002086 if (ctxt->perm_ok)
2087 return true;
2088
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002089 if (emulator_bad_iopl(ctxt))
2090 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002091 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002092
2093 ctxt->perm_ok = true;
2094
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002095 return true;
2096}
2097
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002098static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002099 struct tss_segment_16 *tss)
2100{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002101 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002102 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002103 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2104 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2105 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2106 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2107 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2108 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2109 tss->si = ctxt->regs[VCPU_REGS_RSI];
2110 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002111
Avi Kivity1aa36612011-04-27 13:20:30 +03002112 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2113 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2114 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2115 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2116 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002117}
2118
2119static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002120 struct tss_segment_16 *tss)
2121{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002122 int ret;
2123
Avi Kivity9dac77f2011-06-01 15:34:25 +03002124 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002125 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002126 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2127 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2128 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2129 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2130 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2131 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2132 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2133 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002134
2135 /*
2136 * SDM says that segment selectors are loaded before segment
2137 * descriptors
2138 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002139 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2140 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2141 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2142 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2143 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002144
2145 /*
2146 * Now load segment descriptors. If fault happenes at this stage
2147 * it is handled in a context of new task
2148 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002149 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002150 if (ret != X86EMUL_CONTINUE)
2151 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002152 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153 if (ret != X86EMUL_CONTINUE)
2154 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002155 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002156 if (ret != X86EMUL_CONTINUE)
2157 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002158 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002159 if (ret != X86EMUL_CONTINUE)
2160 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002161 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162 if (ret != X86EMUL_CONTINUE)
2163 return ret;
2164
2165 return X86EMUL_CONTINUE;
2166}
2167
2168static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002169 u16 tss_selector, u16 old_tss_sel,
2170 ulong old_tss_base, struct desc_struct *new_desc)
2171{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002172 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002173 struct tss_segment_16 tss_seg;
2174 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002175 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002176
Avi Kivity0f65dd72011-04-20 13:37:53 +03002177 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002178 &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
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002183 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002184
Avi Kivity0f65dd72011-04-20 13:37:53 +03002185 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002186 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002187 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002188 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190
Avi Kivity0f65dd72011-04-20 13:37:53 +03002191 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002192 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002193 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002194 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002195 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196
2197 if (old_tss_sel != 0xffff) {
2198 tss_seg.prev_task_link = old_tss_sel;
2199
Avi Kivity0f65dd72011-04-20 13:37:53 +03002200 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002201 &tss_seg.prev_task_link,
2202 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002203 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002204 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002205 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002206 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002207 }
2208
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002209 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002210}
2211
2212static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002213 struct tss_segment_32 *tss)
2214{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002215 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002216 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002217 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002218 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2219 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2220 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2221 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2222 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2223 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2224 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2225 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002226
Avi Kivity1aa36612011-04-27 13:20:30 +03002227 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2228 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2229 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2230 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2231 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2232 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2233 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002234}
2235
2236static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002237 struct tss_segment_32 *tss)
2238{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002239 int ret;
2240
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002241 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002242 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002243 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002244 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002245 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2246 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2247 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2248 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2249 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2250 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2251 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2252 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002253
2254 /*
2255 * SDM says that segment selectors are loaded before segment
2256 * descriptors
2257 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002258 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2259 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2260 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2261 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2262 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2263 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2264 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002265
2266 /*
2267 * Now load segment descriptors. If fault happenes at this stage
2268 * it is handled in a context of new task
2269 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002270 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002271 if (ret != X86EMUL_CONTINUE)
2272 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002273 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002274 if (ret != X86EMUL_CONTINUE)
2275 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002276 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002277 if (ret != X86EMUL_CONTINUE)
2278 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002279 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002280 if (ret != X86EMUL_CONTINUE)
2281 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002282 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002283 if (ret != X86EMUL_CONTINUE)
2284 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002285 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286 if (ret != X86EMUL_CONTINUE)
2287 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002288 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002289 if (ret != X86EMUL_CONTINUE)
2290 return ret;
2291
2292 return X86EMUL_CONTINUE;
2293}
2294
2295static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002296 u16 tss_selector, u16 old_tss_sel,
2297 ulong old_tss_base, struct desc_struct *new_desc)
2298{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002299 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002300 struct tss_segment_32 tss_seg;
2301 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002302 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303
Avi Kivity0f65dd72011-04-20 13:37:53 +03002304 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002305 &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
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002310 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311
Avi Kivity0f65dd72011-04-20 13:37:53 +03002312 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002313 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002314 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002315 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002316 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317
Avi Kivity0f65dd72011-04-20 13:37:53 +03002318 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002319 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002320 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002321 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002322 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002323
2324 if (old_tss_sel != 0xffff) {
2325 tss_seg.prev_task_link = old_tss_sel;
2326
Avi Kivity0f65dd72011-04-20 13:37:53 +03002327 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328 &tss_seg.prev_task_link,
2329 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002330 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002331 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002332 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002334 }
2335
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002336 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002337}
2338
2339static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002340 u16 tss_selector, int reason,
2341 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002342{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002343 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002344 struct desc_struct curr_tss_desc, next_tss_desc;
2345 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002346 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002347 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002348 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002349 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350
2351 /* FIXME: old_tss_base == ~0 ? */
2352
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002353 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002354 if (ret != X86EMUL_CONTINUE)
2355 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002356 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002357 if (ret != X86EMUL_CONTINUE)
2358 return ret;
2359
2360 /* FIXME: check that next_tss_desc is tss */
2361
2362 if (reason != TASK_SWITCH_IRET) {
2363 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002364 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002365 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002366 }
2367
Gleb Natapovceffb452010-03-18 15:20:19 +02002368 desc_limit = desc_limit_scaled(&next_tss_desc);
2369 if (!next_tss_desc.p ||
2370 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2371 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002372 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002373 return X86EMUL_PROPAGATE_FAULT;
2374 }
2375
2376 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2377 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002378 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002379 }
2380
2381 if (reason == TASK_SWITCH_IRET)
2382 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2383
2384 /* set back link to prev task only if NT bit is set in eflags
2385 note that old_tss_sel is not used afetr this point */
2386 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2387 old_tss_sel = 0xffff;
2388
2389 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002390 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002391 old_tss_base, &next_tss_desc);
2392 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002393 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002394 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002395 if (ret != X86EMUL_CONTINUE)
2396 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397
2398 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2399 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2400
2401 if (reason != TASK_SWITCH_IRET) {
2402 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002403 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002404 }
2405
Avi Kivity717746e2011-04-20 13:37:53 +03002406 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002407 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408
Jan Kiszkae269fb22010-04-14 15:51:09 +02002409 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002410 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2411 ctxt->lock_prefix = 0;
2412 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002413 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002414 }
2415
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416 return ret;
2417}
2418
2419int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002420 u16 tss_selector, int reason,
2421 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002423 int rc;
2424
Avi Kivity9dac77f2011-06-01 15:34:25 +03002425 ctxt->_eip = ctxt->eip;
2426 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002427
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002428 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002429 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002430
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002431 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002432 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002433
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002434 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002435}
2436
Avi Kivity90de84f2010-11-17 15:28:21 +02002437static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002438 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002439{
Gleb Natapova682e352010-03-18 15:20:21 +02002440 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2441
Avi Kivity9dac77f2011-06-01 15:34:25 +03002442 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2443 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002444 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002445}
2446
Avi Kivity7af04fc2010-08-18 14:16:35 +03002447static int em_das(struct x86_emulate_ctxt *ctxt)
2448{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002449 u8 al, old_al;
2450 bool af, cf, old_cf;
2451
2452 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002453 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002454
2455 old_al = al;
2456 old_cf = cf;
2457 cf = false;
2458 af = ctxt->eflags & X86_EFLAGS_AF;
2459 if ((al & 0x0f) > 9 || af) {
2460 al -= 6;
2461 cf = old_cf | (al >= 250);
2462 af = true;
2463 } else {
2464 af = false;
2465 }
2466 if (old_al > 0x99 || old_cf) {
2467 al -= 0x60;
2468 cf = true;
2469 }
2470
Avi Kivity9dac77f2011-06-01 15:34:25 +03002471 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002472 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002473 ctxt->src.type = OP_IMM;
2474 ctxt->src.val = 0;
2475 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002476 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002477 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2478 if (cf)
2479 ctxt->eflags |= X86_EFLAGS_CF;
2480 if (af)
2481 ctxt->eflags |= X86_EFLAGS_AF;
2482 return X86EMUL_CONTINUE;
2483}
2484
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002485static int em_call(struct x86_emulate_ctxt *ctxt)
2486{
2487 long rel = ctxt->src.val;
2488
2489 ctxt->src.val = (unsigned long)ctxt->_eip;
2490 jmp_rel(ctxt, rel);
2491 return em_push(ctxt);
2492}
2493
Avi Kivity0ef753b2010-08-18 14:51:45 +03002494static int em_call_far(struct x86_emulate_ctxt *ctxt)
2495{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002496 u16 sel, old_cs;
2497 ulong old_eip;
2498 int rc;
2499
Avi Kivity1aa36612011-04-27 13:20:30 +03002500 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002501 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002502
Avi Kivity9dac77f2011-06-01 15:34:25 +03002503 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002504 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002505 return X86EMUL_CONTINUE;
2506
Avi Kivity9dac77f2011-06-01 15:34:25 +03002507 ctxt->_eip = 0;
2508 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002509
Avi Kivity9dac77f2011-06-01 15:34:25 +03002510 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002511 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002512 if (rc != X86EMUL_CONTINUE)
2513 return rc;
2514
Avi Kivity9dac77f2011-06-01 15:34:25 +03002515 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002516 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002517}
2518
Avi Kivity40ece7c2010-08-18 15:12:09 +03002519static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2520{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002521 int rc;
2522
Avi Kivity9dac77f2011-06-01 15:34:25 +03002523 ctxt->dst.type = OP_REG;
2524 ctxt->dst.addr.reg = &ctxt->_eip;
2525 ctxt->dst.bytes = ctxt->op_bytes;
2526 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002527 if (rc != X86EMUL_CONTINUE)
2528 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002529 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002530 return X86EMUL_CONTINUE;
2531}
2532
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002533static int em_add(struct x86_emulate_ctxt *ctxt)
2534{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002535 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002536 return X86EMUL_CONTINUE;
2537}
2538
2539static int em_or(struct x86_emulate_ctxt *ctxt)
2540{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002541 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002542 return X86EMUL_CONTINUE;
2543}
2544
2545static int em_adc(struct x86_emulate_ctxt *ctxt)
2546{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002547 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002548 return X86EMUL_CONTINUE;
2549}
2550
2551static int em_sbb(struct x86_emulate_ctxt *ctxt)
2552{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002553 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002554 return X86EMUL_CONTINUE;
2555}
2556
2557static int em_and(struct x86_emulate_ctxt *ctxt)
2558{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002559 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002560 return X86EMUL_CONTINUE;
2561}
2562
2563static int em_sub(struct x86_emulate_ctxt *ctxt)
2564{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002565 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002566 return X86EMUL_CONTINUE;
2567}
2568
2569static int em_xor(struct x86_emulate_ctxt *ctxt)
2570{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002571 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002572 return X86EMUL_CONTINUE;
2573}
2574
2575static int em_cmp(struct x86_emulate_ctxt *ctxt)
2576{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002577 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002578 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002579 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002580 return X86EMUL_CONTINUE;
2581}
2582
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002583static int em_test(struct x86_emulate_ctxt *ctxt)
2584{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002585 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002586 /* Disable writeback. */
2587 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002588 return X86EMUL_CONTINUE;
2589}
2590
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002591static int em_xchg(struct x86_emulate_ctxt *ctxt)
2592{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002593 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002594 ctxt->src.val = ctxt->dst.val;
2595 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002596
2597 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002598 ctxt->dst.val = ctxt->src.orig_val;
2599 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002600 return X86EMUL_CONTINUE;
2601}
2602
Avi Kivity5c82aa22010-08-18 18:31:43 +03002603static int em_imul(struct x86_emulate_ctxt *ctxt)
2604{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002605 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002606 return X86EMUL_CONTINUE;
2607}
2608
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002609static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2610{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002611 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002612 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002613}
2614
Avi Kivity61429142010-08-19 15:13:00 +03002615static int em_cwd(struct x86_emulate_ctxt *ctxt)
2616{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002617 ctxt->dst.type = OP_REG;
2618 ctxt->dst.bytes = ctxt->src.bytes;
2619 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2620 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002621
2622 return X86EMUL_CONTINUE;
2623}
2624
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002625static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2626{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002627 u64 tsc = 0;
2628
Avi Kivity717746e2011-04-20 13:37:53 +03002629 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002630 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2631 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002632 return X86EMUL_CONTINUE;
2633}
2634
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002635static int em_mov(struct x86_emulate_ctxt *ctxt)
2636{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002637 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002638 return X86EMUL_CONTINUE;
2639}
2640
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002641static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2642{
2643 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2644 return emulate_gp(ctxt, 0);
2645
2646 /* Disable writeback. */
2647 ctxt->dst.type = OP_NONE;
2648 return X86EMUL_CONTINUE;
2649}
2650
2651static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2652{
2653 unsigned long val;
2654
2655 if (ctxt->mode == X86EMUL_MODE_PROT64)
2656 val = ctxt->src.val & ~0ULL;
2657 else
2658 val = ctxt->src.val & ~0U;
2659
2660 /* #UD condition is already handled. */
2661 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2662 return emulate_gp(ctxt, 0);
2663
2664 /* Disable writeback. */
2665 ctxt->dst.type = OP_NONE;
2666 return X86EMUL_CONTINUE;
2667}
2668
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002669static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2670{
2671 u64 msr_data;
2672
2673 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2674 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2675 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2676 return emulate_gp(ctxt, 0);
2677
2678 return X86EMUL_CONTINUE;
2679}
2680
2681static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2682{
2683 u64 msr_data;
2684
2685 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2686 return emulate_gp(ctxt, 0);
2687
2688 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2689 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2690 return X86EMUL_CONTINUE;
2691}
2692
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002693static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2694{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002695 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002696 return emulate_ud(ctxt);
2697
Avi Kivity9dac77f2011-06-01 15:34:25 +03002698 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002699 return X86EMUL_CONTINUE;
2700}
2701
2702static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2703{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002704 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002705
Avi Kivity9dac77f2011-06-01 15:34:25 +03002706 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002707 return emulate_ud(ctxt);
2708
Avi Kivity9dac77f2011-06-01 15:34:25 +03002709 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002710 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2711
2712 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002713 ctxt->dst.type = OP_NONE;
2714 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002715}
2716
Avi Kivityaa97bb42010-01-20 18:09:23 +02002717static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2718{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002719 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002720 return X86EMUL_CONTINUE;
2721}
2722
Avi Kivity38503912011-03-31 18:48:09 +02002723static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2724{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002725 int rc;
2726 ulong linear;
2727
Avi Kivity9dac77f2011-06-01 15:34:25 +03002728 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002729 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002730 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002731 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002732 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002733 return X86EMUL_CONTINUE;
2734}
2735
Avi Kivity2d04a052011-04-20 15:32:49 +03002736static int em_clts(struct x86_emulate_ctxt *ctxt)
2737{
2738 ulong cr0;
2739
2740 cr0 = ctxt->ops->get_cr(ctxt, 0);
2741 cr0 &= ~X86_CR0_TS;
2742 ctxt->ops->set_cr(ctxt, 0, cr0);
2743 return X86EMUL_CONTINUE;
2744}
2745
Avi Kivity26d05cc2011-04-21 12:07:59 +03002746static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2747{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002748 int rc;
2749
Avi Kivity9dac77f2011-06-01 15:34:25 +03002750 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002751 return X86EMUL_UNHANDLEABLE;
2752
2753 rc = ctxt->ops->fix_hypercall(ctxt);
2754 if (rc != X86EMUL_CONTINUE)
2755 return rc;
2756
2757 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002758 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002759 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002760 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002761 return X86EMUL_CONTINUE;
2762}
2763
2764static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2765{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002766 struct desc_ptr desc_ptr;
2767 int rc;
2768
Avi Kivity9dac77f2011-06-01 15:34:25 +03002769 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002770 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002771 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002772 if (rc != X86EMUL_CONTINUE)
2773 return rc;
2774 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2775 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002776 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002777 return X86EMUL_CONTINUE;
2778}
2779
Avi Kivity5ef39c72011-04-21 12:21:50 +03002780static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002781{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002782 int rc;
2783
Avi Kivity5ef39c72011-04-21 12:21:50 +03002784 rc = ctxt->ops->fix_hypercall(ctxt);
2785
Avi Kivity26d05cc2011-04-21 12:07:59 +03002786 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002787 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002788 return rc;
2789}
2790
2791static int em_lidt(struct x86_emulate_ctxt *ctxt)
2792{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002793 struct desc_ptr desc_ptr;
2794 int rc;
2795
Avi Kivity9dac77f2011-06-01 15:34:25 +03002796 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002797 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002798 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002799 if (rc != X86EMUL_CONTINUE)
2800 return rc;
2801 ctxt->ops->set_idt(ctxt, &desc_ptr);
2802 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002803 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002804 return X86EMUL_CONTINUE;
2805}
2806
2807static int em_smsw(struct x86_emulate_ctxt *ctxt)
2808{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002809 ctxt->dst.bytes = 2;
2810 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002811 return X86EMUL_CONTINUE;
2812}
2813
2814static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2815{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002816 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002817 | (ctxt->src.val & 0x0f));
2818 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002819 return X86EMUL_CONTINUE;
2820}
2821
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002822static int em_loop(struct x86_emulate_ctxt *ctxt)
2823{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002824 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2825 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2826 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2827 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002828
2829 return X86EMUL_CONTINUE;
2830}
2831
2832static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2833{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002834 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2835 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002836
2837 return X86EMUL_CONTINUE;
2838}
2839
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09002840static int em_in(struct x86_emulate_ctxt *ctxt)
2841{
2842 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
2843 &ctxt->dst.val))
2844 return X86EMUL_IO_NEEDED;
2845
2846 return X86EMUL_CONTINUE;
2847}
2848
2849static int em_out(struct x86_emulate_ctxt *ctxt)
2850{
2851 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
2852 &ctxt->src.val, 1);
2853 /* Disable writeback. */
2854 ctxt->dst.type = OP_NONE;
2855 return X86EMUL_CONTINUE;
2856}
2857
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002858static int em_cli(struct x86_emulate_ctxt *ctxt)
2859{
2860 if (emulator_bad_iopl(ctxt))
2861 return emulate_gp(ctxt, 0);
2862
2863 ctxt->eflags &= ~X86_EFLAGS_IF;
2864 return X86EMUL_CONTINUE;
2865}
2866
2867static int em_sti(struct x86_emulate_ctxt *ctxt)
2868{
2869 if (emulator_bad_iopl(ctxt))
2870 return emulate_gp(ctxt, 0);
2871
2872 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2873 ctxt->eflags |= X86_EFLAGS_IF;
2874 return X86EMUL_CONTINUE;
2875}
2876
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09002877static int em_bt(struct x86_emulate_ctxt *ctxt)
2878{
2879 /* Disable writeback. */
2880 ctxt->dst.type = OP_NONE;
2881 /* only subword offset */
2882 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
2883
2884 emulate_2op_SrcV_nobyte(ctxt, "bt");
2885 return X86EMUL_CONTINUE;
2886}
2887
2888static int em_bts(struct x86_emulate_ctxt *ctxt)
2889{
2890 emulate_2op_SrcV_nobyte(ctxt, "bts");
2891 return X86EMUL_CONTINUE;
2892}
2893
2894static int em_btr(struct x86_emulate_ctxt *ctxt)
2895{
2896 emulate_2op_SrcV_nobyte(ctxt, "btr");
2897 return X86EMUL_CONTINUE;
2898}
2899
2900static int em_btc(struct x86_emulate_ctxt *ctxt)
2901{
2902 emulate_2op_SrcV_nobyte(ctxt, "btc");
2903 return X86EMUL_CONTINUE;
2904}
2905
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002906static bool valid_cr(int nr)
2907{
2908 switch (nr) {
2909 case 0:
2910 case 2 ... 4:
2911 case 8:
2912 return true;
2913 default:
2914 return false;
2915 }
2916}
2917
2918static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2919{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002920 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002921 return emulate_ud(ctxt);
2922
2923 return X86EMUL_CONTINUE;
2924}
2925
2926static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2927{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002928 u64 new_val = ctxt->src.val64;
2929 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002930 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002931
2932 static u64 cr_reserved_bits[] = {
2933 0xffffffff00000000ULL,
2934 0, 0, 0, /* CR3 checked later */
2935 CR4_RESERVED_BITS,
2936 0, 0, 0,
2937 CR8_RESERVED_BITS,
2938 };
2939
2940 if (!valid_cr(cr))
2941 return emulate_ud(ctxt);
2942
2943 if (new_val & cr_reserved_bits[cr])
2944 return emulate_gp(ctxt, 0);
2945
2946 switch (cr) {
2947 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002948 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002949 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2950 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2951 return emulate_gp(ctxt, 0);
2952
Avi Kivity717746e2011-04-20 13:37:53 +03002953 cr4 = ctxt->ops->get_cr(ctxt, 4);
2954 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002955
2956 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2957 !(cr4 & X86_CR4_PAE))
2958 return emulate_gp(ctxt, 0);
2959
2960 break;
2961 }
2962 case 3: {
2963 u64 rsvd = 0;
2964
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002965 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2966 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002967 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002968 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002969 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002970 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002971 rsvd = CR3_NONPAE_RESERVED_BITS;
2972
2973 if (new_val & rsvd)
2974 return emulate_gp(ctxt, 0);
2975
2976 break;
2977 }
2978 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002979 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002980
Avi Kivity717746e2011-04-20 13:37:53 +03002981 cr4 = ctxt->ops->get_cr(ctxt, 4);
2982 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002983
2984 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2985 return emulate_gp(ctxt, 0);
2986
2987 break;
2988 }
2989 }
2990
2991 return X86EMUL_CONTINUE;
2992}
2993
Joerg Roedel3b88e412011-04-04 12:39:29 +02002994static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2995{
2996 unsigned long dr7;
2997
Avi Kivity717746e2011-04-20 13:37:53 +03002998 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002999
3000 /* Check if DR7.Global_Enable is set */
3001 return dr7 & (1 << 13);
3002}
3003
3004static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3005{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003006 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003007 u64 cr4;
3008
3009 if (dr > 7)
3010 return emulate_ud(ctxt);
3011
Avi Kivity717746e2011-04-20 13:37:53 +03003012 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003013 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3014 return emulate_ud(ctxt);
3015
3016 if (check_dr7_gd(ctxt))
3017 return emulate_db(ctxt);
3018
3019 return X86EMUL_CONTINUE;
3020}
3021
3022static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3023{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003024 u64 new_val = ctxt->src.val64;
3025 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003026
3027 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3028 return emulate_gp(ctxt, 0);
3029
3030 return check_dr_read(ctxt);
3031}
3032
Joerg Roedel01de8b02011-04-04 12:39:31 +02003033static int check_svme(struct x86_emulate_ctxt *ctxt)
3034{
3035 u64 efer;
3036
Avi Kivity717746e2011-04-20 13:37:53 +03003037 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003038
3039 if (!(efer & EFER_SVME))
3040 return emulate_ud(ctxt);
3041
3042 return X86EMUL_CONTINUE;
3043}
3044
3045static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3046{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003047 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003048
3049 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003050 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003051 return emulate_gp(ctxt, 0);
3052
3053 return check_svme(ctxt);
3054}
3055
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003056static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3057{
Avi Kivity717746e2011-04-20 13:37:53 +03003058 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003059
Avi Kivity717746e2011-04-20 13:37:53 +03003060 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003061 return emulate_ud(ctxt);
3062
3063 return X86EMUL_CONTINUE;
3064}
3065
Joerg Roedel80612522011-04-04 12:39:33 +02003066static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3067{
Avi Kivity717746e2011-04-20 13:37:53 +03003068 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003070
Avi Kivity717746e2011-04-20 13:37:53 +03003071 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003072 (rcx > 3))
3073 return emulate_gp(ctxt, 0);
3074
3075 return X86EMUL_CONTINUE;
3076}
3077
Joerg Roedelf6511932011-04-04 12:39:35 +02003078static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3079{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003080 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3081 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003082 return emulate_gp(ctxt, 0);
3083
3084 return X86EMUL_CONTINUE;
3085}
3086
3087static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3088{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003089 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3090 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003091 return emulate_gp(ctxt, 0);
3092
3093 return X86EMUL_CONTINUE;
3094}
3095
Avi Kivity73fba5f2010-07-29 15:11:53 +03003096#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003097#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003098#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3099 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003100#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003101#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003102#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03003103#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003104#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003105#define II(_f, _e, _i) \
3106 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003107#define IIP(_f, _e, _i, _p) \
3108 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3109 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003110#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003111
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003112#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003113#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003114#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003115#define I2bvIP(_f, _e, _i, _p) \
3116 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003117
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003118#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3119 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3120 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003121
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003122static struct opcode group7_rm1[] = {
3123 DI(SrcNone | ModRM | Priv, monitor),
3124 DI(SrcNone | ModRM | Priv, mwait),
3125 N, N, N, N, N, N,
3126};
3127
Joerg Roedel01de8b02011-04-04 12:39:31 +02003128static struct opcode group7_rm3[] = {
3129 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003130 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003131 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3132 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3133 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3134 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3135 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3136 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3137};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003138
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003139static struct opcode group7_rm7[] = {
3140 N,
3141 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3142 N, N, N, N, N, N,
3143};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003144
Avi Kivity73fba5f2010-07-29 15:11:53 +03003145static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003146 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003147 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003148 I(Lock, em_adc),
3149 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003150 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003151 I(Lock, em_sub),
3152 I(Lock, em_xor),
3153 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003154};
3155
3156static struct opcode group1A[] = {
3157 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3158};
3159
3160static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003161 I(DstMem | SrcImm | ModRM, em_test),
3162 I(DstMem | SrcImm | ModRM, em_test),
3163 I(DstMem | SrcNone | ModRM | Lock, em_not),
3164 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3165 I(SrcMem | ModRM, em_mul_ex),
3166 I(SrcMem | ModRM, em_imul_ex),
3167 I(SrcMem | ModRM, em_div_ex),
3168 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003169};
3170
3171static struct opcode group4[] = {
3172 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3173 N, N, N, N, N, N,
3174};
3175
3176static struct opcode group5[] = {
3177 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003178 D(SrcMem | ModRM | Stack),
3179 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003180 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3181 D(SrcMem | ModRM | Stack), N,
3182};
3183
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003184static struct opcode group6[] = {
3185 DI(ModRM | Prot, sldt),
3186 DI(ModRM | Prot, str),
3187 DI(ModRM | Prot | Priv, lldt),
3188 DI(ModRM | Prot | Priv, ltr),
3189 N, N, N, N,
3190};
3191
Avi Kivity73fba5f2010-07-29 15:11:53 +03003192static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003193 DI(ModRM | Mov | DstMem | Priv, sgdt),
3194 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003195 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3196 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3197 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3198 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3199 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003200}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003201 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3202 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003203 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003204 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3205 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003206} };
3207
3208static struct opcode group8[] = {
3209 N, N, N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003210 I(DstMem | SrcImmByte | ModRM, em_bt),
3211 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_bts),
3212 I(DstMem | SrcImmByte | ModRM | Lock, em_btr),
3213 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003214};
3215
3216static struct group_dual group9 = { {
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003217 N, D(DstMem64 | ModRM | Lock | PageTable), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003218}, {
3219 N, N, N, N, N, N, N, N,
3220} };
3221
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003222static struct opcode group11[] = {
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003223 I(DstMem | SrcImm | ModRM | Mov | PageTable, em_mov),
3224 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003225};
3226
Avi Kivityaa97bb42010-01-20 18:09:23 +02003227static struct gprefix pfx_0f_6f_0f_7f = {
3228 N, N, N, I(Sse, em_movdqu),
3229};
3230
Avi Kivity73fba5f2010-07-29 15:11:53 +03003231static struct opcode opcode_table[256] = {
3232 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003233 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003234 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3235 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003236 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003237 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003238 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3239 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003240 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003241 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003242 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3243 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003244 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003245 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003246 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3247 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003248 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003249 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003250 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003251 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003252 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003253 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003254 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003255 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003256 /* 0x40 - 0x4F */
3257 X16(D(DstReg)),
3258 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003259 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003260 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003261 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003262 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003263 I(ImplicitOps | Stack | No64, em_pusha),
3264 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003265 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3266 N, N, N, N,
3267 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003268 I(SrcImm | Mov | Stack, em_push),
3269 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003270 I(SrcImmByte | Mov | Stack, em_push),
3271 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003272 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3273 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003274 /* 0x70 - 0x7F */
3275 X16(D(SrcImmByte)),
3276 /* 0x80 - 0x87 */
3277 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3278 G(DstMem | SrcImm | ModRM | Group, group1),
3279 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3280 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003281 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003282 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003283 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003284 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003285 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003286 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003287 D(ModRM | SrcMem | NoAccess | DstReg),
3288 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3289 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003290 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003291 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003292 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003293 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003294 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003295 II(ImplicitOps | Stack, em_pushf, pushf),
3296 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003297 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003298 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003299 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003300 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003301 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003302 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003303 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003304 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3305 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003306 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003307 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003308 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003309 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003310 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003311 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003312 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003313 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003314 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003315 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3316 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003317 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003318 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003319 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003320 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003321 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003322 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003323 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003324 N, N, N, N,
3325 /* 0xD8 - 0xDF */
3326 N, N, N, N, N, N, N, N,
3327 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003328 X3(I(SrcImmByte, em_loop)),
3329 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003330 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3331 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003332 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003333 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003334 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003335 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3336 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003337 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003338 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003339 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3340 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003341 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003342 D(ImplicitOps), D(ImplicitOps),
3343 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003344 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3345};
3346
3347static struct opcode twobyte_table[256] = {
3348 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003349 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003350 N, I(ImplicitOps | VendorSpecific, em_syscall),
3351 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003352 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003353 N, D(ImplicitOps | ModRM), N, N,
3354 /* 0x10 - 0x1F */
3355 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3356 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003357 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003358 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003359 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3360 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003361 N, N, N, N,
3362 N, N, N, N, N, N, N, N,
3363 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003364 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003365 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003366 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003367 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003368 I(ImplicitOps | VendorSpecific, em_sysenter),
3369 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003370 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003371 N, N, N, N, N, N, N, N,
3372 /* 0x40 - 0x4F */
3373 X16(D(DstReg | SrcMem | ModRM | Mov)),
3374 /* 0x50 - 0x5F */
3375 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3376 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003377 N, N, N, N,
3378 N, N, N, N,
3379 N, N, N, N,
3380 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003381 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003382 N, N, N, N,
3383 N, N, N, N,
3384 N, N, N, N,
3385 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003386 /* 0x80 - 0x8F */
3387 X16(D(SrcImm)),
3388 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003389 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003390 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003391 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003392 DI(ImplicitOps, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003393 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3394 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3395 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003396 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003397 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003398 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003399 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3400 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003401 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003402 /* 0xB0 - 0xB7 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003403 D2bv(DstMem | SrcReg | ModRM | Lock | PageTable),
Avi Kivityd4b43252011-09-13 10:45:50 +03003404 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003405 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003406 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3407 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003408 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003409 /* 0xB8 - 0xBF */
3410 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003411 G(BitOp, group8),
3412 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Wei Yongjund9574a22010-08-10 13:48:22 +08003413 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3414 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003415 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003416 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003417 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003418 N, N, N, GD(0, &group9),
3419 N, N, N, N, N, N, N, N,
3420 /* 0xD0 - 0xDF */
3421 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3422 /* 0xE0 - 0xEF */
3423 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3424 /* 0xF0 - 0xFF */
3425 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3426};
3427
3428#undef D
3429#undef N
3430#undef G
3431#undef GD
3432#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003433#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003434#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003435
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003436#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003437#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003438#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003439#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003440#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003441
Avi Kivity9dac77f2011-06-01 15:34:25 +03003442static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003443{
3444 unsigned size;
3445
Avi Kivity9dac77f2011-06-01 15:34:25 +03003446 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003447 if (size == 8)
3448 size = 4;
3449 return size;
3450}
3451
3452static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3453 unsigned size, bool sign_extension)
3454{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003455 int rc = X86EMUL_CONTINUE;
3456
3457 op->type = OP_IMM;
3458 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003459 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003460 /* NB. Immediates are sign-extended as necessary. */
3461 switch (op->bytes) {
3462 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003463 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003464 break;
3465 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003466 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003467 break;
3468 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003469 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003470 break;
3471 }
3472 if (!sign_extension) {
3473 switch (op->bytes) {
3474 case 1:
3475 op->val &= 0xff;
3476 break;
3477 case 2:
3478 op->val &= 0xffff;
3479 break;
3480 case 4:
3481 op->val &= 0xffffffff;
3482 break;
3483 }
3484 }
3485done:
3486 return rc;
3487}
3488
Avi Kivitya99455492011-09-13 10:45:41 +03003489static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3490 unsigned d)
3491{
3492 int rc = X86EMUL_CONTINUE;
3493
3494 switch (d) {
3495 case OpReg:
3496 decode_register_operand(ctxt, op,
Avi Kivity52179732011-09-13 10:45:46 +03003497 op == &ctxt->dst &&
Avi Kivitya99455492011-09-13 10:45:41 +03003498 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
3499 break;
3500 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003501 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03003502 break;
3503 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003504 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003505 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03003506 *op = ctxt->memop;
3507 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003508 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03003509 fetch_bit_operand(ctxt);
3510 op->orig_val = op->val;
3511 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003512 case OpMem64:
3513 ctxt->memop.bytes = 8;
3514 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03003515 case OpAcc:
3516 op->type = OP_REG;
3517 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3518 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3519 fetch_register_operand(op);
3520 op->orig_val = op->val;
3521 break;
3522 case OpDI:
3523 op->type = OP_MEM;
3524 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3525 op->addr.mem.ea =
3526 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3527 op->addr.mem.seg = VCPU_SREG_ES;
3528 op->val = 0;
3529 break;
3530 case OpDX:
3531 op->type = OP_REG;
3532 op->bytes = 2;
3533 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3534 fetch_register_operand(op);
3535 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003536 case OpCL:
3537 op->bytes = 1;
3538 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3539 break;
3540 case OpImmByte:
3541 rc = decode_imm(ctxt, op, 1, true);
3542 break;
3543 case OpOne:
3544 op->bytes = 1;
3545 op->val = 1;
3546 break;
3547 case OpImm:
3548 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3549 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03003550 case OpMem16:
3551 ctxt->memop.bytes = 2;
3552 goto mem_common;
3553 case OpMem32:
3554 ctxt->memop.bytes = 4;
3555 goto mem_common;
3556 case OpImmU16:
3557 rc = decode_imm(ctxt, op, 2, false);
3558 break;
3559 case OpImmU:
3560 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3561 break;
3562 case OpSI:
3563 op->type = OP_MEM;
3564 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3565 op->addr.mem.ea =
3566 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3567 op->addr.mem.seg = seg_override(ctxt);
3568 op->val = 0;
3569 break;
3570 case OpImmFAddr:
3571 op->type = OP_IMM;
3572 op->addr.mem.ea = ctxt->_eip;
3573 op->bytes = ctxt->op_bytes + 2;
3574 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3575 break;
3576 case OpMemFAddr:
3577 ctxt->memop.bytes = ctxt->op_bytes + 2;
3578 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003579 case OpES:
3580 op->val = VCPU_SREG_ES;
3581 break;
3582 case OpCS:
3583 op->val = VCPU_SREG_CS;
3584 break;
3585 case OpSS:
3586 op->val = VCPU_SREG_SS;
3587 break;
3588 case OpDS:
3589 op->val = VCPU_SREG_DS;
3590 break;
3591 case OpFS:
3592 op->val = VCPU_SREG_FS;
3593 break;
3594 case OpGS:
3595 op->val = VCPU_SREG_GS;
3596 break;
Avi Kivitya99455492011-09-13 10:45:41 +03003597 case OpImplicit:
3598 /* Special instructions do their own operand decoding. */
3599 default:
3600 op->type = OP_NONE; /* Disable writeback. */
3601 break;
3602 }
3603
3604done:
3605 return rc;
3606}
3607
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003608int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003609{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003610 int rc = X86EMUL_CONTINUE;
3611 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003612 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003613 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003614 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003615
Avi Kivityf09ed832011-09-13 10:45:40 +03003616 ctxt->memop.type = OP_NONE;
3617 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003618 ctxt->_eip = ctxt->eip;
3619 ctxt->fetch.start = ctxt->_eip;
3620 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003621 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003622 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003623
3624 switch (mode) {
3625 case X86EMUL_MODE_REAL:
3626 case X86EMUL_MODE_VM86:
3627 case X86EMUL_MODE_PROT16:
3628 def_op_bytes = def_ad_bytes = 2;
3629 break;
3630 case X86EMUL_MODE_PROT32:
3631 def_op_bytes = def_ad_bytes = 4;
3632 break;
3633#ifdef CONFIG_X86_64
3634 case X86EMUL_MODE_PROT64:
3635 def_op_bytes = 4;
3636 def_ad_bytes = 8;
3637 break;
3638#endif
3639 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003640 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003641 }
3642
Avi Kivity9dac77f2011-06-01 15:34:25 +03003643 ctxt->op_bytes = def_op_bytes;
3644 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003645
3646 /* Legacy prefixes. */
3647 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003648 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003649 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003650 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003651 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003652 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003653 break;
3654 case 0x67: /* address-size override */
3655 if (mode == X86EMUL_MODE_PROT64)
3656 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003657 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003658 else
3659 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003660 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003661 break;
3662 case 0x26: /* ES override */
3663 case 0x2e: /* CS override */
3664 case 0x36: /* SS override */
3665 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003666 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003667 break;
3668 case 0x64: /* FS override */
3669 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003670 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003671 break;
3672 case 0x40 ... 0x4f: /* REX */
3673 if (mode != X86EMUL_MODE_PROT64)
3674 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003675 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003676 continue;
3677 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003678 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003679 break;
3680 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003681 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003682 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003683 break;
3684 default:
3685 goto done_prefixes;
3686 }
3687
3688 /* Any legacy prefix after a REX prefix nullifies its effect. */
3689
Avi Kivity9dac77f2011-06-01 15:34:25 +03003690 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003691 }
3692
3693done_prefixes:
3694
3695 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003696 if (ctxt->rex_prefix & 8)
3697 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003698
3699 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003700 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003701 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003702 if (ctxt->b == 0x0f) {
3703 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003704 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003705 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003706 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003707 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003708
Avi Kivity9dac77f2011-06-01 15:34:25 +03003709 while (ctxt->d & GroupMask) {
3710 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003711 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003712 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003713 --ctxt->_eip;
3714 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003715 opcode = opcode.u.group[goffset];
3716 break;
3717 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003718 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003719 --ctxt->_eip;
3720 goffset = (ctxt->modrm >> 3) & 7;
3721 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003722 opcode = opcode.u.gdual->mod3[goffset];
3723 else
3724 opcode = opcode.u.gdual->mod012[goffset];
3725 break;
3726 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003727 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003728 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003729 break;
3730 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003731 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003732 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003733 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003734 switch (simd_prefix) {
3735 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3736 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3737 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3738 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3739 }
3740 break;
3741 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003742 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003743 }
Avi Kivity46561642011-04-24 14:09:59 +03003744
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003745 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003746 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003747 }
3748
Avi Kivity9dac77f2011-06-01 15:34:25 +03003749 ctxt->execute = opcode.u.execute;
3750 ctxt->check_perm = opcode.check_perm;
3751 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003752
3753 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003754 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003755 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003756
Avi Kivity9dac77f2011-06-01 15:34:25 +03003757 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003758 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003759
Avi Kivity9dac77f2011-06-01 15:34:25 +03003760 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3761 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003762
Avi Kivity9dac77f2011-06-01 15:34:25 +03003763 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003764 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003765 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003766 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003767 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003768 }
3769
Avi Kivity9dac77f2011-06-01 15:34:25 +03003770 if (ctxt->d & Sse)
3771 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003772
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003773 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003774 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003775 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003776 if (!ctxt->has_seg_override)
3777 set_seg_override(ctxt, ctxt->modrm_seg);
3778 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003779 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003780 if (rc != X86EMUL_CONTINUE)
3781 goto done;
3782
Avi Kivity9dac77f2011-06-01 15:34:25 +03003783 if (!ctxt->has_seg_override)
3784 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003785
Avi Kivityf09ed832011-09-13 10:45:40 +03003786 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003787
Avi Kivityf09ed832011-09-13 10:45:40 +03003788 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
3789 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003790
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003791 /*
3792 * Decode and fetch the source operand: register, memory
3793 * or immediate.
3794 */
Avi Kivity0fe59122011-09-13 10:45:47 +03003795 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003796 if (rc != X86EMUL_CONTINUE)
3797 goto done;
3798
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003799 /*
3800 * Decode and fetch the second source operand: register, memory
3801 * or immediate.
3802 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03003803 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003804 if (rc != X86EMUL_CONTINUE)
3805 goto done;
3806
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003807 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03003808 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003809
3810done:
Avi Kivityf09ed832011-09-13 10:45:40 +03003811 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
3812 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003813
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003814 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003815}
3816
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08003817bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
3818{
3819 return ctxt->d & PageTable;
3820}
3821
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003822static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3823{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003824 /* The second termination condition only applies for REPE
3825 * and REPNE. Test if the repeat string operation prefix is
3826 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3827 * corresponding termination condition according to:
3828 * - if REPE/REPZ and ZF = 0 then done
3829 * - if REPNE/REPNZ and ZF = 1 then done
3830 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003831 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3832 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3833 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003834 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003835 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003836 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3837 return true;
3838
3839 return false;
3840}
3841
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003842int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003843{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003844 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003845 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003846 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003847
Avi Kivity9dac77f2011-06-01 15:34:25 +03003848 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003849
Avi Kivity9dac77f2011-06-01 15:34:25 +03003850 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003851 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003852 goto done;
3853 }
3854
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003855 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003856 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003857 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003858 goto done;
3859 }
3860
Avi Kivity9dac77f2011-06-01 15:34:25 +03003861 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003862 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003863 goto done;
3864 }
3865
Avi Kivity9dac77f2011-06-01 15:34:25 +03003866 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003867 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3868 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003869 rc = emulate_ud(ctxt);
3870 goto done;
3871 }
3872
Avi Kivity9dac77f2011-06-01 15:34:25 +03003873 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003874 rc = emulate_nm(ctxt);
3875 goto done;
3876 }
3877
Avi Kivity9dac77f2011-06-01 15:34:25 +03003878 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3879 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003880 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003881 if (rc != X86EMUL_CONTINUE)
3882 goto done;
3883 }
3884
Gleb Natapove92805a2010-02-10 14:21:35 +02003885 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003886 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003887 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003888 goto done;
3889 }
3890
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003891 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003892 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003893 rc = emulate_ud(ctxt);
3894 goto done;
3895 }
3896
Joerg Roedeld09beab2011-04-04 12:39:25 +02003897 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003898 if (ctxt->check_perm) {
3899 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003900 if (rc != X86EMUL_CONTINUE)
3901 goto done;
3902 }
3903
Avi Kivity9dac77f2011-06-01 15:34:25 +03003904 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3905 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003906 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003907 if (rc != X86EMUL_CONTINUE)
3908 goto done;
3909 }
3910
Avi Kivity9dac77f2011-06-01 15:34:25 +03003911 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003912 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003913 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3914 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003915 goto done;
3916 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003917 }
3918
Avi Kivity9dac77f2011-06-01 15:34:25 +03003919 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3920 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3921 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003922 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003923 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003924 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003925 }
3926
Avi Kivity9dac77f2011-06-01 15:34:25 +03003927 if (ctxt->src2.type == OP_MEM) {
3928 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3929 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003930 if (rc != X86EMUL_CONTINUE)
3931 goto done;
3932 }
3933
Avi Kivity9dac77f2011-06-01 15:34:25 +03003934 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003935 goto special_insn;
3936
3937
Avi Kivity9dac77f2011-06-01 15:34:25 +03003938 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003939 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003940 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3941 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003942 if (rc != X86EMUL_CONTINUE)
3943 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003944 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003945 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003946
Avi Kivity018a98d2007-11-27 19:30:56 +02003947special_insn:
3948
Avi Kivity9dac77f2011-06-01 15:34:25 +03003949 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3950 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003951 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003952 if (rc != X86EMUL_CONTINUE)
3953 goto done;
3954 }
3955
Avi Kivity9dac77f2011-06-01 15:34:25 +03003956 if (ctxt->execute) {
3957 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003958 if (rc != X86EMUL_CONTINUE)
3959 goto done;
3960 goto writeback;
3961 }
3962
Avi Kivity9dac77f2011-06-01 15:34:25 +03003963 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964 goto twobyte_insn;
3965
Avi Kivity9dac77f2011-06-01 15:34:25 +03003966 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02003967 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003968 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02003969 break;
3970 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003971 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02003972 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003973 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003974 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003976 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003977 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003978 case 0x6c: /* insb */
3979 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003980 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003981 rc = em_in(ctxt);
3982 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003983 case 0x6e: /* outsb */
3984 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003985 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003986 rc = em_out(ctxt);
Gleb Natapov79729952010-03-18 15:20:24 +02003987 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003988 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003989 if (test_cc(ctxt->b, ctxt->eflags))
3990 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003991 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003992 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003993 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003994 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003996 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003998 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003999 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004000 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004001 rc = em_xchg(ctxt);
4002 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004003 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004004 switch (ctxt->op_bytes) {
4005 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4006 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4007 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004008 }
4009 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004010 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004011 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004012 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004013 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004014 rc = emulate_int(ctxt, 3);
4015 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004016 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004017 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004018 break;
4019 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004020 if (ctxt->eflags & EFLG_OF)
4021 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004022 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004023 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004024 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004025 break;
4026 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004027 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004028 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004029 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004030 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004031 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004032 jmp_rel(ctxt, ctxt->src.val);
4033 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004034 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004035 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004036 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004037 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004038 case 0xf5: /* cmc */
4039 /* complement carry flag from eflags reg */
4040 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004041 break;
4042 case 0xf8: /* clc */
4043 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004044 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004045 case 0xf9: /* stc */
4046 ctxt->eflags |= EFLG_CF;
4047 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004048 case 0xfc: /* cld */
4049 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004050 break;
4051 case 0xfd: /* std */
4052 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004053 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02004054 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004055 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004056 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02004057 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004058 rc = em_grp45(ctxt);
4059 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004060 default:
4061 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004063
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004064 if (rc != X86EMUL_CONTINUE)
4065 goto done;
4066
Avi Kivity018a98d2007-11-27 19:30:56 +02004067writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004068 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004069 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004070 goto done;
4071
Gleb Natapov5cd21912010-03-18 15:20:26 +02004072 /*
4073 * restore dst type in case the decoding will be reused
4074 * (happens for string instruction )
4075 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004076 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004077
Avi Kivity9dac77f2011-06-01 15:34:25 +03004078 if ((ctxt->d & SrcMask) == SrcSI)
4079 string_addr_inc(ctxt, seg_override(ctxt),
4080 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004081
Avi Kivity9dac77f2011-06-01 15:34:25 +03004082 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004083 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004084 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004085
Avi Kivity9dac77f2011-06-01 15:34:25 +03004086 if (ctxt->rep_prefix && (ctxt->d & String)) {
4087 struct read_cache *r = &ctxt->io_read;
4088 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004089
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004090 if (!string_insn_completed(ctxt)) {
4091 /*
4092 * Re-enter guest when pio read ahead buffer is empty
4093 * or, if it is not used, after each 1024 iteration.
4094 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004095 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004096 (r->end == 0 || r->end != r->pos)) {
4097 /*
4098 * Reset read cache. Usually happens before
4099 * decode, but since instruction is restarted
4100 * we have to do it here.
4101 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004103 return EMULATION_RESTART;
4104 }
4105 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004106 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004107 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004108
Avi Kivity9dac77f2011-06-01 15:34:25 +03004109 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004110
4111done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004112 if (rc == X86EMUL_PROPAGATE_FAULT)
4113 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004114 if (rc == X86EMUL_INTERCEPTED)
4115 return EMULATION_INTERCEPTED;
4116
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004117 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004118
4119twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004120 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004121 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004122 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004123 break;
4124 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004125 case 0x0d: /* GrpP (prefetch) */
4126 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004127 break;
4128 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004129 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004130 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004132 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004135 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4136 if (!test_cc(ctxt->b, ctxt->eflags))
4137 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004139 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004140 if (test_cc(ctxt->b, ctxt->eflags))
4141 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004142 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004143 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004144 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004145 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004146 case 0xa4: /* shld imm8, r, r/m */
4147 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004148 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004149 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004150 case 0xac: /* shrd imm8, r, r/m */
4151 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004152 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004153 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004154 case 0xae: /* clflush */
4155 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156 case 0xb0 ... 0xb1: /* cmpxchg */
4157 /*
4158 * Save real source value, then compare EAX against
4159 * destination.
4160 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004161 ctxt->src.orig_val = ctxt->src.val;
4162 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004163 emulate_2op_SrcV(ctxt, "cmp");
Laurent Vivier05f086f2007-09-24 11:10:55 +02004164 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004166 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004167 } else {
4168 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004169 ctxt->dst.type = OP_REG;
4170 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 }
4172 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004174 ctxt->dst.bytes = ctxt->op_bytes;
4175 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4176 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004178 case 0xbc: { /* bsf */
4179 u8 zf;
4180 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004181 : "=r"(ctxt->dst.val), "=q"(zf)
4182 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004183 ctxt->eflags &= ~X86_EFLAGS_ZF;
4184 if (zf) {
4185 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004186 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004187 }
4188 break;
4189 }
4190 case 0xbd: { /* bsr */
4191 u8 zf;
4192 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004193 : "=r"(ctxt->dst.val), "=q"(zf)
4194 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004195 ctxt->eflags &= ~X86_EFLAGS_ZF;
4196 if (zf) {
4197 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004198 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004199 }
4200 break;
4201 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004202 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004203 ctxt->dst.bytes = ctxt->op_bytes;
4204 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4205 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004206 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004207 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004208 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004209 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004210 ctxt->src.val = ctxt->dst.orig_val;
4211 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004212 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004213 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004214 ctxt->dst.bytes = ctxt->op_bytes;
4215 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4216 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004217 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004218 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004219 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004220 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004221 default:
4222 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004223 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004224
4225 if (rc != X86EMUL_CONTINUE)
4226 goto done;
4227
Avi Kivity6aa8b732006-12-10 02:21:36 -08004228 goto writeback;
4229
4230cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004231 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004232}