blob: 799000d8bf8b44a1afd5069af9afc5c1e62e67f3 [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
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09001835static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
1836{
1837 /* Save real source value, then compare EAX against destination. */
1838 ctxt->src.orig_val = ctxt->src.val;
1839 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
1840 emulate_2op_SrcV(ctxt, "cmp");
1841
1842 if (ctxt->eflags & EFLG_ZF) {
1843 /* Success: write back to memory. */
1844 ctxt->dst.val = ctxt->src.orig_val;
1845 } else {
1846 /* Failure: write the value we saw to EAX. */
1847 ctxt->dst.type = OP_REG;
1848 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
1849 }
1850 return X86EMUL_CONTINUE;
1851}
1852
Avi Kivityd4b43252011-09-13 10:45:50 +03001853static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001854{
Avi Kivityd4b43252011-09-13 10:45:50 +03001855 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001856 unsigned short sel;
1857 int rc;
1858
Avi Kivity9dac77f2011-06-01 15:34:25 +03001859 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001860
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001861 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001862 if (rc != X86EMUL_CONTINUE)
1863 return rc;
1864
Avi Kivity9dac77f2011-06-01 15:34:25 +03001865 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001866 return rc;
1867}
1868
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001869static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001870setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001871 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001872{
Avi Kivity1aa36612011-04-27 13:20:30 +03001873 u16 selector;
1874
Gleb Natapov79168fd2010-04-28 19:15:30 +03001875 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001876 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001877 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001878
1879 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001880 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001881 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001883 cs->type = 0x0b; /* Read, Execute, Accessed */
1884 cs->s = 1;
1885 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001886 cs->p = 1;
1887 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001888
Gleb Natapov79168fd2010-04-28 19:15:30 +03001889 set_desc_base(ss, 0); /* flat segment */
1890 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891 ss->g = 1; /* 4kb granularity */
1892 ss->s = 1;
1893 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001894 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001895 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001896 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001897}
1898
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001899static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001900{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001901 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001902 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001903 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001904 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001905 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001906
1907 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001908 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001909 ctxt->mode == X86EMUL_MODE_VM86)
1910 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001911
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001912 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001913 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001914
Avi Kivity717746e2011-04-20 13:37:53 +03001915 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001916 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001917 cs_sel = (u16)(msr_data & 0xfffc);
1918 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001919
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001920 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001921 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001922 cs.l = 1;
1923 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001924 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1925 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001926
Avi Kivity9dac77f2011-06-01 15:34:25 +03001927 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001928 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001929#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001930 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001931
Avi Kivity717746e2011-04-20 13:37:53 +03001932 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001933 ctxt->mode == X86EMUL_MODE_PROT64 ?
1934 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001935 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001936
Avi Kivity717746e2011-04-20 13:37:53 +03001937 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001938 ctxt->eflags &= ~(msr_data | EFLG_RF);
1939#endif
1940 } else {
1941 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001942 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001943 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001944
1945 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1946 }
1947
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001948 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001949}
1950
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001951static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001952{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001953 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001954 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001955 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001956 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001957 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001958
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001959 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001960 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001961 if (ctxt->mode == X86EMUL_MODE_REAL)
1962 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001963
1964 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1965 * Therefore, we inject an #UD.
1966 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001967 if (ctxt->mode == X86EMUL_MODE_PROT64)
1968 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001969
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001970 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001971
Avi Kivity717746e2011-04-20 13:37:53 +03001972 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001973 switch (ctxt->mode) {
1974 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001975 if ((msr_data & 0xfffc) == 0x0)
1976 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001977 break;
1978 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001979 if (msr_data == 0x0)
1980 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001981 break;
1982 }
1983
1984 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001985 cs_sel = (u16)msr_data;
1986 cs_sel &= ~SELECTOR_RPL_MASK;
1987 ss_sel = cs_sel + 8;
1988 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001989 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001990 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001991 cs.l = 1;
1992 }
1993
Avi Kivity1aa36612011-04-27 13:20:30 +03001994 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1995 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001996
Avi Kivity717746e2011-04-20 13:37:53 +03001997 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001998 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001999
Avi Kivity717746e2011-04-20 13:37:53 +03002000 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002001 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002002
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002003 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002004}
2005
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002006static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002007{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002008 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002009 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002010 u64 msr_data;
2011 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002012 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002013
Gleb Natapova0044752010-02-10 14:21:31 +02002014 /* inject #GP if in real mode or Virtual 8086 mode */
2015 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002016 ctxt->mode == X86EMUL_MODE_VM86)
2017 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002018
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002019 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002020
Avi Kivity9dac77f2011-06-01 15:34:25 +03002021 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002022 usermode = X86EMUL_MODE_PROT64;
2023 else
2024 usermode = X86EMUL_MODE_PROT32;
2025
2026 cs.dpl = 3;
2027 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002028 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002029 switch (usermode) {
2030 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002031 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002032 if ((msr_data & 0xfffc) == 0x0)
2033 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002034 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002035 break;
2036 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002037 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002038 if (msr_data == 0x0)
2039 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002040 ss_sel = cs_sel + 8;
2041 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002042 cs.l = 1;
2043 break;
2044 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002045 cs_sel |= SELECTOR_RPL_MASK;
2046 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002047
Avi Kivity1aa36612011-04-27 13:20:30 +03002048 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2049 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002050
Avi Kivity9dac77f2011-06-01 15:34:25 +03002051 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2052 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002053
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002054 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002055}
2056
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002057static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002058{
2059 int iopl;
2060 if (ctxt->mode == X86EMUL_MODE_REAL)
2061 return false;
2062 if (ctxt->mode == X86EMUL_MODE_VM86)
2063 return true;
2064 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002065 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002066}
2067
2068static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002069 u16 port, u16 len)
2070{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002071 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002072 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002073 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002074 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002075 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002076 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002077 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002078
Avi Kivity1aa36612011-04-27 13:20:30 +03002079 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002080 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002081 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002082 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002083 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002084 base = get_desc_base(&tr_seg);
2085#ifdef CONFIG_X86_64
2086 base |= ((u64)base3) << 32;
2087#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002088 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002089 if (r != X86EMUL_CONTINUE)
2090 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002091 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002092 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002093 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002094 if (r != X86EMUL_CONTINUE)
2095 return false;
2096 if ((perm >> bit_idx) & mask)
2097 return false;
2098 return true;
2099}
2100
2101static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002102 u16 port, u16 len)
2103{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002104 if (ctxt->perm_ok)
2105 return true;
2106
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002107 if (emulator_bad_iopl(ctxt))
2108 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002109 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002110
2111 ctxt->perm_ok = true;
2112
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002113 return true;
2114}
2115
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002116static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002117 struct tss_segment_16 *tss)
2118{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002119 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002120 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002121 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2122 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2123 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2124 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2125 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2126 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2127 tss->si = ctxt->regs[VCPU_REGS_RSI];
2128 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002129
Avi Kivity1aa36612011-04-27 13:20:30 +03002130 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2131 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2132 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2133 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2134 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002135}
2136
2137static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002138 struct tss_segment_16 *tss)
2139{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002140 int ret;
2141
Avi Kivity9dac77f2011-06-01 15:34:25 +03002142 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002143 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002144 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2145 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2146 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2147 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2148 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2149 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2150 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2151 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152
2153 /*
2154 * SDM says that segment selectors are loaded before segment
2155 * descriptors
2156 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002157 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2158 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2159 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2160 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2161 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162
2163 /*
2164 * Now load segment descriptors. If fault happenes at this stage
2165 * it is handled in a context of new task
2166 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002167 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168 if (ret != X86EMUL_CONTINUE)
2169 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002170 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002171 if (ret != X86EMUL_CONTINUE)
2172 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002173 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002174 if (ret != X86EMUL_CONTINUE)
2175 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002176 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177 if (ret != X86EMUL_CONTINUE)
2178 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002179 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180 if (ret != X86EMUL_CONTINUE)
2181 return ret;
2182
2183 return X86EMUL_CONTINUE;
2184}
2185
2186static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002187 u16 tss_selector, u16 old_tss_sel,
2188 ulong old_tss_base, struct desc_struct *new_desc)
2189{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002190 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002191 struct tss_segment_16 tss_seg;
2192 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002193 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002194
Avi Kivity0f65dd72011-04-20 13:37:53 +03002195 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002196 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002197 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002198 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002199 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002200
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002201 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002202
Avi Kivity0f65dd72011-04-20 13:37:53 +03002203 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002204 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002205 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002206 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002207 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002208
Avi Kivity0f65dd72011-04-20 13:37:53 +03002209 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002210 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002211 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002212 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002213 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002214
2215 if (old_tss_sel != 0xffff) {
2216 tss_seg.prev_task_link = old_tss_sel;
2217
Avi Kivity0f65dd72011-04-20 13:37:53 +03002218 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002219 &tss_seg.prev_task_link,
2220 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002221 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002222 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002223 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002224 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002225 }
2226
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002227 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002228}
2229
2230static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002231 struct tss_segment_32 *tss)
2232{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002233 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002234 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002235 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002236 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2237 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2238 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2239 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2240 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2241 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2242 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2243 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002244
Avi Kivity1aa36612011-04-27 13:20:30 +03002245 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2246 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2247 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2248 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2249 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2250 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2251 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002252}
2253
2254static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002255 struct tss_segment_32 *tss)
2256{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002257 int ret;
2258
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002259 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002260 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002261 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002262 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002263 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2264 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2265 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2266 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2267 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2268 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2269 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2270 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002271
2272 /*
2273 * SDM says that segment selectors are loaded before segment
2274 * descriptors
2275 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002276 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2277 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2278 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2279 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2280 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2281 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2282 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002283
2284 /*
2285 * Now load segment descriptors. If fault happenes at this stage
2286 * it is handled in a context of new task
2287 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002288 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002289 if (ret != X86EMUL_CONTINUE)
2290 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002291 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292 if (ret != X86EMUL_CONTINUE)
2293 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002294 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002295 if (ret != X86EMUL_CONTINUE)
2296 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002297 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298 if (ret != X86EMUL_CONTINUE)
2299 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002300 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002301 if (ret != X86EMUL_CONTINUE)
2302 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002303 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002304 if (ret != X86EMUL_CONTINUE)
2305 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002306 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002307 if (ret != X86EMUL_CONTINUE)
2308 return ret;
2309
2310 return X86EMUL_CONTINUE;
2311}
2312
2313static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002314 u16 tss_selector, u16 old_tss_sel,
2315 ulong old_tss_base, struct desc_struct *new_desc)
2316{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002317 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318 struct tss_segment_32 tss_seg;
2319 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002320 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002321
Avi Kivity0f65dd72011-04-20 13:37:53 +03002322 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002323 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002324 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002325 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002328 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002329
Avi Kivity0f65dd72011-04-20 13:37:53 +03002330 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002331 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002332 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002334 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002335
Avi Kivity0f65dd72011-04-20 13:37:53 +03002336 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002337 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002338 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002340 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002341
2342 if (old_tss_sel != 0xffff) {
2343 tss_seg.prev_task_link = old_tss_sel;
2344
Avi Kivity0f65dd72011-04-20 13:37:53 +03002345 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002346 &tss_seg.prev_task_link,
2347 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002348 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002349 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002351 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002352 }
2353
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002354 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002355}
2356
2357static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002358 u16 tss_selector, int reason,
2359 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002360{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002361 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002362 struct desc_struct curr_tss_desc, next_tss_desc;
2363 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002364 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002365 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002366 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002367 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002368
2369 /* FIXME: old_tss_base == ~0 ? */
2370
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002371 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002372 if (ret != X86EMUL_CONTINUE)
2373 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002374 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375 if (ret != X86EMUL_CONTINUE)
2376 return ret;
2377
2378 /* FIXME: check that next_tss_desc is tss */
2379
2380 if (reason != TASK_SWITCH_IRET) {
2381 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002382 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002383 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002384 }
2385
Gleb Natapovceffb452010-03-18 15:20:19 +02002386 desc_limit = desc_limit_scaled(&next_tss_desc);
2387 if (!next_tss_desc.p ||
2388 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2389 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002390 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002391 return X86EMUL_PROPAGATE_FAULT;
2392 }
2393
2394 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2395 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002396 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397 }
2398
2399 if (reason == TASK_SWITCH_IRET)
2400 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2401
2402 /* set back link to prev task only if NT bit is set in eflags
2403 note that old_tss_sel is not used afetr this point */
2404 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2405 old_tss_sel = 0xffff;
2406
2407 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002408 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 old_tss_base, &next_tss_desc);
2410 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002411 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002413 if (ret != X86EMUL_CONTINUE)
2414 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002415
2416 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2417 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2418
2419 if (reason != TASK_SWITCH_IRET) {
2420 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002421 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002422 }
2423
Avi Kivity717746e2011-04-20 13:37:53 +03002424 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002425 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002426
Jan Kiszkae269fb22010-04-14 15:51:09 +02002427 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002428 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2429 ctxt->lock_prefix = 0;
2430 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002431 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002432 }
2433
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002434 return ret;
2435}
2436
2437int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002438 u16 tss_selector, int reason,
2439 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002440{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002441 int rc;
2442
Avi Kivity9dac77f2011-06-01 15:34:25 +03002443 ctxt->_eip = ctxt->eip;
2444 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002445
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002446 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002447 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002448
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002449 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002450 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002451
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002452 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002453}
2454
Avi Kivity90de84f2010-11-17 15:28:21 +02002455static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002456 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002457{
Gleb Natapova682e352010-03-18 15:20:21 +02002458 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2459
Avi Kivity9dac77f2011-06-01 15:34:25 +03002460 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2461 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002462 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002463}
2464
Avi Kivity7af04fc2010-08-18 14:16:35 +03002465static int em_das(struct x86_emulate_ctxt *ctxt)
2466{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002467 u8 al, old_al;
2468 bool af, cf, old_cf;
2469
2470 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002471 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002472
2473 old_al = al;
2474 old_cf = cf;
2475 cf = false;
2476 af = ctxt->eflags & X86_EFLAGS_AF;
2477 if ((al & 0x0f) > 9 || af) {
2478 al -= 6;
2479 cf = old_cf | (al >= 250);
2480 af = true;
2481 } else {
2482 af = false;
2483 }
2484 if (old_al > 0x99 || old_cf) {
2485 al -= 0x60;
2486 cf = true;
2487 }
2488
Avi Kivity9dac77f2011-06-01 15:34:25 +03002489 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002490 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002491 ctxt->src.type = OP_IMM;
2492 ctxt->src.val = 0;
2493 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002494 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002495 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2496 if (cf)
2497 ctxt->eflags |= X86_EFLAGS_CF;
2498 if (af)
2499 ctxt->eflags |= X86_EFLAGS_AF;
2500 return X86EMUL_CONTINUE;
2501}
2502
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002503static int em_call(struct x86_emulate_ctxt *ctxt)
2504{
2505 long rel = ctxt->src.val;
2506
2507 ctxt->src.val = (unsigned long)ctxt->_eip;
2508 jmp_rel(ctxt, rel);
2509 return em_push(ctxt);
2510}
2511
Avi Kivity0ef753b2010-08-18 14:51:45 +03002512static int em_call_far(struct x86_emulate_ctxt *ctxt)
2513{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002514 u16 sel, old_cs;
2515 ulong old_eip;
2516 int rc;
2517
Avi Kivity1aa36612011-04-27 13:20:30 +03002518 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002519 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002520
Avi Kivity9dac77f2011-06-01 15:34:25 +03002521 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002522 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002523 return X86EMUL_CONTINUE;
2524
Avi Kivity9dac77f2011-06-01 15:34:25 +03002525 ctxt->_eip = 0;
2526 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002527
Avi Kivity9dac77f2011-06-01 15:34:25 +03002528 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002529 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002530 if (rc != X86EMUL_CONTINUE)
2531 return rc;
2532
Avi Kivity9dac77f2011-06-01 15:34:25 +03002533 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002534 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002535}
2536
Avi Kivity40ece7c2010-08-18 15:12:09 +03002537static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2538{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002539 int rc;
2540
Avi Kivity9dac77f2011-06-01 15:34:25 +03002541 ctxt->dst.type = OP_REG;
2542 ctxt->dst.addr.reg = &ctxt->_eip;
2543 ctxt->dst.bytes = ctxt->op_bytes;
2544 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002545 if (rc != X86EMUL_CONTINUE)
2546 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002547 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002548 return X86EMUL_CONTINUE;
2549}
2550
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002551static int em_add(struct x86_emulate_ctxt *ctxt)
2552{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002553 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002554 return X86EMUL_CONTINUE;
2555}
2556
2557static int em_or(struct x86_emulate_ctxt *ctxt)
2558{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002559 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002560 return X86EMUL_CONTINUE;
2561}
2562
2563static int em_adc(struct x86_emulate_ctxt *ctxt)
2564{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002565 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002566 return X86EMUL_CONTINUE;
2567}
2568
2569static int em_sbb(struct x86_emulate_ctxt *ctxt)
2570{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002571 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002572 return X86EMUL_CONTINUE;
2573}
2574
2575static int em_and(struct x86_emulate_ctxt *ctxt)
2576{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002577 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002578 return X86EMUL_CONTINUE;
2579}
2580
2581static int em_sub(struct x86_emulate_ctxt *ctxt)
2582{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002583 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002584 return X86EMUL_CONTINUE;
2585}
2586
2587static int em_xor(struct x86_emulate_ctxt *ctxt)
2588{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002589 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002590 return X86EMUL_CONTINUE;
2591}
2592
2593static int em_cmp(struct x86_emulate_ctxt *ctxt)
2594{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002595 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002596 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002597 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002598 return X86EMUL_CONTINUE;
2599}
2600
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002601static int em_test(struct x86_emulate_ctxt *ctxt)
2602{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002603 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03002604 /* Disable writeback. */
2605 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002606 return X86EMUL_CONTINUE;
2607}
2608
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002609static int em_xchg(struct x86_emulate_ctxt *ctxt)
2610{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002611 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002612 ctxt->src.val = ctxt->dst.val;
2613 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002614
2615 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002616 ctxt->dst.val = ctxt->src.orig_val;
2617 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002618 return X86EMUL_CONTINUE;
2619}
2620
Avi Kivity5c82aa22010-08-18 18:31:43 +03002621static int em_imul(struct x86_emulate_ctxt *ctxt)
2622{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002623 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002624 return X86EMUL_CONTINUE;
2625}
2626
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002627static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2628{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002629 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002630 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002631}
2632
Avi Kivity61429142010-08-19 15:13:00 +03002633static int em_cwd(struct x86_emulate_ctxt *ctxt)
2634{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002635 ctxt->dst.type = OP_REG;
2636 ctxt->dst.bytes = ctxt->src.bytes;
2637 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2638 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002639
2640 return X86EMUL_CONTINUE;
2641}
2642
Avi Kivity48bb5d32010-08-18 18:54:34 +03002643static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2644{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002645 u64 tsc = 0;
2646
Avi Kivity717746e2011-04-20 13:37:53 +03002647 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002648 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2649 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002650 return X86EMUL_CONTINUE;
2651}
2652
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002653static int em_mov(struct x86_emulate_ctxt *ctxt)
2654{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002655 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002656 return X86EMUL_CONTINUE;
2657}
2658
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09002659static int em_cr_write(struct x86_emulate_ctxt *ctxt)
2660{
2661 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
2662 return emulate_gp(ctxt, 0);
2663
2664 /* Disable writeback. */
2665 ctxt->dst.type = OP_NONE;
2666 return X86EMUL_CONTINUE;
2667}
2668
2669static int em_dr_write(struct x86_emulate_ctxt *ctxt)
2670{
2671 unsigned long val;
2672
2673 if (ctxt->mode == X86EMUL_MODE_PROT64)
2674 val = ctxt->src.val & ~0ULL;
2675 else
2676 val = ctxt->src.val & ~0U;
2677
2678 /* #UD condition is already handled. */
2679 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
2680 return emulate_gp(ctxt, 0);
2681
2682 /* Disable writeback. */
2683 ctxt->dst.type = OP_NONE;
2684 return X86EMUL_CONTINUE;
2685}
2686
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09002687static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
2688{
2689 u64 msr_data;
2690
2691 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
2692 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
2693 if (ctxt->ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data))
2694 return emulate_gp(ctxt, 0);
2695
2696 return X86EMUL_CONTINUE;
2697}
2698
2699static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
2700{
2701 u64 msr_data;
2702
2703 if (ctxt->ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data))
2704 return emulate_gp(ctxt, 0);
2705
2706 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
2707 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
2708 return X86EMUL_CONTINUE;
2709}
2710
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002711static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2712{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002713 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002714 return emulate_ud(ctxt);
2715
Avi Kivity9dac77f2011-06-01 15:34:25 +03002716 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002717 return X86EMUL_CONTINUE;
2718}
2719
2720static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2721{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002722 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002723
Avi Kivity9dac77f2011-06-01 15:34:25 +03002724 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002725 return emulate_ud(ctxt);
2726
Avi Kivity9dac77f2011-06-01 15:34:25 +03002727 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002728 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2729
2730 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002731 ctxt->dst.type = OP_NONE;
2732 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002733}
2734
Avi Kivityaa97bb42010-01-20 18:09:23 +02002735static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2736{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002737 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002738 return X86EMUL_CONTINUE;
2739}
2740
Avi Kivity38503912011-03-31 18:48:09 +02002741static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2742{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002743 int rc;
2744 ulong linear;
2745
Avi Kivity9dac77f2011-06-01 15:34:25 +03002746 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002747 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002748 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002749 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002750 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002751 return X86EMUL_CONTINUE;
2752}
2753
Avi Kivity2d04a052011-04-20 15:32:49 +03002754static int em_clts(struct x86_emulate_ctxt *ctxt)
2755{
2756 ulong cr0;
2757
2758 cr0 = ctxt->ops->get_cr(ctxt, 0);
2759 cr0 &= ~X86_CR0_TS;
2760 ctxt->ops->set_cr(ctxt, 0, cr0);
2761 return X86EMUL_CONTINUE;
2762}
2763
Avi Kivity26d05cc2011-04-21 12:07:59 +03002764static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2765{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002766 int rc;
2767
Avi Kivity9dac77f2011-06-01 15:34:25 +03002768 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002769 return X86EMUL_UNHANDLEABLE;
2770
2771 rc = ctxt->ops->fix_hypercall(ctxt);
2772 if (rc != X86EMUL_CONTINUE)
2773 return rc;
2774
2775 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002776 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002777 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002778 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002779 return X86EMUL_CONTINUE;
2780}
2781
2782static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2783{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002784 struct desc_ptr desc_ptr;
2785 int rc;
2786
Avi Kivity9dac77f2011-06-01 15:34:25 +03002787 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002788 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002789 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002790 if (rc != X86EMUL_CONTINUE)
2791 return rc;
2792 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2793 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002794 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002795 return X86EMUL_CONTINUE;
2796}
2797
Avi Kivity5ef39c72011-04-21 12:21:50 +03002798static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002799{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002800 int rc;
2801
Avi Kivity5ef39c72011-04-21 12:21:50 +03002802 rc = ctxt->ops->fix_hypercall(ctxt);
2803
Avi Kivity26d05cc2011-04-21 12:07:59 +03002804 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002805 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002806 return rc;
2807}
2808
2809static int em_lidt(struct x86_emulate_ctxt *ctxt)
2810{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002811 struct desc_ptr desc_ptr;
2812 int rc;
2813
Avi Kivity9dac77f2011-06-01 15:34:25 +03002814 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002815 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002816 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002817 if (rc != X86EMUL_CONTINUE)
2818 return rc;
2819 ctxt->ops->set_idt(ctxt, &desc_ptr);
2820 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002821 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002822 return X86EMUL_CONTINUE;
2823}
2824
2825static int em_smsw(struct x86_emulate_ctxt *ctxt)
2826{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002827 ctxt->dst.bytes = 2;
2828 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002829 return X86EMUL_CONTINUE;
2830}
2831
2832static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2833{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002834 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002835 | (ctxt->src.val & 0x0f));
2836 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002837 return X86EMUL_CONTINUE;
2838}
2839
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002840static int em_loop(struct x86_emulate_ctxt *ctxt)
2841{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002842 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2843 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2844 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2845 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002846
2847 return X86EMUL_CONTINUE;
2848}
2849
2850static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2851{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002852 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2853 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002854
2855 return X86EMUL_CONTINUE;
2856}
2857
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09002858static int em_in(struct x86_emulate_ctxt *ctxt)
2859{
2860 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
2861 &ctxt->dst.val))
2862 return X86EMUL_IO_NEEDED;
2863
2864 return X86EMUL_CONTINUE;
2865}
2866
2867static int em_out(struct x86_emulate_ctxt *ctxt)
2868{
2869 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
2870 &ctxt->src.val, 1);
2871 /* Disable writeback. */
2872 ctxt->dst.type = OP_NONE;
2873 return X86EMUL_CONTINUE;
2874}
2875
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002876static int em_cli(struct x86_emulate_ctxt *ctxt)
2877{
2878 if (emulator_bad_iopl(ctxt))
2879 return emulate_gp(ctxt, 0);
2880
2881 ctxt->eflags &= ~X86_EFLAGS_IF;
2882 return X86EMUL_CONTINUE;
2883}
2884
2885static int em_sti(struct x86_emulate_ctxt *ctxt)
2886{
2887 if (emulator_bad_iopl(ctxt))
2888 return emulate_gp(ctxt, 0);
2889
2890 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2891 ctxt->eflags |= X86_EFLAGS_IF;
2892 return X86EMUL_CONTINUE;
2893}
2894
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09002895static int em_bt(struct x86_emulate_ctxt *ctxt)
2896{
2897 /* Disable writeback. */
2898 ctxt->dst.type = OP_NONE;
2899 /* only subword offset */
2900 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
2901
2902 emulate_2op_SrcV_nobyte(ctxt, "bt");
2903 return X86EMUL_CONTINUE;
2904}
2905
2906static int em_bts(struct x86_emulate_ctxt *ctxt)
2907{
2908 emulate_2op_SrcV_nobyte(ctxt, "bts");
2909 return X86EMUL_CONTINUE;
2910}
2911
2912static int em_btr(struct x86_emulate_ctxt *ctxt)
2913{
2914 emulate_2op_SrcV_nobyte(ctxt, "btr");
2915 return X86EMUL_CONTINUE;
2916}
2917
2918static int em_btc(struct x86_emulate_ctxt *ctxt)
2919{
2920 emulate_2op_SrcV_nobyte(ctxt, "btc");
2921 return X86EMUL_CONTINUE;
2922}
2923
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002924static bool valid_cr(int nr)
2925{
2926 switch (nr) {
2927 case 0:
2928 case 2 ... 4:
2929 case 8:
2930 return true;
2931 default:
2932 return false;
2933 }
2934}
2935
2936static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2937{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002938 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002939 return emulate_ud(ctxt);
2940
2941 return X86EMUL_CONTINUE;
2942}
2943
2944static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2945{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 u64 new_val = ctxt->src.val64;
2947 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002948 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002949
2950 static u64 cr_reserved_bits[] = {
2951 0xffffffff00000000ULL,
2952 0, 0, 0, /* CR3 checked later */
2953 CR4_RESERVED_BITS,
2954 0, 0, 0,
2955 CR8_RESERVED_BITS,
2956 };
2957
2958 if (!valid_cr(cr))
2959 return emulate_ud(ctxt);
2960
2961 if (new_val & cr_reserved_bits[cr])
2962 return emulate_gp(ctxt, 0);
2963
2964 switch (cr) {
2965 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002966 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002967 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2968 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2969 return emulate_gp(ctxt, 0);
2970
Avi Kivity717746e2011-04-20 13:37:53 +03002971 cr4 = ctxt->ops->get_cr(ctxt, 4);
2972 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002973
2974 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2975 !(cr4 & X86_CR4_PAE))
2976 return emulate_gp(ctxt, 0);
2977
2978 break;
2979 }
2980 case 3: {
2981 u64 rsvd = 0;
2982
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002983 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2984 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002985 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002986 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002987 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002988 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002989 rsvd = CR3_NONPAE_RESERVED_BITS;
2990
2991 if (new_val & rsvd)
2992 return emulate_gp(ctxt, 0);
2993
2994 break;
2995 }
2996 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002997 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002998
Avi Kivity717746e2011-04-20 13:37:53 +03002999 cr4 = ctxt->ops->get_cr(ctxt, 4);
3000 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003001
3002 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3003 return emulate_gp(ctxt, 0);
3004
3005 break;
3006 }
3007 }
3008
3009 return X86EMUL_CONTINUE;
3010}
3011
Joerg Roedel3b88e412011-04-04 12:39:29 +02003012static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3013{
3014 unsigned long dr7;
3015
Avi Kivity717746e2011-04-20 13:37:53 +03003016 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003017
3018 /* Check if DR7.Global_Enable is set */
3019 return dr7 & (1 << 13);
3020}
3021
3022static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3023{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003024 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003025 u64 cr4;
3026
3027 if (dr > 7)
3028 return emulate_ud(ctxt);
3029
Avi Kivity717746e2011-04-20 13:37:53 +03003030 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003031 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3032 return emulate_ud(ctxt);
3033
3034 if (check_dr7_gd(ctxt))
3035 return emulate_db(ctxt);
3036
3037 return X86EMUL_CONTINUE;
3038}
3039
3040static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3041{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003042 u64 new_val = ctxt->src.val64;
3043 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003044
3045 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3046 return emulate_gp(ctxt, 0);
3047
3048 return check_dr_read(ctxt);
3049}
3050
Joerg Roedel01de8b02011-04-04 12:39:31 +02003051static int check_svme(struct x86_emulate_ctxt *ctxt)
3052{
3053 u64 efer;
3054
Avi Kivity717746e2011-04-20 13:37:53 +03003055 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003056
3057 if (!(efer & EFER_SVME))
3058 return emulate_ud(ctxt);
3059
3060 return X86EMUL_CONTINUE;
3061}
3062
3063static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3064{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003065 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003066
3067 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003068 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003069 return emulate_gp(ctxt, 0);
3070
3071 return check_svme(ctxt);
3072}
3073
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003074static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3075{
Avi Kivity717746e2011-04-20 13:37:53 +03003076 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003077
Avi Kivity717746e2011-04-20 13:37:53 +03003078 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003079 return emulate_ud(ctxt);
3080
3081 return X86EMUL_CONTINUE;
3082}
3083
Joerg Roedel80612522011-04-04 12:39:33 +02003084static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3085{
Avi Kivity717746e2011-04-20 13:37:53 +03003086 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003087 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02003088
Avi Kivity717746e2011-04-20 13:37:53 +03003089 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003090 (rcx > 3))
3091 return emulate_gp(ctxt, 0);
3092
3093 return X86EMUL_CONTINUE;
3094}
3095
Joerg Roedelf6511932011-04-04 12:39:35 +02003096static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3097{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003098 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3099 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003100 return emulate_gp(ctxt, 0);
3101
3102 return X86EMUL_CONTINUE;
3103}
3104
3105static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3106{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003107 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3108 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003109 return emulate_gp(ctxt, 0);
3110
3111 return X86EMUL_CONTINUE;
3112}
3113
Avi Kivity73fba5f2010-07-29 15:11:53 +03003114#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003115#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003116#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3117 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003118#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003119#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003120#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03003121#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003122#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003123#define II(_f, _e, _i) \
3124 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003125#define IIP(_f, _e, _i, _p) \
3126 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3127 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003128#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003129
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003130#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003131#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003132#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003133#define I2bvIP(_f, _e, _i, _p) \
3134 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003135
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003136#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3137 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3138 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003139
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003140static struct opcode group7_rm1[] = {
3141 DI(SrcNone | ModRM | Priv, monitor),
3142 DI(SrcNone | ModRM | Priv, mwait),
3143 N, N, N, N, N, N,
3144};
3145
Joerg Roedel01de8b02011-04-04 12:39:31 +02003146static struct opcode group7_rm3[] = {
3147 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003148 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003149 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3150 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3151 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3152 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3153 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3154 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3155};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003156
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003157static struct opcode group7_rm7[] = {
3158 N,
3159 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3160 N, N, N, N, N, N,
3161};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003162
Avi Kivity73fba5f2010-07-29 15:11:53 +03003163static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003164 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003165 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003166 I(Lock, em_adc),
3167 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003168 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003169 I(Lock, em_sub),
3170 I(Lock, em_xor),
3171 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003172};
3173
3174static struct opcode group1A[] = {
3175 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3176};
3177
3178static struct opcode group3[] = {
Avi Kivity3329ece2011-09-13 10:45:39 +03003179 I(DstMem | SrcImm | ModRM, em_test),
3180 I(DstMem | SrcImm | ModRM, em_test),
3181 I(DstMem | SrcNone | ModRM | Lock, em_not),
3182 I(DstMem | SrcNone | ModRM | Lock, em_neg),
3183 I(SrcMem | ModRM, em_mul_ex),
3184 I(SrcMem | ModRM, em_imul_ex),
3185 I(SrcMem | ModRM, em_div_ex),
3186 I(SrcMem | ModRM, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003187};
3188
3189static struct opcode group4[] = {
3190 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3191 N, N, N, N, N, N,
3192};
3193
3194static struct opcode group5[] = {
3195 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003196 D(SrcMem | ModRM | Stack),
3197 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003198 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3199 D(SrcMem | ModRM | Stack), N,
3200};
3201
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003202static struct opcode group6[] = {
3203 DI(ModRM | Prot, sldt),
3204 DI(ModRM | Prot, str),
3205 DI(ModRM | Prot | Priv, lldt),
3206 DI(ModRM | Prot | Priv, ltr),
3207 N, N, N, N,
3208};
3209
Avi Kivity73fba5f2010-07-29 15:11:53 +03003210static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003211 DI(ModRM | Mov | DstMem | Priv, sgdt),
3212 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003213 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3214 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3215 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3216 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3217 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003218}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003219 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3220 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003221 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003222 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3223 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003224} };
3225
3226static struct opcode group8[] = {
3227 N, N, N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003228 I(DstMem | SrcImmByte | ModRM, em_bt),
3229 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_bts),
3230 I(DstMem | SrcImmByte | ModRM | Lock, em_btr),
3231 I(DstMem | SrcImmByte | ModRM | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003232};
3233
3234static struct group_dual group9 = { {
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003235 N, D(DstMem64 | ModRM | Lock | PageTable), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003236}, {
3237 N, N, N, N, N, N, N, N,
3238} };
3239
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003240static struct opcode group11[] = {
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003241 I(DstMem | SrcImm | ModRM | Mov | PageTable, em_mov),
3242 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003243};
3244
Avi Kivityaa97bb42010-01-20 18:09:23 +02003245static struct gprefix pfx_0f_6f_0f_7f = {
3246 N, N, N, I(Sse, em_movdqu),
3247};
3248
Avi Kivity73fba5f2010-07-29 15:11:53 +03003249static struct opcode opcode_table[256] = {
3250 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003251 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003252 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3253 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003254 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003255 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003256 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3257 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003258 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003259 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003260 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3261 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003262 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003263 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003264 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3265 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003266 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003267 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003268 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003269 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003270 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003271 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003272 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003273 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003274 /* 0x40 - 0x4F */
3275 X16(D(DstReg)),
3276 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003277 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003278 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003279 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003280 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003281 I(ImplicitOps | Stack | No64, em_pusha),
3282 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003283 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3284 N, N, N, N,
3285 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003286 I(SrcImm | Mov | Stack, em_push),
3287 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003288 I(SrcImmByte | Mov | Stack, em_push),
3289 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003290 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3291 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003292 /* 0x70 - 0x7F */
3293 X16(D(SrcImmByte)),
3294 /* 0x80 - 0x87 */
3295 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3296 G(DstMem | SrcImm | ModRM | Group, group1),
3297 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3298 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003299 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003300 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003301 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003302 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003303 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003304 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003305 D(ModRM | SrcMem | NoAccess | DstReg),
3306 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3307 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003308 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003309 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003310 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003311 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003312 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003313 II(ImplicitOps | Stack, em_pushf, pushf),
3314 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003315 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003316 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003317 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003318 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003319 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003320 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003321 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003322 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3323 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003324 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003325 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003326 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003327 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003328 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003329 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003330 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003331 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003332 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003333 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3334 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003335 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003336 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003337 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003338 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003339 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003340 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003341 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003342 N, N, N, N,
3343 /* 0xD8 - 0xDF */
3344 N, N, N, N, N, N, N, N,
3345 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003346 X3(I(SrcImmByte, em_loop)),
3347 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003348 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3349 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003350 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003351 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003352 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003353 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3354 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003355 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003356 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003357 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3358 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003359 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003360 D(ImplicitOps), D(ImplicitOps),
3361 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003362 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3363};
3364
3365static struct opcode twobyte_table[256] = {
3366 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003367 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003368 N, I(ImplicitOps | VendorSpecific, em_syscall),
3369 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003370 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003371 N, D(ImplicitOps | ModRM), N, N,
3372 /* 0x10 - 0x1F */
3373 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3374 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003375 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003376 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003377 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3378 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003379 N, N, N, N,
3380 N, N, N, N, N, N, N, N,
3381 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003382 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003383 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003384 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003385 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003386 I(ImplicitOps | VendorSpecific, em_sysenter),
3387 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003388 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003389 N, N, N, N, N, N, N, N,
3390 /* 0x40 - 0x4F */
3391 X16(D(DstReg | SrcMem | ModRM | Mov)),
3392 /* 0x50 - 0x5F */
3393 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3394 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003395 N, N, N, N,
3396 N, N, N, N,
3397 N, N, N, N,
3398 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003399 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003400 N, N, N, N,
3401 N, N, N, N,
3402 N, N, N, N,
3403 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003404 /* 0x80 - 0x8F */
3405 X16(D(SrcImm)),
3406 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003407 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003408 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003409 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003410 DI(ImplicitOps, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003411 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3412 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3413 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03003414 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003415 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003416 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003417 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3418 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003419 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003420 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09003421 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03003422 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003423 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03003424 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
3425 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003426 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003427 /* 0xB8 - 0xBF */
3428 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003429 G(BitOp, group8),
3430 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Wei Yongjund9574a22010-08-10 13:48:22 +08003431 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3432 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003433 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003434 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003435 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003436 N, N, N, GD(0, &group9),
3437 N, N, N, N, N, N, N, N,
3438 /* 0xD0 - 0xDF */
3439 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3440 /* 0xE0 - 0xEF */
3441 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3442 /* 0xF0 - 0xFF */
3443 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3444};
3445
3446#undef D
3447#undef N
3448#undef G
3449#undef GD
3450#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003451#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003452#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003453
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003454#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003455#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003456#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003457#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003458#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003459
Avi Kivity9dac77f2011-06-01 15:34:25 +03003460static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003461{
3462 unsigned size;
3463
Avi Kivity9dac77f2011-06-01 15:34:25 +03003464 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003465 if (size == 8)
3466 size = 4;
3467 return size;
3468}
3469
3470static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3471 unsigned size, bool sign_extension)
3472{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003473 int rc = X86EMUL_CONTINUE;
3474
3475 op->type = OP_IMM;
3476 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003477 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003478 /* NB. Immediates are sign-extended as necessary. */
3479 switch (op->bytes) {
3480 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003481 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003482 break;
3483 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003484 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003485 break;
3486 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003487 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003488 break;
3489 }
3490 if (!sign_extension) {
3491 switch (op->bytes) {
3492 case 1:
3493 op->val &= 0xff;
3494 break;
3495 case 2:
3496 op->val &= 0xffff;
3497 break;
3498 case 4:
3499 op->val &= 0xffffffff;
3500 break;
3501 }
3502 }
3503done:
3504 return rc;
3505}
3506
Avi Kivitya99455492011-09-13 10:45:41 +03003507static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
3508 unsigned d)
3509{
3510 int rc = X86EMUL_CONTINUE;
3511
3512 switch (d) {
3513 case OpReg:
3514 decode_register_operand(ctxt, op,
Avi Kivity52179732011-09-13 10:45:46 +03003515 op == &ctxt->dst &&
Avi Kivitya99455492011-09-13 10:45:41 +03003516 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
3517 break;
3518 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03003519 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03003520 break;
3521 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03003522 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03003523 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03003524 *op = ctxt->memop;
3525 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03003526 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03003527 fetch_bit_operand(ctxt);
3528 op->orig_val = op->val;
3529 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03003530 case OpMem64:
3531 ctxt->memop.bytes = 8;
3532 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03003533 case OpAcc:
3534 op->type = OP_REG;
3535 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3536 op->addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3537 fetch_register_operand(op);
3538 op->orig_val = op->val;
3539 break;
3540 case OpDI:
3541 op->type = OP_MEM;
3542 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3543 op->addr.mem.ea =
3544 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3545 op->addr.mem.seg = VCPU_SREG_ES;
3546 op->val = 0;
3547 break;
3548 case OpDX:
3549 op->type = OP_REG;
3550 op->bytes = 2;
3551 op->addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3552 fetch_register_operand(op);
3553 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03003554 case OpCL:
3555 op->bytes = 1;
3556 op->val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
3557 break;
3558 case OpImmByte:
3559 rc = decode_imm(ctxt, op, 1, true);
3560 break;
3561 case OpOne:
3562 op->bytes = 1;
3563 op->val = 1;
3564 break;
3565 case OpImm:
3566 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
3567 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03003568 case OpMem16:
3569 ctxt->memop.bytes = 2;
3570 goto mem_common;
3571 case OpMem32:
3572 ctxt->memop.bytes = 4;
3573 goto mem_common;
3574 case OpImmU16:
3575 rc = decode_imm(ctxt, op, 2, false);
3576 break;
3577 case OpImmU:
3578 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
3579 break;
3580 case OpSI:
3581 op->type = OP_MEM;
3582 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3583 op->addr.mem.ea =
3584 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3585 op->addr.mem.seg = seg_override(ctxt);
3586 op->val = 0;
3587 break;
3588 case OpImmFAddr:
3589 op->type = OP_IMM;
3590 op->addr.mem.ea = ctxt->_eip;
3591 op->bytes = ctxt->op_bytes + 2;
3592 insn_fetch_arr(op->valptr, op->bytes, ctxt);
3593 break;
3594 case OpMemFAddr:
3595 ctxt->memop.bytes = ctxt->op_bytes + 2;
3596 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03003597 case OpES:
3598 op->val = VCPU_SREG_ES;
3599 break;
3600 case OpCS:
3601 op->val = VCPU_SREG_CS;
3602 break;
3603 case OpSS:
3604 op->val = VCPU_SREG_SS;
3605 break;
3606 case OpDS:
3607 op->val = VCPU_SREG_DS;
3608 break;
3609 case OpFS:
3610 op->val = VCPU_SREG_FS;
3611 break;
3612 case OpGS:
3613 op->val = VCPU_SREG_GS;
3614 break;
Avi Kivitya99455492011-09-13 10:45:41 +03003615 case OpImplicit:
3616 /* Special instructions do their own operand decoding. */
3617 default:
3618 op->type = OP_NONE; /* Disable writeback. */
3619 break;
3620 }
3621
3622done:
3623 return rc;
3624}
3625
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003626int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003627{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003628 int rc = X86EMUL_CONTINUE;
3629 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003630 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003631 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003632 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003633
Avi Kivityf09ed832011-09-13 10:45:40 +03003634 ctxt->memop.type = OP_NONE;
3635 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003636 ctxt->_eip = ctxt->eip;
3637 ctxt->fetch.start = ctxt->_eip;
3638 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003639 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003640 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003641
3642 switch (mode) {
3643 case X86EMUL_MODE_REAL:
3644 case X86EMUL_MODE_VM86:
3645 case X86EMUL_MODE_PROT16:
3646 def_op_bytes = def_ad_bytes = 2;
3647 break;
3648 case X86EMUL_MODE_PROT32:
3649 def_op_bytes = def_ad_bytes = 4;
3650 break;
3651#ifdef CONFIG_X86_64
3652 case X86EMUL_MODE_PROT64:
3653 def_op_bytes = 4;
3654 def_ad_bytes = 8;
3655 break;
3656#endif
3657 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003658 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003659 }
3660
Avi Kivity9dac77f2011-06-01 15:34:25 +03003661 ctxt->op_bytes = def_op_bytes;
3662 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003663
3664 /* Legacy prefixes. */
3665 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003666 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003667 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003668 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003669 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003670 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003671 break;
3672 case 0x67: /* address-size override */
3673 if (mode == X86EMUL_MODE_PROT64)
3674 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003675 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003676 else
3677 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003678 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003679 break;
3680 case 0x26: /* ES override */
3681 case 0x2e: /* CS override */
3682 case 0x36: /* SS override */
3683 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003684 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003685 break;
3686 case 0x64: /* FS override */
3687 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003688 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003689 break;
3690 case 0x40 ... 0x4f: /* REX */
3691 if (mode != X86EMUL_MODE_PROT64)
3692 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003693 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003694 continue;
3695 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003696 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003697 break;
3698 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003699 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003700 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003701 break;
3702 default:
3703 goto done_prefixes;
3704 }
3705
3706 /* Any legacy prefix after a REX prefix nullifies its effect. */
3707
Avi Kivity9dac77f2011-06-01 15:34:25 +03003708 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003709 }
3710
3711done_prefixes:
3712
3713 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003714 if (ctxt->rex_prefix & 8)
3715 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003716
3717 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003718 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003719 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003720 if (ctxt->b == 0x0f) {
3721 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003722 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003723 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003724 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003725 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003726
Avi Kivity9dac77f2011-06-01 15:34:25 +03003727 while (ctxt->d & GroupMask) {
3728 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003729 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003730 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003731 --ctxt->_eip;
3732 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003733 opcode = opcode.u.group[goffset];
3734 break;
3735 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003736 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003737 --ctxt->_eip;
3738 goffset = (ctxt->modrm >> 3) & 7;
3739 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003740 opcode = opcode.u.gdual->mod3[goffset];
3741 else
3742 opcode = opcode.u.gdual->mod012[goffset];
3743 break;
3744 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003745 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003746 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003747 break;
3748 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003749 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003750 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003751 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003752 switch (simd_prefix) {
3753 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3754 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3755 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3756 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3757 }
3758 break;
3759 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003760 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003761 }
Avi Kivity46561642011-04-24 14:09:59 +03003762
Avi Kivityb1ea50b2011-09-13 10:45:42 +03003763 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003764 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003765 }
3766
Avi Kivity9dac77f2011-06-01 15:34:25 +03003767 ctxt->execute = opcode.u.execute;
3768 ctxt->check_perm = opcode.check_perm;
3769 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003770
3771 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003772 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003773 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003774
Avi Kivity9dac77f2011-06-01 15:34:25 +03003775 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003776 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003777
Avi Kivity9dac77f2011-06-01 15:34:25 +03003778 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3779 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003780
Avi Kivity9dac77f2011-06-01 15:34:25 +03003781 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003782 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003783 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003784 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003785 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003786 }
3787
Avi Kivity9dac77f2011-06-01 15:34:25 +03003788 if (ctxt->d & Sse)
3789 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003790
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003791 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003792 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03003793 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003794 if (!ctxt->has_seg_override)
3795 set_seg_override(ctxt, ctxt->modrm_seg);
3796 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03003797 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003798 if (rc != X86EMUL_CONTINUE)
3799 goto done;
3800
Avi Kivity9dac77f2011-06-01 15:34:25 +03003801 if (!ctxt->has_seg_override)
3802 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003803
Avi Kivityf09ed832011-09-13 10:45:40 +03003804 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003805
Avi Kivityf09ed832011-09-13 10:45:40 +03003806 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
3807 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003808
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003809 /*
3810 * Decode and fetch the source operand: register, memory
3811 * or immediate.
3812 */
Avi Kivity0fe59122011-09-13 10:45:47 +03003813 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003814 if (rc != X86EMUL_CONTINUE)
3815 goto done;
3816
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003817 /*
3818 * Decode and fetch the second source operand: register, memory
3819 * or immediate.
3820 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03003821 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003822 if (rc != X86EMUL_CONTINUE)
3823 goto done;
3824
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003825 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03003826 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003827
3828done:
Avi Kivityf09ed832011-09-13 10:45:40 +03003829 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
3830 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003831
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003832 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003833}
3834
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08003835bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
3836{
3837 return ctxt->d & PageTable;
3838}
3839
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003840static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3841{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003842 /* The second termination condition only applies for REPE
3843 * and REPNE. Test if the repeat string operation prefix is
3844 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3845 * corresponding termination condition according to:
3846 * - if REPE/REPZ and ZF = 0 then done
3847 * - if REPNE/REPNZ and ZF = 1 then done
3848 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003849 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3850 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3851 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003852 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003853 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003854 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3855 return true;
3856
3857 return false;
3858}
3859
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003860int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003861{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003862 struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003863 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003864 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003865
Avi Kivity9dac77f2011-06-01 15:34:25 +03003866 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003867
Avi Kivity9dac77f2011-06-01 15:34:25 +03003868 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003869 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003870 goto done;
3871 }
3872
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003873 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003874 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003875 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003876 goto done;
3877 }
3878
Avi Kivity9dac77f2011-06-01 15:34:25 +03003879 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003880 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003881 goto done;
3882 }
3883
Avi Kivity9dac77f2011-06-01 15:34:25 +03003884 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003885 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3886 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003887 rc = emulate_ud(ctxt);
3888 goto done;
3889 }
3890
Avi Kivity9dac77f2011-06-01 15:34:25 +03003891 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003892 rc = emulate_nm(ctxt);
3893 goto done;
3894 }
3895
Avi Kivity9dac77f2011-06-01 15:34:25 +03003896 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3897 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003898 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003899 if (rc != X86EMUL_CONTINUE)
3900 goto done;
3901 }
3902
Gleb Natapove92805a2010-02-10 14:21:35 +02003903 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003904 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003905 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003906 goto done;
3907 }
3908
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003909 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003910 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003911 rc = emulate_ud(ctxt);
3912 goto done;
3913 }
3914
Joerg Roedeld09beab2011-04-04 12:39:25 +02003915 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003916 if (ctxt->check_perm) {
3917 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003918 if (rc != X86EMUL_CONTINUE)
3919 goto done;
3920 }
3921
Avi Kivity9dac77f2011-06-01 15:34:25 +03003922 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3923 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003924 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003925 if (rc != X86EMUL_CONTINUE)
3926 goto done;
3927 }
3928
Avi Kivity9dac77f2011-06-01 15:34:25 +03003929 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003930 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003931 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3932 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003933 goto done;
3934 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003935 }
3936
Avi Kivity9dac77f2011-06-01 15:34:25 +03003937 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3938 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3939 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003940 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003941 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003942 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003943 }
3944
Avi Kivity9dac77f2011-06-01 15:34:25 +03003945 if (ctxt->src2.type == OP_MEM) {
3946 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3947 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003948 if (rc != X86EMUL_CONTINUE)
3949 goto done;
3950 }
3951
Avi Kivity9dac77f2011-06-01 15:34:25 +03003952 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003953 goto special_insn;
3954
3955
Avi Kivity9dac77f2011-06-01 15:34:25 +03003956 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003957 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003958 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3959 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003960 if (rc != X86EMUL_CONTINUE)
3961 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003962 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003963 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003964
Avi Kivity018a98d2007-11-27 19:30:56 +02003965special_insn:
3966
Avi Kivity9dac77f2011-06-01 15:34:25 +03003967 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3968 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003969 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003970 if (rc != X86EMUL_CONTINUE)
3971 goto done;
3972 }
3973
Avi Kivity9dac77f2011-06-01 15:34:25 +03003974 if (ctxt->execute) {
3975 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003976 if (rc != X86EMUL_CONTINUE)
3977 goto done;
3978 goto writeback;
3979 }
3980
Avi Kivity9dac77f2011-06-01 15:34:25 +03003981 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982 goto twobyte_insn;
3983
Avi Kivity9dac77f2011-06-01 15:34:25 +03003984 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02003985 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003986 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02003987 break;
3988 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03003989 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02003990 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003991 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003992 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003994 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003996 case 0x6c: /* insb */
3997 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003998 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003999 rc = em_in(ctxt);
4000 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004001 case 0x6e: /* outsb */
4002 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004003 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004004 rc = em_out(ctxt);
Gleb Natapov79729952010-03-18 15:20:24 +02004005 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004006 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004007 if (test_cc(ctxt->b, ctxt->eflags))
4008 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004009 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004010 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004011 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004012 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004014 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004015 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004016 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004017 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03004018 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004019 rc = em_xchg(ctxt);
4020 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004021 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004022 switch (ctxt->op_bytes) {
4023 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4024 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4025 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004026 }
4027 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004028 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004029 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004030 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004031 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004032 rc = emulate_int(ctxt, 3);
4033 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004034 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004035 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004036 break;
4037 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004038 if (ctxt->eflags & EFLG_OF)
4039 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004040 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004041 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004042 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004043 break;
4044 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004045 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004046 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004047 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004048 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004049 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004050 jmp_rel(ctxt, ctxt->src.val);
4051 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004052 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004053 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004054 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004055 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004056 case 0xf5: /* cmc */
4057 /* complement carry flag from eflags reg */
4058 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004059 break;
4060 case 0xf8: /* clc */
4061 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004062 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004063 case 0xf9: /* stc */
4064 ctxt->eflags |= EFLG_CF;
4065 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004066 case 0xfc: /* cld */
4067 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004068 break;
4069 case 0xfd: /* std */
4070 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004071 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02004072 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004073 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004074 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02004075 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004076 rc = em_grp45(ctxt);
4077 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004078 default:
4079 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004080 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004081
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004082 if (rc != X86EMUL_CONTINUE)
4083 goto done;
4084
Avi Kivity018a98d2007-11-27 19:30:56 +02004085writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004086 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004087 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004088 goto done;
4089
Gleb Natapov5cd21912010-03-18 15:20:26 +02004090 /*
4091 * restore dst type in case the decoding will be reused
4092 * (happens for string instruction )
4093 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004094 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004095
Avi Kivity9dac77f2011-06-01 15:34:25 +03004096 if ((ctxt->d & SrcMask) == SrcSI)
4097 string_addr_inc(ctxt, seg_override(ctxt),
4098 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004099
Avi Kivity9dac77f2011-06-01 15:34:25 +03004100 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004101 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004103
Avi Kivity9dac77f2011-06-01 15:34:25 +03004104 if (ctxt->rep_prefix && (ctxt->d & String)) {
4105 struct read_cache *r = &ctxt->io_read;
4106 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004107
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004108 if (!string_insn_completed(ctxt)) {
4109 /*
4110 * Re-enter guest when pio read ahead buffer is empty
4111 * or, if it is not used, after each 1024 iteration.
4112 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004113 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004114 (r->end == 0 || r->end != r->pos)) {
4115 /*
4116 * Reset read cache. Usually happens before
4117 * decode, but since instruction is restarted
4118 * we have to do it here.
4119 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004120 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004121 return EMULATION_RESTART;
4122 }
4123 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004124 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004125 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004126
Avi Kivity9dac77f2011-06-01 15:34:25 +03004127 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004128
4129done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004130 if (rc == X86EMUL_PROPAGATE_FAULT)
4131 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004132 if (rc == X86EMUL_INTERCEPTED)
4133 return EMULATION_INTERCEPTED;
4134
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004135 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136
4137twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004138 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004139 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004140 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004141 break;
4142 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004143 case 0x0d: /* GrpP (prefetch) */
4144 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004145 break;
4146 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004147 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004148 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004150 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004153 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4154 if (!test_cc(ctxt->b, ctxt->eflags))
4155 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004157 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004158 if (test_cc(ctxt->b, ctxt->eflags))
4159 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004160 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004161 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004162 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004163 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004164 case 0xa4: /* shld imm8, r, r/m */
4165 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004166 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004167 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004168 case 0xac: /* shrd imm8, r, r/m */
4169 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004170 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004171 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004172 case 0xae: /* clflush */
4173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175 ctxt->dst.bytes = ctxt->op_bytes;
4176 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4177 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004179 case 0xbc: { /* bsf */
4180 u8 zf;
4181 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004182 : "=r"(ctxt->dst.val), "=q"(zf)
4183 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004184 ctxt->eflags &= ~X86_EFLAGS_ZF;
4185 if (zf) {
4186 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004187 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004188 }
4189 break;
4190 }
4191 case 0xbd: { /* bsr */
4192 u8 zf;
4193 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004194 : "=r"(ctxt->dst.val), "=q"(zf)
4195 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004196 ctxt->eflags &= ~X86_EFLAGS_ZF;
4197 if (zf) {
4198 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004199 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004200 }
4201 break;
4202 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004204 ctxt->dst.bytes = ctxt->op_bytes;
4205 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4206 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004208 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004209 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004210 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004211 ctxt->src.val = ctxt->dst.orig_val;
4212 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004213 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004214 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004215 ctxt->dst.bytes = ctxt->op_bytes;
4216 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4217 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004218 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004220 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004221 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004222 default:
4223 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004224 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004225
4226 if (rc != X86EMUL_CONTINUE)
4227 goto done;
4228
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 goto writeback;
4230
4231cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004232 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004233}