blob: fcce7aeacc84024d830988b72691d09ec0aeaf79 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Avi Kivityab85b12b2010-07-29 15:11:49 +030050#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080051/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define SrcNone (0<<4) /* No source operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcReg (1<<4) /* Register operand. */
54#define SrcMem (2<<4) /* Memory operand. */
55#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
57#define SrcImm (5<<4) /* Immediate operand. */
58#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010059#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030060#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030061#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020062#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030063#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
64#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080065#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030066#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020076#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Avi Kivity0d7cdee2011-03-29 11:34:38 +020078#define Prefix (1<<16) /* Instruction varies with 66/f2/f3 prefix */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030079/* Misc flags */
Avi Kivityd8671622011-02-01 16:32:03 +020080#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030081#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030082#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030083#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020084#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020085#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030086#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010087/* Source 2 operand type */
88#define Src2None (0<<29)
89#define Src2CL (1<<29)
90#define Src2ImmByte (2<<29)
91#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030092#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010093#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080094
Avi Kivityd0e53322010-07-29 15:11:54 +030095#define X2(x...) x, x
96#define X3(x...) X2(x), x
97#define X4(x...) X2(x), X2(x)
98#define X5(x...) X4(x), x
99#define X6(x...) X4(x), X2(x)
100#define X7(x...) X4(x), X3(x)
101#define X8(x...) X4(x), X4(x)
102#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300103
Avi Kivityd65b1de2010-07-29 15:11:35 +0300104struct opcode {
105 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300106 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300107 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300108 struct opcode *group;
109 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200110 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300111 } u;
112};
113
114struct group_dual {
115 struct opcode mod012[8];
116 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300117};
118
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200119struct gprefix {
120 struct opcode pfx_no;
121 struct opcode pfx_66;
122 struct opcode pfx_f2;
123 struct opcode pfx_f3;
124};
125
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200127#define EFLG_ID (1<<21)
128#define EFLG_VIP (1<<20)
129#define EFLG_VIF (1<<19)
130#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200131#define EFLG_VM (1<<17)
132#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200133#define EFLG_IOPL (3<<12)
134#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135#define EFLG_OF (1<<11)
136#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200137#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200138#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139#define EFLG_SF (1<<7)
140#define EFLG_ZF (1<<6)
141#define EFLG_AF (1<<4)
142#define EFLG_PF (1<<2)
143#define EFLG_CF (1<<0)
144
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300145#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
146#define EFLG_RESERVED_ONE_MASK 2
147
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148/*
149 * Instruction emulation:
150 * Most instructions are emulated directly via a fragment of inline assembly
151 * code. This allows us to save/restore EFLAGS and thus very easily pick up
152 * any modified flags.
153 */
154
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800155#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156#define _LO32 "k" /* force 32-bit operand */
157#define _STK "%%rsp" /* stack pointer */
158#elif defined(__i386__)
159#define _LO32 "" /* force 32-bit operand */
160#define _STK "%%esp" /* stack pointer */
161#endif
162
163/*
164 * These EFLAGS bits are restored from saved value during emulation, and
165 * any changes are written back to the saved value after emulation.
166 */
167#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
168
169/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200170#define _PRE_EFLAGS(_sav, _msk, _tmp) \
171 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
172 "movl %"_sav",%"_LO32 _tmp"; " \
173 "push %"_tmp"; " \
174 "push %"_tmp"; " \
175 "movl %"_msk",%"_LO32 _tmp"; " \
176 "andl %"_LO32 _tmp",("_STK"); " \
177 "pushf; " \
178 "notl %"_LO32 _tmp"; " \
179 "andl %"_LO32 _tmp",("_STK"); " \
180 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
181 "pop %"_tmp"; " \
182 "orl %"_LO32 _tmp",("_STK"); " \
183 "popf; " \
184 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185
186/* After executing instruction: write-back necessary bits in EFLAGS. */
187#define _POST_EFLAGS(_sav, _msk, _tmp) \
188 /* _sav |= EFLAGS & _msk; */ \
189 "pushf; " \
190 "pop %"_tmp"; " \
191 "andl %"_msk",%"_LO32 _tmp"; " \
192 "orl %"_LO32 _tmp",%"_sav"; "
193
Avi Kivitydda96d82008-11-26 15:14:10 +0200194#ifdef CONFIG_X86_64
195#define ON64(x) x
196#else
197#define ON64(x)
198#endif
199
Avi Kivityb3b3d252010-08-16 17:49:52 +0300200#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200201 do { \
202 __asm__ __volatile__ ( \
203 _PRE_EFLAGS("0", "4", "2") \
204 _op _suffix " %"_x"3,%1; " \
205 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300206 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200207 "=&r" (_tmp) \
208 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200209 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200210
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212/* Raw emulation: instruction has two explicit operands. */
213#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200214 do { \
215 unsigned long _tmp; \
216 \
217 switch ((_dst).bytes) { \
218 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300219 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200220 break; \
221 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300222 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200223 break; \
224 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300225 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200226 break; \
227 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 } while (0)
229
230#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
231 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200232 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400233 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300235 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236 break; \
237 default: \
238 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
239 _wx, _wy, _lx, _ly, _qx, _qy); \
240 break; \
241 } \
242 } while (0)
243
244/* Source operand is byte-sized and may be restricted to just %cl. */
245#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
246 __emulate_2op(_op, _src, _dst, _eflags, \
247 "b", "c", "b", "c", "b", "c", "b", "c")
248
249/* Source operand is byte, word, long or quad sized. */
250#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
251 __emulate_2op(_op, _src, _dst, _eflags, \
252 "b", "q", "w", "r", _LO32, "r", "", "r")
253
254/* Source operand is word, long or quad sized. */
255#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
256 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
257 "w", "r", _LO32, "r", "", "r")
258
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100259/* Instruction has three operands and one operand is stored in ECX register */
260#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
261 do { \
262 unsigned long _tmp; \
263 _type _clv = (_cl).val; \
264 _type _srcv = (_src).val; \
265 _type _dstv = (_dst).val; \
266 \
267 __asm__ __volatile__ ( \
268 _PRE_EFLAGS("0", "5", "2") \
269 _op _suffix " %4,%1 \n" \
270 _POST_EFLAGS("0", "5", "2") \
271 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
272 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
273 ); \
274 \
275 (_cl).val = (unsigned long) _clv; \
276 (_src).val = (unsigned long) _srcv; \
277 (_dst).val = (unsigned long) _dstv; \
278 } while (0)
279
280#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
281 do { \
282 switch ((_dst).bytes) { \
283 case 2: \
284 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
285 "w", unsigned short); \
286 break; \
287 case 4: \
288 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
289 "l", unsigned int); \
290 break; \
291 case 8: \
292 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
293 "q", unsigned long)); \
294 break; \
295 } \
296 } while (0)
297
Avi Kivitydda96d82008-11-26 15:14:10 +0200298#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299 do { \
300 unsigned long _tmp; \
301 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200302 __asm__ __volatile__ ( \
303 _PRE_EFLAGS("0", "3", "2") \
304 _op _suffix " %1; " \
305 _POST_EFLAGS("0", "3", "2") \
306 : "=m" (_eflags), "+m" ((_dst).val), \
307 "=&r" (_tmp) \
308 : "i" (EFLAGS_MASK)); \
309 } while (0)
310
311/* Instruction has only one explicit operand (no source operand). */
312#define emulate_1op(_op, _dst, _eflags) \
313 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400314 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200315 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
316 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
317 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
318 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319 } \
320 } while (0)
321
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300322#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
323 do { \
324 unsigned long _tmp; \
325 \
326 __asm__ __volatile__ ( \
327 _PRE_EFLAGS("0", "4", "1") \
328 _op _suffix " %5; " \
329 _POST_EFLAGS("0", "4", "1") \
330 : "=m" (_eflags), "=&r" (_tmp), \
331 "+a" (_rax), "+d" (_rdx) \
332 : "i" (EFLAGS_MASK), "m" ((_src).val), \
333 "a" (_rax), "d" (_rdx)); \
334 } while (0)
335
Avi Kivityf6b35972010-08-26 11:59:00 +0300336#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
337 do { \
338 unsigned long _tmp; \
339 \
340 __asm__ __volatile__ ( \
341 _PRE_EFLAGS("0", "5", "1") \
342 "1: \n\t" \
343 _op _suffix " %6; " \
344 "2: \n\t" \
345 _POST_EFLAGS("0", "5", "1") \
346 ".pushsection .fixup,\"ax\" \n\t" \
347 "3: movb $1, %4 \n\t" \
348 "jmp 2b \n\t" \
349 ".popsection \n\t" \
350 _ASM_EXTABLE(1b, 3b) \
351 : "=m" (_eflags), "=&r" (_tmp), \
352 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
353 : "i" (EFLAGS_MASK), "m" ((_src).val), \
354 "a" (_rax), "d" (_rdx)); \
355 } while (0)
356
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300357/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
358#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
359 do { \
360 switch((_src).bytes) { \
361 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
362 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
363 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
364 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
365 } \
366 } while (0)
367
Avi Kivityf6b35972010-08-26 11:59:00 +0300368#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
369 do { \
370 switch((_src).bytes) { \
371 case 1: \
372 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
373 _eflags, "b", _ex); \
374 break; \
375 case 2: \
376 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
377 _eflags, "w", _ex); \
378 break; \
379 case 4: \
380 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
381 _eflags, "l", _ex); \
382 break; \
383 case 8: ON64( \
384 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
385 _eflags, "q", _ex)); \
386 break; \
387 } \
388 } while (0)
389
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390/* Fetch next part of the instruction being emulated. */
391#define insn_fetch(_type, _size, _eip) \
392({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200393 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200394 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 goto done; \
396 (_eip) += (_size); \
397 (_type)_x; \
398})
399
Gleb Natapov414e6272010-04-28 19:15:26 +0300400#define insn_fetch_arr(_arr, _size, _eip) \
401({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
402 if (rc != X86EMUL_CONTINUE) \
403 goto done; \
404 (_eip) += (_size); \
405})
406
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800407static inline unsigned long ad_mask(struct decode_cache *c)
408{
409 return (1UL << (c->ad_bytes << 3)) - 1;
410}
411
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800413static inline unsigned long
414address_mask(struct decode_cache *c, unsigned long reg)
415{
416 if (c->ad_bytes == sizeof(unsigned long))
417 return reg;
418 else
419 return reg & ad_mask(c);
420}
421
422static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200423register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800424{
Avi Kivity90de84f2010-11-17 15:28:21 +0200425 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800426}
427
Harvey Harrison7a9572752008-02-19 07:40:41 -0800428static inline void
429register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
430{
431 if (c->ad_bytes == sizeof(unsigned long))
432 *reg += inc;
433 else
434 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
435}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436
Harvey Harrison7a9572752008-02-19 07:40:41 -0800437static inline void jmp_rel(struct decode_cache *c, int rel)
438{
439 register_address_increment(c, &c->eip, rel);
440}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300441
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300442static void set_seg_override(struct decode_cache *c, int seg)
443{
444 c->has_seg_override = true;
445 c->seg_override = seg;
446}
447
Gleb Natapov79168fd2010-04-28 19:15:30 +0300448static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
449 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300450{
451 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
452 return 0;
453
Gleb Natapov79168fd2010-04-28 19:15:30 +0300454 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300455}
456
Avi Kivity90de84f2010-11-17 15:28:21 +0200457static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
458 struct x86_emulate_ops *ops,
459 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300460{
461 if (!c->has_seg_override)
462 return 0;
463
Avi Kivity90de84f2010-11-17 15:28:21 +0200464 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300465}
466
Avi Kivity90de84f2010-11-17 15:28:21 +0200467static ulong linear(struct x86_emulate_ctxt *ctxt,
468 struct segmented_address addr)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300469{
Avi Kivity90de84f2010-11-17 15:28:21 +0200470 struct decode_cache *c = &ctxt->decode;
471 ulong la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300472
Avi Kivity90de84f2010-11-17 15:28:21 +0200473 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
474 if (c->ad_bytes != 8)
475 la &= (u32)-1;
476 return la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300477}
478
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200479static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
480 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300481{
Avi Kivityda9cb572010-11-22 17:53:21 +0200482 ctxt->exception.vector = vec;
483 ctxt->exception.error_code = error;
484 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200485 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300486}
487
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200488static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300489{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200490 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300491}
492
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200493static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300494{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200495 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300496}
497
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200498static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300499{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200500 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300501}
502
Avi Kivity34d1f492010-08-26 11:59:01 +0300503static int emulate_de(struct x86_emulate_ctxt *ctxt)
504{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200505 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300506}
507
Avi Kivity62266862007-11-20 13:15:52 +0200508static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
509 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300510 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200511{
512 struct fetch_cache *fc = &ctxt->decode.fetch;
513 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300514 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200515
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300516 if (eip == fc->end) {
517 cur_size = fc->end - fc->start;
518 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
519 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200520 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900521 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200522 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300523 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200524 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300525 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900526 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200527}
528
529static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
530 struct x86_emulate_ops *ops,
531 unsigned long eip, void *dest, unsigned size)
532{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900533 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200534
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200535 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200536 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200537 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200538 while (size--) {
539 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900540 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200541 return rc;
542 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900543 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200544}
545
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000546/*
547 * Given the 'reg' portion of a ModRM byte, and a register block, return a
548 * pointer into the block that addresses the relevant register.
549 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
550 */
551static void *decode_register(u8 modrm_reg, unsigned long *regs,
552 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553{
554 void *p;
555
556 p = &regs[modrm_reg];
557 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
558 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
559 return p;
560}
561
562static int read_descriptor(struct x86_emulate_ctxt *ctxt,
563 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200564 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 u16 *size, unsigned long *address, int op_bytes)
566{
567 int rc;
568
569 if (op_bytes == 2)
570 op_bytes = 3;
571 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200572 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200573 ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900574 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200576 addr.ea += 2;
577 rc = ops->read_std(linear(ctxt, addr), address, op_bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200578 ctxt->vcpu, &ctxt->exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 return rc;
580}
581
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300582static int test_cc(unsigned int condition, unsigned int flags)
583{
584 int rc = 0;
585
586 switch ((condition & 15) >> 1) {
587 case 0: /* o */
588 rc |= (flags & EFLG_OF);
589 break;
590 case 1: /* b/c/nae */
591 rc |= (flags & EFLG_CF);
592 break;
593 case 2: /* z/e */
594 rc |= (flags & EFLG_ZF);
595 break;
596 case 3: /* be/na */
597 rc |= (flags & (EFLG_CF|EFLG_ZF));
598 break;
599 case 4: /* s */
600 rc |= (flags & EFLG_SF);
601 break;
602 case 5: /* p/pe */
603 rc |= (flags & EFLG_PF);
604 break;
605 case 7: /* le/ng */
606 rc |= (flags & EFLG_ZF);
607 /* fall through */
608 case 6: /* l/nge */
609 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
610 break;
611 }
612
613 /* Odd condition identifiers (lsb == 1) have inverted sense. */
614 return (!!rc ^ (condition & 1));
615}
616
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300617static void fetch_register_operand(struct operand *op)
618{
619 switch (op->bytes) {
620 case 1:
621 op->val = *(u8 *)op->addr.reg;
622 break;
623 case 2:
624 op->val = *(u16 *)op->addr.reg;
625 break;
626 case 4:
627 op->val = *(u32 *)op->addr.reg;
628 break;
629 case 8:
630 op->val = *(u64 *)op->addr.reg;
631 break;
632 }
633}
634
Avi Kivity3c118e22007-10-31 10:27:04 +0200635static void decode_register_operand(struct operand *op,
636 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200637 int inhibit_bytereg)
638{
Avi Kivity33615aa2007-10-31 11:15:56 +0200639 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200640 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200641
642 if (!(c->d & ModRM))
643 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200644 op->type = OP_REG;
645 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300646 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200647 op->bytes = 1;
648 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300649 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200650 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200651 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300652 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200653 op->orig_val = op->val;
654}
655
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200656static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300657 struct x86_emulate_ops *ops,
658 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200659{
660 struct decode_cache *c = &ctxt->decode;
661 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700662 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900663 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300664 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200665
666 if (c->rex_prefix) {
667 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
668 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
669 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
670 }
671
672 c->modrm = insn_fetch(u8, 1, c->eip);
673 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
674 c->modrm_reg |= (c->modrm & 0x38) >> 3;
675 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300676 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200677
678 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300679 op->type = OP_REG;
680 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
681 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300682 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300683 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200684 return rc;
685 }
686
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300687 op->type = OP_MEM;
688
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200689 if (c->ad_bytes == 2) {
690 unsigned bx = c->regs[VCPU_REGS_RBX];
691 unsigned bp = c->regs[VCPU_REGS_RBP];
692 unsigned si = c->regs[VCPU_REGS_RSI];
693 unsigned di = c->regs[VCPU_REGS_RDI];
694
695 /* 16-bit ModR/M decode. */
696 switch (c->modrm_mod) {
697 case 0:
698 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300699 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200700 break;
701 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300702 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200703 break;
704 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300705 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200706 break;
707 }
708 switch (c->modrm_rm) {
709 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300710 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200711 break;
712 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300713 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200714 break;
715 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300716 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200717 break;
718 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300719 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720 break;
721 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300722 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200723 break;
724 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300725 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200726 break;
727 case 6:
728 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300729 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200730 break;
731 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300732 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200733 break;
734 }
735 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
736 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300737 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300738 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200739 } else {
740 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700741 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200742 sib = insn_fetch(u8, 1, c->eip);
743 index_reg |= (sib >> 3) & 7;
744 base_reg |= sib & 7;
745 scale = sib >> 6;
746
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700747 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300748 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700749 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300750 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700751 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300752 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700753 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
754 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700755 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700756 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300757 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200758 switch (c->modrm_mod) {
759 case 0:
760 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300761 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200762 break;
763 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300764 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200765 break;
766 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300767 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200768 break;
769 }
770 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200771 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200772done:
773 return rc;
774}
775
776static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300777 struct x86_emulate_ops *ops,
778 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200779{
780 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900781 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200782
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300783 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200784 switch (c->ad_bytes) {
785 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200786 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200787 break;
788 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200789 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200790 break;
791 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200792 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200793 break;
794 }
795done:
796 return rc;
797}
798
Wei Yongjun35c843c2010-08-09 11:34:56 +0800799static void fetch_bit_operand(struct decode_cache *c)
800{
Sheng Yang7129eec2010-09-28 16:33:32 +0800801 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800802
Wei Yongjun3885f182010-08-09 11:37:37 +0800803 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800804 mask = ~(c->dst.bytes * 8 - 1);
805
806 if (c->src.bytes == 2)
807 sv = (s16)c->src.val & (s16)mask;
808 else if (c->src.bytes == 4)
809 sv = (s32)c->src.val & (s32)mask;
810
Avi Kivity90de84f2010-11-17 15:28:21 +0200811 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800812 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800813
814 /* only subword offset */
815 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800816}
817
Gleb Natapov9de41572010-04-28 19:15:22 +0300818static int read_emulated(struct x86_emulate_ctxt *ctxt,
819 struct x86_emulate_ops *ops,
820 unsigned long addr, void *dest, unsigned size)
821{
822 int rc;
823 struct read_cache *mc = &ctxt->decode.mem_read;
824
825 while (size) {
826 int n = min(size, 8u);
827 size -= n;
828 if (mc->pos < mc->end)
829 goto read_cached;
830
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200831 rc = ops->read_emulated(addr, mc->data + mc->end, n,
832 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +0300833 if (rc != X86EMUL_CONTINUE)
834 return rc;
835 mc->end += n;
836
837 read_cached:
838 memcpy(dest, mc->data + mc->pos, n);
839 mc->pos += n;
840 dest += n;
841 addr += n;
842 }
843 return X86EMUL_CONTINUE;
844}
845
Gleb Natapov7b262e92010-03-18 15:20:27 +0200846static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
847 struct x86_emulate_ops *ops,
848 unsigned int size, unsigned short port,
849 void *dest)
850{
851 struct read_cache *rc = &ctxt->decode.io_read;
852
853 if (rc->pos == rc->end) { /* refill pio read ahead */
854 struct decode_cache *c = &ctxt->decode;
855 unsigned int in_page, n;
856 unsigned int count = c->rep_prefix ?
857 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
858 in_page = (ctxt->eflags & EFLG_DF) ?
859 offset_in_page(c->regs[VCPU_REGS_RDI]) :
860 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
861 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
862 count);
863 if (n == 0)
864 n = 1;
865 rc->pos = rc->end = 0;
866 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
867 return 0;
868 rc->end = n * size;
869 }
870
871 memcpy(dest, rc->data + rc->pos, size);
872 rc->pos += size;
873 return 1;
874}
875
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200876static u32 desc_limit_scaled(struct desc_struct *desc)
877{
878 u32 limit = get_desc_limit(desc);
879
880 return desc->g ? (limit << 12) | 0xfff : limit;
881}
882
883static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
884 struct x86_emulate_ops *ops,
885 u16 selector, struct desc_ptr *dt)
886{
887 if (selector & 1 << 2) {
888 struct desc_struct desc;
889 memset (dt, 0, sizeof *dt);
Gleb Natapov5601d052011-03-07 14:55:06 +0200890 if (!ops->get_cached_descriptor(&desc, NULL, VCPU_SREG_LDTR,
891 ctxt->vcpu))
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200892 return;
893
894 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
895 dt->address = get_desc_base(&desc);
896 } else
897 ops->get_gdt(dt, ctxt->vcpu);
898}
899
900/* allowed just for 8 bytes segments */
901static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
902 struct x86_emulate_ops *ops,
903 u16 selector, struct desc_struct *desc)
904{
905 struct desc_ptr dt;
906 u16 index = selector >> 3;
907 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200908 ulong addr;
909
910 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
911
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200912 if (dt.size < index * 8 + 7)
913 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200914 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200915 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
916 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200917
918 return ret;
919}
920
921/* allowed just for 8 bytes segments */
922static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
923 struct x86_emulate_ops *ops,
924 u16 selector, struct desc_struct *desc)
925{
926 struct desc_ptr dt;
927 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200928 ulong addr;
929 int ret;
930
931 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
932
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200933 if (dt.size < index * 8 + 7)
934 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200935
936 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200937 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
938 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200939
940 return ret;
941}
942
Gleb Natapov5601d052011-03-07 14:55:06 +0200943/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200944static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
945 struct x86_emulate_ops *ops,
946 u16 selector, int seg)
947{
948 struct desc_struct seg_desc;
949 u8 dpl, rpl, cpl;
950 unsigned err_vec = GP_VECTOR;
951 u32 err_code = 0;
952 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
953 int ret;
954
955 memset(&seg_desc, 0, sizeof seg_desc);
956
957 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
958 || ctxt->mode == X86EMUL_MODE_REAL) {
959 /* set real mode segment descriptor */
960 set_desc_base(&seg_desc, selector << 4);
961 set_desc_limit(&seg_desc, 0xffff);
962 seg_desc.type = 3;
963 seg_desc.p = 1;
964 seg_desc.s = 1;
965 goto load;
966 }
967
968 /* NULL selector is not valid for TR, CS and SS */
969 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
970 && null_selector)
971 goto exception;
972
973 /* TR should be in GDT only */
974 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
975 goto exception;
976
977 if (null_selector) /* for NULL selector skip all following checks */
978 goto load;
979
980 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
981 if (ret != X86EMUL_CONTINUE)
982 return ret;
983
984 err_code = selector & 0xfffc;
985 err_vec = GP_VECTOR;
986
987 /* can't load system descriptor into segment selecor */
988 if (seg <= VCPU_SREG_GS && !seg_desc.s)
989 goto exception;
990
991 if (!seg_desc.p) {
992 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
993 goto exception;
994 }
995
996 rpl = selector & 3;
997 dpl = seg_desc.dpl;
998 cpl = ops->cpl(ctxt->vcpu);
999
1000 switch (seg) {
1001 case VCPU_SREG_SS:
1002 /*
1003 * segment is not a writable data segment or segment
1004 * selector's RPL != CPL or segment selector's RPL != CPL
1005 */
1006 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1007 goto exception;
1008 break;
1009 case VCPU_SREG_CS:
1010 if (!(seg_desc.type & 8))
1011 goto exception;
1012
1013 if (seg_desc.type & 4) {
1014 /* conforming */
1015 if (dpl > cpl)
1016 goto exception;
1017 } else {
1018 /* nonconforming */
1019 if (rpl > cpl || dpl != cpl)
1020 goto exception;
1021 }
1022 /* CS(RPL) <- CPL */
1023 selector = (selector & 0xfffc) | cpl;
1024 break;
1025 case VCPU_SREG_TR:
1026 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1027 goto exception;
1028 break;
1029 case VCPU_SREG_LDTR:
1030 if (seg_desc.s || seg_desc.type != 2)
1031 goto exception;
1032 break;
1033 default: /* DS, ES, FS, or GS */
1034 /*
1035 * segment is not a data or readable code segment or
1036 * ((segment is a data or nonconforming code segment)
1037 * and (both RPL and CPL > DPL))
1038 */
1039 if ((seg_desc.type & 0xa) == 0x8 ||
1040 (((seg_desc.type & 0xc) != 0xc) &&
1041 (rpl > dpl && cpl > dpl)))
1042 goto exception;
1043 break;
1044 }
1045
1046 if (seg_desc.s) {
1047 /* mark segment as accessed */
1048 seg_desc.type |= 1;
1049 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1050 if (ret != X86EMUL_CONTINUE)
1051 return ret;
1052 }
1053load:
1054 ops->set_segment_selector(selector, seg, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001055 ops->set_cached_descriptor(&seg_desc, 0, seg, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001056 return X86EMUL_CONTINUE;
1057exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001058 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001059 return X86EMUL_PROPAGATE_FAULT;
1060}
1061
Wei Yongjun31be40b2010-08-17 09:17:30 +08001062static void write_register_operand(struct operand *op)
1063{
1064 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1065 switch (op->bytes) {
1066 case 1:
1067 *(u8 *)op->addr.reg = (u8)op->val;
1068 break;
1069 case 2:
1070 *(u16 *)op->addr.reg = (u16)op->val;
1071 break;
1072 case 4:
1073 *op->addr.reg = (u32)op->val;
1074 break; /* 64b: zero-extend */
1075 case 8:
1076 *op->addr.reg = op->val;
1077 break;
1078 }
1079}
1080
Wei Yongjunc37eda12010-06-15 09:03:33 +08001081static inline int writeback(struct x86_emulate_ctxt *ctxt,
1082 struct x86_emulate_ops *ops)
1083{
1084 int rc;
1085 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001086
1087 switch (c->dst.type) {
1088 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001089 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001090 break;
1091 case OP_MEM:
1092 if (c->lock_prefix)
1093 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001094 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001095 &c->dst.orig_val,
1096 &c->dst.val,
1097 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001098 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001099 ctxt->vcpu);
1100 else
1101 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001102 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001103 &c->dst.val,
1104 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001105 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001106 ctxt->vcpu);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001107 if (rc != X86EMUL_CONTINUE)
1108 return rc;
1109 break;
1110 case OP_NONE:
1111 /* no writeback */
1112 break;
1113 default:
1114 break;
1115 }
1116 return X86EMUL_CONTINUE;
1117}
1118
Gleb Natapov79168fd2010-04-28 19:15:30 +03001119static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1120 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001121{
1122 struct decode_cache *c = &ctxt->decode;
1123
1124 c->dst.type = OP_MEM;
1125 c->dst.bytes = c->op_bytes;
1126 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001127 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001128 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1129 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001130}
1131
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001132static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001133 struct x86_emulate_ops *ops,
1134 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001135{
1136 struct decode_cache *c = &ctxt->decode;
1137 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001138 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001139
Avi Kivity90de84f2010-11-17 15:28:21 +02001140 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1141 addr.seg = VCPU_SREG_SS;
1142 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001143 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001144 return rc;
1145
Avi Kivity350f69d2009-01-05 11:12:40 +02001146 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001147 return rc;
1148}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001149
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001150static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1151 struct x86_emulate_ops *ops,
1152 void *dest, int len)
1153{
1154 int rc;
1155 unsigned long val, change_mask;
1156 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001157 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001158
1159 rc = emulate_pop(ctxt, ops, &val, len);
1160 if (rc != X86EMUL_CONTINUE)
1161 return rc;
1162
1163 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1164 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1165
1166 switch(ctxt->mode) {
1167 case X86EMUL_MODE_PROT64:
1168 case X86EMUL_MODE_PROT32:
1169 case X86EMUL_MODE_PROT16:
1170 if (cpl == 0)
1171 change_mask |= EFLG_IOPL;
1172 if (cpl <= iopl)
1173 change_mask |= EFLG_IF;
1174 break;
1175 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001176 if (iopl < 3)
1177 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001178 change_mask |= EFLG_IF;
1179 break;
1180 default: /* real mode */
1181 change_mask |= (EFLG_IOPL | EFLG_IF);
1182 break;
1183 }
1184
1185 *(unsigned long *)dest =
1186 (ctxt->eflags & ~change_mask) | (val & change_mask);
1187
1188 return rc;
1189}
1190
Gleb Natapov79168fd2010-04-28 19:15:30 +03001191static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1192 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001193{
1194 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001195
Gleb Natapov79168fd2010-04-28 19:15:30 +03001196 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001197
Gleb Natapov79168fd2010-04-28 19:15:30 +03001198 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001199}
1200
1201static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1202 struct x86_emulate_ops *ops, int seg)
1203{
1204 struct decode_cache *c = &ctxt->decode;
1205 unsigned long selector;
1206 int rc;
1207
1208 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001209 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001210 return rc;
1211
Gleb Natapov2e873022010-03-18 15:20:18 +02001212 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001213 return rc;
1214}
1215
Wei Yongjunc37eda12010-06-15 09:03:33 +08001216static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001217 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001218{
1219 struct decode_cache *c = &ctxt->decode;
1220 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001221 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001222 int reg = VCPU_REGS_RAX;
1223
1224 while (reg <= VCPU_REGS_RDI) {
1225 (reg == VCPU_REGS_RSP) ?
1226 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1227
Gleb Natapov79168fd2010-04-28 19:15:30 +03001228 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001229
1230 rc = writeback(ctxt, ops);
1231 if (rc != X86EMUL_CONTINUE)
1232 return rc;
1233
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001234 ++reg;
1235 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001236
1237 /* Disable writeback. */
1238 c->dst.type = OP_NONE;
1239
1240 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001241}
1242
1243static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1244 struct x86_emulate_ops *ops)
1245{
1246 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001247 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001248 int reg = VCPU_REGS_RDI;
1249
1250 while (reg >= VCPU_REGS_RAX) {
1251 if (reg == VCPU_REGS_RSP) {
1252 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1253 c->op_bytes);
1254 --reg;
1255 }
1256
1257 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001258 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001259 break;
1260 --reg;
1261 }
1262 return rc;
1263}
1264
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001265int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1266 struct x86_emulate_ops *ops, int irq)
1267{
1268 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001269 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001270 struct desc_ptr dt;
1271 gva_t cs_addr;
1272 gva_t eip_addr;
1273 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001274
1275 /* TODO: Add limit checks */
1276 c->src.val = ctxt->eflags;
1277 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001278 rc = writeback(ctxt, ops);
1279 if (rc != X86EMUL_CONTINUE)
1280 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001281
1282 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1283
1284 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1285 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001286 rc = writeback(ctxt, ops);
1287 if (rc != X86EMUL_CONTINUE)
1288 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001289
1290 c->src.val = c->eip;
1291 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001292 rc = writeback(ctxt, ops);
1293 if (rc != X86EMUL_CONTINUE)
1294 return rc;
1295
1296 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001297
1298 ops->get_idt(&dt, ctxt->vcpu);
1299
1300 eip_addr = dt.address + (irq << 2);
1301 cs_addr = dt.address + (irq << 2) + 2;
1302
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001303 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001304 if (rc != X86EMUL_CONTINUE)
1305 return rc;
1306
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001307 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001308 if (rc != X86EMUL_CONTINUE)
1309 return rc;
1310
1311 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1312 if (rc != X86EMUL_CONTINUE)
1313 return rc;
1314
1315 c->eip = eip;
1316
1317 return rc;
1318}
1319
1320static int emulate_int(struct x86_emulate_ctxt *ctxt,
1321 struct x86_emulate_ops *ops, int irq)
1322{
1323 switch(ctxt->mode) {
1324 case X86EMUL_MODE_REAL:
1325 return emulate_int_real(ctxt, ops, irq);
1326 case X86EMUL_MODE_VM86:
1327 case X86EMUL_MODE_PROT16:
1328 case X86EMUL_MODE_PROT32:
1329 case X86EMUL_MODE_PROT64:
1330 default:
1331 /* Protected mode interrupts unimplemented yet */
1332 return X86EMUL_UNHANDLEABLE;
1333 }
1334}
1335
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001336static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1337 struct x86_emulate_ops *ops)
1338{
1339 struct decode_cache *c = &ctxt->decode;
1340 int rc = X86EMUL_CONTINUE;
1341 unsigned long temp_eip = 0;
1342 unsigned long temp_eflags = 0;
1343 unsigned long cs = 0;
1344 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1345 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1346 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1347 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1348
1349 /* TODO: Add stack limit check */
1350
1351 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1352
1353 if (rc != X86EMUL_CONTINUE)
1354 return rc;
1355
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001356 if (temp_eip & ~0xffff)
1357 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001358
1359 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1360
1361 if (rc != X86EMUL_CONTINUE)
1362 return rc;
1363
1364 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1365
1366 if (rc != X86EMUL_CONTINUE)
1367 return rc;
1368
1369 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1370
1371 if (rc != X86EMUL_CONTINUE)
1372 return rc;
1373
1374 c->eip = temp_eip;
1375
1376
1377 if (c->op_bytes == 4)
1378 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1379 else if (c->op_bytes == 2) {
1380 ctxt->eflags &= ~0xffff;
1381 ctxt->eflags |= temp_eflags;
1382 }
1383
1384 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1385 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1386
1387 return rc;
1388}
1389
1390static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1391 struct x86_emulate_ops* ops)
1392{
1393 switch(ctxt->mode) {
1394 case X86EMUL_MODE_REAL:
1395 return emulate_iret_real(ctxt, ops);
1396 case X86EMUL_MODE_VM86:
1397 case X86EMUL_MODE_PROT16:
1398 case X86EMUL_MODE_PROT32:
1399 case X86EMUL_MODE_PROT64:
1400 default:
1401 /* iret from protected mode unimplemented yet */
1402 return X86EMUL_UNHANDLEABLE;
1403 }
1404}
1405
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001406static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1407 struct x86_emulate_ops *ops)
1408{
1409 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001410
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001411 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001412}
1413
Laurent Vivier05f086f2007-09-24 11:10:55 +02001414static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001415{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001416 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001417 switch (c->modrm_reg) {
1418 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001419 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001420 break;
1421 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001422 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001423 break;
1424 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001425 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001426 break;
1427 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001428 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001429 break;
1430 case 4: /* sal/shl */
1431 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001432 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001433 break;
1434 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001435 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001436 break;
1437 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001438 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001439 break;
1440 }
1441}
1442
1443static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001444 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001445{
1446 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001447 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1448 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001449 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001450
1451 switch (c->modrm_reg) {
1452 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001453 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001454 break;
1455 case 2: /* not */
1456 c->dst.val = ~c->dst.val;
1457 break;
1458 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001459 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001460 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001461 case 4: /* mul */
1462 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1463 break;
1464 case 5: /* imul */
1465 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1466 break;
1467 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001468 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1469 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001470 break;
1471 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001472 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1473 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001474 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001475 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001476 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001477 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001478 if (de)
1479 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001480 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001481}
1482
1483static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001484 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001485{
1486 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001487
1488 switch (c->modrm_reg) {
1489 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001490 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001491 break;
1492 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001493 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001494 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001495 case 2: /* call near abs */ {
1496 long int old_eip;
1497 old_eip = c->eip;
1498 c->eip = c->src.val;
1499 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001500 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001501 break;
1502 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001503 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001504 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001505 break;
1506 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001507 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001508 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001509 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001510 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001511}
1512
1513static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001514 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001515{
1516 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001517 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001518
1519 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1520 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001521 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1522 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001523 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001524 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001525 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1526 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001527
Laurent Vivier05f086f2007-09-24 11:10:55 +02001528 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001530 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001531}
1532
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001533static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1534 struct x86_emulate_ops *ops)
1535{
1536 struct decode_cache *c = &ctxt->decode;
1537 int rc;
1538 unsigned long cs;
1539
1540 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001541 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001542 return rc;
1543 if (c->op_bytes == 4)
1544 c->eip = (u32)c->eip;
1545 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001546 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001547 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001548 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001549 return rc;
1550}
1551
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001552static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1553 struct x86_emulate_ops *ops, int seg)
1554{
1555 struct decode_cache *c = &ctxt->decode;
1556 unsigned short sel;
1557 int rc;
1558
1559 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1560
1561 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1562 if (rc != X86EMUL_CONTINUE)
1563 return rc;
1564
1565 c->dst.val = c->src.val;
1566 return rc;
1567}
1568
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001569static inline void
1570setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001571 struct x86_emulate_ops *ops, struct desc_struct *cs,
1572 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001573{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001574 memset(cs, 0, sizeof(struct desc_struct));
Gleb Natapov5601d052011-03-07 14:55:06 +02001575 ops->get_cached_descriptor(cs, NULL, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001576 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001577
1578 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001579 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001580 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001581 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001582 cs->type = 0x0b; /* Read, Execute, Accessed */
1583 cs->s = 1;
1584 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001585 cs->p = 1;
1586 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001587
Gleb Natapov79168fd2010-04-28 19:15:30 +03001588 set_desc_base(ss, 0); /* flat segment */
1589 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001590 ss->g = 1; /* 4kb granularity */
1591 ss->s = 1;
1592 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001593 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001594 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001595 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001596}
1597
1598static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001599emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001600{
1601 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001602 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001603 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001604 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001605
1606 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001607 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001608 ctxt->mode == X86EMUL_MODE_VM86)
1609 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001610
Gleb Natapov79168fd2010-04-28 19:15:30 +03001611 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001612
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001613 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001614 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001615 cs_sel = (u16)(msr_data & 0xfffc);
1616 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001617
1618 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001619 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001620 cs.l = 1;
1621 }
Gleb Natapov5601d052011-03-07 14:55:06 +02001622 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001623 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001624 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001625 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001626
1627 c->regs[VCPU_REGS_RCX] = c->eip;
1628 if (is_long_mode(ctxt->vcpu)) {
1629#ifdef CONFIG_X86_64
1630 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1631
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001632 ops->get_msr(ctxt->vcpu,
1633 ctxt->mode == X86EMUL_MODE_PROT64 ?
1634 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001635 c->eip = msr_data;
1636
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001637 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001638 ctxt->eflags &= ~(msr_data | EFLG_RF);
1639#endif
1640 } else {
1641 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001642 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001643 c->eip = (u32)msr_data;
1644
1645 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1646 }
1647
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001648 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001649}
1650
Andre Przywara8c604352009-06-18 12:56:01 +02001651static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001652emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001653{
1654 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001655 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001656 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001657 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001658
Gleb Natapova0044752010-02-10 14:21:31 +02001659 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001660 if (ctxt->mode == X86EMUL_MODE_REAL)
1661 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001662
1663 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1664 * Therefore, we inject an #UD.
1665 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001666 if (ctxt->mode == X86EMUL_MODE_PROT64)
1667 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001668
Gleb Natapov79168fd2010-04-28 19:15:30 +03001669 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001670
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001671 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001672 switch (ctxt->mode) {
1673 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001674 if ((msr_data & 0xfffc) == 0x0)
1675 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001676 break;
1677 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001678 if (msr_data == 0x0)
1679 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001680 break;
1681 }
1682
1683 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001684 cs_sel = (u16)msr_data;
1685 cs_sel &= ~SELECTOR_RPL_MASK;
1686 ss_sel = cs_sel + 8;
1687 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001688 if (ctxt->mode == X86EMUL_MODE_PROT64
1689 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001690 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001691 cs.l = 1;
1692 }
1693
Gleb Natapov5601d052011-03-07 14:55:06 +02001694 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001695 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001696 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001697 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001698
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001699 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001700 c->eip = msr_data;
1701
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001702 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001703 c->regs[VCPU_REGS_RSP] = msr_data;
1704
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001705 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001706}
1707
Andre Przywara4668f052009-06-18 12:56:02 +02001708static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001709emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001710{
1711 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001712 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001713 u64 msr_data;
1714 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001715 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001716
Gleb Natapova0044752010-02-10 14:21:31 +02001717 /* inject #GP if in real mode or Virtual 8086 mode */
1718 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001719 ctxt->mode == X86EMUL_MODE_VM86)
1720 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001721
Gleb Natapov79168fd2010-04-28 19:15:30 +03001722 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001723
1724 if ((c->rex_prefix & 0x8) != 0x0)
1725 usermode = X86EMUL_MODE_PROT64;
1726 else
1727 usermode = X86EMUL_MODE_PROT32;
1728
1729 cs.dpl = 3;
1730 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001731 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001732 switch (usermode) {
1733 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001734 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001735 if ((msr_data & 0xfffc) == 0x0)
1736 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001737 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001738 break;
1739 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001740 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001741 if (msr_data == 0x0)
1742 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001743 ss_sel = cs_sel + 8;
1744 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001745 cs.l = 1;
1746 break;
1747 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001748 cs_sel |= SELECTOR_RPL_MASK;
1749 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001750
Gleb Natapov5601d052011-03-07 14:55:06 +02001751 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001752 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001753 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001754 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001755
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001756 c->eip = c->regs[VCPU_REGS_RDX];
1757 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001758
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001759 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001760}
1761
Gleb Natapov9c537242010-03-18 15:20:05 +02001762static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1763 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001764{
1765 int iopl;
1766 if (ctxt->mode == X86EMUL_MODE_REAL)
1767 return false;
1768 if (ctxt->mode == X86EMUL_MODE_VM86)
1769 return true;
1770 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001771 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001772}
1773
1774static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1775 struct x86_emulate_ops *ops,
1776 u16 port, u16 len)
1777{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001778 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02001779 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001780 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001781 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001782 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02001783 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001784
Gleb Natapov5601d052011-03-07 14:55:06 +02001785 ops->get_cached_descriptor(&tr_seg, &base3, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001786 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001787 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001788 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001789 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02001790 base = get_desc_base(&tr_seg);
1791#ifdef CONFIG_X86_64
1792 base |= ((u64)base3) << 32;
1793#endif
1794 r = ops->read_std(base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001795 if (r != X86EMUL_CONTINUE)
1796 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001797 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001798 return false;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001799 r = ops->read_std(base + io_bitmap_ptr + port/8, &perm, 2, ctxt->vcpu,
Gleb Natapov5601d052011-03-07 14:55:06 +02001800 NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001801 if (r != X86EMUL_CONTINUE)
1802 return false;
1803 if ((perm >> bit_idx) & mask)
1804 return false;
1805 return true;
1806}
1807
1808static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1809 struct x86_emulate_ops *ops,
1810 u16 port, u16 len)
1811{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001812 if (ctxt->perm_ok)
1813 return true;
1814
Gleb Natapov9c537242010-03-18 15:20:05 +02001815 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001816 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1817 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001818
1819 ctxt->perm_ok = true;
1820
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001821 return true;
1822}
1823
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001824static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1825 struct x86_emulate_ops *ops,
1826 struct tss_segment_16 *tss)
1827{
1828 struct decode_cache *c = &ctxt->decode;
1829
1830 tss->ip = c->eip;
1831 tss->flag = ctxt->eflags;
1832 tss->ax = c->regs[VCPU_REGS_RAX];
1833 tss->cx = c->regs[VCPU_REGS_RCX];
1834 tss->dx = c->regs[VCPU_REGS_RDX];
1835 tss->bx = c->regs[VCPU_REGS_RBX];
1836 tss->sp = c->regs[VCPU_REGS_RSP];
1837 tss->bp = c->regs[VCPU_REGS_RBP];
1838 tss->si = c->regs[VCPU_REGS_RSI];
1839 tss->di = c->regs[VCPU_REGS_RDI];
1840
1841 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1842 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1843 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1844 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1845 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1846}
1847
1848static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1849 struct x86_emulate_ops *ops,
1850 struct tss_segment_16 *tss)
1851{
1852 struct decode_cache *c = &ctxt->decode;
1853 int ret;
1854
1855 c->eip = tss->ip;
1856 ctxt->eflags = tss->flag | 2;
1857 c->regs[VCPU_REGS_RAX] = tss->ax;
1858 c->regs[VCPU_REGS_RCX] = tss->cx;
1859 c->regs[VCPU_REGS_RDX] = tss->dx;
1860 c->regs[VCPU_REGS_RBX] = tss->bx;
1861 c->regs[VCPU_REGS_RSP] = tss->sp;
1862 c->regs[VCPU_REGS_RBP] = tss->bp;
1863 c->regs[VCPU_REGS_RSI] = tss->si;
1864 c->regs[VCPU_REGS_RDI] = tss->di;
1865
1866 /*
1867 * SDM says that segment selectors are loaded before segment
1868 * descriptors
1869 */
1870 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1871 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1872 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1873 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1874 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1875
1876 /*
1877 * Now load segment descriptors. If fault happenes at this stage
1878 * it is handled in a context of new task
1879 */
1880 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1881 if (ret != X86EMUL_CONTINUE)
1882 return ret;
1883 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1884 if (ret != X86EMUL_CONTINUE)
1885 return ret;
1886 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1887 if (ret != X86EMUL_CONTINUE)
1888 return ret;
1889 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1890 if (ret != X86EMUL_CONTINUE)
1891 return ret;
1892 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1893 if (ret != X86EMUL_CONTINUE)
1894 return ret;
1895
1896 return X86EMUL_CONTINUE;
1897}
1898
1899static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1900 struct x86_emulate_ops *ops,
1901 u16 tss_selector, u16 old_tss_sel,
1902 ulong old_tss_base, struct desc_struct *new_desc)
1903{
1904 struct tss_segment_16 tss_seg;
1905 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001906 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001907
1908 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001909 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001910 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001911 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001912 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001913
1914 save_state_to_tss16(ctxt, ops, &tss_seg);
1915
1916 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001917 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001918 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001919 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001920 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001921
1922 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001923 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001924 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001925 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001926 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001927
1928 if (old_tss_sel != 0xffff) {
1929 tss_seg.prev_task_link = old_tss_sel;
1930
1931 ret = ops->write_std(new_tss_base,
1932 &tss_seg.prev_task_link,
1933 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001934 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001935 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001936 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001937 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001938 }
1939
1940 return load_state_from_tss16(ctxt, ops, &tss_seg);
1941}
1942
1943static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1944 struct x86_emulate_ops *ops,
1945 struct tss_segment_32 *tss)
1946{
1947 struct decode_cache *c = &ctxt->decode;
1948
1949 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1950 tss->eip = c->eip;
1951 tss->eflags = ctxt->eflags;
1952 tss->eax = c->regs[VCPU_REGS_RAX];
1953 tss->ecx = c->regs[VCPU_REGS_RCX];
1954 tss->edx = c->regs[VCPU_REGS_RDX];
1955 tss->ebx = c->regs[VCPU_REGS_RBX];
1956 tss->esp = c->regs[VCPU_REGS_RSP];
1957 tss->ebp = c->regs[VCPU_REGS_RBP];
1958 tss->esi = c->regs[VCPU_REGS_RSI];
1959 tss->edi = c->regs[VCPU_REGS_RDI];
1960
1961 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1962 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1963 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1964 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1965 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1966 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1967 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1968}
1969
1970static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1971 struct x86_emulate_ops *ops,
1972 struct tss_segment_32 *tss)
1973{
1974 struct decode_cache *c = &ctxt->decode;
1975 int ret;
1976
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001977 if (ops->set_cr(3, tss->cr3, ctxt->vcpu))
1978 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001979 c->eip = tss->eip;
1980 ctxt->eflags = tss->eflags | 2;
1981 c->regs[VCPU_REGS_RAX] = tss->eax;
1982 c->regs[VCPU_REGS_RCX] = tss->ecx;
1983 c->regs[VCPU_REGS_RDX] = tss->edx;
1984 c->regs[VCPU_REGS_RBX] = tss->ebx;
1985 c->regs[VCPU_REGS_RSP] = tss->esp;
1986 c->regs[VCPU_REGS_RBP] = tss->ebp;
1987 c->regs[VCPU_REGS_RSI] = tss->esi;
1988 c->regs[VCPU_REGS_RDI] = tss->edi;
1989
1990 /*
1991 * SDM says that segment selectors are loaded before segment
1992 * descriptors
1993 */
1994 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1995 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1996 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1997 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1998 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1999 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2000 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2001
2002 /*
2003 * Now load segment descriptors. If fault happenes at this stage
2004 * it is handled in a context of new task
2005 */
2006 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2007 if (ret != X86EMUL_CONTINUE)
2008 return ret;
2009 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2010 if (ret != X86EMUL_CONTINUE)
2011 return ret;
2012 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2013 if (ret != X86EMUL_CONTINUE)
2014 return ret;
2015 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2016 if (ret != X86EMUL_CONTINUE)
2017 return ret;
2018 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2019 if (ret != X86EMUL_CONTINUE)
2020 return ret;
2021 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2022 if (ret != X86EMUL_CONTINUE)
2023 return ret;
2024 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2025 if (ret != X86EMUL_CONTINUE)
2026 return ret;
2027
2028 return X86EMUL_CONTINUE;
2029}
2030
2031static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2032 struct x86_emulate_ops *ops,
2033 u16 tss_selector, u16 old_tss_sel,
2034 ulong old_tss_base, struct desc_struct *new_desc)
2035{
2036 struct tss_segment_32 tss_seg;
2037 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002038 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002039
2040 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002041 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002042 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002043 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002044 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002045
2046 save_state_to_tss32(ctxt, ops, &tss_seg);
2047
2048 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002049 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002050 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002051 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002052 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002053
2054 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002055 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002056 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002057 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002058 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002059
2060 if (old_tss_sel != 0xffff) {
2061 tss_seg.prev_task_link = old_tss_sel;
2062
2063 ret = ops->write_std(new_tss_base,
2064 &tss_seg.prev_task_link,
2065 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002066 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002067 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002068 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002069 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002070 }
2071
2072 return load_state_from_tss32(ctxt, ops, &tss_seg);
2073}
2074
2075static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002076 struct x86_emulate_ops *ops,
2077 u16 tss_selector, int reason,
2078 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002079{
2080 struct desc_struct curr_tss_desc, next_tss_desc;
2081 int ret;
2082 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2083 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002084 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002085 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002086
2087 /* FIXME: old_tss_base == ~0 ? */
2088
2089 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2090 if (ret != X86EMUL_CONTINUE)
2091 return ret;
2092 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2093 if (ret != X86EMUL_CONTINUE)
2094 return ret;
2095
2096 /* FIXME: check that next_tss_desc is tss */
2097
2098 if (reason != TASK_SWITCH_IRET) {
2099 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002100 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl)
2101 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002102 }
2103
Gleb Natapovceffb452010-03-18 15:20:19 +02002104 desc_limit = desc_limit_scaled(&next_tss_desc);
2105 if (!next_tss_desc.p ||
2106 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2107 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002108 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002109 return X86EMUL_PROPAGATE_FAULT;
2110 }
2111
2112 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2113 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2114 write_segment_descriptor(ctxt, ops, old_tss_sel,
2115 &curr_tss_desc);
2116 }
2117
2118 if (reason == TASK_SWITCH_IRET)
2119 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2120
2121 /* set back link to prev task only if NT bit is set in eflags
2122 note that old_tss_sel is not used afetr this point */
2123 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2124 old_tss_sel = 0xffff;
2125
2126 if (next_tss_desc.type & 8)
2127 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2128 old_tss_base, &next_tss_desc);
2129 else
2130 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2131 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002132 if (ret != X86EMUL_CONTINUE)
2133 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002134
2135 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2136 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2137
2138 if (reason != TASK_SWITCH_IRET) {
2139 next_tss_desc.type |= (1 << 1); /* set busy flag */
2140 write_segment_descriptor(ctxt, ops, tss_selector,
2141 &next_tss_desc);
2142 }
2143
2144 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02002145 ops->set_cached_descriptor(&next_tss_desc, 0, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002146 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2147
Jan Kiszkae269fb22010-04-14 15:51:09 +02002148 if (has_error_code) {
2149 struct decode_cache *c = &ctxt->decode;
2150
2151 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2152 c->lock_prefix = 0;
2153 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002154 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002155 }
2156
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002157 return ret;
2158}
2159
2160int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002161 u16 tss_selector, int reason,
2162 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002164 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165 struct decode_cache *c = &ctxt->decode;
2166 int rc;
2167
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002169 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002170
Jan Kiszkae269fb22010-04-14 15:51:09 +02002171 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2172 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002173
2174 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002175 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002176 if (rc == X86EMUL_CONTINUE)
2177 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178 }
2179
Gleb Natapov19d04432010-04-15 12:29:50 +03002180 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002181}
2182
Avi Kivity90de84f2010-11-17 15:28:21 +02002183static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002184 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002185{
2186 struct decode_cache *c = &ctxt->decode;
2187 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2188
Gleb Natapovd9271122010-03-18 15:20:22 +02002189 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002190 op->addr.mem.ea = register_address(c, c->regs[reg]);
2191 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002192}
2193
Avi Kivity63540382010-07-29 15:11:55 +03002194static int em_push(struct x86_emulate_ctxt *ctxt)
2195{
2196 emulate_push(ctxt, ctxt->ops);
2197 return X86EMUL_CONTINUE;
2198}
2199
Avi Kivity7af04fc2010-08-18 14:16:35 +03002200static int em_das(struct x86_emulate_ctxt *ctxt)
2201{
2202 struct decode_cache *c = &ctxt->decode;
2203 u8 al, old_al;
2204 bool af, cf, old_cf;
2205
2206 cf = ctxt->eflags & X86_EFLAGS_CF;
2207 al = c->dst.val;
2208
2209 old_al = al;
2210 old_cf = cf;
2211 cf = false;
2212 af = ctxt->eflags & X86_EFLAGS_AF;
2213 if ((al & 0x0f) > 9 || af) {
2214 al -= 6;
2215 cf = old_cf | (al >= 250);
2216 af = true;
2217 } else {
2218 af = false;
2219 }
2220 if (old_al > 0x99 || old_cf) {
2221 al -= 0x60;
2222 cf = true;
2223 }
2224
2225 c->dst.val = al;
2226 /* Set PF, ZF, SF */
2227 c->src.type = OP_IMM;
2228 c->src.val = 0;
2229 c->src.bytes = 1;
2230 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2231 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2232 if (cf)
2233 ctxt->eflags |= X86_EFLAGS_CF;
2234 if (af)
2235 ctxt->eflags |= X86_EFLAGS_AF;
2236 return X86EMUL_CONTINUE;
2237}
2238
Avi Kivity0ef753b2010-08-18 14:51:45 +03002239static int em_call_far(struct x86_emulate_ctxt *ctxt)
2240{
2241 struct decode_cache *c = &ctxt->decode;
2242 u16 sel, old_cs;
2243 ulong old_eip;
2244 int rc;
2245
2246 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2247 old_eip = c->eip;
2248
2249 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2250 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2251 return X86EMUL_CONTINUE;
2252
2253 c->eip = 0;
2254 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2255
2256 c->src.val = old_cs;
2257 emulate_push(ctxt, ctxt->ops);
2258 rc = writeback(ctxt, ctxt->ops);
2259 if (rc != X86EMUL_CONTINUE)
2260 return rc;
2261
2262 c->src.val = old_eip;
2263 emulate_push(ctxt, ctxt->ops);
2264 rc = writeback(ctxt, ctxt->ops);
2265 if (rc != X86EMUL_CONTINUE)
2266 return rc;
2267
2268 c->dst.type = OP_NONE;
2269
2270 return X86EMUL_CONTINUE;
2271}
2272
Avi Kivity40ece7c2010-08-18 15:12:09 +03002273static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2274{
2275 struct decode_cache *c = &ctxt->decode;
2276 int rc;
2277
2278 c->dst.type = OP_REG;
2279 c->dst.addr.reg = &c->eip;
2280 c->dst.bytes = c->op_bytes;
2281 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2282 if (rc != X86EMUL_CONTINUE)
2283 return rc;
2284 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2285 return X86EMUL_CONTINUE;
2286}
2287
Avi Kivity5c82aa22010-08-18 18:31:43 +03002288static int em_imul(struct x86_emulate_ctxt *ctxt)
2289{
2290 struct decode_cache *c = &ctxt->decode;
2291
2292 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2293 return X86EMUL_CONTINUE;
2294}
2295
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002296static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2297{
2298 struct decode_cache *c = &ctxt->decode;
2299
2300 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002301 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002302}
2303
Avi Kivity61429142010-08-19 15:13:00 +03002304static int em_cwd(struct x86_emulate_ctxt *ctxt)
2305{
2306 struct decode_cache *c = &ctxt->decode;
2307
2308 c->dst.type = OP_REG;
2309 c->dst.bytes = c->src.bytes;
2310 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2311 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2312
2313 return X86EMUL_CONTINUE;
2314}
2315
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002316static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2317{
2318 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2319 struct decode_cache *c = &ctxt->decode;
2320 u64 tsc = 0;
2321
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002322 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD))
2323 return emulate_gp(ctxt, 0);
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002324 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2325 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2326 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2327 return X86EMUL_CONTINUE;
2328}
2329
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002330static int em_mov(struct x86_emulate_ctxt *ctxt)
2331{
2332 struct decode_cache *c = &ctxt->decode;
2333 c->dst.val = c->src.val;
2334 return X86EMUL_CONTINUE;
2335}
2336
Avi Kivity73fba5f2010-07-29 15:11:53 +03002337#define D(_y) { .flags = (_y) }
2338#define N D(0)
2339#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2340#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2341#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2342
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002343#define D2bv(_f) D((_f) | ByteOp), D(_f)
2344#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2345
Avi Kivity6230f7f2010-08-26 18:34:55 +03002346#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2347 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2348 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2349
2350
Avi Kivity73fba5f2010-07-29 15:11:53 +03002351static struct opcode group1[] = {
2352 X7(D(Lock)), N
2353};
2354
2355static struct opcode group1A[] = {
2356 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2357};
2358
2359static struct opcode group3[] = {
2360 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2361 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002362 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002363};
2364
2365static struct opcode group4[] = {
2366 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2367 N, N, N, N, N, N,
2368};
2369
2370static struct opcode group5[] = {
2371 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002372 D(SrcMem | ModRM | Stack),
2373 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002374 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2375 D(SrcMem | ModRM | Stack), N,
2376};
2377
2378static struct group_dual group7 = { {
2379 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2380 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002381 D(SrcMem16 | ModRM | Mov | Priv),
2382 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002383}, {
Avi Kivityd8671622011-02-01 16:32:03 +02002384 D(SrcNone | ModRM | Priv | VendorSpecific), N,
2385 N, D(SrcNone | ModRM | Priv | VendorSpecific),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002386 D(SrcNone | ModRM | DstMem | Mov), N,
2387 D(SrcMem16 | ModRM | Mov | Priv), N,
2388} };
2389
2390static struct opcode group8[] = {
2391 N, N, N, N,
2392 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2393 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2394};
2395
2396static struct group_dual group9 = { {
2397 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2398}, {
2399 N, N, N, N, N, N, N, N,
2400} };
2401
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002402static struct opcode group11[] = {
2403 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2404};
2405
Avi Kivity73fba5f2010-07-29 15:11:53 +03002406static struct opcode opcode_table[256] = {
2407 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002408 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002409 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2410 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002411 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002412 D(ImplicitOps | Stack | No64), N,
2413 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002414 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002415 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2416 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002417 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002418 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2419 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002420 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002421 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002422 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002423 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002424 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002425 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002426 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002427 /* 0x40 - 0x4F */
2428 X16(D(DstReg)),
2429 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002430 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002431 /* 0x58 - 0x5F */
2432 X8(D(DstReg | Stack)),
2433 /* 0x60 - 0x67 */
2434 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2435 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2436 N, N, N, N,
2437 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002438 I(SrcImm | Mov | Stack, em_push),
2439 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002440 I(SrcImmByte | Mov | Stack, em_push),
2441 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002442 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2443 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002444 /* 0x70 - 0x7F */
2445 X16(D(SrcImmByte)),
2446 /* 0x80 - 0x87 */
2447 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2448 G(DstMem | SrcImm | ModRM | Group, group1),
2449 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2450 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002451 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002452 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002453 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2454 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002455 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002456 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2457 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002458 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002459 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002460 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002461 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002462 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2463 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002464 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2465 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2466 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2467 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002468 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002469 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002470 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2471 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002472 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002473 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002474 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002475 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002476 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002477 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002478 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002479 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2480 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002481 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002482 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002483 /* 0xC8 - 0xCF */
2484 N, N, N, D(ImplicitOps | Stack),
2485 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2486 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002487 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002488 N, N, N, N,
2489 /* 0xD8 - 0xDF */
2490 N, N, N, N, N, N, N, N,
2491 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002492 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002493 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002494 /* 0xE8 - 0xEF */
2495 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2496 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002497 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002498 /* 0xF0 - 0xF7 */
2499 N, N, N, N,
2500 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2501 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002502 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002503 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2504};
2505
2506static struct opcode twobyte_table[256] = {
2507 /* 0x00 - 0x0F */
2508 N, GD(0, &group7), N, N,
Avi Kivityd8671622011-02-01 16:32:03 +02002509 N, D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002510 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2511 N, D(ImplicitOps | ModRM), N, N,
2512 /* 0x10 - 0x1F */
2513 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2514 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002515 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2516 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002517 N, N, N, N,
2518 N, N, N, N, N, N, N, N,
2519 /* 0x30 - 0x3F */
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002520 D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc),
2521 D(ImplicitOps | Priv), N,
Avi Kivityd8671622011-02-01 16:32:03 +02002522 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
2523 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002524 N, N, N, N, N, N, N, N,
2525 /* 0x40 - 0x4F */
2526 X16(D(DstReg | SrcMem | ModRM | Mov)),
2527 /* 0x50 - 0x5F */
2528 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2529 /* 0x60 - 0x6F */
2530 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2531 /* 0x70 - 0x7F */
2532 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2533 /* 0x80 - 0x8F */
2534 X16(D(SrcImm)),
2535 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002536 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002537 /* 0xA0 - 0xA7 */
2538 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2539 N, D(DstMem | SrcReg | ModRM | BitOp),
2540 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2541 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2542 /* 0xA8 - 0xAF */
2543 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2544 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2545 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2546 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002547 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002548 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002549 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002550 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2551 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2552 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002553 /* 0xB8 - 0xBF */
2554 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002555 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002556 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2557 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002558 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002559 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002560 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002561 N, N, N, GD(0, &group9),
2562 N, N, N, N, N, N, N, N,
2563 /* 0xD0 - 0xDF */
2564 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2565 /* 0xE0 - 0xEF */
2566 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2567 /* 0xF0 - 0xFF */
2568 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2569};
2570
2571#undef D
2572#undef N
2573#undef G
2574#undef GD
2575#undef I
2576
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002577#undef D2bv
2578#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002579#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002580
Avi Kivity39f21ee2010-08-18 19:20:21 +03002581static unsigned imm_size(struct decode_cache *c)
2582{
2583 unsigned size;
2584
2585 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2586 if (size == 8)
2587 size = 4;
2588 return size;
2589}
2590
2591static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2592 unsigned size, bool sign_extension)
2593{
2594 struct decode_cache *c = &ctxt->decode;
2595 struct x86_emulate_ops *ops = ctxt->ops;
2596 int rc = X86EMUL_CONTINUE;
2597
2598 op->type = OP_IMM;
2599 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002600 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002601 /* NB. Immediates are sign-extended as necessary. */
2602 switch (op->bytes) {
2603 case 1:
2604 op->val = insn_fetch(s8, 1, c->eip);
2605 break;
2606 case 2:
2607 op->val = insn_fetch(s16, 2, c->eip);
2608 break;
2609 case 4:
2610 op->val = insn_fetch(s32, 4, c->eip);
2611 break;
2612 }
2613 if (!sign_extension) {
2614 switch (op->bytes) {
2615 case 1:
2616 op->val &= 0xff;
2617 break;
2618 case 2:
2619 op->val &= 0xffff;
2620 break;
2621 case 4:
2622 op->val &= 0xffffffff;
2623 break;
2624 }
2625 }
2626done:
2627 return rc;
2628}
2629
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002630int
Andre Przywaradc25e892010-12-21 11:12:07 +01002631x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002632{
2633 struct x86_emulate_ops *ops = ctxt->ops;
2634 struct decode_cache *c = &ctxt->decode;
2635 int rc = X86EMUL_CONTINUE;
2636 int mode = ctxt->mode;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002637 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
2638 bool op_prefix = false;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002639 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002640 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002641
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002642 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01002643 c->fetch.start = c->eip;
2644 c->fetch.end = c->fetch.start + insn_len;
2645 if (insn_len > 0)
2646 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002647 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2648
2649 switch (mode) {
2650 case X86EMUL_MODE_REAL:
2651 case X86EMUL_MODE_VM86:
2652 case X86EMUL_MODE_PROT16:
2653 def_op_bytes = def_ad_bytes = 2;
2654 break;
2655 case X86EMUL_MODE_PROT32:
2656 def_op_bytes = def_ad_bytes = 4;
2657 break;
2658#ifdef CONFIG_X86_64
2659 case X86EMUL_MODE_PROT64:
2660 def_op_bytes = 4;
2661 def_ad_bytes = 8;
2662 break;
2663#endif
2664 default:
2665 return -1;
2666 }
2667
2668 c->op_bytes = def_op_bytes;
2669 c->ad_bytes = def_ad_bytes;
2670
2671 /* Legacy prefixes. */
2672 for (;;) {
2673 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2674 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002675 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002676 /* switch between 2/4 bytes */
2677 c->op_bytes = def_op_bytes ^ 6;
2678 break;
2679 case 0x67: /* address-size override */
2680 if (mode == X86EMUL_MODE_PROT64)
2681 /* switch between 4/8 bytes */
2682 c->ad_bytes = def_ad_bytes ^ 12;
2683 else
2684 /* switch between 2/4 bytes */
2685 c->ad_bytes = def_ad_bytes ^ 6;
2686 break;
2687 case 0x26: /* ES override */
2688 case 0x2e: /* CS override */
2689 case 0x36: /* SS override */
2690 case 0x3e: /* DS override */
2691 set_seg_override(c, (c->b >> 3) & 3);
2692 break;
2693 case 0x64: /* FS override */
2694 case 0x65: /* GS override */
2695 set_seg_override(c, c->b & 7);
2696 break;
2697 case 0x40 ... 0x4f: /* REX */
2698 if (mode != X86EMUL_MODE_PROT64)
2699 goto done_prefixes;
2700 c->rex_prefix = c->b;
2701 continue;
2702 case 0xf0: /* LOCK */
2703 c->lock_prefix = 1;
2704 break;
2705 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002706 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02002707 c->rep_prefix = c->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002708 break;
2709 default:
2710 goto done_prefixes;
2711 }
2712
2713 /* Any legacy prefix after a REX prefix nullifies its effect. */
2714
2715 c->rex_prefix = 0;
2716 }
2717
2718done_prefixes:
2719
2720 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002721 if (c->rex_prefix & 8)
2722 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002723
2724 /* Opcode byte(s). */
2725 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002726 /* Two-byte opcode? */
2727 if (c->b == 0x0f) {
2728 c->twobyte = 1;
2729 c->b = insn_fetch(u8, 1, c->eip);
2730 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002731 }
2732 c->d = opcode.flags;
2733
2734 if (c->d & Group) {
2735 dual = c->d & GroupDual;
2736 c->modrm = insn_fetch(u8, 1, c->eip);
2737 --c->eip;
2738
2739 if (c->d & GroupDual) {
2740 g_mod012 = opcode.u.gdual->mod012;
2741 g_mod3 = opcode.u.gdual->mod3;
2742 } else
2743 g_mod012 = g_mod3 = opcode.u.group;
2744
2745 c->d &= ~(Group | GroupDual);
2746
2747 goffset = (c->modrm >> 3) & 7;
2748
2749 if ((c->modrm >> 6) == 3)
2750 opcode = g_mod3[goffset];
2751 else
2752 opcode = g_mod012[goffset];
2753 c->d |= opcode.flags;
2754 }
2755
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002756 if (c->d & Prefix) {
2757 if (c->rep_prefix && op_prefix)
2758 return X86EMUL_UNHANDLEABLE;
2759 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
2760 switch (simd_prefix) {
2761 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
2762 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
2763 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
2764 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
2765 }
2766 c->d |= opcode.flags;
2767 }
2768
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002769 c->execute = opcode.u.execute;
2770
2771 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02002772 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002773 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002774
Avi Kivityd8671622011-02-01 16:32:03 +02002775 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
2776 return -1;
2777
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002778 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2779 c->op_bytes = 8;
2780
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002781 if (c->d & Op3264) {
2782 if (mode == X86EMUL_MODE_PROT64)
2783 c->op_bytes = 8;
2784 else
2785 c->op_bytes = 4;
2786 }
2787
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002788 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002789 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002790 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002791 if (!c->has_seg_override)
2792 set_seg_override(c, c->modrm_seg);
2793 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002794 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002795 if (rc != X86EMUL_CONTINUE)
2796 goto done;
2797
2798 if (!c->has_seg_override)
2799 set_seg_override(c, VCPU_SREG_DS);
2800
Avi Kivity90de84f2010-11-17 15:28:21 +02002801 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002802
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002803 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02002804 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002805
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002806 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02002807 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002808
2809 /*
2810 * Decode and fetch the source operand: register, memory
2811 * or immediate.
2812 */
2813 switch (c->d & SrcMask) {
2814 case SrcNone:
2815 break;
2816 case SrcReg:
2817 decode_register_operand(&c->src, c, 0);
2818 break;
2819 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002820 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002821 goto srcmem_common;
2822 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002823 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002824 goto srcmem_common;
2825 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002826 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002827 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002828 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002829 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002830 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002831 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002832 rc = decode_imm(ctxt, &c->src, 2, false);
2833 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002834 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002835 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2836 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002837 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002838 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002839 break;
2840 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002841 rc = decode_imm(ctxt, &c->src, 1, true);
2842 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002843 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002844 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002845 break;
2846 case SrcAcc:
2847 c->src.type = OP_REG;
2848 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002849 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002850 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002851 break;
2852 case SrcOne:
2853 c->src.bytes = 1;
2854 c->src.val = 1;
2855 break;
2856 case SrcSI:
2857 c->src.type = OP_MEM;
2858 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002859 c->src.addr.mem.ea =
2860 register_address(c, c->regs[VCPU_REGS_RSI]);
2861 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002862 c->src.val = 0;
2863 break;
2864 case SrcImmFAddr:
2865 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002866 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002867 c->src.bytes = c->op_bytes + 2;
2868 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2869 break;
2870 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002871 memop.bytes = c->op_bytes + 2;
2872 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002873 break;
2874 }
2875
Avi Kivity39f21ee2010-08-18 19:20:21 +03002876 if (rc != X86EMUL_CONTINUE)
2877 goto done;
2878
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002879 /*
2880 * Decode and fetch the second source operand: register, memory
2881 * or immediate.
2882 */
2883 switch (c->d & Src2Mask) {
2884 case Src2None:
2885 break;
2886 case Src2CL:
2887 c->src2.bytes = 1;
2888 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2889 break;
2890 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002891 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002892 break;
2893 case Src2One:
2894 c->src2.bytes = 1;
2895 c->src2.val = 1;
2896 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03002897 case Src2Imm:
2898 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
2899 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002900 }
2901
Avi Kivity39f21ee2010-08-18 19:20:21 +03002902 if (rc != X86EMUL_CONTINUE)
2903 goto done;
2904
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002905 /* Decode and fetch the destination operand: register or memory. */
2906 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002907 case DstReg:
2908 decode_register_operand(&c->dst, c,
2909 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2910 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08002911 case DstImmUByte:
2912 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002913 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08002914 c->dst.bytes = 1;
2915 c->dst.val = insn_fetch(u8, 1, c->eip);
2916 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002917 case DstMem:
2918 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002919 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002920 if ((c->d & DstMask) == DstMem64)
2921 c->dst.bytes = 8;
2922 else
2923 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002924 if (c->d & BitOp)
2925 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002926 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002927 break;
2928 case DstAcc:
2929 c->dst.type = OP_REG;
2930 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002931 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002932 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002933 c->dst.orig_val = c->dst.val;
2934 break;
2935 case DstDI:
2936 c->dst.type = OP_MEM;
2937 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002938 c->dst.addr.mem.ea =
2939 register_address(c, c->regs[VCPU_REGS_RDI]);
2940 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002941 c->dst.val = 0;
2942 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002943 case ImplicitOps:
2944 /* Special instructions do their own operand decoding. */
2945 default:
2946 c->dst.type = OP_NONE; /* Disable writeback. */
2947 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002948 }
2949
2950done:
2951 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2952}
2953
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03002954static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
2955{
2956 struct decode_cache *c = &ctxt->decode;
2957
2958 /* The second termination condition only applies for REPE
2959 * and REPNE. Test if the repeat string operation prefix is
2960 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2961 * corresponding termination condition according to:
2962 * - if REPE/REPZ and ZF = 0 then done
2963 * - if REPNE/REPNZ and ZF = 1 then done
2964 */
2965 if (((c->b == 0xa6) || (c->b == 0xa7) ||
2966 (c->b == 0xae) || (c->b == 0xaf))
2967 && (((c->rep_prefix == REPE_PREFIX) &&
2968 ((ctxt->eflags & EFLG_ZF) == 0))
2969 || ((c->rep_prefix == REPNE_PREFIX) &&
2970 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
2971 return true;
2972
2973 return false;
2974}
2975
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002976int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002977x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002978{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002979 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002980 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002981 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002982 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002983 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002984 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002985
Gleb Natapov9de41572010-04-28 19:15:22 +03002986 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002987
Gleb Natapov11616242010-02-11 14:43:14 +02002988 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002989 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002990 goto done;
2991 }
2992
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002993 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002994 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002995 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002996 goto done;
2997 }
2998
Avi Kivity081bca02010-08-26 11:06:15 +03002999 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003000 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003001 goto done;
3002 }
3003
Gleb Natapove92805a2010-02-10 14:21:35 +02003004 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003005 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003006 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003007 goto done;
3008 }
3009
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003010 if (c->rep_prefix && (c->d & String)) {
3011 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003012 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003013 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003014 goto done;
3015 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003016 }
3017
Wei Yongjunc483c022010-08-06 15:36:36 +08003018 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003019 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03003020 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003021 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003022 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003023 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003024 }
3025
Gleb Natapove35b7b92010-02-25 16:36:42 +02003026 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003027 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003028 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003029 if (rc != X86EMUL_CONTINUE)
3030 goto done;
3031 }
3032
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003033 if ((c->d & DstMask) == ImplicitOps)
3034 goto special_insn;
3035
3036
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003037 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3038 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003039 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003040 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003041 if (rc != X86EMUL_CONTINUE)
3042 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003043 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003044 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003045
Avi Kivity018a98d2007-11-27 19:30:56 +02003046special_insn:
3047
Avi Kivityef65c882010-07-29 15:11:51 +03003048 if (c->execute) {
3049 rc = c->execute(ctxt);
3050 if (rc != X86EMUL_CONTINUE)
3051 goto done;
3052 goto writeback;
3053 }
3054
Laurent Viviere4e03de2007-09-18 11:52:50 +02003055 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056 goto twobyte_insn;
3057
Laurent Viviere4e03de2007-09-18 11:52:50 +02003058 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 case 0x00 ... 0x05:
3060 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003061 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003063 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003064 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003065 break;
3066 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003067 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003068 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 case 0x08 ... 0x0d:
3070 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003071 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003073 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003074 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003075 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076 case 0x10 ... 0x15:
3077 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003078 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003080 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003081 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003082 break;
3083 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003084 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003085 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 case 0x18 ... 0x1d:
3087 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003088 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003090 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003091 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003092 break;
3093 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003094 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003095 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003096 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003098 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 break;
3100 case 0x28 ... 0x2d:
3101 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003102 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 break;
3104 case 0x30 ... 0x35:
3105 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003106 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 break;
3108 case 0x38 ... 0x3d:
3109 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003110 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003112 case 0x40 ... 0x47: /* inc r16/r32 */
3113 emulate_1op("inc", c->dst, ctxt->eflags);
3114 break;
3115 case 0x48 ... 0x4f: /* dec r16/r32 */
3116 emulate_1op("dec", c->dst, ctxt->eflags);
3117 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003118 case 0x58 ... 0x5f: /* pop reg */
3119 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003120 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003121 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003122 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003123 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003124 break;
3125 case 0x61: /* popa */
3126 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003127 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003129 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003131 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003133 case 0x6c: /* insb */
3134 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003135 c->src.val = c->regs[VCPU_REGS_RDX];
3136 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003137 case 0x6e: /* outsb */
3138 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003139 c->dst.val = c->regs[VCPU_REGS_RDX];
3140 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003141 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003142 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003143 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003144 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003147 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 case 0:
3149 goto add;
3150 case 1:
3151 goto or;
3152 case 2:
3153 goto adc;
3154 case 3:
3155 goto sbb;
3156 case 4:
3157 goto and;
3158 case 5:
3159 goto sub;
3160 case 6:
3161 goto xor;
3162 case 7:
3163 goto cmp;
3164 }
3165 break;
3166 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003167 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003168 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 break;
3170 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003171 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003173 c->src.val = c->dst.val;
3174 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175 /*
3176 * Write back the memory destination with implicit LOCK
3177 * prefix.
3178 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003179 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003180 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003182 case 0x8c: /* mov r/m, sreg */
3183 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003184 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003185 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003186 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003187 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003188 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003189 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003190 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003191 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003192 case 0x8e: { /* mov seg, r/m16 */
3193 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003194
3195 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003196
Gleb Natapovc6975182010-02-18 12:15:01 +02003197 if (c->modrm_reg == VCPU_SREG_CS ||
3198 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003199 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003200 goto done;
3201 }
3202
Glauber Costa310b5d32009-05-12 16:21:06 -04003203 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003204 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003205
Gleb Natapov2e873022010-03-18 15:20:18 +02003206 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003207
3208 c->dst.type = OP_NONE; /* Disable writeback. */
3209 break;
3210 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003212 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003214 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3215 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003216 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003217 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003218 case 0x98: /* cbw/cwde/cdqe */
3219 switch (c->op_bytes) {
3220 case 2: c->dst.val = (s8)c->dst.val; break;
3221 case 4: c->dst.val = (s16)c->dst.val; break;
3222 case 8: c->dst.val = (s32)c->dst.val; break;
3223 }
3224 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003225 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003226 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003227 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003228 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003229 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003230 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003231 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003232 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003233 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003234 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003236 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003237 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003238 case 0xa8 ... 0xa9: /* test ax, imm */
3239 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003241 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003242 case 0xc0 ... 0xc1:
3243 emulate_grp2(ctxt);
3244 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003245 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003246 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003247 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003248 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003249 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003250 case 0xc4: /* les */
3251 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003252 break;
3253 case 0xc5: /* lds */
3254 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003255 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003256 case 0xcb: /* ret far */
3257 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003258 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003259 case 0xcc: /* int3 */
3260 irq = 3;
3261 goto do_interrupt;
3262 case 0xcd: /* int n */
3263 irq = c->src.val;
3264 do_interrupt:
3265 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003266 break;
3267 case 0xce: /* into */
3268 if (ctxt->eflags & EFLG_OF) {
3269 irq = 4;
3270 goto do_interrupt;
3271 }
3272 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003273 case 0xcf: /* iret */
3274 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003275 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003276 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003277 emulate_grp2(ctxt);
3278 break;
3279 case 0xd2 ... 0xd3: /* Grp2 */
3280 c->src.val = c->regs[VCPU_REGS_RCX];
3281 emulate_grp2(ctxt);
3282 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003283 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3284 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3285 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3286 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3287 jmp_rel(c, c->src.val);
3288 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003289 case 0xe3: /* jcxz/jecxz/jrcxz */
3290 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3291 jmp_rel(c, c->src.val);
3292 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003293 case 0xe4: /* inb */
3294 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003295 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003296 case 0xe6: /* outb */
3297 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003298 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003299 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003300 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003301 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003302 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003303 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003304 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003305 }
3306 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003307 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003308 case 0xea: { /* jmp far */
3309 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003310 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003311 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3312
3313 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003314 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003315
Gleb Natapov414e6272010-04-28 19:15:26 +03003316 c->eip = 0;
3317 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003318 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003319 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003320 case 0xeb:
3321 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003322 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003323 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003324 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003325 case 0xec: /* in al,dx */
3326 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003327 c->src.val = c->regs[VCPU_REGS_RDX];
3328 do_io_in:
3329 c->dst.bytes = min(c->dst.bytes, 4u);
3330 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003331 rc = emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003332 goto done;
3333 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003334 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3335 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003336 goto done; /* IO is needed */
3337 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003338 case 0xee: /* out dx,al */
3339 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003340 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003341 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003342 c->src.bytes = min(c->src.bytes, 4u);
3343 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3344 c->src.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003345 rc = emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003346 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003347 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003348 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3349 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003350 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003351 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003352 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003353 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003354 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003355 case 0xf5: /* cmc */
3356 /* complement carry flag from eflags reg */
3357 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003358 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003359 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003360 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003361 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003362 case 0xf8: /* clc */
3363 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003364 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003365 case 0xf9: /* stc */
3366 ctxt->eflags |= EFLG_CF;
3367 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003368 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003369 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003370 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003371 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003372 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003373 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003374 break;
3375 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003376 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003377 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003378 goto done;
3379 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003380 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003381 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003382 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003383 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003384 case 0xfc: /* cld */
3385 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003386 break;
3387 case 0xfd: /* std */
3388 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003389 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003390 case 0xfe: /* Grp4 */
3391 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003392 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003393 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003394 case 0xff: /* Grp5 */
3395 if (c->modrm_reg == 5)
3396 goto jump_far;
3397 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003398 default:
3399 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003401
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003402 if (rc != X86EMUL_CONTINUE)
3403 goto done;
3404
Avi Kivity018a98d2007-11-27 19:30:56 +02003405writeback:
3406 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003407 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003408 goto done;
3409
Gleb Natapov5cd21912010-03-18 15:20:26 +02003410 /*
3411 * restore dst type in case the decoding will be reused
3412 * (happens for string instruction )
3413 */
3414 c->dst.type = saved_dst_type;
3415
Gleb Natapova682e352010-03-18 15:20:21 +02003416 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003417 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003418 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003419
3420 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003421 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003422 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003423
Gleb Natapov5cd21912010-03-18 15:20:26 +02003424 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003425 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003426 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003427
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003428 if (!string_insn_completed(ctxt)) {
3429 /*
3430 * Re-enter guest when pio read ahead buffer is empty
3431 * or, if it is not used, after each 1024 iteration.
3432 */
3433 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3434 (r->end == 0 || r->end != r->pos)) {
3435 /*
3436 * Reset read cache. Usually happens before
3437 * decode, but since instruction is restarted
3438 * we have to do it here.
3439 */
3440 ctxt->decode.mem_read.end = 0;
3441 return EMULATION_RESTART;
3442 }
3443 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003444 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003445 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003446
3447 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003448
3449done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003450 if (rc == X86EMUL_PROPAGATE_FAULT)
3451 ctxt->have_exception = true;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003452 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453
3454twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003455 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003456 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003457 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458 u16 size;
3459 unsigned long address;
3460
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003461 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003462 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003463 goto cannot_emulate;
3464
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003465 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003466 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003467 goto done;
3468
Avi Kivity33e38852008-05-21 15:34:25 +03003469 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003470 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003471 /* Disable writeback. */
3472 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003473 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003475 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003476 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003477 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478 goto done;
3479 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003480 /* Disable writeback. */
3481 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003483 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003484 if (c->modrm_mod == 3) {
3485 switch (c->modrm_rm) {
3486 case 1:
3487 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003488 break;
3489 default:
3490 goto cannot_emulate;
3491 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003492 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003493 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003494 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003495 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003496 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003497 goto done;
3498 realmode_lidt(ctxt->vcpu, size, address);
3499 }
Avi Kivity16286d02008-04-14 14:40:50 +03003500 /* Disable writeback. */
3501 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 break;
3503 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003504 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003505 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003506 break;
3507 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003508 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003509 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003510 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003512 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003513 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003514 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003515 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003517 emulate_invlpg(ctxt->vcpu,
3518 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003519 /* Disable writeback. */
3520 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521 break;
3522 default:
3523 goto cannot_emulate;
3524 }
3525 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003526 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003527 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003528 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003529 case 0x06:
3530 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003531 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003532 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003533 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003534 break;
3535 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003536 case 0x0d: /* GrpP (prefetch) */
3537 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003538 break;
3539 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003540 switch (c->modrm_reg) {
3541 case 1:
3542 case 5 ... 7:
3543 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003544 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003545 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003546 goto done;
3547 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003548 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003549 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003551 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3552 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003553 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003554 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003555 goto done;
3556 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003557 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003559 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003560 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003561 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003562 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03003563 goto done;
3564 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003565 c->dst.type = OP_NONE;
3566 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003568 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3569 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003570 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003571 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003572 goto done;
3573 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003574
Avi Kivityb27f3852010-08-01 14:25:22 +03003575 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003576 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3577 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3578 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003579 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003580 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03003581 goto done;
3582 }
3583
Laurent Viviera01af5e2007-09-24 11:10:56 +02003584 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003585 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003586 case 0x30:
3587 /* wrmsr */
3588 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3589 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003590 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003591 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003592 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003593 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003594 }
3595 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003596 break;
3597 case 0x32:
3598 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003599 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003600 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003601 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003602 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003603 } else {
3604 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3605 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3606 }
3607 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003608 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003609 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003610 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003611 break;
3612 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003613 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003614 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003616 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003617 if (!test_cc(c->b, ctxt->eflags))
3618 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003619 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003620 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003621 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003622 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003623 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003624 case 0x90 ... 0x9f: /* setcc r/m8 */
3625 c->dst.val = test_cc(c->b, ctxt->eflags);
3626 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003627 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003628 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003629 break;
3630 case 0xa1: /* pop fs */
3631 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003632 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003633 case 0xa3:
3634 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003635 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003636 /* only subword offset */
3637 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003638 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003639 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003640 case 0xa4: /* shld imm8, r, r/m */
3641 case 0xa5: /* shld cl, r, r/m */
3642 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3643 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003644 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003645 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003646 break;
3647 case 0xa9: /* pop gs */
3648 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003649 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003650 case 0xab:
3651 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003652 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003653 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003654 case 0xac: /* shrd imm8, r, r/m */
3655 case 0xad: /* shrd cl, r, r/m */
3656 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3657 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003658 case 0xae: /* clflush */
3659 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660 case 0xb0 ... 0xb1: /* cmpxchg */
3661 /*
3662 * Save real source value, then compare EAX against
3663 * destination.
3664 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003665 c->src.orig_val = c->src.val;
3666 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003667 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3668 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003670 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 } else {
3672 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003673 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003674 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675 }
3676 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003677 case 0xb2: /* lss */
3678 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003679 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003680 case 0xb3:
3681 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003682 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003684 case 0xb4: /* lfs */
3685 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003686 break;
3687 case 0xb5: /* lgs */
3688 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003689 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003690 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003691 c->dst.bytes = c->op_bytes;
3692 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3693 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003694 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003695 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003696 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003697 case 0:
3698 goto bt;
3699 case 1:
3700 goto bts;
3701 case 2:
3702 goto btr;
3703 case 3:
3704 goto btc;
3705 }
3706 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003707 case 0xbb:
3708 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003709 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003710 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003711 case 0xbc: { /* bsf */
3712 u8 zf;
3713 __asm__ ("bsf %2, %0; setz %1"
3714 : "=r"(c->dst.val), "=q"(zf)
3715 : "r"(c->src.val));
3716 ctxt->eflags &= ~X86_EFLAGS_ZF;
3717 if (zf) {
3718 ctxt->eflags |= X86_EFLAGS_ZF;
3719 c->dst.type = OP_NONE; /* Disable writeback. */
3720 }
3721 break;
3722 }
3723 case 0xbd: { /* bsr */
3724 u8 zf;
3725 __asm__ ("bsr %2, %0; setz %1"
3726 : "=r"(c->dst.val), "=q"(zf)
3727 : "r"(c->src.val));
3728 ctxt->eflags &= ~X86_EFLAGS_ZF;
3729 if (zf) {
3730 ctxt->eflags |= X86_EFLAGS_ZF;
3731 c->dst.type = OP_NONE; /* Disable writeback. */
3732 }
3733 break;
3734 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003735 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003736 c->dst.bytes = c->op_bytes;
3737 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3738 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003739 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003740 case 0xc0 ... 0xc1: /* xadd */
3741 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3742 /* Write back the register source. */
3743 c->src.val = c->dst.orig_val;
3744 write_register_operand(&c->src);
3745 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003746 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003747 c->dst.bytes = c->op_bytes;
3748 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3749 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003750 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003751 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003752 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003753 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003754 default:
3755 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003757
3758 if (rc != X86EMUL_CONTINUE)
3759 goto done;
3760
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761 goto writeback;
3762
3763cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764 return -1;
3765}