blob: 36534ecaf5966816c553911d1a396f53815a1922 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Avi Kivityab85b12b2010-07-29 15:11:49 +030050#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080051/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define SrcNone (0<<4) /* No source operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcReg (1<<4) /* Register operand. */
54#define SrcMem (2<<4) /* Memory operand. */
55#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
57#define SrcImm (5<<4) /* Immediate operand. */
58#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010059#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030060#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030061#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020062#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030063#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
64#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080065#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030066#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020076#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030078/* Misc flags */
Avi Kivity5a506b12010-08-01 15:10:29 +030079#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030080#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030081#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020082#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020083#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030084#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010085/* Source 2 operand type */
86#define Src2None (0<<29)
87#define Src2CL (1<<29)
88#define Src2ImmByte (2<<29)
89#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030090#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010091#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
Avi Kivityd0e53322010-07-29 15:11:54 +030093#define X2(x...) x, x
94#define X3(x...) X2(x), x
95#define X4(x...) X2(x), X2(x)
96#define X5(x...) X4(x), x
97#define X6(x...) X4(x), X2(x)
98#define X7(x...) X4(x), X3(x)
99#define X8(x...) X4(x), X4(x)
100#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300101
Avi Kivityd65b1de2010-07-29 15:11:35 +0300102struct opcode {
103 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300104 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300105 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300106 struct opcode *group;
107 struct group_dual *gdual;
108 } u;
109};
110
111struct group_dual {
112 struct opcode mod012[8];
113 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300114};
115
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200117#define EFLG_ID (1<<21)
118#define EFLG_VIP (1<<20)
119#define EFLG_VIF (1<<19)
120#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200121#define EFLG_VM (1<<17)
122#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200123#define EFLG_IOPL (3<<12)
124#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125#define EFLG_OF (1<<11)
126#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200127#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200128#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129#define EFLG_SF (1<<7)
130#define EFLG_ZF (1<<6)
131#define EFLG_AF (1<<4)
132#define EFLG_PF (1<<2)
133#define EFLG_CF (1<<0)
134
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300135#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
136#define EFLG_RESERVED_ONE_MASK 2
137
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138/*
139 * Instruction emulation:
140 * Most instructions are emulated directly via a fragment of inline assembly
141 * code. This allows us to save/restore EFLAGS and thus very easily pick up
142 * any modified flags.
143 */
144
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800145#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146#define _LO32 "k" /* force 32-bit operand */
147#define _STK "%%rsp" /* stack pointer */
148#elif defined(__i386__)
149#define _LO32 "" /* force 32-bit operand */
150#define _STK "%%esp" /* stack pointer */
151#endif
152
153/*
154 * These EFLAGS bits are restored from saved value during emulation, and
155 * any changes are written back to the saved value after emulation.
156 */
157#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
158
159/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200160#define _PRE_EFLAGS(_sav, _msk, _tmp) \
161 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
162 "movl %"_sav",%"_LO32 _tmp"; " \
163 "push %"_tmp"; " \
164 "push %"_tmp"; " \
165 "movl %"_msk",%"_LO32 _tmp"; " \
166 "andl %"_LO32 _tmp",("_STK"); " \
167 "pushf; " \
168 "notl %"_LO32 _tmp"; " \
169 "andl %"_LO32 _tmp",("_STK"); " \
170 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
171 "pop %"_tmp"; " \
172 "orl %"_LO32 _tmp",("_STK"); " \
173 "popf; " \
174 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175
176/* After executing instruction: write-back necessary bits in EFLAGS. */
177#define _POST_EFLAGS(_sav, _msk, _tmp) \
178 /* _sav |= EFLAGS & _msk; */ \
179 "pushf; " \
180 "pop %"_tmp"; " \
181 "andl %"_msk",%"_LO32 _tmp"; " \
182 "orl %"_LO32 _tmp",%"_sav"; "
183
Avi Kivitydda96d82008-11-26 15:14:10 +0200184#ifdef CONFIG_X86_64
185#define ON64(x) x
186#else
187#define ON64(x)
188#endif
189
Avi Kivityb3b3d252010-08-16 17:49:52 +0300190#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200191 do { \
192 __asm__ __volatile__ ( \
193 _PRE_EFLAGS("0", "4", "2") \
194 _op _suffix " %"_x"3,%1; " \
195 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300196 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200197 "=&r" (_tmp) \
198 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200199 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200200
201
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202/* Raw emulation: instruction has two explicit operands. */
203#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200204 do { \
205 unsigned long _tmp; \
206 \
207 switch ((_dst).bytes) { \
208 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300209 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200210 break; \
211 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300212 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200213 break; \
214 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300215 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200216 break; \
217 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218 } while (0)
219
220#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
221 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200222 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400223 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300225 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 break; \
227 default: \
228 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
229 _wx, _wy, _lx, _ly, _qx, _qy); \
230 break; \
231 } \
232 } while (0)
233
234/* Source operand is byte-sized and may be restricted to just %cl. */
235#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
236 __emulate_2op(_op, _src, _dst, _eflags, \
237 "b", "c", "b", "c", "b", "c", "b", "c")
238
239/* Source operand is byte, word, long or quad sized. */
240#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
241 __emulate_2op(_op, _src, _dst, _eflags, \
242 "b", "q", "w", "r", _LO32, "r", "", "r")
243
244/* Source operand is word, long or quad sized. */
245#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
246 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
247 "w", "r", _LO32, "r", "", "r")
248
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100249/* Instruction has three operands and one operand is stored in ECX register */
250#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
251 do { \
252 unsigned long _tmp; \
253 _type _clv = (_cl).val; \
254 _type _srcv = (_src).val; \
255 _type _dstv = (_dst).val; \
256 \
257 __asm__ __volatile__ ( \
258 _PRE_EFLAGS("0", "5", "2") \
259 _op _suffix " %4,%1 \n" \
260 _POST_EFLAGS("0", "5", "2") \
261 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
262 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
263 ); \
264 \
265 (_cl).val = (unsigned long) _clv; \
266 (_src).val = (unsigned long) _srcv; \
267 (_dst).val = (unsigned long) _dstv; \
268 } while (0)
269
270#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
271 do { \
272 switch ((_dst).bytes) { \
273 case 2: \
274 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
275 "w", unsigned short); \
276 break; \
277 case 4: \
278 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
279 "l", unsigned int); \
280 break; \
281 case 8: \
282 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
283 "q", unsigned long)); \
284 break; \
285 } \
286 } while (0)
287
Avi Kivitydda96d82008-11-26 15:14:10 +0200288#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 do { \
290 unsigned long _tmp; \
291 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200292 __asm__ __volatile__ ( \
293 _PRE_EFLAGS("0", "3", "2") \
294 _op _suffix " %1; " \
295 _POST_EFLAGS("0", "3", "2") \
296 : "=m" (_eflags), "+m" ((_dst).val), \
297 "=&r" (_tmp) \
298 : "i" (EFLAGS_MASK)); \
299 } while (0)
300
301/* Instruction has only one explicit operand (no source operand). */
302#define emulate_1op(_op, _dst, _eflags) \
303 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400304 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200305 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
306 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
307 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
308 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309 } \
310 } while (0)
311
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300312#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
313 do { \
314 unsigned long _tmp; \
315 \
316 __asm__ __volatile__ ( \
317 _PRE_EFLAGS("0", "4", "1") \
318 _op _suffix " %5; " \
319 _POST_EFLAGS("0", "4", "1") \
320 : "=m" (_eflags), "=&r" (_tmp), \
321 "+a" (_rax), "+d" (_rdx) \
322 : "i" (EFLAGS_MASK), "m" ((_src).val), \
323 "a" (_rax), "d" (_rdx)); \
324 } while (0)
325
Avi Kivityf6b35972010-08-26 11:59:00 +0300326#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
327 do { \
328 unsigned long _tmp; \
329 \
330 __asm__ __volatile__ ( \
331 _PRE_EFLAGS("0", "5", "1") \
332 "1: \n\t" \
333 _op _suffix " %6; " \
334 "2: \n\t" \
335 _POST_EFLAGS("0", "5", "1") \
336 ".pushsection .fixup,\"ax\" \n\t" \
337 "3: movb $1, %4 \n\t" \
338 "jmp 2b \n\t" \
339 ".popsection \n\t" \
340 _ASM_EXTABLE(1b, 3b) \
341 : "=m" (_eflags), "=&r" (_tmp), \
342 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
343 : "i" (EFLAGS_MASK), "m" ((_src).val), \
344 "a" (_rax), "d" (_rdx)); \
345 } while (0)
346
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300347/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
348#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
349 do { \
350 switch((_src).bytes) { \
351 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
352 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
353 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
354 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
355 } \
356 } while (0)
357
Avi Kivityf6b35972010-08-26 11:59:00 +0300358#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
359 do { \
360 switch((_src).bytes) { \
361 case 1: \
362 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
363 _eflags, "b", _ex); \
364 break; \
365 case 2: \
366 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
367 _eflags, "w", _ex); \
368 break; \
369 case 4: \
370 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
371 _eflags, "l", _ex); \
372 break; \
373 case 8: ON64( \
374 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
375 _eflags, "q", _ex)); \
376 break; \
377 } \
378 } while (0)
379
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380/* Fetch next part of the instruction being emulated. */
381#define insn_fetch(_type, _size, _eip) \
382({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200383 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200384 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385 goto done; \
386 (_eip) += (_size); \
387 (_type)_x; \
388})
389
Gleb Natapov414e6272010-04-28 19:15:26 +0300390#define insn_fetch_arr(_arr, _size, _eip) \
391({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
392 if (rc != X86EMUL_CONTINUE) \
393 goto done; \
394 (_eip) += (_size); \
395})
396
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800397static inline unsigned long ad_mask(struct decode_cache *c)
398{
399 return (1UL << (c->ad_bytes << 3)) - 1;
400}
401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800403static inline unsigned long
404address_mask(struct decode_cache *c, unsigned long reg)
405{
406 if (c->ad_bytes == sizeof(unsigned long))
407 return reg;
408 else
409 return reg & ad_mask(c);
410}
411
412static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200413register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800414{
Avi Kivity90de84f2010-11-17 15:28:21 +0200415 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800416}
417
Harvey Harrison7a9572752008-02-19 07:40:41 -0800418static inline void
419register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
420{
421 if (c->ad_bytes == sizeof(unsigned long))
422 *reg += inc;
423 else
424 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
425}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426
Harvey Harrison7a9572752008-02-19 07:40:41 -0800427static inline void jmp_rel(struct decode_cache *c, int rel)
428{
429 register_address_increment(c, &c->eip, rel);
430}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300431
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300432static void set_seg_override(struct decode_cache *c, int seg)
433{
434 c->has_seg_override = true;
435 c->seg_override = seg;
436}
437
Gleb Natapov79168fd2010-04-28 19:15:30 +0300438static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
439 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300440{
441 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
442 return 0;
443
Gleb Natapov79168fd2010-04-28 19:15:30 +0300444 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300445}
446
Avi Kivity90de84f2010-11-17 15:28:21 +0200447static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
448 struct x86_emulate_ops *ops,
449 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300450{
451 if (!c->has_seg_override)
452 return 0;
453
Avi Kivity90de84f2010-11-17 15:28:21 +0200454 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300455}
456
Avi Kivity90de84f2010-11-17 15:28:21 +0200457static ulong linear(struct x86_emulate_ctxt *ctxt,
458 struct segmented_address addr)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300459{
Avi Kivity90de84f2010-11-17 15:28:21 +0200460 struct decode_cache *c = &ctxt->decode;
461 ulong la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300462
Avi Kivity90de84f2010-11-17 15:28:21 +0200463 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
464 if (c->ad_bytes != 8)
465 la &= (u32)-1;
466 return la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300467}
468
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200469static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
470 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300471{
Avi Kivityda9cb572010-11-22 17:53:21 +0200472 ctxt->exception.vector = vec;
473 ctxt->exception.error_code = error;
474 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200475 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300476}
477
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200478static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300479{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200480 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300481}
482
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200483static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300484{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200485 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300486}
487
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200488static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300489{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200490 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300491}
492
Avi Kivity34d1f492010-08-26 11:59:01 +0300493static int emulate_de(struct x86_emulate_ctxt *ctxt)
494{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200495 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300496}
497
Avi Kivity62266862007-11-20 13:15:52 +0200498static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
499 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300500 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200501{
502 struct fetch_cache *fc = &ctxt->decode.fetch;
503 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300504 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200505
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300506 if (eip == fc->end) {
507 cur_size = fc->end - fc->start;
508 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
509 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200510 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900511 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200512 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300513 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200514 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300515 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900516 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200517}
518
519static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
520 struct x86_emulate_ops *ops,
521 unsigned long eip, void *dest, unsigned size)
522{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900523 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200524
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200525 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200526 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200527 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200528 while (size--) {
529 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900530 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200531 return rc;
532 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900533 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200534}
535
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000536/*
537 * Given the 'reg' portion of a ModRM byte, and a register block, return a
538 * pointer into the block that addresses the relevant register.
539 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
540 */
541static void *decode_register(u8 modrm_reg, unsigned long *regs,
542 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543{
544 void *p;
545
546 p = &regs[modrm_reg];
547 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
548 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
549 return p;
550}
551
552static int read_descriptor(struct x86_emulate_ctxt *ctxt,
553 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200554 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 u16 *size, unsigned long *address, int op_bytes)
556{
557 int rc;
558
559 if (op_bytes == 2)
560 op_bytes = 3;
561 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200562 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200563 ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900564 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200566 addr.ea += 2;
567 rc = ops->read_std(linear(ctxt, addr), address, op_bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200568 ctxt->vcpu, &ctxt->exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569 return rc;
570}
571
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300572static int test_cc(unsigned int condition, unsigned int flags)
573{
574 int rc = 0;
575
576 switch ((condition & 15) >> 1) {
577 case 0: /* o */
578 rc |= (flags & EFLG_OF);
579 break;
580 case 1: /* b/c/nae */
581 rc |= (flags & EFLG_CF);
582 break;
583 case 2: /* z/e */
584 rc |= (flags & EFLG_ZF);
585 break;
586 case 3: /* be/na */
587 rc |= (flags & (EFLG_CF|EFLG_ZF));
588 break;
589 case 4: /* s */
590 rc |= (flags & EFLG_SF);
591 break;
592 case 5: /* p/pe */
593 rc |= (flags & EFLG_PF);
594 break;
595 case 7: /* le/ng */
596 rc |= (flags & EFLG_ZF);
597 /* fall through */
598 case 6: /* l/nge */
599 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
600 break;
601 }
602
603 /* Odd condition identifiers (lsb == 1) have inverted sense. */
604 return (!!rc ^ (condition & 1));
605}
606
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300607static void fetch_register_operand(struct operand *op)
608{
609 switch (op->bytes) {
610 case 1:
611 op->val = *(u8 *)op->addr.reg;
612 break;
613 case 2:
614 op->val = *(u16 *)op->addr.reg;
615 break;
616 case 4:
617 op->val = *(u32 *)op->addr.reg;
618 break;
619 case 8:
620 op->val = *(u64 *)op->addr.reg;
621 break;
622 }
623}
624
Avi Kivity3c118e22007-10-31 10:27:04 +0200625static void decode_register_operand(struct operand *op,
626 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200627 int inhibit_bytereg)
628{
Avi Kivity33615aa2007-10-31 11:15:56 +0200629 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200630 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200631
632 if (!(c->d & ModRM))
633 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200634 op->type = OP_REG;
635 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300636 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200637 op->bytes = 1;
638 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300639 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200640 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200641 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300642 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200643 op->orig_val = op->val;
644}
645
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200646static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300647 struct x86_emulate_ops *ops,
648 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200649{
650 struct decode_cache *c = &ctxt->decode;
651 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700652 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900653 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300654 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200655
656 if (c->rex_prefix) {
657 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
658 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
659 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
660 }
661
662 c->modrm = insn_fetch(u8, 1, c->eip);
663 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
664 c->modrm_reg |= (c->modrm & 0x38) >> 3;
665 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300666 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200667
668 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300669 op->type = OP_REG;
670 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
671 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300672 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300673 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200674 return rc;
675 }
676
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300677 op->type = OP_MEM;
678
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200679 if (c->ad_bytes == 2) {
680 unsigned bx = c->regs[VCPU_REGS_RBX];
681 unsigned bp = c->regs[VCPU_REGS_RBP];
682 unsigned si = c->regs[VCPU_REGS_RSI];
683 unsigned di = c->regs[VCPU_REGS_RDI];
684
685 /* 16-bit ModR/M decode. */
686 switch (c->modrm_mod) {
687 case 0:
688 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300689 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200690 break;
691 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300692 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200693 break;
694 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300695 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200696 break;
697 }
698 switch (c->modrm_rm) {
699 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300700 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200701 break;
702 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300703 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200704 break;
705 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300706 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200707 break;
708 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300709 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200710 break;
711 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300712 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200713 break;
714 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300715 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200716 break;
717 case 6:
718 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300719 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720 break;
721 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300722 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200723 break;
724 }
725 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
726 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300727 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300728 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200729 } else {
730 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700731 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200732 sib = insn_fetch(u8, 1, c->eip);
733 index_reg |= (sib >> 3) & 7;
734 base_reg |= sib & 7;
735 scale = sib >> 6;
736
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700737 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300738 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700739 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300740 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700741 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300742 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700743 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
744 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700745 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700746 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300747 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200748 switch (c->modrm_mod) {
749 case 0:
750 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300751 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200752 break;
753 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300754 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200755 break;
756 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300757 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200758 break;
759 }
760 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200761 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200762done:
763 return rc;
764}
765
766static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300767 struct x86_emulate_ops *ops,
768 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200769{
770 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900771 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200772
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300773 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200774 switch (c->ad_bytes) {
775 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200776 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200777 break;
778 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200779 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200780 break;
781 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200782 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200783 break;
784 }
785done:
786 return rc;
787}
788
Wei Yongjun35c843c2010-08-09 11:34:56 +0800789static void fetch_bit_operand(struct decode_cache *c)
790{
Sheng Yang7129eec2010-09-28 16:33:32 +0800791 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800792
Wei Yongjun3885f182010-08-09 11:37:37 +0800793 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800794 mask = ~(c->dst.bytes * 8 - 1);
795
796 if (c->src.bytes == 2)
797 sv = (s16)c->src.val & (s16)mask;
798 else if (c->src.bytes == 4)
799 sv = (s32)c->src.val & (s32)mask;
800
Avi Kivity90de84f2010-11-17 15:28:21 +0200801 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800802 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800803
804 /* only subword offset */
805 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800806}
807
Gleb Natapov9de41572010-04-28 19:15:22 +0300808static int read_emulated(struct x86_emulate_ctxt *ctxt,
809 struct x86_emulate_ops *ops,
810 unsigned long addr, void *dest, unsigned size)
811{
812 int rc;
813 struct read_cache *mc = &ctxt->decode.mem_read;
814
815 while (size) {
816 int n = min(size, 8u);
817 size -= n;
818 if (mc->pos < mc->end)
819 goto read_cached;
820
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200821 rc = ops->read_emulated(addr, mc->data + mc->end, n,
822 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +0300823 if (rc != X86EMUL_CONTINUE)
824 return rc;
825 mc->end += n;
826
827 read_cached:
828 memcpy(dest, mc->data + mc->pos, n);
829 mc->pos += n;
830 dest += n;
831 addr += n;
832 }
833 return X86EMUL_CONTINUE;
834}
835
Gleb Natapov7b262e92010-03-18 15:20:27 +0200836static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
837 struct x86_emulate_ops *ops,
838 unsigned int size, unsigned short port,
839 void *dest)
840{
841 struct read_cache *rc = &ctxt->decode.io_read;
842
843 if (rc->pos == rc->end) { /* refill pio read ahead */
844 struct decode_cache *c = &ctxt->decode;
845 unsigned int in_page, n;
846 unsigned int count = c->rep_prefix ?
847 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
848 in_page = (ctxt->eflags & EFLG_DF) ?
849 offset_in_page(c->regs[VCPU_REGS_RDI]) :
850 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
851 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
852 count);
853 if (n == 0)
854 n = 1;
855 rc->pos = rc->end = 0;
856 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
857 return 0;
858 rc->end = n * size;
859 }
860
861 memcpy(dest, rc->data + rc->pos, size);
862 rc->pos += size;
863 return 1;
864}
865
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200866static u32 desc_limit_scaled(struct desc_struct *desc)
867{
868 u32 limit = get_desc_limit(desc);
869
870 return desc->g ? (limit << 12) | 0xfff : limit;
871}
872
873static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
874 struct x86_emulate_ops *ops,
875 u16 selector, struct desc_ptr *dt)
876{
877 if (selector & 1 << 2) {
878 struct desc_struct desc;
879 memset (dt, 0, sizeof *dt);
880 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
881 return;
882
883 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
884 dt->address = get_desc_base(&desc);
885 } else
886 ops->get_gdt(dt, ctxt->vcpu);
887}
888
889/* allowed just for 8 bytes segments */
890static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
891 struct x86_emulate_ops *ops,
892 u16 selector, struct desc_struct *desc)
893{
894 struct desc_ptr dt;
895 u16 index = selector >> 3;
896 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200897 ulong addr;
898
899 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
900
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200901 if (dt.size < index * 8 + 7)
902 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200903 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200904 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
905 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200906
907 return ret;
908}
909
910/* allowed just for 8 bytes segments */
911static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
912 struct x86_emulate_ops *ops,
913 u16 selector, struct desc_struct *desc)
914{
915 struct desc_ptr dt;
916 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200917 ulong addr;
918 int ret;
919
920 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
921
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200922 if (dt.size < index * 8 + 7)
923 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200924
925 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200926 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
927 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200928
929 return ret;
930}
931
932static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
933 struct x86_emulate_ops *ops,
934 u16 selector, int seg)
935{
936 struct desc_struct seg_desc;
937 u8 dpl, rpl, cpl;
938 unsigned err_vec = GP_VECTOR;
939 u32 err_code = 0;
940 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
941 int ret;
942
943 memset(&seg_desc, 0, sizeof seg_desc);
944
945 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
946 || ctxt->mode == X86EMUL_MODE_REAL) {
947 /* set real mode segment descriptor */
948 set_desc_base(&seg_desc, selector << 4);
949 set_desc_limit(&seg_desc, 0xffff);
950 seg_desc.type = 3;
951 seg_desc.p = 1;
952 seg_desc.s = 1;
953 goto load;
954 }
955
956 /* NULL selector is not valid for TR, CS and SS */
957 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
958 && null_selector)
959 goto exception;
960
961 /* TR should be in GDT only */
962 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
963 goto exception;
964
965 if (null_selector) /* for NULL selector skip all following checks */
966 goto load;
967
968 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
969 if (ret != X86EMUL_CONTINUE)
970 return ret;
971
972 err_code = selector & 0xfffc;
973 err_vec = GP_VECTOR;
974
975 /* can't load system descriptor into segment selecor */
976 if (seg <= VCPU_SREG_GS && !seg_desc.s)
977 goto exception;
978
979 if (!seg_desc.p) {
980 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
981 goto exception;
982 }
983
984 rpl = selector & 3;
985 dpl = seg_desc.dpl;
986 cpl = ops->cpl(ctxt->vcpu);
987
988 switch (seg) {
989 case VCPU_SREG_SS:
990 /*
991 * segment is not a writable data segment or segment
992 * selector's RPL != CPL or segment selector's RPL != CPL
993 */
994 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
995 goto exception;
996 break;
997 case VCPU_SREG_CS:
998 if (!(seg_desc.type & 8))
999 goto exception;
1000
1001 if (seg_desc.type & 4) {
1002 /* conforming */
1003 if (dpl > cpl)
1004 goto exception;
1005 } else {
1006 /* nonconforming */
1007 if (rpl > cpl || dpl != cpl)
1008 goto exception;
1009 }
1010 /* CS(RPL) <- CPL */
1011 selector = (selector & 0xfffc) | cpl;
1012 break;
1013 case VCPU_SREG_TR:
1014 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1015 goto exception;
1016 break;
1017 case VCPU_SREG_LDTR:
1018 if (seg_desc.s || seg_desc.type != 2)
1019 goto exception;
1020 break;
1021 default: /* DS, ES, FS, or GS */
1022 /*
1023 * segment is not a data or readable code segment or
1024 * ((segment is a data or nonconforming code segment)
1025 * and (both RPL and CPL > DPL))
1026 */
1027 if ((seg_desc.type & 0xa) == 0x8 ||
1028 (((seg_desc.type & 0xc) != 0xc) &&
1029 (rpl > dpl && cpl > dpl)))
1030 goto exception;
1031 break;
1032 }
1033
1034 if (seg_desc.s) {
1035 /* mark segment as accessed */
1036 seg_desc.type |= 1;
1037 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1038 if (ret != X86EMUL_CONTINUE)
1039 return ret;
1040 }
1041load:
1042 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1043 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1044 return X86EMUL_CONTINUE;
1045exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001046 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001047 return X86EMUL_PROPAGATE_FAULT;
1048}
1049
Wei Yongjun31be40b2010-08-17 09:17:30 +08001050static void write_register_operand(struct operand *op)
1051{
1052 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1053 switch (op->bytes) {
1054 case 1:
1055 *(u8 *)op->addr.reg = (u8)op->val;
1056 break;
1057 case 2:
1058 *(u16 *)op->addr.reg = (u16)op->val;
1059 break;
1060 case 4:
1061 *op->addr.reg = (u32)op->val;
1062 break; /* 64b: zero-extend */
1063 case 8:
1064 *op->addr.reg = op->val;
1065 break;
1066 }
1067}
1068
Wei Yongjunc37eda12010-06-15 09:03:33 +08001069static inline int writeback(struct x86_emulate_ctxt *ctxt,
1070 struct x86_emulate_ops *ops)
1071{
1072 int rc;
1073 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001074
1075 switch (c->dst.type) {
1076 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001077 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001078 break;
1079 case OP_MEM:
1080 if (c->lock_prefix)
1081 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001082 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001083 &c->dst.orig_val,
1084 &c->dst.val,
1085 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001086 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001087 ctxt->vcpu);
1088 else
1089 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001090 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001091 &c->dst.val,
1092 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001093 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001094 ctxt->vcpu);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001095 if (rc != X86EMUL_CONTINUE)
1096 return rc;
1097 break;
1098 case OP_NONE:
1099 /* no writeback */
1100 break;
1101 default:
1102 break;
1103 }
1104 return X86EMUL_CONTINUE;
1105}
1106
Gleb Natapov79168fd2010-04-28 19:15:30 +03001107static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1108 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001109{
1110 struct decode_cache *c = &ctxt->decode;
1111
1112 c->dst.type = OP_MEM;
1113 c->dst.bytes = c->op_bytes;
1114 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001115 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001116 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1117 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001118}
1119
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001120static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001121 struct x86_emulate_ops *ops,
1122 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001123{
1124 struct decode_cache *c = &ctxt->decode;
1125 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001126 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001127
Avi Kivity90de84f2010-11-17 15:28:21 +02001128 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1129 addr.seg = VCPU_SREG_SS;
1130 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001131 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001132 return rc;
1133
Avi Kivity350f69d2009-01-05 11:12:40 +02001134 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001135 return rc;
1136}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001137
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001138static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1139 struct x86_emulate_ops *ops,
1140 void *dest, int len)
1141{
1142 int rc;
1143 unsigned long val, change_mask;
1144 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001145 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001146
1147 rc = emulate_pop(ctxt, ops, &val, len);
1148 if (rc != X86EMUL_CONTINUE)
1149 return rc;
1150
1151 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1152 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1153
1154 switch(ctxt->mode) {
1155 case X86EMUL_MODE_PROT64:
1156 case X86EMUL_MODE_PROT32:
1157 case X86EMUL_MODE_PROT16:
1158 if (cpl == 0)
1159 change_mask |= EFLG_IOPL;
1160 if (cpl <= iopl)
1161 change_mask |= EFLG_IF;
1162 break;
1163 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001164 if (iopl < 3)
1165 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001166 change_mask |= EFLG_IF;
1167 break;
1168 default: /* real mode */
1169 change_mask |= (EFLG_IOPL | EFLG_IF);
1170 break;
1171 }
1172
1173 *(unsigned long *)dest =
1174 (ctxt->eflags & ~change_mask) | (val & change_mask);
1175
1176 return rc;
1177}
1178
Gleb Natapov79168fd2010-04-28 19:15:30 +03001179static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1180 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001181{
1182 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001183
Gleb Natapov79168fd2010-04-28 19:15:30 +03001184 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001185
Gleb Natapov79168fd2010-04-28 19:15:30 +03001186 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001187}
1188
1189static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1190 struct x86_emulate_ops *ops, int seg)
1191{
1192 struct decode_cache *c = &ctxt->decode;
1193 unsigned long selector;
1194 int rc;
1195
1196 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001197 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001198 return rc;
1199
Gleb Natapov2e873022010-03-18 15:20:18 +02001200 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001201 return rc;
1202}
1203
Wei Yongjunc37eda12010-06-15 09:03:33 +08001204static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001205 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001206{
1207 struct decode_cache *c = &ctxt->decode;
1208 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001209 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001210 int reg = VCPU_REGS_RAX;
1211
1212 while (reg <= VCPU_REGS_RDI) {
1213 (reg == VCPU_REGS_RSP) ?
1214 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1215
Gleb Natapov79168fd2010-04-28 19:15:30 +03001216 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001217
1218 rc = writeback(ctxt, ops);
1219 if (rc != X86EMUL_CONTINUE)
1220 return rc;
1221
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001222 ++reg;
1223 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001224
1225 /* Disable writeback. */
1226 c->dst.type = OP_NONE;
1227
1228 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001229}
1230
1231static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1232 struct x86_emulate_ops *ops)
1233{
1234 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001235 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001236 int reg = VCPU_REGS_RDI;
1237
1238 while (reg >= VCPU_REGS_RAX) {
1239 if (reg == VCPU_REGS_RSP) {
1240 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1241 c->op_bytes);
1242 --reg;
1243 }
1244
1245 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001246 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001247 break;
1248 --reg;
1249 }
1250 return rc;
1251}
1252
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001253int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1254 struct x86_emulate_ops *ops, int irq)
1255{
1256 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001257 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001258 struct desc_ptr dt;
1259 gva_t cs_addr;
1260 gva_t eip_addr;
1261 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001262
1263 /* TODO: Add limit checks */
1264 c->src.val = ctxt->eflags;
1265 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001266 rc = writeback(ctxt, ops);
1267 if (rc != X86EMUL_CONTINUE)
1268 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001269
1270 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1271
1272 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1273 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001274 rc = writeback(ctxt, ops);
1275 if (rc != X86EMUL_CONTINUE)
1276 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001277
1278 c->src.val = c->eip;
1279 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001280 rc = writeback(ctxt, ops);
1281 if (rc != X86EMUL_CONTINUE)
1282 return rc;
1283
1284 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001285
1286 ops->get_idt(&dt, ctxt->vcpu);
1287
1288 eip_addr = dt.address + (irq << 2);
1289 cs_addr = dt.address + (irq << 2) + 2;
1290
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001291 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001292 if (rc != X86EMUL_CONTINUE)
1293 return rc;
1294
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001295 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001296 if (rc != X86EMUL_CONTINUE)
1297 return rc;
1298
1299 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1300 if (rc != X86EMUL_CONTINUE)
1301 return rc;
1302
1303 c->eip = eip;
1304
1305 return rc;
1306}
1307
1308static int emulate_int(struct x86_emulate_ctxt *ctxt,
1309 struct x86_emulate_ops *ops, int irq)
1310{
1311 switch(ctxt->mode) {
1312 case X86EMUL_MODE_REAL:
1313 return emulate_int_real(ctxt, ops, irq);
1314 case X86EMUL_MODE_VM86:
1315 case X86EMUL_MODE_PROT16:
1316 case X86EMUL_MODE_PROT32:
1317 case X86EMUL_MODE_PROT64:
1318 default:
1319 /* Protected mode interrupts unimplemented yet */
1320 return X86EMUL_UNHANDLEABLE;
1321 }
1322}
1323
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001324static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1325 struct x86_emulate_ops *ops)
1326{
1327 struct decode_cache *c = &ctxt->decode;
1328 int rc = X86EMUL_CONTINUE;
1329 unsigned long temp_eip = 0;
1330 unsigned long temp_eflags = 0;
1331 unsigned long cs = 0;
1332 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1333 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1334 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1335 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1336
1337 /* TODO: Add stack limit check */
1338
1339 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1340
1341 if (rc != X86EMUL_CONTINUE)
1342 return rc;
1343
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001344 if (temp_eip & ~0xffff)
1345 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001346
1347 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1348
1349 if (rc != X86EMUL_CONTINUE)
1350 return rc;
1351
1352 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1353
1354 if (rc != X86EMUL_CONTINUE)
1355 return rc;
1356
1357 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1358
1359 if (rc != X86EMUL_CONTINUE)
1360 return rc;
1361
1362 c->eip = temp_eip;
1363
1364
1365 if (c->op_bytes == 4)
1366 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1367 else if (c->op_bytes == 2) {
1368 ctxt->eflags &= ~0xffff;
1369 ctxt->eflags |= temp_eflags;
1370 }
1371
1372 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1373 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1374
1375 return rc;
1376}
1377
1378static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1379 struct x86_emulate_ops* ops)
1380{
1381 switch(ctxt->mode) {
1382 case X86EMUL_MODE_REAL:
1383 return emulate_iret_real(ctxt, ops);
1384 case X86EMUL_MODE_VM86:
1385 case X86EMUL_MODE_PROT16:
1386 case X86EMUL_MODE_PROT32:
1387 case X86EMUL_MODE_PROT64:
1388 default:
1389 /* iret from protected mode unimplemented yet */
1390 return X86EMUL_UNHANDLEABLE;
1391 }
1392}
1393
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001394static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1395 struct x86_emulate_ops *ops)
1396{
1397 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001398
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001399 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001400}
1401
Laurent Vivier05f086f2007-09-24 11:10:55 +02001402static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001404 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001405 switch (c->modrm_reg) {
1406 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001407 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001408 break;
1409 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001410 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001411 break;
1412 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001413 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001414 break;
1415 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001416 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001417 break;
1418 case 4: /* sal/shl */
1419 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001420 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001421 break;
1422 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001423 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001424 break;
1425 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001426 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001427 break;
1428 }
1429}
1430
1431static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001432 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001433{
1434 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001435 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1436 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001437 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001438
1439 switch (c->modrm_reg) {
1440 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001441 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001442 break;
1443 case 2: /* not */
1444 c->dst.val = ~c->dst.val;
1445 break;
1446 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001447 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001448 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001449 case 4: /* mul */
1450 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1451 break;
1452 case 5: /* imul */
1453 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1454 break;
1455 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001456 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1457 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001458 break;
1459 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001460 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1461 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001462 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001463 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001464 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001465 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001466 if (de)
1467 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001468 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001469}
1470
1471static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001472 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001473{
1474 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001475
1476 switch (c->modrm_reg) {
1477 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001478 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001479 break;
1480 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001481 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001482 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001483 case 2: /* call near abs */ {
1484 long int old_eip;
1485 old_eip = c->eip;
1486 c->eip = c->src.val;
1487 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001488 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001489 break;
1490 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001491 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001492 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001493 break;
1494 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001495 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001496 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001497 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001498 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001499}
1500
1501static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001502 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001503{
1504 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001505 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001506
1507 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1508 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001509 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1510 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001511 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001512 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001513 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1514 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001515
Laurent Vivier05f086f2007-09-24 11:10:55 +02001516 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001517 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001518 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001519}
1520
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001521static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1522 struct x86_emulate_ops *ops)
1523{
1524 struct decode_cache *c = &ctxt->decode;
1525 int rc;
1526 unsigned long cs;
1527
1528 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001529 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001530 return rc;
1531 if (c->op_bytes == 4)
1532 c->eip = (u32)c->eip;
1533 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001534 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001535 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001536 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001537 return rc;
1538}
1539
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001540static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1541 struct x86_emulate_ops *ops, int seg)
1542{
1543 struct decode_cache *c = &ctxt->decode;
1544 unsigned short sel;
1545 int rc;
1546
1547 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1548
1549 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1550 if (rc != X86EMUL_CONTINUE)
1551 return rc;
1552
1553 c->dst.val = c->src.val;
1554 return rc;
1555}
1556
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001557static inline void
1558setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001559 struct x86_emulate_ops *ops, struct desc_struct *cs,
1560 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001561{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001562 memset(cs, 0, sizeof(struct desc_struct));
1563 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1564 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001565
1566 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001567 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001568 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001569 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001570 cs->type = 0x0b; /* Read, Execute, Accessed */
1571 cs->s = 1;
1572 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001573 cs->p = 1;
1574 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001575
Gleb Natapov79168fd2010-04-28 19:15:30 +03001576 set_desc_base(ss, 0); /* flat segment */
1577 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001578 ss->g = 1; /* 4kb granularity */
1579 ss->s = 1;
1580 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001581 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001582 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001583 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001584}
1585
1586static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001587emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001588{
1589 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001590 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001591 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001592 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001593
1594 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001595 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001596 ctxt->mode == X86EMUL_MODE_VM86)
1597 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001598
Gleb Natapov79168fd2010-04-28 19:15:30 +03001599 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001600
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001601 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001602 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001603 cs_sel = (u16)(msr_data & 0xfffc);
1604 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001605
1606 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001608 cs.l = 1;
1609 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001610 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1611 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1612 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1613 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001614
1615 c->regs[VCPU_REGS_RCX] = c->eip;
1616 if (is_long_mode(ctxt->vcpu)) {
1617#ifdef CONFIG_X86_64
1618 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1619
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001620 ops->get_msr(ctxt->vcpu,
1621 ctxt->mode == X86EMUL_MODE_PROT64 ?
1622 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001623 c->eip = msr_data;
1624
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001625 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001626 ctxt->eflags &= ~(msr_data | EFLG_RF);
1627#endif
1628 } else {
1629 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001630 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001631 c->eip = (u32)msr_data;
1632
1633 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1634 }
1635
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001636 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001637}
1638
Andre Przywara8c604352009-06-18 12:56:01 +02001639static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001640emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001641{
1642 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001643 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001644 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001645 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001646
Gleb Natapova0044752010-02-10 14:21:31 +02001647 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001648 if (ctxt->mode == X86EMUL_MODE_REAL)
1649 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001650
1651 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1652 * Therefore, we inject an #UD.
1653 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001654 if (ctxt->mode == X86EMUL_MODE_PROT64)
1655 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001656
Gleb Natapov79168fd2010-04-28 19:15:30 +03001657 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001658
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001659 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001660 switch (ctxt->mode) {
1661 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001662 if ((msr_data & 0xfffc) == 0x0)
1663 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001664 break;
1665 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001666 if (msr_data == 0x0)
1667 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001668 break;
1669 }
1670
1671 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001672 cs_sel = (u16)msr_data;
1673 cs_sel &= ~SELECTOR_RPL_MASK;
1674 ss_sel = cs_sel + 8;
1675 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001676 if (ctxt->mode == X86EMUL_MODE_PROT64
1677 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001678 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001679 cs.l = 1;
1680 }
1681
Gleb Natapov79168fd2010-04-28 19:15:30 +03001682 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1683 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1684 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1685 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001686
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001687 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001688 c->eip = msr_data;
1689
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001690 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001691 c->regs[VCPU_REGS_RSP] = msr_data;
1692
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001693 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001694}
1695
Andre Przywara4668f052009-06-18 12:56:02 +02001696static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001697emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001698{
1699 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001700 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001701 u64 msr_data;
1702 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001703 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001704
Gleb Natapova0044752010-02-10 14:21:31 +02001705 /* inject #GP if in real mode or Virtual 8086 mode */
1706 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001707 ctxt->mode == X86EMUL_MODE_VM86)
1708 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001709
Gleb Natapov79168fd2010-04-28 19:15:30 +03001710 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001711
1712 if ((c->rex_prefix & 0x8) != 0x0)
1713 usermode = X86EMUL_MODE_PROT64;
1714 else
1715 usermode = X86EMUL_MODE_PROT32;
1716
1717 cs.dpl = 3;
1718 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001719 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001720 switch (usermode) {
1721 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001722 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001723 if ((msr_data & 0xfffc) == 0x0)
1724 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001725 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001726 break;
1727 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001728 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001729 if (msr_data == 0x0)
1730 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001731 ss_sel = cs_sel + 8;
1732 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001733 cs.l = 1;
1734 break;
1735 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001736 cs_sel |= SELECTOR_RPL_MASK;
1737 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001738
Gleb Natapov79168fd2010-04-28 19:15:30 +03001739 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1740 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1741 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1742 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001743
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001744 c->eip = c->regs[VCPU_REGS_RDX];
1745 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001746
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001747 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001748}
1749
Gleb Natapov9c537242010-03-18 15:20:05 +02001750static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1751 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001752{
1753 int iopl;
1754 if (ctxt->mode == X86EMUL_MODE_REAL)
1755 return false;
1756 if (ctxt->mode == X86EMUL_MODE_VM86)
1757 return true;
1758 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001759 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001760}
1761
1762static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1763 struct x86_emulate_ops *ops,
1764 u16 port, u16 len)
1765{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001766 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001767 int r;
1768 u16 io_bitmap_ptr;
1769 u8 perm, bit_idx = port & 0x7;
1770 unsigned mask = (1 << len) - 1;
1771
Gleb Natapov79168fd2010-04-28 19:15:30 +03001772 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1773 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001774 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001775 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001776 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001777 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1778 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001779 if (r != X86EMUL_CONTINUE)
1780 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001781 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001782 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001783 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1784 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001785 if (r != X86EMUL_CONTINUE)
1786 return false;
1787 if ((perm >> bit_idx) & mask)
1788 return false;
1789 return true;
1790}
1791
1792static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1793 struct x86_emulate_ops *ops,
1794 u16 port, u16 len)
1795{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001796 if (ctxt->perm_ok)
1797 return true;
1798
Gleb Natapov9c537242010-03-18 15:20:05 +02001799 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001800 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1801 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001802
1803 ctxt->perm_ok = true;
1804
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001805 return true;
1806}
1807
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001808static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1809 struct x86_emulate_ops *ops,
1810 struct tss_segment_16 *tss)
1811{
1812 struct decode_cache *c = &ctxt->decode;
1813
1814 tss->ip = c->eip;
1815 tss->flag = ctxt->eflags;
1816 tss->ax = c->regs[VCPU_REGS_RAX];
1817 tss->cx = c->regs[VCPU_REGS_RCX];
1818 tss->dx = c->regs[VCPU_REGS_RDX];
1819 tss->bx = c->regs[VCPU_REGS_RBX];
1820 tss->sp = c->regs[VCPU_REGS_RSP];
1821 tss->bp = c->regs[VCPU_REGS_RBP];
1822 tss->si = c->regs[VCPU_REGS_RSI];
1823 tss->di = c->regs[VCPU_REGS_RDI];
1824
1825 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1826 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1827 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1828 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1829 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1830}
1831
1832static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1833 struct x86_emulate_ops *ops,
1834 struct tss_segment_16 *tss)
1835{
1836 struct decode_cache *c = &ctxt->decode;
1837 int ret;
1838
1839 c->eip = tss->ip;
1840 ctxt->eflags = tss->flag | 2;
1841 c->regs[VCPU_REGS_RAX] = tss->ax;
1842 c->regs[VCPU_REGS_RCX] = tss->cx;
1843 c->regs[VCPU_REGS_RDX] = tss->dx;
1844 c->regs[VCPU_REGS_RBX] = tss->bx;
1845 c->regs[VCPU_REGS_RSP] = tss->sp;
1846 c->regs[VCPU_REGS_RBP] = tss->bp;
1847 c->regs[VCPU_REGS_RSI] = tss->si;
1848 c->regs[VCPU_REGS_RDI] = tss->di;
1849
1850 /*
1851 * SDM says that segment selectors are loaded before segment
1852 * descriptors
1853 */
1854 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1855 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1856 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1857 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1858 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1859
1860 /*
1861 * Now load segment descriptors. If fault happenes at this stage
1862 * it is handled in a context of new task
1863 */
1864 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1865 if (ret != X86EMUL_CONTINUE)
1866 return ret;
1867 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1868 if (ret != X86EMUL_CONTINUE)
1869 return ret;
1870 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1871 if (ret != X86EMUL_CONTINUE)
1872 return ret;
1873 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1874 if (ret != X86EMUL_CONTINUE)
1875 return ret;
1876 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1877 if (ret != X86EMUL_CONTINUE)
1878 return ret;
1879
1880 return X86EMUL_CONTINUE;
1881}
1882
1883static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1884 struct x86_emulate_ops *ops,
1885 u16 tss_selector, u16 old_tss_sel,
1886 ulong old_tss_base, struct desc_struct *new_desc)
1887{
1888 struct tss_segment_16 tss_seg;
1889 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001890 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001891
1892 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001893 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001894 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001895 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001896 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001897
1898 save_state_to_tss16(ctxt, ops, &tss_seg);
1899
1900 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001901 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001902 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001903 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001904 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001905
1906 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001907 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001908 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001909 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001910 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001911
1912 if (old_tss_sel != 0xffff) {
1913 tss_seg.prev_task_link = old_tss_sel;
1914
1915 ret = ops->write_std(new_tss_base,
1916 &tss_seg.prev_task_link,
1917 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001918 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001919 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001920 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001921 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001922 }
1923
1924 return load_state_from_tss16(ctxt, ops, &tss_seg);
1925}
1926
1927static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1928 struct x86_emulate_ops *ops,
1929 struct tss_segment_32 *tss)
1930{
1931 struct decode_cache *c = &ctxt->decode;
1932
1933 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1934 tss->eip = c->eip;
1935 tss->eflags = ctxt->eflags;
1936 tss->eax = c->regs[VCPU_REGS_RAX];
1937 tss->ecx = c->regs[VCPU_REGS_RCX];
1938 tss->edx = c->regs[VCPU_REGS_RDX];
1939 tss->ebx = c->regs[VCPU_REGS_RBX];
1940 tss->esp = c->regs[VCPU_REGS_RSP];
1941 tss->ebp = c->regs[VCPU_REGS_RBP];
1942 tss->esi = c->regs[VCPU_REGS_RSI];
1943 tss->edi = c->regs[VCPU_REGS_RDI];
1944
1945 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1946 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1947 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1948 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1949 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1950 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1951 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1952}
1953
1954static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1955 struct x86_emulate_ops *ops,
1956 struct tss_segment_32 *tss)
1957{
1958 struct decode_cache *c = &ctxt->decode;
1959 int ret;
1960
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001961 if (ops->set_cr(3, tss->cr3, ctxt->vcpu))
1962 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001963 c->eip = tss->eip;
1964 ctxt->eflags = tss->eflags | 2;
1965 c->regs[VCPU_REGS_RAX] = tss->eax;
1966 c->regs[VCPU_REGS_RCX] = tss->ecx;
1967 c->regs[VCPU_REGS_RDX] = tss->edx;
1968 c->regs[VCPU_REGS_RBX] = tss->ebx;
1969 c->regs[VCPU_REGS_RSP] = tss->esp;
1970 c->regs[VCPU_REGS_RBP] = tss->ebp;
1971 c->regs[VCPU_REGS_RSI] = tss->esi;
1972 c->regs[VCPU_REGS_RDI] = tss->edi;
1973
1974 /*
1975 * SDM says that segment selectors are loaded before segment
1976 * descriptors
1977 */
1978 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1979 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1980 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1981 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1982 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1983 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1984 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1985
1986 /*
1987 * Now load segment descriptors. If fault happenes at this stage
1988 * it is handled in a context of new task
1989 */
1990 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1991 if (ret != X86EMUL_CONTINUE)
1992 return ret;
1993 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1994 if (ret != X86EMUL_CONTINUE)
1995 return ret;
1996 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1997 if (ret != X86EMUL_CONTINUE)
1998 return ret;
1999 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2000 if (ret != X86EMUL_CONTINUE)
2001 return ret;
2002 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2003 if (ret != X86EMUL_CONTINUE)
2004 return ret;
2005 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2006 if (ret != X86EMUL_CONTINUE)
2007 return ret;
2008 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2009 if (ret != X86EMUL_CONTINUE)
2010 return ret;
2011
2012 return X86EMUL_CONTINUE;
2013}
2014
2015static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2016 struct x86_emulate_ops *ops,
2017 u16 tss_selector, u16 old_tss_sel,
2018 ulong old_tss_base, struct desc_struct *new_desc)
2019{
2020 struct tss_segment_32 tss_seg;
2021 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002022 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002023
2024 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002025 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002026 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002027 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002028 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002029
2030 save_state_to_tss32(ctxt, ops, &tss_seg);
2031
2032 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002033 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002034 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002035 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002036 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002037
2038 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002039 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002040 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002041 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002042 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002043
2044 if (old_tss_sel != 0xffff) {
2045 tss_seg.prev_task_link = old_tss_sel;
2046
2047 ret = ops->write_std(new_tss_base,
2048 &tss_seg.prev_task_link,
2049 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002050 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002051 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002052 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002053 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002054 }
2055
2056 return load_state_from_tss32(ctxt, ops, &tss_seg);
2057}
2058
2059static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002060 struct x86_emulate_ops *ops,
2061 u16 tss_selector, int reason,
2062 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002063{
2064 struct desc_struct curr_tss_desc, next_tss_desc;
2065 int ret;
2066 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2067 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002068 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002069 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002070
2071 /* FIXME: old_tss_base == ~0 ? */
2072
2073 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2074 if (ret != X86EMUL_CONTINUE)
2075 return ret;
2076 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2077 if (ret != X86EMUL_CONTINUE)
2078 return ret;
2079
2080 /* FIXME: check that next_tss_desc is tss */
2081
2082 if (reason != TASK_SWITCH_IRET) {
2083 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002084 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl)
2085 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002086 }
2087
Gleb Natapovceffb452010-03-18 15:20:19 +02002088 desc_limit = desc_limit_scaled(&next_tss_desc);
2089 if (!next_tss_desc.p ||
2090 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2091 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002092 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002093 return X86EMUL_PROPAGATE_FAULT;
2094 }
2095
2096 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2097 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2098 write_segment_descriptor(ctxt, ops, old_tss_sel,
2099 &curr_tss_desc);
2100 }
2101
2102 if (reason == TASK_SWITCH_IRET)
2103 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2104
2105 /* set back link to prev task only if NT bit is set in eflags
2106 note that old_tss_sel is not used afetr this point */
2107 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2108 old_tss_sel = 0xffff;
2109
2110 if (next_tss_desc.type & 8)
2111 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2112 old_tss_base, &next_tss_desc);
2113 else
2114 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2115 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002116 if (ret != X86EMUL_CONTINUE)
2117 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002118
2119 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2120 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2121
2122 if (reason != TASK_SWITCH_IRET) {
2123 next_tss_desc.type |= (1 << 1); /* set busy flag */
2124 write_segment_descriptor(ctxt, ops, tss_selector,
2125 &next_tss_desc);
2126 }
2127
2128 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2129 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2130 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2131
Jan Kiszkae269fb22010-04-14 15:51:09 +02002132 if (has_error_code) {
2133 struct decode_cache *c = &ctxt->decode;
2134
2135 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2136 c->lock_prefix = 0;
2137 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002138 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002139 }
2140
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002141 return ret;
2142}
2143
2144int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002145 u16 tss_selector, int reason,
2146 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002147{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002148 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002149 struct decode_cache *c = &ctxt->decode;
2150 int rc;
2151
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002153 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154
Jan Kiszkae269fb22010-04-14 15:51:09 +02002155 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2156 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002157
2158 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002159 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002160 if (rc == X86EMUL_CONTINUE)
2161 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162 }
2163
Gleb Natapov19d04432010-04-15 12:29:50 +03002164 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165}
2166
Avi Kivity90de84f2010-11-17 15:28:21 +02002167static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002168 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002169{
2170 struct decode_cache *c = &ctxt->decode;
2171 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2172
Gleb Natapovd9271122010-03-18 15:20:22 +02002173 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002174 op->addr.mem.ea = register_address(c, c->regs[reg]);
2175 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002176}
2177
Avi Kivity63540382010-07-29 15:11:55 +03002178static int em_push(struct x86_emulate_ctxt *ctxt)
2179{
2180 emulate_push(ctxt, ctxt->ops);
2181 return X86EMUL_CONTINUE;
2182}
2183
Avi Kivity7af04fc2010-08-18 14:16:35 +03002184static int em_das(struct x86_emulate_ctxt *ctxt)
2185{
2186 struct decode_cache *c = &ctxt->decode;
2187 u8 al, old_al;
2188 bool af, cf, old_cf;
2189
2190 cf = ctxt->eflags & X86_EFLAGS_CF;
2191 al = c->dst.val;
2192
2193 old_al = al;
2194 old_cf = cf;
2195 cf = false;
2196 af = ctxt->eflags & X86_EFLAGS_AF;
2197 if ((al & 0x0f) > 9 || af) {
2198 al -= 6;
2199 cf = old_cf | (al >= 250);
2200 af = true;
2201 } else {
2202 af = false;
2203 }
2204 if (old_al > 0x99 || old_cf) {
2205 al -= 0x60;
2206 cf = true;
2207 }
2208
2209 c->dst.val = al;
2210 /* Set PF, ZF, SF */
2211 c->src.type = OP_IMM;
2212 c->src.val = 0;
2213 c->src.bytes = 1;
2214 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2215 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2216 if (cf)
2217 ctxt->eflags |= X86_EFLAGS_CF;
2218 if (af)
2219 ctxt->eflags |= X86_EFLAGS_AF;
2220 return X86EMUL_CONTINUE;
2221}
2222
Avi Kivity0ef753b2010-08-18 14:51:45 +03002223static int em_call_far(struct x86_emulate_ctxt *ctxt)
2224{
2225 struct decode_cache *c = &ctxt->decode;
2226 u16 sel, old_cs;
2227 ulong old_eip;
2228 int rc;
2229
2230 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2231 old_eip = c->eip;
2232
2233 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2234 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2235 return X86EMUL_CONTINUE;
2236
2237 c->eip = 0;
2238 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2239
2240 c->src.val = old_cs;
2241 emulate_push(ctxt, ctxt->ops);
2242 rc = writeback(ctxt, ctxt->ops);
2243 if (rc != X86EMUL_CONTINUE)
2244 return rc;
2245
2246 c->src.val = old_eip;
2247 emulate_push(ctxt, ctxt->ops);
2248 rc = writeback(ctxt, ctxt->ops);
2249 if (rc != X86EMUL_CONTINUE)
2250 return rc;
2251
2252 c->dst.type = OP_NONE;
2253
2254 return X86EMUL_CONTINUE;
2255}
2256
Avi Kivity40ece7c2010-08-18 15:12:09 +03002257static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2258{
2259 struct decode_cache *c = &ctxt->decode;
2260 int rc;
2261
2262 c->dst.type = OP_REG;
2263 c->dst.addr.reg = &c->eip;
2264 c->dst.bytes = c->op_bytes;
2265 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2266 if (rc != X86EMUL_CONTINUE)
2267 return rc;
2268 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2269 return X86EMUL_CONTINUE;
2270}
2271
Avi Kivity5c82aa22010-08-18 18:31:43 +03002272static int em_imul(struct x86_emulate_ctxt *ctxt)
2273{
2274 struct decode_cache *c = &ctxt->decode;
2275
2276 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2277 return X86EMUL_CONTINUE;
2278}
2279
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002280static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2281{
2282 struct decode_cache *c = &ctxt->decode;
2283
2284 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002285 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002286}
2287
Avi Kivity61429142010-08-19 15:13:00 +03002288static int em_cwd(struct x86_emulate_ctxt *ctxt)
2289{
2290 struct decode_cache *c = &ctxt->decode;
2291
2292 c->dst.type = OP_REG;
2293 c->dst.bytes = c->src.bytes;
2294 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2295 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2296
2297 return X86EMUL_CONTINUE;
2298}
2299
Avi Kivity48bb5d32010-08-18 18:54:34 +03002300static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2301{
2302 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2303 struct decode_cache *c = &ctxt->decode;
2304 u64 tsc = 0;
2305
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002306 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD))
2307 return emulate_gp(ctxt, 0);
Avi Kivity48bb5d32010-08-18 18:54:34 +03002308 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2309 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2310 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2311 return X86EMUL_CONTINUE;
2312}
2313
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002314static int em_mov(struct x86_emulate_ctxt *ctxt)
2315{
2316 struct decode_cache *c = &ctxt->decode;
2317 c->dst.val = c->src.val;
2318 return X86EMUL_CONTINUE;
2319}
2320
Avi Kivity73fba5f2010-07-29 15:11:53 +03002321#define D(_y) { .flags = (_y) }
2322#define N D(0)
2323#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2324#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2325#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2326
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002327#define D2bv(_f) D((_f) | ByteOp), D(_f)
2328#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2329
Avi Kivity6230f7f2010-08-26 18:34:55 +03002330#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2331 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2332 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2333
2334
Avi Kivity73fba5f2010-07-29 15:11:53 +03002335static struct opcode group1[] = {
2336 X7(D(Lock)), N
2337};
2338
2339static struct opcode group1A[] = {
2340 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2341};
2342
2343static struct opcode group3[] = {
2344 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2345 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002346 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002347};
2348
2349static struct opcode group4[] = {
2350 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2351 N, N, N, N, N, N,
2352};
2353
2354static struct opcode group5[] = {
2355 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002356 D(SrcMem | ModRM | Stack),
2357 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002358 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2359 D(SrcMem | ModRM | Stack), N,
2360};
2361
2362static struct group_dual group7 = { {
2363 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2364 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002365 D(SrcMem16 | ModRM | Mov | Priv),
2366 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002367}, {
2368 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2369 D(SrcNone | ModRM | DstMem | Mov), N,
2370 D(SrcMem16 | ModRM | Mov | Priv), N,
2371} };
2372
2373static struct opcode group8[] = {
2374 N, N, N, N,
2375 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2376 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2377};
2378
2379static struct group_dual group9 = { {
2380 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2381}, {
2382 N, N, N, N, N, N, N, N,
2383} };
2384
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002385static struct opcode group11[] = {
2386 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2387};
2388
Avi Kivity73fba5f2010-07-29 15:11:53 +03002389static struct opcode opcode_table[256] = {
2390 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002391 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002392 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2393 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002394 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002395 D(ImplicitOps | Stack | No64), N,
2396 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002397 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002398 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2399 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002400 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002401 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2402 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002403 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002404 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002405 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002406 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002407 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002408 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002409 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002410 /* 0x40 - 0x4F */
2411 X16(D(DstReg)),
2412 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002413 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002414 /* 0x58 - 0x5F */
2415 X8(D(DstReg | Stack)),
2416 /* 0x60 - 0x67 */
2417 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2418 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2419 N, N, N, N,
2420 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002421 I(SrcImm | Mov | Stack, em_push),
2422 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002423 I(SrcImmByte | Mov | Stack, em_push),
2424 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002425 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2426 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002427 /* 0x70 - 0x7F */
2428 X16(D(SrcImmByte)),
2429 /* 0x80 - 0x87 */
2430 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2431 G(DstMem | SrcImm | ModRM | Group, group1),
2432 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2433 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002434 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002435 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002436 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2437 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002438 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002439 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2440 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002441 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002442 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002443 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002444 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002445 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2446 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002447 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2448 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2449 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2450 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002451 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002452 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002453 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2454 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002455 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002456 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002457 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002458 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002459 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002460 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002461 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002462 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2463 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002464 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002465 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002466 /* 0xC8 - 0xCF */
2467 N, N, N, D(ImplicitOps | Stack),
2468 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2469 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002470 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002471 N, N, N, N,
2472 /* 0xD8 - 0xDF */
2473 N, N, N, N, N, N, N, N,
2474 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002475 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002476 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002477 /* 0xE8 - 0xEF */
2478 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2479 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002480 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002481 /* 0xF0 - 0xF7 */
2482 N, N, N, N,
2483 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2484 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002485 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002486 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2487};
2488
2489static struct opcode twobyte_table[256] = {
2490 /* 0x00 - 0x0F */
2491 N, GD(0, &group7), N, N,
2492 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2493 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2494 N, D(ImplicitOps | ModRM), N, N,
2495 /* 0x10 - 0x1F */
2496 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2497 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002498 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2499 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002500 N, N, N, N,
2501 N, N, N, N, N, N, N, N,
2502 /* 0x30 - 0x3F */
Avi Kivity48bb5d32010-08-18 18:54:34 +03002503 D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc),
2504 D(ImplicitOps | Priv), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002505 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2506 N, N, N, N, N, N, N, N,
2507 /* 0x40 - 0x4F */
2508 X16(D(DstReg | SrcMem | ModRM | Mov)),
2509 /* 0x50 - 0x5F */
2510 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2511 /* 0x60 - 0x6F */
2512 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2513 /* 0x70 - 0x7F */
2514 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2515 /* 0x80 - 0x8F */
2516 X16(D(SrcImm)),
2517 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002518 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002519 /* 0xA0 - 0xA7 */
2520 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2521 N, D(DstMem | SrcReg | ModRM | BitOp),
2522 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2523 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2524 /* 0xA8 - 0xAF */
2525 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2526 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2527 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2528 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002529 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002530 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002531 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002532 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2533 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2534 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002535 /* 0xB8 - 0xBF */
2536 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002537 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002538 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2539 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002540 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002541 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002542 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002543 N, N, N, GD(0, &group9),
2544 N, N, N, N, N, N, N, N,
2545 /* 0xD0 - 0xDF */
2546 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2547 /* 0xE0 - 0xEF */
2548 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2549 /* 0xF0 - 0xFF */
2550 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2551};
2552
2553#undef D
2554#undef N
2555#undef G
2556#undef GD
2557#undef I
2558
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002559#undef D2bv
2560#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002561#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002562
Avi Kivity39f21ee2010-08-18 19:20:21 +03002563static unsigned imm_size(struct decode_cache *c)
2564{
2565 unsigned size;
2566
2567 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2568 if (size == 8)
2569 size = 4;
2570 return size;
2571}
2572
2573static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2574 unsigned size, bool sign_extension)
2575{
2576 struct decode_cache *c = &ctxt->decode;
2577 struct x86_emulate_ops *ops = ctxt->ops;
2578 int rc = X86EMUL_CONTINUE;
2579
2580 op->type = OP_IMM;
2581 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002582 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002583 /* NB. Immediates are sign-extended as necessary. */
2584 switch (op->bytes) {
2585 case 1:
2586 op->val = insn_fetch(s8, 1, c->eip);
2587 break;
2588 case 2:
2589 op->val = insn_fetch(s16, 2, c->eip);
2590 break;
2591 case 4:
2592 op->val = insn_fetch(s32, 4, c->eip);
2593 break;
2594 }
2595 if (!sign_extension) {
2596 switch (op->bytes) {
2597 case 1:
2598 op->val &= 0xff;
2599 break;
2600 case 2:
2601 op->val &= 0xffff;
2602 break;
2603 case 4:
2604 op->val &= 0xffffffff;
2605 break;
2606 }
2607 }
2608done:
2609 return rc;
2610}
2611
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002612int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002613x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2614{
2615 struct x86_emulate_ops *ops = ctxt->ops;
2616 struct decode_cache *c = &ctxt->decode;
2617 int rc = X86EMUL_CONTINUE;
2618 int mode = ctxt->mode;
2619 int def_op_bytes, def_ad_bytes, dual, goffset;
2620 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002621 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002622
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002623 c->eip = ctxt->eip;
2624 c->fetch.start = c->fetch.end = c->eip;
2625 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2626
2627 switch (mode) {
2628 case X86EMUL_MODE_REAL:
2629 case X86EMUL_MODE_VM86:
2630 case X86EMUL_MODE_PROT16:
2631 def_op_bytes = def_ad_bytes = 2;
2632 break;
2633 case X86EMUL_MODE_PROT32:
2634 def_op_bytes = def_ad_bytes = 4;
2635 break;
2636#ifdef CONFIG_X86_64
2637 case X86EMUL_MODE_PROT64:
2638 def_op_bytes = 4;
2639 def_ad_bytes = 8;
2640 break;
2641#endif
2642 default:
2643 return -1;
2644 }
2645
2646 c->op_bytes = def_op_bytes;
2647 c->ad_bytes = def_ad_bytes;
2648
2649 /* Legacy prefixes. */
2650 for (;;) {
2651 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2652 case 0x66: /* operand-size override */
2653 /* switch between 2/4 bytes */
2654 c->op_bytes = def_op_bytes ^ 6;
2655 break;
2656 case 0x67: /* address-size override */
2657 if (mode == X86EMUL_MODE_PROT64)
2658 /* switch between 4/8 bytes */
2659 c->ad_bytes = def_ad_bytes ^ 12;
2660 else
2661 /* switch between 2/4 bytes */
2662 c->ad_bytes = def_ad_bytes ^ 6;
2663 break;
2664 case 0x26: /* ES override */
2665 case 0x2e: /* CS override */
2666 case 0x36: /* SS override */
2667 case 0x3e: /* DS override */
2668 set_seg_override(c, (c->b >> 3) & 3);
2669 break;
2670 case 0x64: /* FS override */
2671 case 0x65: /* GS override */
2672 set_seg_override(c, c->b & 7);
2673 break;
2674 case 0x40 ... 0x4f: /* REX */
2675 if (mode != X86EMUL_MODE_PROT64)
2676 goto done_prefixes;
2677 c->rex_prefix = c->b;
2678 continue;
2679 case 0xf0: /* LOCK */
2680 c->lock_prefix = 1;
2681 break;
2682 case 0xf2: /* REPNE/REPNZ */
2683 c->rep_prefix = REPNE_PREFIX;
2684 break;
2685 case 0xf3: /* REP/REPE/REPZ */
2686 c->rep_prefix = REPE_PREFIX;
2687 break;
2688 default:
2689 goto done_prefixes;
2690 }
2691
2692 /* Any legacy prefix after a REX prefix nullifies its effect. */
2693
2694 c->rex_prefix = 0;
2695 }
2696
2697done_prefixes:
2698
2699 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002700 if (c->rex_prefix & 8)
2701 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002702
2703 /* Opcode byte(s). */
2704 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002705 /* Two-byte opcode? */
2706 if (c->b == 0x0f) {
2707 c->twobyte = 1;
2708 c->b = insn_fetch(u8, 1, c->eip);
2709 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002710 }
2711 c->d = opcode.flags;
2712
2713 if (c->d & Group) {
2714 dual = c->d & GroupDual;
2715 c->modrm = insn_fetch(u8, 1, c->eip);
2716 --c->eip;
2717
2718 if (c->d & GroupDual) {
2719 g_mod012 = opcode.u.gdual->mod012;
2720 g_mod3 = opcode.u.gdual->mod3;
2721 } else
2722 g_mod012 = g_mod3 = opcode.u.group;
2723
2724 c->d &= ~(Group | GroupDual);
2725
2726 goffset = (c->modrm >> 3) & 7;
2727
2728 if ((c->modrm >> 6) == 3)
2729 opcode = g_mod3[goffset];
2730 else
2731 opcode = g_mod012[goffset];
2732 c->d |= opcode.flags;
2733 }
2734
2735 c->execute = opcode.u.execute;
2736
2737 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02002738 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002739 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002740
2741 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2742 c->op_bytes = 8;
2743
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002744 if (c->d & Op3264) {
2745 if (mode == X86EMUL_MODE_PROT64)
2746 c->op_bytes = 8;
2747 else
2748 c->op_bytes = 4;
2749 }
2750
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002751 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002752 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002753 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002754 if (!c->has_seg_override)
2755 set_seg_override(c, c->modrm_seg);
2756 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002757 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002758 if (rc != X86EMUL_CONTINUE)
2759 goto done;
2760
2761 if (!c->has_seg_override)
2762 set_seg_override(c, VCPU_SREG_DS);
2763
Avi Kivity90de84f2010-11-17 15:28:21 +02002764 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002765
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002766 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02002767 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002768
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002769 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02002770 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002771
2772 /*
2773 * Decode and fetch the source operand: register, memory
2774 * or immediate.
2775 */
2776 switch (c->d & SrcMask) {
2777 case SrcNone:
2778 break;
2779 case SrcReg:
2780 decode_register_operand(&c->src, c, 0);
2781 break;
2782 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002783 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002784 goto srcmem_common;
2785 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002786 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002787 goto srcmem_common;
2788 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002789 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002790 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002791 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002792 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002793 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002794 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002795 rc = decode_imm(ctxt, &c->src, 2, false);
2796 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002797 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002798 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2799 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002800 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002801 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002802 break;
2803 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002804 rc = decode_imm(ctxt, &c->src, 1, true);
2805 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002806 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002807 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002808 break;
2809 case SrcAcc:
2810 c->src.type = OP_REG;
2811 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002812 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002813 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002814 break;
2815 case SrcOne:
2816 c->src.bytes = 1;
2817 c->src.val = 1;
2818 break;
2819 case SrcSI:
2820 c->src.type = OP_MEM;
2821 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002822 c->src.addr.mem.ea =
2823 register_address(c, c->regs[VCPU_REGS_RSI]);
2824 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002825 c->src.val = 0;
2826 break;
2827 case SrcImmFAddr:
2828 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002829 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002830 c->src.bytes = c->op_bytes + 2;
2831 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2832 break;
2833 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002834 memop.bytes = c->op_bytes + 2;
2835 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002836 break;
2837 }
2838
Avi Kivity39f21ee2010-08-18 19:20:21 +03002839 if (rc != X86EMUL_CONTINUE)
2840 goto done;
2841
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002842 /*
2843 * Decode and fetch the second source operand: register, memory
2844 * or immediate.
2845 */
2846 switch (c->d & Src2Mask) {
2847 case Src2None:
2848 break;
2849 case Src2CL:
2850 c->src2.bytes = 1;
2851 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2852 break;
2853 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002854 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002855 break;
2856 case Src2One:
2857 c->src2.bytes = 1;
2858 c->src2.val = 1;
2859 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03002860 case Src2Imm:
2861 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
2862 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002863 }
2864
Avi Kivity39f21ee2010-08-18 19:20:21 +03002865 if (rc != X86EMUL_CONTINUE)
2866 goto done;
2867
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002868 /* Decode and fetch the destination operand: register or memory. */
2869 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002870 case DstReg:
2871 decode_register_operand(&c->dst, c,
2872 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2873 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08002874 case DstImmUByte:
2875 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002876 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08002877 c->dst.bytes = 1;
2878 c->dst.val = insn_fetch(u8, 1, c->eip);
2879 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002880 case DstMem:
2881 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002882 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002883 if ((c->d & DstMask) == DstMem64)
2884 c->dst.bytes = 8;
2885 else
2886 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002887 if (c->d & BitOp)
2888 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002889 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002890 break;
2891 case DstAcc:
2892 c->dst.type = OP_REG;
2893 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002894 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002895 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002896 c->dst.orig_val = c->dst.val;
2897 break;
2898 case DstDI:
2899 c->dst.type = OP_MEM;
2900 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002901 c->dst.addr.mem.ea =
2902 register_address(c, c->regs[VCPU_REGS_RDI]);
2903 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002904 c->dst.val = 0;
2905 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002906 case ImplicitOps:
2907 /* Special instructions do their own operand decoding. */
2908 default:
2909 c->dst.type = OP_NONE; /* Disable writeback. */
2910 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002911 }
2912
2913done:
2914 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2915}
2916
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03002917static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
2918{
2919 struct decode_cache *c = &ctxt->decode;
2920
2921 /* The second termination condition only applies for REPE
2922 * and REPNE. Test if the repeat string operation prefix is
2923 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2924 * corresponding termination condition according to:
2925 * - if REPE/REPZ and ZF = 0 then done
2926 * - if REPNE/REPNZ and ZF = 1 then done
2927 */
2928 if (((c->b == 0xa6) || (c->b == 0xa7) ||
2929 (c->b == 0xae) || (c->b == 0xaf))
2930 && (((c->rep_prefix == REPE_PREFIX) &&
2931 ((ctxt->eflags & EFLG_ZF) == 0))
2932 || ((c->rep_prefix == REPNE_PREFIX) &&
2933 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
2934 return true;
2935
2936 return false;
2937}
2938
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002939int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002940x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002941{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002942 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002943 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002944 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002945 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002946 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002947 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002948
Gleb Natapov9de41572010-04-28 19:15:22 +03002949 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002950
Gleb Natapov11616242010-02-11 14:43:14 +02002951 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002952 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002953 goto done;
2954 }
2955
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002956 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002957 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002958 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002959 goto done;
2960 }
2961
Avi Kivity081bca02010-08-26 11:06:15 +03002962 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002963 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03002964 goto done;
2965 }
2966
Gleb Natapove92805a2010-02-10 14:21:35 +02002967 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002968 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002969 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002970 goto done;
2971 }
2972
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002973 if (c->rep_prefix && (c->d & String)) {
2974 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002975 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03002976 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002977 goto done;
2978 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002979 }
2980
Wei Yongjunc483c022010-08-06 15:36:36 +08002981 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02002982 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03002983 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002984 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002985 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002986 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002987 }
2988
Gleb Natapove35b7b92010-02-25 16:36:42 +02002989 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02002990 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03002991 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002992 if (rc != X86EMUL_CONTINUE)
2993 goto done;
2994 }
2995
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002996 if ((c->d & DstMask) == ImplicitOps)
2997 goto special_insn;
2998
2999
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003000 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3001 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003002 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003003 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003004 if (rc != X86EMUL_CONTINUE)
3005 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003006 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003007 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003008
Avi Kivity018a98d2007-11-27 19:30:56 +02003009special_insn:
3010
Avi Kivityef65c882010-07-29 15:11:51 +03003011 if (c->execute) {
3012 rc = c->execute(ctxt);
3013 if (rc != X86EMUL_CONTINUE)
3014 goto done;
3015 goto writeback;
3016 }
3017
Laurent Viviere4e03de2007-09-18 11:52:50 +02003018 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 goto twobyte_insn;
3020
Laurent Viviere4e03de2007-09-18 11:52:50 +02003021 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 case 0x00 ... 0x05:
3023 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003024 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003026 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003027 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003028 break;
3029 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003030 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003031 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032 case 0x08 ... 0x0d:
3033 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003034 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003036 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003037 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003038 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 case 0x10 ... 0x15:
3040 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003041 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003043 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003044 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003045 break;
3046 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003047 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003048 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049 case 0x18 ... 0x1d:
3050 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003051 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003053 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003054 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003055 break;
3056 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003057 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003058 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003059 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003061 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062 break;
3063 case 0x28 ... 0x2d:
3064 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003065 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 break;
3067 case 0x30 ... 0x35:
3068 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003069 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070 break;
3071 case 0x38 ... 0x3d:
3072 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003073 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003075 case 0x40 ... 0x47: /* inc r16/r32 */
3076 emulate_1op("inc", c->dst, ctxt->eflags);
3077 break;
3078 case 0x48 ... 0x4f: /* dec r16/r32 */
3079 emulate_1op("dec", c->dst, ctxt->eflags);
3080 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003081 case 0x58 ... 0x5f: /* pop reg */
3082 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003083 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003084 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003085 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003086 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003087 break;
3088 case 0x61: /* popa */
3089 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003090 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003092 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003094 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003096 case 0x6c: /* insb */
3097 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003098 c->src.val = c->regs[VCPU_REGS_RDX];
3099 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003100 case 0x6e: /* outsb */
3101 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003102 c->dst.val = c->regs[VCPU_REGS_RDX];
3103 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003104 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003105 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003106 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003107 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003108 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003110 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 case 0:
3112 goto add;
3113 case 1:
3114 goto or;
3115 case 2:
3116 goto adc;
3117 case 3:
3118 goto sbb;
3119 case 4:
3120 goto and;
3121 case 5:
3122 goto sub;
3123 case 6:
3124 goto xor;
3125 case 7:
3126 goto cmp;
3127 }
3128 break;
3129 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003130 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003131 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 break;
3133 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003134 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003136 c->src.val = c->dst.val;
3137 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 /*
3139 * Write back the memory destination with implicit LOCK
3140 * prefix.
3141 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003142 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003143 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003145 case 0x8c: /* mov r/m, sreg */
3146 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003147 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003148 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003149 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003150 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003151 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003152 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003153 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003154 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003155 case 0x8e: { /* mov seg, r/m16 */
3156 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003157
3158 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003159
Gleb Natapovc6975182010-02-18 12:15:01 +02003160 if (c->modrm_reg == VCPU_SREG_CS ||
3161 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003162 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003163 goto done;
3164 }
3165
Glauber Costa310b5d32009-05-12 16:21:06 -04003166 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003167 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003168
Gleb Natapov2e873022010-03-18 15:20:18 +02003169 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003170
3171 c->dst.type = OP_NONE; /* Disable writeback. */
3172 break;
3173 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003175 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003177 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3178 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003179 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003180 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003181 case 0x98: /* cbw/cwde/cdqe */
3182 switch (c->op_bytes) {
3183 case 2: c->dst.val = (s8)c->dst.val; break;
3184 case 4: c->dst.val = (s16)c->dst.val; break;
3185 case 8: c->dst.val = (s32)c->dst.val; break;
3186 }
3187 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003188 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003189 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003190 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003191 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003192 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003193 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003194 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003195 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003196 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003197 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003199 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003200 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003201 case 0xa8 ... 0xa9: /* test ax, imm */
3202 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003204 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003205 case 0xc0 ... 0xc1:
3206 emulate_grp2(ctxt);
3207 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003208 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003209 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003210 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003211 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003212 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003213 case 0xc4: /* les */
3214 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003215 break;
3216 case 0xc5: /* lds */
3217 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003218 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003219 case 0xcb: /* ret far */
3220 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003221 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003222 case 0xcc: /* int3 */
3223 irq = 3;
3224 goto do_interrupt;
3225 case 0xcd: /* int n */
3226 irq = c->src.val;
3227 do_interrupt:
3228 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003229 break;
3230 case 0xce: /* into */
3231 if (ctxt->eflags & EFLG_OF) {
3232 irq = 4;
3233 goto do_interrupt;
3234 }
3235 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003236 case 0xcf: /* iret */
3237 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003238 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003239 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003240 emulate_grp2(ctxt);
3241 break;
3242 case 0xd2 ... 0xd3: /* Grp2 */
3243 c->src.val = c->regs[VCPU_REGS_RCX];
3244 emulate_grp2(ctxt);
3245 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003246 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3247 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3248 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3249 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3250 jmp_rel(c, c->src.val);
3251 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003252 case 0xe3: /* jcxz/jecxz/jrcxz */
3253 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3254 jmp_rel(c, c->src.val);
3255 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003256 case 0xe4: /* inb */
3257 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003258 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003259 case 0xe6: /* outb */
3260 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003261 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003262 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003263 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003264 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003265 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003266 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003267 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003268 }
3269 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003270 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003271 case 0xea: { /* jmp far */
3272 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003273 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003274 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3275
3276 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003277 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003278
Gleb Natapov414e6272010-04-28 19:15:26 +03003279 c->eip = 0;
3280 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003281 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003282 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003283 case 0xeb:
3284 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003285 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003286 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003287 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003288 case 0xec: /* in al,dx */
3289 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003290 c->src.val = c->regs[VCPU_REGS_RDX];
3291 do_io_in:
3292 c->dst.bytes = min(c->dst.bytes, 4u);
3293 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003294 rc = emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003295 goto done;
3296 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003297 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3298 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003299 goto done; /* IO is needed */
3300 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003301 case 0xee: /* out dx,al */
3302 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003303 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003304 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003305 c->src.bytes = min(c->src.bytes, 4u);
3306 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3307 c->src.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003308 rc = emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003309 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003310 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003311 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3312 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003313 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003314 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003315 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003316 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003317 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003318 case 0xf5: /* cmc */
3319 /* complement carry flag from eflags reg */
3320 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003321 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003322 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003323 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003324 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003325 case 0xf8: /* clc */
3326 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003327 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003328 case 0xf9: /* stc */
3329 ctxt->eflags |= EFLG_CF;
3330 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003331 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003332 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003333 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003334 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003335 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003336 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003337 break;
3338 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003339 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003340 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003341 goto done;
3342 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003343 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003344 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003345 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003346 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003347 case 0xfc: /* cld */
3348 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003349 break;
3350 case 0xfd: /* std */
3351 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003352 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003353 case 0xfe: /* Grp4 */
3354 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003355 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003356 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003357 case 0xff: /* Grp5 */
3358 if (c->modrm_reg == 5)
3359 goto jump_far;
3360 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003361 default:
3362 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003364
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003365 if (rc != X86EMUL_CONTINUE)
3366 goto done;
3367
Avi Kivity018a98d2007-11-27 19:30:56 +02003368writeback:
3369 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003370 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003371 goto done;
3372
Gleb Natapov5cd21912010-03-18 15:20:26 +02003373 /*
3374 * restore dst type in case the decoding will be reused
3375 * (happens for string instruction )
3376 */
3377 c->dst.type = saved_dst_type;
3378
Gleb Natapova682e352010-03-18 15:20:21 +02003379 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003380 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003381 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003382
3383 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003384 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003385 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003386
Gleb Natapov5cd21912010-03-18 15:20:26 +02003387 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003388 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003389 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003390
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003391 if (!string_insn_completed(ctxt)) {
3392 /*
3393 * Re-enter guest when pio read ahead buffer is empty
3394 * or, if it is not used, after each 1024 iteration.
3395 */
3396 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3397 (r->end == 0 || r->end != r->pos)) {
3398 /*
3399 * Reset read cache. Usually happens before
3400 * decode, but since instruction is restarted
3401 * we have to do it here.
3402 */
3403 ctxt->decode.mem_read.end = 0;
3404 return EMULATION_RESTART;
3405 }
3406 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003407 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003408 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003409
3410 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003411
3412done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003413 if (rc == X86EMUL_PROPAGATE_FAULT)
3414 ctxt->have_exception = true;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003415 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416
3417twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003418 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003420 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 u16 size;
3422 unsigned long address;
3423
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003424 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003425 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003426 goto cannot_emulate;
3427
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003428 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003429 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003430 goto done;
3431
Avi Kivity33e38852008-05-21 15:34:25 +03003432 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003433 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003434 /* Disable writeback. */
3435 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003436 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003438 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003439 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003440 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 goto done;
3442 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003443 /* Disable writeback. */
3444 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003446 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003447 if (c->modrm_mod == 3) {
3448 switch (c->modrm_rm) {
3449 case 1:
3450 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003451 break;
3452 default:
3453 goto cannot_emulate;
3454 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003455 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003456 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003457 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003458 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003459 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003460 goto done;
3461 realmode_lidt(ctxt->vcpu, size, address);
3462 }
Avi Kivity16286d02008-04-14 14:40:50 +03003463 /* Disable writeback. */
3464 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465 break;
3466 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003467 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003468 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469 break;
3470 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003471 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003472 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003473 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003475 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003476 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003477 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003478 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003480 emulate_invlpg(ctxt->vcpu,
3481 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003482 /* Disable writeback. */
3483 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484 break;
3485 default:
3486 goto cannot_emulate;
3487 }
3488 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003489 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003490 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003491 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003492 case 0x06:
3493 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003494 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003495 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003496 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003497 break;
3498 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003499 case 0x0d: /* GrpP (prefetch) */
3500 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003501 break;
3502 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003503 switch (c->modrm_reg) {
3504 case 1:
3505 case 5 ... 7:
3506 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003507 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003508 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003509 goto done;
3510 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003511 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003512 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003514 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3515 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003516 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003517 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003518 goto done;
3519 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003520 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003522 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003523 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003524 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003525 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03003526 goto done;
3527 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003528 c->dst.type = OP_NONE;
3529 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003530 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003531 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3532 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003533 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003534 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003535 goto done;
3536 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003537
Avi Kivityb27f3852010-08-01 14:25:22 +03003538 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003539 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3540 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3541 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003542 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003543 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03003544 goto done;
3545 }
3546
Laurent Viviera01af5e2007-09-24 11:10:56 +02003547 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003549 case 0x30:
3550 /* wrmsr */
3551 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3552 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003553 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003554 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003555 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003556 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003557 }
3558 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003559 break;
3560 case 0x32:
3561 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003562 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003563 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003564 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003565 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003566 } else {
3567 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3568 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3569 }
3570 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003571 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003572 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003573 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003574 break;
3575 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003576 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003577 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003579 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003580 if (!test_cc(c->b, ctxt->eflags))
3581 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003582 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003583 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003584 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003585 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003586 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003587 case 0x90 ... 0x9f: /* setcc r/m8 */
3588 c->dst.val = test_cc(c->b, ctxt->eflags);
3589 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003590 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003591 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003592 break;
3593 case 0xa1: /* pop fs */
3594 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003595 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003596 case 0xa3:
3597 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003598 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003599 /* only subword offset */
3600 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003601 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003602 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003603 case 0xa4: /* shld imm8, r, r/m */
3604 case 0xa5: /* shld cl, r, r/m */
3605 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3606 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003607 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003608 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003609 break;
3610 case 0xa9: /* pop gs */
3611 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003612 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003613 case 0xab:
3614 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003615 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003616 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003617 case 0xac: /* shrd imm8, r, r/m */
3618 case 0xad: /* shrd cl, r, r/m */
3619 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3620 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003621 case 0xae: /* clflush */
3622 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003623 case 0xb0 ... 0xb1: /* cmpxchg */
3624 /*
3625 * Save real source value, then compare EAX against
3626 * destination.
3627 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003628 c->src.orig_val = c->src.val;
3629 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003630 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3631 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003632 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003633 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634 } else {
3635 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003636 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003637 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638 }
3639 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003640 case 0xb2: /* lss */
3641 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003642 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643 case 0xb3:
3644 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003645 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003647 case 0xb4: /* lfs */
3648 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003649 break;
3650 case 0xb5: /* lgs */
3651 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003652 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003653 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003654 c->dst.bytes = c->op_bytes;
3655 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3656 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003659 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660 case 0:
3661 goto bt;
3662 case 1:
3663 goto bts;
3664 case 2:
3665 goto btr;
3666 case 3:
3667 goto btc;
3668 }
3669 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003670 case 0xbb:
3671 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003672 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003673 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003674 case 0xbc: { /* bsf */
3675 u8 zf;
3676 __asm__ ("bsf %2, %0; setz %1"
3677 : "=r"(c->dst.val), "=q"(zf)
3678 : "r"(c->src.val));
3679 ctxt->eflags &= ~X86_EFLAGS_ZF;
3680 if (zf) {
3681 ctxt->eflags |= X86_EFLAGS_ZF;
3682 c->dst.type = OP_NONE; /* Disable writeback. */
3683 }
3684 break;
3685 }
3686 case 0xbd: { /* bsr */
3687 u8 zf;
3688 __asm__ ("bsr %2, %0; setz %1"
3689 : "=r"(c->dst.val), "=q"(zf)
3690 : "r"(c->src.val));
3691 ctxt->eflags &= ~X86_EFLAGS_ZF;
3692 if (zf) {
3693 ctxt->eflags |= X86_EFLAGS_ZF;
3694 c->dst.type = OP_NONE; /* Disable writeback. */
3695 }
3696 break;
3697 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003698 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003699 c->dst.bytes = c->op_bytes;
3700 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3701 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003702 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003703 case 0xc0 ... 0xc1: /* xadd */
3704 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3705 /* Write back the register source. */
3706 c->src.val = c->dst.orig_val;
3707 write_register_operand(&c->src);
3708 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003709 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003710 c->dst.bytes = c->op_bytes;
3711 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3712 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003713 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003715 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003716 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003717 default:
3718 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003720
3721 if (rc != X86EMUL_CONTINUE)
3722 goto done;
3723
Avi Kivity6aa8b732006-12-10 02:21:36 -08003724 goto writeback;
3725
3726cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727 return -1;
3728}