blob: 3f32a6699fbd46161b02439527a4aad09f450581 [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 */
Avi Kivity12537912011-03-29 11:41:27 +020079#define Sse (1<<17) /* SSE Vector instruction */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030080/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +020081#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +020082#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030083#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030084#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030085#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020086#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020087#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030088#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010089/* Source 2 operand type */
90#define Src2None (0<<29)
91#define Src2CL (1<<29)
92#define Src2ImmByte (2<<29)
93#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030094#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010095#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
Avi Kivityd0e53322010-07-29 15:11:54 +030097#define X2(x...) x, x
98#define X3(x...) X2(x), x
99#define X4(x...) X2(x), X2(x)
100#define X5(x...) X4(x), x
101#define X6(x...) X4(x), X2(x)
102#define X7(x...) X4(x), X3(x)
103#define X8(x...) X4(x), X4(x)
104#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300105
Avi Kivityd65b1de2010-07-29 15:11:35 +0300106struct opcode {
107 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200108 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300109 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300110 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300111 struct opcode *group;
112 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200113 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300114 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200115 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300116};
117
118struct group_dual {
119 struct opcode mod012[8];
120 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300121};
122
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200123struct gprefix {
124 struct opcode pfx_no;
125 struct opcode pfx_66;
126 struct opcode pfx_f2;
127 struct opcode pfx_f3;
128};
129
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200131#define EFLG_ID (1<<21)
132#define EFLG_VIP (1<<20)
133#define EFLG_VIF (1<<19)
134#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200135#define EFLG_VM (1<<17)
136#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200137#define EFLG_IOPL (3<<12)
138#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139#define EFLG_OF (1<<11)
140#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200141#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200142#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143#define EFLG_SF (1<<7)
144#define EFLG_ZF (1<<6)
145#define EFLG_AF (1<<4)
146#define EFLG_PF (1<<2)
147#define EFLG_CF (1<<0)
148
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300149#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
150#define EFLG_RESERVED_ONE_MASK 2
151
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152/*
153 * Instruction emulation:
154 * Most instructions are emulated directly via a fragment of inline assembly
155 * code. This allows us to save/restore EFLAGS and thus very easily pick up
156 * any modified flags.
157 */
158
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800159#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160#define _LO32 "k" /* force 32-bit operand */
161#define _STK "%%rsp" /* stack pointer */
162#elif defined(__i386__)
163#define _LO32 "" /* force 32-bit operand */
164#define _STK "%%esp" /* stack pointer */
165#endif
166
167/*
168 * These EFLAGS bits are restored from saved value during emulation, and
169 * any changes are written back to the saved value after emulation.
170 */
171#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
172
173/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200174#define _PRE_EFLAGS(_sav, _msk, _tmp) \
175 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
176 "movl %"_sav",%"_LO32 _tmp"; " \
177 "push %"_tmp"; " \
178 "push %"_tmp"; " \
179 "movl %"_msk",%"_LO32 _tmp"; " \
180 "andl %"_LO32 _tmp",("_STK"); " \
181 "pushf; " \
182 "notl %"_LO32 _tmp"; " \
183 "andl %"_LO32 _tmp",("_STK"); " \
184 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
185 "pop %"_tmp"; " \
186 "orl %"_LO32 _tmp",("_STK"); " \
187 "popf; " \
188 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189
190/* After executing instruction: write-back necessary bits in EFLAGS. */
191#define _POST_EFLAGS(_sav, _msk, _tmp) \
192 /* _sav |= EFLAGS & _msk; */ \
193 "pushf; " \
194 "pop %"_tmp"; " \
195 "andl %"_msk",%"_LO32 _tmp"; " \
196 "orl %"_LO32 _tmp",%"_sav"; "
197
Avi Kivitydda96d82008-11-26 15:14:10 +0200198#ifdef CONFIG_X86_64
199#define ON64(x) x
200#else
201#define ON64(x)
202#endif
203
Avi Kivityb3b3d252010-08-16 17:49:52 +0300204#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200205 do { \
206 __asm__ __volatile__ ( \
207 _PRE_EFLAGS("0", "4", "2") \
208 _op _suffix " %"_x"3,%1; " \
209 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300210 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200211 "=&r" (_tmp) \
212 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200213 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200214
215
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216/* Raw emulation: instruction has two explicit operands. */
217#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200218 do { \
219 unsigned long _tmp; \
220 \
221 switch ((_dst).bytes) { \
222 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300223 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200224 break; \
225 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300226 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200227 break; \
228 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300229 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200230 break; \
231 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 } while (0)
233
234#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
235 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200236 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400237 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300239 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 break; \
241 default: \
242 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
243 _wx, _wy, _lx, _ly, _qx, _qy); \
244 break; \
245 } \
246 } while (0)
247
248/* Source operand is byte-sized and may be restricted to just %cl. */
249#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
250 __emulate_2op(_op, _src, _dst, _eflags, \
251 "b", "c", "b", "c", "b", "c", "b", "c")
252
253/* Source operand is byte, word, long or quad sized. */
254#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
255 __emulate_2op(_op, _src, _dst, _eflags, \
256 "b", "q", "w", "r", _LO32, "r", "", "r")
257
258/* Source operand is word, long or quad sized. */
259#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
260 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
261 "w", "r", _LO32, "r", "", "r")
262
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100263/* Instruction has three operands and one operand is stored in ECX register */
264#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
265 do { \
266 unsigned long _tmp; \
267 _type _clv = (_cl).val; \
268 _type _srcv = (_src).val; \
269 _type _dstv = (_dst).val; \
270 \
271 __asm__ __volatile__ ( \
272 _PRE_EFLAGS("0", "5", "2") \
273 _op _suffix " %4,%1 \n" \
274 _POST_EFLAGS("0", "5", "2") \
275 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
276 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
277 ); \
278 \
279 (_cl).val = (unsigned long) _clv; \
280 (_src).val = (unsigned long) _srcv; \
281 (_dst).val = (unsigned long) _dstv; \
282 } while (0)
283
284#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
285 do { \
286 switch ((_dst).bytes) { \
287 case 2: \
288 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
289 "w", unsigned short); \
290 break; \
291 case 4: \
292 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
293 "l", unsigned int); \
294 break; \
295 case 8: \
296 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
297 "q", unsigned long)); \
298 break; \
299 } \
300 } while (0)
301
Avi Kivitydda96d82008-11-26 15:14:10 +0200302#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303 do { \
304 unsigned long _tmp; \
305 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200306 __asm__ __volatile__ ( \
307 _PRE_EFLAGS("0", "3", "2") \
308 _op _suffix " %1; " \
309 _POST_EFLAGS("0", "3", "2") \
310 : "=m" (_eflags), "+m" ((_dst).val), \
311 "=&r" (_tmp) \
312 : "i" (EFLAGS_MASK)); \
313 } while (0)
314
315/* Instruction has only one explicit operand (no source operand). */
316#define emulate_1op(_op, _dst, _eflags) \
317 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400318 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200319 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
320 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
321 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
322 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323 } \
324 } while (0)
325
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300326#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
327 do { \
328 unsigned long _tmp; \
329 \
330 __asm__ __volatile__ ( \
331 _PRE_EFLAGS("0", "4", "1") \
332 _op _suffix " %5; " \
333 _POST_EFLAGS("0", "4", "1") \
334 : "=m" (_eflags), "=&r" (_tmp), \
335 "+a" (_rax), "+d" (_rdx) \
336 : "i" (EFLAGS_MASK), "m" ((_src).val), \
337 "a" (_rax), "d" (_rdx)); \
338 } while (0)
339
Avi Kivityf6b35972010-08-26 11:59:00 +0300340#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
341 do { \
342 unsigned long _tmp; \
343 \
344 __asm__ __volatile__ ( \
345 _PRE_EFLAGS("0", "5", "1") \
346 "1: \n\t" \
347 _op _suffix " %6; " \
348 "2: \n\t" \
349 _POST_EFLAGS("0", "5", "1") \
350 ".pushsection .fixup,\"ax\" \n\t" \
351 "3: movb $1, %4 \n\t" \
352 "jmp 2b \n\t" \
353 ".popsection \n\t" \
354 _ASM_EXTABLE(1b, 3b) \
355 : "=m" (_eflags), "=&r" (_tmp), \
356 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
357 : "i" (EFLAGS_MASK), "m" ((_src).val), \
358 "a" (_rax), "d" (_rdx)); \
359 } while (0)
360
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300361/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
362#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
363 do { \
364 switch((_src).bytes) { \
365 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
366 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
367 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
368 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
369 } \
370 } while (0)
371
Avi Kivityf6b35972010-08-26 11:59:00 +0300372#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
373 do { \
374 switch((_src).bytes) { \
375 case 1: \
376 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
377 _eflags, "b", _ex); \
378 break; \
379 case 2: \
380 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
381 _eflags, "w", _ex); \
382 break; \
383 case 4: \
384 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
385 _eflags, "l", _ex); \
386 break; \
387 case 8: ON64( \
388 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
389 _eflags, "q", _ex)); \
390 break; \
391 } \
392 } while (0)
393
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394/* Fetch next part of the instruction being emulated. */
395#define insn_fetch(_type, _size, _eip) \
396({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200397 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200398 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 goto done; \
400 (_eip) += (_size); \
401 (_type)_x; \
402})
403
Gleb Natapov414e6272010-04-28 19:15:26 +0300404#define insn_fetch_arr(_arr, _size, _eip) \
405({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
406 if (rc != X86EMUL_CONTINUE) \
407 goto done; \
408 (_eip) += (_size); \
409})
410
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800411static inline unsigned long ad_mask(struct decode_cache *c)
412{
413 return (1UL << (c->ad_bytes << 3)) - 1;
414}
415
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800417static inline unsigned long
418address_mask(struct decode_cache *c, unsigned long reg)
419{
420 if (c->ad_bytes == sizeof(unsigned long))
421 return reg;
422 else
423 return reg & ad_mask(c);
424}
425
426static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200427register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800428{
Avi Kivity90de84f2010-11-17 15:28:21 +0200429 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800430}
431
Harvey Harrison7a9572752008-02-19 07:40:41 -0800432static inline void
433register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
434{
435 if (c->ad_bytes == sizeof(unsigned long))
436 *reg += inc;
437 else
438 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
439}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440
Harvey Harrison7a9572752008-02-19 07:40:41 -0800441static inline void jmp_rel(struct decode_cache *c, int rel)
442{
443 register_address_increment(c, &c->eip, rel);
444}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300445
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300446static void set_seg_override(struct decode_cache *c, int seg)
447{
448 c->has_seg_override = true;
449 c->seg_override = seg;
450}
451
Gleb Natapov79168fd2010-04-28 19:15:30 +0300452static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
453 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300454{
455 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
456 return 0;
457
Gleb Natapov79168fd2010-04-28 19:15:30 +0300458 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300459}
460
Avi Kivity90de84f2010-11-17 15:28:21 +0200461static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
462 struct x86_emulate_ops *ops,
463 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300464{
465 if (!c->has_seg_override)
466 return 0;
467
Avi Kivity90de84f2010-11-17 15:28:21 +0200468 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300469}
470
Avi Kivity90de84f2010-11-17 15:28:21 +0200471static ulong linear(struct x86_emulate_ctxt *ctxt,
472 struct segmented_address addr)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300473{
Avi Kivity90de84f2010-11-17 15:28:21 +0200474 struct decode_cache *c = &ctxt->decode;
475 ulong la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300476
Avi Kivity90de84f2010-11-17 15:28:21 +0200477 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
478 if (c->ad_bytes != 8)
479 la &= (u32)-1;
480 return la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300481}
482
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200483static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
484 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300485{
Avi Kivityda9cb572010-11-22 17:53:21 +0200486 ctxt->exception.vector = vec;
487 ctxt->exception.error_code = error;
488 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200489 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300490}
491
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200492static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300493{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200494 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300495}
496
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200497static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300498{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200499 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300500}
501
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200502static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300503{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200504 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300505}
506
Avi Kivity34d1f492010-08-26 11:59:01 +0300507static int emulate_de(struct x86_emulate_ctxt *ctxt)
508{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200509 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300510}
511
Avi Kivity12537912011-03-29 11:41:27 +0200512static int emulate_nm(struct x86_emulate_ctxt *ctxt)
513{
514 return emulate_exception(ctxt, NM_VECTOR, 0, false);
515}
516
Avi Kivity62266862007-11-20 13:15:52 +0200517static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
518 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300519 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200520{
521 struct fetch_cache *fc = &ctxt->decode.fetch;
522 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300523 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200524
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300525 if (eip == fc->end) {
526 cur_size = fc->end - fc->start;
527 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
528 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200529 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900530 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200531 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300532 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200533 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300534 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900535 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200536}
537
538static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
539 struct x86_emulate_ops *ops,
540 unsigned long eip, void *dest, unsigned size)
541{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900542 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200543
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200544 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200545 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200546 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200547 while (size--) {
548 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900549 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200550 return rc;
551 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900552 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200553}
554
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000555/*
556 * Given the 'reg' portion of a ModRM byte, and a register block, return a
557 * pointer into the block that addresses the relevant register.
558 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
559 */
560static void *decode_register(u8 modrm_reg, unsigned long *regs,
561 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562{
563 void *p;
564
565 p = &regs[modrm_reg];
566 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
567 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
568 return p;
569}
570
571static int read_descriptor(struct x86_emulate_ctxt *ctxt,
572 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200573 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574 u16 *size, unsigned long *address, int op_bytes)
575{
576 int rc;
577
578 if (op_bytes == 2)
579 op_bytes = 3;
580 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200581 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200582 ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900583 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200585 addr.ea += 2;
586 rc = ops->read_std(linear(ctxt, addr), address, op_bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200587 ctxt->vcpu, &ctxt->exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 return rc;
589}
590
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300591static int test_cc(unsigned int condition, unsigned int flags)
592{
593 int rc = 0;
594
595 switch ((condition & 15) >> 1) {
596 case 0: /* o */
597 rc |= (flags & EFLG_OF);
598 break;
599 case 1: /* b/c/nae */
600 rc |= (flags & EFLG_CF);
601 break;
602 case 2: /* z/e */
603 rc |= (flags & EFLG_ZF);
604 break;
605 case 3: /* be/na */
606 rc |= (flags & (EFLG_CF|EFLG_ZF));
607 break;
608 case 4: /* s */
609 rc |= (flags & EFLG_SF);
610 break;
611 case 5: /* p/pe */
612 rc |= (flags & EFLG_PF);
613 break;
614 case 7: /* le/ng */
615 rc |= (flags & EFLG_ZF);
616 /* fall through */
617 case 6: /* l/nge */
618 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
619 break;
620 }
621
622 /* Odd condition identifiers (lsb == 1) have inverted sense. */
623 return (!!rc ^ (condition & 1));
624}
625
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300626static void fetch_register_operand(struct operand *op)
627{
628 switch (op->bytes) {
629 case 1:
630 op->val = *(u8 *)op->addr.reg;
631 break;
632 case 2:
633 op->val = *(u16 *)op->addr.reg;
634 break;
635 case 4:
636 op->val = *(u32 *)op->addr.reg;
637 break;
638 case 8:
639 op->val = *(u64 *)op->addr.reg;
640 break;
641 }
642}
643
Avi Kivity12537912011-03-29 11:41:27 +0200644static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
645{
646 ctxt->ops->get_fpu(ctxt);
647 switch (reg) {
648 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
649 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
650 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
651 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
652 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
653 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
654 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
655 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
656#ifdef CONFIG_X86_64
657 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
658 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
659 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
660 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
661 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
662 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
663 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
664 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
665#endif
666 default: BUG();
667 }
668 ctxt->ops->put_fpu(ctxt);
669}
670
671static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
672 int reg)
673{
674 ctxt->ops->get_fpu(ctxt);
675 switch (reg) {
676 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
677 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
678 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
679 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
680 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
681 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
682 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
683 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
684#ifdef CONFIG_X86_64
685 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
686 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
687 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
688 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
689 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
690 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
691 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
692 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
693#endif
694 default: BUG();
695 }
696 ctxt->ops->put_fpu(ctxt);
697}
698
699static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
700 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200701 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200702 int inhibit_bytereg)
703{
Avi Kivity33615aa2007-10-31 11:15:56 +0200704 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200705 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200706
707 if (!(c->d & ModRM))
708 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200709
710 if (c->d & Sse) {
711 op->type = OP_XMM;
712 op->bytes = 16;
713 op->addr.xmm = reg;
714 read_sse_reg(ctxt, &op->vec_val, reg);
715 return;
716 }
717
Avi Kivity3c118e22007-10-31 10:27:04 +0200718 op->type = OP_REG;
719 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300720 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200721 op->bytes = 1;
722 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300723 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200724 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200725 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300726 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200727 op->orig_val = op->val;
728}
729
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200730static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300731 struct x86_emulate_ops *ops,
732 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200733{
734 struct decode_cache *c = &ctxt->decode;
735 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700736 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900737 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300738 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200739
740 if (c->rex_prefix) {
741 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
742 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
743 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
744 }
745
746 c->modrm = insn_fetch(u8, 1, c->eip);
747 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
748 c->modrm_reg |= (c->modrm & 0x38) >> 3;
749 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300750 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200751
752 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300753 op->type = OP_REG;
754 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
755 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300756 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200757 if (c->d & Sse) {
758 op->type = OP_XMM;
759 op->bytes = 16;
760 op->addr.xmm = c->modrm_rm;
761 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
762 return rc;
763 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300764 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200765 return rc;
766 }
767
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300768 op->type = OP_MEM;
769
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200770 if (c->ad_bytes == 2) {
771 unsigned bx = c->regs[VCPU_REGS_RBX];
772 unsigned bp = c->regs[VCPU_REGS_RBP];
773 unsigned si = c->regs[VCPU_REGS_RSI];
774 unsigned di = c->regs[VCPU_REGS_RDI];
775
776 /* 16-bit ModR/M decode. */
777 switch (c->modrm_mod) {
778 case 0:
779 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300780 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200781 break;
782 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300783 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200784 break;
785 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300786 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200787 break;
788 }
789 switch (c->modrm_rm) {
790 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300791 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200792 break;
793 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300794 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200795 break;
796 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300797 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200798 break;
799 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300800 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200801 break;
802 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300803 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200804 break;
805 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300806 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200807 break;
808 case 6:
809 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300810 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200811 break;
812 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300813 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200814 break;
815 }
816 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
817 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300818 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300819 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200820 } else {
821 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700822 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200823 sib = insn_fetch(u8, 1, c->eip);
824 index_reg |= (sib >> 3) & 7;
825 base_reg |= sib & 7;
826 scale = sib >> 6;
827
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700828 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300829 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700830 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300831 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700832 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300833 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700834 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
835 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700836 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700837 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300838 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200839 switch (c->modrm_mod) {
840 case 0:
841 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300842 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200843 break;
844 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300845 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200846 break;
847 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300848 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200849 break;
850 }
851 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200852 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200853done:
854 return rc;
855}
856
857static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300858 struct x86_emulate_ops *ops,
859 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200860{
861 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900862 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200863
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300864 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200865 switch (c->ad_bytes) {
866 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200867 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200868 break;
869 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200870 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871 break;
872 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200873 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200874 break;
875 }
876done:
877 return rc;
878}
879
Wei Yongjun35c843c2010-08-09 11:34:56 +0800880static void fetch_bit_operand(struct decode_cache *c)
881{
Sheng Yang7129eec2010-09-28 16:33:32 +0800882 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800883
Wei Yongjun3885f182010-08-09 11:37:37 +0800884 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800885 mask = ~(c->dst.bytes * 8 - 1);
886
887 if (c->src.bytes == 2)
888 sv = (s16)c->src.val & (s16)mask;
889 else if (c->src.bytes == 4)
890 sv = (s32)c->src.val & (s32)mask;
891
Avi Kivity90de84f2010-11-17 15:28:21 +0200892 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800893 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800894
895 /* only subword offset */
896 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800897}
898
Gleb Natapov9de41572010-04-28 19:15:22 +0300899static int read_emulated(struct x86_emulate_ctxt *ctxt,
900 struct x86_emulate_ops *ops,
901 unsigned long addr, void *dest, unsigned size)
902{
903 int rc;
904 struct read_cache *mc = &ctxt->decode.mem_read;
905
906 while (size) {
907 int n = min(size, 8u);
908 size -= n;
909 if (mc->pos < mc->end)
910 goto read_cached;
911
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200912 rc = ops->read_emulated(addr, mc->data + mc->end, n,
913 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +0300914 if (rc != X86EMUL_CONTINUE)
915 return rc;
916 mc->end += n;
917
918 read_cached:
919 memcpy(dest, mc->data + mc->pos, n);
920 mc->pos += n;
921 dest += n;
922 addr += n;
923 }
924 return X86EMUL_CONTINUE;
925}
926
Gleb Natapov7b262e92010-03-18 15:20:27 +0200927static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
928 struct x86_emulate_ops *ops,
929 unsigned int size, unsigned short port,
930 void *dest)
931{
932 struct read_cache *rc = &ctxt->decode.io_read;
933
934 if (rc->pos == rc->end) { /* refill pio read ahead */
935 struct decode_cache *c = &ctxt->decode;
936 unsigned int in_page, n;
937 unsigned int count = c->rep_prefix ?
938 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
939 in_page = (ctxt->eflags & EFLG_DF) ?
940 offset_in_page(c->regs[VCPU_REGS_RDI]) :
941 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
942 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
943 count);
944 if (n == 0)
945 n = 1;
946 rc->pos = rc->end = 0;
947 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
948 return 0;
949 rc->end = n * size;
950 }
951
952 memcpy(dest, rc->data + rc->pos, size);
953 rc->pos += size;
954 return 1;
955}
956
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200957static u32 desc_limit_scaled(struct desc_struct *desc)
958{
959 u32 limit = get_desc_limit(desc);
960
961 return desc->g ? (limit << 12) | 0xfff : limit;
962}
963
964static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
965 struct x86_emulate_ops *ops,
966 u16 selector, struct desc_ptr *dt)
967{
968 if (selector & 1 << 2) {
969 struct desc_struct desc;
970 memset (dt, 0, sizeof *dt);
Gleb Natapov5601d052011-03-07 14:55:06 +0200971 if (!ops->get_cached_descriptor(&desc, NULL, VCPU_SREG_LDTR,
972 ctxt->vcpu))
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200973 return;
974
975 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
976 dt->address = get_desc_base(&desc);
977 } else
978 ops->get_gdt(dt, ctxt->vcpu);
979}
980
981/* allowed just for 8 bytes segments */
982static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
983 struct x86_emulate_ops *ops,
984 u16 selector, struct desc_struct *desc)
985{
986 struct desc_ptr dt;
987 u16 index = selector >> 3;
988 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200989 ulong addr;
990
991 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
992
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200993 if (dt.size < index * 8 + 7)
994 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200995 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200996 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
997 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200998
999 return ret;
1000}
1001
1002/* allowed just for 8 bytes segments */
1003static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1004 struct x86_emulate_ops *ops,
1005 u16 selector, struct desc_struct *desc)
1006{
1007 struct desc_ptr dt;
1008 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001009 ulong addr;
1010 int ret;
1011
1012 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1013
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001014 if (dt.size < index * 8 + 7)
1015 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001016
1017 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001018 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
1019 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001020
1021 return ret;
1022}
1023
Gleb Natapov5601d052011-03-07 14:55:06 +02001024/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001025static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1026 struct x86_emulate_ops *ops,
1027 u16 selector, int seg)
1028{
1029 struct desc_struct seg_desc;
1030 u8 dpl, rpl, cpl;
1031 unsigned err_vec = GP_VECTOR;
1032 u32 err_code = 0;
1033 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1034 int ret;
1035
1036 memset(&seg_desc, 0, sizeof seg_desc);
1037
1038 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1039 || ctxt->mode == X86EMUL_MODE_REAL) {
1040 /* set real mode segment descriptor */
1041 set_desc_base(&seg_desc, selector << 4);
1042 set_desc_limit(&seg_desc, 0xffff);
1043 seg_desc.type = 3;
1044 seg_desc.p = 1;
1045 seg_desc.s = 1;
1046 goto load;
1047 }
1048
1049 /* NULL selector is not valid for TR, CS and SS */
1050 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1051 && null_selector)
1052 goto exception;
1053
1054 /* TR should be in GDT only */
1055 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1056 goto exception;
1057
1058 if (null_selector) /* for NULL selector skip all following checks */
1059 goto load;
1060
1061 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1062 if (ret != X86EMUL_CONTINUE)
1063 return ret;
1064
1065 err_code = selector & 0xfffc;
1066 err_vec = GP_VECTOR;
1067
1068 /* can't load system descriptor into segment selecor */
1069 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1070 goto exception;
1071
1072 if (!seg_desc.p) {
1073 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1074 goto exception;
1075 }
1076
1077 rpl = selector & 3;
1078 dpl = seg_desc.dpl;
1079 cpl = ops->cpl(ctxt->vcpu);
1080
1081 switch (seg) {
1082 case VCPU_SREG_SS:
1083 /*
1084 * segment is not a writable data segment or segment
1085 * selector's RPL != CPL or segment selector's RPL != CPL
1086 */
1087 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1088 goto exception;
1089 break;
1090 case VCPU_SREG_CS:
1091 if (!(seg_desc.type & 8))
1092 goto exception;
1093
1094 if (seg_desc.type & 4) {
1095 /* conforming */
1096 if (dpl > cpl)
1097 goto exception;
1098 } else {
1099 /* nonconforming */
1100 if (rpl > cpl || dpl != cpl)
1101 goto exception;
1102 }
1103 /* CS(RPL) <- CPL */
1104 selector = (selector & 0xfffc) | cpl;
1105 break;
1106 case VCPU_SREG_TR:
1107 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1108 goto exception;
1109 break;
1110 case VCPU_SREG_LDTR:
1111 if (seg_desc.s || seg_desc.type != 2)
1112 goto exception;
1113 break;
1114 default: /* DS, ES, FS, or GS */
1115 /*
1116 * segment is not a data or readable code segment or
1117 * ((segment is a data or nonconforming code segment)
1118 * and (both RPL and CPL > DPL))
1119 */
1120 if ((seg_desc.type & 0xa) == 0x8 ||
1121 (((seg_desc.type & 0xc) != 0xc) &&
1122 (rpl > dpl && cpl > dpl)))
1123 goto exception;
1124 break;
1125 }
1126
1127 if (seg_desc.s) {
1128 /* mark segment as accessed */
1129 seg_desc.type |= 1;
1130 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1131 if (ret != X86EMUL_CONTINUE)
1132 return ret;
1133 }
1134load:
1135 ops->set_segment_selector(selector, seg, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001136 ops->set_cached_descriptor(&seg_desc, 0, seg, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001137 return X86EMUL_CONTINUE;
1138exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001139 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001140 return X86EMUL_PROPAGATE_FAULT;
1141}
1142
Wei Yongjun31be40b2010-08-17 09:17:30 +08001143static void write_register_operand(struct operand *op)
1144{
1145 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1146 switch (op->bytes) {
1147 case 1:
1148 *(u8 *)op->addr.reg = (u8)op->val;
1149 break;
1150 case 2:
1151 *(u16 *)op->addr.reg = (u16)op->val;
1152 break;
1153 case 4:
1154 *op->addr.reg = (u32)op->val;
1155 break; /* 64b: zero-extend */
1156 case 8:
1157 *op->addr.reg = op->val;
1158 break;
1159 }
1160}
1161
Wei Yongjunc37eda12010-06-15 09:03:33 +08001162static inline int writeback(struct x86_emulate_ctxt *ctxt,
1163 struct x86_emulate_ops *ops)
1164{
1165 int rc;
1166 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001167
1168 switch (c->dst.type) {
1169 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001170 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001171 break;
1172 case OP_MEM:
1173 if (c->lock_prefix)
1174 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001175 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001176 &c->dst.orig_val,
1177 &c->dst.val,
1178 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001179 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001180 ctxt->vcpu);
1181 else
1182 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001183 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001184 &c->dst.val,
1185 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001186 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001187 ctxt->vcpu);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001188 if (rc != X86EMUL_CONTINUE)
1189 return rc;
1190 break;
Avi Kivity12537912011-03-29 11:41:27 +02001191 case OP_XMM:
1192 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1193 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001194 case OP_NONE:
1195 /* no writeback */
1196 break;
1197 default:
1198 break;
1199 }
1200 return X86EMUL_CONTINUE;
1201}
1202
Gleb Natapov79168fd2010-04-28 19:15:30 +03001203static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1204 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001205{
1206 struct decode_cache *c = &ctxt->decode;
1207
1208 c->dst.type = OP_MEM;
1209 c->dst.bytes = c->op_bytes;
1210 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001211 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001212 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1213 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001214}
1215
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001216static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001217 struct x86_emulate_ops *ops,
1218 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001219{
1220 struct decode_cache *c = &ctxt->decode;
1221 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001222 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001223
Avi Kivity90de84f2010-11-17 15:28:21 +02001224 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1225 addr.seg = VCPU_SREG_SS;
1226 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001227 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001228 return rc;
1229
Avi Kivity350f69d2009-01-05 11:12:40 +02001230 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001231 return rc;
1232}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001233
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001234static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1235 struct x86_emulate_ops *ops,
1236 void *dest, int len)
1237{
1238 int rc;
1239 unsigned long val, change_mask;
1240 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001241 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001242
1243 rc = emulate_pop(ctxt, ops, &val, len);
1244 if (rc != X86EMUL_CONTINUE)
1245 return rc;
1246
1247 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1248 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1249
1250 switch(ctxt->mode) {
1251 case X86EMUL_MODE_PROT64:
1252 case X86EMUL_MODE_PROT32:
1253 case X86EMUL_MODE_PROT16:
1254 if (cpl == 0)
1255 change_mask |= EFLG_IOPL;
1256 if (cpl <= iopl)
1257 change_mask |= EFLG_IF;
1258 break;
1259 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001260 if (iopl < 3)
1261 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001262 change_mask |= EFLG_IF;
1263 break;
1264 default: /* real mode */
1265 change_mask |= (EFLG_IOPL | EFLG_IF);
1266 break;
1267 }
1268
1269 *(unsigned long *)dest =
1270 (ctxt->eflags & ~change_mask) | (val & change_mask);
1271
1272 return rc;
1273}
1274
Gleb Natapov79168fd2010-04-28 19:15:30 +03001275static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1276 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001277{
1278 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001279
Gleb Natapov79168fd2010-04-28 19:15:30 +03001280 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001281
Gleb Natapov79168fd2010-04-28 19:15:30 +03001282 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001283}
1284
1285static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1286 struct x86_emulate_ops *ops, int seg)
1287{
1288 struct decode_cache *c = &ctxt->decode;
1289 unsigned long selector;
1290 int rc;
1291
1292 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001293 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001294 return rc;
1295
Gleb Natapov2e873022010-03-18 15:20:18 +02001296 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001297 return rc;
1298}
1299
Wei Yongjunc37eda12010-06-15 09:03:33 +08001300static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001301 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001302{
1303 struct decode_cache *c = &ctxt->decode;
1304 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001305 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001306 int reg = VCPU_REGS_RAX;
1307
1308 while (reg <= VCPU_REGS_RDI) {
1309 (reg == VCPU_REGS_RSP) ?
1310 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1311
Gleb Natapov79168fd2010-04-28 19:15:30 +03001312 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001313
1314 rc = writeback(ctxt, ops);
1315 if (rc != X86EMUL_CONTINUE)
1316 return rc;
1317
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001318 ++reg;
1319 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001320
1321 /* Disable writeback. */
1322 c->dst.type = OP_NONE;
1323
1324 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001325}
1326
1327static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1328 struct x86_emulate_ops *ops)
1329{
1330 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001331 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001332 int reg = VCPU_REGS_RDI;
1333
1334 while (reg >= VCPU_REGS_RAX) {
1335 if (reg == VCPU_REGS_RSP) {
1336 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1337 c->op_bytes);
1338 --reg;
1339 }
1340
1341 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001342 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001343 break;
1344 --reg;
1345 }
1346 return rc;
1347}
1348
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001349int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1350 struct x86_emulate_ops *ops, int irq)
1351{
1352 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001353 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001354 struct desc_ptr dt;
1355 gva_t cs_addr;
1356 gva_t eip_addr;
1357 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001358
1359 /* TODO: Add limit checks */
1360 c->src.val = ctxt->eflags;
1361 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001362 rc = writeback(ctxt, ops);
1363 if (rc != X86EMUL_CONTINUE)
1364 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001365
1366 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1367
1368 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1369 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001370 rc = writeback(ctxt, ops);
1371 if (rc != X86EMUL_CONTINUE)
1372 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001373
1374 c->src.val = c->eip;
1375 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001376 rc = writeback(ctxt, ops);
1377 if (rc != X86EMUL_CONTINUE)
1378 return rc;
1379
1380 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001381
1382 ops->get_idt(&dt, ctxt->vcpu);
1383
1384 eip_addr = dt.address + (irq << 2);
1385 cs_addr = dt.address + (irq << 2) + 2;
1386
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001387 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001388 if (rc != X86EMUL_CONTINUE)
1389 return rc;
1390
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001391 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001392 if (rc != X86EMUL_CONTINUE)
1393 return rc;
1394
1395 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1396 if (rc != X86EMUL_CONTINUE)
1397 return rc;
1398
1399 c->eip = eip;
1400
1401 return rc;
1402}
1403
1404static int emulate_int(struct x86_emulate_ctxt *ctxt,
1405 struct x86_emulate_ops *ops, int irq)
1406{
1407 switch(ctxt->mode) {
1408 case X86EMUL_MODE_REAL:
1409 return emulate_int_real(ctxt, ops, irq);
1410 case X86EMUL_MODE_VM86:
1411 case X86EMUL_MODE_PROT16:
1412 case X86EMUL_MODE_PROT32:
1413 case X86EMUL_MODE_PROT64:
1414 default:
1415 /* Protected mode interrupts unimplemented yet */
1416 return X86EMUL_UNHANDLEABLE;
1417 }
1418}
1419
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001420static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1421 struct x86_emulate_ops *ops)
1422{
1423 struct decode_cache *c = &ctxt->decode;
1424 int rc = X86EMUL_CONTINUE;
1425 unsigned long temp_eip = 0;
1426 unsigned long temp_eflags = 0;
1427 unsigned long cs = 0;
1428 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1429 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1430 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1431 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1432
1433 /* TODO: Add stack limit check */
1434
1435 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1436
1437 if (rc != X86EMUL_CONTINUE)
1438 return rc;
1439
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001440 if (temp_eip & ~0xffff)
1441 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001442
1443 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1444
1445 if (rc != X86EMUL_CONTINUE)
1446 return rc;
1447
1448 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1449
1450 if (rc != X86EMUL_CONTINUE)
1451 return rc;
1452
1453 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1454
1455 if (rc != X86EMUL_CONTINUE)
1456 return rc;
1457
1458 c->eip = temp_eip;
1459
1460
1461 if (c->op_bytes == 4)
1462 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1463 else if (c->op_bytes == 2) {
1464 ctxt->eflags &= ~0xffff;
1465 ctxt->eflags |= temp_eflags;
1466 }
1467
1468 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1469 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1470
1471 return rc;
1472}
1473
1474static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1475 struct x86_emulate_ops* ops)
1476{
1477 switch(ctxt->mode) {
1478 case X86EMUL_MODE_REAL:
1479 return emulate_iret_real(ctxt, ops);
1480 case X86EMUL_MODE_VM86:
1481 case X86EMUL_MODE_PROT16:
1482 case X86EMUL_MODE_PROT32:
1483 case X86EMUL_MODE_PROT64:
1484 default:
1485 /* iret from protected mode unimplemented yet */
1486 return X86EMUL_UNHANDLEABLE;
1487 }
1488}
1489
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001490static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1491 struct x86_emulate_ops *ops)
1492{
1493 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001494
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001495 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001496}
1497
Laurent Vivier05f086f2007-09-24 11:10:55 +02001498static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001499{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001500 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001501 switch (c->modrm_reg) {
1502 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001503 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001504 break;
1505 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001506 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001507 break;
1508 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001509 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001510 break;
1511 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001512 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001513 break;
1514 case 4: /* sal/shl */
1515 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001516 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001517 break;
1518 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001519 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001520 break;
1521 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001522 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001523 break;
1524 }
1525}
1526
1527static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001528 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529{
1530 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001531 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1532 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001533 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001534
1535 switch (c->modrm_reg) {
1536 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001537 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001538 break;
1539 case 2: /* not */
1540 c->dst.val = ~c->dst.val;
1541 break;
1542 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001543 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001544 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001545 case 4: /* mul */
1546 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1547 break;
1548 case 5: /* imul */
1549 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1550 break;
1551 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001552 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1553 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001554 break;
1555 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001556 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1557 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001558 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001559 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001560 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001561 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001562 if (de)
1563 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001564 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001565}
1566
1567static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001568 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001569{
1570 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001571
1572 switch (c->modrm_reg) {
1573 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001574 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001575 break;
1576 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001577 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001578 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001579 case 2: /* call near abs */ {
1580 long int old_eip;
1581 old_eip = c->eip;
1582 c->eip = c->src.val;
1583 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001584 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001585 break;
1586 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001587 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001588 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001589 break;
1590 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001591 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001592 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001593 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001594 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001595}
1596
1597static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001598 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001599{
1600 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001601 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001602
1603 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1604 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001605 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1606 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001607 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001608 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001609 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1610 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001611
Laurent Vivier05f086f2007-09-24 11:10:55 +02001612 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001613 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001614 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001615}
1616
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001617static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1618 struct x86_emulate_ops *ops)
1619{
1620 struct decode_cache *c = &ctxt->decode;
1621 int rc;
1622 unsigned long cs;
1623
1624 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001625 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001626 return rc;
1627 if (c->op_bytes == 4)
1628 c->eip = (u32)c->eip;
1629 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001630 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001631 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001632 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001633 return rc;
1634}
1635
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001636static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1637 struct x86_emulate_ops *ops, int seg)
1638{
1639 struct decode_cache *c = &ctxt->decode;
1640 unsigned short sel;
1641 int rc;
1642
1643 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1644
1645 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1646 if (rc != X86EMUL_CONTINUE)
1647 return rc;
1648
1649 c->dst.val = c->src.val;
1650 return rc;
1651}
1652
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001653static inline void
1654setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001655 struct x86_emulate_ops *ops, struct desc_struct *cs,
1656 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001657{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001658 memset(cs, 0, sizeof(struct desc_struct));
Gleb Natapov5601d052011-03-07 14:55:06 +02001659 ops->get_cached_descriptor(cs, NULL, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001660 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001661
1662 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001663 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001664 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001665 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001666 cs->type = 0x0b; /* Read, Execute, Accessed */
1667 cs->s = 1;
1668 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001669 cs->p = 1;
1670 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001671
Gleb Natapov79168fd2010-04-28 19:15:30 +03001672 set_desc_base(ss, 0); /* flat segment */
1673 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001674 ss->g = 1; /* 4kb granularity */
1675 ss->s = 1;
1676 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001677 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001678 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001679 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001680}
1681
1682static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001683emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001684{
1685 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001686 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001687 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001688 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001689
1690 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001691 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001692 ctxt->mode == X86EMUL_MODE_VM86)
1693 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001694
Gleb Natapov79168fd2010-04-28 19:15:30 +03001695 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001696
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001697 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001698 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001699 cs_sel = (u16)(msr_data & 0xfffc);
1700 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001701
1702 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001703 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001704 cs.l = 1;
1705 }
Gleb Natapov5601d052011-03-07 14:55:06 +02001706 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001707 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001708 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001709 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001710
1711 c->regs[VCPU_REGS_RCX] = c->eip;
1712 if (is_long_mode(ctxt->vcpu)) {
1713#ifdef CONFIG_X86_64
1714 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1715
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001716 ops->get_msr(ctxt->vcpu,
1717 ctxt->mode == X86EMUL_MODE_PROT64 ?
1718 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001719 c->eip = msr_data;
1720
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001721 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001722 ctxt->eflags &= ~(msr_data | EFLG_RF);
1723#endif
1724 } else {
1725 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001726 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001727 c->eip = (u32)msr_data;
1728
1729 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1730 }
1731
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001732 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001733}
1734
Andre Przywara8c604352009-06-18 12:56:01 +02001735static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001736emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001737{
1738 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001739 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001740 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001741 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001742
Gleb Natapova0044752010-02-10 14:21:31 +02001743 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001744 if (ctxt->mode == X86EMUL_MODE_REAL)
1745 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001746
1747 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1748 * Therefore, we inject an #UD.
1749 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001750 if (ctxt->mode == X86EMUL_MODE_PROT64)
1751 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001752
Gleb Natapov79168fd2010-04-28 19:15:30 +03001753 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001754
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001755 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001756 switch (ctxt->mode) {
1757 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001758 if ((msr_data & 0xfffc) == 0x0)
1759 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001760 break;
1761 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001762 if (msr_data == 0x0)
1763 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001764 break;
1765 }
1766
1767 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001768 cs_sel = (u16)msr_data;
1769 cs_sel &= ~SELECTOR_RPL_MASK;
1770 ss_sel = cs_sel + 8;
1771 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001772 if (ctxt->mode == X86EMUL_MODE_PROT64
1773 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001774 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001775 cs.l = 1;
1776 }
1777
Gleb Natapov5601d052011-03-07 14:55:06 +02001778 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001779 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001780 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001781 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001782
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001783 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001784 c->eip = msr_data;
1785
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001786 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001787 c->regs[VCPU_REGS_RSP] = msr_data;
1788
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001789 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001790}
1791
Andre Przywara4668f052009-06-18 12:56:02 +02001792static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001793emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001794{
1795 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001796 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001797 u64 msr_data;
1798 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001799 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001800
Gleb Natapova0044752010-02-10 14:21:31 +02001801 /* inject #GP if in real mode or Virtual 8086 mode */
1802 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001803 ctxt->mode == X86EMUL_MODE_VM86)
1804 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001805
Gleb Natapov79168fd2010-04-28 19:15:30 +03001806 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001807
1808 if ((c->rex_prefix & 0x8) != 0x0)
1809 usermode = X86EMUL_MODE_PROT64;
1810 else
1811 usermode = X86EMUL_MODE_PROT32;
1812
1813 cs.dpl = 3;
1814 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001815 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001816 switch (usermode) {
1817 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001818 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001819 if ((msr_data & 0xfffc) == 0x0)
1820 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001821 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001822 break;
1823 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001824 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001825 if (msr_data == 0x0)
1826 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001827 ss_sel = cs_sel + 8;
1828 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001829 cs.l = 1;
1830 break;
1831 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001832 cs_sel |= SELECTOR_RPL_MASK;
1833 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001834
Gleb Natapov5601d052011-03-07 14:55:06 +02001835 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001836 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001837 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001838 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001839
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001840 c->eip = c->regs[VCPU_REGS_RDX];
1841 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001842
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001843 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001844}
1845
Gleb Natapov9c537242010-03-18 15:20:05 +02001846static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1847 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001848{
1849 int iopl;
1850 if (ctxt->mode == X86EMUL_MODE_REAL)
1851 return false;
1852 if (ctxt->mode == X86EMUL_MODE_VM86)
1853 return true;
1854 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001855 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001856}
1857
1858static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1859 struct x86_emulate_ops *ops,
1860 u16 port, u16 len)
1861{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001862 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02001863 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001864 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001865 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001866 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02001867 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001868
Gleb Natapov5601d052011-03-07 14:55:06 +02001869 ops->get_cached_descriptor(&tr_seg, &base3, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001870 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001871 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001872 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001873 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02001874 base = get_desc_base(&tr_seg);
1875#ifdef CONFIG_X86_64
1876 base |= ((u64)base3) << 32;
1877#endif
1878 r = ops->read_std(base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001879 if (r != X86EMUL_CONTINUE)
1880 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001881 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001882 return false;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001883 r = ops->read_std(base + io_bitmap_ptr + port/8, &perm, 2, ctxt->vcpu,
Gleb Natapov5601d052011-03-07 14:55:06 +02001884 NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001885 if (r != X86EMUL_CONTINUE)
1886 return false;
1887 if ((perm >> bit_idx) & mask)
1888 return false;
1889 return true;
1890}
1891
1892static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1893 struct x86_emulate_ops *ops,
1894 u16 port, u16 len)
1895{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001896 if (ctxt->perm_ok)
1897 return true;
1898
Gleb Natapov9c537242010-03-18 15:20:05 +02001899 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001900 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1901 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001902
1903 ctxt->perm_ok = true;
1904
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001905 return true;
1906}
1907
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001908static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1909 struct x86_emulate_ops *ops,
1910 struct tss_segment_16 *tss)
1911{
1912 struct decode_cache *c = &ctxt->decode;
1913
1914 tss->ip = c->eip;
1915 tss->flag = ctxt->eflags;
1916 tss->ax = c->regs[VCPU_REGS_RAX];
1917 tss->cx = c->regs[VCPU_REGS_RCX];
1918 tss->dx = c->regs[VCPU_REGS_RDX];
1919 tss->bx = c->regs[VCPU_REGS_RBX];
1920 tss->sp = c->regs[VCPU_REGS_RSP];
1921 tss->bp = c->regs[VCPU_REGS_RBP];
1922 tss->si = c->regs[VCPU_REGS_RSI];
1923 tss->di = c->regs[VCPU_REGS_RDI];
1924
1925 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1926 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1927 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1928 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1929 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1930}
1931
1932static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1933 struct x86_emulate_ops *ops,
1934 struct tss_segment_16 *tss)
1935{
1936 struct decode_cache *c = &ctxt->decode;
1937 int ret;
1938
1939 c->eip = tss->ip;
1940 ctxt->eflags = tss->flag | 2;
1941 c->regs[VCPU_REGS_RAX] = tss->ax;
1942 c->regs[VCPU_REGS_RCX] = tss->cx;
1943 c->regs[VCPU_REGS_RDX] = tss->dx;
1944 c->regs[VCPU_REGS_RBX] = tss->bx;
1945 c->regs[VCPU_REGS_RSP] = tss->sp;
1946 c->regs[VCPU_REGS_RBP] = tss->bp;
1947 c->regs[VCPU_REGS_RSI] = tss->si;
1948 c->regs[VCPU_REGS_RDI] = tss->di;
1949
1950 /*
1951 * SDM says that segment selectors are loaded before segment
1952 * descriptors
1953 */
1954 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1955 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1956 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1957 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1958 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1959
1960 /*
1961 * Now load segment descriptors. If fault happenes at this stage
1962 * it is handled in a context of new task
1963 */
1964 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1965 if (ret != X86EMUL_CONTINUE)
1966 return ret;
1967 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1968 if (ret != X86EMUL_CONTINUE)
1969 return ret;
1970 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1971 if (ret != X86EMUL_CONTINUE)
1972 return ret;
1973 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1974 if (ret != X86EMUL_CONTINUE)
1975 return ret;
1976 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1977 if (ret != X86EMUL_CONTINUE)
1978 return ret;
1979
1980 return X86EMUL_CONTINUE;
1981}
1982
1983static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1984 struct x86_emulate_ops *ops,
1985 u16 tss_selector, u16 old_tss_sel,
1986 ulong old_tss_base, struct desc_struct *new_desc)
1987{
1988 struct tss_segment_16 tss_seg;
1989 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001990 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001991
1992 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001993 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001994 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001995 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001996 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001997
1998 save_state_to_tss16(ctxt, ops, &tss_seg);
1999
2000 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002001 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002002 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002003 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002004 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002005
2006 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002007 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002008 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002009 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002010 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002011
2012 if (old_tss_sel != 0xffff) {
2013 tss_seg.prev_task_link = old_tss_sel;
2014
2015 ret = ops->write_std(new_tss_base,
2016 &tss_seg.prev_task_link,
2017 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002018 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002019 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002020 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002021 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002022 }
2023
2024 return load_state_from_tss16(ctxt, ops, &tss_seg);
2025}
2026
2027static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2028 struct x86_emulate_ops *ops,
2029 struct tss_segment_32 *tss)
2030{
2031 struct decode_cache *c = &ctxt->decode;
2032
2033 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2034 tss->eip = c->eip;
2035 tss->eflags = ctxt->eflags;
2036 tss->eax = c->regs[VCPU_REGS_RAX];
2037 tss->ecx = c->regs[VCPU_REGS_RCX];
2038 tss->edx = c->regs[VCPU_REGS_RDX];
2039 tss->ebx = c->regs[VCPU_REGS_RBX];
2040 tss->esp = c->regs[VCPU_REGS_RSP];
2041 tss->ebp = c->regs[VCPU_REGS_RBP];
2042 tss->esi = c->regs[VCPU_REGS_RSI];
2043 tss->edi = c->regs[VCPU_REGS_RDI];
2044
2045 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2046 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2047 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2048 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2049 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2050 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2051 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2052}
2053
2054static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2055 struct x86_emulate_ops *ops,
2056 struct tss_segment_32 *tss)
2057{
2058 struct decode_cache *c = &ctxt->decode;
2059 int ret;
2060
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002061 if (ops->set_cr(3, tss->cr3, ctxt->vcpu))
2062 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002063 c->eip = tss->eip;
2064 ctxt->eflags = tss->eflags | 2;
2065 c->regs[VCPU_REGS_RAX] = tss->eax;
2066 c->regs[VCPU_REGS_RCX] = tss->ecx;
2067 c->regs[VCPU_REGS_RDX] = tss->edx;
2068 c->regs[VCPU_REGS_RBX] = tss->ebx;
2069 c->regs[VCPU_REGS_RSP] = tss->esp;
2070 c->regs[VCPU_REGS_RBP] = tss->ebp;
2071 c->regs[VCPU_REGS_RSI] = tss->esi;
2072 c->regs[VCPU_REGS_RDI] = tss->edi;
2073
2074 /*
2075 * SDM says that segment selectors are loaded before segment
2076 * descriptors
2077 */
2078 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2079 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2080 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2081 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2082 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2083 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2084 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2085
2086 /*
2087 * Now load segment descriptors. If fault happenes at this stage
2088 * it is handled in a context of new task
2089 */
2090 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2091 if (ret != X86EMUL_CONTINUE)
2092 return ret;
2093 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2094 if (ret != X86EMUL_CONTINUE)
2095 return ret;
2096 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2097 if (ret != X86EMUL_CONTINUE)
2098 return ret;
2099 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2100 if (ret != X86EMUL_CONTINUE)
2101 return ret;
2102 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2103 if (ret != X86EMUL_CONTINUE)
2104 return ret;
2105 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2106 if (ret != X86EMUL_CONTINUE)
2107 return ret;
2108 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2109 if (ret != X86EMUL_CONTINUE)
2110 return ret;
2111
2112 return X86EMUL_CONTINUE;
2113}
2114
2115static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2116 struct x86_emulate_ops *ops,
2117 u16 tss_selector, u16 old_tss_sel,
2118 ulong old_tss_base, struct desc_struct *new_desc)
2119{
2120 struct tss_segment_32 tss_seg;
2121 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002122 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002123
2124 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002125 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002126 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002127 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002128 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002129
2130 save_state_to_tss32(ctxt, ops, &tss_seg);
2131
2132 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002133 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002134 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002135 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002136 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002137
2138 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002139 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002140 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002141 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002142 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002143
2144 if (old_tss_sel != 0xffff) {
2145 tss_seg.prev_task_link = old_tss_sel;
2146
2147 ret = ops->write_std(new_tss_base,
2148 &tss_seg.prev_task_link,
2149 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002150 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002151 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154 }
2155
2156 return load_state_from_tss32(ctxt, ops, &tss_seg);
2157}
2158
2159static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002160 struct x86_emulate_ops *ops,
2161 u16 tss_selector, int reason,
2162 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163{
2164 struct desc_struct curr_tss_desc, next_tss_desc;
2165 int ret;
2166 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2167 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002168 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002169 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002170
2171 /* FIXME: old_tss_base == ~0 ? */
2172
2173 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2174 if (ret != X86EMUL_CONTINUE)
2175 return ret;
2176 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2177 if (ret != X86EMUL_CONTINUE)
2178 return ret;
2179
2180 /* FIXME: check that next_tss_desc is tss */
2181
2182 if (reason != TASK_SWITCH_IRET) {
2183 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002184 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl)
2185 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002186 }
2187
Gleb Natapovceffb452010-03-18 15:20:19 +02002188 desc_limit = desc_limit_scaled(&next_tss_desc);
2189 if (!next_tss_desc.p ||
2190 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2191 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002192 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002193 return X86EMUL_PROPAGATE_FAULT;
2194 }
2195
2196 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2197 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2198 write_segment_descriptor(ctxt, ops, old_tss_sel,
2199 &curr_tss_desc);
2200 }
2201
2202 if (reason == TASK_SWITCH_IRET)
2203 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2204
2205 /* set back link to prev task only if NT bit is set in eflags
2206 note that old_tss_sel is not used afetr this point */
2207 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2208 old_tss_sel = 0xffff;
2209
2210 if (next_tss_desc.type & 8)
2211 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2212 old_tss_base, &next_tss_desc);
2213 else
2214 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2215 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002216 if (ret != X86EMUL_CONTINUE)
2217 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218
2219 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2220 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2221
2222 if (reason != TASK_SWITCH_IRET) {
2223 next_tss_desc.type |= (1 << 1); /* set busy flag */
2224 write_segment_descriptor(ctxt, ops, tss_selector,
2225 &next_tss_desc);
2226 }
2227
2228 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02002229 ops->set_cached_descriptor(&next_tss_desc, 0, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002230 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2231
Jan Kiszkae269fb22010-04-14 15:51:09 +02002232 if (has_error_code) {
2233 struct decode_cache *c = &ctxt->decode;
2234
2235 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2236 c->lock_prefix = 0;
2237 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002238 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002239 }
2240
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002241 return ret;
2242}
2243
2244int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002245 u16 tss_selector, int reason,
2246 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002247{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002248 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002249 struct decode_cache *c = &ctxt->decode;
2250 int rc;
2251
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002252 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002253 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002254
Jan Kiszkae269fb22010-04-14 15:51:09 +02002255 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2256 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002257
2258 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002259 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002260 if (rc == X86EMUL_CONTINUE)
2261 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002262 }
2263
Gleb Natapov19d04432010-04-15 12:29:50 +03002264 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002265}
2266
Avi Kivity90de84f2010-11-17 15:28:21 +02002267static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002268 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002269{
2270 struct decode_cache *c = &ctxt->decode;
2271 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2272
Gleb Natapovd9271122010-03-18 15:20:22 +02002273 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002274 op->addr.mem.ea = register_address(c, c->regs[reg]);
2275 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002276}
2277
Avi Kivity63540382010-07-29 15:11:55 +03002278static int em_push(struct x86_emulate_ctxt *ctxt)
2279{
2280 emulate_push(ctxt, ctxt->ops);
2281 return X86EMUL_CONTINUE;
2282}
2283
Avi Kivity7af04fc2010-08-18 14:16:35 +03002284static int em_das(struct x86_emulate_ctxt *ctxt)
2285{
2286 struct decode_cache *c = &ctxt->decode;
2287 u8 al, old_al;
2288 bool af, cf, old_cf;
2289
2290 cf = ctxt->eflags & X86_EFLAGS_CF;
2291 al = c->dst.val;
2292
2293 old_al = al;
2294 old_cf = cf;
2295 cf = false;
2296 af = ctxt->eflags & X86_EFLAGS_AF;
2297 if ((al & 0x0f) > 9 || af) {
2298 al -= 6;
2299 cf = old_cf | (al >= 250);
2300 af = true;
2301 } else {
2302 af = false;
2303 }
2304 if (old_al > 0x99 || old_cf) {
2305 al -= 0x60;
2306 cf = true;
2307 }
2308
2309 c->dst.val = al;
2310 /* Set PF, ZF, SF */
2311 c->src.type = OP_IMM;
2312 c->src.val = 0;
2313 c->src.bytes = 1;
2314 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2315 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2316 if (cf)
2317 ctxt->eflags |= X86_EFLAGS_CF;
2318 if (af)
2319 ctxt->eflags |= X86_EFLAGS_AF;
2320 return X86EMUL_CONTINUE;
2321}
2322
Avi Kivity0ef753b2010-08-18 14:51:45 +03002323static int em_call_far(struct x86_emulate_ctxt *ctxt)
2324{
2325 struct decode_cache *c = &ctxt->decode;
2326 u16 sel, old_cs;
2327 ulong old_eip;
2328 int rc;
2329
2330 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2331 old_eip = c->eip;
2332
2333 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2334 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2335 return X86EMUL_CONTINUE;
2336
2337 c->eip = 0;
2338 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2339
2340 c->src.val = old_cs;
2341 emulate_push(ctxt, ctxt->ops);
2342 rc = writeback(ctxt, ctxt->ops);
2343 if (rc != X86EMUL_CONTINUE)
2344 return rc;
2345
2346 c->src.val = old_eip;
2347 emulate_push(ctxt, ctxt->ops);
2348 rc = writeback(ctxt, ctxt->ops);
2349 if (rc != X86EMUL_CONTINUE)
2350 return rc;
2351
2352 c->dst.type = OP_NONE;
2353
2354 return X86EMUL_CONTINUE;
2355}
2356
Avi Kivity40ece7c2010-08-18 15:12:09 +03002357static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2358{
2359 struct decode_cache *c = &ctxt->decode;
2360 int rc;
2361
2362 c->dst.type = OP_REG;
2363 c->dst.addr.reg = &c->eip;
2364 c->dst.bytes = c->op_bytes;
2365 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2366 if (rc != X86EMUL_CONTINUE)
2367 return rc;
2368 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2369 return X86EMUL_CONTINUE;
2370}
2371
Avi Kivity5c82aa22010-08-18 18:31:43 +03002372static int em_imul(struct x86_emulate_ctxt *ctxt)
2373{
2374 struct decode_cache *c = &ctxt->decode;
2375
2376 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2377 return X86EMUL_CONTINUE;
2378}
2379
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002380static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2381{
2382 struct decode_cache *c = &ctxt->decode;
2383
2384 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002385 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002386}
2387
Avi Kivity61429142010-08-19 15:13:00 +03002388static int em_cwd(struct x86_emulate_ctxt *ctxt)
2389{
2390 struct decode_cache *c = &ctxt->decode;
2391
2392 c->dst.type = OP_REG;
2393 c->dst.bytes = c->src.bytes;
2394 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2395 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2396
2397 return X86EMUL_CONTINUE;
2398}
2399
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002400static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2401{
2402 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2403 struct decode_cache *c = &ctxt->decode;
2404 u64 tsc = 0;
2405
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002406 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD))
2407 return emulate_gp(ctxt, 0);
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002408 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2409 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2410 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2411 return X86EMUL_CONTINUE;
2412}
2413
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002414static int em_mov(struct x86_emulate_ctxt *ctxt)
2415{
2416 struct decode_cache *c = &ctxt->decode;
2417 c->dst.val = c->src.val;
2418 return X86EMUL_CONTINUE;
2419}
2420
Avi Kivityaa97bb42010-01-20 18:09:23 +02002421static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2422{
2423 struct decode_cache *c = &ctxt->decode;
2424 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2425 return X86EMUL_CONTINUE;
2426}
2427
Avi Kivity73fba5f2010-07-29 15:11:53 +03002428#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002429#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002430#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2431 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002432#define N D(0)
2433#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2434#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2435#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002436#define II(_f, _e, _i) \
2437 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002438#define IIP(_f, _e, _i, _p) \
2439 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2440 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002441#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002442
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002443#define D2bv(_f) D((_f) | ByteOp), D(_f)
2444#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2445
Avi Kivity6230f7f2010-08-26 18:34:55 +03002446#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2447 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2448 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2449
2450
Avi Kivity73fba5f2010-07-29 15:11:53 +03002451static struct opcode group1[] = {
2452 X7(D(Lock)), N
2453};
2454
2455static struct opcode group1A[] = {
2456 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2457};
2458
2459static struct opcode group3[] = {
2460 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2461 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002462 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002463};
2464
2465static struct opcode group4[] = {
2466 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2467 N, N, N, N, N, N,
2468};
2469
2470static struct opcode group5[] = {
2471 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002472 D(SrcMem | ModRM | Stack),
2473 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002474 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2475 D(SrcMem | ModRM | Stack), N,
2476};
2477
2478static struct group_dual group7 = { {
Avi Kivity3c6e2762011-04-04 12:39:23 +02002479 N, N, DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
2480 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2481 DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
2482 DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002483}, {
Avi Kivityd8671622011-02-01 16:32:03 +02002484 D(SrcNone | ModRM | Priv | VendorSpecific), N,
2485 N, D(SrcNone | ModRM | Priv | VendorSpecific),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002486 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2487 DI(SrcMem16 | ModRM | Mov | Priv, lmsw), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002488} };
2489
2490static struct opcode group8[] = {
2491 N, N, N, N,
2492 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2493 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2494};
2495
2496static struct group_dual group9 = { {
2497 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2498}, {
2499 N, N, N, N, N, N, N, N,
2500} };
2501
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002502static struct opcode group11[] = {
2503 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2504};
2505
Avi Kivityaa97bb42010-01-20 18:09:23 +02002506static struct gprefix pfx_0f_6f_0f_7f = {
2507 N, N, N, I(Sse, em_movdqu),
2508};
2509
Avi Kivity73fba5f2010-07-29 15:11:53 +03002510static struct opcode opcode_table[256] = {
2511 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002512 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002513 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2514 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002515 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002516 D(ImplicitOps | Stack | No64), N,
2517 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002518 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002519 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2520 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002521 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002522 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2523 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002524 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002525 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002526 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002527 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002528 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002529 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002530 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002531 /* 0x40 - 0x4F */
2532 X16(D(DstReg)),
2533 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002534 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002535 /* 0x58 - 0x5F */
2536 X8(D(DstReg | Stack)),
2537 /* 0x60 - 0x67 */
2538 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2539 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2540 N, N, N, N,
2541 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002542 I(SrcImm | Mov | Stack, em_push),
2543 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002544 I(SrcImmByte | Mov | Stack, em_push),
2545 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002546 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2547 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002548 /* 0x70 - 0x7F */
2549 X16(D(SrcImmByte)),
2550 /* 0x80 - 0x87 */
2551 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2552 G(DstMem | SrcImm | ModRM | Group, group1),
2553 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2554 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002555 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002556 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002557 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2558 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002559 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002560 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2561 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002562 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002563 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002564 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002565 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002566 DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002567 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002568 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2569 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2570 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2571 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002572 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002573 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002574 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2575 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002576 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002577 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002578 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002579 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002580 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002581 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002582 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002583 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2584 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002585 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002586 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002587 /* 0xC8 - 0xCF */
2588 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002589 D(ImplicitOps), DI(SrcImmByte, intn),
2590 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002591 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002592 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002593 N, N, N, N,
2594 /* 0xD8 - 0xDF */
2595 N, N, N, N, N, N, N, N,
2596 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002597 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002598 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002599 /* 0xE8 - 0xEF */
2600 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2601 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002602 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002603 /* 0xF0 - 0xF7 */
2604 N, N, N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002605 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
2606 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002607 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002608 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002609 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2610};
2611
2612static struct opcode twobyte_table[256] = {
2613 /* 0x00 - 0x0F */
2614 N, GD(0, &group7), N, N,
Avi Kivityd8671622011-02-01 16:32:03 +02002615 N, D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002616 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002617 N, D(ImplicitOps | ModRM), N, N,
2618 /* 0x10 - 0x1F */
2619 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2620 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002621 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2622 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002623 N, N, N, N,
2624 N, N, N, N, N, N, N, N,
2625 /* 0x30 - 0x3F */
Avi Kivity3c6e2762011-04-04 12:39:23 +02002626 D(ImplicitOps | Priv), II(ImplicitOps, em_rdtsc, rdtsc),
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002627 D(ImplicitOps | Priv), N,
Avi Kivityd8671622011-02-01 16:32:03 +02002628 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
2629 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002630 N, N, N, N, N, N, N, N,
2631 /* 0x40 - 0x4F */
2632 X16(D(DstReg | SrcMem | ModRM | Mov)),
2633 /* 0x50 - 0x5F */
2634 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2635 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002636 N, N, N, N,
2637 N, N, N, N,
2638 N, N, N, N,
2639 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002640 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002641 N, N, N, N,
2642 N, N, N, N,
2643 N, N, N, N,
2644 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002645 /* 0x80 - 0x8F */
2646 X16(D(SrcImm)),
2647 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002648 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002649 /* 0xA0 - 0xA7 */
2650 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2651 N, D(DstMem | SrcReg | ModRM | BitOp),
2652 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2653 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2654 /* 0xA8 - 0xAF */
2655 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2656 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2657 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2658 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002659 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002660 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002661 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002662 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2663 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2664 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002665 /* 0xB8 - 0xBF */
2666 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002667 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002668 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2669 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002670 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002671 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002672 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002673 N, N, N, GD(0, &group9),
2674 N, N, N, N, N, N, N, N,
2675 /* 0xD0 - 0xDF */
2676 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2677 /* 0xE0 - 0xEF */
2678 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2679 /* 0xF0 - 0xFF */
2680 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2681};
2682
2683#undef D
2684#undef N
2685#undef G
2686#undef GD
2687#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02002688#undef GP
Avi Kivity73fba5f2010-07-29 15:11:53 +03002689
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002690#undef D2bv
2691#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002692#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002693
Avi Kivity39f21ee2010-08-18 19:20:21 +03002694static unsigned imm_size(struct decode_cache *c)
2695{
2696 unsigned size;
2697
2698 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2699 if (size == 8)
2700 size = 4;
2701 return size;
2702}
2703
2704static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2705 unsigned size, bool sign_extension)
2706{
2707 struct decode_cache *c = &ctxt->decode;
2708 struct x86_emulate_ops *ops = ctxt->ops;
2709 int rc = X86EMUL_CONTINUE;
2710
2711 op->type = OP_IMM;
2712 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002713 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002714 /* NB. Immediates are sign-extended as necessary. */
2715 switch (op->bytes) {
2716 case 1:
2717 op->val = insn_fetch(s8, 1, c->eip);
2718 break;
2719 case 2:
2720 op->val = insn_fetch(s16, 2, c->eip);
2721 break;
2722 case 4:
2723 op->val = insn_fetch(s32, 4, c->eip);
2724 break;
2725 }
2726 if (!sign_extension) {
2727 switch (op->bytes) {
2728 case 1:
2729 op->val &= 0xff;
2730 break;
2731 case 2:
2732 op->val &= 0xffff;
2733 break;
2734 case 4:
2735 op->val &= 0xffffffff;
2736 break;
2737 }
2738 }
2739done:
2740 return rc;
2741}
2742
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002743int
Andre Przywaradc25e892010-12-21 11:12:07 +01002744x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002745{
2746 struct x86_emulate_ops *ops = ctxt->ops;
2747 struct decode_cache *c = &ctxt->decode;
2748 int rc = X86EMUL_CONTINUE;
2749 int mode = ctxt->mode;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002750 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
2751 bool op_prefix = false;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002752 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002753 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002754
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002755 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01002756 c->fetch.start = c->eip;
2757 c->fetch.end = c->fetch.start + insn_len;
2758 if (insn_len > 0)
2759 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002760 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2761
2762 switch (mode) {
2763 case X86EMUL_MODE_REAL:
2764 case X86EMUL_MODE_VM86:
2765 case X86EMUL_MODE_PROT16:
2766 def_op_bytes = def_ad_bytes = 2;
2767 break;
2768 case X86EMUL_MODE_PROT32:
2769 def_op_bytes = def_ad_bytes = 4;
2770 break;
2771#ifdef CONFIG_X86_64
2772 case X86EMUL_MODE_PROT64:
2773 def_op_bytes = 4;
2774 def_ad_bytes = 8;
2775 break;
2776#endif
2777 default:
2778 return -1;
2779 }
2780
2781 c->op_bytes = def_op_bytes;
2782 c->ad_bytes = def_ad_bytes;
2783
2784 /* Legacy prefixes. */
2785 for (;;) {
2786 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2787 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002788 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002789 /* switch between 2/4 bytes */
2790 c->op_bytes = def_op_bytes ^ 6;
2791 break;
2792 case 0x67: /* address-size override */
2793 if (mode == X86EMUL_MODE_PROT64)
2794 /* switch between 4/8 bytes */
2795 c->ad_bytes = def_ad_bytes ^ 12;
2796 else
2797 /* switch between 2/4 bytes */
2798 c->ad_bytes = def_ad_bytes ^ 6;
2799 break;
2800 case 0x26: /* ES override */
2801 case 0x2e: /* CS override */
2802 case 0x36: /* SS override */
2803 case 0x3e: /* DS override */
2804 set_seg_override(c, (c->b >> 3) & 3);
2805 break;
2806 case 0x64: /* FS override */
2807 case 0x65: /* GS override */
2808 set_seg_override(c, c->b & 7);
2809 break;
2810 case 0x40 ... 0x4f: /* REX */
2811 if (mode != X86EMUL_MODE_PROT64)
2812 goto done_prefixes;
2813 c->rex_prefix = c->b;
2814 continue;
2815 case 0xf0: /* LOCK */
2816 c->lock_prefix = 1;
2817 break;
2818 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002819 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02002820 c->rep_prefix = c->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002821 break;
2822 default:
2823 goto done_prefixes;
2824 }
2825
2826 /* Any legacy prefix after a REX prefix nullifies its effect. */
2827
2828 c->rex_prefix = 0;
2829 }
2830
2831done_prefixes:
2832
2833 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002834 if (c->rex_prefix & 8)
2835 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002836
2837 /* Opcode byte(s). */
2838 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002839 /* Two-byte opcode? */
2840 if (c->b == 0x0f) {
2841 c->twobyte = 1;
2842 c->b = insn_fetch(u8, 1, c->eip);
2843 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002844 }
2845 c->d = opcode.flags;
2846
2847 if (c->d & Group) {
2848 dual = c->d & GroupDual;
2849 c->modrm = insn_fetch(u8, 1, c->eip);
2850 --c->eip;
2851
2852 if (c->d & GroupDual) {
2853 g_mod012 = opcode.u.gdual->mod012;
2854 g_mod3 = opcode.u.gdual->mod3;
2855 } else
2856 g_mod012 = g_mod3 = opcode.u.group;
2857
2858 c->d &= ~(Group | GroupDual);
2859
2860 goffset = (c->modrm >> 3) & 7;
2861
2862 if ((c->modrm >> 6) == 3)
2863 opcode = g_mod3[goffset];
2864 else
2865 opcode = g_mod012[goffset];
2866 c->d |= opcode.flags;
2867 }
2868
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002869 if (c->d & Prefix) {
2870 if (c->rep_prefix && op_prefix)
2871 return X86EMUL_UNHANDLEABLE;
2872 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
2873 switch (simd_prefix) {
2874 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
2875 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
2876 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
2877 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
2878 }
2879 c->d |= opcode.flags;
2880 }
2881
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002882 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02002883 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02002884 c->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002885
2886 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02002887 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002888 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002889
Avi Kivityd8671622011-02-01 16:32:03 +02002890 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
2891 return -1;
2892
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002893 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2894 c->op_bytes = 8;
2895
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002896 if (c->d & Op3264) {
2897 if (mode == X86EMUL_MODE_PROT64)
2898 c->op_bytes = 8;
2899 else
2900 c->op_bytes = 4;
2901 }
2902
Avi Kivity12537912011-03-29 11:41:27 +02002903 if (c->d & Sse)
2904 c->op_bytes = 16;
2905
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002906 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002907 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002908 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002909 if (!c->has_seg_override)
2910 set_seg_override(c, c->modrm_seg);
2911 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002912 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002913 if (rc != X86EMUL_CONTINUE)
2914 goto done;
2915
2916 if (!c->has_seg_override)
2917 set_seg_override(c, VCPU_SREG_DS);
2918
Avi Kivity90de84f2010-11-17 15:28:21 +02002919 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002920
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002921 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02002922 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002923
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002924 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02002925 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002926
2927 /*
2928 * Decode and fetch the source operand: register, memory
2929 * or immediate.
2930 */
2931 switch (c->d & SrcMask) {
2932 case SrcNone:
2933 break;
2934 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02002935 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002936 break;
2937 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002938 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002939 goto srcmem_common;
2940 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002941 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002942 goto srcmem_common;
2943 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002944 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002945 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002946 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002947 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002948 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002949 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002950 rc = decode_imm(ctxt, &c->src, 2, false);
2951 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002952 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002953 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2954 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002955 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002956 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002957 break;
2958 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002959 rc = decode_imm(ctxt, &c->src, 1, true);
2960 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002961 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002962 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002963 break;
2964 case SrcAcc:
2965 c->src.type = OP_REG;
2966 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002967 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002968 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002969 break;
2970 case SrcOne:
2971 c->src.bytes = 1;
2972 c->src.val = 1;
2973 break;
2974 case SrcSI:
2975 c->src.type = OP_MEM;
2976 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002977 c->src.addr.mem.ea =
2978 register_address(c, c->regs[VCPU_REGS_RSI]);
2979 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002980 c->src.val = 0;
2981 break;
2982 case SrcImmFAddr:
2983 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002984 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002985 c->src.bytes = c->op_bytes + 2;
2986 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2987 break;
2988 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002989 memop.bytes = c->op_bytes + 2;
2990 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002991 break;
2992 }
2993
Avi Kivity39f21ee2010-08-18 19:20:21 +03002994 if (rc != X86EMUL_CONTINUE)
2995 goto done;
2996
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002997 /*
2998 * Decode and fetch the second source operand: register, memory
2999 * or immediate.
3000 */
3001 switch (c->d & Src2Mask) {
3002 case Src2None:
3003 break;
3004 case Src2CL:
3005 c->src2.bytes = 1;
3006 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3007 break;
3008 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003009 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003010 break;
3011 case Src2One:
3012 c->src2.bytes = 1;
3013 c->src2.val = 1;
3014 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003015 case Src2Imm:
3016 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3017 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003018 }
3019
Avi Kivity39f21ee2010-08-18 19:20:21 +03003020 if (rc != X86EMUL_CONTINUE)
3021 goto done;
3022
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003023 /* Decode and fetch the destination operand: register or memory. */
3024 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003025 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003026 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003027 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3028 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003029 case DstImmUByte:
3030 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003031 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003032 c->dst.bytes = 1;
3033 c->dst.val = insn_fetch(u8, 1, c->eip);
3034 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003035 case DstMem:
3036 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003037 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003038 if ((c->d & DstMask) == DstMem64)
3039 c->dst.bytes = 8;
3040 else
3041 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003042 if (c->d & BitOp)
3043 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003044 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003045 break;
3046 case DstAcc:
3047 c->dst.type = OP_REG;
3048 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003049 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003050 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003051 c->dst.orig_val = c->dst.val;
3052 break;
3053 case DstDI:
3054 c->dst.type = OP_MEM;
3055 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003056 c->dst.addr.mem.ea =
3057 register_address(c, c->regs[VCPU_REGS_RDI]);
3058 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003059 c->dst.val = 0;
3060 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003061 case ImplicitOps:
3062 /* Special instructions do their own operand decoding. */
3063 default:
3064 c->dst.type = OP_NONE; /* Disable writeback. */
3065 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003066 }
3067
3068done:
3069 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
3070}
3071
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003072static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3073{
3074 struct decode_cache *c = &ctxt->decode;
3075
3076 /* The second termination condition only applies for REPE
3077 * and REPNE. Test if the repeat string operation prefix is
3078 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3079 * corresponding termination condition according to:
3080 * - if REPE/REPZ and ZF = 0 then done
3081 * - if REPNE/REPNZ and ZF = 1 then done
3082 */
3083 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3084 (c->b == 0xae) || (c->b == 0xaf))
3085 && (((c->rep_prefix == REPE_PREFIX) &&
3086 ((ctxt->eflags & EFLG_ZF) == 0))
3087 || ((c->rep_prefix == REPNE_PREFIX) &&
3088 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3089 return true;
3090
3091 return false;
3092}
3093
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003094int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003095x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003096{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003097 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003098 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003099 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003100 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003101 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003102 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003103
Gleb Natapov9de41572010-04-28 19:15:22 +03003104 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003105
Gleb Natapov11616242010-02-11 14:43:14 +02003106 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003107 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003108 goto done;
3109 }
3110
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003111 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003112 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003113 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003114 goto done;
3115 }
3116
Avi Kivity081bca02010-08-26 11:06:15 +03003117 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003118 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003119 goto done;
3120 }
3121
Avi Kivity12537912011-03-29 11:41:27 +02003122 if ((c->d & Sse)
3123 && ((ops->get_cr(0, ctxt->vcpu) & X86_CR0_EM)
3124 || !(ops->get_cr(4, ctxt->vcpu) & X86_CR4_OSFXSR))) {
3125 rc = emulate_ud(ctxt);
3126 goto done;
3127 }
3128
3129 if ((c->d & Sse) && (ops->get_cr(0, ctxt->vcpu) & X86_CR0_TS)) {
3130 rc = emulate_nm(ctxt);
3131 goto done;
3132 }
3133
Avi Kivityc4f035c2011-04-04 12:39:22 +02003134 if (unlikely(ctxt->guest_mode) && c->intercept) {
3135 rc = ops->intercept(ctxt, c->intercept,
3136 X86_ICPT_PRE_EXCEPT);
3137 if (rc != X86EMUL_CONTINUE)
3138 goto done;
3139 }
3140
Gleb Natapove92805a2010-02-10 14:21:35 +02003141 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003142 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003143 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003144 goto done;
3145 }
3146
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003147 /* Instruction can only be executed in protected mode */
3148 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3149 rc = emulate_ud(ctxt);
3150 goto done;
3151 }
3152
Joerg Roedeld09beab2011-04-04 12:39:25 +02003153 /* Do instruction specific permission checks */
3154 if (c->check_perm) {
3155 rc = c->check_perm(ctxt);
3156 if (rc != X86EMUL_CONTINUE)
3157 goto done;
3158 }
3159
Avi Kivityc4f035c2011-04-04 12:39:22 +02003160 if (unlikely(ctxt->guest_mode) && c->intercept) {
3161 rc = ops->intercept(ctxt, c->intercept,
3162 X86_ICPT_POST_EXCEPT);
3163 if (rc != X86EMUL_CONTINUE)
3164 goto done;
3165 }
3166
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003167 if (c->rep_prefix && (c->d & String)) {
3168 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003169 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003170 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003171 goto done;
3172 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003173 }
3174
Wei Yongjunc483c022010-08-06 15:36:36 +08003175 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003176 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03003177 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003178 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003179 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003180 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003181 }
3182
Gleb Natapove35b7b92010-02-25 16:36:42 +02003183 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003184 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003185 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003186 if (rc != X86EMUL_CONTINUE)
3187 goto done;
3188 }
3189
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003190 if ((c->d & DstMask) == ImplicitOps)
3191 goto special_insn;
3192
3193
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003194 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3195 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003196 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003197 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003198 if (rc != X86EMUL_CONTINUE)
3199 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003200 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003201 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003202
Avi Kivity018a98d2007-11-27 19:30:56 +02003203special_insn:
3204
Avi Kivityc4f035c2011-04-04 12:39:22 +02003205 if (unlikely(ctxt->guest_mode) && c->intercept) {
3206 rc = ops->intercept(ctxt, c->intercept,
3207 X86_ICPT_POST_MEMACCESS);
3208 if (rc != X86EMUL_CONTINUE)
3209 goto done;
3210 }
3211
Avi Kivityef65c882010-07-29 15:11:51 +03003212 if (c->execute) {
3213 rc = c->execute(ctxt);
3214 if (rc != X86EMUL_CONTINUE)
3215 goto done;
3216 goto writeback;
3217 }
3218
Laurent Viviere4e03de2007-09-18 11:52:50 +02003219 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 goto twobyte_insn;
3221
Laurent Viviere4e03de2007-09-18 11:52:50 +02003222 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 case 0x00 ... 0x05:
3224 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003225 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003227 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003228 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003229 break;
3230 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003231 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003232 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 case 0x08 ... 0x0d:
3234 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003235 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003237 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003238 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003239 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240 case 0x10 ... 0x15:
3241 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003242 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003244 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003245 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003246 break;
3247 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003248 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003249 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 case 0x18 ... 0x1d:
3251 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003252 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003254 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003255 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003256 break;
3257 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003258 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003259 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003260 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003262 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263 break;
3264 case 0x28 ... 0x2d:
3265 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003266 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 break;
3268 case 0x30 ... 0x35:
3269 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003270 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 break;
3272 case 0x38 ... 0x3d:
3273 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003274 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003276 case 0x40 ... 0x47: /* inc r16/r32 */
3277 emulate_1op("inc", c->dst, ctxt->eflags);
3278 break;
3279 case 0x48 ... 0x4f: /* dec r16/r32 */
3280 emulate_1op("dec", c->dst, ctxt->eflags);
3281 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003282 case 0x58 ... 0x5f: /* pop reg */
3283 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003284 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003285 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003286 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003287 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003288 break;
3289 case 0x61: /* popa */
3290 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003291 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003293 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003295 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003296 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003297 case 0x6c: /* insb */
3298 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003299 c->src.val = c->regs[VCPU_REGS_RDX];
3300 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003301 case 0x6e: /* outsb */
3302 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003303 c->dst.val = c->regs[VCPU_REGS_RDX];
3304 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003305 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003306 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003307 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003308 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003309 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003311 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312 case 0:
3313 goto add;
3314 case 1:
3315 goto or;
3316 case 2:
3317 goto adc;
3318 case 3:
3319 goto sbb;
3320 case 4:
3321 goto and;
3322 case 5:
3323 goto sub;
3324 case 6:
3325 goto xor;
3326 case 7:
3327 goto cmp;
3328 }
3329 break;
3330 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003331 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003332 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333 break;
3334 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003335 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003337 c->src.val = c->dst.val;
3338 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339 /*
3340 * Write back the memory destination with implicit LOCK
3341 * prefix.
3342 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003343 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003344 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003346 case 0x8c: /* mov r/m, sreg */
3347 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003348 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003349 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003350 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003351 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003352 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003353 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003354 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003355 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003356 case 0x8e: { /* mov seg, r/m16 */
3357 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003358
3359 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003360
Gleb Natapovc6975182010-02-18 12:15:01 +02003361 if (c->modrm_reg == VCPU_SREG_CS ||
3362 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003363 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003364 goto done;
3365 }
3366
Glauber Costa310b5d32009-05-12 16:21:06 -04003367 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003368 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003369
Gleb Natapov2e873022010-03-18 15:20:18 +02003370 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003371
3372 c->dst.type = OP_NONE; /* Disable writeback. */
3373 break;
3374 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003376 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003378 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3379 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003380 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003381 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003382 case 0x98: /* cbw/cwde/cdqe */
3383 switch (c->op_bytes) {
3384 case 2: c->dst.val = (s8)c->dst.val; break;
3385 case 4: c->dst.val = (s16)c->dst.val; break;
3386 case 8: c->dst.val = (s32)c->dst.val; break;
3387 }
3388 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003389 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003390 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003391 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003392 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003393 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003394 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003395 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003396 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003397 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003398 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003400 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003401 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003402 case 0xa8 ... 0xa9: /* test ax, imm */
3403 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003404 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003405 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003406 case 0xc0 ... 0xc1:
3407 emulate_grp2(ctxt);
3408 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003409 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003410 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003411 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003412 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003413 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003414 case 0xc4: /* les */
3415 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003416 break;
3417 case 0xc5: /* lds */
3418 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003419 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003420 case 0xcb: /* ret far */
3421 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003422 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003423 case 0xcc: /* int3 */
3424 irq = 3;
3425 goto do_interrupt;
3426 case 0xcd: /* int n */
3427 irq = c->src.val;
3428 do_interrupt:
3429 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003430 break;
3431 case 0xce: /* into */
3432 if (ctxt->eflags & EFLG_OF) {
3433 irq = 4;
3434 goto do_interrupt;
3435 }
3436 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003437 case 0xcf: /* iret */
3438 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003439 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003440 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003441 emulate_grp2(ctxt);
3442 break;
3443 case 0xd2 ... 0xd3: /* Grp2 */
3444 c->src.val = c->regs[VCPU_REGS_RCX];
3445 emulate_grp2(ctxt);
3446 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003447 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3448 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3449 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3450 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3451 jmp_rel(c, c->src.val);
3452 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003453 case 0xe3: /* jcxz/jecxz/jrcxz */
3454 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3455 jmp_rel(c, c->src.val);
3456 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003457 case 0xe4: /* inb */
3458 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003459 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003460 case 0xe6: /* outb */
3461 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003462 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003463 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003464 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003465 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003466 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003467 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003468 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003469 }
3470 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003471 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003472 case 0xea: { /* jmp far */
3473 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003474 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003475 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3476
3477 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003478 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003479
Gleb Natapov414e6272010-04-28 19:15:26 +03003480 c->eip = 0;
3481 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003482 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003483 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003484 case 0xeb:
3485 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003486 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003487 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003488 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003489 case 0xec: /* in al,dx */
3490 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003491 c->src.val = c->regs[VCPU_REGS_RDX];
3492 do_io_in:
3493 c->dst.bytes = min(c->dst.bytes, 4u);
3494 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003495 rc = emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003496 goto done;
3497 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003498 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3499 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003500 goto done; /* IO is needed */
3501 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003502 case 0xee: /* out dx,al */
3503 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003504 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003505 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003506 c->src.bytes = min(c->src.bytes, 4u);
3507 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3508 c->src.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003509 rc = emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003510 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003511 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003512 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3513 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003514 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003515 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003516 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003517 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003518 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003519 case 0xf5: /* cmc */
3520 /* complement carry flag from eflags reg */
3521 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003522 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003523 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003524 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003525 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003526 case 0xf8: /* clc */
3527 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003528 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003529 case 0xf9: /* stc */
3530 ctxt->eflags |= EFLG_CF;
3531 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003532 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003533 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003534 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003535 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003536 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003537 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003538 break;
3539 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003540 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003541 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003542 goto done;
3543 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003544 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003545 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003546 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003547 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003548 case 0xfc: /* cld */
3549 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003550 break;
3551 case 0xfd: /* std */
3552 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003553 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003554 case 0xfe: /* Grp4 */
3555 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003556 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003557 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003558 case 0xff: /* Grp5 */
3559 if (c->modrm_reg == 5)
3560 goto jump_far;
3561 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003562 default:
3563 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003565
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003566 if (rc != X86EMUL_CONTINUE)
3567 goto done;
3568
Avi Kivity018a98d2007-11-27 19:30:56 +02003569writeback:
3570 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003571 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003572 goto done;
3573
Gleb Natapov5cd21912010-03-18 15:20:26 +02003574 /*
3575 * restore dst type in case the decoding will be reused
3576 * (happens for string instruction )
3577 */
3578 c->dst.type = saved_dst_type;
3579
Gleb Natapova682e352010-03-18 15:20:21 +02003580 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003581 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003582 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003583
3584 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003585 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003586 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003587
Gleb Natapov5cd21912010-03-18 15:20:26 +02003588 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003589 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003590 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003591
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003592 if (!string_insn_completed(ctxt)) {
3593 /*
3594 * Re-enter guest when pio read ahead buffer is empty
3595 * or, if it is not used, after each 1024 iteration.
3596 */
3597 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3598 (r->end == 0 || r->end != r->pos)) {
3599 /*
3600 * Reset read cache. Usually happens before
3601 * decode, but since instruction is restarted
3602 * we have to do it here.
3603 */
3604 ctxt->decode.mem_read.end = 0;
3605 return EMULATION_RESTART;
3606 }
3607 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003608 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003609 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003610
3611 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003612
3613done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003614 if (rc == X86EMUL_PROPAGATE_FAULT)
3615 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02003616 if (rc == X86EMUL_INTERCEPTED)
3617 return EMULATION_INTERCEPTED;
3618
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003619 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620
3621twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003622 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003623 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003624 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625 u16 size;
3626 unsigned long address;
3627
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003628 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003629 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003630 goto cannot_emulate;
3631
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003632 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003633 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003634 goto done;
3635
Avi Kivity33e38852008-05-21 15:34:25 +03003636 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003637 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003638 /* Disable writeback. */
3639 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003640 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003642 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003643 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003644 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645 goto done;
3646 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003647 /* Disable writeback. */
3648 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003650 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003651 if (c->modrm_mod == 3) {
3652 switch (c->modrm_rm) {
3653 case 1:
3654 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003655 break;
3656 default:
3657 goto cannot_emulate;
3658 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003659 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003660 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003661 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003662 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003663 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003664 goto done;
3665 realmode_lidt(ctxt->vcpu, size, address);
3666 }
Avi Kivity16286d02008-04-14 14:40:50 +03003667 /* Disable writeback. */
3668 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669 break;
3670 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003671 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003672 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673 break;
3674 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003675 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003676 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003677 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003679 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003680 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003681 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003682 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003684 emulate_invlpg(ctxt->vcpu,
3685 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003686 /* Disable writeback. */
3687 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003688 break;
3689 default:
3690 goto cannot_emulate;
3691 }
3692 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003693 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003694 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003695 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003696 case 0x06:
3697 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003698 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003699 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003700 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003701 break;
3702 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003703 case 0x0d: /* GrpP (prefetch) */
3704 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003705 break;
3706 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003707 switch (c->modrm_reg) {
3708 case 1:
3709 case 5 ... 7:
3710 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003711 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003712 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003713 goto done;
3714 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003715 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003716 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003717 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003718 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3719 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003720 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003721 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003722 goto done;
3723 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003724 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003726 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003727 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003728 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003729 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03003730 goto done;
3731 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003732 c->dst.type = OP_NONE;
3733 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003735 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3736 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003737 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003738 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003739 goto done;
3740 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003741
Avi Kivityb27f3852010-08-01 14:25:22 +03003742 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003743 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3744 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3745 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003746 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003747 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03003748 goto done;
3749 }
3750
Laurent Viviera01af5e2007-09-24 11:10:56 +02003751 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003752 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003753 case 0x30:
3754 /* wrmsr */
3755 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3756 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003757 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003758 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003759 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003760 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003761 }
3762 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003763 break;
3764 case 0x32:
3765 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003766 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003767 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003768 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003769 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003770 } else {
3771 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3772 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3773 }
3774 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003775 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003776 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003777 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003778 break;
3779 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003780 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003781 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003783 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003784 if (!test_cc(c->b, ctxt->eflags))
3785 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003787 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003788 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003789 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003790 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003791 case 0x90 ... 0x9f: /* setcc r/m8 */
3792 c->dst.val = test_cc(c->b, ctxt->eflags);
3793 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003794 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003795 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003796 break;
3797 case 0xa1: /* pop fs */
3798 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003799 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003800 case 0xa3:
3801 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003802 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003803 /* only subword offset */
3804 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003805 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003806 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003807 case 0xa4: /* shld imm8, r, r/m */
3808 case 0xa5: /* shld cl, r, r/m */
3809 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3810 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003811 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003812 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003813 break;
3814 case 0xa9: /* pop gs */
3815 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003816 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003817 case 0xab:
3818 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003819 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003820 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003821 case 0xac: /* shrd imm8, r, r/m */
3822 case 0xad: /* shrd cl, r, r/m */
3823 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3824 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003825 case 0xae: /* clflush */
3826 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003827 case 0xb0 ... 0xb1: /* cmpxchg */
3828 /*
3829 * Save real source value, then compare EAX against
3830 * destination.
3831 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003832 c->src.orig_val = c->src.val;
3833 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003834 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3835 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003837 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003838 } else {
3839 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003840 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003841 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842 }
3843 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003844 case 0xb2: /* lss */
3845 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003846 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 case 0xb3:
3848 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003849 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003851 case 0xb4: /* lfs */
3852 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003853 break;
3854 case 0xb5: /* lgs */
3855 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003856 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003858 c->dst.bytes = c->op_bytes;
3859 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3860 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003863 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 case 0:
3865 goto bt;
3866 case 1:
3867 goto bts;
3868 case 2:
3869 goto btr;
3870 case 3:
3871 goto btc;
3872 }
3873 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003874 case 0xbb:
3875 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003876 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003877 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003878 case 0xbc: { /* bsf */
3879 u8 zf;
3880 __asm__ ("bsf %2, %0; setz %1"
3881 : "=r"(c->dst.val), "=q"(zf)
3882 : "r"(c->src.val));
3883 ctxt->eflags &= ~X86_EFLAGS_ZF;
3884 if (zf) {
3885 ctxt->eflags |= X86_EFLAGS_ZF;
3886 c->dst.type = OP_NONE; /* Disable writeback. */
3887 }
3888 break;
3889 }
3890 case 0xbd: { /* bsr */
3891 u8 zf;
3892 __asm__ ("bsr %2, %0; setz %1"
3893 : "=r"(c->dst.val), "=q"(zf)
3894 : "r"(c->src.val));
3895 ctxt->eflags &= ~X86_EFLAGS_ZF;
3896 if (zf) {
3897 ctxt->eflags |= X86_EFLAGS_ZF;
3898 c->dst.type = OP_NONE; /* Disable writeback. */
3899 }
3900 break;
3901 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003903 c->dst.bytes = c->op_bytes;
3904 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3905 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003906 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003907 case 0xc0 ... 0xc1: /* xadd */
3908 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3909 /* Write back the register source. */
3910 c->src.val = c->dst.orig_val;
3911 write_register_operand(&c->src);
3912 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003913 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003914 c->dst.bytes = c->op_bytes;
3915 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3916 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003917 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003919 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003920 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003921 default:
3922 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003924
3925 if (rc != X86EMUL_CONTINUE)
3926 goto done;
3927
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928 goto writeback;
3929
3930cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931 return -1;
3932}