blob: 8bfa3e3aa71dea0b05a9bf54abe8c1f39fa378b4 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b122010-07-29 15:11:49 +030049#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivityab85b122010-07-29 15:11:49 +030051#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
54#define DstAcc (4<<1) /* Destination Accumulator */
55#define DstDI (5<<1) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080057#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Avi Kivityab85b122010-07-29 15:11:49 +030058#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080059/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020060#define SrcNone (0<<4) /* No source operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020061#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030074#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030075#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080076/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030077#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080078/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030079#define Mov (1<<9)
80#define BitOp (1<<10)
81#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020082#define String (1<<12) /* String instruction (rep capable) */
83#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020084#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
85#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030086/* Misc flags */
Avi Kivity5a506b12010-08-01 15:10:29 +030087#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030088#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030089#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020090#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020091#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030092#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010093/* Source 2 operand type */
94#define Src2None (0<<29)
95#define Src2CL (1<<29)
96#define Src2ImmByte (2<<29)
97#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030098#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010099#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100
Avi Kivityd0e53322010-07-29 15:11:54 +0300101#define X2(x...) x, x
102#define X3(x...) X2(x), x
103#define X4(x...) X2(x), X2(x)
104#define X5(x...) X4(x), x
105#define X6(x...) X4(x), X2(x)
106#define X7(x...) X4(x), X3(x)
107#define X8(x...) X4(x), X4(x)
108#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300109
Avi Kivityd65b1de2010-07-29 15:11:35 +0300110struct opcode {
111 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300112 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300113 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300114 struct opcode *group;
115 struct group_dual *gdual;
116 } u;
117};
118
119struct group_dual {
120 struct opcode mod012[8];
121 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300122};
123
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200125#define EFLG_ID (1<<21)
126#define EFLG_VIP (1<<20)
127#define EFLG_VIF (1<<19)
128#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200129#define EFLG_VM (1<<17)
130#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200131#define EFLG_IOPL (3<<12)
132#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133#define EFLG_OF (1<<11)
134#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200135#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200136#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800137#define EFLG_SF (1<<7)
138#define EFLG_ZF (1<<6)
139#define EFLG_AF (1<<4)
140#define EFLG_PF (1<<2)
141#define EFLG_CF (1<<0)
142
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300143#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
144#define EFLG_RESERVED_ONE_MASK 2
145
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146/*
147 * Instruction emulation:
148 * Most instructions are emulated directly via a fragment of inline assembly
149 * code. This allows us to save/restore EFLAGS and thus very easily pick up
150 * any modified flags.
151 */
152
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800153#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154#define _LO32 "k" /* force 32-bit operand */
155#define _STK "%%rsp" /* stack pointer */
156#elif defined(__i386__)
157#define _LO32 "" /* force 32-bit operand */
158#define _STK "%%esp" /* stack pointer */
159#endif
160
161/*
162 * These EFLAGS bits are restored from saved value during emulation, and
163 * any changes are written back to the saved value after emulation.
164 */
165#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
166
167/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200168#define _PRE_EFLAGS(_sav, _msk, _tmp) \
169 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
170 "movl %"_sav",%"_LO32 _tmp"; " \
171 "push %"_tmp"; " \
172 "push %"_tmp"; " \
173 "movl %"_msk",%"_LO32 _tmp"; " \
174 "andl %"_LO32 _tmp",("_STK"); " \
175 "pushf; " \
176 "notl %"_LO32 _tmp"; " \
177 "andl %"_LO32 _tmp",("_STK"); " \
178 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
179 "pop %"_tmp"; " \
180 "orl %"_LO32 _tmp",("_STK"); " \
181 "popf; " \
182 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183
184/* After executing instruction: write-back necessary bits in EFLAGS. */
185#define _POST_EFLAGS(_sav, _msk, _tmp) \
186 /* _sav |= EFLAGS & _msk; */ \
187 "pushf; " \
188 "pop %"_tmp"; " \
189 "andl %"_msk",%"_LO32 _tmp"; " \
190 "orl %"_LO32 _tmp",%"_sav"; "
191
Avi Kivitydda96d82008-11-26 15:14:10 +0200192#ifdef CONFIG_X86_64
193#define ON64(x) x
194#else
195#define ON64(x)
196#endif
197
Avi Kivityb3b3d252010-08-16 17:49:52 +0300198#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200199 do { \
200 __asm__ __volatile__ ( \
201 _PRE_EFLAGS("0", "4", "2") \
202 _op _suffix " %"_x"3,%1; " \
203 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300204 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200205 "=&r" (_tmp) \
206 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200207 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200208
209
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210/* Raw emulation: instruction has two explicit operands. */
211#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200212 do { \
213 unsigned long _tmp; \
214 \
215 switch ((_dst).bytes) { \
216 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300217 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200218 break; \
219 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300220 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200221 break; \
222 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300223 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200224 break; \
225 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 } while (0)
227
228#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
229 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200230 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400231 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300233 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234 break; \
235 default: \
236 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
237 _wx, _wy, _lx, _ly, _qx, _qy); \
238 break; \
239 } \
240 } while (0)
241
242/* Source operand is byte-sized and may be restricted to just %cl. */
243#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
244 __emulate_2op(_op, _src, _dst, _eflags, \
245 "b", "c", "b", "c", "b", "c", "b", "c")
246
247/* Source operand is byte, word, long or quad sized. */
248#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
249 __emulate_2op(_op, _src, _dst, _eflags, \
250 "b", "q", "w", "r", _LO32, "r", "", "r")
251
252/* Source operand is word, long or quad sized. */
253#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
254 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
255 "w", "r", _LO32, "r", "", "r")
256
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100257/* Instruction has three operands and one operand is stored in ECX register */
258#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
259 do { \
260 unsigned long _tmp; \
261 _type _clv = (_cl).val; \
262 _type _srcv = (_src).val; \
263 _type _dstv = (_dst).val; \
264 \
265 __asm__ __volatile__ ( \
266 _PRE_EFLAGS("0", "5", "2") \
267 _op _suffix " %4,%1 \n" \
268 _POST_EFLAGS("0", "5", "2") \
269 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
270 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
271 ); \
272 \
273 (_cl).val = (unsigned long) _clv; \
274 (_src).val = (unsigned long) _srcv; \
275 (_dst).val = (unsigned long) _dstv; \
276 } while (0)
277
278#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
279 do { \
280 switch ((_dst).bytes) { \
281 case 2: \
282 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
283 "w", unsigned short); \
284 break; \
285 case 4: \
286 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
287 "l", unsigned int); \
288 break; \
289 case 8: \
290 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
291 "q", unsigned long)); \
292 break; \
293 } \
294 } while (0)
295
Avi Kivitydda96d82008-11-26 15:14:10 +0200296#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297 do { \
298 unsigned long _tmp; \
299 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200300 __asm__ __volatile__ ( \
301 _PRE_EFLAGS("0", "3", "2") \
302 _op _suffix " %1; " \
303 _POST_EFLAGS("0", "3", "2") \
304 : "=m" (_eflags), "+m" ((_dst).val), \
305 "=&r" (_tmp) \
306 : "i" (EFLAGS_MASK)); \
307 } while (0)
308
309/* Instruction has only one explicit operand (no source operand). */
310#define emulate_1op(_op, _dst, _eflags) \
311 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400312 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200313 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
314 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
315 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
316 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 } \
318 } while (0)
319
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300320#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
321 do { \
322 unsigned long _tmp; \
323 \
324 __asm__ __volatile__ ( \
325 _PRE_EFLAGS("0", "4", "1") \
326 _op _suffix " %5; " \
327 _POST_EFLAGS("0", "4", "1") \
328 : "=m" (_eflags), "=&r" (_tmp), \
329 "+a" (_rax), "+d" (_rdx) \
330 : "i" (EFLAGS_MASK), "m" ((_src).val), \
331 "a" (_rax), "d" (_rdx)); \
332 } while (0)
333
Avi Kivityf6b35972010-08-26 11:59:00 +0300334#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
335 do { \
336 unsigned long _tmp; \
337 \
338 __asm__ __volatile__ ( \
339 _PRE_EFLAGS("0", "5", "1") \
340 "1: \n\t" \
341 _op _suffix " %6; " \
342 "2: \n\t" \
343 _POST_EFLAGS("0", "5", "1") \
344 ".pushsection .fixup,\"ax\" \n\t" \
345 "3: movb $1, %4 \n\t" \
346 "jmp 2b \n\t" \
347 ".popsection \n\t" \
348 _ASM_EXTABLE(1b, 3b) \
349 : "=m" (_eflags), "=&r" (_tmp), \
350 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
351 : "i" (EFLAGS_MASK), "m" ((_src).val), \
352 "a" (_rax), "d" (_rdx)); \
353 } while (0)
354
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300355/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
356#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
357 do { \
358 switch((_src).bytes) { \
359 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
360 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
361 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
362 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
363 } \
364 } while (0)
365
Avi Kivityf6b35972010-08-26 11:59:00 +0300366#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
367 do { \
368 switch((_src).bytes) { \
369 case 1: \
370 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
371 _eflags, "b", _ex); \
372 break; \
373 case 2: \
374 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
375 _eflags, "w", _ex); \
376 break; \
377 case 4: \
378 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
379 _eflags, "l", _ex); \
380 break; \
381 case 8: ON64( \
382 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
383 _eflags, "q", _ex)); \
384 break; \
385 } \
386 } while (0)
387
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388/* Fetch next part of the instruction being emulated. */
389#define insn_fetch(_type, _size, _eip) \
390({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200391 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200392 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393 goto done; \
394 (_eip) += (_size); \
395 (_type)_x; \
396})
397
Gleb Natapov414e6272010-04-28 19:15:26 +0300398#define insn_fetch_arr(_arr, _size, _eip) \
399({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
400 if (rc != X86EMUL_CONTINUE) \
401 goto done; \
402 (_eip) += (_size); \
403})
404
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800405static inline unsigned long ad_mask(struct decode_cache *c)
406{
407 return (1UL << (c->ad_bytes << 3)) - 1;
408}
409
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800411static inline unsigned long
412address_mask(struct decode_cache *c, unsigned long reg)
413{
414 if (c->ad_bytes == sizeof(unsigned long))
415 return reg;
416 else
417 return reg & ad_mask(c);
418}
419
420static inline unsigned long
421register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
422{
423 return base + address_mask(c, reg);
424}
425
Harvey Harrison7a9572752008-02-19 07:40:41 -0800426static inline void
427register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
428{
429 if (c->ad_bytes == sizeof(unsigned long))
430 *reg += inc;
431 else
432 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
433}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434
Harvey Harrison7a9572752008-02-19 07:40:41 -0800435static inline void jmp_rel(struct decode_cache *c, int rel)
436{
437 register_address_increment(c, &c->eip, rel);
438}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300439
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300440static void set_seg_override(struct decode_cache *c, int seg)
441{
442 c->has_seg_override = true;
443 c->seg_override = seg;
444}
445
Gleb Natapov79168fd2010-04-28 19:15:30 +0300446static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
447 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300448{
449 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
450 return 0;
451
Gleb Natapov79168fd2010-04-28 19:15:30 +0300452 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300453}
454
455static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300456 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300457 struct decode_cache *c)
458{
459 if (!c->has_seg_override)
460 return 0;
461
Gleb Natapov79168fd2010-04-28 19:15:30 +0300462 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300463}
464
Gleb Natapov79168fd2010-04-28 19:15:30 +0300465static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
466 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300467{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300468 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300469}
470
Gleb Natapov79168fd2010-04-28 19:15:30 +0300471static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
472 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300473{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300474 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300475}
476
Gleb Natapov54b84862010-04-28 19:15:44 +0300477static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
478 u32 error, bool valid)
479{
480 ctxt->exception = vec;
481 ctxt->error_code = error;
482 ctxt->error_code_valid = valid;
Gleb Natapov54b84862010-04-28 19:15:44 +0300483}
484
485static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
486{
487 emulate_exception(ctxt, GP_VECTOR, err, true);
488}
489
490static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
491 int err)
492{
493 ctxt->cr2 = addr;
494 emulate_exception(ctxt, PF_VECTOR, err, true);
495}
496
497static void emulate_ud(struct x86_emulate_ctxt *ctxt)
498{
499 emulate_exception(ctxt, UD_VECTOR, 0, false);
500}
501
502static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
503{
504 emulate_exception(ctxt, TS_VECTOR, err, true);
505}
506
Avi Kivity34d1f492010-08-26 11:59:01 +0300507static int emulate_de(struct x86_emulate_ctxt *ctxt)
508{
509 emulate_exception(ctxt, DE_VECTOR, 0, false);
510 return X86EMUL_PROPAGATE_FAULT;
511}
512
Avi Kivity62266862007-11-20 13:15:52 +0200513static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
514 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300515 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200516{
517 struct fetch_cache *fc = &ctxt->decode.fetch;
518 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300519 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200520
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300521 if (eip == fc->end) {
522 cur_size = fc->end - fc->start;
523 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
524 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
525 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900526 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200527 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300528 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200529 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300530 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900531 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200532}
533
534static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
535 struct x86_emulate_ops *ops,
536 unsigned long eip, void *dest, unsigned size)
537{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900538 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200539
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200540 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200541 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200542 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200543 while (size--) {
544 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900545 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200546 return rc;
547 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900548 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200549}
550
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000551/*
552 * Given the 'reg' portion of a ModRM byte, and a register block, return a
553 * pointer into the block that addresses the relevant register.
554 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
555 */
556static void *decode_register(u8 modrm_reg, unsigned long *regs,
557 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558{
559 void *p;
560
561 p = &regs[modrm_reg];
562 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
563 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
564 return p;
565}
566
567static int read_descriptor(struct x86_emulate_ctxt *ctxt,
568 struct x86_emulate_ops *ops,
Avi Kivity1a6440a2010-08-01 12:35:10 +0300569 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 u16 *size, unsigned long *address, int op_bytes)
571{
572 int rc;
573
574 if (op_bytes == 2)
575 op_bytes = 3;
576 *address = 0;
Avi Kivity1a6440a2010-08-01 12:35:10 +0300577 rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900578 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 return rc;
Avi Kivity1a6440a2010-08-01 12:35:10 +0300580 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 return rc;
582}
583
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300584static int test_cc(unsigned int condition, unsigned int flags)
585{
586 int rc = 0;
587
588 switch ((condition & 15) >> 1) {
589 case 0: /* o */
590 rc |= (flags & EFLG_OF);
591 break;
592 case 1: /* b/c/nae */
593 rc |= (flags & EFLG_CF);
594 break;
595 case 2: /* z/e */
596 rc |= (flags & EFLG_ZF);
597 break;
598 case 3: /* be/na */
599 rc |= (flags & (EFLG_CF|EFLG_ZF));
600 break;
601 case 4: /* s */
602 rc |= (flags & EFLG_SF);
603 break;
604 case 5: /* p/pe */
605 rc |= (flags & EFLG_PF);
606 break;
607 case 7: /* le/ng */
608 rc |= (flags & EFLG_ZF);
609 /* fall through */
610 case 6: /* l/nge */
611 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
612 break;
613 }
614
615 /* Odd condition identifiers (lsb == 1) have inverted sense. */
616 return (!!rc ^ (condition & 1));
617}
618
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300619static void fetch_register_operand(struct operand *op)
620{
621 switch (op->bytes) {
622 case 1:
623 op->val = *(u8 *)op->addr.reg;
624 break;
625 case 2:
626 op->val = *(u16 *)op->addr.reg;
627 break;
628 case 4:
629 op->val = *(u32 *)op->addr.reg;
630 break;
631 case 8:
632 op->val = *(u64 *)op->addr.reg;
633 break;
634 }
635}
636
Avi Kivity3c118e22007-10-31 10:27:04 +0200637static void decode_register_operand(struct operand *op,
638 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200639 int inhibit_bytereg)
640{
Avi Kivity33615aa2007-10-31 11:15:56 +0200641 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200642 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200643
644 if (!(c->d & ModRM))
645 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200646 op->type = OP_REG;
647 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300648 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200649 op->bytes = 1;
650 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300651 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200652 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200653 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300654 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200655 op->orig_val = op->val;
656}
657
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200658static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300659 struct x86_emulate_ops *ops,
660 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200661{
662 struct decode_cache *c = &ctxt->decode;
663 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700664 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900665 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300666 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200667
668 if (c->rex_prefix) {
669 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
670 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
671 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
672 }
673
674 c->modrm = insn_fetch(u8, 1, c->eip);
675 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
676 c->modrm_reg |= (c->modrm & 0x38) >> 3;
677 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300678 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200679
680 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300681 op->type = OP_REG;
682 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
683 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300684 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300685 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200686 return rc;
687 }
688
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300689 op->type = OP_MEM;
690
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200691 if (c->ad_bytes == 2) {
692 unsigned bx = c->regs[VCPU_REGS_RBX];
693 unsigned bp = c->regs[VCPU_REGS_RBP];
694 unsigned si = c->regs[VCPU_REGS_RSI];
695 unsigned di = c->regs[VCPU_REGS_RDI];
696
697 /* 16-bit ModR/M decode. */
698 switch (c->modrm_mod) {
699 case 0:
700 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300701 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200702 break;
703 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300704 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200705 break;
706 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300707 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200708 break;
709 }
710 switch (c->modrm_rm) {
711 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300712 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200713 break;
714 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300715 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200716 break;
717 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300718 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200719 break;
720 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300721 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200722 break;
723 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300724 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200725 break;
726 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300727 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200728 break;
729 case 6:
730 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300731 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200732 break;
733 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300734 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200735 break;
736 }
737 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
738 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300739 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300740 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200741 } else {
742 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700743 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200744 sib = insn_fetch(u8, 1, c->eip);
745 index_reg |= (sib >> 3) & 7;
746 base_reg |= sib & 7;
747 scale = sib >> 6;
748
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700749 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300750 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700751 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300752 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700753 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300754 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700755 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
756 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700757 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700758 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300759 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200760 switch (c->modrm_mod) {
761 case 0:
762 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300763 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200764 break;
765 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300766 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200767 break;
768 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300769 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200770 break;
771 }
772 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300773 op->addr.mem = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200774done:
775 return rc;
776}
777
778static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300779 struct x86_emulate_ops *ops,
780 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200781{
782 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900783 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200784
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300785 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200786 switch (c->ad_bytes) {
787 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300788 op->addr.mem = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200789 break;
790 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300791 op->addr.mem = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200792 break;
793 case 8:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300794 op->addr.mem = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200795 break;
796 }
797done:
798 return rc;
799}
800
Wei Yongjun35c843c2010-08-09 11:34:56 +0800801static void fetch_bit_operand(struct decode_cache *c)
802{
803 long sv, mask;
804
Wei Yongjun3885f182010-08-09 11:37:37 +0800805 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800806 mask = ~(c->dst.bytes * 8 - 1);
807
808 if (c->src.bytes == 2)
809 sv = (s16)c->src.val & (s16)mask;
810 else if (c->src.bytes == 4)
811 sv = (s32)c->src.val & (s32)mask;
812
813 c->dst.addr.mem += (sv >> 3);
814 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800815
816 /* only subword offset */
817 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800818}
819
Gleb Natapov9de41572010-04-28 19:15:22 +0300820static int read_emulated(struct x86_emulate_ctxt *ctxt,
821 struct x86_emulate_ops *ops,
822 unsigned long addr, void *dest, unsigned size)
823{
824 int rc;
825 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300826 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300827
828 while (size) {
829 int n = min(size, 8u);
830 size -= n;
831 if (mc->pos < mc->end)
832 goto read_cached;
833
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300834 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
835 ctxt->vcpu);
836 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300837 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300838 if (rc != X86EMUL_CONTINUE)
839 return rc;
840 mc->end += n;
841
842 read_cached:
843 memcpy(dest, mc->data + mc->pos, n);
844 mc->pos += n;
845 dest += n;
846 addr += n;
847 }
848 return X86EMUL_CONTINUE;
849}
850
Gleb Natapov7b262e92010-03-18 15:20:27 +0200851static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
852 struct x86_emulate_ops *ops,
853 unsigned int size, unsigned short port,
854 void *dest)
855{
856 struct read_cache *rc = &ctxt->decode.io_read;
857
858 if (rc->pos == rc->end) { /* refill pio read ahead */
859 struct decode_cache *c = &ctxt->decode;
860 unsigned int in_page, n;
861 unsigned int count = c->rep_prefix ?
862 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
863 in_page = (ctxt->eflags & EFLG_DF) ?
864 offset_in_page(c->regs[VCPU_REGS_RDI]) :
865 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
866 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
867 count);
868 if (n == 0)
869 n = 1;
870 rc->pos = rc->end = 0;
871 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
872 return 0;
873 rc->end = n * size;
874 }
875
876 memcpy(dest, rc->data + rc->pos, size);
877 rc->pos += size;
878 return 1;
879}
880
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200881static u32 desc_limit_scaled(struct desc_struct *desc)
882{
883 u32 limit = get_desc_limit(desc);
884
885 return desc->g ? (limit << 12) | 0xfff : limit;
886}
887
888static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
889 struct x86_emulate_ops *ops,
890 u16 selector, struct desc_ptr *dt)
891{
892 if (selector & 1 << 2) {
893 struct desc_struct desc;
894 memset (dt, 0, sizeof *dt);
895 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
896 return;
897
898 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
899 dt->address = get_desc_base(&desc);
900 } else
901 ops->get_gdt(dt, ctxt->vcpu);
902}
903
904/* allowed just for 8 bytes segments */
905static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
906 struct x86_emulate_ops *ops,
907 u16 selector, struct desc_struct *desc)
908{
909 struct desc_ptr dt;
910 u16 index = selector >> 3;
911 int ret;
912 u32 err;
913 ulong addr;
914
915 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
916
917 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300918 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200919 return X86EMUL_PROPAGATE_FAULT;
920 }
921 addr = dt.address + index * 8;
922 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
923 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300924 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200925
926 return ret;
927}
928
929/* allowed just for 8 bytes segments */
930static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
931 struct x86_emulate_ops *ops,
932 u16 selector, struct desc_struct *desc)
933{
934 struct desc_ptr dt;
935 u16 index = selector >> 3;
936 u32 err;
937 ulong addr;
938 int ret;
939
940 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
941
942 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300943 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200944 return X86EMUL_PROPAGATE_FAULT;
945 }
946
947 addr = dt.address + index * 8;
948 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
949 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300950 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200951
952 return ret;
953}
954
955static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
956 struct x86_emulate_ops *ops,
957 u16 selector, int seg)
958{
959 struct desc_struct seg_desc;
960 u8 dpl, rpl, cpl;
961 unsigned err_vec = GP_VECTOR;
962 u32 err_code = 0;
963 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
964 int ret;
965
966 memset(&seg_desc, 0, sizeof seg_desc);
967
968 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
969 || ctxt->mode == X86EMUL_MODE_REAL) {
970 /* set real mode segment descriptor */
971 set_desc_base(&seg_desc, selector << 4);
972 set_desc_limit(&seg_desc, 0xffff);
973 seg_desc.type = 3;
974 seg_desc.p = 1;
975 seg_desc.s = 1;
976 goto load;
977 }
978
979 /* NULL selector is not valid for TR, CS and SS */
980 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
981 && null_selector)
982 goto exception;
983
984 /* TR should be in GDT only */
985 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
986 goto exception;
987
988 if (null_selector) /* for NULL selector skip all following checks */
989 goto load;
990
991 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
992 if (ret != X86EMUL_CONTINUE)
993 return ret;
994
995 err_code = selector & 0xfffc;
996 err_vec = GP_VECTOR;
997
998 /* can't load system descriptor into segment selecor */
999 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1000 goto exception;
1001
1002 if (!seg_desc.p) {
1003 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1004 goto exception;
1005 }
1006
1007 rpl = selector & 3;
1008 dpl = seg_desc.dpl;
1009 cpl = ops->cpl(ctxt->vcpu);
1010
1011 switch (seg) {
1012 case VCPU_SREG_SS:
1013 /*
1014 * segment is not a writable data segment or segment
1015 * selector's RPL != CPL or segment selector's RPL != CPL
1016 */
1017 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1018 goto exception;
1019 break;
1020 case VCPU_SREG_CS:
1021 if (!(seg_desc.type & 8))
1022 goto exception;
1023
1024 if (seg_desc.type & 4) {
1025 /* conforming */
1026 if (dpl > cpl)
1027 goto exception;
1028 } else {
1029 /* nonconforming */
1030 if (rpl > cpl || dpl != cpl)
1031 goto exception;
1032 }
1033 /* CS(RPL) <- CPL */
1034 selector = (selector & 0xfffc) | cpl;
1035 break;
1036 case VCPU_SREG_TR:
1037 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1038 goto exception;
1039 break;
1040 case VCPU_SREG_LDTR:
1041 if (seg_desc.s || seg_desc.type != 2)
1042 goto exception;
1043 break;
1044 default: /* DS, ES, FS, or GS */
1045 /*
1046 * segment is not a data or readable code segment or
1047 * ((segment is a data or nonconforming code segment)
1048 * and (both RPL and CPL > DPL))
1049 */
1050 if ((seg_desc.type & 0xa) == 0x8 ||
1051 (((seg_desc.type & 0xc) != 0xc) &&
1052 (rpl > dpl && cpl > dpl)))
1053 goto exception;
1054 break;
1055 }
1056
1057 if (seg_desc.s) {
1058 /* mark segment as accessed */
1059 seg_desc.type |= 1;
1060 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1061 if (ret != X86EMUL_CONTINUE)
1062 return ret;
1063 }
1064load:
1065 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1066 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1067 return X86EMUL_CONTINUE;
1068exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001069 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001070 return X86EMUL_PROPAGATE_FAULT;
1071}
1072
Wei Yongjun31be40b2010-08-17 09:17:30 +08001073static void write_register_operand(struct operand *op)
1074{
1075 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1076 switch (op->bytes) {
1077 case 1:
1078 *(u8 *)op->addr.reg = (u8)op->val;
1079 break;
1080 case 2:
1081 *(u16 *)op->addr.reg = (u16)op->val;
1082 break;
1083 case 4:
1084 *op->addr.reg = (u32)op->val;
1085 break; /* 64b: zero-extend */
1086 case 8:
1087 *op->addr.reg = op->val;
1088 break;
1089 }
1090}
1091
Wei Yongjunc37eda12010-06-15 09:03:33 +08001092static inline int writeback(struct x86_emulate_ctxt *ctxt,
1093 struct x86_emulate_ops *ops)
1094{
1095 int rc;
1096 struct decode_cache *c = &ctxt->decode;
1097 u32 err;
1098
1099 switch (c->dst.type) {
1100 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001101 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001102 break;
1103 case OP_MEM:
1104 if (c->lock_prefix)
1105 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440a2010-08-01 12:35:10 +03001106 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001107 &c->dst.orig_val,
1108 &c->dst.val,
1109 c->dst.bytes,
1110 &err,
1111 ctxt->vcpu);
1112 else
1113 rc = ops->write_emulated(
Avi Kivity1a6440a2010-08-01 12:35:10 +03001114 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001115 &c->dst.val,
1116 c->dst.bytes,
1117 &err,
1118 ctxt->vcpu);
1119 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440a2010-08-01 12:35:10 +03001120 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001121 if (rc != X86EMUL_CONTINUE)
1122 return rc;
1123 break;
1124 case OP_NONE:
1125 /* no writeback */
1126 break;
1127 default:
1128 break;
1129 }
1130 return X86EMUL_CONTINUE;
1131}
1132
Gleb Natapov79168fd2010-04-28 19:15:30 +03001133static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1134 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001135{
1136 struct decode_cache *c = &ctxt->decode;
1137
1138 c->dst.type = OP_MEM;
1139 c->dst.bytes = c->op_bytes;
1140 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001141 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440a2010-08-01 12:35:10 +03001142 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1143 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001144}
1145
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001146static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001147 struct x86_emulate_ops *ops,
1148 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001149{
1150 struct decode_cache *c = &ctxt->decode;
1151 int rc;
1152
Gleb Natapov79168fd2010-04-28 19:15:30 +03001153 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001154 c->regs[VCPU_REGS_RSP]),
1155 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001156 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001157 return rc;
1158
Avi Kivity350f69d2009-01-05 11:12:40 +02001159 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001160 return rc;
1161}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001162
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001163static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1164 struct x86_emulate_ops *ops,
1165 void *dest, int len)
1166{
1167 int rc;
1168 unsigned long val, change_mask;
1169 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001170 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001171
1172 rc = emulate_pop(ctxt, ops, &val, len);
1173 if (rc != X86EMUL_CONTINUE)
1174 return rc;
1175
1176 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1177 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1178
1179 switch(ctxt->mode) {
1180 case X86EMUL_MODE_PROT64:
1181 case X86EMUL_MODE_PROT32:
1182 case X86EMUL_MODE_PROT16:
1183 if (cpl == 0)
1184 change_mask |= EFLG_IOPL;
1185 if (cpl <= iopl)
1186 change_mask |= EFLG_IF;
1187 break;
1188 case X86EMUL_MODE_VM86:
1189 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001190 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001191 return X86EMUL_PROPAGATE_FAULT;
1192 }
1193 change_mask |= EFLG_IF;
1194 break;
1195 default: /* real mode */
1196 change_mask |= (EFLG_IOPL | EFLG_IF);
1197 break;
1198 }
1199
1200 *(unsigned long *)dest =
1201 (ctxt->eflags & ~change_mask) | (val & change_mask);
1202
1203 return rc;
1204}
1205
Gleb Natapov79168fd2010-04-28 19:15:30 +03001206static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1207 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001208{
1209 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001210
Gleb Natapov79168fd2010-04-28 19:15:30 +03001211 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001212
Gleb Natapov79168fd2010-04-28 19:15:30 +03001213 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001214}
1215
1216static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1217 struct x86_emulate_ops *ops, int seg)
1218{
1219 struct decode_cache *c = &ctxt->decode;
1220 unsigned long selector;
1221 int rc;
1222
1223 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001224 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001225 return rc;
1226
Gleb Natapov2e873022010-03-18 15:20:18 +02001227 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001228 return rc;
1229}
1230
Wei Yongjunc37eda12010-06-15 09:03:33 +08001231static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001232 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001233{
1234 struct decode_cache *c = &ctxt->decode;
1235 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001236 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001237 int reg = VCPU_REGS_RAX;
1238
1239 while (reg <= VCPU_REGS_RDI) {
1240 (reg == VCPU_REGS_RSP) ?
1241 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1242
Gleb Natapov79168fd2010-04-28 19:15:30 +03001243 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001244
1245 rc = writeback(ctxt, ops);
1246 if (rc != X86EMUL_CONTINUE)
1247 return rc;
1248
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001249 ++reg;
1250 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001251
1252 /* Disable writeback. */
1253 c->dst.type = OP_NONE;
1254
1255 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001256}
1257
1258static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1259 struct x86_emulate_ops *ops)
1260{
1261 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001262 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001263 int reg = VCPU_REGS_RDI;
1264
1265 while (reg >= VCPU_REGS_RAX) {
1266 if (reg == VCPU_REGS_RSP) {
1267 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1268 c->op_bytes);
1269 --reg;
1270 }
1271
1272 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001273 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001274 break;
1275 --reg;
1276 }
1277 return rc;
1278}
1279
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001280int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1281 struct x86_emulate_ops *ops, int irq)
1282{
1283 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001284 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001285 struct desc_ptr dt;
1286 gva_t cs_addr;
1287 gva_t eip_addr;
1288 u16 cs, eip;
1289 u32 err;
1290
1291 /* TODO: Add limit checks */
1292 c->src.val = ctxt->eflags;
1293 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001294 rc = writeback(ctxt, ops);
1295 if (rc != X86EMUL_CONTINUE)
1296 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001297
1298 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1299
1300 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1301 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001302 rc = writeback(ctxt, ops);
1303 if (rc != X86EMUL_CONTINUE)
1304 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001305
1306 c->src.val = c->eip;
1307 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001308 rc = writeback(ctxt, ops);
1309 if (rc != X86EMUL_CONTINUE)
1310 return rc;
1311
1312 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001313
1314 ops->get_idt(&dt, ctxt->vcpu);
1315
1316 eip_addr = dt.address + (irq << 2);
1317 cs_addr = dt.address + (irq << 2) + 2;
1318
1319 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &err);
1320 if (rc != X86EMUL_CONTINUE)
1321 return rc;
1322
1323 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &err);
1324 if (rc != X86EMUL_CONTINUE)
1325 return rc;
1326
1327 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1328 if (rc != X86EMUL_CONTINUE)
1329 return rc;
1330
1331 c->eip = eip;
1332
1333 return rc;
1334}
1335
1336static int emulate_int(struct x86_emulate_ctxt *ctxt,
1337 struct x86_emulate_ops *ops, int irq)
1338{
1339 switch(ctxt->mode) {
1340 case X86EMUL_MODE_REAL:
1341 return emulate_int_real(ctxt, ops, irq);
1342 case X86EMUL_MODE_VM86:
1343 case X86EMUL_MODE_PROT16:
1344 case X86EMUL_MODE_PROT32:
1345 case X86EMUL_MODE_PROT64:
1346 default:
1347 /* Protected mode interrupts unimplemented yet */
1348 return X86EMUL_UNHANDLEABLE;
1349 }
1350}
1351
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001352static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1353 struct x86_emulate_ops *ops)
1354{
1355 struct decode_cache *c = &ctxt->decode;
1356 int rc = X86EMUL_CONTINUE;
1357 unsigned long temp_eip = 0;
1358 unsigned long temp_eflags = 0;
1359 unsigned long cs = 0;
1360 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1361 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1362 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1363 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1364
1365 /* TODO: Add stack limit check */
1366
1367 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1368
1369 if (rc != X86EMUL_CONTINUE)
1370 return rc;
1371
1372 if (temp_eip & ~0xffff) {
1373 emulate_gp(ctxt, 0);
1374 return X86EMUL_PROPAGATE_FAULT;
1375 }
1376
1377 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1378
1379 if (rc != X86EMUL_CONTINUE)
1380 return rc;
1381
1382 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1383
1384 if (rc != X86EMUL_CONTINUE)
1385 return rc;
1386
1387 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1388
1389 if (rc != X86EMUL_CONTINUE)
1390 return rc;
1391
1392 c->eip = temp_eip;
1393
1394
1395 if (c->op_bytes == 4)
1396 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1397 else if (c->op_bytes == 2) {
1398 ctxt->eflags &= ~0xffff;
1399 ctxt->eflags |= temp_eflags;
1400 }
1401
1402 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1403 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1404
1405 return rc;
1406}
1407
1408static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1409 struct x86_emulate_ops* ops)
1410{
1411 switch(ctxt->mode) {
1412 case X86EMUL_MODE_REAL:
1413 return emulate_iret_real(ctxt, ops);
1414 case X86EMUL_MODE_VM86:
1415 case X86EMUL_MODE_PROT16:
1416 case X86EMUL_MODE_PROT32:
1417 case X86EMUL_MODE_PROT64:
1418 default:
1419 /* iret from protected mode unimplemented yet */
1420 return X86EMUL_UNHANDLEABLE;
1421 }
1422}
1423
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001424static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1425 struct x86_emulate_ops *ops)
1426{
1427 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001428
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001429 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001430}
1431
Laurent Vivier05f086f2007-09-24 11:10:55 +02001432static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001433{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001434 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001435 switch (c->modrm_reg) {
1436 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001437 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001438 break;
1439 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001440 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001441 break;
1442 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001443 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001444 break;
1445 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001446 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001447 break;
1448 case 4: /* sal/shl */
1449 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001450 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001451 break;
1452 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001453 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001454 break;
1455 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001456 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001457 break;
1458 }
1459}
1460
1461static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001462 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001463{
1464 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001465 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1466 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001467 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001468
1469 switch (c->modrm_reg) {
1470 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001471 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001472 break;
1473 case 2: /* not */
1474 c->dst.val = ~c->dst.val;
1475 break;
1476 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001477 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001478 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001479 case 4: /* mul */
1480 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1481 break;
1482 case 5: /* imul */
1483 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1484 break;
1485 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001486 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1487 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001488 break;
1489 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001490 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1491 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001492 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001493 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001494 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001495 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001496 if (de)
1497 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001498 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001499}
1500
1501static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001502 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001503{
1504 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001505
1506 switch (c->modrm_reg) {
1507 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001508 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001509 break;
1510 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001511 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001512 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001513 case 2: /* call near abs */ {
1514 long int old_eip;
1515 old_eip = c->eip;
1516 c->eip = c->src.val;
1517 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001518 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001519 break;
1520 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001521 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001522 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001523 break;
1524 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001525 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001526 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001527 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001528 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529}
1530
1531static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001532 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001533{
1534 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001535 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001536
1537 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1538 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001539 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1540 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001541 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001542 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001543 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1544 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001545
Laurent Vivier05f086f2007-09-24 11:10:55 +02001546 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001547 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001548 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001549}
1550
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001551static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1552 struct x86_emulate_ops *ops)
1553{
1554 struct decode_cache *c = &ctxt->decode;
1555 int rc;
1556 unsigned long cs;
1557
1558 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001559 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001560 return rc;
1561 if (c->op_bytes == 4)
1562 c->eip = (u32)c->eip;
1563 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001564 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001565 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001566 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001567 return rc;
1568}
1569
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001570static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1571 struct x86_emulate_ops *ops, int seg)
1572{
1573 struct decode_cache *c = &ctxt->decode;
1574 unsigned short sel;
1575 int rc;
1576
1577 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1578
1579 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1580 if (rc != X86EMUL_CONTINUE)
1581 return rc;
1582
1583 c->dst.val = c->src.val;
1584 return rc;
1585}
1586
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001587static inline void
1588setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001589 struct x86_emulate_ops *ops, struct desc_struct *cs,
1590 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001591{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001592 memset(cs, 0, sizeof(struct desc_struct));
1593 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1594 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001595
1596 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001597 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001598 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001599 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001600 cs->type = 0x0b; /* Read, Execute, Accessed */
1601 cs->s = 1;
1602 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001603 cs->p = 1;
1604 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001605
Gleb Natapov79168fd2010-04-28 19:15:30 +03001606 set_desc_base(ss, 0); /* flat segment */
1607 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001608 ss->g = 1; /* 4kb granularity */
1609 ss->s = 1;
1610 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001611 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001612 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001613 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001614}
1615
1616static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001617emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001618{
1619 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001620 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001621 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001622 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001623
1624 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001625 if (ctxt->mode == X86EMUL_MODE_REAL ||
1626 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001627 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001628 return X86EMUL_PROPAGATE_FAULT;
1629 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001630
Gleb Natapov79168fd2010-04-28 19:15:30 +03001631 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001632
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001633 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001634 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001635 cs_sel = (u16)(msr_data & 0xfffc);
1636 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001637
1638 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001639 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001640 cs.l = 1;
1641 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001642 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1643 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1644 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1645 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001646
1647 c->regs[VCPU_REGS_RCX] = c->eip;
1648 if (is_long_mode(ctxt->vcpu)) {
1649#ifdef CONFIG_X86_64
1650 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1651
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001652 ops->get_msr(ctxt->vcpu,
1653 ctxt->mode == X86EMUL_MODE_PROT64 ?
1654 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001655 c->eip = msr_data;
1656
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001657 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001658 ctxt->eflags &= ~(msr_data | EFLG_RF);
1659#endif
1660 } else {
1661 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001662 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001663 c->eip = (u32)msr_data;
1664
1665 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1666 }
1667
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001668 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001669}
1670
Andre Przywara8c604352009-06-18 12:56:01 +02001671static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001672emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001673{
1674 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001675 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001676 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001677 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001678
Gleb Natapova0044752010-02-10 14:21:31 +02001679 /* inject #GP if in real mode */
1680 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001681 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001682 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001683 }
1684
1685 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1686 * Therefore, we inject an #UD.
1687 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001688 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001689 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001690 return X86EMUL_PROPAGATE_FAULT;
1691 }
Andre Przywara8c604352009-06-18 12:56:01 +02001692
Gleb Natapov79168fd2010-04-28 19:15:30 +03001693 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001694
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001695 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001696 switch (ctxt->mode) {
1697 case X86EMUL_MODE_PROT32:
1698 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001699 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001700 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001701 }
1702 break;
1703 case X86EMUL_MODE_PROT64:
1704 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001705 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001706 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001707 }
1708 break;
1709 }
1710
1711 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001712 cs_sel = (u16)msr_data;
1713 cs_sel &= ~SELECTOR_RPL_MASK;
1714 ss_sel = cs_sel + 8;
1715 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001716 if (ctxt->mode == X86EMUL_MODE_PROT64
1717 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001718 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001719 cs.l = 1;
1720 }
1721
Gleb Natapov79168fd2010-04-28 19:15:30 +03001722 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1723 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1724 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1725 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001726
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001727 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001728 c->eip = msr_data;
1729
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001730 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001731 c->regs[VCPU_REGS_RSP] = msr_data;
1732
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001733 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001734}
1735
Andre Przywara4668f052009-06-18 12:56:02 +02001736static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001737emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001738{
1739 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001740 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001741 u64 msr_data;
1742 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001743 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001744
Gleb Natapova0044752010-02-10 14:21:31 +02001745 /* inject #GP if in real mode or Virtual 8086 mode */
1746 if (ctxt->mode == X86EMUL_MODE_REAL ||
1747 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001748 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001749 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001750 }
1751
Gleb Natapov79168fd2010-04-28 19:15:30 +03001752 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001753
1754 if ((c->rex_prefix & 0x8) != 0x0)
1755 usermode = X86EMUL_MODE_PROT64;
1756 else
1757 usermode = X86EMUL_MODE_PROT32;
1758
1759 cs.dpl = 3;
1760 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001761 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001762 switch (usermode) {
1763 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001764 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001765 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001766 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001767 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001768 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001769 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001770 break;
1771 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001772 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001773 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001774 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001775 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001776 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001777 ss_sel = cs_sel + 8;
1778 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001779 cs.l = 1;
1780 break;
1781 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001782 cs_sel |= SELECTOR_RPL_MASK;
1783 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001784
Gleb Natapov79168fd2010-04-28 19:15:30 +03001785 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1786 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1787 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1788 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001789
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001790 c->eip = c->regs[VCPU_REGS_RDX];
1791 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001792
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001793 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001794}
1795
Gleb Natapov9c537242010-03-18 15:20:05 +02001796static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1797 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001798{
1799 int iopl;
1800 if (ctxt->mode == X86EMUL_MODE_REAL)
1801 return false;
1802 if (ctxt->mode == X86EMUL_MODE_VM86)
1803 return true;
1804 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001805 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001806}
1807
1808static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1809 struct x86_emulate_ops *ops,
1810 u16 port, u16 len)
1811{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001812 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001813 int r;
1814 u16 io_bitmap_ptr;
1815 u8 perm, bit_idx = port & 0x7;
1816 unsigned mask = (1 << len) - 1;
1817
Gleb Natapov79168fd2010-04-28 19:15:30 +03001818 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1819 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001820 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001821 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001822 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001823 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1824 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001825 if (r != X86EMUL_CONTINUE)
1826 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001827 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001828 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001829 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1830 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001831 if (r != X86EMUL_CONTINUE)
1832 return false;
1833 if ((perm >> bit_idx) & mask)
1834 return false;
1835 return true;
1836}
1837
1838static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1839 struct x86_emulate_ops *ops,
1840 u16 port, u16 len)
1841{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001842 if (ctxt->perm_ok)
1843 return true;
1844
Gleb Natapov9c537242010-03-18 15:20:05 +02001845 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001846 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1847 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001848
1849 ctxt->perm_ok = true;
1850
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001851 return true;
1852}
1853
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001854static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1855 struct x86_emulate_ops *ops,
1856 struct tss_segment_16 *tss)
1857{
1858 struct decode_cache *c = &ctxt->decode;
1859
1860 tss->ip = c->eip;
1861 tss->flag = ctxt->eflags;
1862 tss->ax = c->regs[VCPU_REGS_RAX];
1863 tss->cx = c->regs[VCPU_REGS_RCX];
1864 tss->dx = c->regs[VCPU_REGS_RDX];
1865 tss->bx = c->regs[VCPU_REGS_RBX];
1866 tss->sp = c->regs[VCPU_REGS_RSP];
1867 tss->bp = c->regs[VCPU_REGS_RBP];
1868 tss->si = c->regs[VCPU_REGS_RSI];
1869 tss->di = c->regs[VCPU_REGS_RDI];
1870
1871 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1872 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1873 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1874 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1875 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1876}
1877
1878static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1879 struct x86_emulate_ops *ops,
1880 struct tss_segment_16 *tss)
1881{
1882 struct decode_cache *c = &ctxt->decode;
1883 int ret;
1884
1885 c->eip = tss->ip;
1886 ctxt->eflags = tss->flag | 2;
1887 c->regs[VCPU_REGS_RAX] = tss->ax;
1888 c->regs[VCPU_REGS_RCX] = tss->cx;
1889 c->regs[VCPU_REGS_RDX] = tss->dx;
1890 c->regs[VCPU_REGS_RBX] = tss->bx;
1891 c->regs[VCPU_REGS_RSP] = tss->sp;
1892 c->regs[VCPU_REGS_RBP] = tss->bp;
1893 c->regs[VCPU_REGS_RSI] = tss->si;
1894 c->regs[VCPU_REGS_RDI] = tss->di;
1895
1896 /*
1897 * SDM says that segment selectors are loaded before segment
1898 * descriptors
1899 */
1900 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1901 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1902 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1903 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1904 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1905
1906 /*
1907 * Now load segment descriptors. If fault happenes at this stage
1908 * it is handled in a context of new task
1909 */
1910 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1911 if (ret != X86EMUL_CONTINUE)
1912 return ret;
1913 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1914 if (ret != X86EMUL_CONTINUE)
1915 return ret;
1916 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1917 if (ret != X86EMUL_CONTINUE)
1918 return ret;
1919 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1920 if (ret != X86EMUL_CONTINUE)
1921 return ret;
1922 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1923 if (ret != X86EMUL_CONTINUE)
1924 return ret;
1925
1926 return X86EMUL_CONTINUE;
1927}
1928
1929static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1930 struct x86_emulate_ops *ops,
1931 u16 tss_selector, u16 old_tss_sel,
1932 ulong old_tss_base, struct desc_struct *new_desc)
1933{
1934 struct tss_segment_16 tss_seg;
1935 int ret;
1936 u32 err, new_tss_base = get_desc_base(new_desc);
1937
1938 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1939 &err);
1940 if (ret == X86EMUL_PROPAGATE_FAULT) {
1941 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001942 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001943 return ret;
1944 }
1945
1946 save_state_to_tss16(ctxt, ops, &tss_seg);
1947
1948 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1949 &err);
1950 if (ret == X86EMUL_PROPAGATE_FAULT) {
1951 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001952 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001953 return ret;
1954 }
1955
1956 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1957 &err);
1958 if (ret == X86EMUL_PROPAGATE_FAULT) {
1959 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001960 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001961 return ret;
1962 }
1963
1964 if (old_tss_sel != 0xffff) {
1965 tss_seg.prev_task_link = old_tss_sel;
1966
1967 ret = ops->write_std(new_tss_base,
1968 &tss_seg.prev_task_link,
1969 sizeof tss_seg.prev_task_link,
1970 ctxt->vcpu, &err);
1971 if (ret == X86EMUL_PROPAGATE_FAULT) {
1972 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001973 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001974 return ret;
1975 }
1976 }
1977
1978 return load_state_from_tss16(ctxt, ops, &tss_seg);
1979}
1980
1981static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1982 struct x86_emulate_ops *ops,
1983 struct tss_segment_32 *tss)
1984{
1985 struct decode_cache *c = &ctxt->decode;
1986
1987 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1988 tss->eip = c->eip;
1989 tss->eflags = ctxt->eflags;
1990 tss->eax = c->regs[VCPU_REGS_RAX];
1991 tss->ecx = c->regs[VCPU_REGS_RCX];
1992 tss->edx = c->regs[VCPU_REGS_RDX];
1993 tss->ebx = c->regs[VCPU_REGS_RBX];
1994 tss->esp = c->regs[VCPU_REGS_RSP];
1995 tss->ebp = c->regs[VCPU_REGS_RBP];
1996 tss->esi = c->regs[VCPU_REGS_RSI];
1997 tss->edi = c->regs[VCPU_REGS_RDI];
1998
1999 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2000 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2001 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2002 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2003 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2004 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2005 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2006}
2007
2008static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2009 struct x86_emulate_ops *ops,
2010 struct tss_segment_32 *tss)
2011{
2012 struct decode_cache *c = &ctxt->decode;
2013 int ret;
2014
Gleb Natapov0f122442010-04-28 19:15:31 +03002015 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002016 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002017 return X86EMUL_PROPAGATE_FAULT;
2018 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002019 c->eip = tss->eip;
2020 ctxt->eflags = tss->eflags | 2;
2021 c->regs[VCPU_REGS_RAX] = tss->eax;
2022 c->regs[VCPU_REGS_RCX] = tss->ecx;
2023 c->regs[VCPU_REGS_RDX] = tss->edx;
2024 c->regs[VCPU_REGS_RBX] = tss->ebx;
2025 c->regs[VCPU_REGS_RSP] = tss->esp;
2026 c->regs[VCPU_REGS_RBP] = tss->ebp;
2027 c->regs[VCPU_REGS_RSI] = tss->esi;
2028 c->regs[VCPU_REGS_RDI] = tss->edi;
2029
2030 /*
2031 * SDM says that segment selectors are loaded before segment
2032 * descriptors
2033 */
2034 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2035 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2036 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2037 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2038 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2039 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2040 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2041
2042 /*
2043 * Now load segment descriptors. If fault happenes at this stage
2044 * it is handled in a context of new task
2045 */
2046 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2047 if (ret != X86EMUL_CONTINUE)
2048 return ret;
2049 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2050 if (ret != X86EMUL_CONTINUE)
2051 return ret;
2052 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2053 if (ret != X86EMUL_CONTINUE)
2054 return ret;
2055 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2056 if (ret != X86EMUL_CONTINUE)
2057 return ret;
2058 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2059 if (ret != X86EMUL_CONTINUE)
2060 return ret;
2061 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2062 if (ret != X86EMUL_CONTINUE)
2063 return ret;
2064 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2065 if (ret != X86EMUL_CONTINUE)
2066 return ret;
2067
2068 return X86EMUL_CONTINUE;
2069}
2070
2071static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2072 struct x86_emulate_ops *ops,
2073 u16 tss_selector, u16 old_tss_sel,
2074 ulong old_tss_base, struct desc_struct *new_desc)
2075{
2076 struct tss_segment_32 tss_seg;
2077 int ret;
2078 u32 err, new_tss_base = get_desc_base(new_desc);
2079
2080 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2081 &err);
2082 if (ret == X86EMUL_PROPAGATE_FAULT) {
2083 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002084 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002085 return ret;
2086 }
2087
2088 save_state_to_tss32(ctxt, ops, &tss_seg);
2089
2090 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2091 &err);
2092 if (ret == X86EMUL_PROPAGATE_FAULT) {
2093 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002094 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002095 return ret;
2096 }
2097
2098 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2099 &err);
2100 if (ret == X86EMUL_PROPAGATE_FAULT) {
2101 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002102 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002103 return ret;
2104 }
2105
2106 if (old_tss_sel != 0xffff) {
2107 tss_seg.prev_task_link = old_tss_sel;
2108
2109 ret = ops->write_std(new_tss_base,
2110 &tss_seg.prev_task_link,
2111 sizeof tss_seg.prev_task_link,
2112 ctxt->vcpu, &err);
2113 if (ret == X86EMUL_PROPAGATE_FAULT) {
2114 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002115 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002116 return ret;
2117 }
2118 }
2119
2120 return load_state_from_tss32(ctxt, ops, &tss_seg);
2121}
2122
2123static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002124 struct x86_emulate_ops *ops,
2125 u16 tss_selector, int reason,
2126 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002127{
2128 struct desc_struct curr_tss_desc, next_tss_desc;
2129 int ret;
2130 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2131 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002132 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002133 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002134
2135 /* FIXME: old_tss_base == ~0 ? */
2136
2137 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2138 if (ret != X86EMUL_CONTINUE)
2139 return ret;
2140 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2141 if (ret != X86EMUL_CONTINUE)
2142 return ret;
2143
2144 /* FIXME: check that next_tss_desc is tss */
2145
2146 if (reason != TASK_SWITCH_IRET) {
2147 if ((tss_selector & 3) > next_tss_desc.dpl ||
2148 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002149 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002150 return X86EMUL_PROPAGATE_FAULT;
2151 }
2152 }
2153
Gleb Natapovceffb452010-03-18 15:20:19 +02002154 desc_limit = desc_limit_scaled(&next_tss_desc);
2155 if (!next_tss_desc.p ||
2156 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2157 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002158 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002159 return X86EMUL_PROPAGATE_FAULT;
2160 }
2161
2162 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2163 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2164 write_segment_descriptor(ctxt, ops, old_tss_sel,
2165 &curr_tss_desc);
2166 }
2167
2168 if (reason == TASK_SWITCH_IRET)
2169 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2170
2171 /* set back link to prev task only if NT bit is set in eflags
2172 note that old_tss_sel is not used afetr this point */
2173 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2174 old_tss_sel = 0xffff;
2175
2176 if (next_tss_desc.type & 8)
2177 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2178 old_tss_base, &next_tss_desc);
2179 else
2180 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2181 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002182 if (ret != X86EMUL_CONTINUE)
2183 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002184
2185 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2186 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2187
2188 if (reason != TASK_SWITCH_IRET) {
2189 next_tss_desc.type |= (1 << 1); /* set busy flag */
2190 write_segment_descriptor(ctxt, ops, tss_selector,
2191 &next_tss_desc);
2192 }
2193
2194 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2195 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2196 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2197
Jan Kiszkae269fb22010-04-14 15:51:09 +02002198 if (has_error_code) {
2199 struct decode_cache *c = &ctxt->decode;
2200
2201 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2202 c->lock_prefix = 0;
2203 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002204 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002205 }
2206
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002207 return ret;
2208}
2209
2210int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002211 u16 tss_selector, int reason,
2212 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002213{
Avi Kivity9aabc882010-07-29 15:11:50 +03002214 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002215 struct decode_cache *c = &ctxt->decode;
2216 int rc;
2217
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002219 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002220
Jan Kiszkae269fb22010-04-14 15:51:09 +02002221 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2222 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002223
2224 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002225 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002226 if (rc == X86EMUL_CONTINUE)
2227 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002228 }
2229
Gleb Natapov19d04432010-04-15 12:29:50 +03002230 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002231}
2232
Gleb Natapova682e352010-03-18 15:20:21 +02002233static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002234 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002235{
2236 struct decode_cache *c = &ctxt->decode;
2237 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2238
Gleb Natapovd9271122010-03-18 15:20:22 +02002239 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440a2010-08-01 12:35:10 +03002240 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002241}
2242
Avi Kivity63540382010-07-29 15:11:55 +03002243static int em_push(struct x86_emulate_ctxt *ctxt)
2244{
2245 emulate_push(ctxt, ctxt->ops);
2246 return X86EMUL_CONTINUE;
2247}
2248
Avi Kivity7af04fc2010-08-18 14:16:35 +03002249static int em_das(struct x86_emulate_ctxt *ctxt)
2250{
2251 struct decode_cache *c = &ctxt->decode;
2252 u8 al, old_al;
2253 bool af, cf, old_cf;
2254
2255 cf = ctxt->eflags & X86_EFLAGS_CF;
2256 al = c->dst.val;
2257
2258 old_al = al;
2259 old_cf = cf;
2260 cf = false;
2261 af = ctxt->eflags & X86_EFLAGS_AF;
2262 if ((al & 0x0f) > 9 || af) {
2263 al -= 6;
2264 cf = old_cf | (al >= 250);
2265 af = true;
2266 } else {
2267 af = false;
2268 }
2269 if (old_al > 0x99 || old_cf) {
2270 al -= 0x60;
2271 cf = true;
2272 }
2273
2274 c->dst.val = al;
2275 /* Set PF, ZF, SF */
2276 c->src.type = OP_IMM;
2277 c->src.val = 0;
2278 c->src.bytes = 1;
2279 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2280 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2281 if (cf)
2282 ctxt->eflags |= X86_EFLAGS_CF;
2283 if (af)
2284 ctxt->eflags |= X86_EFLAGS_AF;
2285 return X86EMUL_CONTINUE;
2286}
2287
Avi Kivity0ef753b2010-08-18 14:51:45 +03002288static int em_call_far(struct x86_emulate_ctxt *ctxt)
2289{
2290 struct decode_cache *c = &ctxt->decode;
2291 u16 sel, old_cs;
2292 ulong old_eip;
2293 int rc;
2294
2295 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2296 old_eip = c->eip;
2297
2298 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2299 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2300 return X86EMUL_CONTINUE;
2301
2302 c->eip = 0;
2303 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2304
2305 c->src.val = old_cs;
2306 emulate_push(ctxt, ctxt->ops);
2307 rc = writeback(ctxt, ctxt->ops);
2308 if (rc != X86EMUL_CONTINUE)
2309 return rc;
2310
2311 c->src.val = old_eip;
2312 emulate_push(ctxt, ctxt->ops);
2313 rc = writeback(ctxt, ctxt->ops);
2314 if (rc != X86EMUL_CONTINUE)
2315 return rc;
2316
2317 c->dst.type = OP_NONE;
2318
2319 return X86EMUL_CONTINUE;
2320}
2321
Avi Kivity40ece7c2010-08-18 15:12:09 +03002322static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2323{
2324 struct decode_cache *c = &ctxt->decode;
2325 int rc;
2326
2327 c->dst.type = OP_REG;
2328 c->dst.addr.reg = &c->eip;
2329 c->dst.bytes = c->op_bytes;
2330 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2331 if (rc != X86EMUL_CONTINUE)
2332 return rc;
2333 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2334 return X86EMUL_CONTINUE;
2335}
2336
Avi Kivity5c82aa22010-08-18 18:31:43 +03002337static int em_imul(struct x86_emulate_ctxt *ctxt)
2338{
2339 struct decode_cache *c = &ctxt->decode;
2340
2341 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2342 return X86EMUL_CONTINUE;
2343}
2344
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002345static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2346{
2347 struct decode_cache *c = &ctxt->decode;
2348
2349 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002350 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002351}
2352
Avi Kivity61429142010-08-19 15:13:00 +03002353static int em_cwd(struct x86_emulate_ctxt *ctxt)
2354{
2355 struct decode_cache *c = &ctxt->decode;
2356
2357 c->dst.type = OP_REG;
2358 c->dst.bytes = c->src.bytes;
2359 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2360 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2361
2362 return X86EMUL_CONTINUE;
2363}
2364
Avi Kivity48bb5d32010-08-18 18:54:34 +03002365static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2366{
2367 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2368 struct decode_cache *c = &ctxt->decode;
2369 u64 tsc = 0;
2370
2371 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) {
2372 emulate_gp(ctxt, 0);
2373 return X86EMUL_PROPAGATE_FAULT;
2374 }
2375 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2376 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2377 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2378 return X86EMUL_CONTINUE;
2379}
2380
Avi Kivity73fba5f2010-07-29 15:11:53 +03002381#define D(_y) { .flags = (_y) }
2382#define N D(0)
2383#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2384#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2385#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2386
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002387#define D2bv(_f) D((_f) | ByteOp), D(_f)
2388#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2389
Avi Kivity6230f7f2010-08-26 18:34:55 +03002390#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2391 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2392 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2393
2394
Avi Kivity73fba5f2010-07-29 15:11:53 +03002395static struct opcode group1[] = {
2396 X7(D(Lock)), N
2397};
2398
2399static struct opcode group1A[] = {
2400 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2401};
2402
2403static struct opcode group3[] = {
2404 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2405 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002406 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002407};
2408
2409static struct opcode group4[] = {
2410 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2411 N, N, N, N, N, N,
2412};
2413
2414static struct opcode group5[] = {
2415 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002416 D(SrcMem | ModRM | Stack),
2417 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002418 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2419 D(SrcMem | ModRM | Stack), N,
2420};
2421
2422static struct group_dual group7 = { {
2423 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2424 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002425 D(SrcMem16 | ModRM | Mov | Priv),
2426 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002427}, {
2428 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2429 D(SrcNone | ModRM | DstMem | Mov), N,
2430 D(SrcMem16 | ModRM | Mov | Priv), N,
2431} };
2432
2433static struct opcode group8[] = {
2434 N, N, N, N,
2435 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2436 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2437};
2438
2439static struct group_dual group9 = { {
2440 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2441}, {
2442 N, N, N, N, N, N, N, N,
2443} };
2444
2445static struct opcode opcode_table[256] = {
2446 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002447 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002448 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2449 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002450 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002451 D(ImplicitOps | Stack | No64), N,
2452 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002453 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002454 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2455 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002456 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002457 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2458 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002459 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002460 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002461 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002462 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002463 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002464 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002465 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002466 /* 0x40 - 0x4F */
2467 X16(D(DstReg)),
2468 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002469 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002470 /* 0x58 - 0x5F */
2471 X8(D(DstReg | Stack)),
2472 /* 0x60 - 0x67 */
2473 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2474 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2475 N, N, N, N,
2476 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002477 I(SrcImm | Mov | Stack, em_push),
2478 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002479 I(SrcImmByte | Mov | Stack, em_push),
2480 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002481 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2482 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002483 /* 0x70 - 0x7F */
2484 X16(D(SrcImmByte)),
2485 /* 0x80 - 0x87 */
2486 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2487 G(DstMem | SrcImm | ModRM | Group, group1),
2488 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2489 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002490 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002491 /* 0x88 - 0x8F */
Avi Kivity76e8e682010-08-26 11:56:09 +03002492 D2bv(DstMem | SrcReg | ModRM | Mov),
2493 D2bv(DstReg | SrcMem | ModRM | Mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002494 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002495 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2496 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002497 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002498 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002499 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002500 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002501 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2502 /* 0xA0 - 0xA7 */
Avi Kivity50748612010-08-26 11:56:10 +03002503 D2bv(DstAcc | SrcMem | Mov | MemAbs),
2504 D2bv(DstMem | SrcAcc | Mov | MemAbs),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002505 D2bv(SrcSI | DstDI | Mov | String), D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002506 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002507 D2bv(DstAcc | SrcImm),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002508 D2bv(SrcAcc | DstDI | Mov | String),
2509 D2bv(SrcSI | DstAcc | Mov | String),
2510 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002511 /* 0xB0 - 0xB7 */
2512 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2513 /* 0xB8 - 0xBF */
2514 X8(D(DstReg | SrcImm | Mov)),
2515 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002516 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002517 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2518 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002519 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002520 D2bv(DstMem | SrcImm | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002521 /* 0xC8 - 0xCF */
2522 N, N, N, D(ImplicitOps | Stack),
2523 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2524 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002525 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002526 N, N, N, N,
2527 /* 0xD8 - 0xDF */
2528 N, N, N, N, N, N, N, N,
2529 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002530 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002531 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002532 /* 0xE8 - 0xEF */
2533 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2534 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002535 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002536 /* 0xF0 - 0xF7 */
2537 N, N, N, N,
2538 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2539 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002540 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002541 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2542};
2543
2544static struct opcode twobyte_table[256] = {
2545 /* 0x00 - 0x0F */
2546 N, GD(0, &group7), N, N,
2547 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2548 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2549 N, D(ImplicitOps | ModRM), N, N,
2550 /* 0x10 - 0x1F */
2551 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2552 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002553 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2554 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002555 N, N, N, N,
2556 N, N, N, N, N, N, N, N,
2557 /* 0x30 - 0x3F */
Avi Kivity48bb5d32010-08-18 18:54:34 +03002558 D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc),
2559 D(ImplicitOps | Priv), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002560 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2561 N, N, N, N, N, N, N, N,
2562 /* 0x40 - 0x4F */
2563 X16(D(DstReg | SrcMem | ModRM | Mov)),
2564 /* 0x50 - 0x5F */
2565 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2566 /* 0x60 - 0x6F */
2567 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2568 /* 0x70 - 0x7F */
2569 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2570 /* 0x80 - 0x8F */
2571 X16(D(SrcImm)),
2572 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002573 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002574 /* 0xA0 - 0xA7 */
2575 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2576 N, D(DstMem | SrcReg | ModRM | BitOp),
2577 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2578 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2579 /* 0xA8 - 0xAF */
2580 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2581 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2582 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2583 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002584 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002585 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002586 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002587 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2588 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2589 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002590 /* 0xB8 - 0xBF */
2591 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002592 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002593 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2594 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002595 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002596 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002597 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002598 N, N, N, GD(0, &group9),
2599 N, N, N, N, N, N, N, N,
2600 /* 0xD0 - 0xDF */
2601 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2602 /* 0xE0 - 0xEF */
2603 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2604 /* 0xF0 - 0xFF */
2605 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2606};
2607
2608#undef D
2609#undef N
2610#undef G
2611#undef GD
2612#undef I
2613
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002614#undef D2bv
2615#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002616#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002617
Avi Kivity39f21ee2010-08-18 19:20:21 +03002618static unsigned imm_size(struct decode_cache *c)
2619{
2620 unsigned size;
2621
2622 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2623 if (size == 8)
2624 size = 4;
2625 return size;
2626}
2627
2628static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2629 unsigned size, bool sign_extension)
2630{
2631 struct decode_cache *c = &ctxt->decode;
2632 struct x86_emulate_ops *ops = ctxt->ops;
2633 int rc = X86EMUL_CONTINUE;
2634
2635 op->type = OP_IMM;
2636 op->bytes = size;
2637 op->addr.mem = c->eip;
2638 /* NB. Immediates are sign-extended as necessary. */
2639 switch (op->bytes) {
2640 case 1:
2641 op->val = insn_fetch(s8, 1, c->eip);
2642 break;
2643 case 2:
2644 op->val = insn_fetch(s16, 2, c->eip);
2645 break;
2646 case 4:
2647 op->val = insn_fetch(s32, 4, c->eip);
2648 break;
2649 }
2650 if (!sign_extension) {
2651 switch (op->bytes) {
2652 case 1:
2653 op->val &= 0xff;
2654 break;
2655 case 2:
2656 op->val &= 0xffff;
2657 break;
2658 case 4:
2659 op->val &= 0xffffffff;
2660 break;
2661 }
2662 }
2663done:
2664 return rc;
2665}
2666
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002667int
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002668x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2669{
2670 struct x86_emulate_ops *ops = ctxt->ops;
2671 struct decode_cache *c = &ctxt->decode;
2672 int rc = X86EMUL_CONTINUE;
2673 int mode = ctxt->mode;
2674 int def_op_bytes, def_ad_bytes, dual, goffset;
2675 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002676 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002677
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002678 c->eip = ctxt->eip;
2679 c->fetch.start = c->fetch.end = c->eip;
2680 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2681
2682 switch (mode) {
2683 case X86EMUL_MODE_REAL:
2684 case X86EMUL_MODE_VM86:
2685 case X86EMUL_MODE_PROT16:
2686 def_op_bytes = def_ad_bytes = 2;
2687 break;
2688 case X86EMUL_MODE_PROT32:
2689 def_op_bytes = def_ad_bytes = 4;
2690 break;
2691#ifdef CONFIG_X86_64
2692 case X86EMUL_MODE_PROT64:
2693 def_op_bytes = 4;
2694 def_ad_bytes = 8;
2695 break;
2696#endif
2697 default:
2698 return -1;
2699 }
2700
2701 c->op_bytes = def_op_bytes;
2702 c->ad_bytes = def_ad_bytes;
2703
2704 /* Legacy prefixes. */
2705 for (;;) {
2706 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2707 case 0x66: /* operand-size override */
2708 /* switch between 2/4 bytes */
2709 c->op_bytes = def_op_bytes ^ 6;
2710 break;
2711 case 0x67: /* address-size override */
2712 if (mode == X86EMUL_MODE_PROT64)
2713 /* switch between 4/8 bytes */
2714 c->ad_bytes = def_ad_bytes ^ 12;
2715 else
2716 /* switch between 2/4 bytes */
2717 c->ad_bytes = def_ad_bytes ^ 6;
2718 break;
2719 case 0x26: /* ES override */
2720 case 0x2e: /* CS override */
2721 case 0x36: /* SS override */
2722 case 0x3e: /* DS override */
2723 set_seg_override(c, (c->b >> 3) & 3);
2724 break;
2725 case 0x64: /* FS override */
2726 case 0x65: /* GS override */
2727 set_seg_override(c, c->b & 7);
2728 break;
2729 case 0x40 ... 0x4f: /* REX */
2730 if (mode != X86EMUL_MODE_PROT64)
2731 goto done_prefixes;
2732 c->rex_prefix = c->b;
2733 continue;
2734 case 0xf0: /* LOCK */
2735 c->lock_prefix = 1;
2736 break;
2737 case 0xf2: /* REPNE/REPNZ */
2738 c->rep_prefix = REPNE_PREFIX;
2739 break;
2740 case 0xf3: /* REP/REPE/REPZ */
2741 c->rep_prefix = REPE_PREFIX;
2742 break;
2743 default:
2744 goto done_prefixes;
2745 }
2746
2747 /* Any legacy prefix after a REX prefix nullifies its effect. */
2748
2749 c->rex_prefix = 0;
2750 }
2751
2752done_prefixes:
2753
2754 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002755 if (c->rex_prefix & 8)
2756 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002757
2758 /* Opcode byte(s). */
2759 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002760 /* Two-byte opcode? */
2761 if (c->b == 0x0f) {
2762 c->twobyte = 1;
2763 c->b = insn_fetch(u8, 1, c->eip);
2764 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002765 }
2766 c->d = opcode.flags;
2767
2768 if (c->d & Group) {
2769 dual = c->d & GroupDual;
2770 c->modrm = insn_fetch(u8, 1, c->eip);
2771 --c->eip;
2772
2773 if (c->d & GroupDual) {
2774 g_mod012 = opcode.u.gdual->mod012;
2775 g_mod3 = opcode.u.gdual->mod3;
2776 } else
2777 g_mod012 = g_mod3 = opcode.u.group;
2778
2779 c->d &= ~(Group | GroupDual);
2780
2781 goffset = (c->modrm >> 3) & 7;
2782
2783 if ((c->modrm >> 6) == 3)
2784 opcode = g_mod3[goffset];
2785 else
2786 opcode = g_mod012[goffset];
2787 c->d |= opcode.flags;
2788 }
2789
2790 c->execute = opcode.u.execute;
2791
2792 /* Unrecognised? */
2793 if (c->d == 0 || (c->d & Undefined)) {
2794 DPRINTF("Cannot emulate %02x\n", c->b);
2795 return -1;
2796 }
2797
2798 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2799 c->op_bytes = 8;
2800
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002801 if (c->d & Op3264) {
2802 if (mode == X86EMUL_MODE_PROT64)
2803 c->op_bytes = 8;
2804 else
2805 c->op_bytes = 4;
2806 }
2807
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002808 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002809 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002810 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002811 if (!c->has_seg_override)
2812 set_seg_override(c, c->modrm_seg);
2813 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002814 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002815 if (rc != X86EMUL_CONTINUE)
2816 goto done;
2817
2818 if (!c->has_seg_override)
2819 set_seg_override(c, VCPU_SREG_DS);
2820
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002821 if (memop.type == OP_MEM && !(!c->twobyte && c->b == 0x8d))
2822 memop.addr.mem += seg_override_base(ctxt, ops, c);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002823
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002824 if (memop.type == OP_MEM && c->ad_bytes != 8)
2825 memop.addr.mem = (u32)memop.addr.mem;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002826
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002827 if (memop.type == OP_MEM && c->rip_relative)
2828 memop.addr.mem += c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002829
2830 /*
2831 * Decode and fetch the source operand: register, memory
2832 * or immediate.
2833 */
2834 switch (c->d & SrcMask) {
2835 case SrcNone:
2836 break;
2837 case SrcReg:
2838 decode_register_operand(&c->src, c, 0);
2839 break;
2840 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002841 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002842 goto srcmem_common;
2843 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002844 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002845 goto srcmem_common;
2846 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002847 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002848 c->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002849 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002850 c->src = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002851 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002852 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002853 rc = decode_imm(ctxt, &c->src, 2, false);
2854 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002855 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002856 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2857 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002858 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002859 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002860 break;
2861 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002862 rc = decode_imm(ctxt, &c->src, 1, true);
2863 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002864 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002865 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002866 break;
2867 case SrcAcc:
2868 c->src.type = OP_REG;
2869 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002870 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002871 fetch_register_operand(&c->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002872 break;
2873 case SrcOne:
2874 c->src.bytes = 1;
2875 c->src.val = 1;
2876 break;
2877 case SrcSI:
2878 c->src.type = OP_MEM;
2879 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002880 c->src.addr.mem =
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002881 register_address(c, seg_override_base(ctxt, ops, c),
2882 c->regs[VCPU_REGS_RSI]);
2883 c->src.val = 0;
2884 break;
2885 case SrcImmFAddr:
2886 c->src.type = OP_IMM;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002887 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002888 c->src.bytes = c->op_bytes + 2;
2889 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2890 break;
2891 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002892 memop.bytes = c->op_bytes + 2;
2893 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002894 break;
2895 }
2896
Avi Kivity39f21ee2010-08-18 19:20:21 +03002897 if (rc != X86EMUL_CONTINUE)
2898 goto done;
2899
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002900 /*
2901 * Decode and fetch the second source operand: register, memory
2902 * or immediate.
2903 */
2904 switch (c->d & Src2Mask) {
2905 case Src2None:
2906 break;
2907 case Src2CL:
2908 c->src2.bytes = 1;
2909 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2910 break;
2911 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002912 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002913 break;
2914 case Src2One:
2915 c->src2.bytes = 1;
2916 c->src2.val = 1;
2917 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03002918 case Src2Imm:
2919 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
2920 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002921 }
2922
Avi Kivity39f21ee2010-08-18 19:20:21 +03002923 if (rc != X86EMUL_CONTINUE)
2924 goto done;
2925
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002926 /* Decode and fetch the destination operand: register or memory. */
2927 switch (c->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002928 case DstReg:
2929 decode_register_operand(&c->dst, c,
2930 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2931 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08002932 case DstImmUByte:
2933 c->dst.type = OP_IMM;
2934 c->dst.addr.mem = c->eip;
2935 c->dst.bytes = 1;
2936 c->dst.val = insn_fetch(u8, 1, c->eip);
2937 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002938 case DstMem:
2939 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002940 c->dst = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002941 if ((c->d & DstMask) == DstMem64)
2942 c->dst.bytes = 8;
2943 else
2944 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002945 if (c->d & BitOp)
2946 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002947 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002948 break;
2949 case DstAcc:
2950 c->dst.type = OP_REG;
2951 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002952 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002953 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002954 c->dst.orig_val = c->dst.val;
2955 break;
2956 case DstDI:
2957 c->dst.type = OP_MEM;
2958 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002959 c->dst.addr.mem =
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002960 register_address(c, es_base(ctxt, ops),
2961 c->regs[VCPU_REGS_RDI]);
2962 c->dst.val = 0;
2963 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002964 case ImplicitOps:
2965 /* Special instructions do their own operand decoding. */
2966 default:
2967 c->dst.type = OP_NONE; /* Disable writeback. */
2968 return 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002969 }
2970
2971done:
2972 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2973}
2974
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03002975static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
2976{
2977 struct decode_cache *c = &ctxt->decode;
2978
2979 /* The second termination condition only applies for REPE
2980 * and REPNE. Test if the repeat string operation prefix is
2981 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2982 * corresponding termination condition according to:
2983 * - if REPE/REPZ and ZF = 0 then done
2984 * - if REPNE/REPNZ and ZF = 1 then done
2985 */
2986 if (((c->b == 0xa6) || (c->b == 0xa7) ||
2987 (c->b == 0xae) || (c->b == 0xaf))
2988 && (((c->rep_prefix == REPE_PREFIX) &&
2989 ((ctxt->eflags & EFLG_ZF) == 0))
2990 || ((c->rep_prefix == REPNE_PREFIX) &&
2991 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
2992 return true;
2993
2994 return false;
2995}
2996
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002997int
Avi Kivity9aabc882010-07-29 15:11:50 +03002998x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002999{
Avi Kivity9aabc882010-07-29 15:11:50 +03003000 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003001 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003002 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003003 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003004 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003005 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003006
Gleb Natapov9de41572010-04-28 19:15:22 +03003007 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003008
Gleb Natapov11616242010-02-11 14:43:14 +02003009 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003010 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003011 goto done;
3012 }
3013
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003014 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003015 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003016 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003017 goto done;
3018 }
3019
Avi Kivity081bca02010-08-26 11:06:15 +03003020 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
3021 emulate_ud(ctxt);
3022 goto done;
3023 }
3024
Gleb Natapove92805a2010-02-10 14:21:35 +02003025 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003026 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003027 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003028 goto done;
3029 }
3030
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003031 if (c->rep_prefix && (c->d & String)) {
3032 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003033 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003034 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003035 goto done;
3036 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003037 }
3038
Wei Yongjunc483c022010-08-06 15:36:36 +08003039 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity1a6440a2010-08-01 12:35:10 +03003040 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03003041 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003042 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003043 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003044 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003045 }
3046
Gleb Natapove35b7b92010-02-25 16:36:42 +02003047 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440a2010-08-01 12:35:10 +03003048 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03003049 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003050 if (rc != X86EMUL_CONTINUE)
3051 goto done;
3052 }
3053
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003054 if ((c->d & DstMask) == ImplicitOps)
3055 goto special_insn;
3056
3057
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003058 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3059 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440a2010-08-01 12:35:10 +03003060 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03003061 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003062 if (rc != X86EMUL_CONTINUE)
3063 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003064 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003065 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003066
Avi Kivity018a98d2007-11-27 19:30:56 +02003067special_insn:
3068
Avi Kivityef65c882010-07-29 15:11:51 +03003069 if (c->execute) {
3070 rc = c->execute(ctxt);
3071 if (rc != X86EMUL_CONTINUE)
3072 goto done;
3073 goto writeback;
3074 }
3075
Laurent Viviere4e03de2007-09-18 11:52:50 +02003076 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 goto twobyte_insn;
3078
Laurent Viviere4e03de2007-09-18 11:52:50 +02003079 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 case 0x00 ... 0x05:
3081 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003082 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003084 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003085 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003086 break;
3087 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003088 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003089 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003090 goto done;
3091 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 case 0x08 ... 0x0d:
3093 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003094 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003096 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003097 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003098 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 case 0x10 ... 0x15:
3100 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003101 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003103 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003104 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003105 break;
3106 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003107 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003108 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003109 goto done;
3110 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 case 0x18 ... 0x1d:
3112 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003113 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003115 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003116 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003117 break;
3118 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003119 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003120 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003121 goto done;
3122 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003123 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003125 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126 break;
3127 case 0x28 ... 0x2d:
3128 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003129 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130 break;
3131 case 0x30 ... 0x35:
3132 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003133 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 break;
3135 case 0x38 ... 0x3d:
3136 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003137 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003139 case 0x40 ... 0x47: /* inc r16/r32 */
3140 emulate_1op("inc", c->dst, ctxt->eflags);
3141 break;
3142 case 0x48 ... 0x4f: /* dec r16/r32 */
3143 emulate_1op("dec", c->dst, ctxt->eflags);
3144 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003145 case 0x58 ... 0x5f: /* pop reg */
3146 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003147 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003148 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02003149 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02003150 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003151 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003152 rc = emulate_pusha(ctxt, ops);
3153 if (rc != X86EMUL_CONTINUE)
3154 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003155 break;
3156 case 0x61: /* popa */
3157 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003158 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003159 goto done;
3160 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003162 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003164 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003166 case 0x6c: /* insb */
3167 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003168 c->src.val = c->regs[VCPU_REGS_RDX];
3169 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003170 case 0x6e: /* outsb */
3171 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003172 c->dst.val = c->regs[VCPU_REGS_RDX];
3173 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003174 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003175 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003176 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003177 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003178 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003180 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 case 0:
3182 goto add;
3183 case 1:
3184 goto or;
3185 case 2:
3186 goto adc;
3187 case 3:
3188 goto sbb;
3189 case 4:
3190 goto and;
3191 case 5:
3192 goto sub;
3193 case 6:
3194 goto xor;
3195 case 7:
3196 goto cmp;
3197 }
3198 break;
3199 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003200 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003201 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 break;
3203 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003204 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003206 c->src.val = c->dst.val;
3207 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 /*
3209 * Write back the memory destination with implicit LOCK
3210 * prefix.
3211 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003212 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003213 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03003216 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003217 case 0x8c: /* mov r/m, sreg */
3218 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003219 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003220 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003221 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003222 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003223 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003224 case 0x8d: /* lea r16/r32, m */
Avi Kivity342fc632010-08-01 15:13:22 +03003225 c->dst.val = c->src.addr.mem;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003226 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003227 case 0x8e: { /* mov seg, r/m16 */
3228 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003229
3230 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003231
Gleb Natapovc6975182010-02-18 12:15:01 +02003232 if (c->modrm_reg == VCPU_SREG_CS ||
3233 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003234 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003235 goto done;
3236 }
3237
Glauber Costa310b5d32009-05-12 16:21:06 -04003238 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003239 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003240
Gleb Natapov2e873022010-03-18 15:20:18 +02003241 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003242
3243 c->dst.type = OP_NONE; /* Disable writeback. */
3244 break;
3245 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003247 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003248 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003251 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3252 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003253 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003254 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003255 case 0x98: /* cbw/cwde/cdqe */
3256 switch (c->op_bytes) {
3257 case 2: c->dst.val = (s8)c->dst.val; break;
3258 case 4: c->dst.val = (s16)c->dst.val; break;
3259 case 8: c->dst.val = (s32)c->dst.val; break;
3260 }
3261 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003262 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003263 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003264 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003265 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003266 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003267 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003268 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003269 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003270 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
3271 if (rc != X86EMUL_CONTINUE)
3272 goto done;
3273 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08003274 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02003276 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003278 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440a2010-08-01 12:35:10 +03003279 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02003280 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003281 case 0xa8 ... 0xa9: /* test ax, imm */
3282 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 case 0xaa ... 0xab: /* stos */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02003285 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003286 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003287 goto cmp;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03003288 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02003289 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02003290 case 0xc0 ... 0xc1:
3291 emulate_grp2(ctxt);
3292 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003293 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003294 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003295 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003296 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003297 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003298 case 0xc4: /* les */
3299 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
3300 if (rc != X86EMUL_CONTINUE)
3301 goto done;
3302 break;
3303 case 0xc5: /* lds */
3304 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
3305 if (rc != X86EMUL_CONTINUE)
3306 goto done;
3307 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003308 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
3309 mov:
3310 c->dst.val = c->src.val;
3311 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003312 case 0xcb: /* ret far */
3313 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003314 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003315 goto done;
3316 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003317 case 0xcc: /* int3 */
3318 irq = 3;
3319 goto do_interrupt;
3320 case 0xcd: /* int n */
3321 irq = c->src.val;
3322 do_interrupt:
3323 rc = emulate_int(ctxt, ops, irq);
3324 if (rc != X86EMUL_CONTINUE)
3325 goto done;
3326 break;
3327 case 0xce: /* into */
3328 if (ctxt->eflags & EFLG_OF) {
3329 irq = 4;
3330 goto do_interrupt;
3331 }
3332 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003333 case 0xcf: /* iret */
3334 rc = emulate_iret(ctxt, ops);
3335
3336 if (rc != X86EMUL_CONTINUE)
3337 goto done;
3338 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003339 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003340 emulate_grp2(ctxt);
3341 break;
3342 case 0xd2 ... 0xd3: /* Grp2 */
3343 c->src.val = c->regs[VCPU_REGS_RCX];
3344 emulate_grp2(ctxt);
3345 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003346 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3347 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3348 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3349 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3350 jmp_rel(c, c->src.val);
3351 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003352 case 0xe3: /* jcxz/jecxz/jrcxz */
3353 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3354 jmp_rel(c, c->src.val);
3355 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003356 case 0xe4: /* inb */
3357 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003358 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003359 case 0xe6: /* outb */
3360 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003361 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003362 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003363 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003364 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003365 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003366 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003367 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003368 }
3369 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003370 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003371 case 0xea: { /* jmp far */
3372 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02003373 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003374 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3375
3376 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003377 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003378
Gleb Natapov414e6272010-04-28 19:15:26 +03003379 c->eip = 0;
3380 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003381 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003382 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003383 case 0xeb:
3384 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003385 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003386 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003387 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003388 case 0xec: /* in al,dx */
3389 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003390 c->src.val = c->regs[VCPU_REGS_RDX];
3391 do_io_in:
3392 c->dst.bytes = min(c->dst.bytes, 4u);
3393 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003394 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003395 goto done;
3396 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003397 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3398 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003399 goto done; /* IO is needed */
3400 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003401 case 0xee: /* out dx,al */
3402 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003403 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003404 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003405 c->src.bytes = min(c->src.bytes, 4u);
3406 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3407 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003408 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003409 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003410 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003411 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3412 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003413 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003414 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003415 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003416 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003417 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003418 case 0xf5: /* cmc */
3419 /* complement carry flag from eflags reg */
3420 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003421 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003422 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003423 rc = emulate_grp3(ctxt, ops);
3424 if (rc != X86EMUL_CONTINUE)
3425 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003426 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003427 case 0xf8: /* clc */
3428 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003429 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003430 case 0xf9: /* stc */
3431 ctxt->eflags |= EFLG_CF;
3432 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003433 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003434 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003435 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003436 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003437 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003438 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003439 break;
3440 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003441 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003442 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003443 goto done;
3444 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003445 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003446 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003447 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003448 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003449 case 0xfc: /* cld */
3450 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003451 break;
3452 case 0xfd: /* std */
3453 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003454 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003455 case 0xfe: /* Grp4 */
3456 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003457 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003458 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003459 goto done;
3460 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003461 case 0xff: /* Grp5 */
3462 if (c->modrm_reg == 5)
3463 goto jump_far;
3464 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003465 default:
3466 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003468
3469writeback:
3470 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003471 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003472 goto done;
3473
Gleb Natapov5cd21912010-03-18 15:20:26 +02003474 /*
3475 * restore dst type in case the decoding will be reused
3476 * (happens for string instruction )
3477 */
3478 c->dst.type = saved_dst_type;
3479
Gleb Natapova682e352010-03-18 15:20:21 +02003480 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003481 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3482 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003483
3484 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003485 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3486 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003487
Gleb Natapov5cd21912010-03-18 15:20:26 +02003488 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003489 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003490 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003491
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003492 if (!string_insn_completed(ctxt)) {
3493 /*
3494 * Re-enter guest when pio read ahead buffer is empty
3495 * or, if it is not used, after each 1024 iteration.
3496 */
3497 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3498 (r->end == 0 || r->end != r->pos)) {
3499 /*
3500 * Reset read cache. Usually happens before
3501 * decode, but since instruction is restarted
3502 * we have to do it here.
3503 */
3504 ctxt->decode.mem_read.end = 0;
3505 return EMULATION_RESTART;
3506 }
3507 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003508 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003509 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003510
3511 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003512
3513done:
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003514 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515
3516twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003517 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003519 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520 u16 size;
3521 unsigned long address;
3522
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003523 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003524 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003525 goto cannot_emulate;
3526
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003527 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003528 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003529 goto done;
3530
Avi Kivity33e38852008-05-21 15:34:25 +03003531 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003532 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003533 /* Disable writeback. */
3534 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003535 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 case 2: /* lgdt */
Avi Kivity1a6440a2010-08-01 12:35:10 +03003537 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003538 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003539 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 goto done;
3541 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003542 /* Disable writeback. */
3543 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003545 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003546 if (c->modrm_mod == 3) {
3547 switch (c->modrm_rm) {
3548 case 1:
3549 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003550 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003551 goto done;
3552 break;
3553 default:
3554 goto cannot_emulate;
3555 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003556 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +03003557 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003558 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003559 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003560 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003561 goto done;
3562 realmode_lidt(ctxt->vcpu, size, address);
3563 }
Avi Kivity16286d02008-04-14 14:40:50 +03003564 /* Disable writeback. */
3565 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003566 break;
3567 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003568 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003569 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003570 break;
3571 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003572 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003573 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003574 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003576 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003577 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003578 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003579 case 7: /* invlpg*/
Avi Kivity1f6f0582010-08-01 15:19:22 +03003580 emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
Avi Kivity16286d02008-04-14 14:40:50 +03003581 /* Disable writeback. */
3582 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 break;
3584 default:
3585 goto cannot_emulate;
3586 }
3587 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003588 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003589 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003590 if (rc != X86EMUL_CONTINUE)
3591 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003592 else
3593 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003594 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003595 case 0x06:
3596 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003597 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003598 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003599 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003600 break;
3601 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003602 case 0x0d: /* GrpP (prefetch) */
3603 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003604 break;
3605 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003606 switch (c->modrm_reg) {
3607 case 1:
3608 case 5 ... 7:
3609 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003610 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003611 goto done;
3612 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003613 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003614 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003616 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3617 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003618 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003619 goto done;
3620 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003621 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003622 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003623 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003624 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003625 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003626 goto done;
3627 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003628 c->dst.type = OP_NONE;
3629 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003631 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3632 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003633 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003634 goto done;
3635 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003636
Avi Kivityb27f3852010-08-01 14:25:22 +03003637 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003638 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3639 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3640 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003641 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003642 goto done;
3643 }
3644
Laurent Viviera01af5e2007-09-24 11:10:56 +02003645 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003647 case 0x30:
3648 /* wrmsr */
3649 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3650 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003651 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003652 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003653 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003654 }
3655 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003656 break;
3657 case 0x32:
3658 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003659 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003660 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003661 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003662 } else {
3663 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3664 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3665 }
3666 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003667 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003668 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003669 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003670 if (rc != X86EMUL_CONTINUE)
3671 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003672 else
3673 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003674 break;
3675 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003676 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003677 if (rc != X86EMUL_CONTINUE)
3678 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003679 else
3680 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003681 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003682 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003683 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003684 if (!test_cc(c->b, ctxt->eflags))
3685 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003687 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003688 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003689 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003690 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003691 case 0x90 ... 0x9f: /* setcc r/m8 */
3692 c->dst.val = test_cc(c->b, ctxt->eflags);
3693 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003694 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003695 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003696 break;
3697 case 0xa1: /* pop fs */
3698 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003699 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003700 goto done;
3701 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003702 case 0xa3:
3703 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003704 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003705 /* only subword offset */
3706 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003707 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003708 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003709 case 0xa4: /* shld imm8, r, r/m */
3710 case 0xa5: /* shld cl, r, r/m */
3711 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3712 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003713 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003714 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003715 break;
3716 case 0xa9: /* pop gs */
3717 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003718 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003719 goto done;
3720 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003721 case 0xab:
3722 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003723 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003724 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003725 case 0xac: /* shrd imm8, r, r/m */
3726 case 0xad: /* shrd cl, r, r/m */
3727 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3728 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003729 case 0xae: /* clflush */
3730 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 case 0xb0 ... 0xb1: /* cmpxchg */
3732 /*
3733 * Save real source value, then compare EAX against
3734 * destination.
3735 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003736 c->src.orig_val = c->src.val;
3737 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003738 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3739 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003740 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003741 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742 } else {
3743 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003744 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003745 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746 }
3747 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003748 case 0xb2: /* lss */
3749 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
3750 if (rc != X86EMUL_CONTINUE)
3751 goto done;
3752 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003753 case 0xb3:
3754 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003755 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003757 case 0xb4: /* lfs */
3758 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
3759 if (rc != X86EMUL_CONTINUE)
3760 goto done;
3761 break;
3762 case 0xb5: /* lgs */
3763 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
3764 if (rc != X86EMUL_CONTINUE)
3765 goto done;
3766 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003767 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003768 c->dst.bytes = c->op_bytes;
3769 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3770 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003773 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774 case 0:
3775 goto bt;
3776 case 1:
3777 goto bts;
3778 case 2:
3779 goto btr;
3780 case 3:
3781 goto btc;
3782 }
3783 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003784 case 0xbb:
3785 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003786 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003787 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003788 case 0xbc: { /* bsf */
3789 u8 zf;
3790 __asm__ ("bsf %2, %0; setz %1"
3791 : "=r"(c->dst.val), "=q"(zf)
3792 : "r"(c->src.val));
3793 ctxt->eflags &= ~X86_EFLAGS_ZF;
3794 if (zf) {
3795 ctxt->eflags |= X86_EFLAGS_ZF;
3796 c->dst.type = OP_NONE; /* Disable writeback. */
3797 }
3798 break;
3799 }
3800 case 0xbd: { /* bsr */
3801 u8 zf;
3802 __asm__ ("bsr %2, %0; setz %1"
3803 : "=r"(c->dst.val), "=q"(zf)
3804 : "r"(c->src.val));
3805 ctxt->eflags &= ~X86_EFLAGS_ZF;
3806 if (zf) {
3807 ctxt->eflags |= X86_EFLAGS_ZF;
3808 c->dst.type = OP_NONE; /* Disable writeback. */
3809 }
3810 break;
3811 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003813 c->dst.bytes = c->op_bytes;
3814 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3815 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003817 case 0xc0 ... 0xc1: /* xadd */
3818 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3819 /* Write back the register source. */
3820 c->src.val = c->dst.orig_val;
3821 write_register_operand(&c->src);
3822 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003823 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003824 c->dst.bytes = c->op_bytes;
3825 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3826 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003827 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003828 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003829 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003830 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003831 goto done;
3832 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003833 default:
3834 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 }
3836 goto writeback;
3837
3838cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003839 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003840 return -1;
3841}