blob: e257f22868663caf1f9f1ebb09fbafd8f8b0f3db [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)
Avi Kivity7db41eb2010-08-18 19:25:28 +030098#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010099#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100
Avi Kivityd0e53322010-07-29 15:11:54 +0300101#define X2(x...) x, x
102#define X3(x...) X2(x), x
103#define X4(x...) X2(x), X2(x)
104#define X5(x...) X4(x), x
105#define X6(x...) X4(x), X2(x)
106#define X7(x...) X4(x), X3(x)
107#define X8(x...) X4(x), X4(x)
108#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300109
Avi Kivityd65b1de2010-07-29 15:11:35 +0300110struct opcode {
111 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300112 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300113 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300114 struct opcode *group;
115 struct group_dual *gdual;
116 } u;
117};
118
119struct group_dual {
120 struct opcode mod012[8];
121 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300122};
123
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200125#define EFLG_ID (1<<21)
126#define EFLG_VIP (1<<20)
127#define EFLG_VIF (1<<19)
128#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200129#define EFLG_VM (1<<17)
130#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200131#define EFLG_IOPL (3<<12)
132#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133#define EFLG_OF (1<<11)
134#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200135#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200136#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137#define EFLG_SF (1<<7)
138#define EFLG_ZF (1<<6)
139#define EFLG_AF (1<<4)
140#define EFLG_PF (1<<2)
141#define EFLG_CF (1<<0)
142
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300143#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
144#define EFLG_RESERVED_ONE_MASK 2
145
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146/*
147 * Instruction emulation:
148 * Most instructions are emulated directly via a fragment of inline assembly
149 * code. This allows us to save/restore EFLAGS and thus very easily pick up
150 * any modified flags.
151 */
152
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800153#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154#define _LO32 "k" /* force 32-bit operand */
155#define _STK "%%rsp" /* stack pointer */
156#elif defined(__i386__)
157#define _LO32 "" /* force 32-bit operand */
158#define _STK "%%esp" /* stack pointer */
159#endif
160
161/*
162 * These EFLAGS bits are restored from saved value during emulation, and
163 * any changes are written back to the saved value after emulation.
164 */
165#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
166
167/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200168#define _PRE_EFLAGS(_sav, _msk, _tmp) \
169 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
170 "movl %"_sav",%"_LO32 _tmp"; " \
171 "push %"_tmp"; " \
172 "push %"_tmp"; " \
173 "movl %"_msk",%"_LO32 _tmp"; " \
174 "andl %"_LO32 _tmp",("_STK"); " \
175 "pushf; " \
176 "notl %"_LO32 _tmp"; " \
177 "andl %"_LO32 _tmp",("_STK"); " \
178 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
179 "pop %"_tmp"; " \
180 "orl %"_LO32 _tmp",("_STK"); " \
181 "popf; " \
182 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183
184/* After executing instruction: write-back necessary bits in EFLAGS. */
185#define _POST_EFLAGS(_sav, _msk, _tmp) \
186 /* _sav |= EFLAGS & _msk; */ \
187 "pushf; " \
188 "pop %"_tmp"; " \
189 "andl %"_msk",%"_LO32 _tmp"; " \
190 "orl %"_LO32 _tmp",%"_sav"; "
191
Avi Kivitydda96d82008-11-26 15:14:10 +0200192#ifdef CONFIG_X86_64
193#define ON64(x) x
194#else
195#define ON64(x)
196#endif
197
Avi Kivityb3b3d252010-08-16 17:49:52 +0300198#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200199 do { \
200 __asm__ __volatile__ ( \
201 _PRE_EFLAGS("0", "4", "2") \
202 _op _suffix " %"_x"3,%1; " \
203 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300204 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200205 "=&r" (_tmp) \
206 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200207 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200208
209
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210/* Raw emulation: instruction has two explicit operands. */
211#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200212 do { \
213 unsigned long _tmp; \
214 \
215 switch ((_dst).bytes) { \
216 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300217 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200218 break; \
219 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300220 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200221 break; \
222 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300223 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200224 break; \
225 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 } while (0)
227
228#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
229 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200230 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400231 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300233 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 break; \
235 default: \
236 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
237 _wx, _wy, _lx, _ly, _qx, _qy); \
238 break; \
239 } \
240 } while (0)
241
242/* Source operand is byte-sized and may be restricted to just %cl. */
243#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
244 __emulate_2op(_op, _src, _dst, _eflags, \
245 "b", "c", "b", "c", "b", "c", "b", "c")
246
247/* Source operand is byte, word, long or quad sized. */
248#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
249 __emulate_2op(_op, _src, _dst, _eflags, \
250 "b", "q", "w", "r", _LO32, "r", "", "r")
251
252/* Source operand is word, long or quad sized. */
253#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
254 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
255 "w", "r", _LO32, "r", "", "r")
256
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100257/* Instruction has three operands and one operand is stored in ECX register */
258#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
259 do { \
260 unsigned long _tmp; \
261 _type _clv = (_cl).val; \
262 _type _srcv = (_src).val; \
263 _type _dstv = (_dst).val; \
264 \
265 __asm__ __volatile__ ( \
266 _PRE_EFLAGS("0", "5", "2") \
267 _op _suffix " %4,%1 \n" \
268 _POST_EFLAGS("0", "5", "2") \
269 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
270 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
271 ); \
272 \
273 (_cl).val = (unsigned long) _clv; \
274 (_src).val = (unsigned long) _srcv; \
275 (_dst).val = (unsigned long) _dstv; \
276 } while (0)
277
278#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
279 do { \
280 switch ((_dst).bytes) { \
281 case 2: \
282 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
283 "w", unsigned short); \
284 break; \
285 case 4: \
286 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
287 "l", unsigned int); \
288 break; \
289 case 8: \
290 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
291 "q", unsigned long)); \
292 break; \
293 } \
294 } while (0)
295
Avi Kivitydda96d82008-11-26 15:14:10 +0200296#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297 do { \
298 unsigned long _tmp; \
299 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200300 __asm__ __volatile__ ( \
301 _PRE_EFLAGS("0", "3", "2") \
302 _op _suffix " %1; " \
303 _POST_EFLAGS("0", "3", "2") \
304 : "=m" (_eflags), "+m" ((_dst).val), \
305 "=&r" (_tmp) \
306 : "i" (EFLAGS_MASK)); \
307 } while (0)
308
309/* Instruction has only one explicit operand (no source operand). */
310#define emulate_1op(_op, _dst, _eflags) \
311 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400312 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200313 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
314 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
315 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
316 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 } \
318 } while (0)
319
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300320#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
321 do { \
322 unsigned long _tmp; \
323 \
324 __asm__ __volatile__ ( \
325 _PRE_EFLAGS("0", "4", "1") \
326 _op _suffix " %5; " \
327 _POST_EFLAGS("0", "4", "1") \
328 : "=m" (_eflags), "=&r" (_tmp), \
329 "+a" (_rax), "+d" (_rdx) \
330 : "i" (EFLAGS_MASK), "m" ((_src).val), \
331 "a" (_rax), "d" (_rdx)); \
332 } while (0)
333
334/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
335#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
336 do { \
337 switch((_src).bytes) { \
338 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
339 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
340 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
341 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
342 } \
343 } while (0)
344
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345/* Fetch next part of the instruction being emulated. */
346#define insn_fetch(_type, _size, _eip) \
347({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200348 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200349 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350 goto done; \
351 (_eip) += (_size); \
352 (_type)_x; \
353})
354
Gleb Natapov414e6272010-04-28 19:15:26 +0300355#define insn_fetch_arr(_arr, _size, _eip) \
356({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
357 if (rc != X86EMUL_CONTINUE) \
358 goto done; \
359 (_eip) += (_size); \
360})
361
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800362static inline unsigned long ad_mask(struct decode_cache *c)
363{
364 return (1UL << (c->ad_bytes << 3)) - 1;
365}
366
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800368static inline unsigned long
369address_mask(struct decode_cache *c, unsigned long reg)
370{
371 if (c->ad_bytes == sizeof(unsigned long))
372 return reg;
373 else
374 return reg & ad_mask(c);
375}
376
377static inline unsigned long
378register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
379{
380 return base + address_mask(c, reg);
381}
382
Harvey Harrison7a9572752008-02-19 07:40:41 -0800383static inline void
384register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
385{
386 if (c->ad_bytes == sizeof(unsigned long))
387 *reg += inc;
388 else
389 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
390}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391
Harvey Harrison7a9572752008-02-19 07:40:41 -0800392static inline void jmp_rel(struct decode_cache *c, int rel)
393{
394 register_address_increment(c, &c->eip, rel);
395}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300396
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300397static void set_seg_override(struct decode_cache *c, int seg)
398{
399 c->has_seg_override = true;
400 c->seg_override = seg;
401}
402
Gleb Natapov79168fd2010-04-28 19:15:30 +0300403static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
404 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300405{
406 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
407 return 0;
408
Gleb Natapov79168fd2010-04-28 19:15:30 +0300409 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300410}
411
412static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300413 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300414 struct decode_cache *c)
415{
416 if (!c->has_seg_override)
417 return 0;
418
Gleb Natapov79168fd2010-04-28 19:15:30 +0300419 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300420}
421
Gleb Natapov79168fd2010-04-28 19:15:30 +0300422static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
423 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300424{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300425 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300426}
427
Gleb Natapov79168fd2010-04-28 19:15:30 +0300428static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
429 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300430{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300431 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300432}
433
Gleb Natapov54b84862010-04-28 19:15:44 +0300434static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
435 u32 error, bool valid)
436{
437 ctxt->exception = vec;
438 ctxt->error_code = error;
439 ctxt->error_code_valid = valid;
440 ctxt->restart = false;
441}
442
443static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
444{
445 emulate_exception(ctxt, GP_VECTOR, err, true);
446}
447
448static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
449 int err)
450{
451 ctxt->cr2 = addr;
452 emulate_exception(ctxt, PF_VECTOR, err, true);
453}
454
455static void emulate_ud(struct x86_emulate_ctxt *ctxt)
456{
457 emulate_exception(ctxt, UD_VECTOR, 0, false);
458}
459
460static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
461{
462 emulate_exception(ctxt, TS_VECTOR, err, true);
463}
464
Avi Kivity62266862007-11-20 13:15:52 +0200465static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
466 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300467 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200468{
469 struct fetch_cache *fc = &ctxt->decode.fetch;
470 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300471 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200472
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300473 if (eip == fc->end) {
474 cur_size = fc->end - fc->start;
475 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
476 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
477 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900478 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200479 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300480 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200481 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300482 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900483 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200484}
485
486static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
487 struct x86_emulate_ops *ops,
488 unsigned long eip, void *dest, unsigned size)
489{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900490 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200491
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200492 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200493 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200494 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200495 while (size--) {
496 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900497 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200498 return rc;
499 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900500 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200501}
502
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000503/*
504 * Given the 'reg' portion of a ModRM byte, and a register block, return a
505 * pointer into the block that addresses the relevant register.
506 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
507 */
508static void *decode_register(u8 modrm_reg, unsigned long *regs,
509 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510{
511 void *p;
512
513 p = &regs[modrm_reg];
514 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
515 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
516 return p;
517}
518
519static int read_descriptor(struct x86_emulate_ctxt *ctxt,
520 struct x86_emulate_ops *ops,
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300521 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 u16 *size, unsigned long *address, int op_bytes)
523{
524 int rc;
525
526 if (op_bytes == 2)
527 op_bytes = 3;
528 *address = 0;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300529 rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900530 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531 return rc;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300532 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 return rc;
534}
535
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300536static int test_cc(unsigned int condition, unsigned int flags)
537{
538 int rc = 0;
539
540 switch ((condition & 15) >> 1) {
541 case 0: /* o */
542 rc |= (flags & EFLG_OF);
543 break;
544 case 1: /* b/c/nae */
545 rc |= (flags & EFLG_CF);
546 break;
547 case 2: /* z/e */
548 rc |= (flags & EFLG_ZF);
549 break;
550 case 3: /* be/na */
551 rc |= (flags & (EFLG_CF|EFLG_ZF));
552 break;
553 case 4: /* s */
554 rc |= (flags & EFLG_SF);
555 break;
556 case 5: /* p/pe */
557 rc |= (flags & EFLG_PF);
558 break;
559 case 7: /* le/ng */
560 rc |= (flags & EFLG_ZF);
561 /* fall through */
562 case 6: /* l/nge */
563 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
564 break;
565 }
566
567 /* Odd condition identifiers (lsb == 1) have inverted sense. */
568 return (!!rc ^ (condition & 1));
569}
570
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300571static void fetch_register_operand(struct operand *op)
572{
573 switch (op->bytes) {
574 case 1:
575 op->val = *(u8 *)op->addr.reg;
576 break;
577 case 2:
578 op->val = *(u16 *)op->addr.reg;
579 break;
580 case 4:
581 op->val = *(u32 *)op->addr.reg;
582 break;
583 case 8:
584 op->val = *(u64 *)op->addr.reg;
585 break;
586 }
587}
588
Avi Kivity3c118e22007-10-31 10:27:04 +0200589static void decode_register_operand(struct operand *op,
590 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200591 int inhibit_bytereg)
592{
Avi Kivity33615aa2007-10-31 11:15:56 +0200593 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200594 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200595
596 if (!(c->d & ModRM))
597 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200598 op->type = OP_REG;
599 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300600 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200601 op->bytes = 1;
602 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300603 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200604 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200605 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300606 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200607 op->orig_val = op->val;
608}
609
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200610static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300611 struct x86_emulate_ops *ops,
612 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200613{
614 struct decode_cache *c = &ctxt->decode;
615 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700616 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900617 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300618 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200619
620 if (c->rex_prefix) {
621 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
622 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
623 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
624 }
625
626 c->modrm = insn_fetch(u8, 1, c->eip);
627 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
628 c->modrm_reg |= (c->modrm & 0x38) >> 3;
629 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300630 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200631
632 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300633 op->type = OP_REG;
634 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
635 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300636 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300637 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200638 return rc;
639 }
640
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300641 op->type = OP_MEM;
642
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200643 if (c->ad_bytes == 2) {
644 unsigned bx = c->regs[VCPU_REGS_RBX];
645 unsigned bp = c->regs[VCPU_REGS_RBP];
646 unsigned si = c->regs[VCPU_REGS_RSI];
647 unsigned di = c->regs[VCPU_REGS_RDI];
648
649 /* 16-bit ModR/M decode. */
650 switch (c->modrm_mod) {
651 case 0:
652 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300653 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200654 break;
655 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300656 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200657 break;
658 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300659 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200660 break;
661 }
662 switch (c->modrm_rm) {
663 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300664 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200665 break;
666 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300667 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200668 break;
669 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300670 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200671 break;
672 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300673 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200674 break;
675 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300676 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200677 break;
678 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300679 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200680 break;
681 case 6:
682 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300683 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200684 break;
685 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300686 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200687 break;
688 }
689 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
690 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300691 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300692 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200693 } else {
694 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700695 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200696 sib = insn_fetch(u8, 1, c->eip);
697 index_reg |= (sib >> 3) & 7;
698 base_reg |= sib & 7;
699 scale = sib >> 6;
700
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700701 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300702 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700703 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300704 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700705 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300706 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700707 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
708 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700709 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700710 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300711 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200712 switch (c->modrm_mod) {
713 case 0:
714 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300715 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200716 break;
717 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300718 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200719 break;
720 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300721 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200722 break;
723 }
724 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300725 op->addr.mem = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200726done:
727 return rc;
728}
729
730static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300731 struct x86_emulate_ops *ops,
732 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200733{
734 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900735 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200736
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300737 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200738 switch (c->ad_bytes) {
739 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300740 op->addr.mem = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200741 break;
742 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300743 op->addr.mem = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200744 break;
745 case 8:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300746 op->addr.mem = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200747 break;
748 }
749done:
750 return rc;
751}
752
Wei Yongjun35c843c2010-08-09 11:34:56 +0800753static void fetch_bit_operand(struct decode_cache *c)
754{
755 long sv, mask;
756
Wei Yongjun3885f182010-08-09 11:37:37 +0800757 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800758 mask = ~(c->dst.bytes * 8 - 1);
759
760 if (c->src.bytes == 2)
761 sv = (s16)c->src.val & (s16)mask;
762 else if (c->src.bytes == 4)
763 sv = (s32)c->src.val & (s32)mask;
764
765 c->dst.addr.mem += (sv >> 3);
766 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800767
768 /* only subword offset */
769 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800770}
771
Gleb Natapov9de41572010-04-28 19:15:22 +0300772static int read_emulated(struct x86_emulate_ctxt *ctxt,
773 struct x86_emulate_ops *ops,
774 unsigned long addr, void *dest, unsigned size)
775{
776 int rc;
777 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300778 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300779
780 while (size) {
781 int n = min(size, 8u);
782 size -= n;
783 if (mc->pos < mc->end)
784 goto read_cached;
785
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300786 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
787 ctxt->vcpu);
788 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300789 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300790 if (rc != X86EMUL_CONTINUE)
791 return rc;
792 mc->end += n;
793
794 read_cached:
795 memcpy(dest, mc->data + mc->pos, n);
796 mc->pos += n;
797 dest += n;
798 addr += n;
799 }
800 return X86EMUL_CONTINUE;
801}
802
Gleb Natapov7b262e92010-03-18 15:20:27 +0200803static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
804 struct x86_emulate_ops *ops,
805 unsigned int size, unsigned short port,
806 void *dest)
807{
808 struct read_cache *rc = &ctxt->decode.io_read;
809
810 if (rc->pos == rc->end) { /* refill pio read ahead */
811 struct decode_cache *c = &ctxt->decode;
812 unsigned int in_page, n;
813 unsigned int count = c->rep_prefix ?
814 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
815 in_page = (ctxt->eflags & EFLG_DF) ?
816 offset_in_page(c->regs[VCPU_REGS_RDI]) :
817 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
818 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
819 count);
820 if (n == 0)
821 n = 1;
822 rc->pos = rc->end = 0;
823 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
824 return 0;
825 rc->end = n * size;
826 }
827
828 memcpy(dest, rc->data + rc->pos, size);
829 rc->pos += size;
830 return 1;
831}
832
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200833static u32 desc_limit_scaled(struct desc_struct *desc)
834{
835 u32 limit = get_desc_limit(desc);
836
837 return desc->g ? (limit << 12) | 0xfff : limit;
838}
839
840static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
841 struct x86_emulate_ops *ops,
842 u16 selector, struct desc_ptr *dt)
843{
844 if (selector & 1 << 2) {
845 struct desc_struct desc;
846 memset (dt, 0, sizeof *dt);
847 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
848 return;
849
850 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
851 dt->address = get_desc_base(&desc);
852 } else
853 ops->get_gdt(dt, ctxt->vcpu);
854}
855
856/* allowed just for 8 bytes segments */
857static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
858 struct x86_emulate_ops *ops,
859 u16 selector, struct desc_struct *desc)
860{
861 struct desc_ptr dt;
862 u16 index = selector >> 3;
863 int ret;
864 u32 err;
865 ulong addr;
866
867 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
868
869 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300870 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200871 return X86EMUL_PROPAGATE_FAULT;
872 }
873 addr = dt.address + index * 8;
874 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
875 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300876 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200877
878 return ret;
879}
880
881/* allowed just for 8 bytes segments */
882static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
883 struct x86_emulate_ops *ops,
884 u16 selector, struct desc_struct *desc)
885{
886 struct desc_ptr dt;
887 u16 index = selector >> 3;
888 u32 err;
889 ulong addr;
890 int ret;
891
892 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
893
894 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300895 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200896 return X86EMUL_PROPAGATE_FAULT;
897 }
898
899 addr = dt.address + index * 8;
900 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
901 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300902 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200903
904 return ret;
905}
906
907static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
908 struct x86_emulate_ops *ops,
909 u16 selector, int seg)
910{
911 struct desc_struct seg_desc;
912 u8 dpl, rpl, cpl;
913 unsigned err_vec = GP_VECTOR;
914 u32 err_code = 0;
915 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
916 int ret;
917
918 memset(&seg_desc, 0, sizeof seg_desc);
919
920 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
921 || ctxt->mode == X86EMUL_MODE_REAL) {
922 /* set real mode segment descriptor */
923 set_desc_base(&seg_desc, selector << 4);
924 set_desc_limit(&seg_desc, 0xffff);
925 seg_desc.type = 3;
926 seg_desc.p = 1;
927 seg_desc.s = 1;
928 goto load;
929 }
930
931 /* NULL selector is not valid for TR, CS and SS */
932 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
933 && null_selector)
934 goto exception;
935
936 /* TR should be in GDT only */
937 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
938 goto exception;
939
940 if (null_selector) /* for NULL selector skip all following checks */
941 goto load;
942
943 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
944 if (ret != X86EMUL_CONTINUE)
945 return ret;
946
947 err_code = selector & 0xfffc;
948 err_vec = GP_VECTOR;
949
950 /* can't load system descriptor into segment selecor */
951 if (seg <= VCPU_SREG_GS && !seg_desc.s)
952 goto exception;
953
954 if (!seg_desc.p) {
955 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
956 goto exception;
957 }
958
959 rpl = selector & 3;
960 dpl = seg_desc.dpl;
961 cpl = ops->cpl(ctxt->vcpu);
962
963 switch (seg) {
964 case VCPU_SREG_SS:
965 /*
966 * segment is not a writable data segment or segment
967 * selector's RPL != CPL or segment selector's RPL != CPL
968 */
969 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
970 goto exception;
971 break;
972 case VCPU_SREG_CS:
973 if (!(seg_desc.type & 8))
974 goto exception;
975
976 if (seg_desc.type & 4) {
977 /* conforming */
978 if (dpl > cpl)
979 goto exception;
980 } else {
981 /* nonconforming */
982 if (rpl > cpl || dpl != cpl)
983 goto exception;
984 }
985 /* CS(RPL) <- CPL */
986 selector = (selector & 0xfffc) | cpl;
987 break;
988 case VCPU_SREG_TR:
989 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
990 goto exception;
991 break;
992 case VCPU_SREG_LDTR:
993 if (seg_desc.s || seg_desc.type != 2)
994 goto exception;
995 break;
996 default: /* DS, ES, FS, or GS */
997 /*
998 * segment is not a data or readable code segment or
999 * ((segment is a data or nonconforming code segment)
1000 * and (both RPL and CPL > DPL))
1001 */
1002 if ((seg_desc.type & 0xa) == 0x8 ||
1003 (((seg_desc.type & 0xc) != 0xc) &&
1004 (rpl > dpl && cpl > dpl)))
1005 goto exception;
1006 break;
1007 }
1008
1009 if (seg_desc.s) {
1010 /* mark segment as accessed */
1011 seg_desc.type |= 1;
1012 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1013 if (ret != X86EMUL_CONTINUE)
1014 return ret;
1015 }
1016load:
1017 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1018 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1019 return X86EMUL_CONTINUE;
1020exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001021 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001022 return X86EMUL_PROPAGATE_FAULT;
1023}
1024
Wei Yongjun31be40b2010-08-17 09:17:30 +08001025static void write_register_operand(struct operand *op)
1026{
1027 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1028 switch (op->bytes) {
1029 case 1:
1030 *(u8 *)op->addr.reg = (u8)op->val;
1031 break;
1032 case 2:
1033 *(u16 *)op->addr.reg = (u16)op->val;
1034 break;
1035 case 4:
1036 *op->addr.reg = (u32)op->val;
1037 break; /* 64b: zero-extend */
1038 case 8:
1039 *op->addr.reg = op->val;
1040 break;
1041 }
1042}
1043
Wei Yongjunc37eda12010-06-15 09:03:33 +08001044static inline int writeback(struct x86_emulate_ctxt *ctxt,
1045 struct x86_emulate_ops *ops)
1046{
1047 int rc;
1048 struct decode_cache *c = &ctxt->decode;
1049 u32 err;
1050
1051 switch (c->dst.type) {
1052 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001053 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001054 break;
1055 case OP_MEM:
1056 if (c->lock_prefix)
1057 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001058 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001059 &c->dst.orig_val,
1060 &c->dst.val,
1061 c->dst.bytes,
1062 &err,
1063 ctxt->vcpu);
1064 else
1065 rc = ops->write_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001066 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001067 &c->dst.val,
1068 c->dst.bytes,
1069 &err,
1070 ctxt->vcpu);
1071 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001072 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001073 if (rc != X86EMUL_CONTINUE)
1074 return rc;
1075 break;
1076 case OP_NONE:
1077 /* no writeback */
1078 break;
1079 default:
1080 break;
1081 }
1082 return X86EMUL_CONTINUE;
1083}
1084
Gleb Natapov79168fd2010-04-28 19:15:30 +03001085static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1086 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001087{
1088 struct decode_cache *c = &ctxt->decode;
1089
1090 c->dst.type = OP_MEM;
1091 c->dst.bytes = c->op_bytes;
1092 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001093 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001094 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1095 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001096}
1097
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001098static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001099 struct x86_emulate_ops *ops,
1100 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001101{
1102 struct decode_cache *c = &ctxt->decode;
1103 int rc;
1104
Gleb Natapov79168fd2010-04-28 19:15:30 +03001105 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001106 c->regs[VCPU_REGS_RSP]),
1107 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001108 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001109 return rc;
1110
Avi Kivity350f69d2009-01-05 11:12:40 +02001111 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001112 return rc;
1113}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001114
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001115static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1116 struct x86_emulate_ops *ops,
1117 void *dest, int len)
1118{
1119 int rc;
1120 unsigned long val, change_mask;
1121 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001122 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001123
1124 rc = emulate_pop(ctxt, ops, &val, len);
1125 if (rc != X86EMUL_CONTINUE)
1126 return rc;
1127
1128 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1129 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1130
1131 switch(ctxt->mode) {
1132 case X86EMUL_MODE_PROT64:
1133 case X86EMUL_MODE_PROT32:
1134 case X86EMUL_MODE_PROT16:
1135 if (cpl == 0)
1136 change_mask |= EFLG_IOPL;
1137 if (cpl <= iopl)
1138 change_mask |= EFLG_IF;
1139 break;
1140 case X86EMUL_MODE_VM86:
1141 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001142 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001143 return X86EMUL_PROPAGATE_FAULT;
1144 }
1145 change_mask |= EFLG_IF;
1146 break;
1147 default: /* real mode */
1148 change_mask |= (EFLG_IOPL | EFLG_IF);
1149 break;
1150 }
1151
1152 *(unsigned long *)dest =
1153 (ctxt->eflags & ~change_mask) | (val & change_mask);
1154
1155 return rc;
1156}
1157
Gleb Natapov79168fd2010-04-28 19:15:30 +03001158static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1159 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001160{
1161 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001162
Gleb Natapov79168fd2010-04-28 19:15:30 +03001163 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001164
Gleb Natapov79168fd2010-04-28 19:15:30 +03001165 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001166}
1167
1168static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1169 struct x86_emulate_ops *ops, int seg)
1170{
1171 struct decode_cache *c = &ctxt->decode;
1172 unsigned long selector;
1173 int rc;
1174
1175 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001176 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001177 return rc;
1178
Gleb Natapov2e873022010-03-18 15:20:18 +02001179 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001180 return rc;
1181}
1182
Wei Yongjunc37eda12010-06-15 09:03:33 +08001183static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001184 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001185{
1186 struct decode_cache *c = &ctxt->decode;
1187 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001188 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001189 int reg = VCPU_REGS_RAX;
1190
1191 while (reg <= VCPU_REGS_RDI) {
1192 (reg == VCPU_REGS_RSP) ?
1193 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1194
Gleb Natapov79168fd2010-04-28 19:15:30 +03001195 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001196
1197 rc = writeback(ctxt, ops);
1198 if (rc != X86EMUL_CONTINUE)
1199 return rc;
1200
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001201 ++reg;
1202 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001203
1204 /* Disable writeback. */
1205 c->dst.type = OP_NONE;
1206
1207 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001208}
1209
1210static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1211 struct x86_emulate_ops *ops)
1212{
1213 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001214 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001215 int reg = VCPU_REGS_RDI;
1216
1217 while (reg >= VCPU_REGS_RAX) {
1218 if (reg == VCPU_REGS_RSP) {
1219 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1220 c->op_bytes);
1221 --reg;
1222 }
1223
1224 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001225 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001226 break;
1227 --reg;
1228 }
1229 return rc;
1230}
1231
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001232int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1233 struct x86_emulate_ops *ops, int irq)
1234{
1235 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001236 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001237 struct desc_ptr dt;
1238 gva_t cs_addr;
1239 gva_t eip_addr;
1240 u16 cs, eip;
1241 u32 err;
1242
1243 /* TODO: Add limit checks */
1244 c->src.val = ctxt->eflags;
1245 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001246 rc = writeback(ctxt, ops);
1247 if (rc != X86EMUL_CONTINUE)
1248 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001249
1250 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1251
1252 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1253 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001254 rc = writeback(ctxt, ops);
1255 if (rc != X86EMUL_CONTINUE)
1256 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001257
1258 c->src.val = c->eip;
1259 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001260 rc = writeback(ctxt, ops);
1261 if (rc != X86EMUL_CONTINUE)
1262 return rc;
1263
1264 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001265
1266 ops->get_idt(&dt, ctxt->vcpu);
1267
1268 eip_addr = dt.address + (irq << 2);
1269 cs_addr = dt.address + (irq << 2) + 2;
1270
1271 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &err);
1272 if (rc != X86EMUL_CONTINUE)
1273 return rc;
1274
1275 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &err);
1276 if (rc != X86EMUL_CONTINUE)
1277 return rc;
1278
1279 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1280 if (rc != X86EMUL_CONTINUE)
1281 return rc;
1282
1283 c->eip = eip;
1284
1285 return rc;
1286}
1287
1288static int emulate_int(struct x86_emulate_ctxt *ctxt,
1289 struct x86_emulate_ops *ops, int irq)
1290{
1291 switch(ctxt->mode) {
1292 case X86EMUL_MODE_REAL:
1293 return emulate_int_real(ctxt, ops, irq);
1294 case X86EMUL_MODE_VM86:
1295 case X86EMUL_MODE_PROT16:
1296 case X86EMUL_MODE_PROT32:
1297 case X86EMUL_MODE_PROT64:
1298 default:
1299 /* Protected mode interrupts unimplemented yet */
1300 return X86EMUL_UNHANDLEABLE;
1301 }
1302}
1303
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001304static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1305 struct x86_emulate_ops *ops)
1306{
1307 struct decode_cache *c = &ctxt->decode;
1308 int rc = X86EMUL_CONTINUE;
1309 unsigned long temp_eip = 0;
1310 unsigned long temp_eflags = 0;
1311 unsigned long cs = 0;
1312 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1313 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1314 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1315 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1316
1317 /* TODO: Add stack limit check */
1318
1319 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1320
1321 if (rc != X86EMUL_CONTINUE)
1322 return rc;
1323
1324 if (temp_eip & ~0xffff) {
1325 emulate_gp(ctxt, 0);
1326 return X86EMUL_PROPAGATE_FAULT;
1327 }
1328
1329 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1330
1331 if (rc != X86EMUL_CONTINUE)
1332 return rc;
1333
1334 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1335
1336 if (rc != X86EMUL_CONTINUE)
1337 return rc;
1338
1339 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1340
1341 if (rc != X86EMUL_CONTINUE)
1342 return rc;
1343
1344 c->eip = temp_eip;
1345
1346
1347 if (c->op_bytes == 4)
1348 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1349 else if (c->op_bytes == 2) {
1350 ctxt->eflags &= ~0xffff;
1351 ctxt->eflags |= temp_eflags;
1352 }
1353
1354 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1355 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1356
1357 return rc;
1358}
1359
1360static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1361 struct x86_emulate_ops* ops)
1362{
1363 switch(ctxt->mode) {
1364 case X86EMUL_MODE_REAL:
1365 return emulate_iret_real(ctxt, ops);
1366 case X86EMUL_MODE_VM86:
1367 case X86EMUL_MODE_PROT16:
1368 case X86EMUL_MODE_PROT32:
1369 case X86EMUL_MODE_PROT64:
1370 default:
1371 /* iret from protected mode unimplemented yet */
1372 return X86EMUL_UNHANDLEABLE;
1373 }
1374}
1375
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001376static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1377 struct x86_emulate_ops *ops)
1378{
1379 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001380
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001381 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001382}
1383
Laurent Vivier05f086f2007-09-24 11:10:55 +02001384static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001385{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001386 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001387 switch (c->modrm_reg) {
1388 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001389 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001390 break;
1391 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001392 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393 break;
1394 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001395 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396 break;
1397 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001398 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001399 break;
1400 case 4: /* sal/shl */
1401 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001402 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403 break;
1404 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001405 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001406 break;
1407 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001408 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001409 break;
1410 }
1411}
1412
1413static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001414 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001415{
1416 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001417 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1418 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001419
1420 switch (c->modrm_reg) {
1421 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001422 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001423 break;
1424 case 2: /* not */
1425 c->dst.val = ~c->dst.val;
1426 break;
1427 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001428 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001429 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001430 case 4: /* mul */
1431 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1432 break;
1433 case 5: /* imul */
1434 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1435 break;
1436 case 6: /* div */
1437 emulate_1op_rax_rdx("div", c->src, *rax, *rdx, ctxt->eflags);
1438 break;
1439 case 7: /* idiv */
1440 emulate_1op_rax_rdx("idiv", c->src, *rax, *rdx, ctxt->eflags);
1441 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001442 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001443 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001444 }
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001445 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001446}
1447
1448static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001449 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001450{
1451 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001452
1453 switch (c->modrm_reg) {
1454 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001455 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001456 break;
1457 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001458 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001459 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001460 case 2: /* call near abs */ {
1461 long int old_eip;
1462 old_eip = c->eip;
1463 c->eip = c->src.val;
1464 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001465 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001466 break;
1467 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001468 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001469 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001470 break;
1471 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001472 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001473 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001474 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001475 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001476}
1477
1478static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001479 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001480{
1481 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001482 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001483
1484 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1485 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001486 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1487 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001488 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001489 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001490 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1491 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001492
Laurent Vivier05f086f2007-09-24 11:10:55 +02001493 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001494 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001495 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001496}
1497
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001498static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1499 struct x86_emulate_ops *ops)
1500{
1501 struct decode_cache *c = &ctxt->decode;
1502 int rc;
1503 unsigned long cs;
1504
1505 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001506 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001507 return rc;
1508 if (c->op_bytes == 4)
1509 c->eip = (u32)c->eip;
1510 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001511 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001512 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001513 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001514 return rc;
1515}
1516
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001517static inline void
1518setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001519 struct x86_emulate_ops *ops, struct desc_struct *cs,
1520 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001521{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001522 memset(cs, 0, sizeof(struct desc_struct));
1523 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1524 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001525
1526 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001527 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001528 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001529 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001530 cs->type = 0x0b; /* Read, Execute, Accessed */
1531 cs->s = 1;
1532 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001533 cs->p = 1;
1534 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001535
Gleb Natapov79168fd2010-04-28 19:15:30 +03001536 set_desc_base(ss, 0); /* flat segment */
1537 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001538 ss->g = 1; /* 4kb granularity */
1539 ss->s = 1;
1540 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001541 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001542 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001543 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001544}
1545
1546static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001547emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001548{
1549 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001550 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001551 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001552 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001553
1554 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001555 if (ctxt->mode == X86EMUL_MODE_REAL ||
1556 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001557 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001558 return X86EMUL_PROPAGATE_FAULT;
1559 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001560
Gleb Natapov79168fd2010-04-28 19:15:30 +03001561 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001562
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001563 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001564 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001565 cs_sel = (u16)(msr_data & 0xfffc);
1566 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001567
1568 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001569 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001570 cs.l = 1;
1571 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001572 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1573 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1574 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1575 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001576
1577 c->regs[VCPU_REGS_RCX] = c->eip;
1578 if (is_long_mode(ctxt->vcpu)) {
1579#ifdef CONFIG_X86_64
1580 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1581
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001582 ops->get_msr(ctxt->vcpu,
1583 ctxt->mode == X86EMUL_MODE_PROT64 ?
1584 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001585 c->eip = msr_data;
1586
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001587 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001588 ctxt->eflags &= ~(msr_data | EFLG_RF);
1589#endif
1590 } else {
1591 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001592 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001593 c->eip = (u32)msr_data;
1594
1595 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1596 }
1597
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001598 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001599}
1600
Andre Przywara8c604352009-06-18 12:56:01 +02001601static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001602emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001603{
1604 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001605 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001606 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001608
Gleb Natapova0044752010-02-10 14:21:31 +02001609 /* inject #GP if in real mode */
1610 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001611 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001612 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001613 }
1614
1615 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1616 * Therefore, we inject an #UD.
1617 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001618 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001619 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001620 return X86EMUL_PROPAGATE_FAULT;
1621 }
Andre Przywara8c604352009-06-18 12:56:01 +02001622
Gleb Natapov79168fd2010-04-28 19:15:30 +03001623 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001624
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001625 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001626 switch (ctxt->mode) {
1627 case X86EMUL_MODE_PROT32:
1628 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001629 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001630 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001631 }
1632 break;
1633 case X86EMUL_MODE_PROT64:
1634 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001635 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001636 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001637 }
1638 break;
1639 }
1640
1641 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001642 cs_sel = (u16)msr_data;
1643 cs_sel &= ~SELECTOR_RPL_MASK;
1644 ss_sel = cs_sel + 8;
1645 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001646 if (ctxt->mode == X86EMUL_MODE_PROT64
1647 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001648 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001649 cs.l = 1;
1650 }
1651
Gleb Natapov79168fd2010-04-28 19:15:30 +03001652 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1653 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1654 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1655 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001656
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001657 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001658 c->eip = msr_data;
1659
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001660 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001661 c->regs[VCPU_REGS_RSP] = msr_data;
1662
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001663 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001664}
1665
Andre Przywara4668f052009-06-18 12:56:02 +02001666static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001667emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001668{
1669 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001670 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001671 u64 msr_data;
1672 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001673 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001674
Gleb Natapova0044752010-02-10 14:21:31 +02001675 /* inject #GP if in real mode or Virtual 8086 mode */
1676 if (ctxt->mode == X86EMUL_MODE_REAL ||
1677 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001678 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001679 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001680 }
1681
Gleb Natapov79168fd2010-04-28 19:15:30 +03001682 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001683
1684 if ((c->rex_prefix & 0x8) != 0x0)
1685 usermode = X86EMUL_MODE_PROT64;
1686 else
1687 usermode = X86EMUL_MODE_PROT32;
1688
1689 cs.dpl = 3;
1690 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001691 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001692 switch (usermode) {
1693 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001694 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001695 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001696 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001697 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001698 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001699 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001700 break;
1701 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001702 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001703 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001704 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001705 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001706 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001707 ss_sel = cs_sel + 8;
1708 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001709 cs.l = 1;
1710 break;
1711 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001712 cs_sel |= SELECTOR_RPL_MASK;
1713 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001714
Gleb Natapov79168fd2010-04-28 19:15:30 +03001715 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1716 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1717 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1718 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001719
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001720 c->eip = c->regs[VCPU_REGS_RDX];
1721 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001722
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001723 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001724}
1725
Gleb Natapov9c537242010-03-18 15:20:05 +02001726static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1727 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001728{
1729 int iopl;
1730 if (ctxt->mode == X86EMUL_MODE_REAL)
1731 return false;
1732 if (ctxt->mode == X86EMUL_MODE_VM86)
1733 return true;
1734 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001735 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001736}
1737
1738static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1739 struct x86_emulate_ops *ops,
1740 u16 port, u16 len)
1741{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001742 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001743 int r;
1744 u16 io_bitmap_ptr;
1745 u8 perm, bit_idx = port & 0x7;
1746 unsigned mask = (1 << len) - 1;
1747
Gleb Natapov79168fd2010-04-28 19:15:30 +03001748 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1749 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001750 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001751 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001752 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001753 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1754 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001755 if (r != X86EMUL_CONTINUE)
1756 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001757 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001758 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001759 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1760 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001761 if (r != X86EMUL_CONTINUE)
1762 return false;
1763 if ((perm >> bit_idx) & mask)
1764 return false;
1765 return true;
1766}
1767
1768static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1769 struct x86_emulate_ops *ops,
1770 u16 port, u16 len)
1771{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001772 if (ctxt->perm_ok)
1773 return true;
1774
Gleb Natapov9c537242010-03-18 15:20:05 +02001775 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001776 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1777 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001778
1779 ctxt->perm_ok = true;
1780
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001781 return true;
1782}
1783
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001784static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1785 struct x86_emulate_ops *ops,
1786 struct tss_segment_16 *tss)
1787{
1788 struct decode_cache *c = &ctxt->decode;
1789
1790 tss->ip = c->eip;
1791 tss->flag = ctxt->eflags;
1792 tss->ax = c->regs[VCPU_REGS_RAX];
1793 tss->cx = c->regs[VCPU_REGS_RCX];
1794 tss->dx = c->regs[VCPU_REGS_RDX];
1795 tss->bx = c->regs[VCPU_REGS_RBX];
1796 tss->sp = c->regs[VCPU_REGS_RSP];
1797 tss->bp = c->regs[VCPU_REGS_RBP];
1798 tss->si = c->regs[VCPU_REGS_RSI];
1799 tss->di = c->regs[VCPU_REGS_RDI];
1800
1801 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1802 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1803 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1804 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1805 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1806}
1807
1808static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1809 struct x86_emulate_ops *ops,
1810 struct tss_segment_16 *tss)
1811{
1812 struct decode_cache *c = &ctxt->decode;
1813 int ret;
1814
1815 c->eip = tss->ip;
1816 ctxt->eflags = tss->flag | 2;
1817 c->regs[VCPU_REGS_RAX] = tss->ax;
1818 c->regs[VCPU_REGS_RCX] = tss->cx;
1819 c->regs[VCPU_REGS_RDX] = tss->dx;
1820 c->regs[VCPU_REGS_RBX] = tss->bx;
1821 c->regs[VCPU_REGS_RSP] = tss->sp;
1822 c->regs[VCPU_REGS_RBP] = tss->bp;
1823 c->regs[VCPU_REGS_RSI] = tss->si;
1824 c->regs[VCPU_REGS_RDI] = tss->di;
1825
1826 /*
1827 * SDM says that segment selectors are loaded before segment
1828 * descriptors
1829 */
1830 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1831 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1832 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1833 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1834 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1835
1836 /*
1837 * Now load segment descriptors. If fault happenes at this stage
1838 * it is handled in a context of new task
1839 */
1840 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1841 if (ret != X86EMUL_CONTINUE)
1842 return ret;
1843 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1844 if (ret != X86EMUL_CONTINUE)
1845 return ret;
1846 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1847 if (ret != X86EMUL_CONTINUE)
1848 return ret;
1849 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1850 if (ret != X86EMUL_CONTINUE)
1851 return ret;
1852 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1853 if (ret != X86EMUL_CONTINUE)
1854 return ret;
1855
1856 return X86EMUL_CONTINUE;
1857}
1858
1859static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1860 struct x86_emulate_ops *ops,
1861 u16 tss_selector, u16 old_tss_sel,
1862 ulong old_tss_base, struct desc_struct *new_desc)
1863{
1864 struct tss_segment_16 tss_seg;
1865 int ret;
1866 u32 err, new_tss_base = get_desc_base(new_desc);
1867
1868 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1869 &err);
1870 if (ret == X86EMUL_PROPAGATE_FAULT) {
1871 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001872 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001873 return ret;
1874 }
1875
1876 save_state_to_tss16(ctxt, ops, &tss_seg);
1877
1878 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1879 &err);
1880 if (ret == X86EMUL_PROPAGATE_FAULT) {
1881 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001882 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001883 return ret;
1884 }
1885
1886 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1887 &err);
1888 if (ret == X86EMUL_PROPAGATE_FAULT) {
1889 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001890 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001891 return ret;
1892 }
1893
1894 if (old_tss_sel != 0xffff) {
1895 tss_seg.prev_task_link = old_tss_sel;
1896
1897 ret = ops->write_std(new_tss_base,
1898 &tss_seg.prev_task_link,
1899 sizeof tss_seg.prev_task_link,
1900 ctxt->vcpu, &err);
1901 if (ret == X86EMUL_PROPAGATE_FAULT) {
1902 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001903 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001904 return ret;
1905 }
1906 }
1907
1908 return load_state_from_tss16(ctxt, ops, &tss_seg);
1909}
1910
1911static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1912 struct x86_emulate_ops *ops,
1913 struct tss_segment_32 *tss)
1914{
1915 struct decode_cache *c = &ctxt->decode;
1916
1917 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1918 tss->eip = c->eip;
1919 tss->eflags = ctxt->eflags;
1920 tss->eax = c->regs[VCPU_REGS_RAX];
1921 tss->ecx = c->regs[VCPU_REGS_RCX];
1922 tss->edx = c->regs[VCPU_REGS_RDX];
1923 tss->ebx = c->regs[VCPU_REGS_RBX];
1924 tss->esp = c->regs[VCPU_REGS_RSP];
1925 tss->ebp = c->regs[VCPU_REGS_RBP];
1926 tss->esi = c->regs[VCPU_REGS_RSI];
1927 tss->edi = c->regs[VCPU_REGS_RDI];
1928
1929 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1930 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1931 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1932 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1933 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1934 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1935 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1936}
1937
1938static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1939 struct x86_emulate_ops *ops,
1940 struct tss_segment_32 *tss)
1941{
1942 struct decode_cache *c = &ctxt->decode;
1943 int ret;
1944
Gleb Natapov0f122442010-04-28 19:15:31 +03001945 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001946 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001947 return X86EMUL_PROPAGATE_FAULT;
1948 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001949 c->eip = tss->eip;
1950 ctxt->eflags = tss->eflags | 2;
1951 c->regs[VCPU_REGS_RAX] = tss->eax;
1952 c->regs[VCPU_REGS_RCX] = tss->ecx;
1953 c->regs[VCPU_REGS_RDX] = tss->edx;
1954 c->regs[VCPU_REGS_RBX] = tss->ebx;
1955 c->regs[VCPU_REGS_RSP] = tss->esp;
1956 c->regs[VCPU_REGS_RBP] = tss->ebp;
1957 c->regs[VCPU_REGS_RSI] = tss->esi;
1958 c->regs[VCPU_REGS_RDI] = tss->edi;
1959
1960 /*
1961 * SDM says that segment selectors are loaded before segment
1962 * descriptors
1963 */
1964 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1965 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1966 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1967 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1968 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1969 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1970 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1971
1972 /*
1973 * Now load segment descriptors. If fault happenes at this stage
1974 * it is handled in a context of new task
1975 */
1976 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1977 if (ret != X86EMUL_CONTINUE)
1978 return ret;
1979 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1980 if (ret != X86EMUL_CONTINUE)
1981 return ret;
1982 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1983 if (ret != X86EMUL_CONTINUE)
1984 return ret;
1985 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1986 if (ret != X86EMUL_CONTINUE)
1987 return ret;
1988 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1989 if (ret != X86EMUL_CONTINUE)
1990 return ret;
1991 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1992 if (ret != X86EMUL_CONTINUE)
1993 return ret;
1994 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1995 if (ret != X86EMUL_CONTINUE)
1996 return ret;
1997
1998 return X86EMUL_CONTINUE;
1999}
2000
2001static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2002 struct x86_emulate_ops *ops,
2003 u16 tss_selector, u16 old_tss_sel,
2004 ulong old_tss_base, struct desc_struct *new_desc)
2005{
2006 struct tss_segment_32 tss_seg;
2007 int ret;
2008 u32 err, new_tss_base = get_desc_base(new_desc);
2009
2010 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2011 &err);
2012 if (ret == X86EMUL_PROPAGATE_FAULT) {
2013 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002014 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002015 return ret;
2016 }
2017
2018 save_state_to_tss32(ctxt, ops, &tss_seg);
2019
2020 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2021 &err);
2022 if (ret == X86EMUL_PROPAGATE_FAULT) {
2023 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002024 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002025 return ret;
2026 }
2027
2028 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2029 &err);
2030 if (ret == X86EMUL_PROPAGATE_FAULT) {
2031 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002032 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002033 return ret;
2034 }
2035
2036 if (old_tss_sel != 0xffff) {
2037 tss_seg.prev_task_link = old_tss_sel;
2038
2039 ret = ops->write_std(new_tss_base,
2040 &tss_seg.prev_task_link,
2041 sizeof tss_seg.prev_task_link,
2042 ctxt->vcpu, &err);
2043 if (ret == X86EMUL_PROPAGATE_FAULT) {
2044 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002045 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002046 return ret;
2047 }
2048 }
2049
2050 return load_state_from_tss32(ctxt, ops, &tss_seg);
2051}
2052
2053static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002054 struct x86_emulate_ops *ops,
2055 u16 tss_selector, int reason,
2056 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002057{
2058 struct desc_struct curr_tss_desc, next_tss_desc;
2059 int ret;
2060 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2061 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002062 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002063 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002064
2065 /* FIXME: old_tss_base == ~0 ? */
2066
2067 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2068 if (ret != X86EMUL_CONTINUE)
2069 return ret;
2070 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2071 if (ret != X86EMUL_CONTINUE)
2072 return ret;
2073
2074 /* FIXME: check that next_tss_desc is tss */
2075
2076 if (reason != TASK_SWITCH_IRET) {
2077 if ((tss_selector & 3) > next_tss_desc.dpl ||
2078 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002079 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002080 return X86EMUL_PROPAGATE_FAULT;
2081 }
2082 }
2083
Gleb Natapovceffb452010-03-18 15:20:19 +02002084 desc_limit = desc_limit_scaled(&next_tss_desc);
2085 if (!next_tss_desc.p ||
2086 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2087 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002088 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002089 return X86EMUL_PROPAGATE_FAULT;
2090 }
2091
2092 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2093 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2094 write_segment_descriptor(ctxt, ops, old_tss_sel,
2095 &curr_tss_desc);
2096 }
2097
2098 if (reason == TASK_SWITCH_IRET)
2099 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2100
2101 /* set back link to prev task only if NT bit is set in eflags
2102 note that old_tss_sel is not used afetr this point */
2103 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2104 old_tss_sel = 0xffff;
2105
2106 if (next_tss_desc.type & 8)
2107 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2108 old_tss_base, &next_tss_desc);
2109 else
2110 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2111 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002112 if (ret != X86EMUL_CONTINUE)
2113 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002114
2115 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2116 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2117
2118 if (reason != TASK_SWITCH_IRET) {
2119 next_tss_desc.type |= (1 << 1); /* set busy flag */
2120 write_segment_descriptor(ctxt, ops, tss_selector,
2121 &next_tss_desc);
2122 }
2123
2124 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2125 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2126 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2127
Jan Kiszkae269fb22010-04-14 15:51:09 +02002128 if (has_error_code) {
2129 struct decode_cache *c = &ctxt->decode;
2130
2131 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2132 c->lock_prefix = 0;
2133 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002134 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002135 }
2136
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002137 return ret;
2138}
2139
2140int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002141 u16 tss_selector, int reason,
2142 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002143{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002144 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002145 struct decode_cache *c = &ctxt->decode;
2146 int rc;
2147
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002149 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002150
Jan Kiszkae269fb22010-04-14 15:51:09 +02002151 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2152 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153
2154 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002155 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002156 if (rc == X86EMUL_CONTINUE)
2157 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002158 }
2159
Gleb Natapov19d04432010-04-15 12:29:50 +03002160 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002161}
2162
Gleb Natapova682e352010-03-18 15:20:21 +02002163static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002164 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002165{
2166 struct decode_cache *c = &ctxt->decode;
2167 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2168
Gleb Natapovd9271122010-03-18 15:20:22 +02002169 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002170 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002171}
2172
Avi Kivity63540382010-07-29 15:11:55 +03002173static int em_push(struct x86_emulate_ctxt *ctxt)
2174{
2175 emulate_push(ctxt, ctxt->ops);
2176 return X86EMUL_CONTINUE;
2177}
2178
Avi Kivity7af04fc2010-08-18 14:16:35 +03002179static int em_das(struct x86_emulate_ctxt *ctxt)
2180{
2181 struct decode_cache *c = &ctxt->decode;
2182 u8 al, old_al;
2183 bool af, cf, old_cf;
2184
2185 cf = ctxt->eflags & X86_EFLAGS_CF;
2186 al = c->dst.val;
2187
2188 old_al = al;
2189 old_cf = cf;
2190 cf = false;
2191 af = ctxt->eflags & X86_EFLAGS_AF;
2192 if ((al & 0x0f) > 9 || af) {
2193 al -= 6;
2194 cf = old_cf | (al >= 250);
2195 af = true;
2196 } else {
2197 af = false;
2198 }
2199 if (old_al > 0x99 || old_cf) {
2200 al -= 0x60;
2201 cf = true;
2202 }
2203
2204 c->dst.val = al;
2205 /* Set PF, ZF, SF */
2206 c->src.type = OP_IMM;
2207 c->src.val = 0;
2208 c->src.bytes = 1;
2209 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2210 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2211 if (cf)
2212 ctxt->eflags |= X86_EFLAGS_CF;
2213 if (af)
2214 ctxt->eflags |= X86_EFLAGS_AF;
2215 return X86EMUL_CONTINUE;
2216}
2217
Avi Kivity0ef753b2010-08-18 14:51:45 +03002218static int em_call_far(struct x86_emulate_ctxt *ctxt)
2219{
2220 struct decode_cache *c = &ctxt->decode;
2221 u16 sel, old_cs;
2222 ulong old_eip;
2223 int rc;
2224
2225 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2226 old_eip = c->eip;
2227
2228 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2229 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2230 return X86EMUL_CONTINUE;
2231
2232 c->eip = 0;
2233 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2234
2235 c->src.val = old_cs;
2236 emulate_push(ctxt, ctxt->ops);
2237 rc = writeback(ctxt, ctxt->ops);
2238 if (rc != X86EMUL_CONTINUE)
2239 return rc;
2240
2241 c->src.val = old_eip;
2242 emulate_push(ctxt, ctxt->ops);
2243 rc = writeback(ctxt, ctxt->ops);
2244 if (rc != X86EMUL_CONTINUE)
2245 return rc;
2246
2247 c->dst.type = OP_NONE;
2248
2249 return X86EMUL_CONTINUE;
2250}
2251
Avi Kivity40ece7c2010-08-18 15:12:09 +03002252static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2253{
2254 struct decode_cache *c = &ctxt->decode;
2255 int rc;
2256
2257 c->dst.type = OP_REG;
2258 c->dst.addr.reg = &c->eip;
2259 c->dst.bytes = c->op_bytes;
2260 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2261 if (rc != X86EMUL_CONTINUE)
2262 return rc;
2263 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2264 return X86EMUL_CONTINUE;
2265}
2266
Avi Kivity5c82aa22010-08-18 18:31:43 +03002267static int em_imul(struct x86_emulate_ctxt *ctxt)
2268{
2269 struct decode_cache *c = &ctxt->decode;
2270
2271 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2272 return X86EMUL_CONTINUE;
2273}
2274
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002275static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2276{
2277 struct decode_cache *c = &ctxt->decode;
2278
2279 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002280 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002281}
2282
Avi Kivity61429142010-08-19 15:13:00 +03002283static int em_cwd(struct x86_emulate_ctxt *ctxt)
2284{
2285 struct decode_cache *c = &ctxt->decode;
2286
2287 c->dst.type = OP_REG;
2288 c->dst.bytes = c->src.bytes;
2289 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2290 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2291
2292 return X86EMUL_CONTINUE;
2293}
2294
Avi Kivity48bb5d32010-08-18 18:54:34 +03002295static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2296{
2297 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2298 struct decode_cache *c = &ctxt->decode;
2299 u64 tsc = 0;
2300
2301 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) {
2302 emulate_gp(ctxt, 0);
2303 return X86EMUL_PROPAGATE_FAULT;
2304 }
2305 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2306 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2307 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2308 return X86EMUL_CONTINUE;
2309}
2310
Avi Kivity73fba5f2010-07-29 15:11:53 +03002311#define D(_y) { .flags = (_y) }
2312#define N D(0)
2313#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2314#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2315#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2316
2317static struct opcode group1[] = {
2318 X7(D(Lock)), N
2319};
2320
2321static struct opcode group1A[] = {
2322 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2323};
2324
2325static struct opcode group3[] = {
2326 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2327 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002328 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002329};
2330
2331static struct opcode group4[] = {
2332 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2333 N, N, N, N, N, N,
2334};
2335
2336static struct opcode group5[] = {
2337 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002338 D(SrcMem | ModRM | Stack),
2339 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002340 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2341 D(SrcMem | ModRM | Stack), N,
2342};
2343
2344static struct group_dual group7 = { {
2345 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2346 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002347 D(SrcMem16 | ModRM | Mov | Priv),
2348 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002349}, {
2350 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2351 D(SrcNone | ModRM | DstMem | Mov), N,
2352 D(SrcMem16 | ModRM | Mov | Priv), N,
2353} };
2354
2355static struct opcode group8[] = {
2356 N, N, N, N,
2357 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2358 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2359};
2360
2361static struct group_dual group9 = { {
2362 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2363}, {
2364 N, N, N, N, N, N, N, N,
2365} };
2366
2367static struct opcode opcode_table[256] = {
2368 /* 0x00 - 0x07 */
2369 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2370 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2371 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2372 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2373 /* 0x08 - 0x0F */
2374 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2375 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2376 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2377 D(ImplicitOps | Stack | No64), N,
2378 /* 0x10 - 0x17 */
2379 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2380 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2381 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2382 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2383 /* 0x18 - 0x1F */
2384 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2385 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2386 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2387 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2388 /* 0x20 - 0x27 */
2389 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2390 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2391 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2392 /* 0x28 - 0x2F */
2393 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2394 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
Avi Kivity7af04fc2010-08-18 14:16:35 +03002395 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm),
2396 N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002397 /* 0x30 - 0x37 */
2398 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2399 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2400 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2401 /* 0x38 - 0x3F */
2402 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2403 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2404 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2405 N, N,
2406 /* 0x40 - 0x4F */
2407 X16(D(DstReg)),
2408 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002409 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002410 /* 0x58 - 0x5F */
2411 X8(D(DstReg | Stack)),
2412 /* 0x60 - 0x67 */
2413 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2414 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2415 N, N, N, N,
2416 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002417 I(SrcImm | Mov | Stack, em_push),
2418 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002419 I(SrcImmByte | Mov | Stack, em_push),
2420 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002421 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2422 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2423 /* 0x70 - 0x7F */
2424 X16(D(SrcImmByte)),
2425 /* 0x80 - 0x87 */
2426 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2427 G(DstMem | SrcImm | ModRM | Group, group1),
2428 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2429 G(DstMem | SrcImmByte | ModRM | Group, group1),
2430 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2431 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2432 /* 0x88 - 0x8F */
2433 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2434 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002435 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002436 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2437 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002438 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002439 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002440 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
2441 D(SrcImmFAddr | No64), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002442 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2443 /* 0xA0 - 0xA7 */
2444 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2445 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2446 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2447 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2448 /* 0xA8 - 0xAF */
Wei Yongjun06cb7042010-08-04 15:36:53 +08002449 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm),
2450 D(ByteOp | SrcAcc | DstDI | Mov | String), D(SrcAcc | DstDI | Mov | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002451 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
Avi Kivityf6b33fc2010-08-17 11:20:37 +03002452 D(ByteOp | SrcAcc | DstDI | String), D(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002453 /* 0xB0 - 0xB7 */
2454 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2455 /* 0xB8 - 0xBF */
2456 X8(D(DstReg | SrcImm | Mov)),
2457 /* 0xC0 - 0xC7 */
2458 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002459 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2460 D(ImplicitOps | Stack),
2461 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002462 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2463 /* 0xC8 - 0xCF */
2464 N, N, N, D(ImplicitOps | Stack),
2465 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2466 /* 0xD0 - 0xD7 */
Wei Yongjunc034da82010-08-04 15:38:59 +08002467 D(ByteOp | DstMem | SrcOne | ModRM), D(DstMem | SrcOne | ModRM),
Avi Kivity7077aec2010-08-18 18:53:43 +03002468 D(ByteOp | DstMem | ModRM), D(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002469 N, N, N, N,
2470 /* 0xD8 - 0xDF */
2471 N, N, N, N, N, N, N, N,
2472 /* 0xE0 - 0xE7 */
Wei Yongjunf2f31842010-08-18 16:38:21 +08002473 X3(D(SrcImmByte)), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002474 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
Wei Yongjun41167be2010-08-06 11:45:12 +08002475 D(ByteOp | SrcAcc | DstImmUByte), D(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002476 /* 0xE8 - 0xEF */
2477 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2478 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2479 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
Wei Yongjun41167be2010-08-06 11:45:12 +08002480 D(ByteOp | SrcAcc | ImplicitOps), D(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002481 /* 0xF0 - 0xF7 */
2482 N, N, N, N,
2483 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2484 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002485 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002486 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2487};
2488
2489static struct opcode twobyte_table[256] = {
2490 /* 0x00 - 0x0F */
2491 N, GD(0, &group7), N, N,
2492 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2493 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2494 N, D(ImplicitOps | ModRM), N, N,
2495 /* 0x10 - 0x1F */
2496 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2497 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002498 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2499 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002500 N, N, N, N,
2501 N, N, N, N, N, N, N, N,
2502 /* 0x30 - 0x3F */
Avi Kivity48bb5d32010-08-18 18:54:34 +03002503 D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc),
2504 D(ImplicitOps | Priv), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002505 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2506 N, N, N, N, N, N, N, N,
2507 /* 0x40 - 0x4F */
2508 X16(D(DstReg | SrcMem | ModRM | Mov)),
2509 /* 0x50 - 0x5F */
2510 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2511 /* 0x60 - 0x6F */
2512 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2513 /* 0x70 - 0x7F */
2514 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2515 /* 0x80 - 0x8F */
2516 X16(D(SrcImm)),
2517 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002518 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002519 /* 0xA0 - 0xA7 */
2520 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2521 N, D(DstMem | SrcReg | ModRM | BitOp),
2522 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2523 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2524 /* 0xA8 - 0xAF */
2525 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2526 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2527 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2528 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002529 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002530 /* 0xB0 - 0xB7 */
2531 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2532 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2533 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2534 D(DstReg | SrcMem16 | ModRM | Mov),
2535 /* 0xB8 - 0xBF */
2536 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002537 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002538 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2539 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002540 /* 0xC0 - 0xCF */
Wei Yongjun92f738a2010-08-17 09:19:34 +08002541 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2542 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002543 N, N, N, GD(0, &group9),
2544 N, N, N, N, N, N, N, N,
2545 /* 0xD0 - 0xDF */
2546 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2547 /* 0xE0 - 0xEF */
2548 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2549 /* 0xF0 - 0xFF */
2550 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2551};
2552
2553#undef D
2554#undef N
2555#undef G
2556#undef GD
2557#undef I
2558
Avi Kivity39f21ee2010-08-18 19:20:21 +03002559static unsigned imm_size(struct decode_cache *c)
2560{
2561 unsigned size;
2562
2563 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2564 if (size == 8)
2565 size = 4;
2566 return size;
2567}
2568
2569static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2570 unsigned size, bool sign_extension)
2571{
2572 struct decode_cache *c = &ctxt->decode;
2573 struct x86_emulate_ops *ops = ctxt->ops;
2574 int rc = X86EMUL_CONTINUE;
2575
2576 op->type = OP_IMM;
2577 op->bytes = size;
2578 op->addr.mem = c->eip;
2579 /* NB. Immediates are sign-extended as necessary. */
2580 switch (op->bytes) {
2581 case 1:
2582 op->val = insn_fetch(s8, 1, c->eip);
2583 break;
2584 case 2:
2585 op->val = insn_fetch(s16, 2, c->eip);
2586 break;
2587 case 4:
2588 op->val = insn_fetch(s32, 4, c->eip);
2589 break;
2590 }
2591 if (!sign_extension) {
2592 switch (op->bytes) {
2593 case 1:
2594 op->val &= 0xff;
2595 break;
2596 case 2:
2597 op->val &= 0xffff;
2598 break;
2599 case 4:
2600 op->val &= 0xffffffff;
2601 break;
2602 }
2603 }
2604done:
2605 return rc;
2606}
2607
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002608int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002609x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2610{
2611 struct x86_emulate_ops *ops = ctxt->ops;
2612 struct decode_cache *c = &ctxt->decode;
2613 int rc = X86EMUL_CONTINUE;
2614 int mode = ctxt->mode;
2615 int def_op_bytes, def_ad_bytes, dual, goffset;
2616 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002617 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002618
2619 /* we cannot decode insn before we complete previous rep insn */
2620 WARN_ON(ctxt->restart);
2621
2622 c->eip = ctxt->eip;
2623 c->fetch.start = c->fetch.end = c->eip;
2624 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2625
2626 switch (mode) {
2627 case X86EMUL_MODE_REAL:
2628 case X86EMUL_MODE_VM86:
2629 case X86EMUL_MODE_PROT16:
2630 def_op_bytes = def_ad_bytes = 2;
2631 break;
2632 case X86EMUL_MODE_PROT32:
2633 def_op_bytes = def_ad_bytes = 4;
2634 break;
2635#ifdef CONFIG_X86_64
2636 case X86EMUL_MODE_PROT64:
2637 def_op_bytes = 4;
2638 def_ad_bytes = 8;
2639 break;
2640#endif
2641 default:
2642 return -1;
2643 }
2644
2645 c->op_bytes = def_op_bytes;
2646 c->ad_bytes = def_ad_bytes;
2647
2648 /* Legacy prefixes. */
2649 for (;;) {
2650 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2651 case 0x66: /* operand-size override */
2652 /* switch between 2/4 bytes */
2653 c->op_bytes = def_op_bytes ^ 6;
2654 break;
2655 case 0x67: /* address-size override */
2656 if (mode == X86EMUL_MODE_PROT64)
2657 /* switch between 4/8 bytes */
2658 c->ad_bytes = def_ad_bytes ^ 12;
2659 else
2660 /* switch between 2/4 bytes */
2661 c->ad_bytes = def_ad_bytes ^ 6;
2662 break;
2663 case 0x26: /* ES override */
2664 case 0x2e: /* CS override */
2665 case 0x36: /* SS override */
2666 case 0x3e: /* DS override */
2667 set_seg_override(c, (c->b >> 3) & 3);
2668 break;
2669 case 0x64: /* FS override */
2670 case 0x65: /* GS override */
2671 set_seg_override(c, c->b & 7);
2672 break;
2673 case 0x40 ... 0x4f: /* REX */
2674 if (mode != X86EMUL_MODE_PROT64)
2675 goto done_prefixes;
2676 c->rex_prefix = c->b;
2677 continue;
2678 case 0xf0: /* LOCK */
2679 c->lock_prefix = 1;
2680 break;
2681 case 0xf2: /* REPNE/REPNZ */
2682 c->rep_prefix = REPNE_PREFIX;
2683 break;
2684 case 0xf3: /* REP/REPE/REPZ */
2685 c->rep_prefix = REPE_PREFIX;
2686 break;
2687 default:
2688 goto done_prefixes;
2689 }
2690
2691 /* Any legacy prefix after a REX prefix nullifies its effect. */
2692
2693 c->rex_prefix = 0;
2694 }
2695
2696done_prefixes:
2697
2698 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002699 if (c->rex_prefix & 8)
2700 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002701
2702 /* Opcode byte(s). */
2703 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002704 /* Two-byte opcode? */
2705 if (c->b == 0x0f) {
2706 c->twobyte = 1;
2707 c->b = insn_fetch(u8, 1, c->eip);
2708 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002709 }
2710 c->d = opcode.flags;
2711
2712 if (c->d & Group) {
2713 dual = c->d & GroupDual;
2714 c->modrm = insn_fetch(u8, 1, c->eip);
2715 --c->eip;
2716
2717 if (c->d & GroupDual) {
2718 g_mod012 = opcode.u.gdual->mod012;
2719 g_mod3 = opcode.u.gdual->mod3;
2720 } else
2721 g_mod012 = g_mod3 = opcode.u.group;
2722
2723 c->d &= ~(Group | GroupDual);
2724
2725 goffset = (c->modrm >> 3) & 7;
2726
2727 if ((c->modrm >> 6) == 3)
2728 opcode = g_mod3[goffset];
2729 else
2730 opcode = g_mod012[goffset];
2731 c->d |= opcode.flags;
2732 }
2733
2734 c->execute = opcode.u.execute;
2735
2736 /* Unrecognised? */
2737 if (c->d == 0 || (c->d & Undefined)) {
2738 DPRINTF("Cannot emulate %02x\n", c->b);
2739 return -1;
2740 }
2741
2742 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2743 c->op_bytes = 8;
2744
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002745 if (c->d & Op3264) {
2746 if (mode == X86EMUL_MODE_PROT64)
2747 c->op_bytes = 8;
2748 else
2749 c->op_bytes = 4;
2750 }
2751
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002752 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002753 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002754 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002755 if (!c->has_seg_override)
2756 set_seg_override(c, c->modrm_seg);
2757 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002758 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002759 if (rc != X86EMUL_CONTINUE)
2760 goto done;
2761
2762 if (!c->has_seg_override)
2763 set_seg_override(c, VCPU_SREG_DS);
2764
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002765 if (memop.type == OP_MEM && !(!c->twobyte && c->b == 0x8d))
2766 memop.addr.mem += seg_override_base(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002767
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002768 if (memop.type == OP_MEM && c->ad_bytes != 8)
2769 memop.addr.mem = (u32)memop.addr.mem;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002770
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002771 if (memop.type == OP_MEM && c->rip_relative)
2772 memop.addr.mem += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002773
2774 /*
2775 * Decode and fetch the source operand: register, memory
2776 * or immediate.
2777 */
2778 switch (c->d & SrcMask) {
2779 case SrcNone:
2780 break;
2781 case SrcReg:
2782 decode_register_operand(&c->src, c, 0);
2783 break;
2784 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002785 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002786 goto srcmem_common;
2787 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002788 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002789 goto srcmem_common;
2790 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002791 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002792 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002793 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002794 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002795 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002796 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002797 rc = decode_imm(ctxt, &c->src, 2, false);
2798 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002799 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002800 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2801 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002802 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002803 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002804 break;
2805 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002806 rc = decode_imm(ctxt, &c->src, 1, true);
2807 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002808 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002809 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002810 break;
2811 case SrcAcc:
2812 c->src.type = OP_REG;
2813 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002814 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002815 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002816 break;
2817 case SrcOne:
2818 c->src.bytes = 1;
2819 c->src.val = 1;
2820 break;
2821 case SrcSI:
2822 c->src.type = OP_MEM;
2823 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002824 c->src.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002825 register_address(c, seg_override_base(ctxt, ops, c),
2826 c->regs[VCPU_REGS_RSI]);
2827 c->src.val = 0;
2828 break;
2829 case SrcImmFAddr:
2830 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002831 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002832 c->src.bytes = c->op_bytes + 2;
2833 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2834 break;
2835 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002836 memop.bytes = c->op_bytes + 2;
2837 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002838 break;
2839 }
2840
Avi Kivity39f21ee2010-08-18 19:20:21 +03002841 if (rc != X86EMUL_CONTINUE)
2842 goto done;
2843
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002844 /*
2845 * Decode and fetch the second source operand: register, memory
2846 * or immediate.
2847 */
2848 switch (c->d & Src2Mask) {
2849 case Src2None:
2850 break;
2851 case Src2CL:
2852 c->src2.bytes = 1;
2853 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2854 break;
2855 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002856 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002857 break;
2858 case Src2One:
2859 c->src2.bytes = 1;
2860 c->src2.val = 1;
2861 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03002862 case Src2Imm:
2863 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
2864 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002865 }
2866
Avi Kivity39f21ee2010-08-18 19:20:21 +03002867 if (rc != X86EMUL_CONTINUE)
2868 goto done;
2869
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002870 /* Decode and fetch the destination operand: register or memory. */
2871 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002872 case DstReg:
2873 decode_register_operand(&c->dst, c,
2874 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2875 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08002876 case DstImmUByte:
2877 c->dst.type = OP_IMM;
2878 c->dst.addr.mem = c->eip;
2879 c->dst.bytes = 1;
2880 c->dst.val = insn_fetch(u8, 1, c->eip);
2881 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002882 case DstMem:
2883 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002884 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002885 if ((c->d & DstMask) == DstMem64)
2886 c->dst.bytes = 8;
2887 else
2888 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002889 if (c->d & BitOp)
2890 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002891 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002892 break;
2893 case DstAcc:
2894 c->dst.type = OP_REG;
2895 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002896 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002897 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002898 c->dst.orig_val = c->dst.val;
2899 break;
2900 case DstDI:
2901 c->dst.type = OP_MEM;
2902 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002903 c->dst.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002904 register_address(c, es_base(ctxt, ops),
2905 c->regs[VCPU_REGS_RDI]);
2906 c->dst.val = 0;
2907 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002908 case ImplicitOps:
2909 /* Special instructions do their own operand decoding. */
2910 default:
2911 c->dst.type = OP_NONE; /* Disable writeback. */
2912 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002913 }
2914
2915done:
2916 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2917}
2918
2919int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002920x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002921{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002922 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002923 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002924 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002925 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002926 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002927 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002928
Gleb Natapov9de41572010-04-28 19:15:22 +03002929 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002930
Gleb Natapov11616242010-02-11 14:43:14 +02002931 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002932 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002933 goto done;
2934 }
2935
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002936 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002937 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002938 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002939 goto done;
2940 }
2941
Gleb Natapove92805a2010-02-10 14:21:35 +02002942 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002943 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002944 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002945 goto done;
2946 }
2947
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002948 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002949 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002950 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002951 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002952 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002953 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002954 goto done;
2955 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002956 }
2957
Wei Yongjunc483c022010-08-06 15:36:36 +08002958 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002959 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002960 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002961 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002962 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002963 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002964 }
2965
Gleb Natapove35b7b92010-02-25 16:36:42 +02002966 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002967 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002968 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002969 if (rc != X86EMUL_CONTINUE)
2970 goto done;
2971 }
2972
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002973 if ((c->d & DstMask) == ImplicitOps)
2974 goto special_insn;
2975
2976
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002977 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2978 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002979 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002980 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002981 if (rc != X86EMUL_CONTINUE)
2982 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002983 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002984 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002985
Avi Kivity018a98d2007-11-27 19:30:56 +02002986special_insn:
2987
Avi Kivityef65c882010-07-29 15:11:51 +03002988 if (c->execute) {
2989 rc = c->execute(ctxt);
2990 if (rc != X86EMUL_CONTINUE)
2991 goto done;
2992 goto writeback;
2993 }
2994
Laurent Viviere4e03de2007-09-18 11:52:50 +02002995 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996 goto twobyte_insn;
2997
Laurent Viviere4e03de2007-09-18 11:52:50 +02002998 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 case 0x00 ... 0x05:
3000 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003001 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003003 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003004 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003005 break;
3006 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003007 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003008 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003009 goto done;
3010 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011 case 0x08 ... 0x0d:
3012 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003013 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003015 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003016 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003017 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018 case 0x10 ... 0x15:
3019 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003020 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003022 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003023 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003024 break;
3025 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003026 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003027 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003028 goto done;
3029 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 case 0x18 ... 0x1d:
3031 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003032 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003034 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003035 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003036 break;
3037 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003038 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003039 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003040 goto done;
3041 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003042 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003044 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045 break;
3046 case 0x28 ... 0x2d:
3047 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003048 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049 break;
3050 case 0x30 ... 0x35:
3051 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003052 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 break;
3054 case 0x38 ... 0x3d:
3055 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003056 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003058 case 0x40 ... 0x47: /* inc r16/r32 */
3059 emulate_1op("inc", c->dst, ctxt->eflags);
3060 break;
3061 case 0x48 ... 0x4f: /* dec r16/r32 */
3062 emulate_1op("dec", c->dst, ctxt->eflags);
3063 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003064 case 0x58 ... 0x5f: /* pop reg */
3065 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003066 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003067 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02003068 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02003069 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003070 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003071 rc = emulate_pusha(ctxt, ops);
3072 if (rc != X86EMUL_CONTINUE)
3073 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003074 break;
3075 case 0x61: /* popa */
3076 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003077 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003078 goto done;
3079 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003081 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003083 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003085 case 0x6c: /* insb */
3086 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003087 c->src.val = c->regs[VCPU_REGS_RDX];
3088 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003089 case 0x6e: /* outsb */
3090 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003091 c->dst.val = c->regs[VCPU_REGS_RDX];
3092 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003093 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003094 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003095 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003096 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003097 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003099 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 case 0:
3101 goto add;
3102 case 1:
3103 goto or;
3104 case 2:
3105 goto adc;
3106 case 3:
3107 goto sbb;
3108 case 4:
3109 goto and;
3110 case 5:
3111 goto sub;
3112 case 6:
3113 goto xor;
3114 case 7:
3115 goto cmp;
3116 }
3117 break;
3118 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003119 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003120 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 break;
3122 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003123 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003125 c->src.val = c->dst.val;
3126 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 /*
3128 * Write back the memory destination with implicit LOCK
3129 * prefix.
3130 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003131 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003132 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03003135 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003136 case 0x8c: /* mov r/m, sreg */
3137 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003138 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003139 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003140 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003141 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003142 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003143 case 0x8d: /* lea r16/r32, m */
Avi Kivity342fc632010-08-01 15:13:22 +03003144 c->dst.val = c->src.addr.mem;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003145 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003146 case 0x8e: { /* mov seg, r/m16 */
3147 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003148
3149 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003150
Gleb Natapovc6975182010-02-18 12:15:01 +02003151 if (c->modrm_reg == VCPU_SREG_CS ||
3152 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003153 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003154 goto done;
3155 }
3156
Glauber Costa310b5d32009-05-12 16:21:06 -04003157 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003158 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003159
Gleb Natapov2e873022010-03-18 15:20:18 +02003160 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003161
3162 c->dst.type = OP_NONE; /* Disable writeback. */
3163 break;
3164 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003166 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003167 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003170 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3171 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003172 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003173 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003174 case 0x98: /* cbw/cwde/cdqe */
3175 switch (c->op_bytes) {
3176 case 2: c->dst.val = (s8)c->dst.val; break;
3177 case 4: c->dst.val = (s16)c->dst.val; break;
3178 case 8: c->dst.val = (s32)c->dst.val; break;
3179 }
3180 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003181 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003182 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003183 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003184 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003185 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003186 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003187 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003188 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003189 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
3190 if (rc != X86EMUL_CONTINUE)
3191 goto done;
3192 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08003193 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02003195 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003197 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003198 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02003199 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003200 case 0xa8 ... 0xa9: /* test ax, imm */
3201 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 case 0xaa ... 0xab: /* stos */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02003204 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003206 goto cmp;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03003207 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02003208 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02003209 case 0xc0 ... 0xc1:
3210 emulate_grp2(ctxt);
3211 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003212 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003213 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003214 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003215 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003216 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02003217 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
3218 mov:
3219 c->dst.val = c->src.val;
3220 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003221 case 0xcb: /* ret far */
3222 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003223 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003224 goto done;
3225 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003226 case 0xcc: /* int3 */
3227 irq = 3;
3228 goto do_interrupt;
3229 case 0xcd: /* int n */
3230 irq = c->src.val;
3231 do_interrupt:
3232 rc = emulate_int(ctxt, ops, irq);
3233 if (rc != X86EMUL_CONTINUE)
3234 goto done;
3235 break;
3236 case 0xce: /* into */
3237 if (ctxt->eflags & EFLG_OF) {
3238 irq = 4;
3239 goto do_interrupt;
3240 }
3241 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003242 case 0xcf: /* iret */
3243 rc = emulate_iret(ctxt, ops);
3244
3245 if (rc != X86EMUL_CONTINUE)
3246 goto done;
3247 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003248 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003249 emulate_grp2(ctxt);
3250 break;
3251 case 0xd2 ... 0xd3: /* Grp2 */
3252 c->src.val = c->regs[VCPU_REGS_RCX];
3253 emulate_grp2(ctxt);
3254 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003255 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3256 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3257 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3258 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3259 jmp_rel(c, c->src.val);
3260 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003261 case 0xe4: /* inb */
3262 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003263 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003264 case 0xe6: /* outb */
3265 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003266 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003267 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003268 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003269 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003270 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003271 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003272 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003273 }
3274 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003275 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003276 case 0xea: { /* jmp far */
3277 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003278 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003279 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3280
3281 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003282 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003283
Gleb Natapov414e6272010-04-28 19:15:26 +03003284 c->eip = 0;
3285 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003286 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003287 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003288 case 0xeb:
3289 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003290 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003291 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003292 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003293 case 0xec: /* in al,dx */
3294 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003295 c->src.val = c->regs[VCPU_REGS_RDX];
3296 do_io_in:
3297 c->dst.bytes = min(c->dst.bytes, 4u);
3298 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003299 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003300 goto done;
3301 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003302 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3303 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003304 goto done; /* IO is needed */
3305 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003306 case 0xee: /* out dx,al */
3307 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003308 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003309 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003310 c->src.bytes = min(c->src.bytes, 4u);
3311 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3312 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003313 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003314 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003315 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003316 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3317 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003318 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003319 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003320 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003321 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003322 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003323 case 0xf5: /* cmc */
3324 /* complement carry flag from eflags reg */
3325 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003326 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003327 case 0xf6 ... 0xf7: /* Grp3 */
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03003328 if (emulate_grp3(ctxt, ops) != X86EMUL_CONTINUE)
Gleb Natapovaca06a82010-03-18 15:20:15 +02003329 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003330 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003331 case 0xf8: /* clc */
3332 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003333 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003334 case 0xf9: /* stc */
3335 ctxt->eflags |= EFLG_CF;
3336 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003337 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003338 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003339 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003340 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003341 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003342 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003343 break;
3344 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003345 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003346 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003347 goto done;
3348 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003349 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003350 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003351 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003352 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003353 case 0xfc: /* cld */
3354 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003355 break;
3356 case 0xfd: /* std */
3357 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003358 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003359 case 0xfe: /* Grp4 */
3360 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003361 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003362 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003363 goto done;
3364 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003365 case 0xff: /* Grp5 */
3366 if (c->modrm_reg == 5)
3367 goto jump_far;
3368 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003369 default:
3370 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003372
3373writeback:
3374 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003375 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003376 goto done;
3377
Gleb Natapov5cd21912010-03-18 15:20:26 +02003378 /*
3379 * restore dst type in case the decoding will be reused
3380 * (happens for string instruction )
3381 */
3382 c->dst.type = saved_dst_type;
3383
Gleb Natapova682e352010-03-18 15:20:21 +02003384 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003385 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3386 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003387
3388 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003389 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3390 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003391
Gleb Natapov5cd21912010-03-18 15:20:26 +02003392 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003393 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003394 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003395 /* The second termination condition only applies for REPE
3396 * and REPNE. Test if the repeat string operation prefix is
3397 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3398 * corresponding termination condition according to:
3399 * - if REPE/REPZ and ZF = 0 then done
3400 * - if REPNE/REPNZ and ZF = 1 then done
3401 */
3402 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3403 (c->b == 0xae) || (c->b == 0xaf))
3404 && (((c->rep_prefix == REPE_PREFIX) &&
3405 ((ctxt->eflags & EFLG_ZF) == 0))
3406 || ((c->rep_prefix == REPNE_PREFIX) &&
3407 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3408 ctxt->restart = false;
Gleb Natapov7b262e92010-03-18 15:20:27 +02003409 /*
3410 * Re-enter guest when pio read ahead buffer is empty or,
3411 * if it is not used, after each 1024 iteration.
3412 */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003413 else if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3414 (rc->end != 0 && rc->end == rc->pos)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02003415 ctxt->restart = false;
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003416 c->eip = ctxt->eip;
3417 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003418 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003419 /*
3420 * reset read cache here in case string instruction is restared
3421 * without decoding
3422 */
3423 ctxt->decode.mem_read.end = 0;
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003424 if (!ctxt->restart)
3425 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003426
3427done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003428 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429
3430twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003431 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003433 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 u16 size;
3435 unsigned long address;
3436
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003437 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003438 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003439 goto cannot_emulate;
3440
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003441 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003442 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003443 goto done;
3444
Avi Kivity33e38852008-05-21 15:34:25 +03003445 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003446 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003447 /* Disable writeback. */
3448 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003449 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003450 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003451 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003452 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003453 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454 goto done;
3455 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003456 /* Disable writeback. */
3457 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003459 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003460 if (c->modrm_mod == 3) {
3461 switch (c->modrm_rm) {
3462 case 1:
3463 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003464 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003465 goto done;
3466 break;
3467 default:
3468 goto cannot_emulate;
3469 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003470 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003471 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003472 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003473 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003474 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003475 goto done;
3476 realmode_lidt(ctxt->vcpu, size, address);
3477 }
Avi Kivity16286d02008-04-14 14:40:50 +03003478 /* Disable writeback. */
3479 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480 break;
3481 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003482 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003483 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484 break;
3485 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003486 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003487 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003488 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003490 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003491 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003492 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 case 7: /* invlpg*/
Avi Kivity1f6f0582010-08-01 15:19:22 +03003494 emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
Avi Kivity16286d02008-04-14 14:40:50 +03003495 /* Disable writeback. */
3496 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497 break;
3498 default:
3499 goto cannot_emulate;
3500 }
3501 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003502 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003503 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003504 if (rc != X86EMUL_CONTINUE)
3505 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003506 else
3507 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003508 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003509 case 0x06:
3510 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003511 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003512 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003513 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003514 break;
3515 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003516 case 0x0d: /* GrpP (prefetch) */
3517 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003518 break;
3519 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003520 switch (c->modrm_reg) {
3521 case 1:
3522 case 5 ... 7:
3523 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003524 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003525 goto done;
3526 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003527 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003528 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003530 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3531 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003532 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003533 goto done;
3534 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003535 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003537 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003538 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003539 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003540 goto done;
3541 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003542 c->dst.type = OP_NONE;
3543 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003545 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3546 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003547 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003548 goto done;
3549 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003550
Avi Kivityb27f3852010-08-01 14:25:22 +03003551 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003552 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3553 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3554 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003555 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003556 goto done;
3557 }
3558
Laurent Viviera01af5e2007-09-24 11:10:56 +02003559 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003560 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003561 case 0x30:
3562 /* wrmsr */
3563 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3564 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003565 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003566 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003567 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003568 }
3569 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003570 break;
3571 case 0x32:
3572 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003573 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003574 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003575 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003576 } else {
3577 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3578 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3579 }
3580 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003581 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003582 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003583 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003584 if (rc != X86EMUL_CONTINUE)
3585 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003586 else
3587 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003588 break;
3589 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003590 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003591 if (rc != X86EMUL_CONTINUE)
3592 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003593 else
3594 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003595 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003596 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003597 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003598 if (!test_cc(c->b, ctxt->eflags))
3599 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003601 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003602 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003603 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003604 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003605 case 0x90 ... 0x9f: /* setcc r/m8 */
3606 c->dst.val = test_cc(c->b, ctxt->eflags);
3607 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003608 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003609 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003610 break;
3611 case 0xa1: /* pop fs */
3612 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003613 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003614 goto done;
3615 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003616 case 0xa3:
3617 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003618 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003619 /* only subword offset */
3620 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003621 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003622 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003623 case 0xa4: /* shld imm8, r, r/m */
3624 case 0xa5: /* shld cl, r, r/m */
3625 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3626 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003627 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003628 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003629 break;
3630 case 0xa9: /* pop gs */
3631 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003632 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003633 goto done;
3634 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003635 case 0xab:
3636 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003637 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003638 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003639 case 0xac: /* shrd imm8, r, r/m */
3640 case 0xad: /* shrd cl, r, r/m */
3641 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3642 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003643 case 0xae: /* clflush */
3644 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645 case 0xb0 ... 0xb1: /* cmpxchg */
3646 /*
3647 * Save real source value, then compare EAX against
3648 * destination.
3649 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003650 c->src.orig_val = c->src.val;
3651 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003652 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3653 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003655 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656 } else {
3657 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003658 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003659 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660 }
3661 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003662 case 0xb3:
3663 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003664 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003665 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003667 c->dst.bytes = c->op_bytes;
3668 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3669 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003672 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673 case 0:
3674 goto bt;
3675 case 1:
3676 goto bts;
3677 case 2:
3678 goto btr;
3679 case 3:
3680 goto btc;
3681 }
3682 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003683 case 0xbb:
3684 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003685 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003686 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003687 case 0xbc: { /* bsf */
3688 u8 zf;
3689 __asm__ ("bsf %2, %0; setz %1"
3690 : "=r"(c->dst.val), "=q"(zf)
3691 : "r"(c->src.val));
3692 ctxt->eflags &= ~X86_EFLAGS_ZF;
3693 if (zf) {
3694 ctxt->eflags |= X86_EFLAGS_ZF;
3695 c->dst.type = OP_NONE; /* Disable writeback. */
3696 }
3697 break;
3698 }
3699 case 0xbd: { /* bsr */
3700 u8 zf;
3701 __asm__ ("bsr %2, %0; setz %1"
3702 : "=r"(c->dst.val), "=q"(zf)
3703 : "r"(c->src.val));
3704 ctxt->eflags &= ~X86_EFLAGS_ZF;
3705 if (zf) {
3706 ctxt->eflags |= X86_EFLAGS_ZF;
3707 c->dst.type = OP_NONE; /* Disable writeback. */
3708 }
3709 break;
3710 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003712 c->dst.bytes = c->op_bytes;
3713 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3714 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003716 case 0xc0 ... 0xc1: /* xadd */
3717 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3718 /* Write back the register source. */
3719 c->src.val = c->dst.orig_val;
3720 write_register_operand(&c->src);
3721 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003722 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003723 c->dst.bytes = c->op_bytes;
3724 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3725 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003726 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003728 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003729 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003730 goto done;
3731 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003732 default:
3733 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 }
3735 goto writeback;
3736
3737cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003738 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003739 return -1;
3740}