blob: ed192d220201738b3dc4e1a6ab0404aae5a138b5 [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
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030049#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030051#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
54#define DstAcc (4<<1) /* Destination Accumulator */
55#define DstDI (5<<1) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080057#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Avi Kivityab85b12b2010-07-29 15:11:49 +030058#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080059/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020060#define SrcNone (0<<4) /* No source operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020061#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030074#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030075#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030077#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080078/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030079#define Mov (1<<9)
80#define BitOp (1<<10)
81#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020082#define String (1<<12) /* String instruction (rep capable) */
83#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020084#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
85#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030086/* Misc flags */
Avi Kivity5a506b12010-08-01 15:10:29 +030087#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030088#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030089#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020090#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020091#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030092#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010093/* Source 2 operand type */
94#define Src2None (0<<29)
95#define Src2CL (1<<29)
96#define Src2ImmByte (2<<29)
97#define Src2One (3<<29)
98#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080099
Avi Kivityd0e53322010-07-29 15:11:54 +0300100#define X2(x...) x, x
101#define X3(x...) X2(x), x
102#define X4(x...) X2(x), X2(x)
103#define X5(x...) X4(x), x
104#define X6(x...) X4(x), X2(x)
105#define X7(x...) X4(x), X3(x)
106#define X8(x...) X4(x), X4(x)
107#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300108
Avi Kivityd65b1de2010-07-29 15:11:35 +0300109struct opcode {
110 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300111 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300112 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300113 struct opcode *group;
114 struct group_dual *gdual;
115 } u;
116};
117
118struct group_dual {
119 struct opcode mod012[8];
120 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300121};
122
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200124#define EFLG_ID (1<<21)
125#define EFLG_VIP (1<<20)
126#define EFLG_VIF (1<<19)
127#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200128#define EFLG_VM (1<<17)
129#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200130#define EFLG_IOPL (3<<12)
131#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132#define EFLG_OF (1<<11)
133#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200134#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200135#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136#define EFLG_SF (1<<7)
137#define EFLG_ZF (1<<6)
138#define EFLG_AF (1<<4)
139#define EFLG_PF (1<<2)
140#define EFLG_CF (1<<0)
141
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300142#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
143#define EFLG_RESERVED_ONE_MASK 2
144
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145/*
146 * Instruction emulation:
147 * Most instructions are emulated directly via a fragment of inline assembly
148 * code. This allows us to save/restore EFLAGS and thus very easily pick up
149 * any modified flags.
150 */
151
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800152#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153#define _LO32 "k" /* force 32-bit operand */
154#define _STK "%%rsp" /* stack pointer */
155#elif defined(__i386__)
156#define _LO32 "" /* force 32-bit operand */
157#define _STK "%%esp" /* stack pointer */
158#endif
159
160/*
161 * These EFLAGS bits are restored from saved value during emulation, and
162 * any changes are written back to the saved value after emulation.
163 */
164#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
165
166/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200167#define _PRE_EFLAGS(_sav, _msk, _tmp) \
168 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
169 "movl %"_sav",%"_LO32 _tmp"; " \
170 "push %"_tmp"; " \
171 "push %"_tmp"; " \
172 "movl %"_msk",%"_LO32 _tmp"; " \
173 "andl %"_LO32 _tmp",("_STK"); " \
174 "pushf; " \
175 "notl %"_LO32 _tmp"; " \
176 "andl %"_LO32 _tmp",("_STK"); " \
177 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
178 "pop %"_tmp"; " \
179 "orl %"_LO32 _tmp",("_STK"); " \
180 "popf; " \
181 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182
183/* After executing instruction: write-back necessary bits in EFLAGS. */
184#define _POST_EFLAGS(_sav, _msk, _tmp) \
185 /* _sav |= EFLAGS & _msk; */ \
186 "pushf; " \
187 "pop %"_tmp"; " \
188 "andl %"_msk",%"_LO32 _tmp"; " \
189 "orl %"_LO32 _tmp",%"_sav"; "
190
Avi Kivitydda96d82008-11-26 15:14:10 +0200191#ifdef CONFIG_X86_64
192#define ON64(x) x
193#else
194#define ON64(x)
195#endif
196
Avi Kivityb3b3d252010-08-16 17:49:52 +0300197#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200198 do { \
199 __asm__ __volatile__ ( \
200 _PRE_EFLAGS("0", "4", "2") \
201 _op _suffix " %"_x"3,%1; " \
202 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300203 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200204 "=&r" (_tmp) \
205 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200206 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200207
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209/* Raw emulation: instruction has two explicit operands. */
210#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200211 do { \
212 unsigned long _tmp; \
213 \
214 switch ((_dst).bytes) { \
215 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300216 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200217 break; \
218 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300219 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200220 break; \
221 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300222 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200223 break; \
224 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 } while (0)
226
227#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
228 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200229 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400230 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300232 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 break; \
234 default: \
235 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
236 _wx, _wy, _lx, _ly, _qx, _qy); \
237 break; \
238 } \
239 } while (0)
240
241/* Source operand is byte-sized and may be restricted to just %cl. */
242#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
243 __emulate_2op(_op, _src, _dst, _eflags, \
244 "b", "c", "b", "c", "b", "c", "b", "c")
245
246/* Source operand is byte, word, long or quad sized. */
247#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
248 __emulate_2op(_op, _src, _dst, _eflags, \
249 "b", "q", "w", "r", _LO32, "r", "", "r")
250
251/* Source operand is word, long or quad sized. */
252#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
253 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
254 "w", "r", _LO32, "r", "", "r")
255
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100256/* Instruction has three operands and one operand is stored in ECX register */
257#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
258 do { \
259 unsigned long _tmp; \
260 _type _clv = (_cl).val; \
261 _type _srcv = (_src).val; \
262 _type _dstv = (_dst).val; \
263 \
264 __asm__ __volatile__ ( \
265 _PRE_EFLAGS("0", "5", "2") \
266 _op _suffix " %4,%1 \n" \
267 _POST_EFLAGS("0", "5", "2") \
268 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
269 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
270 ); \
271 \
272 (_cl).val = (unsigned long) _clv; \
273 (_src).val = (unsigned long) _srcv; \
274 (_dst).val = (unsigned long) _dstv; \
275 } while (0)
276
277#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
278 do { \
279 switch ((_dst).bytes) { \
280 case 2: \
281 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
282 "w", unsigned short); \
283 break; \
284 case 4: \
285 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
286 "l", unsigned int); \
287 break; \
288 case 8: \
289 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
290 "q", unsigned long)); \
291 break; \
292 } \
293 } while (0)
294
Avi Kivitydda96d82008-11-26 15:14:10 +0200295#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296 do { \
297 unsigned long _tmp; \
298 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200299 __asm__ __volatile__ ( \
300 _PRE_EFLAGS("0", "3", "2") \
301 _op _suffix " %1; " \
302 _POST_EFLAGS("0", "3", "2") \
303 : "=m" (_eflags), "+m" ((_dst).val), \
304 "=&r" (_tmp) \
305 : "i" (EFLAGS_MASK)); \
306 } while (0)
307
308/* Instruction has only one explicit operand (no source operand). */
309#define emulate_1op(_op, _dst, _eflags) \
310 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400311 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200312 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
313 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
314 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
315 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316 } \
317 } while (0)
318
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300319#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
320 do { \
321 unsigned long _tmp; \
322 \
323 __asm__ __volatile__ ( \
324 _PRE_EFLAGS("0", "4", "1") \
325 _op _suffix " %5; " \
326 _POST_EFLAGS("0", "4", "1") \
327 : "=m" (_eflags), "=&r" (_tmp), \
328 "+a" (_rax), "+d" (_rdx) \
329 : "i" (EFLAGS_MASK), "m" ((_src).val), \
330 "a" (_rax), "d" (_rdx)); \
331 } while (0)
332
333/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
334#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
335 do { \
336 switch((_src).bytes) { \
337 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
338 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
339 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
340 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
341 } \
342 } while (0)
343
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344/* Fetch next part of the instruction being emulated. */
345#define insn_fetch(_type, _size, _eip) \
346({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200347 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200348 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349 goto done; \
350 (_eip) += (_size); \
351 (_type)_x; \
352})
353
Gleb Natapov414e6272010-04-28 19:15:26 +0300354#define insn_fetch_arr(_arr, _size, _eip) \
355({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
356 if (rc != X86EMUL_CONTINUE) \
357 goto done; \
358 (_eip) += (_size); \
359})
360
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800361static inline unsigned long ad_mask(struct decode_cache *c)
362{
363 return (1UL << (c->ad_bytes << 3)) - 1;
364}
365
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800367static inline unsigned long
368address_mask(struct decode_cache *c, unsigned long reg)
369{
370 if (c->ad_bytes == sizeof(unsigned long))
371 return reg;
372 else
373 return reg & ad_mask(c);
374}
375
376static inline unsigned long
377register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
378{
379 return base + address_mask(c, reg);
380}
381
Harvey Harrison7a9572752008-02-19 07:40:41 -0800382static inline void
383register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
384{
385 if (c->ad_bytes == sizeof(unsigned long))
386 *reg += inc;
387 else
388 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
389}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390
Harvey Harrison7a9572752008-02-19 07:40:41 -0800391static inline void jmp_rel(struct decode_cache *c, int rel)
392{
393 register_address_increment(c, &c->eip, rel);
394}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300395
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300396static void set_seg_override(struct decode_cache *c, int seg)
397{
398 c->has_seg_override = true;
399 c->seg_override = seg;
400}
401
Gleb Natapov79168fd2010-04-28 19:15:30 +0300402static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
403 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300404{
405 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
406 return 0;
407
Gleb Natapov79168fd2010-04-28 19:15:30 +0300408 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300409}
410
411static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300412 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300413 struct decode_cache *c)
414{
415 if (!c->has_seg_override)
416 return 0;
417
Gleb Natapov79168fd2010-04-28 19:15:30 +0300418 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300419}
420
Gleb Natapov79168fd2010-04-28 19:15:30 +0300421static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
422 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300423{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300424 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300425}
426
Gleb Natapov79168fd2010-04-28 19:15:30 +0300427static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
428 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300429{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300430 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300431}
432
Gleb Natapov54b84862010-04-28 19:15:44 +0300433static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
434 u32 error, bool valid)
435{
436 ctxt->exception = vec;
437 ctxt->error_code = error;
438 ctxt->error_code_valid = valid;
439 ctxt->restart = false;
440}
441
442static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
443{
444 emulate_exception(ctxt, GP_VECTOR, err, true);
445}
446
447static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
448 int err)
449{
450 ctxt->cr2 = addr;
451 emulate_exception(ctxt, PF_VECTOR, err, true);
452}
453
454static void emulate_ud(struct x86_emulate_ctxt *ctxt)
455{
456 emulate_exception(ctxt, UD_VECTOR, 0, false);
457}
458
459static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
460{
461 emulate_exception(ctxt, TS_VECTOR, err, true);
462}
463
Avi Kivity62266862007-11-20 13:15:52 +0200464static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
465 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300466 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200467{
468 struct fetch_cache *fc = &ctxt->decode.fetch;
469 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300470 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200471
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300472 if (eip == fc->end) {
473 cur_size = fc->end - fc->start;
474 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
475 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
476 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900477 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200478 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300479 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200480 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300481 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900482 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200483}
484
485static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
486 struct x86_emulate_ops *ops,
487 unsigned long eip, void *dest, unsigned size)
488{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900489 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200490
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200491 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200492 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200493 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200494 while (size--) {
495 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900496 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200497 return rc;
498 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900499 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200500}
501
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000502/*
503 * Given the 'reg' portion of a ModRM byte, and a register block, return a
504 * pointer into the block that addresses the relevant register.
505 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
506 */
507static void *decode_register(u8 modrm_reg, unsigned long *regs,
508 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509{
510 void *p;
511
512 p = &regs[modrm_reg];
513 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
514 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
515 return p;
516}
517
518static int read_descriptor(struct x86_emulate_ctxt *ctxt,
519 struct x86_emulate_ops *ops,
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300520 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 u16 *size, unsigned long *address, int op_bytes)
522{
523 int rc;
524
525 if (op_bytes == 2)
526 op_bytes = 3;
527 *address = 0;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300528 rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900529 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 return rc;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300531 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532 return rc;
533}
534
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300535static int test_cc(unsigned int condition, unsigned int flags)
536{
537 int rc = 0;
538
539 switch ((condition & 15) >> 1) {
540 case 0: /* o */
541 rc |= (flags & EFLG_OF);
542 break;
543 case 1: /* b/c/nae */
544 rc |= (flags & EFLG_CF);
545 break;
546 case 2: /* z/e */
547 rc |= (flags & EFLG_ZF);
548 break;
549 case 3: /* be/na */
550 rc |= (flags & (EFLG_CF|EFLG_ZF));
551 break;
552 case 4: /* s */
553 rc |= (flags & EFLG_SF);
554 break;
555 case 5: /* p/pe */
556 rc |= (flags & EFLG_PF);
557 break;
558 case 7: /* le/ng */
559 rc |= (flags & EFLG_ZF);
560 /* fall through */
561 case 6: /* l/nge */
562 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
563 break;
564 }
565
566 /* Odd condition identifiers (lsb == 1) have inverted sense. */
567 return (!!rc ^ (condition & 1));
568}
569
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300570static void fetch_register_operand(struct operand *op)
571{
572 switch (op->bytes) {
573 case 1:
574 op->val = *(u8 *)op->addr.reg;
575 break;
576 case 2:
577 op->val = *(u16 *)op->addr.reg;
578 break;
579 case 4:
580 op->val = *(u32 *)op->addr.reg;
581 break;
582 case 8:
583 op->val = *(u64 *)op->addr.reg;
584 break;
585 }
586}
587
Avi Kivity3c118e22007-10-31 10:27:04 +0200588static void decode_register_operand(struct operand *op,
589 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200590 int inhibit_bytereg)
591{
Avi Kivity33615aa2007-10-31 11:15:56 +0200592 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200593 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200594
595 if (!(c->d & ModRM))
596 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200597 op->type = OP_REG;
598 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300599 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200600 op->bytes = 1;
601 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300602 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200603 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200604 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300605 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200606 op->orig_val = op->val;
607}
608
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200609static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300610 struct x86_emulate_ops *ops,
611 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200612{
613 struct decode_cache *c = &ctxt->decode;
614 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700615 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900616 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300617 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200618
619 if (c->rex_prefix) {
620 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
621 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
622 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
623 }
624
625 c->modrm = insn_fetch(u8, 1, c->eip);
626 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
627 c->modrm_reg |= (c->modrm & 0x38) >> 3;
628 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300629 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200630
631 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300632 op->type = OP_REG;
633 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
634 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300635 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300636 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200637 return rc;
638 }
639
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300640 op->type = OP_MEM;
641
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200642 if (c->ad_bytes == 2) {
643 unsigned bx = c->regs[VCPU_REGS_RBX];
644 unsigned bp = c->regs[VCPU_REGS_RBP];
645 unsigned si = c->regs[VCPU_REGS_RSI];
646 unsigned di = c->regs[VCPU_REGS_RDI];
647
648 /* 16-bit ModR/M decode. */
649 switch (c->modrm_mod) {
650 case 0:
651 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300652 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200653 break;
654 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300655 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200656 break;
657 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300658 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200659 break;
660 }
661 switch (c->modrm_rm) {
662 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300663 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200664 break;
665 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300666 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200667 break;
668 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300669 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200670 break;
671 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300672 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200673 break;
674 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300675 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200676 break;
677 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300678 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200679 break;
680 case 6:
681 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300682 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200683 break;
684 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300685 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200686 break;
687 }
688 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
689 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300690 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300691 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200692 } else {
693 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700694 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200695 sib = insn_fetch(u8, 1, c->eip);
696 index_reg |= (sib >> 3) & 7;
697 base_reg |= sib & 7;
698 scale = sib >> 6;
699
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700700 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300701 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700702 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300703 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700704 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300705 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700706 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
707 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700708 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700709 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300710 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200711 switch (c->modrm_mod) {
712 case 0:
713 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300714 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200715 break;
716 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300717 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200718 break;
719 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300720 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200721 break;
722 }
723 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300724 op->addr.mem = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200725done:
726 return rc;
727}
728
729static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300730 struct x86_emulate_ops *ops,
731 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200732{
733 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900734 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200735
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300736 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200737 switch (c->ad_bytes) {
738 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300739 op->addr.mem = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200740 break;
741 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300742 op->addr.mem = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200743 break;
744 case 8:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300745 op->addr.mem = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200746 break;
747 }
748done:
749 return rc;
750}
751
Wei Yongjun35c843c2010-08-09 11:34:56 +0800752static void fetch_bit_operand(struct decode_cache *c)
753{
754 long sv, mask;
755
Wei Yongjun3885f182010-08-09 11:37:37 +0800756 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800757 mask = ~(c->dst.bytes * 8 - 1);
758
759 if (c->src.bytes == 2)
760 sv = (s16)c->src.val & (s16)mask;
761 else if (c->src.bytes == 4)
762 sv = (s32)c->src.val & (s32)mask;
763
764 c->dst.addr.mem += (sv >> 3);
765 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800766
767 /* only subword offset */
768 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800769}
770
Gleb Natapov9de41572010-04-28 19:15:22 +0300771static int read_emulated(struct x86_emulate_ctxt *ctxt,
772 struct x86_emulate_ops *ops,
773 unsigned long addr, void *dest, unsigned size)
774{
775 int rc;
776 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300777 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300778
779 while (size) {
780 int n = min(size, 8u);
781 size -= n;
782 if (mc->pos < mc->end)
783 goto read_cached;
784
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300785 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
786 ctxt->vcpu);
787 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300788 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300789 if (rc != X86EMUL_CONTINUE)
790 return rc;
791 mc->end += n;
792
793 read_cached:
794 memcpy(dest, mc->data + mc->pos, n);
795 mc->pos += n;
796 dest += n;
797 addr += n;
798 }
799 return X86EMUL_CONTINUE;
800}
801
Gleb Natapov7b262e92010-03-18 15:20:27 +0200802static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
803 struct x86_emulate_ops *ops,
804 unsigned int size, unsigned short port,
805 void *dest)
806{
807 struct read_cache *rc = &ctxt->decode.io_read;
808
809 if (rc->pos == rc->end) { /* refill pio read ahead */
810 struct decode_cache *c = &ctxt->decode;
811 unsigned int in_page, n;
812 unsigned int count = c->rep_prefix ?
813 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
814 in_page = (ctxt->eflags & EFLG_DF) ?
815 offset_in_page(c->regs[VCPU_REGS_RDI]) :
816 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
817 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
818 count);
819 if (n == 0)
820 n = 1;
821 rc->pos = rc->end = 0;
822 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
823 return 0;
824 rc->end = n * size;
825 }
826
827 memcpy(dest, rc->data + rc->pos, size);
828 rc->pos += size;
829 return 1;
830}
831
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200832static u32 desc_limit_scaled(struct desc_struct *desc)
833{
834 u32 limit = get_desc_limit(desc);
835
836 return desc->g ? (limit << 12) | 0xfff : limit;
837}
838
839static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
840 struct x86_emulate_ops *ops,
841 u16 selector, struct desc_ptr *dt)
842{
843 if (selector & 1 << 2) {
844 struct desc_struct desc;
845 memset (dt, 0, sizeof *dt);
846 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
847 return;
848
849 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
850 dt->address = get_desc_base(&desc);
851 } else
852 ops->get_gdt(dt, ctxt->vcpu);
853}
854
855/* allowed just for 8 bytes segments */
856static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
857 struct x86_emulate_ops *ops,
858 u16 selector, struct desc_struct *desc)
859{
860 struct desc_ptr dt;
861 u16 index = selector >> 3;
862 int ret;
863 u32 err;
864 ulong addr;
865
866 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
867
868 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300869 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200870 return X86EMUL_PROPAGATE_FAULT;
871 }
872 addr = dt.address + index * 8;
873 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
874 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300875 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200876
877 return ret;
878}
879
880/* allowed just for 8 bytes segments */
881static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
882 struct x86_emulate_ops *ops,
883 u16 selector, struct desc_struct *desc)
884{
885 struct desc_ptr dt;
886 u16 index = selector >> 3;
887 u32 err;
888 ulong addr;
889 int ret;
890
891 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
892
893 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300894 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200895 return X86EMUL_PROPAGATE_FAULT;
896 }
897
898 addr = dt.address + index * 8;
899 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
900 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300901 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200902
903 return ret;
904}
905
906static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
907 struct x86_emulate_ops *ops,
908 u16 selector, int seg)
909{
910 struct desc_struct seg_desc;
911 u8 dpl, rpl, cpl;
912 unsigned err_vec = GP_VECTOR;
913 u32 err_code = 0;
914 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
915 int ret;
916
917 memset(&seg_desc, 0, sizeof seg_desc);
918
919 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
920 || ctxt->mode == X86EMUL_MODE_REAL) {
921 /* set real mode segment descriptor */
922 set_desc_base(&seg_desc, selector << 4);
923 set_desc_limit(&seg_desc, 0xffff);
924 seg_desc.type = 3;
925 seg_desc.p = 1;
926 seg_desc.s = 1;
927 goto load;
928 }
929
930 /* NULL selector is not valid for TR, CS and SS */
931 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
932 && null_selector)
933 goto exception;
934
935 /* TR should be in GDT only */
936 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
937 goto exception;
938
939 if (null_selector) /* for NULL selector skip all following checks */
940 goto load;
941
942 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
943 if (ret != X86EMUL_CONTINUE)
944 return ret;
945
946 err_code = selector & 0xfffc;
947 err_vec = GP_VECTOR;
948
949 /* can't load system descriptor into segment selecor */
950 if (seg <= VCPU_SREG_GS && !seg_desc.s)
951 goto exception;
952
953 if (!seg_desc.p) {
954 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
955 goto exception;
956 }
957
958 rpl = selector & 3;
959 dpl = seg_desc.dpl;
960 cpl = ops->cpl(ctxt->vcpu);
961
962 switch (seg) {
963 case VCPU_SREG_SS:
964 /*
965 * segment is not a writable data segment or segment
966 * selector's RPL != CPL or segment selector's RPL != CPL
967 */
968 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
969 goto exception;
970 break;
971 case VCPU_SREG_CS:
972 if (!(seg_desc.type & 8))
973 goto exception;
974
975 if (seg_desc.type & 4) {
976 /* conforming */
977 if (dpl > cpl)
978 goto exception;
979 } else {
980 /* nonconforming */
981 if (rpl > cpl || dpl != cpl)
982 goto exception;
983 }
984 /* CS(RPL) <- CPL */
985 selector = (selector & 0xfffc) | cpl;
986 break;
987 case VCPU_SREG_TR:
988 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
989 goto exception;
990 break;
991 case VCPU_SREG_LDTR:
992 if (seg_desc.s || seg_desc.type != 2)
993 goto exception;
994 break;
995 default: /* DS, ES, FS, or GS */
996 /*
997 * segment is not a data or readable code segment or
998 * ((segment is a data or nonconforming code segment)
999 * and (both RPL and CPL > DPL))
1000 */
1001 if ((seg_desc.type & 0xa) == 0x8 ||
1002 (((seg_desc.type & 0xc) != 0xc) &&
1003 (rpl > dpl && cpl > dpl)))
1004 goto exception;
1005 break;
1006 }
1007
1008 if (seg_desc.s) {
1009 /* mark segment as accessed */
1010 seg_desc.type |= 1;
1011 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1012 if (ret != X86EMUL_CONTINUE)
1013 return ret;
1014 }
1015load:
1016 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1017 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1018 return X86EMUL_CONTINUE;
1019exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001020 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001021 return X86EMUL_PROPAGATE_FAULT;
1022}
1023
Wei Yongjun31be40b2010-08-17 09:17:30 +08001024static void write_register_operand(struct operand *op)
1025{
1026 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1027 switch (op->bytes) {
1028 case 1:
1029 *(u8 *)op->addr.reg = (u8)op->val;
1030 break;
1031 case 2:
1032 *(u16 *)op->addr.reg = (u16)op->val;
1033 break;
1034 case 4:
1035 *op->addr.reg = (u32)op->val;
1036 break; /* 64b: zero-extend */
1037 case 8:
1038 *op->addr.reg = op->val;
1039 break;
1040 }
1041}
1042
Wei Yongjunc37eda12010-06-15 09:03:33 +08001043static inline int writeback(struct x86_emulate_ctxt *ctxt,
1044 struct x86_emulate_ops *ops)
1045{
1046 int rc;
1047 struct decode_cache *c = &ctxt->decode;
1048 u32 err;
1049
1050 switch (c->dst.type) {
1051 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001052 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001053 break;
1054 case OP_MEM:
1055 if (c->lock_prefix)
1056 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001057 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001058 &c->dst.orig_val,
1059 &c->dst.val,
1060 c->dst.bytes,
1061 &err,
1062 ctxt->vcpu);
1063 else
1064 rc = ops->write_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001065 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001066 &c->dst.val,
1067 c->dst.bytes,
1068 &err,
1069 ctxt->vcpu);
1070 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001071 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001072 if (rc != X86EMUL_CONTINUE)
1073 return rc;
1074 break;
1075 case OP_NONE:
1076 /* no writeback */
1077 break;
1078 default:
1079 break;
1080 }
1081 return X86EMUL_CONTINUE;
1082}
1083
Gleb Natapov79168fd2010-04-28 19:15:30 +03001084static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1085 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001086{
1087 struct decode_cache *c = &ctxt->decode;
1088
1089 c->dst.type = OP_MEM;
1090 c->dst.bytes = c->op_bytes;
1091 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001092 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001093 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1094 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001095}
1096
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001097static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001098 struct x86_emulate_ops *ops,
1099 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001100{
1101 struct decode_cache *c = &ctxt->decode;
1102 int rc;
1103
Gleb Natapov79168fd2010-04-28 19:15:30 +03001104 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001105 c->regs[VCPU_REGS_RSP]),
1106 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001107 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001108 return rc;
1109
Avi Kivity350f69d2009-01-05 11:12:40 +02001110 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001111 return rc;
1112}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001113
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001114static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1115 struct x86_emulate_ops *ops,
1116 void *dest, int len)
1117{
1118 int rc;
1119 unsigned long val, change_mask;
1120 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001121 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001122
1123 rc = emulate_pop(ctxt, ops, &val, len);
1124 if (rc != X86EMUL_CONTINUE)
1125 return rc;
1126
1127 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1128 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1129
1130 switch(ctxt->mode) {
1131 case X86EMUL_MODE_PROT64:
1132 case X86EMUL_MODE_PROT32:
1133 case X86EMUL_MODE_PROT16:
1134 if (cpl == 0)
1135 change_mask |= EFLG_IOPL;
1136 if (cpl <= iopl)
1137 change_mask |= EFLG_IF;
1138 break;
1139 case X86EMUL_MODE_VM86:
1140 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001141 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001142 return X86EMUL_PROPAGATE_FAULT;
1143 }
1144 change_mask |= EFLG_IF;
1145 break;
1146 default: /* real mode */
1147 change_mask |= (EFLG_IOPL | EFLG_IF);
1148 break;
1149 }
1150
1151 *(unsigned long *)dest =
1152 (ctxt->eflags & ~change_mask) | (val & change_mask);
1153
1154 return rc;
1155}
1156
Gleb Natapov79168fd2010-04-28 19:15:30 +03001157static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1158 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001159{
1160 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001161
Gleb Natapov79168fd2010-04-28 19:15:30 +03001162 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001163
Gleb Natapov79168fd2010-04-28 19:15:30 +03001164 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001165}
1166
1167static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1168 struct x86_emulate_ops *ops, int seg)
1169{
1170 struct decode_cache *c = &ctxt->decode;
1171 unsigned long selector;
1172 int rc;
1173
1174 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001175 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001176 return rc;
1177
Gleb Natapov2e873022010-03-18 15:20:18 +02001178 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001179 return rc;
1180}
1181
Wei Yongjunc37eda12010-06-15 09:03:33 +08001182static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001183 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001184{
1185 struct decode_cache *c = &ctxt->decode;
1186 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001187 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001188 int reg = VCPU_REGS_RAX;
1189
1190 while (reg <= VCPU_REGS_RDI) {
1191 (reg == VCPU_REGS_RSP) ?
1192 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1193
Gleb Natapov79168fd2010-04-28 19:15:30 +03001194 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001195
1196 rc = writeback(ctxt, ops);
1197 if (rc != X86EMUL_CONTINUE)
1198 return rc;
1199
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001200 ++reg;
1201 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001202
1203 /* Disable writeback. */
1204 c->dst.type = OP_NONE;
1205
1206 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001207}
1208
1209static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1210 struct x86_emulate_ops *ops)
1211{
1212 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001213 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001214 int reg = VCPU_REGS_RDI;
1215
1216 while (reg >= VCPU_REGS_RAX) {
1217 if (reg == VCPU_REGS_RSP) {
1218 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1219 c->op_bytes);
1220 --reg;
1221 }
1222
1223 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001224 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001225 break;
1226 --reg;
1227 }
1228 return rc;
1229}
1230
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001231int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1232 struct x86_emulate_ops *ops, int irq)
1233{
1234 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001235 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001236 struct desc_ptr dt;
1237 gva_t cs_addr;
1238 gva_t eip_addr;
1239 u16 cs, eip;
1240 u32 err;
1241
1242 /* TODO: Add limit checks */
1243 c->src.val = ctxt->eflags;
1244 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001245 rc = writeback(ctxt, ops);
1246 if (rc != X86EMUL_CONTINUE)
1247 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001248
1249 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1250
1251 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1252 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001253 rc = writeback(ctxt, ops);
1254 if (rc != X86EMUL_CONTINUE)
1255 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001256
1257 c->src.val = c->eip;
1258 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001259 rc = writeback(ctxt, ops);
1260 if (rc != X86EMUL_CONTINUE)
1261 return rc;
1262
1263 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001264
1265 ops->get_idt(&dt, ctxt->vcpu);
1266
1267 eip_addr = dt.address + (irq << 2);
1268 cs_addr = dt.address + (irq << 2) + 2;
1269
1270 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &err);
1271 if (rc != X86EMUL_CONTINUE)
1272 return rc;
1273
1274 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &err);
1275 if (rc != X86EMUL_CONTINUE)
1276 return rc;
1277
1278 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1279 if (rc != X86EMUL_CONTINUE)
1280 return rc;
1281
1282 c->eip = eip;
1283
1284 return rc;
1285}
1286
1287static int emulate_int(struct x86_emulate_ctxt *ctxt,
1288 struct x86_emulate_ops *ops, int irq)
1289{
1290 switch(ctxt->mode) {
1291 case X86EMUL_MODE_REAL:
1292 return emulate_int_real(ctxt, ops, irq);
1293 case X86EMUL_MODE_VM86:
1294 case X86EMUL_MODE_PROT16:
1295 case X86EMUL_MODE_PROT32:
1296 case X86EMUL_MODE_PROT64:
1297 default:
1298 /* Protected mode interrupts unimplemented yet */
1299 return X86EMUL_UNHANDLEABLE;
1300 }
1301}
1302
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001303static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1304 struct x86_emulate_ops *ops)
1305{
1306 struct decode_cache *c = &ctxt->decode;
1307 int rc = X86EMUL_CONTINUE;
1308 unsigned long temp_eip = 0;
1309 unsigned long temp_eflags = 0;
1310 unsigned long cs = 0;
1311 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1312 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1313 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1314 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1315
1316 /* TODO: Add stack limit check */
1317
1318 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1319
1320 if (rc != X86EMUL_CONTINUE)
1321 return rc;
1322
1323 if (temp_eip & ~0xffff) {
1324 emulate_gp(ctxt, 0);
1325 return X86EMUL_PROPAGATE_FAULT;
1326 }
1327
1328 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1329
1330 if (rc != X86EMUL_CONTINUE)
1331 return rc;
1332
1333 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1334
1335 if (rc != X86EMUL_CONTINUE)
1336 return rc;
1337
1338 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1339
1340 if (rc != X86EMUL_CONTINUE)
1341 return rc;
1342
1343 c->eip = temp_eip;
1344
1345
1346 if (c->op_bytes == 4)
1347 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1348 else if (c->op_bytes == 2) {
1349 ctxt->eflags &= ~0xffff;
1350 ctxt->eflags |= temp_eflags;
1351 }
1352
1353 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1354 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1355
1356 return rc;
1357}
1358
1359static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1360 struct x86_emulate_ops* ops)
1361{
1362 switch(ctxt->mode) {
1363 case X86EMUL_MODE_REAL:
1364 return emulate_iret_real(ctxt, ops);
1365 case X86EMUL_MODE_VM86:
1366 case X86EMUL_MODE_PROT16:
1367 case X86EMUL_MODE_PROT32:
1368 case X86EMUL_MODE_PROT64:
1369 default:
1370 /* iret from protected mode unimplemented yet */
1371 return X86EMUL_UNHANDLEABLE;
1372 }
1373}
1374
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001375static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1376 struct x86_emulate_ops *ops)
1377{
1378 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001379
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001380 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001381}
1382
Laurent Vivier05f086f2007-09-24 11:10:55 +02001383static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001384{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001385 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001386 switch (c->modrm_reg) {
1387 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001388 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001389 break;
1390 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001391 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392 break;
1393 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001394 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001395 break;
1396 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001397 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398 break;
1399 case 4: /* sal/shl */
1400 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001401 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001402 break;
1403 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001404 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001405 break;
1406 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001407 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001408 break;
1409 }
1410}
1411
1412static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001413 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001414{
1415 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001416 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1417 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001418
1419 switch (c->modrm_reg) {
1420 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001421 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001422 break;
1423 case 2: /* not */
1424 c->dst.val = ~c->dst.val;
1425 break;
1426 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001427 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001428 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001429 case 4: /* mul */
1430 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1431 break;
1432 case 5: /* imul */
1433 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1434 break;
1435 case 6: /* div */
1436 emulate_1op_rax_rdx("div", c->src, *rax, *rdx, ctxt->eflags);
1437 break;
1438 case 7: /* idiv */
1439 emulate_1op_rax_rdx("idiv", c->src, *rax, *rdx, ctxt->eflags);
1440 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001441 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001442 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001443 }
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001444 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001445}
1446
1447static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001448 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001449{
1450 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001451
1452 switch (c->modrm_reg) {
1453 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001454 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001455 break;
1456 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001457 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001458 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001459 case 2: /* call near abs */ {
1460 long int old_eip;
1461 old_eip = c->eip;
1462 c->eip = c->src.val;
1463 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001464 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001465 break;
1466 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001467 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001468 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001469 break;
1470 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001471 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001472 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001473 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001474 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001475}
1476
1477static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001478 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001479{
1480 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001481 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001482
1483 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1484 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001485 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1486 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001487 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001488 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001489 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1490 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001491
Laurent Vivier05f086f2007-09-24 11:10:55 +02001492 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001493 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001494 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001495}
1496
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001497static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1498 struct x86_emulate_ops *ops)
1499{
1500 struct decode_cache *c = &ctxt->decode;
1501 int rc;
1502 unsigned long cs;
1503
1504 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001505 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001506 return rc;
1507 if (c->op_bytes == 4)
1508 c->eip = (u32)c->eip;
1509 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001510 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001511 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001512 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001513 return rc;
1514}
1515
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001516static inline void
1517setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001518 struct x86_emulate_ops *ops, struct desc_struct *cs,
1519 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001520{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001521 memset(cs, 0, sizeof(struct desc_struct));
1522 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1523 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001524
1525 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001526 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001527 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001528 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001529 cs->type = 0x0b; /* Read, Execute, Accessed */
1530 cs->s = 1;
1531 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001532 cs->p = 1;
1533 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001534
Gleb Natapov79168fd2010-04-28 19:15:30 +03001535 set_desc_base(ss, 0); /* flat segment */
1536 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001537 ss->g = 1; /* 4kb granularity */
1538 ss->s = 1;
1539 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001540 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001541 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001542 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001543}
1544
1545static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001546emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001547{
1548 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001549 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001550 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001551 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001552
1553 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001554 if (ctxt->mode == X86EMUL_MODE_REAL ||
1555 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001556 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001557 return X86EMUL_PROPAGATE_FAULT;
1558 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001559
Gleb Natapov79168fd2010-04-28 19:15:30 +03001560 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001561
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001562 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001563 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001564 cs_sel = (u16)(msr_data & 0xfffc);
1565 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001566
1567 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001568 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001569 cs.l = 1;
1570 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001571 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1572 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1573 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1574 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001575
1576 c->regs[VCPU_REGS_RCX] = c->eip;
1577 if (is_long_mode(ctxt->vcpu)) {
1578#ifdef CONFIG_X86_64
1579 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1580
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001581 ops->get_msr(ctxt->vcpu,
1582 ctxt->mode == X86EMUL_MODE_PROT64 ?
1583 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001584 c->eip = msr_data;
1585
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001586 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001587 ctxt->eflags &= ~(msr_data | EFLG_RF);
1588#endif
1589 } else {
1590 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001591 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001592 c->eip = (u32)msr_data;
1593
1594 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1595 }
1596
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001597 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001598}
1599
Andre Przywara8c604352009-06-18 12:56:01 +02001600static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001601emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001602{
1603 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001604 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001605 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001606 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001607
Gleb Natapova0044752010-02-10 14:21:31 +02001608 /* inject #GP if in real mode */
1609 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001610 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001611 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001612 }
1613
1614 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1615 * Therefore, we inject an #UD.
1616 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001617 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001618 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001619 return X86EMUL_PROPAGATE_FAULT;
1620 }
Andre Przywara8c604352009-06-18 12:56:01 +02001621
Gleb Natapov79168fd2010-04-28 19:15:30 +03001622 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001623
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001624 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001625 switch (ctxt->mode) {
1626 case X86EMUL_MODE_PROT32:
1627 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001628 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001629 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001630 }
1631 break;
1632 case X86EMUL_MODE_PROT64:
1633 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001634 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001635 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001636 }
1637 break;
1638 }
1639
1640 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001641 cs_sel = (u16)msr_data;
1642 cs_sel &= ~SELECTOR_RPL_MASK;
1643 ss_sel = cs_sel + 8;
1644 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001645 if (ctxt->mode == X86EMUL_MODE_PROT64
1646 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001647 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001648 cs.l = 1;
1649 }
1650
Gleb Natapov79168fd2010-04-28 19:15:30 +03001651 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1652 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1653 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1654 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001655
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001656 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001657 c->eip = msr_data;
1658
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001659 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001660 c->regs[VCPU_REGS_RSP] = msr_data;
1661
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001662 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001663}
1664
Andre Przywara4668f052009-06-18 12:56:02 +02001665static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001666emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001667{
1668 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001669 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001670 u64 msr_data;
1671 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001672 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001673
Gleb Natapova0044752010-02-10 14:21:31 +02001674 /* inject #GP if in real mode or Virtual 8086 mode */
1675 if (ctxt->mode == X86EMUL_MODE_REAL ||
1676 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001677 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001678 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001679 }
1680
Gleb Natapov79168fd2010-04-28 19:15:30 +03001681 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001682
1683 if ((c->rex_prefix & 0x8) != 0x0)
1684 usermode = X86EMUL_MODE_PROT64;
1685 else
1686 usermode = X86EMUL_MODE_PROT32;
1687
1688 cs.dpl = 3;
1689 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001690 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001691 switch (usermode) {
1692 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001693 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001694 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001695 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001696 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001697 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001698 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001699 break;
1700 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001701 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001702 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001703 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001704 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001705 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001706 ss_sel = cs_sel + 8;
1707 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001708 cs.l = 1;
1709 break;
1710 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001711 cs_sel |= SELECTOR_RPL_MASK;
1712 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001713
Gleb Natapov79168fd2010-04-28 19:15:30 +03001714 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1715 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1716 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1717 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001718
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001719 c->eip = c->regs[VCPU_REGS_RDX];
1720 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001721
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001722 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001723}
1724
Gleb Natapov9c537242010-03-18 15:20:05 +02001725static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1726 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001727{
1728 int iopl;
1729 if (ctxt->mode == X86EMUL_MODE_REAL)
1730 return false;
1731 if (ctxt->mode == X86EMUL_MODE_VM86)
1732 return true;
1733 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001734 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001735}
1736
1737static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1738 struct x86_emulate_ops *ops,
1739 u16 port, u16 len)
1740{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001741 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001742 int r;
1743 u16 io_bitmap_ptr;
1744 u8 perm, bit_idx = port & 0x7;
1745 unsigned mask = (1 << len) - 1;
1746
Gleb Natapov79168fd2010-04-28 19:15:30 +03001747 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1748 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001749 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001750 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001751 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001752 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1753 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001754 if (r != X86EMUL_CONTINUE)
1755 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001756 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001757 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001758 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1759 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001760 if (r != X86EMUL_CONTINUE)
1761 return false;
1762 if ((perm >> bit_idx) & mask)
1763 return false;
1764 return true;
1765}
1766
1767static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1768 struct x86_emulate_ops *ops,
1769 u16 port, u16 len)
1770{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001771 if (ctxt->perm_ok)
1772 return true;
1773
Gleb Natapov9c537242010-03-18 15:20:05 +02001774 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001775 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1776 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001777
1778 ctxt->perm_ok = true;
1779
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001780 return true;
1781}
1782
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001783static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1784 struct x86_emulate_ops *ops,
1785 struct tss_segment_16 *tss)
1786{
1787 struct decode_cache *c = &ctxt->decode;
1788
1789 tss->ip = c->eip;
1790 tss->flag = ctxt->eflags;
1791 tss->ax = c->regs[VCPU_REGS_RAX];
1792 tss->cx = c->regs[VCPU_REGS_RCX];
1793 tss->dx = c->regs[VCPU_REGS_RDX];
1794 tss->bx = c->regs[VCPU_REGS_RBX];
1795 tss->sp = c->regs[VCPU_REGS_RSP];
1796 tss->bp = c->regs[VCPU_REGS_RBP];
1797 tss->si = c->regs[VCPU_REGS_RSI];
1798 tss->di = c->regs[VCPU_REGS_RDI];
1799
1800 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1801 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1802 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1803 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1804 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1805}
1806
1807static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1808 struct x86_emulate_ops *ops,
1809 struct tss_segment_16 *tss)
1810{
1811 struct decode_cache *c = &ctxt->decode;
1812 int ret;
1813
1814 c->eip = tss->ip;
1815 ctxt->eflags = tss->flag | 2;
1816 c->regs[VCPU_REGS_RAX] = tss->ax;
1817 c->regs[VCPU_REGS_RCX] = tss->cx;
1818 c->regs[VCPU_REGS_RDX] = tss->dx;
1819 c->regs[VCPU_REGS_RBX] = tss->bx;
1820 c->regs[VCPU_REGS_RSP] = tss->sp;
1821 c->regs[VCPU_REGS_RBP] = tss->bp;
1822 c->regs[VCPU_REGS_RSI] = tss->si;
1823 c->regs[VCPU_REGS_RDI] = tss->di;
1824
1825 /*
1826 * SDM says that segment selectors are loaded before segment
1827 * descriptors
1828 */
1829 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1830 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1831 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1832 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1833 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1834
1835 /*
1836 * Now load segment descriptors. If fault happenes at this stage
1837 * it is handled in a context of new task
1838 */
1839 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1840 if (ret != X86EMUL_CONTINUE)
1841 return ret;
1842 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1843 if (ret != X86EMUL_CONTINUE)
1844 return ret;
1845 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1846 if (ret != X86EMUL_CONTINUE)
1847 return ret;
1848 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1849 if (ret != X86EMUL_CONTINUE)
1850 return ret;
1851 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1852 if (ret != X86EMUL_CONTINUE)
1853 return ret;
1854
1855 return X86EMUL_CONTINUE;
1856}
1857
1858static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1859 struct x86_emulate_ops *ops,
1860 u16 tss_selector, u16 old_tss_sel,
1861 ulong old_tss_base, struct desc_struct *new_desc)
1862{
1863 struct tss_segment_16 tss_seg;
1864 int ret;
1865 u32 err, new_tss_base = get_desc_base(new_desc);
1866
1867 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1868 &err);
1869 if (ret == X86EMUL_PROPAGATE_FAULT) {
1870 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001871 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001872 return ret;
1873 }
1874
1875 save_state_to_tss16(ctxt, ops, &tss_seg);
1876
1877 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1878 &err);
1879 if (ret == X86EMUL_PROPAGATE_FAULT) {
1880 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001881 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001882 return ret;
1883 }
1884
1885 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1886 &err);
1887 if (ret == X86EMUL_PROPAGATE_FAULT) {
1888 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001889 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001890 return ret;
1891 }
1892
1893 if (old_tss_sel != 0xffff) {
1894 tss_seg.prev_task_link = old_tss_sel;
1895
1896 ret = ops->write_std(new_tss_base,
1897 &tss_seg.prev_task_link,
1898 sizeof tss_seg.prev_task_link,
1899 ctxt->vcpu, &err);
1900 if (ret == X86EMUL_PROPAGATE_FAULT) {
1901 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001902 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001903 return ret;
1904 }
1905 }
1906
1907 return load_state_from_tss16(ctxt, ops, &tss_seg);
1908}
1909
1910static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1911 struct x86_emulate_ops *ops,
1912 struct tss_segment_32 *tss)
1913{
1914 struct decode_cache *c = &ctxt->decode;
1915
1916 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1917 tss->eip = c->eip;
1918 tss->eflags = ctxt->eflags;
1919 tss->eax = c->regs[VCPU_REGS_RAX];
1920 tss->ecx = c->regs[VCPU_REGS_RCX];
1921 tss->edx = c->regs[VCPU_REGS_RDX];
1922 tss->ebx = c->regs[VCPU_REGS_RBX];
1923 tss->esp = c->regs[VCPU_REGS_RSP];
1924 tss->ebp = c->regs[VCPU_REGS_RBP];
1925 tss->esi = c->regs[VCPU_REGS_RSI];
1926 tss->edi = c->regs[VCPU_REGS_RDI];
1927
1928 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1929 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1930 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1931 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1932 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1933 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1934 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1935}
1936
1937static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1938 struct x86_emulate_ops *ops,
1939 struct tss_segment_32 *tss)
1940{
1941 struct decode_cache *c = &ctxt->decode;
1942 int ret;
1943
Gleb Natapov0f122442010-04-28 19:15:31 +03001944 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001945 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001946 return X86EMUL_PROPAGATE_FAULT;
1947 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001948 c->eip = tss->eip;
1949 ctxt->eflags = tss->eflags | 2;
1950 c->regs[VCPU_REGS_RAX] = tss->eax;
1951 c->regs[VCPU_REGS_RCX] = tss->ecx;
1952 c->regs[VCPU_REGS_RDX] = tss->edx;
1953 c->regs[VCPU_REGS_RBX] = tss->ebx;
1954 c->regs[VCPU_REGS_RSP] = tss->esp;
1955 c->regs[VCPU_REGS_RBP] = tss->ebp;
1956 c->regs[VCPU_REGS_RSI] = tss->esi;
1957 c->regs[VCPU_REGS_RDI] = tss->edi;
1958
1959 /*
1960 * SDM says that segment selectors are loaded before segment
1961 * descriptors
1962 */
1963 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1964 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1965 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1966 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1967 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1968 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1969 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1970
1971 /*
1972 * Now load segment descriptors. If fault happenes at this stage
1973 * it is handled in a context of new task
1974 */
1975 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1976 if (ret != X86EMUL_CONTINUE)
1977 return ret;
1978 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1979 if (ret != X86EMUL_CONTINUE)
1980 return ret;
1981 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1982 if (ret != X86EMUL_CONTINUE)
1983 return ret;
1984 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1985 if (ret != X86EMUL_CONTINUE)
1986 return ret;
1987 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1988 if (ret != X86EMUL_CONTINUE)
1989 return ret;
1990 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1991 if (ret != X86EMUL_CONTINUE)
1992 return ret;
1993 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1994 if (ret != X86EMUL_CONTINUE)
1995 return ret;
1996
1997 return X86EMUL_CONTINUE;
1998}
1999
2000static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2001 struct x86_emulate_ops *ops,
2002 u16 tss_selector, u16 old_tss_sel,
2003 ulong old_tss_base, struct desc_struct *new_desc)
2004{
2005 struct tss_segment_32 tss_seg;
2006 int ret;
2007 u32 err, new_tss_base = get_desc_base(new_desc);
2008
2009 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2010 &err);
2011 if (ret == X86EMUL_PROPAGATE_FAULT) {
2012 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002013 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002014 return ret;
2015 }
2016
2017 save_state_to_tss32(ctxt, ops, &tss_seg);
2018
2019 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2020 &err);
2021 if (ret == X86EMUL_PROPAGATE_FAULT) {
2022 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002023 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002024 return ret;
2025 }
2026
2027 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2028 &err);
2029 if (ret == X86EMUL_PROPAGATE_FAULT) {
2030 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002031 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002032 return ret;
2033 }
2034
2035 if (old_tss_sel != 0xffff) {
2036 tss_seg.prev_task_link = old_tss_sel;
2037
2038 ret = ops->write_std(new_tss_base,
2039 &tss_seg.prev_task_link,
2040 sizeof tss_seg.prev_task_link,
2041 ctxt->vcpu, &err);
2042 if (ret == X86EMUL_PROPAGATE_FAULT) {
2043 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002044 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002045 return ret;
2046 }
2047 }
2048
2049 return load_state_from_tss32(ctxt, ops, &tss_seg);
2050}
2051
2052static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002053 struct x86_emulate_ops *ops,
2054 u16 tss_selector, int reason,
2055 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002056{
2057 struct desc_struct curr_tss_desc, next_tss_desc;
2058 int ret;
2059 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2060 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002061 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002062 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002063
2064 /* FIXME: old_tss_base == ~0 ? */
2065
2066 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2067 if (ret != X86EMUL_CONTINUE)
2068 return ret;
2069 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2070 if (ret != X86EMUL_CONTINUE)
2071 return ret;
2072
2073 /* FIXME: check that next_tss_desc is tss */
2074
2075 if (reason != TASK_SWITCH_IRET) {
2076 if ((tss_selector & 3) > next_tss_desc.dpl ||
2077 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002078 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002079 return X86EMUL_PROPAGATE_FAULT;
2080 }
2081 }
2082
Gleb Natapovceffb452010-03-18 15:20:19 +02002083 desc_limit = desc_limit_scaled(&next_tss_desc);
2084 if (!next_tss_desc.p ||
2085 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2086 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002087 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002088 return X86EMUL_PROPAGATE_FAULT;
2089 }
2090
2091 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2092 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2093 write_segment_descriptor(ctxt, ops, old_tss_sel,
2094 &curr_tss_desc);
2095 }
2096
2097 if (reason == TASK_SWITCH_IRET)
2098 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2099
2100 /* set back link to prev task only if NT bit is set in eflags
2101 note that old_tss_sel is not used afetr this point */
2102 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2103 old_tss_sel = 0xffff;
2104
2105 if (next_tss_desc.type & 8)
2106 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2107 old_tss_base, &next_tss_desc);
2108 else
2109 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2110 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002111 if (ret != X86EMUL_CONTINUE)
2112 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002113
2114 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2115 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2116
2117 if (reason != TASK_SWITCH_IRET) {
2118 next_tss_desc.type |= (1 << 1); /* set busy flag */
2119 write_segment_descriptor(ctxt, ops, tss_selector,
2120 &next_tss_desc);
2121 }
2122
2123 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2124 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2125 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2126
Jan Kiszkae269fb22010-04-14 15:51:09 +02002127 if (has_error_code) {
2128 struct decode_cache *c = &ctxt->decode;
2129
2130 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2131 c->lock_prefix = 0;
2132 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002133 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002134 }
2135
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002136 return ret;
2137}
2138
2139int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002140 u16 tss_selector, int reason,
2141 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002142{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002143 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002144 struct decode_cache *c = &ctxt->decode;
2145 int rc;
2146
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002147 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002148 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002149
Jan Kiszkae269fb22010-04-14 15:51:09 +02002150 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2151 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152
2153 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002154 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002155 if (rc == X86EMUL_CONTINUE)
2156 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002157 }
2158
Gleb Natapov19d04432010-04-15 12:29:50 +03002159 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002160}
2161
Gleb Natapova682e352010-03-18 15:20:21 +02002162static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002163 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002164{
2165 struct decode_cache *c = &ctxt->decode;
2166 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2167
Gleb Natapovd9271122010-03-18 15:20:22 +02002168 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002169 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002170}
2171
Avi Kivity63540382010-07-29 15:11:55 +03002172static int em_push(struct x86_emulate_ctxt *ctxt)
2173{
2174 emulate_push(ctxt, ctxt->ops);
2175 return X86EMUL_CONTINUE;
2176}
2177
Avi Kivity7af04fc2010-08-18 14:16:35 +03002178static int em_das(struct x86_emulate_ctxt *ctxt)
2179{
2180 struct decode_cache *c = &ctxt->decode;
2181 u8 al, old_al;
2182 bool af, cf, old_cf;
2183
2184 cf = ctxt->eflags & X86_EFLAGS_CF;
2185 al = c->dst.val;
2186
2187 old_al = al;
2188 old_cf = cf;
2189 cf = false;
2190 af = ctxt->eflags & X86_EFLAGS_AF;
2191 if ((al & 0x0f) > 9 || af) {
2192 al -= 6;
2193 cf = old_cf | (al >= 250);
2194 af = true;
2195 } else {
2196 af = false;
2197 }
2198 if (old_al > 0x99 || old_cf) {
2199 al -= 0x60;
2200 cf = true;
2201 }
2202
2203 c->dst.val = al;
2204 /* Set PF, ZF, SF */
2205 c->src.type = OP_IMM;
2206 c->src.val = 0;
2207 c->src.bytes = 1;
2208 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2209 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2210 if (cf)
2211 ctxt->eflags |= X86_EFLAGS_CF;
2212 if (af)
2213 ctxt->eflags |= X86_EFLAGS_AF;
2214 return X86EMUL_CONTINUE;
2215}
2216
Avi Kivity0ef753b2010-08-18 14:51:45 +03002217static int em_call_far(struct x86_emulate_ctxt *ctxt)
2218{
2219 struct decode_cache *c = &ctxt->decode;
2220 u16 sel, old_cs;
2221 ulong old_eip;
2222 int rc;
2223
2224 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2225 old_eip = c->eip;
2226
2227 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2228 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2229 return X86EMUL_CONTINUE;
2230
2231 c->eip = 0;
2232 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2233
2234 c->src.val = old_cs;
2235 emulate_push(ctxt, ctxt->ops);
2236 rc = writeback(ctxt, ctxt->ops);
2237 if (rc != X86EMUL_CONTINUE)
2238 return rc;
2239
2240 c->src.val = old_eip;
2241 emulate_push(ctxt, ctxt->ops);
2242 rc = writeback(ctxt, ctxt->ops);
2243 if (rc != X86EMUL_CONTINUE)
2244 return rc;
2245
2246 c->dst.type = OP_NONE;
2247
2248 return X86EMUL_CONTINUE;
2249}
2250
Avi Kivity40ece7c2010-08-18 15:12:09 +03002251static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2252{
2253 struct decode_cache *c = &ctxt->decode;
2254 int rc;
2255
2256 c->dst.type = OP_REG;
2257 c->dst.addr.reg = &c->eip;
2258 c->dst.bytes = c->op_bytes;
2259 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2260 if (rc != X86EMUL_CONTINUE)
2261 return rc;
2262 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2263 return X86EMUL_CONTINUE;
2264}
2265
Avi Kivity5c82aa22010-08-18 18:31:43 +03002266static int em_imul(struct x86_emulate_ctxt *ctxt)
2267{
2268 struct decode_cache *c = &ctxt->decode;
2269
2270 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2271 return X86EMUL_CONTINUE;
2272}
2273
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002274static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2275{
2276 struct decode_cache *c = &ctxt->decode;
2277
2278 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002279 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002280}
2281
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002282static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2283{
2284 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2285 struct decode_cache *c = &ctxt->decode;
2286 u64 tsc = 0;
2287
2288 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) {
2289 emulate_gp(ctxt, 0);
2290 return X86EMUL_PROPAGATE_FAULT;
2291 }
2292 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2293 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2294 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2295 return X86EMUL_CONTINUE;
2296}
2297
Avi Kivity73fba5f2010-07-29 15:11:53 +03002298#define D(_y) { .flags = (_y) }
2299#define N D(0)
2300#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2301#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2302#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2303
2304static struct opcode group1[] = {
2305 X7(D(Lock)), N
2306};
2307
2308static struct opcode group1A[] = {
2309 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2310};
2311
2312static struct opcode group3[] = {
2313 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2314 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002315 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002316};
2317
2318static struct opcode group4[] = {
2319 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2320 N, N, N, N, N, N,
2321};
2322
2323static struct opcode group5[] = {
2324 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002325 D(SrcMem | ModRM | Stack),
2326 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002327 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2328 D(SrcMem | ModRM | Stack), N,
2329};
2330
2331static struct group_dual group7 = { {
2332 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2333 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002334 D(SrcMem16 | ModRM | Mov | Priv),
2335 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002336}, {
2337 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2338 D(SrcNone | ModRM | DstMem | Mov), N,
2339 D(SrcMem16 | ModRM | Mov | Priv), N,
2340} };
2341
2342static struct opcode group8[] = {
2343 N, N, N, N,
2344 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2345 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2346};
2347
2348static struct group_dual group9 = { {
2349 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2350}, {
2351 N, N, N, N, N, N, N, N,
2352} };
2353
2354static struct opcode opcode_table[256] = {
2355 /* 0x00 - 0x07 */
2356 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2357 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2358 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2359 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2360 /* 0x08 - 0x0F */
2361 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2362 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2363 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2364 D(ImplicitOps | Stack | No64), N,
2365 /* 0x10 - 0x17 */
2366 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2367 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2368 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2369 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2370 /* 0x18 - 0x1F */
2371 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2372 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2373 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2374 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2375 /* 0x20 - 0x27 */
2376 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2377 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2378 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2379 /* 0x28 - 0x2F */
2380 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2381 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
Avi Kivity7af04fc2010-08-18 14:16:35 +03002382 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm),
2383 N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002384 /* 0x30 - 0x37 */
2385 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2386 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2387 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2388 /* 0x38 - 0x3F */
2389 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2390 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2391 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2392 N, N,
2393 /* 0x40 - 0x4F */
2394 X16(D(DstReg)),
2395 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002396 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002397 /* 0x58 - 0x5F */
2398 X8(D(DstReg | Stack)),
2399 /* 0x60 - 0x67 */
2400 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2401 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2402 N, N, N, N,
2403 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002404 I(SrcImm | Mov | Stack, em_push), N,
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002405 I(SrcImmByte | Mov | Stack, em_push),
2406 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002407 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2408 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2409 /* 0x70 - 0x7F */
2410 X16(D(SrcImmByte)),
2411 /* 0x80 - 0x87 */
2412 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2413 G(DstMem | SrcImm | ModRM | Group, group1),
2414 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2415 G(DstMem | SrcImmByte | ModRM | Group, group1),
2416 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2417 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2418 /* 0x88 - 0x8F */
2419 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2420 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002421 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002422 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2423 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002424 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002425 /* 0x98 - 0x9F */
Wei Yongjune8b6fa72010-08-18 16:43:13 +08002426 D(DstAcc | SrcNone), N, D(SrcImmFAddr | No64), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002427 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2428 /* 0xA0 - 0xA7 */
2429 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2430 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2431 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2432 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2433 /* 0xA8 - 0xAF */
Wei Yongjun06cb7042010-08-04 15:36:53 +08002434 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm),
2435 D(ByteOp | SrcAcc | DstDI | Mov | String), D(SrcAcc | DstDI | Mov | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002436 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
Avi Kivityf6b33fc2010-08-17 11:20:37 +03002437 D(ByteOp | SrcAcc | DstDI | String), D(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002438 /* 0xB0 - 0xB7 */
2439 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2440 /* 0xB8 - 0xBF */
2441 X8(D(DstReg | SrcImm | Mov)),
2442 /* 0xC0 - 0xC7 */
2443 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002444 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2445 D(ImplicitOps | Stack),
2446 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002447 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2448 /* 0xC8 - 0xCF */
2449 N, N, N, D(ImplicitOps | Stack),
2450 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2451 /* 0xD0 - 0xD7 */
Wei Yongjunc034da82010-08-04 15:38:59 +08002452 D(ByteOp | DstMem | SrcOne | ModRM), D(DstMem | SrcOne | ModRM),
Avi Kivity7077aec2010-08-18 18:53:43 +03002453 D(ByteOp | DstMem | ModRM), D(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002454 N, N, N, N,
2455 /* 0xD8 - 0xDF */
2456 N, N, N, N, N, N, N, N,
2457 /* 0xE0 - 0xE7 */
Wei Yongjunf2f31842010-08-18 16:38:21 +08002458 X3(D(SrcImmByte)), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002459 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
Wei Yongjun41167be2010-08-06 11:45:12 +08002460 D(ByteOp | SrcAcc | DstImmUByte), D(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002461 /* 0xE8 - 0xEF */
2462 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2463 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2464 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
Wei Yongjun41167be2010-08-06 11:45:12 +08002465 D(ByteOp | SrcAcc | ImplicitOps), D(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002466 /* 0xF0 - 0xF7 */
2467 N, N, N, N,
2468 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2469 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002470 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002471 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2472};
2473
2474static struct opcode twobyte_table[256] = {
2475 /* 0x00 - 0x0F */
2476 N, GD(0, &group7), N, N,
2477 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2478 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2479 N, D(ImplicitOps | ModRM), N, N,
2480 /* 0x10 - 0x1F */
2481 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2482 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002483 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2484 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002485 N, N, N, N,
2486 N, N, N, N, N, N, N, N,
2487 /* 0x30 - 0x3F */
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002488 D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc),
2489 D(ImplicitOps | Priv), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002490 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2491 N, N, N, N, N, N, N, N,
2492 /* 0x40 - 0x4F */
2493 X16(D(DstReg | SrcMem | ModRM | Mov)),
2494 /* 0x50 - 0x5F */
2495 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2496 /* 0x60 - 0x6F */
2497 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2498 /* 0x70 - 0x7F */
2499 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2500 /* 0x80 - 0x8F */
2501 X16(D(SrcImm)),
2502 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002503 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002504 /* 0xA0 - 0xA7 */
2505 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2506 N, D(DstMem | SrcReg | ModRM | BitOp),
2507 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2508 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2509 /* 0xA8 - 0xAF */
2510 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2511 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2512 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2513 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002514 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002515 /* 0xB0 - 0xB7 */
2516 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2517 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2518 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2519 D(DstReg | SrcMem16 | ModRM | Mov),
2520 /* 0xB8 - 0xBF */
2521 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002522 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002523 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2524 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002525 /* 0xC0 - 0xCF */
Wei Yongjun92f738a2010-08-17 09:19:34 +08002526 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2527 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002528 N, N, N, GD(0, &group9),
2529 N, N, N, N, N, N, N, N,
2530 /* 0xD0 - 0xDF */
2531 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2532 /* 0xE0 - 0xEF */
2533 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2534 /* 0xF0 - 0xFF */
2535 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2536};
2537
2538#undef D
2539#undef N
2540#undef G
2541#undef GD
2542#undef I
2543
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002544int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002545x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2546{
2547 struct x86_emulate_ops *ops = ctxt->ops;
2548 struct decode_cache *c = &ctxt->decode;
2549 int rc = X86EMUL_CONTINUE;
2550 int mode = ctxt->mode;
2551 int def_op_bytes, def_ad_bytes, dual, goffset;
2552 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002553 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002554
2555 /* we cannot decode insn before we complete previous rep insn */
2556 WARN_ON(ctxt->restart);
2557
2558 c->eip = ctxt->eip;
2559 c->fetch.start = c->fetch.end = c->eip;
2560 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2561
2562 switch (mode) {
2563 case X86EMUL_MODE_REAL:
2564 case X86EMUL_MODE_VM86:
2565 case X86EMUL_MODE_PROT16:
2566 def_op_bytes = def_ad_bytes = 2;
2567 break;
2568 case X86EMUL_MODE_PROT32:
2569 def_op_bytes = def_ad_bytes = 4;
2570 break;
2571#ifdef CONFIG_X86_64
2572 case X86EMUL_MODE_PROT64:
2573 def_op_bytes = 4;
2574 def_ad_bytes = 8;
2575 break;
2576#endif
2577 default:
2578 return -1;
2579 }
2580
2581 c->op_bytes = def_op_bytes;
2582 c->ad_bytes = def_ad_bytes;
2583
2584 /* Legacy prefixes. */
2585 for (;;) {
2586 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2587 case 0x66: /* operand-size override */
2588 /* switch between 2/4 bytes */
2589 c->op_bytes = def_op_bytes ^ 6;
2590 break;
2591 case 0x67: /* address-size override */
2592 if (mode == X86EMUL_MODE_PROT64)
2593 /* switch between 4/8 bytes */
2594 c->ad_bytes = def_ad_bytes ^ 12;
2595 else
2596 /* switch between 2/4 bytes */
2597 c->ad_bytes = def_ad_bytes ^ 6;
2598 break;
2599 case 0x26: /* ES override */
2600 case 0x2e: /* CS override */
2601 case 0x36: /* SS override */
2602 case 0x3e: /* DS override */
2603 set_seg_override(c, (c->b >> 3) & 3);
2604 break;
2605 case 0x64: /* FS override */
2606 case 0x65: /* GS override */
2607 set_seg_override(c, c->b & 7);
2608 break;
2609 case 0x40 ... 0x4f: /* REX */
2610 if (mode != X86EMUL_MODE_PROT64)
2611 goto done_prefixes;
2612 c->rex_prefix = c->b;
2613 continue;
2614 case 0xf0: /* LOCK */
2615 c->lock_prefix = 1;
2616 break;
2617 case 0xf2: /* REPNE/REPNZ */
2618 c->rep_prefix = REPNE_PREFIX;
2619 break;
2620 case 0xf3: /* REP/REPE/REPZ */
2621 c->rep_prefix = REPE_PREFIX;
2622 break;
2623 default:
2624 goto done_prefixes;
2625 }
2626
2627 /* Any legacy prefix after a REX prefix nullifies its effect. */
2628
2629 c->rex_prefix = 0;
2630 }
2631
2632done_prefixes:
2633
2634 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002635 if (c->rex_prefix & 8)
2636 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002637
2638 /* Opcode byte(s). */
2639 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002640 /* Two-byte opcode? */
2641 if (c->b == 0x0f) {
2642 c->twobyte = 1;
2643 c->b = insn_fetch(u8, 1, c->eip);
2644 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002645 }
2646 c->d = opcode.flags;
2647
2648 if (c->d & Group) {
2649 dual = c->d & GroupDual;
2650 c->modrm = insn_fetch(u8, 1, c->eip);
2651 --c->eip;
2652
2653 if (c->d & GroupDual) {
2654 g_mod012 = opcode.u.gdual->mod012;
2655 g_mod3 = opcode.u.gdual->mod3;
2656 } else
2657 g_mod012 = g_mod3 = opcode.u.group;
2658
2659 c->d &= ~(Group | GroupDual);
2660
2661 goffset = (c->modrm >> 3) & 7;
2662
2663 if ((c->modrm >> 6) == 3)
2664 opcode = g_mod3[goffset];
2665 else
2666 opcode = g_mod012[goffset];
2667 c->d |= opcode.flags;
2668 }
2669
2670 c->execute = opcode.u.execute;
2671
2672 /* Unrecognised? */
2673 if (c->d == 0 || (c->d & Undefined)) {
2674 DPRINTF("Cannot emulate %02x\n", c->b);
2675 return -1;
2676 }
2677
2678 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2679 c->op_bytes = 8;
2680
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002681 if (c->d & Op3264) {
2682 if (mode == X86EMUL_MODE_PROT64)
2683 c->op_bytes = 8;
2684 else
2685 c->op_bytes = 4;
2686 }
2687
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002688 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002689 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002690 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002691 if (!c->has_seg_override)
2692 set_seg_override(c, c->modrm_seg);
2693 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002694 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002695 if (rc != X86EMUL_CONTINUE)
2696 goto done;
2697
2698 if (!c->has_seg_override)
2699 set_seg_override(c, VCPU_SREG_DS);
2700
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002701 if (memop.type == OP_MEM && !(!c->twobyte && c->b == 0x8d))
2702 memop.addr.mem += seg_override_base(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002703
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002704 if (memop.type == OP_MEM && c->ad_bytes != 8)
2705 memop.addr.mem = (u32)memop.addr.mem;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002706
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002707 if (memop.type == OP_MEM && c->rip_relative)
2708 memop.addr.mem += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002709
2710 /*
2711 * Decode and fetch the source operand: register, memory
2712 * or immediate.
2713 */
2714 switch (c->d & SrcMask) {
2715 case SrcNone:
2716 break;
2717 case SrcReg:
2718 decode_register_operand(&c->src, c, 0);
2719 break;
2720 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002721 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002722 goto srcmem_common;
2723 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002724 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002725 goto srcmem_common;
2726 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002727 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002728 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002729 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002730 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002731 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002732 case SrcImmU16:
2733 c->src.bytes = 2;
2734 goto srcimm;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002735 case SrcImm:
2736 case SrcImmU:
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002737 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2738 if (c->src.bytes == 8)
2739 c->src.bytes = 4;
Avi Kivityb250e602010-08-18 15:11:24 +03002740 srcimm:
2741 c->src.type = OP_IMM;
2742 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002743 /* NB. Immediates are sign-extended as necessary. */
2744 switch (c->src.bytes) {
2745 case 1:
2746 c->src.val = insn_fetch(s8, 1, c->eip);
2747 break;
2748 case 2:
2749 c->src.val = insn_fetch(s16, 2, c->eip);
2750 break;
2751 case 4:
2752 c->src.val = insn_fetch(s32, 4, c->eip);
2753 break;
2754 }
Avi Kivityb250e602010-08-18 15:11:24 +03002755 if ((c->d & SrcMask) == SrcImmU
2756 || (c->d & SrcMask) == SrcImmU16) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002757 switch (c->src.bytes) {
2758 case 1:
2759 c->src.val &= 0xff;
2760 break;
2761 case 2:
2762 c->src.val &= 0xffff;
2763 break;
2764 case 4:
2765 c->src.val &= 0xffffffff;
2766 break;
2767 }
2768 }
2769 break;
2770 case SrcImmByte:
2771 case SrcImmUByte:
2772 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002773 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002774 c->src.bytes = 1;
2775 if ((c->d & SrcMask) == SrcImmByte)
2776 c->src.val = insn_fetch(s8, 1, c->eip);
2777 else
2778 c->src.val = insn_fetch(u8, 1, c->eip);
2779 break;
2780 case SrcAcc:
2781 c->src.type = OP_REG;
2782 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002783 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002784 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002785 break;
2786 case SrcOne:
2787 c->src.bytes = 1;
2788 c->src.val = 1;
2789 break;
2790 case SrcSI:
2791 c->src.type = OP_MEM;
2792 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002793 c->src.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002794 register_address(c, seg_override_base(ctxt, ops, c),
2795 c->regs[VCPU_REGS_RSI]);
2796 c->src.val = 0;
2797 break;
2798 case SrcImmFAddr:
2799 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002800 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002801 c->src.bytes = c->op_bytes + 2;
2802 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2803 break;
2804 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002805 memop.bytes = c->op_bytes + 2;
2806 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002807 break;
2808 }
2809
2810 /*
2811 * Decode and fetch the second source operand: register, memory
2812 * or immediate.
2813 */
2814 switch (c->d & Src2Mask) {
2815 case Src2None:
2816 break;
2817 case Src2CL:
2818 c->src2.bytes = 1;
2819 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2820 break;
2821 case Src2ImmByte:
2822 c->src2.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002823 c->src2.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002824 c->src2.bytes = 1;
2825 c->src2.val = insn_fetch(u8, 1, c->eip);
2826 break;
2827 case Src2One:
2828 c->src2.bytes = 1;
2829 c->src2.val = 1;
2830 break;
2831 }
2832
2833 /* Decode and fetch the destination operand: register or memory. */
2834 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002835 case DstReg:
2836 decode_register_operand(&c->dst, c,
2837 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2838 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08002839 case DstImmUByte:
2840 c->dst.type = OP_IMM;
2841 c->dst.addr.mem = c->eip;
2842 c->dst.bytes = 1;
2843 c->dst.val = insn_fetch(u8, 1, c->eip);
2844 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002845 case DstMem:
2846 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002847 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002848 if ((c->d & DstMask) == DstMem64)
2849 c->dst.bytes = 8;
2850 else
2851 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002852 if (c->d & BitOp)
2853 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002854 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002855 break;
2856 case DstAcc:
2857 c->dst.type = OP_REG;
2858 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002859 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002860 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002861 c->dst.orig_val = c->dst.val;
2862 break;
2863 case DstDI:
2864 c->dst.type = OP_MEM;
2865 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002866 c->dst.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002867 register_address(c, es_base(ctxt, ops),
2868 c->regs[VCPU_REGS_RDI]);
2869 c->dst.val = 0;
2870 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002871 case ImplicitOps:
2872 /* Special instructions do their own operand decoding. */
2873 default:
2874 c->dst.type = OP_NONE; /* Disable writeback. */
2875 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002876 }
2877
2878done:
2879 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2880}
2881
2882int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002883x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002884{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002885 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002886 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002887 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002888 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002889 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002890 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002891
Gleb Natapov9de41572010-04-28 19:15:22 +03002892 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002893
Gleb Natapov11616242010-02-11 14:43:14 +02002894 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002895 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002896 goto done;
2897 }
2898
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002899 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002900 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002901 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002902 goto done;
2903 }
2904
Gleb Natapove92805a2010-02-10 14:21:35 +02002905 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002906 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002907 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002908 goto done;
2909 }
2910
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002911 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002912 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002913 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002914 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002915 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002916 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002917 goto done;
2918 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002919 }
2920
Wei Yongjunc483c022010-08-06 15:36:36 +08002921 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002922 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002923 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002924 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002925 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002926 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002927 }
2928
Gleb Natapove35b7b92010-02-25 16:36:42 +02002929 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002930 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002931 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002932 if (rc != X86EMUL_CONTINUE)
2933 goto done;
2934 }
2935
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002936 if ((c->d & DstMask) == ImplicitOps)
2937 goto special_insn;
2938
2939
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002940 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2941 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002942 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002943 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002944 if (rc != X86EMUL_CONTINUE)
2945 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002946 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002947 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002948
Avi Kivity018a98d2007-11-27 19:30:56 +02002949special_insn:
2950
Avi Kivityef65c882010-07-29 15:11:51 +03002951 if (c->execute) {
2952 rc = c->execute(ctxt);
2953 if (rc != X86EMUL_CONTINUE)
2954 goto done;
2955 goto writeback;
2956 }
2957
Laurent Viviere4e03de2007-09-18 11:52:50 +02002958 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 goto twobyte_insn;
2960
Laurent Viviere4e03de2007-09-18 11:52:50 +02002961 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 case 0x00 ... 0x05:
2963 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002964 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002966 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002967 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002968 break;
2969 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002970 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002971 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002972 goto done;
2973 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 case 0x08 ... 0x0d:
2975 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002976 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002978 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002979 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002980 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 case 0x10 ... 0x15:
2982 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002983 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002985 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002986 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002987 break;
2988 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002989 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002990 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002991 goto done;
2992 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 case 0x18 ... 0x1d:
2994 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002995 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002997 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002998 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002999 break;
3000 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003001 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003002 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003003 goto done;
3004 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003005 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003007 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003008 break;
3009 case 0x28 ... 0x2d:
3010 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003011 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012 break;
3013 case 0x30 ... 0x35:
3014 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003015 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016 break;
3017 case 0x38 ... 0x3d:
3018 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003019 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003021 case 0x40 ... 0x47: /* inc r16/r32 */
3022 emulate_1op("inc", c->dst, ctxt->eflags);
3023 break;
3024 case 0x48 ... 0x4f: /* dec r16/r32 */
3025 emulate_1op("dec", c->dst, ctxt->eflags);
3026 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003027 case 0x58 ... 0x5f: /* pop reg */
3028 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003029 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003030 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02003031 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02003032 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003033 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003034 rc = emulate_pusha(ctxt, ops);
3035 if (rc != X86EMUL_CONTINUE)
3036 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003037 break;
3038 case 0x61: /* popa */
3039 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003040 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003041 goto done;
3042 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003044 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003046 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003048 case 0x6c: /* insb */
3049 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003050 c->src.val = c->regs[VCPU_REGS_RDX];
3051 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003052 case 0x6e: /* outsb */
3053 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003054 c->dst.val = c->regs[VCPU_REGS_RDX];
3055 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003056 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003057 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003058 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003059 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003062 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063 case 0:
3064 goto add;
3065 case 1:
3066 goto or;
3067 case 2:
3068 goto adc;
3069 case 3:
3070 goto sbb;
3071 case 4:
3072 goto and;
3073 case 5:
3074 goto sub;
3075 case 6:
3076 goto xor;
3077 case 7:
3078 goto cmp;
3079 }
3080 break;
3081 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003082 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003083 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 break;
3085 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003086 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003088 c->src.val = c->dst.val;
3089 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090 /*
3091 * Write back the memory destination with implicit LOCK
3092 * prefix.
3093 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003094 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003095 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03003098 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003099 case 0x8c: /* mov r/m, sreg */
3100 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003101 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003102 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003103 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003104 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003105 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003106 case 0x8d: /* lea r16/r32, m */
Avi Kivity342fc632010-08-01 15:13:22 +03003107 c->dst.val = c->src.addr.mem;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003108 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003109 case 0x8e: { /* mov seg, r/m16 */
3110 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003111
3112 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003113
Gleb Natapovc6975182010-02-18 12:15:01 +02003114 if (c->modrm_reg == VCPU_SREG_CS ||
3115 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003116 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003117 goto done;
3118 }
3119
Glauber Costa310b5d32009-05-12 16:21:06 -04003120 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003121 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003122
Gleb Natapov2e873022010-03-18 15:20:18 +02003123 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003124
3125 c->dst.type = OP_NONE; /* Disable writeback. */
3126 break;
3127 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003129 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003130 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003133 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3134 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003135 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003136 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003137 case 0x98: /* cbw/cwde/cdqe */
3138 switch (c->op_bytes) {
3139 case 2: c->dst.val = (s8)c->dst.val; break;
3140 case 4: c->dst.val = (s16)c->dst.val; break;
3141 case 8: c->dst.val = (s32)c->dst.val; break;
3142 }
3143 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003144 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003145 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003146 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003147 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003148 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003149 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003150 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003151 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003152 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
3153 if (rc != X86EMUL_CONTINUE)
3154 goto done;
3155 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08003156 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02003158 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003160 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003161 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02003162 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003163 case 0xa8 ... 0xa9: /* test ax, imm */
3164 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 case 0xaa ... 0xab: /* stos */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02003167 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003169 goto cmp;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03003170 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02003171 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02003172 case 0xc0 ... 0xc1:
3173 emulate_grp2(ctxt);
3174 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003175 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003176 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003177 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003178 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003179 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02003180 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
3181 mov:
3182 c->dst.val = c->src.val;
3183 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003184 case 0xcb: /* ret far */
3185 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003186 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003187 goto done;
3188 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003189 case 0xcc: /* int3 */
3190 irq = 3;
3191 goto do_interrupt;
3192 case 0xcd: /* int n */
3193 irq = c->src.val;
3194 do_interrupt:
3195 rc = emulate_int(ctxt, ops, irq);
3196 if (rc != X86EMUL_CONTINUE)
3197 goto done;
3198 break;
3199 case 0xce: /* into */
3200 if (ctxt->eflags & EFLG_OF) {
3201 irq = 4;
3202 goto do_interrupt;
3203 }
3204 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003205 case 0xcf: /* iret */
3206 rc = emulate_iret(ctxt, ops);
3207
3208 if (rc != X86EMUL_CONTINUE)
3209 goto done;
3210 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003211 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003212 emulate_grp2(ctxt);
3213 break;
3214 case 0xd2 ... 0xd3: /* Grp2 */
3215 c->src.val = c->regs[VCPU_REGS_RCX];
3216 emulate_grp2(ctxt);
3217 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003218 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3219 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3220 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3221 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3222 jmp_rel(c, c->src.val);
3223 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003224 case 0xe4: /* inb */
3225 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003226 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003227 case 0xe6: /* outb */
3228 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003229 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003230 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003231 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003232 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003233 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003234 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003235 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003236 }
3237 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003238 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003239 case 0xea: { /* jmp far */
3240 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003241 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003242 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3243
3244 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003245 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003246
Gleb Natapov414e6272010-04-28 19:15:26 +03003247 c->eip = 0;
3248 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003249 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003250 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003251 case 0xeb:
3252 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003253 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003254 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003255 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003256 case 0xec: /* in al,dx */
3257 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003258 c->src.val = c->regs[VCPU_REGS_RDX];
3259 do_io_in:
3260 c->dst.bytes = min(c->dst.bytes, 4u);
3261 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003262 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003263 goto done;
3264 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003265 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3266 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003267 goto done; /* IO is needed */
3268 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003269 case 0xee: /* out dx,al */
3270 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003271 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003272 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003273 c->src.bytes = min(c->src.bytes, 4u);
3274 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3275 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003276 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003277 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003278 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003279 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3280 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003281 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003282 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003283 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003284 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003285 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003286 case 0xf5: /* cmc */
3287 /* complement carry flag from eflags reg */
3288 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003289 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003290 case 0xf6 ... 0xf7: /* Grp3 */
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03003291 if (emulate_grp3(ctxt, ops) != X86EMUL_CONTINUE)
Gleb Natapovaca06a82010-03-18 15:20:15 +02003292 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003293 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003294 case 0xf8: /* clc */
3295 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003296 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003297 case 0xf9: /* stc */
3298 ctxt->eflags |= EFLG_CF;
3299 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003300 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003301 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003302 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003303 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003304 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003305 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003306 break;
3307 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003308 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003309 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003310 goto done;
3311 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003312 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003313 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003314 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003315 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003316 case 0xfc: /* cld */
3317 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003318 break;
3319 case 0xfd: /* std */
3320 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003321 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003322 case 0xfe: /* Grp4 */
3323 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003324 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003325 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003326 goto done;
3327 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003328 case 0xff: /* Grp5 */
3329 if (c->modrm_reg == 5)
3330 goto jump_far;
3331 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003332 default:
3333 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003335
3336writeback:
3337 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003338 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003339 goto done;
3340
Gleb Natapov5cd21912010-03-18 15:20:26 +02003341 /*
3342 * restore dst type in case the decoding will be reused
3343 * (happens for string instruction )
3344 */
3345 c->dst.type = saved_dst_type;
3346
Gleb Natapova682e352010-03-18 15:20:21 +02003347 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003348 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3349 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003350
3351 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003352 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3353 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003354
Gleb Natapov5cd21912010-03-18 15:20:26 +02003355 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003356 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003357 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003358 /* The second termination condition only applies for REPE
3359 * and REPNE. Test if the repeat string operation prefix is
3360 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3361 * corresponding termination condition according to:
3362 * - if REPE/REPZ and ZF = 0 then done
3363 * - if REPNE/REPNZ and ZF = 1 then done
3364 */
3365 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3366 (c->b == 0xae) || (c->b == 0xaf))
3367 && (((c->rep_prefix == REPE_PREFIX) &&
3368 ((ctxt->eflags & EFLG_ZF) == 0))
3369 || ((c->rep_prefix == REPNE_PREFIX) &&
3370 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3371 ctxt->restart = false;
Gleb Natapov7b262e92010-03-18 15:20:27 +02003372 /*
3373 * Re-enter guest when pio read ahead buffer is empty or,
3374 * if it is not used, after each 1024 iteration.
3375 */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003376 else if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3377 (rc->end != 0 && rc->end == rc->pos)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02003378 ctxt->restart = false;
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003379 c->eip = ctxt->eip;
3380 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003381 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003382 /*
3383 * reset read cache here in case string instruction is restared
3384 * without decoding
3385 */
3386 ctxt->decode.mem_read.end = 0;
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003387 if (!ctxt->restart)
3388 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003389
3390done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003391 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392
3393twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003394 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003396 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 u16 size;
3398 unsigned long address;
3399
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003400 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003401 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003402 goto cannot_emulate;
3403
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003404 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003405 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003406 goto done;
3407
Avi Kivity33e38852008-05-21 15:34:25 +03003408 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003409 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003410 /* Disable writeback. */
3411 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003412 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003414 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003415 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003416 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 goto done;
3418 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003419 /* Disable writeback. */
3420 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003422 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003423 if (c->modrm_mod == 3) {
3424 switch (c->modrm_rm) {
3425 case 1:
3426 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003427 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003428 goto done;
3429 break;
3430 default:
3431 goto cannot_emulate;
3432 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003433 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003434 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003435 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003436 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003437 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003438 goto done;
3439 realmode_lidt(ctxt->vcpu, size, address);
3440 }
Avi Kivity16286d02008-04-14 14:40:50 +03003441 /* Disable writeback. */
3442 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443 break;
3444 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003445 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003446 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 break;
3448 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003449 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003450 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003451 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003453 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003454 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003455 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003456 case 7: /* invlpg*/
Avi Kivity1f6f0582010-08-01 15:19:22 +03003457 emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
Avi Kivity16286d02008-04-14 14:40:50 +03003458 /* Disable writeback. */
3459 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460 break;
3461 default:
3462 goto cannot_emulate;
3463 }
3464 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003465 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003466 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003467 if (rc != X86EMUL_CONTINUE)
3468 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003469 else
3470 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003471 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003472 case 0x06:
3473 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003474 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003475 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003476 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003477 break;
3478 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003479 case 0x0d: /* GrpP (prefetch) */
3480 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003481 break;
3482 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003483 switch (c->modrm_reg) {
3484 case 1:
3485 case 5 ... 7:
3486 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003487 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003488 goto done;
3489 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003490 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003491 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003493 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3494 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003495 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003496 goto done;
3497 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003498 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003500 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003501 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003502 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003503 goto done;
3504 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003505 c->dst.type = OP_NONE;
3506 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003508 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3509 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003510 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003511 goto done;
3512 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003513
Avi Kivityb27f3852010-08-01 14:25:22 +03003514 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003515 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3516 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3517 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003518 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003519 goto done;
3520 }
3521
Laurent Viviera01af5e2007-09-24 11:10:56 +02003522 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003524 case 0x30:
3525 /* wrmsr */
3526 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3527 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003528 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003529 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003530 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003531 }
3532 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003533 break;
3534 case 0x32:
3535 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003536 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003537 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003538 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003539 } else {
3540 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3541 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3542 }
3543 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003544 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003545 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003546 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003547 if (rc != X86EMUL_CONTINUE)
3548 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003549 else
3550 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003551 break;
3552 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003553 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003554 if (rc != X86EMUL_CONTINUE)
3555 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003556 else
3557 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003558 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003559 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003560 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003561 if (!test_cc(c->b, ctxt->eflags))
3562 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003564 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003565 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003566 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003567 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003568 case 0x90 ... 0x9f: /* setcc r/m8 */
3569 c->dst.val = test_cc(c->b, ctxt->eflags);
3570 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003571 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003572 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003573 break;
3574 case 0xa1: /* pop fs */
3575 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003576 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003577 goto done;
3578 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003579 case 0xa3:
3580 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003581 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003582 /* only subword offset */
3583 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003584 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003585 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003586 case 0xa4: /* shld imm8, r, r/m */
3587 case 0xa5: /* shld cl, r, r/m */
3588 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3589 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003590 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003591 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003592 break;
3593 case 0xa9: /* pop gs */
3594 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003595 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003596 goto done;
3597 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003598 case 0xab:
3599 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003600 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003601 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003602 case 0xac: /* shrd imm8, r, r/m */
3603 case 0xad: /* shrd cl, r, r/m */
3604 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3605 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003606 case 0xae: /* clflush */
3607 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608 case 0xb0 ... 0xb1: /* cmpxchg */
3609 /*
3610 * Save real source value, then compare EAX against
3611 * destination.
3612 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003613 c->src.orig_val = c->src.val;
3614 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003615 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3616 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003618 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003619 } else {
3620 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003621 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003622 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003623 }
3624 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625 case 0xb3:
3626 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003627 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003629 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003630 c->dst.bytes = c->op_bytes;
3631 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3632 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003635 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636 case 0:
3637 goto bt;
3638 case 1:
3639 goto bts;
3640 case 2:
3641 goto btr;
3642 case 3:
3643 goto btc;
3644 }
3645 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003646 case 0xbb:
3647 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003648 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003649 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003650 case 0xbc: { /* bsf */
3651 u8 zf;
3652 __asm__ ("bsf %2, %0; setz %1"
3653 : "=r"(c->dst.val), "=q"(zf)
3654 : "r"(c->src.val));
3655 ctxt->eflags &= ~X86_EFLAGS_ZF;
3656 if (zf) {
3657 ctxt->eflags |= X86_EFLAGS_ZF;
3658 c->dst.type = OP_NONE; /* Disable writeback. */
3659 }
3660 break;
3661 }
3662 case 0xbd: { /* bsr */
3663 u8 zf;
3664 __asm__ ("bsr %2, %0; setz %1"
3665 : "=r"(c->dst.val), "=q"(zf)
3666 : "r"(c->src.val));
3667 ctxt->eflags &= ~X86_EFLAGS_ZF;
3668 if (zf) {
3669 ctxt->eflags |= X86_EFLAGS_ZF;
3670 c->dst.type = OP_NONE; /* Disable writeback. */
3671 }
3672 break;
3673 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003675 c->dst.bytes = c->op_bytes;
3676 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3677 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003679 case 0xc0 ... 0xc1: /* xadd */
3680 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3681 /* Write back the register source. */
3682 c->src.val = c->dst.orig_val;
3683 write_register_operand(&c->src);
3684 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003685 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003686 c->dst.bytes = c->op_bytes;
3687 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3688 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003689 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003690 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003691 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003692 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003693 goto done;
3694 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003695 default:
3696 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003697 }
3698 goto writeback;
3699
3700cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003701 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003702 return -1;
3703}