blob: e96705542634b2dfa33db23ba38065ffd072504e [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
Gleb Natapov54b84862010-04-28 19:15:44 +0300469static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
470 u32 error, bool valid)
471{
472 ctxt->exception = vec;
473 ctxt->error_code = error;
474 ctxt->error_code_valid = valid;
Gleb Natapov54b84862010-04-28 19:15:44 +0300475}
476
477static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
478{
479 emulate_exception(ctxt, GP_VECTOR, err, true);
480}
481
Joerg Roedel8df25a32010-09-10 17:30:46 +0200482static void emulate_pf(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300483{
Joerg Roedel8df25a32010-09-10 17:30:46 +0200484 emulate_exception(ctxt, PF_VECTOR, 0, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300485}
486
487static void emulate_ud(struct x86_emulate_ctxt *ctxt)
488{
489 emulate_exception(ctxt, UD_VECTOR, 0, false);
490}
491
492static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
493{
494 emulate_exception(ctxt, TS_VECTOR, err, true);
495}
496
Avi Kivity34d1f492010-08-26 11:59:01 +0300497static int emulate_de(struct x86_emulate_ctxt *ctxt)
498{
499 emulate_exception(ctxt, DE_VECTOR, 0, false);
500 return X86EMUL_PROPAGATE_FAULT;
501}
502
Avi Kivity62266862007-11-20 13:15:52 +0200503static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
504 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300505 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200506{
507 struct fetch_cache *fc = &ctxt->decode.fetch;
508 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300509 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200510
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300511 if (eip == fc->end) {
512 cur_size = fc->end - fc->start;
513 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
514 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
515 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900516 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200517 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300518 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200519 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300520 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900521 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200522}
523
524static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
525 struct x86_emulate_ops *ops,
526 unsigned long eip, void *dest, unsigned size)
527{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900528 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200529
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200530 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200531 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200532 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200533 while (size--) {
534 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900535 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200536 return rc;
537 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900538 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200539}
540
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000541/*
542 * Given the 'reg' portion of a ModRM byte, and a register block, return a
543 * pointer into the block that addresses the relevant register.
544 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
545 */
546static void *decode_register(u8 modrm_reg, unsigned long *regs,
547 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548{
549 void *p;
550
551 p = &regs[modrm_reg];
552 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
553 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
554 return p;
555}
556
557static int read_descriptor(struct x86_emulate_ctxt *ctxt,
558 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200559 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560 u16 *size, unsigned long *address, int op_bytes)
561{
562 int rc;
563
564 if (op_bytes == 2)
565 op_bytes = 3;
566 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200567 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
568 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900569 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 return rc;
Avi Kivity90de84f2010-11-17 15:28:21 +0200571 rc = ops->read_std(linear(ctxt, addr) + 2, address, op_bytes,
572 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 return rc;
574}
575
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300576static int test_cc(unsigned int condition, unsigned int flags)
577{
578 int rc = 0;
579
580 switch ((condition & 15) >> 1) {
581 case 0: /* o */
582 rc |= (flags & EFLG_OF);
583 break;
584 case 1: /* b/c/nae */
585 rc |= (flags & EFLG_CF);
586 break;
587 case 2: /* z/e */
588 rc |= (flags & EFLG_ZF);
589 break;
590 case 3: /* be/na */
591 rc |= (flags & (EFLG_CF|EFLG_ZF));
592 break;
593 case 4: /* s */
594 rc |= (flags & EFLG_SF);
595 break;
596 case 5: /* p/pe */
597 rc |= (flags & EFLG_PF);
598 break;
599 case 7: /* le/ng */
600 rc |= (flags & EFLG_ZF);
601 /* fall through */
602 case 6: /* l/nge */
603 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
604 break;
605 }
606
607 /* Odd condition identifiers (lsb == 1) have inverted sense. */
608 return (!!rc ^ (condition & 1));
609}
610
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300611static void fetch_register_operand(struct operand *op)
612{
613 switch (op->bytes) {
614 case 1:
615 op->val = *(u8 *)op->addr.reg;
616 break;
617 case 2:
618 op->val = *(u16 *)op->addr.reg;
619 break;
620 case 4:
621 op->val = *(u32 *)op->addr.reg;
622 break;
623 case 8:
624 op->val = *(u64 *)op->addr.reg;
625 break;
626 }
627}
628
Avi Kivity3c118e22007-10-31 10:27:04 +0200629static void decode_register_operand(struct operand *op,
630 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200631 int inhibit_bytereg)
632{
Avi Kivity33615aa2007-10-31 11:15:56 +0200633 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200634 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200635
636 if (!(c->d & ModRM))
637 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200638 op->type = OP_REG;
639 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300640 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200641 op->bytes = 1;
642 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300643 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200644 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200645 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300646 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200647 op->orig_val = op->val;
648}
649
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200650static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300651 struct x86_emulate_ops *ops,
652 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200653{
654 struct decode_cache *c = &ctxt->decode;
655 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700656 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900657 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300658 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200659
660 if (c->rex_prefix) {
661 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
662 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
663 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
664 }
665
666 c->modrm = insn_fetch(u8, 1, c->eip);
667 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
668 c->modrm_reg |= (c->modrm & 0x38) >> 3;
669 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300670 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200671
672 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300673 op->type = OP_REG;
674 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
675 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300676 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300677 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200678 return rc;
679 }
680
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300681 op->type = OP_MEM;
682
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200683 if (c->ad_bytes == 2) {
684 unsigned bx = c->regs[VCPU_REGS_RBX];
685 unsigned bp = c->regs[VCPU_REGS_RBP];
686 unsigned si = c->regs[VCPU_REGS_RSI];
687 unsigned di = c->regs[VCPU_REGS_RDI];
688
689 /* 16-bit ModR/M decode. */
690 switch (c->modrm_mod) {
691 case 0:
692 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300693 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200694 break;
695 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300696 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200697 break;
698 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300699 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200700 break;
701 }
702 switch (c->modrm_rm) {
703 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300704 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200705 break;
706 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300707 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200708 break;
709 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300710 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200711 break;
712 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300713 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200714 break;
715 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300716 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200717 break;
718 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300719 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720 break;
721 case 6:
722 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300723 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200724 break;
725 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300726 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200727 break;
728 }
729 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
730 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300731 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300732 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200733 } else {
734 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700735 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200736 sib = insn_fetch(u8, 1, c->eip);
737 index_reg |= (sib >> 3) & 7;
738 base_reg |= sib & 7;
739 scale = sib >> 6;
740
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700741 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300742 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700743 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300744 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700745 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300746 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700747 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
748 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700749 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700750 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300751 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200752 switch (c->modrm_mod) {
753 case 0:
754 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300755 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200756 break;
757 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300758 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200759 break;
760 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300761 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200762 break;
763 }
764 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200765 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200766done:
767 return rc;
768}
769
770static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300771 struct x86_emulate_ops *ops,
772 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200773{
774 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900775 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200776
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300777 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200778 switch (c->ad_bytes) {
779 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200780 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200781 break;
782 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200783 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200784 break;
785 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200786 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200787 break;
788 }
789done:
790 return rc;
791}
792
Wei Yongjun35c843c2010-08-09 11:34:56 +0800793static void fetch_bit_operand(struct decode_cache *c)
794{
Sheng Yang7129eec2010-09-28 16:33:32 +0800795 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800796
Wei Yongjun3885f182010-08-09 11:37:37 +0800797 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800798 mask = ~(c->dst.bytes * 8 - 1);
799
800 if (c->src.bytes == 2)
801 sv = (s16)c->src.val & (s16)mask;
802 else if (c->src.bytes == 4)
803 sv = (s32)c->src.val & (s32)mask;
804
Avi Kivity90de84f2010-11-17 15:28:21 +0200805 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800806 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800807
808 /* only subword offset */
809 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800810}
811
Gleb Natapov9de41572010-04-28 19:15:22 +0300812static int read_emulated(struct x86_emulate_ctxt *ctxt,
813 struct x86_emulate_ops *ops,
814 unsigned long addr, void *dest, unsigned size)
815{
816 int rc;
817 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300818 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300819
820 while (size) {
821 int n = min(size, 8u);
822 size -= n;
823 if (mc->pos < mc->end)
824 goto read_cached;
825
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300826 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
827 ctxt->vcpu);
828 if (rc == X86EMUL_PROPAGATE_FAULT)
Joerg Roedel8df25a32010-09-10 17:30:46 +0200829 emulate_pf(ctxt);
Gleb Natapov9de41572010-04-28 19:15:22 +0300830 if (rc != X86EMUL_CONTINUE)
831 return rc;
832 mc->end += n;
833
834 read_cached:
835 memcpy(dest, mc->data + mc->pos, n);
836 mc->pos += n;
837 dest += n;
838 addr += n;
839 }
840 return X86EMUL_CONTINUE;
841}
842
Gleb Natapov7b262e92010-03-18 15:20:27 +0200843static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
844 struct x86_emulate_ops *ops,
845 unsigned int size, unsigned short port,
846 void *dest)
847{
848 struct read_cache *rc = &ctxt->decode.io_read;
849
850 if (rc->pos == rc->end) { /* refill pio read ahead */
851 struct decode_cache *c = &ctxt->decode;
852 unsigned int in_page, n;
853 unsigned int count = c->rep_prefix ?
854 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
855 in_page = (ctxt->eflags & EFLG_DF) ?
856 offset_in_page(c->regs[VCPU_REGS_RDI]) :
857 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
858 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
859 count);
860 if (n == 0)
861 n = 1;
862 rc->pos = rc->end = 0;
863 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
864 return 0;
865 rc->end = n * size;
866 }
867
868 memcpy(dest, rc->data + rc->pos, size);
869 rc->pos += size;
870 return 1;
871}
872
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200873static u32 desc_limit_scaled(struct desc_struct *desc)
874{
875 u32 limit = get_desc_limit(desc);
876
877 return desc->g ? (limit << 12) | 0xfff : limit;
878}
879
880static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
881 struct x86_emulate_ops *ops,
882 u16 selector, struct desc_ptr *dt)
883{
884 if (selector & 1 << 2) {
885 struct desc_struct desc;
886 memset (dt, 0, sizeof *dt);
887 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
888 return;
889
890 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
891 dt->address = get_desc_base(&desc);
892 } else
893 ops->get_gdt(dt, ctxt->vcpu);
894}
895
896/* allowed just for 8 bytes segments */
897static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
898 struct x86_emulate_ops *ops,
899 u16 selector, struct desc_struct *desc)
900{
901 struct desc_ptr dt;
902 u16 index = selector >> 3;
903 int ret;
904 u32 err;
905 ulong addr;
906
907 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
908
909 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300910 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200911 return X86EMUL_PROPAGATE_FAULT;
912 }
913 addr = dt.address + index * 8;
914 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
915 if (ret == X86EMUL_PROPAGATE_FAULT)
Joerg Roedel8df25a32010-09-10 17:30:46 +0200916 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200917
918 return ret;
919}
920
921/* allowed just for 8 bytes segments */
922static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
923 struct x86_emulate_ops *ops,
924 u16 selector, struct desc_struct *desc)
925{
926 struct desc_ptr dt;
927 u16 index = selector >> 3;
928 u32 err;
929 ulong addr;
930 int ret;
931
932 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
933
934 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300935 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200936 return X86EMUL_PROPAGATE_FAULT;
937 }
938
939 addr = dt.address + index * 8;
940 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
941 if (ret == X86EMUL_PROPAGATE_FAULT)
Joerg Roedel8df25a32010-09-10 17:30:46 +0200942 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200943
944 return ret;
945}
946
947static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
948 struct x86_emulate_ops *ops,
949 u16 selector, int seg)
950{
951 struct desc_struct seg_desc;
952 u8 dpl, rpl, cpl;
953 unsigned err_vec = GP_VECTOR;
954 u32 err_code = 0;
955 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
956 int ret;
957
958 memset(&seg_desc, 0, sizeof seg_desc);
959
960 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
961 || ctxt->mode == X86EMUL_MODE_REAL) {
962 /* set real mode segment descriptor */
963 set_desc_base(&seg_desc, selector << 4);
964 set_desc_limit(&seg_desc, 0xffff);
965 seg_desc.type = 3;
966 seg_desc.p = 1;
967 seg_desc.s = 1;
968 goto load;
969 }
970
971 /* NULL selector is not valid for TR, CS and SS */
972 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
973 && null_selector)
974 goto exception;
975
976 /* TR should be in GDT only */
977 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
978 goto exception;
979
980 if (null_selector) /* for NULL selector skip all following checks */
981 goto load;
982
983 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
984 if (ret != X86EMUL_CONTINUE)
985 return ret;
986
987 err_code = selector & 0xfffc;
988 err_vec = GP_VECTOR;
989
990 /* can't load system descriptor into segment selecor */
991 if (seg <= VCPU_SREG_GS && !seg_desc.s)
992 goto exception;
993
994 if (!seg_desc.p) {
995 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
996 goto exception;
997 }
998
999 rpl = selector & 3;
1000 dpl = seg_desc.dpl;
1001 cpl = ops->cpl(ctxt->vcpu);
1002
1003 switch (seg) {
1004 case VCPU_SREG_SS:
1005 /*
1006 * segment is not a writable data segment or segment
1007 * selector's RPL != CPL or segment selector's RPL != CPL
1008 */
1009 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1010 goto exception;
1011 break;
1012 case VCPU_SREG_CS:
1013 if (!(seg_desc.type & 8))
1014 goto exception;
1015
1016 if (seg_desc.type & 4) {
1017 /* conforming */
1018 if (dpl > cpl)
1019 goto exception;
1020 } else {
1021 /* nonconforming */
1022 if (rpl > cpl || dpl != cpl)
1023 goto exception;
1024 }
1025 /* CS(RPL) <- CPL */
1026 selector = (selector & 0xfffc) | cpl;
1027 break;
1028 case VCPU_SREG_TR:
1029 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1030 goto exception;
1031 break;
1032 case VCPU_SREG_LDTR:
1033 if (seg_desc.s || seg_desc.type != 2)
1034 goto exception;
1035 break;
1036 default: /* DS, ES, FS, or GS */
1037 /*
1038 * segment is not a data or readable code segment or
1039 * ((segment is a data or nonconforming code segment)
1040 * and (both RPL and CPL > DPL))
1041 */
1042 if ((seg_desc.type & 0xa) == 0x8 ||
1043 (((seg_desc.type & 0xc) != 0xc) &&
1044 (rpl > dpl && cpl > dpl)))
1045 goto exception;
1046 break;
1047 }
1048
1049 if (seg_desc.s) {
1050 /* mark segment as accessed */
1051 seg_desc.type |= 1;
1052 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1053 if (ret != X86EMUL_CONTINUE)
1054 return ret;
1055 }
1056load:
1057 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1058 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1059 return X86EMUL_CONTINUE;
1060exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001061 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001062 return X86EMUL_PROPAGATE_FAULT;
1063}
1064
Wei Yongjun31be40b2010-08-17 09:17:30 +08001065static void write_register_operand(struct operand *op)
1066{
1067 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1068 switch (op->bytes) {
1069 case 1:
1070 *(u8 *)op->addr.reg = (u8)op->val;
1071 break;
1072 case 2:
1073 *(u16 *)op->addr.reg = (u16)op->val;
1074 break;
1075 case 4:
1076 *op->addr.reg = (u32)op->val;
1077 break; /* 64b: zero-extend */
1078 case 8:
1079 *op->addr.reg = op->val;
1080 break;
1081 }
1082}
1083
Wei Yongjunc37eda12010-06-15 09:03:33 +08001084static inline int writeback(struct x86_emulate_ctxt *ctxt,
1085 struct x86_emulate_ops *ops)
1086{
1087 int rc;
1088 struct decode_cache *c = &ctxt->decode;
1089 u32 err;
1090
1091 switch (c->dst.type) {
1092 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001093 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001094 break;
1095 case OP_MEM:
1096 if (c->lock_prefix)
1097 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001098 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001099 &c->dst.orig_val,
1100 &c->dst.val,
1101 c->dst.bytes,
1102 &err,
1103 ctxt->vcpu);
1104 else
1105 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001106 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001107 &c->dst.val,
1108 c->dst.bytes,
1109 &err,
1110 ctxt->vcpu);
1111 if (rc == X86EMUL_PROPAGATE_FAULT)
Joerg Roedel8df25a32010-09-10 17:30:46 +02001112 emulate_pf(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001113 if (rc != X86EMUL_CONTINUE)
1114 return rc;
1115 break;
1116 case OP_NONE:
1117 /* no writeback */
1118 break;
1119 default:
1120 break;
1121 }
1122 return X86EMUL_CONTINUE;
1123}
1124
Gleb Natapov79168fd2010-04-28 19:15:30 +03001125static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1126 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001127{
1128 struct decode_cache *c = &ctxt->decode;
1129
1130 c->dst.type = OP_MEM;
1131 c->dst.bytes = c->op_bytes;
1132 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001133 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001134 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1135 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001136}
1137
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001138static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001139 struct x86_emulate_ops *ops,
1140 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001141{
1142 struct decode_cache *c = &ctxt->decode;
1143 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001144 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001145
Avi Kivity90de84f2010-11-17 15:28:21 +02001146 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1147 addr.seg = VCPU_SREG_SS;
1148 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001149 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001150 return rc;
1151
Avi Kivity350f69d2009-01-05 11:12:40 +02001152 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001153 return rc;
1154}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001155
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001156static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1157 struct x86_emulate_ops *ops,
1158 void *dest, int len)
1159{
1160 int rc;
1161 unsigned long val, change_mask;
1162 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001163 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001164
1165 rc = emulate_pop(ctxt, ops, &val, len);
1166 if (rc != X86EMUL_CONTINUE)
1167 return rc;
1168
1169 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1170 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1171
1172 switch(ctxt->mode) {
1173 case X86EMUL_MODE_PROT64:
1174 case X86EMUL_MODE_PROT32:
1175 case X86EMUL_MODE_PROT16:
1176 if (cpl == 0)
1177 change_mask |= EFLG_IOPL;
1178 if (cpl <= iopl)
1179 change_mask |= EFLG_IF;
1180 break;
1181 case X86EMUL_MODE_VM86:
1182 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001183 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001184 return X86EMUL_PROPAGATE_FAULT;
1185 }
1186 change_mask |= EFLG_IF;
1187 break;
1188 default: /* real mode */
1189 change_mask |= (EFLG_IOPL | EFLG_IF);
1190 break;
1191 }
1192
1193 *(unsigned long *)dest =
1194 (ctxt->eflags & ~change_mask) | (val & change_mask);
1195
Joerg Roedeld47f00a2010-09-10 17:30:56 +02001196 if (rc == X86EMUL_PROPAGATE_FAULT)
1197 emulate_pf(ctxt);
1198
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001199 return rc;
1200}
1201
Gleb Natapov79168fd2010-04-28 19:15:30 +03001202static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1203 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001204{
1205 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001206
Gleb Natapov79168fd2010-04-28 19:15:30 +03001207 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001208
Gleb Natapov79168fd2010-04-28 19:15:30 +03001209 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001210}
1211
1212static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1213 struct x86_emulate_ops *ops, int seg)
1214{
1215 struct decode_cache *c = &ctxt->decode;
1216 unsigned long selector;
1217 int rc;
1218
1219 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001220 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001221 return rc;
1222
Gleb Natapov2e873022010-03-18 15:20:18 +02001223 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001224 return rc;
1225}
1226
Wei Yongjunc37eda12010-06-15 09:03:33 +08001227static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001228 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001229{
1230 struct decode_cache *c = &ctxt->decode;
1231 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001232 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001233 int reg = VCPU_REGS_RAX;
1234
1235 while (reg <= VCPU_REGS_RDI) {
1236 (reg == VCPU_REGS_RSP) ?
1237 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1238
Gleb Natapov79168fd2010-04-28 19:15:30 +03001239 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001240
1241 rc = writeback(ctxt, ops);
1242 if (rc != X86EMUL_CONTINUE)
1243 return rc;
1244
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001245 ++reg;
1246 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001247
1248 /* Disable writeback. */
1249 c->dst.type = OP_NONE;
1250
1251 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001252}
1253
1254static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1255 struct x86_emulate_ops *ops)
1256{
1257 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001258 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001259 int reg = VCPU_REGS_RDI;
1260
1261 while (reg >= VCPU_REGS_RAX) {
1262 if (reg == VCPU_REGS_RSP) {
1263 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1264 c->op_bytes);
1265 --reg;
1266 }
1267
1268 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001269 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001270 break;
1271 --reg;
1272 }
1273 return rc;
1274}
1275
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001276int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1277 struct x86_emulate_ops *ops, int irq)
1278{
1279 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001280 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001281 struct desc_ptr dt;
1282 gva_t cs_addr;
1283 gva_t eip_addr;
1284 u16 cs, eip;
1285 u32 err;
1286
1287 /* TODO: Add limit checks */
1288 c->src.val = ctxt->eflags;
1289 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001290 rc = writeback(ctxt, ops);
1291 if (rc != X86EMUL_CONTINUE)
1292 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001293
1294 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1295
1296 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1297 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001298 rc = writeback(ctxt, ops);
1299 if (rc != X86EMUL_CONTINUE)
1300 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001301
1302 c->src.val = c->eip;
1303 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001304 rc = writeback(ctxt, ops);
1305 if (rc != X86EMUL_CONTINUE)
1306 return rc;
1307
1308 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001309
1310 ops->get_idt(&dt, ctxt->vcpu);
1311
1312 eip_addr = dt.address + (irq << 2);
1313 cs_addr = dt.address + (irq << 2) + 2;
1314
1315 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &err);
1316 if (rc != X86EMUL_CONTINUE)
1317 return rc;
1318
1319 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &err);
1320 if (rc != X86EMUL_CONTINUE)
1321 return rc;
1322
1323 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1324 if (rc != X86EMUL_CONTINUE)
1325 return rc;
1326
1327 c->eip = eip;
1328
1329 return rc;
1330}
1331
1332static int emulate_int(struct x86_emulate_ctxt *ctxt,
1333 struct x86_emulate_ops *ops, int irq)
1334{
1335 switch(ctxt->mode) {
1336 case X86EMUL_MODE_REAL:
1337 return emulate_int_real(ctxt, ops, irq);
1338 case X86EMUL_MODE_VM86:
1339 case X86EMUL_MODE_PROT16:
1340 case X86EMUL_MODE_PROT32:
1341 case X86EMUL_MODE_PROT64:
1342 default:
1343 /* Protected mode interrupts unimplemented yet */
1344 return X86EMUL_UNHANDLEABLE;
1345 }
1346}
1347
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001348static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1349 struct x86_emulate_ops *ops)
1350{
1351 struct decode_cache *c = &ctxt->decode;
1352 int rc = X86EMUL_CONTINUE;
1353 unsigned long temp_eip = 0;
1354 unsigned long temp_eflags = 0;
1355 unsigned long cs = 0;
1356 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1357 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1358 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1359 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1360
1361 /* TODO: Add stack limit check */
1362
1363 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1364
1365 if (rc != X86EMUL_CONTINUE)
1366 return rc;
1367
1368 if (temp_eip & ~0xffff) {
1369 emulate_gp(ctxt, 0);
1370 return X86EMUL_PROPAGATE_FAULT;
1371 }
1372
1373 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1374
1375 if (rc != X86EMUL_CONTINUE)
1376 return rc;
1377
1378 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1379
1380 if (rc != X86EMUL_CONTINUE)
1381 return rc;
1382
1383 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1384
1385 if (rc != X86EMUL_CONTINUE)
1386 return rc;
1387
1388 c->eip = temp_eip;
1389
1390
1391 if (c->op_bytes == 4)
1392 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1393 else if (c->op_bytes == 2) {
1394 ctxt->eflags &= ~0xffff;
1395 ctxt->eflags |= temp_eflags;
1396 }
1397
1398 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1399 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1400
1401 return rc;
1402}
1403
1404static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1405 struct x86_emulate_ops* ops)
1406{
1407 switch(ctxt->mode) {
1408 case X86EMUL_MODE_REAL:
1409 return emulate_iret_real(ctxt, ops);
1410 case X86EMUL_MODE_VM86:
1411 case X86EMUL_MODE_PROT16:
1412 case X86EMUL_MODE_PROT32:
1413 case X86EMUL_MODE_PROT64:
1414 default:
1415 /* iret from protected mode unimplemented yet */
1416 return X86EMUL_UNHANDLEABLE;
1417 }
1418}
1419
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001420static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1421 struct x86_emulate_ops *ops)
1422{
1423 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001424
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001425 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001426}
1427
Laurent Vivier05f086f2007-09-24 11:10:55 +02001428static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001429{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001430 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001431 switch (c->modrm_reg) {
1432 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001433 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001434 break;
1435 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001436 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001437 break;
1438 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001439 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001440 break;
1441 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001442 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001443 break;
1444 case 4: /* sal/shl */
1445 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001446 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001447 break;
1448 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001449 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001450 break;
1451 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001452 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001453 break;
1454 }
1455}
1456
1457static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001458 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001459{
1460 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001461 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1462 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001463 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001464
1465 switch (c->modrm_reg) {
1466 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001467 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001468 break;
1469 case 2: /* not */
1470 c->dst.val = ~c->dst.val;
1471 break;
1472 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001473 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001474 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001475 case 4: /* mul */
1476 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1477 break;
1478 case 5: /* imul */
1479 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1480 break;
1481 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001482 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1483 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001484 break;
1485 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001486 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1487 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001488 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001489 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001490 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001491 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001492 if (de)
1493 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001494 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001495}
1496
1497static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001498 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001499{
1500 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001501
1502 switch (c->modrm_reg) {
1503 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001504 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001505 break;
1506 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001507 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001508 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001509 case 2: /* call near abs */ {
1510 long int old_eip;
1511 old_eip = c->eip;
1512 c->eip = c->src.val;
1513 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001514 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001515 break;
1516 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001517 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001518 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001519 break;
1520 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001521 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001522 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001523 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001524 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001525}
1526
1527static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001528 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529{
1530 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001531 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001532
1533 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1534 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001535 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1536 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001537 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001538 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001539 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1540 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001541
Laurent Vivier05f086f2007-09-24 11:10:55 +02001542 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001543 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001544 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001545}
1546
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001547static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1548 struct x86_emulate_ops *ops)
1549{
1550 struct decode_cache *c = &ctxt->decode;
1551 int rc;
1552 unsigned long cs;
1553
1554 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001555 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001556 return rc;
1557 if (c->op_bytes == 4)
1558 c->eip = (u32)c->eip;
1559 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001560 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001561 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001562 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001563 return rc;
1564}
1565
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001566static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1567 struct x86_emulate_ops *ops, int seg)
1568{
1569 struct decode_cache *c = &ctxt->decode;
1570 unsigned short sel;
1571 int rc;
1572
1573 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1574
1575 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1576 if (rc != X86EMUL_CONTINUE)
1577 return rc;
1578
1579 c->dst.val = c->src.val;
1580 return rc;
1581}
1582
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001583static inline void
1584setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001585 struct x86_emulate_ops *ops, struct desc_struct *cs,
1586 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001587{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001588 memset(cs, 0, sizeof(struct desc_struct));
1589 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1590 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001591
1592 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001593 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001594 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001595 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001596 cs->type = 0x0b; /* Read, Execute, Accessed */
1597 cs->s = 1;
1598 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001599 cs->p = 1;
1600 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001601
Gleb Natapov79168fd2010-04-28 19:15:30 +03001602 set_desc_base(ss, 0); /* flat segment */
1603 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001604 ss->g = 1; /* 4kb granularity */
1605 ss->s = 1;
1606 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001608 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001609 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001610}
1611
1612static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001613emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001614{
1615 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001616 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001617 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001618 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001619
1620 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001621 if (ctxt->mode == X86EMUL_MODE_REAL ||
1622 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001623 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001624 return X86EMUL_PROPAGATE_FAULT;
1625 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001626
Gleb Natapov79168fd2010-04-28 19:15:30 +03001627 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001628
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001629 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001630 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001631 cs_sel = (u16)(msr_data & 0xfffc);
1632 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001633
1634 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001635 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001636 cs.l = 1;
1637 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001638 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1639 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1640 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1641 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001642
1643 c->regs[VCPU_REGS_RCX] = c->eip;
1644 if (is_long_mode(ctxt->vcpu)) {
1645#ifdef CONFIG_X86_64
1646 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1647
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001648 ops->get_msr(ctxt->vcpu,
1649 ctxt->mode == X86EMUL_MODE_PROT64 ?
1650 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001651 c->eip = msr_data;
1652
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001653 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001654 ctxt->eflags &= ~(msr_data | EFLG_RF);
1655#endif
1656 } else {
1657 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001658 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001659 c->eip = (u32)msr_data;
1660
1661 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1662 }
1663
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001664 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001665}
1666
Andre Przywara8c604352009-06-18 12:56:01 +02001667static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001668emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001669{
1670 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001671 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001672 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001673 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001674
Gleb Natapova0044752010-02-10 14:21:31 +02001675 /* inject #GP if in real mode */
1676 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001677 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001678 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001679 }
1680
1681 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1682 * Therefore, we inject an #UD.
1683 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001684 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001685 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001686 return X86EMUL_PROPAGATE_FAULT;
1687 }
Andre Przywara8c604352009-06-18 12:56:01 +02001688
Gleb Natapov79168fd2010-04-28 19:15:30 +03001689 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001690
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001691 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001692 switch (ctxt->mode) {
1693 case X86EMUL_MODE_PROT32:
1694 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001695 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001696 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001697 }
1698 break;
1699 case X86EMUL_MODE_PROT64:
1700 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001701 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001702 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001703 }
1704 break;
1705 }
1706
1707 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001708 cs_sel = (u16)msr_data;
1709 cs_sel &= ~SELECTOR_RPL_MASK;
1710 ss_sel = cs_sel + 8;
1711 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001712 if (ctxt->mode == X86EMUL_MODE_PROT64
1713 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001714 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001715 cs.l = 1;
1716 }
1717
Gleb Natapov79168fd2010-04-28 19:15:30 +03001718 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1719 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1720 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1721 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001722
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001723 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001724 c->eip = msr_data;
1725
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001726 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001727 c->regs[VCPU_REGS_RSP] = msr_data;
1728
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001729 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001730}
1731
Andre Przywara4668f052009-06-18 12:56:02 +02001732static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001733emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001734{
1735 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001736 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001737 u64 msr_data;
1738 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001739 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001740
Gleb Natapova0044752010-02-10 14:21:31 +02001741 /* inject #GP if in real mode or Virtual 8086 mode */
1742 if (ctxt->mode == X86EMUL_MODE_REAL ||
1743 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001744 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001745 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001746 }
1747
Gleb Natapov79168fd2010-04-28 19:15:30 +03001748 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001749
1750 if ((c->rex_prefix & 0x8) != 0x0)
1751 usermode = X86EMUL_MODE_PROT64;
1752 else
1753 usermode = X86EMUL_MODE_PROT32;
1754
1755 cs.dpl = 3;
1756 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001757 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001758 switch (usermode) {
1759 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001760 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001761 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001762 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001763 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001764 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001765 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001766 break;
1767 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001768 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001769 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001770 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001771 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001772 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001773 ss_sel = cs_sel + 8;
1774 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001775 cs.l = 1;
1776 break;
1777 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001778 cs_sel |= SELECTOR_RPL_MASK;
1779 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001780
Gleb Natapov79168fd2010-04-28 19:15:30 +03001781 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1782 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1783 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1784 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001785
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001786 c->eip = c->regs[VCPU_REGS_RDX];
1787 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001788
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001789 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001790}
1791
Gleb Natapov9c537242010-03-18 15:20:05 +02001792static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1793 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001794{
1795 int iopl;
1796 if (ctxt->mode == X86EMUL_MODE_REAL)
1797 return false;
1798 if (ctxt->mode == X86EMUL_MODE_VM86)
1799 return true;
1800 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001801 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001802}
1803
1804static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1805 struct x86_emulate_ops *ops,
1806 u16 port, u16 len)
1807{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001808 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001809 int r;
1810 u16 io_bitmap_ptr;
1811 u8 perm, bit_idx = port & 0x7;
1812 unsigned mask = (1 << len) - 1;
1813
Gleb Natapov79168fd2010-04-28 19:15:30 +03001814 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1815 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001816 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001817 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001818 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001819 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1820 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001821 if (r != X86EMUL_CONTINUE)
1822 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001823 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001824 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001825 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1826 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001827 if (r != X86EMUL_CONTINUE)
1828 return false;
1829 if ((perm >> bit_idx) & mask)
1830 return false;
1831 return true;
1832}
1833
1834static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1835 struct x86_emulate_ops *ops,
1836 u16 port, u16 len)
1837{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001838 if (ctxt->perm_ok)
1839 return true;
1840
Gleb Natapov9c537242010-03-18 15:20:05 +02001841 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001842 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1843 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001844
1845 ctxt->perm_ok = true;
1846
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001847 return true;
1848}
1849
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001850static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1851 struct x86_emulate_ops *ops,
1852 struct tss_segment_16 *tss)
1853{
1854 struct decode_cache *c = &ctxt->decode;
1855
1856 tss->ip = c->eip;
1857 tss->flag = ctxt->eflags;
1858 tss->ax = c->regs[VCPU_REGS_RAX];
1859 tss->cx = c->regs[VCPU_REGS_RCX];
1860 tss->dx = c->regs[VCPU_REGS_RDX];
1861 tss->bx = c->regs[VCPU_REGS_RBX];
1862 tss->sp = c->regs[VCPU_REGS_RSP];
1863 tss->bp = c->regs[VCPU_REGS_RBP];
1864 tss->si = c->regs[VCPU_REGS_RSI];
1865 tss->di = c->regs[VCPU_REGS_RDI];
1866
1867 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1868 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1869 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1870 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1871 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1872}
1873
1874static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1875 struct x86_emulate_ops *ops,
1876 struct tss_segment_16 *tss)
1877{
1878 struct decode_cache *c = &ctxt->decode;
1879 int ret;
1880
1881 c->eip = tss->ip;
1882 ctxt->eflags = tss->flag | 2;
1883 c->regs[VCPU_REGS_RAX] = tss->ax;
1884 c->regs[VCPU_REGS_RCX] = tss->cx;
1885 c->regs[VCPU_REGS_RDX] = tss->dx;
1886 c->regs[VCPU_REGS_RBX] = tss->bx;
1887 c->regs[VCPU_REGS_RSP] = tss->sp;
1888 c->regs[VCPU_REGS_RBP] = tss->bp;
1889 c->regs[VCPU_REGS_RSI] = tss->si;
1890 c->regs[VCPU_REGS_RDI] = tss->di;
1891
1892 /*
1893 * SDM says that segment selectors are loaded before segment
1894 * descriptors
1895 */
1896 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1897 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1898 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1899 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1900 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1901
1902 /*
1903 * Now load segment descriptors. If fault happenes at this stage
1904 * it is handled in a context of new task
1905 */
1906 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1907 if (ret != X86EMUL_CONTINUE)
1908 return ret;
1909 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1910 if (ret != X86EMUL_CONTINUE)
1911 return ret;
1912 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1913 if (ret != X86EMUL_CONTINUE)
1914 return ret;
1915 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1916 if (ret != X86EMUL_CONTINUE)
1917 return ret;
1918 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1919 if (ret != X86EMUL_CONTINUE)
1920 return ret;
1921
1922 return X86EMUL_CONTINUE;
1923}
1924
1925static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1926 struct x86_emulate_ops *ops,
1927 u16 tss_selector, u16 old_tss_sel,
1928 ulong old_tss_base, struct desc_struct *new_desc)
1929{
1930 struct tss_segment_16 tss_seg;
1931 int ret;
1932 u32 err, new_tss_base = get_desc_base(new_desc);
1933
1934 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1935 &err);
1936 if (ret == X86EMUL_PROPAGATE_FAULT) {
1937 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02001938 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001939 return ret;
1940 }
1941
1942 save_state_to_tss16(ctxt, ops, &tss_seg);
1943
1944 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1945 &err);
1946 if (ret == X86EMUL_PROPAGATE_FAULT) {
1947 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02001948 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001949 return ret;
1950 }
1951
1952 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1953 &err);
1954 if (ret == X86EMUL_PROPAGATE_FAULT) {
1955 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02001956 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001957 return ret;
1958 }
1959
1960 if (old_tss_sel != 0xffff) {
1961 tss_seg.prev_task_link = old_tss_sel;
1962
1963 ret = ops->write_std(new_tss_base,
1964 &tss_seg.prev_task_link,
1965 sizeof tss_seg.prev_task_link,
1966 ctxt->vcpu, &err);
1967 if (ret == X86EMUL_PROPAGATE_FAULT) {
1968 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02001969 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001970 return ret;
1971 }
1972 }
1973
1974 return load_state_from_tss16(ctxt, ops, &tss_seg);
1975}
1976
1977static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1978 struct x86_emulate_ops *ops,
1979 struct tss_segment_32 *tss)
1980{
1981 struct decode_cache *c = &ctxt->decode;
1982
1983 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1984 tss->eip = c->eip;
1985 tss->eflags = ctxt->eflags;
1986 tss->eax = c->regs[VCPU_REGS_RAX];
1987 tss->ecx = c->regs[VCPU_REGS_RCX];
1988 tss->edx = c->regs[VCPU_REGS_RDX];
1989 tss->ebx = c->regs[VCPU_REGS_RBX];
1990 tss->esp = c->regs[VCPU_REGS_RSP];
1991 tss->ebp = c->regs[VCPU_REGS_RBP];
1992 tss->esi = c->regs[VCPU_REGS_RSI];
1993 tss->edi = c->regs[VCPU_REGS_RDI];
1994
1995 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1996 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1997 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1998 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1999 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2000 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2001 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2002}
2003
2004static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2005 struct x86_emulate_ops *ops,
2006 struct tss_segment_32 *tss)
2007{
2008 struct decode_cache *c = &ctxt->decode;
2009 int ret;
2010
Gleb Natapov0f122442010-04-28 19:15:31 +03002011 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002012 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03002013 return X86EMUL_PROPAGATE_FAULT;
2014 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002015 c->eip = tss->eip;
2016 ctxt->eflags = tss->eflags | 2;
2017 c->regs[VCPU_REGS_RAX] = tss->eax;
2018 c->regs[VCPU_REGS_RCX] = tss->ecx;
2019 c->regs[VCPU_REGS_RDX] = tss->edx;
2020 c->regs[VCPU_REGS_RBX] = tss->ebx;
2021 c->regs[VCPU_REGS_RSP] = tss->esp;
2022 c->regs[VCPU_REGS_RBP] = tss->ebp;
2023 c->regs[VCPU_REGS_RSI] = tss->esi;
2024 c->regs[VCPU_REGS_RDI] = tss->edi;
2025
2026 /*
2027 * SDM says that segment selectors are loaded before segment
2028 * descriptors
2029 */
2030 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2031 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2032 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2033 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2034 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2035 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2036 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2037
2038 /*
2039 * Now load segment descriptors. If fault happenes at this stage
2040 * it is handled in a context of new task
2041 */
2042 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2043 if (ret != X86EMUL_CONTINUE)
2044 return ret;
2045 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2046 if (ret != X86EMUL_CONTINUE)
2047 return ret;
2048 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2049 if (ret != X86EMUL_CONTINUE)
2050 return ret;
2051 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2052 if (ret != X86EMUL_CONTINUE)
2053 return ret;
2054 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2055 if (ret != X86EMUL_CONTINUE)
2056 return ret;
2057 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2058 if (ret != X86EMUL_CONTINUE)
2059 return ret;
2060 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2061 if (ret != X86EMUL_CONTINUE)
2062 return ret;
2063
2064 return X86EMUL_CONTINUE;
2065}
2066
2067static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2068 struct x86_emulate_ops *ops,
2069 u16 tss_selector, u16 old_tss_sel,
2070 ulong old_tss_base, struct desc_struct *new_desc)
2071{
2072 struct tss_segment_32 tss_seg;
2073 int ret;
2074 u32 err, new_tss_base = get_desc_base(new_desc);
2075
2076 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2077 &err);
2078 if (ret == X86EMUL_PROPAGATE_FAULT) {
2079 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02002080 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002081 return ret;
2082 }
2083
2084 save_state_to_tss32(ctxt, ops, &tss_seg);
2085
2086 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2087 &err);
2088 if (ret == X86EMUL_PROPAGATE_FAULT) {
2089 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02002090 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002091 return ret;
2092 }
2093
2094 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2095 &err);
2096 if (ret == X86EMUL_PROPAGATE_FAULT) {
2097 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02002098 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002099 return ret;
2100 }
2101
2102 if (old_tss_sel != 0xffff) {
2103 tss_seg.prev_task_link = old_tss_sel;
2104
2105 ret = ops->write_std(new_tss_base,
2106 &tss_seg.prev_task_link,
2107 sizeof tss_seg.prev_task_link,
2108 ctxt->vcpu, &err);
2109 if (ret == X86EMUL_PROPAGATE_FAULT) {
2110 /* FIXME: need to provide precise fault address */
Joerg Roedel8df25a32010-09-10 17:30:46 +02002111 emulate_pf(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002112 return ret;
2113 }
2114 }
2115
2116 return load_state_from_tss32(ctxt, ops, &tss_seg);
2117}
2118
2119static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002120 struct x86_emulate_ops *ops,
2121 u16 tss_selector, int reason,
2122 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002123{
2124 struct desc_struct curr_tss_desc, next_tss_desc;
2125 int ret;
2126 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2127 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002128 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002129 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002130
2131 /* FIXME: old_tss_base == ~0 ? */
2132
2133 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2134 if (ret != X86EMUL_CONTINUE)
2135 return ret;
2136 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2137 if (ret != X86EMUL_CONTINUE)
2138 return ret;
2139
2140 /* FIXME: check that next_tss_desc is tss */
2141
2142 if (reason != TASK_SWITCH_IRET) {
2143 if ((tss_selector & 3) > next_tss_desc.dpl ||
2144 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002145 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002146 return X86EMUL_PROPAGATE_FAULT;
2147 }
2148 }
2149
Gleb Natapovceffb452010-03-18 15:20:19 +02002150 desc_limit = desc_limit_scaled(&next_tss_desc);
2151 if (!next_tss_desc.p ||
2152 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2153 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002154 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002155 return X86EMUL_PROPAGATE_FAULT;
2156 }
2157
2158 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2159 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2160 write_segment_descriptor(ctxt, ops, old_tss_sel,
2161 &curr_tss_desc);
2162 }
2163
2164 if (reason == TASK_SWITCH_IRET)
2165 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2166
2167 /* set back link to prev task only if NT bit is set in eflags
2168 note that old_tss_sel is not used afetr this point */
2169 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2170 old_tss_sel = 0xffff;
2171
2172 if (next_tss_desc.type & 8)
2173 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2174 old_tss_base, &next_tss_desc);
2175 else
2176 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2177 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002178 if (ret != X86EMUL_CONTINUE)
2179 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180
2181 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2182 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2183
2184 if (reason != TASK_SWITCH_IRET) {
2185 next_tss_desc.type |= (1 << 1); /* set busy flag */
2186 write_segment_descriptor(ctxt, ops, tss_selector,
2187 &next_tss_desc);
2188 }
2189
2190 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2191 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2192 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2193
Jan Kiszkae269fb22010-04-14 15:51:09 +02002194 if (has_error_code) {
2195 struct decode_cache *c = &ctxt->decode;
2196
2197 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2198 c->lock_prefix = 0;
2199 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002200 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002201 }
2202
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002203 return ret;
2204}
2205
2206int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002207 u16 tss_selector, int reason,
2208 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002209{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002210 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002211 struct decode_cache *c = &ctxt->decode;
2212 int rc;
2213
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002214 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002215 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002216
Jan Kiszkae269fb22010-04-14 15:51:09 +02002217 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2218 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002219
2220 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002221 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002222 if (rc == X86EMUL_CONTINUE)
2223 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002224 }
2225
Gleb Natapov19d04432010-04-15 12:29:50 +03002226 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002227}
2228
Avi Kivity90de84f2010-11-17 15:28:21 +02002229static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002230 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002231{
2232 struct decode_cache *c = &ctxt->decode;
2233 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2234
Gleb Natapovd9271122010-03-18 15:20:22 +02002235 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002236 op->addr.mem.ea = register_address(c, c->regs[reg]);
2237 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002238}
2239
Avi Kivity63540382010-07-29 15:11:55 +03002240static int em_push(struct x86_emulate_ctxt *ctxt)
2241{
2242 emulate_push(ctxt, ctxt->ops);
2243 return X86EMUL_CONTINUE;
2244}
2245
Avi Kivity7af04fc2010-08-18 14:16:35 +03002246static int em_das(struct x86_emulate_ctxt *ctxt)
2247{
2248 struct decode_cache *c = &ctxt->decode;
2249 u8 al, old_al;
2250 bool af, cf, old_cf;
2251
2252 cf = ctxt->eflags & X86_EFLAGS_CF;
2253 al = c->dst.val;
2254
2255 old_al = al;
2256 old_cf = cf;
2257 cf = false;
2258 af = ctxt->eflags & X86_EFLAGS_AF;
2259 if ((al & 0x0f) > 9 || af) {
2260 al -= 6;
2261 cf = old_cf | (al >= 250);
2262 af = true;
2263 } else {
2264 af = false;
2265 }
2266 if (old_al > 0x99 || old_cf) {
2267 al -= 0x60;
2268 cf = true;
2269 }
2270
2271 c->dst.val = al;
2272 /* Set PF, ZF, SF */
2273 c->src.type = OP_IMM;
2274 c->src.val = 0;
2275 c->src.bytes = 1;
2276 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2277 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2278 if (cf)
2279 ctxt->eflags |= X86_EFLAGS_CF;
2280 if (af)
2281 ctxt->eflags |= X86_EFLAGS_AF;
2282 return X86EMUL_CONTINUE;
2283}
2284
Avi Kivity0ef753b2010-08-18 14:51:45 +03002285static int em_call_far(struct x86_emulate_ctxt *ctxt)
2286{
2287 struct decode_cache *c = &ctxt->decode;
2288 u16 sel, old_cs;
2289 ulong old_eip;
2290 int rc;
2291
2292 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2293 old_eip = c->eip;
2294
2295 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2296 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2297 return X86EMUL_CONTINUE;
2298
2299 c->eip = 0;
2300 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2301
2302 c->src.val = old_cs;
2303 emulate_push(ctxt, ctxt->ops);
2304 rc = writeback(ctxt, ctxt->ops);
2305 if (rc != X86EMUL_CONTINUE)
2306 return rc;
2307
2308 c->src.val = old_eip;
2309 emulate_push(ctxt, ctxt->ops);
2310 rc = writeback(ctxt, ctxt->ops);
2311 if (rc != X86EMUL_CONTINUE)
2312 return rc;
2313
2314 c->dst.type = OP_NONE;
2315
2316 return X86EMUL_CONTINUE;
2317}
2318
Avi Kivity40ece7c2010-08-18 15:12:09 +03002319static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2320{
2321 struct decode_cache *c = &ctxt->decode;
2322 int rc;
2323
2324 c->dst.type = OP_REG;
2325 c->dst.addr.reg = &c->eip;
2326 c->dst.bytes = c->op_bytes;
2327 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2328 if (rc != X86EMUL_CONTINUE)
2329 return rc;
2330 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2331 return X86EMUL_CONTINUE;
2332}
2333
Avi Kivity5c82aa22010-08-18 18:31:43 +03002334static int em_imul(struct x86_emulate_ctxt *ctxt)
2335{
2336 struct decode_cache *c = &ctxt->decode;
2337
2338 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2339 return X86EMUL_CONTINUE;
2340}
2341
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002342static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2343{
2344 struct decode_cache *c = &ctxt->decode;
2345
2346 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002347 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002348}
2349
Avi Kivity61429142010-08-19 15:13:00 +03002350static int em_cwd(struct x86_emulate_ctxt *ctxt)
2351{
2352 struct decode_cache *c = &ctxt->decode;
2353
2354 c->dst.type = OP_REG;
2355 c->dst.bytes = c->src.bytes;
2356 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2357 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2358
2359 return X86EMUL_CONTINUE;
2360}
2361
Avi Kivity48bb5d32010-08-18 18:54:34 +03002362static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2363{
2364 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2365 struct decode_cache *c = &ctxt->decode;
2366 u64 tsc = 0;
2367
2368 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) {
2369 emulate_gp(ctxt, 0);
2370 return X86EMUL_PROPAGATE_FAULT;
2371 }
2372 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2373 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2374 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2375 return X86EMUL_CONTINUE;
2376}
2377
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002378static int em_mov(struct x86_emulate_ctxt *ctxt)
2379{
2380 struct decode_cache *c = &ctxt->decode;
2381 c->dst.val = c->src.val;
2382 return X86EMUL_CONTINUE;
2383}
2384
Avi Kivity73fba5f2010-07-29 15:11:53 +03002385#define D(_y) { .flags = (_y) }
2386#define N D(0)
2387#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2388#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2389#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2390
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002391#define D2bv(_f) D((_f) | ByteOp), D(_f)
2392#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2393
Avi Kivity6230f7f2010-08-26 18:34:55 +03002394#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2395 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2396 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2397
2398
Avi Kivity73fba5f2010-07-29 15:11:53 +03002399static struct opcode group1[] = {
2400 X7(D(Lock)), N
2401};
2402
2403static struct opcode group1A[] = {
2404 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2405};
2406
2407static struct opcode group3[] = {
2408 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2409 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002410 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002411};
2412
2413static struct opcode group4[] = {
2414 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2415 N, N, N, N, N, N,
2416};
2417
2418static struct opcode group5[] = {
2419 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002420 D(SrcMem | ModRM | Stack),
2421 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002422 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2423 D(SrcMem | ModRM | Stack), N,
2424};
2425
2426static struct group_dual group7 = { {
2427 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2428 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002429 D(SrcMem16 | ModRM | Mov | Priv),
2430 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002431}, {
2432 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2433 D(SrcNone | ModRM | DstMem | Mov), N,
2434 D(SrcMem16 | ModRM | Mov | Priv), N,
2435} };
2436
2437static struct opcode group8[] = {
2438 N, N, N, N,
2439 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2440 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2441};
2442
2443static struct group_dual group9 = { {
2444 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2445}, {
2446 N, N, N, N, N, N, N, N,
2447} };
2448
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002449static struct opcode group11[] = {
2450 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2451};
2452
Avi Kivity73fba5f2010-07-29 15:11:53 +03002453static struct opcode opcode_table[256] = {
2454 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002455 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002456 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2457 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002458 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002459 D(ImplicitOps | Stack | No64), N,
2460 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002461 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002462 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2463 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002464 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002465 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2466 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002467 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002468 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002469 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002470 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002471 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002472 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002473 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002474 /* 0x40 - 0x4F */
2475 X16(D(DstReg)),
2476 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002477 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002478 /* 0x58 - 0x5F */
2479 X8(D(DstReg | Stack)),
2480 /* 0x60 - 0x67 */
2481 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2482 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2483 N, N, N, N,
2484 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002485 I(SrcImm | Mov | Stack, em_push),
2486 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002487 I(SrcImmByte | Mov | Stack, em_push),
2488 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002489 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2490 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002491 /* 0x70 - 0x7F */
2492 X16(D(SrcImmByte)),
2493 /* 0x80 - 0x87 */
2494 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2495 G(DstMem | SrcImm | ModRM | Group, group1),
2496 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2497 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002498 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002499 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002500 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2501 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002502 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002503 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2504 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002505 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002506 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002507 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002508 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002509 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2510 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002511 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2512 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2513 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2514 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002515 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002516 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002517 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2518 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002519 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002520 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002521 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002522 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002523 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002524 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002525 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002526 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2527 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002528 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002529 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002530 /* 0xC8 - 0xCF */
2531 N, N, N, D(ImplicitOps | Stack),
2532 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2533 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002534 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002535 N, N, N, N,
2536 /* 0xD8 - 0xDF */
2537 N, N, N, N, N, N, N, N,
2538 /* 0xE0 - 0xE7 */
Wei Yongjune4abac672010-08-19 14:25:48 +08002539 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002540 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002541 /* 0xE8 - 0xEF */
2542 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2543 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002544 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002545 /* 0xF0 - 0xF7 */
2546 N, N, N, N,
2547 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2548 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002549 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002550 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2551};
2552
2553static struct opcode twobyte_table[256] = {
2554 /* 0x00 - 0x0F */
2555 N, GD(0, &group7), N, N,
2556 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2557 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2558 N, D(ImplicitOps | ModRM), N, N,
2559 /* 0x10 - 0x1F */
2560 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2561 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002562 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2563 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002564 N, N, N, N,
2565 N, N, N, N, N, N, N, N,
2566 /* 0x30 - 0x3F */
Avi Kivity48bb5d32010-08-18 18:54:34 +03002567 D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc),
2568 D(ImplicitOps | Priv), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002569 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2570 N, N, N, N, N, N, N, N,
2571 /* 0x40 - 0x4F */
2572 X16(D(DstReg | SrcMem | ModRM | Mov)),
2573 /* 0x50 - 0x5F */
2574 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2575 /* 0x60 - 0x6F */
2576 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2577 /* 0x70 - 0x7F */
2578 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2579 /* 0x80 - 0x8F */
2580 X16(D(SrcImm)),
2581 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002582 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002583 /* 0xA0 - 0xA7 */
2584 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2585 N, D(DstMem | SrcReg | ModRM | BitOp),
2586 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2587 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2588 /* 0xA8 - 0xAF */
2589 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2590 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2591 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2592 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002593 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002594 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002595 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002596 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2597 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2598 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002599 /* 0xB8 - 0xBF */
2600 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002601 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002602 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2603 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002604 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002605 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a52010-08-17 09:19:34 +08002606 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002607 N, N, N, GD(0, &group9),
2608 N, N, N, N, N, N, N, N,
2609 /* 0xD0 - 0xDF */
2610 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2611 /* 0xE0 - 0xEF */
2612 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2613 /* 0xF0 - 0xFF */
2614 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2615};
2616
2617#undef D
2618#undef N
2619#undef G
2620#undef GD
2621#undef I
2622
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002623#undef D2bv
2624#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002625#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002626
Avi Kivity39f21ee2010-08-18 19:20:21 +03002627static unsigned imm_size(struct decode_cache *c)
2628{
2629 unsigned size;
2630
2631 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2632 if (size == 8)
2633 size = 4;
2634 return size;
2635}
2636
2637static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2638 unsigned size, bool sign_extension)
2639{
2640 struct decode_cache *c = &ctxt->decode;
2641 struct x86_emulate_ops *ops = ctxt->ops;
2642 int rc = X86EMUL_CONTINUE;
2643
2644 op->type = OP_IMM;
2645 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002646 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002647 /* NB. Immediates are sign-extended as necessary. */
2648 switch (op->bytes) {
2649 case 1:
2650 op->val = insn_fetch(s8, 1, c->eip);
2651 break;
2652 case 2:
2653 op->val = insn_fetch(s16, 2, c->eip);
2654 break;
2655 case 4:
2656 op->val = insn_fetch(s32, 4, c->eip);
2657 break;
2658 }
2659 if (!sign_extension) {
2660 switch (op->bytes) {
2661 case 1:
2662 op->val &= 0xff;
2663 break;
2664 case 2:
2665 op->val &= 0xffff;
2666 break;
2667 case 4:
2668 op->val &= 0xffffffff;
2669 break;
2670 }
2671 }
2672done:
2673 return rc;
2674}
2675
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002676int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002677x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2678{
2679 struct x86_emulate_ops *ops = ctxt->ops;
2680 struct decode_cache *c = &ctxt->decode;
2681 int rc = X86EMUL_CONTINUE;
2682 int mode = ctxt->mode;
2683 int def_op_bytes, def_ad_bytes, dual, goffset;
2684 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002685 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002686
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002687 c->eip = ctxt->eip;
2688 c->fetch.start = c->fetch.end = c->eip;
2689 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2690
2691 switch (mode) {
2692 case X86EMUL_MODE_REAL:
2693 case X86EMUL_MODE_VM86:
2694 case X86EMUL_MODE_PROT16:
2695 def_op_bytes = def_ad_bytes = 2;
2696 break;
2697 case X86EMUL_MODE_PROT32:
2698 def_op_bytes = def_ad_bytes = 4;
2699 break;
2700#ifdef CONFIG_X86_64
2701 case X86EMUL_MODE_PROT64:
2702 def_op_bytes = 4;
2703 def_ad_bytes = 8;
2704 break;
2705#endif
2706 default:
2707 return -1;
2708 }
2709
2710 c->op_bytes = def_op_bytes;
2711 c->ad_bytes = def_ad_bytes;
2712
2713 /* Legacy prefixes. */
2714 for (;;) {
2715 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2716 case 0x66: /* operand-size override */
2717 /* switch between 2/4 bytes */
2718 c->op_bytes = def_op_bytes ^ 6;
2719 break;
2720 case 0x67: /* address-size override */
2721 if (mode == X86EMUL_MODE_PROT64)
2722 /* switch between 4/8 bytes */
2723 c->ad_bytes = def_ad_bytes ^ 12;
2724 else
2725 /* switch between 2/4 bytes */
2726 c->ad_bytes = def_ad_bytes ^ 6;
2727 break;
2728 case 0x26: /* ES override */
2729 case 0x2e: /* CS override */
2730 case 0x36: /* SS override */
2731 case 0x3e: /* DS override */
2732 set_seg_override(c, (c->b >> 3) & 3);
2733 break;
2734 case 0x64: /* FS override */
2735 case 0x65: /* GS override */
2736 set_seg_override(c, c->b & 7);
2737 break;
2738 case 0x40 ... 0x4f: /* REX */
2739 if (mode != X86EMUL_MODE_PROT64)
2740 goto done_prefixes;
2741 c->rex_prefix = c->b;
2742 continue;
2743 case 0xf0: /* LOCK */
2744 c->lock_prefix = 1;
2745 break;
2746 case 0xf2: /* REPNE/REPNZ */
2747 c->rep_prefix = REPNE_PREFIX;
2748 break;
2749 case 0xf3: /* REP/REPE/REPZ */
2750 c->rep_prefix = REPE_PREFIX;
2751 break;
2752 default:
2753 goto done_prefixes;
2754 }
2755
2756 /* Any legacy prefix after a REX prefix nullifies its effect. */
2757
2758 c->rex_prefix = 0;
2759 }
2760
2761done_prefixes:
2762
2763 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002764 if (c->rex_prefix & 8)
2765 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002766
2767 /* Opcode byte(s). */
2768 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002769 /* Two-byte opcode? */
2770 if (c->b == 0x0f) {
2771 c->twobyte = 1;
2772 c->b = insn_fetch(u8, 1, c->eip);
2773 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002774 }
2775 c->d = opcode.flags;
2776
2777 if (c->d & Group) {
2778 dual = c->d & GroupDual;
2779 c->modrm = insn_fetch(u8, 1, c->eip);
2780 --c->eip;
2781
2782 if (c->d & GroupDual) {
2783 g_mod012 = opcode.u.gdual->mod012;
2784 g_mod3 = opcode.u.gdual->mod3;
2785 } else
2786 g_mod012 = g_mod3 = opcode.u.group;
2787
2788 c->d &= ~(Group | GroupDual);
2789
2790 goffset = (c->modrm >> 3) & 7;
2791
2792 if ((c->modrm >> 6) == 3)
2793 opcode = g_mod3[goffset];
2794 else
2795 opcode = g_mod012[goffset];
2796 c->d |= opcode.flags;
2797 }
2798
2799 c->execute = opcode.u.execute;
2800
2801 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02002802 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002803 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002804
2805 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2806 c->op_bytes = 8;
2807
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002808 if (c->d & Op3264) {
2809 if (mode == X86EMUL_MODE_PROT64)
2810 c->op_bytes = 8;
2811 else
2812 c->op_bytes = 4;
2813 }
2814
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002815 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002816 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002817 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002818 if (!c->has_seg_override)
2819 set_seg_override(c, c->modrm_seg);
2820 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002821 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002822 if (rc != X86EMUL_CONTINUE)
2823 goto done;
2824
2825 if (!c->has_seg_override)
2826 set_seg_override(c, VCPU_SREG_DS);
2827
Avi Kivity90de84f2010-11-17 15:28:21 +02002828 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002829
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002830 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02002831 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002832
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002833 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02002834 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002835
2836 /*
2837 * Decode and fetch the source operand: register, memory
2838 * or immediate.
2839 */
2840 switch (c->d & SrcMask) {
2841 case SrcNone:
2842 break;
2843 case SrcReg:
2844 decode_register_operand(&c->src, c, 0);
2845 break;
2846 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002847 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002848 goto srcmem_common;
2849 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002850 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002851 goto srcmem_common;
2852 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002853 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002854 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002855 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002856 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002857 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002858 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002859 rc = decode_imm(ctxt, &c->src, 2, false);
2860 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002861 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002862 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2863 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002864 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002865 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002866 break;
2867 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002868 rc = decode_imm(ctxt, &c->src, 1, true);
2869 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002870 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002871 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002872 break;
2873 case SrcAcc:
2874 c->src.type = OP_REG;
2875 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002876 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002877 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002878 break;
2879 case SrcOne:
2880 c->src.bytes = 1;
2881 c->src.val = 1;
2882 break;
2883 case SrcSI:
2884 c->src.type = OP_MEM;
2885 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002886 c->src.addr.mem.ea =
2887 register_address(c, c->regs[VCPU_REGS_RSI]);
2888 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002889 c->src.val = 0;
2890 break;
2891 case SrcImmFAddr:
2892 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002893 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002894 c->src.bytes = c->op_bytes + 2;
2895 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2896 break;
2897 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002898 memop.bytes = c->op_bytes + 2;
2899 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002900 break;
2901 }
2902
Avi Kivity39f21ee2010-08-18 19:20:21 +03002903 if (rc != X86EMUL_CONTINUE)
2904 goto done;
2905
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002906 /*
2907 * Decode and fetch the second source operand: register, memory
2908 * or immediate.
2909 */
2910 switch (c->d & Src2Mask) {
2911 case Src2None:
2912 break;
2913 case Src2CL:
2914 c->src2.bytes = 1;
2915 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2916 break;
2917 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002918 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002919 break;
2920 case Src2One:
2921 c->src2.bytes = 1;
2922 c->src2.val = 1;
2923 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03002924 case Src2Imm:
2925 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
2926 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002927 }
2928
Avi Kivity39f21ee2010-08-18 19:20:21 +03002929 if (rc != X86EMUL_CONTINUE)
2930 goto done;
2931
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002932 /* Decode and fetch the destination operand: register or memory. */
2933 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002934 case DstReg:
2935 decode_register_operand(&c->dst, c,
2936 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2937 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08002938 case DstImmUByte:
2939 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002940 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08002941 c->dst.bytes = 1;
2942 c->dst.val = insn_fetch(u8, 1, c->eip);
2943 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002944 case DstMem:
2945 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002946 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002947 if ((c->d & DstMask) == DstMem64)
2948 c->dst.bytes = 8;
2949 else
2950 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002951 if (c->d & BitOp)
2952 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002953 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002954 break;
2955 case DstAcc:
2956 c->dst.type = OP_REG;
2957 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002958 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002959 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002960 c->dst.orig_val = c->dst.val;
2961 break;
2962 case DstDI:
2963 c->dst.type = OP_MEM;
2964 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002965 c->dst.addr.mem.ea =
2966 register_address(c, c->regs[VCPU_REGS_RDI]);
2967 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002968 c->dst.val = 0;
2969 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002970 case ImplicitOps:
2971 /* Special instructions do their own operand decoding. */
2972 default:
2973 c->dst.type = OP_NONE; /* Disable writeback. */
2974 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002975 }
2976
2977done:
2978 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2979}
2980
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03002981static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
2982{
2983 struct decode_cache *c = &ctxt->decode;
2984
2985 /* The second termination condition only applies for REPE
2986 * and REPNE. Test if the repeat string operation prefix is
2987 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2988 * corresponding termination condition according to:
2989 * - if REPE/REPZ and ZF = 0 then done
2990 * - if REPNE/REPNZ and ZF = 1 then done
2991 */
2992 if (((c->b == 0xa6) || (c->b == 0xa7) ||
2993 (c->b == 0xae) || (c->b == 0xaf))
2994 && (((c->rep_prefix == REPE_PREFIX) &&
2995 ((ctxt->eflags & EFLG_ZF) == 0))
2996 || ((c->rep_prefix == REPNE_PREFIX) &&
2997 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
2998 return true;
2999
3000 return false;
3001}
3002
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003003int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003004x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003005{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003006 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003007 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003008 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003009 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003010 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003011 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003012
Gleb Natapov9de41572010-04-28 19:15:22 +03003013 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003014
Gleb Natapov11616242010-02-11 14:43:14 +02003015 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003016 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003017 goto done;
3018 }
3019
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003020 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003021 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003022 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003023 goto done;
3024 }
3025
Avi Kivity081bca02010-08-26 11:06:15 +03003026 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
3027 emulate_ud(ctxt);
3028 goto done;
3029 }
3030
Gleb Natapove92805a2010-02-10 14:21:35 +02003031 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003032 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003033 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003034 goto done;
3035 }
3036
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003037 if (c->rep_prefix && (c->d & String)) {
3038 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003039 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003040 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003041 goto done;
3042 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003043 }
3044
Wei Yongjunc483c022010-08-06 15:36:36 +08003045 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003046 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03003047 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003048 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003049 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003050 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003051 }
3052
Gleb Natapove35b7b92010-02-25 16:36:42 +02003053 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003054 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003055 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003056 if (rc != X86EMUL_CONTINUE)
3057 goto done;
3058 }
3059
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003060 if ((c->d & DstMask) == ImplicitOps)
3061 goto special_insn;
3062
3063
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003064 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3065 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003066 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003067 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003068 if (rc != X86EMUL_CONTINUE)
3069 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003070 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003071 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003072
Avi Kivity018a98d2007-11-27 19:30:56 +02003073special_insn:
3074
Avi Kivityef65c882010-07-29 15:11:51 +03003075 if (c->execute) {
3076 rc = c->execute(ctxt);
3077 if (rc != X86EMUL_CONTINUE)
3078 goto done;
3079 goto writeback;
3080 }
3081
Laurent Viviere4e03de2007-09-18 11:52:50 +02003082 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 goto twobyte_insn;
3084
Laurent Viviere4e03de2007-09-18 11:52:50 +02003085 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 case 0x00 ... 0x05:
3087 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003088 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003090 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003091 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003092 break;
3093 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003094 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003095 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 case 0x08 ... 0x0d:
3097 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003098 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003100 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003101 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003102 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 case 0x10 ... 0x15:
3104 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003105 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003107 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003108 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003109 break;
3110 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003111 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003112 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 case 0x18 ... 0x1d:
3114 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003115 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003117 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003118 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003119 break;
3120 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003121 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003122 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003123 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003125 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126 break;
3127 case 0x28 ... 0x2d:
3128 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003129 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130 break;
3131 case 0x30 ... 0x35:
3132 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003133 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 break;
3135 case 0x38 ... 0x3d:
3136 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003137 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003139 case 0x40 ... 0x47: /* inc r16/r32 */
3140 emulate_1op("inc", c->dst, ctxt->eflags);
3141 break;
3142 case 0x48 ... 0x4f: /* dec r16/r32 */
3143 emulate_1op("dec", c->dst, ctxt->eflags);
3144 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003145 case 0x58 ... 0x5f: /* pop reg */
3146 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003147 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003148 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003149 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003150 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003151 break;
3152 case 0x61: /* popa */
3153 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003154 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003156 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003158 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003160 case 0x6c: /* insb */
3161 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003162 c->src.val = c->regs[VCPU_REGS_RDX];
3163 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003164 case 0x6e: /* outsb */
3165 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003166 c->dst.val = c->regs[VCPU_REGS_RDX];
3167 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003168 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003169 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003170 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003171 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003172 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003174 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175 case 0:
3176 goto add;
3177 case 1:
3178 goto or;
3179 case 2:
3180 goto adc;
3181 case 3:
3182 goto sbb;
3183 case 4:
3184 goto and;
3185 case 5:
3186 goto sub;
3187 case 6:
3188 goto xor;
3189 case 7:
3190 goto cmp;
3191 }
3192 break;
3193 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003194 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003195 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196 break;
3197 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003198 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003200 c->src.val = c->dst.val;
3201 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 /*
3203 * Write back the memory destination with implicit LOCK
3204 * prefix.
3205 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003206 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003207 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003209 case 0x8c: /* mov r/m, sreg */
3210 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003211 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003212 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003213 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003214 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003215 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003216 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003217 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003218 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003219 case 0x8e: { /* mov seg, r/m16 */
3220 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003221
3222 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003223
Gleb Natapovc6975182010-02-18 12:15:01 +02003224 if (c->modrm_reg == VCPU_SREG_CS ||
3225 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003226 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003227 goto done;
3228 }
3229
Glauber Costa310b5d32009-05-12 16:21:06 -04003230 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003231 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003232
Gleb Natapov2e873022010-03-18 15:20:18 +02003233 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003234
3235 c->dst.type = OP_NONE; /* Disable writeback. */
3236 break;
3237 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003239 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003241 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3242 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003243 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003244 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003245 case 0x98: /* cbw/cwde/cdqe */
3246 switch (c->op_bytes) {
3247 case 2: c->dst.val = (s8)c->dst.val; break;
3248 case 4: c->dst.val = (s16)c->dst.val; break;
3249 case 8: c->dst.val = (s32)c->dst.val; break;
3250 }
3251 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003252 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003253 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003254 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003255 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003256 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003257 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003258 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003259 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003260 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003261 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003263 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003264 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003265 case 0xa8 ... 0xa9: /* test ax, imm */
3266 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003268 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003269 case 0xc0 ... 0xc1:
3270 emulate_grp2(ctxt);
3271 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003272 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003273 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003274 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003275 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003276 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003277 case 0xc4: /* les */
3278 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003279 break;
3280 case 0xc5: /* lds */
3281 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003282 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003283 case 0xcb: /* ret far */
3284 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003285 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003286 case 0xcc: /* int3 */
3287 irq = 3;
3288 goto do_interrupt;
3289 case 0xcd: /* int n */
3290 irq = c->src.val;
3291 do_interrupt:
3292 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003293 break;
3294 case 0xce: /* into */
3295 if (ctxt->eflags & EFLG_OF) {
3296 irq = 4;
3297 goto do_interrupt;
3298 }
3299 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003300 case 0xcf: /* iret */
3301 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003302 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003303 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003304 emulate_grp2(ctxt);
3305 break;
3306 case 0xd2 ... 0xd3: /* Grp2 */
3307 c->src.val = c->regs[VCPU_REGS_RCX];
3308 emulate_grp2(ctxt);
3309 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003310 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3311 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3312 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3313 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3314 jmp_rel(c, c->src.val);
3315 break;
Wei Yongjune4abac672010-08-19 14:25:48 +08003316 case 0xe3: /* jcxz/jecxz/jrcxz */
3317 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3318 jmp_rel(c, c->src.val);
3319 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003320 case 0xe4: /* inb */
3321 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003322 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003323 case 0xe6: /* outb */
3324 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003325 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003326 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003327 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003328 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003329 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003330 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003331 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003332 }
3333 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003334 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003335 case 0xea: { /* jmp far */
3336 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003337 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003338 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3339
3340 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003341 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003342
Gleb Natapov414e6272010-04-28 19:15:26 +03003343 c->eip = 0;
3344 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003345 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003346 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003347 case 0xeb:
3348 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003349 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003350 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003351 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003352 case 0xec: /* in al,dx */
3353 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003354 c->src.val = c->regs[VCPU_REGS_RDX];
3355 do_io_in:
3356 c->dst.bytes = min(c->dst.bytes, 4u);
3357 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003358 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003359 goto done;
3360 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003361 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3362 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003363 goto done; /* IO is needed */
3364 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003365 case 0xee: /* out dx,al */
3366 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003367 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003368 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003369 c->src.bytes = min(c->src.bytes, 4u);
3370 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3371 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003372 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003373 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003374 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003375 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3376 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003377 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003378 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003379 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003380 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003381 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003382 case 0xf5: /* cmc */
3383 /* complement carry flag from eflags reg */
3384 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003385 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003386 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003387 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003388 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003389 case 0xf8: /* clc */
3390 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003391 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003392 case 0xf9: /* stc */
3393 ctxt->eflags |= EFLG_CF;
3394 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003395 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003396 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003397 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003398 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003399 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003400 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003401 break;
3402 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003403 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003404 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003405 goto done;
3406 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003407 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003408 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003409 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003410 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003411 case 0xfc: /* cld */
3412 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003413 break;
3414 case 0xfd: /* std */
3415 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003416 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003417 case 0xfe: /* Grp4 */
3418 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003419 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003420 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003421 case 0xff: /* Grp5 */
3422 if (c->modrm_reg == 5)
3423 goto jump_far;
3424 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003425 default:
3426 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003428
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003429 if (rc != X86EMUL_CONTINUE)
3430 goto done;
3431
Avi Kivity018a98d2007-11-27 19:30:56 +02003432writeback:
3433 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003434 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003435 goto done;
3436
Gleb Natapov5cd21912010-03-18 15:20:26 +02003437 /*
3438 * restore dst type in case the decoding will be reused
3439 * (happens for string instruction )
3440 */
3441 c->dst.type = saved_dst_type;
3442
Gleb Natapova682e352010-03-18 15:20:21 +02003443 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003444 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003445 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003446
3447 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003448 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003449 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003450
Gleb Natapov5cd21912010-03-18 15:20:26 +02003451 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003452 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003453 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003454
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003455 if (!string_insn_completed(ctxt)) {
3456 /*
3457 * Re-enter guest when pio read ahead buffer is empty
3458 * or, if it is not used, after each 1024 iteration.
3459 */
3460 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3461 (r->end == 0 || r->end != r->pos)) {
3462 /*
3463 * Reset read cache. Usually happens before
3464 * decode, but since instruction is restarted
3465 * we have to do it here.
3466 */
3467 ctxt->decode.mem_read.end = 0;
3468 return EMULATION_RESTART;
3469 }
3470 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003471 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003472 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003473
3474 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003475
3476done:
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003477 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478
3479twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003480 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003482 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 u16 size;
3484 unsigned long address;
3485
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003486 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003487 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003488 goto cannot_emulate;
3489
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003490 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003491 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003492 goto done;
3493
Avi Kivity33e38852008-05-21 15:34:25 +03003494 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003495 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003496 /* Disable writeback. */
3497 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003498 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003500 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003501 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003502 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503 goto done;
3504 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003505 /* Disable writeback. */
3506 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003508 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003509 if (c->modrm_mod == 3) {
3510 switch (c->modrm_rm) {
3511 case 1:
3512 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003513 break;
3514 default:
3515 goto cannot_emulate;
3516 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003517 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003518 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003519 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003520 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003521 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003522 goto done;
3523 realmode_lidt(ctxt->vcpu, size, address);
3524 }
Avi Kivity16286d02008-04-14 14:40:50 +03003525 /* Disable writeback. */
3526 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527 break;
3528 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003529 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003530 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531 break;
3532 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003533 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003534 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003535 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003537 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003538 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003539 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003541 emulate_invlpg(ctxt->vcpu,
3542 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003543 /* Disable writeback. */
3544 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545 break;
3546 default:
3547 goto cannot_emulate;
3548 }
3549 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003550 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003551 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003552 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003553 case 0x06:
3554 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003555 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003556 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003557 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003558 break;
3559 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003560 case 0x0d: /* GrpP (prefetch) */
3561 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003562 break;
3563 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003564 switch (c->modrm_reg) {
3565 case 1:
3566 case 5 ... 7:
3567 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003568 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003569 goto done;
3570 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003571 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003572 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003573 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003574 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3575 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003576 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003577 goto done;
3578 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003579 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003581 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003582 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003583 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003584 goto done;
3585 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003586 c->dst.type = OP_NONE;
3587 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003589 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3590 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003591 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003592 goto done;
3593 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003594
Avi Kivityb27f3852010-08-01 14:25:22 +03003595 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003596 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3597 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3598 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003599 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003600 goto done;
3601 }
3602
Laurent Viviera01af5e2007-09-24 11:10:56 +02003603 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003604 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003605 case 0x30:
3606 /* wrmsr */
3607 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3608 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003609 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003610 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003611 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003612 }
3613 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003614 break;
3615 case 0x32:
3616 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003617 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003618 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003619 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003620 } else {
3621 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3622 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3623 }
3624 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003625 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003626 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003627 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003628 break;
3629 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003630 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003631 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003632 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003633 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003634 if (!test_cc(c->b, ctxt->eflags))
3635 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003637 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003638 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003639 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003640 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003641 case 0x90 ... 0x9f: /* setcc r/m8 */
3642 c->dst.val = test_cc(c->b, ctxt->eflags);
3643 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003644 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003645 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003646 break;
3647 case 0xa1: /* pop fs */
3648 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003649 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003650 case 0xa3:
3651 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003652 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003653 /* only subword offset */
3654 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003655 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003656 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003657 case 0xa4: /* shld imm8, r, r/m */
3658 case 0xa5: /* shld cl, r, r/m */
3659 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3660 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003661 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003662 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003663 break;
3664 case 0xa9: /* pop gs */
3665 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003666 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003667 case 0xab:
3668 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003669 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003670 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003671 case 0xac: /* shrd imm8, r, r/m */
3672 case 0xad: /* shrd cl, r, r/m */
3673 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3674 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003675 case 0xae: /* clflush */
3676 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677 case 0xb0 ... 0xb1: /* cmpxchg */
3678 /*
3679 * Save real source value, then compare EAX against
3680 * destination.
3681 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003682 c->src.orig_val = c->src.val;
3683 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003684 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3685 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003687 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003688 } else {
3689 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003690 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003691 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692 }
3693 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003694 case 0xb2: /* lss */
3695 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003696 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003697 case 0xb3:
3698 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003699 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003701 case 0xb4: /* lfs */
3702 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003703 break;
3704 case 0xb5: /* lgs */
3705 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003706 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003708 c->dst.bytes = c->op_bytes;
3709 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3710 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003713 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714 case 0:
3715 goto bt;
3716 case 1:
3717 goto bts;
3718 case 2:
3719 goto btr;
3720 case 3:
3721 goto btc;
3722 }
3723 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003724 case 0xbb:
3725 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003726 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003727 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003728 case 0xbc: { /* bsf */
3729 u8 zf;
3730 __asm__ ("bsf %2, %0; setz %1"
3731 : "=r"(c->dst.val), "=q"(zf)
3732 : "r"(c->src.val));
3733 ctxt->eflags &= ~X86_EFLAGS_ZF;
3734 if (zf) {
3735 ctxt->eflags |= X86_EFLAGS_ZF;
3736 c->dst.type = OP_NONE; /* Disable writeback. */
3737 }
3738 break;
3739 }
3740 case 0xbd: { /* bsr */
3741 u8 zf;
3742 __asm__ ("bsr %2, %0; setz %1"
3743 : "=r"(c->dst.val), "=q"(zf)
3744 : "r"(c->src.val));
3745 ctxt->eflags &= ~X86_EFLAGS_ZF;
3746 if (zf) {
3747 ctxt->eflags |= X86_EFLAGS_ZF;
3748 c->dst.type = OP_NONE; /* Disable writeback. */
3749 }
3750 break;
3751 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003752 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003753 c->dst.bytes = c->op_bytes;
3754 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3755 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756 break;
Wei Yongjun92f738a52010-08-17 09:19:34 +08003757 case 0xc0 ... 0xc1: /* xadd */
3758 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3759 /* Write back the register source. */
3760 c->src.val = c->dst.orig_val;
3761 write_register_operand(&c->src);
3762 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003763 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003764 c->dst.bytes = c->op_bytes;
3765 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3766 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003767 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003768 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003769 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003770 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003771 default:
3772 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003774
3775 if (rc != X86EMUL_CONTINUE)
3776 goto done;
3777
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778 goto writeback;
3779
3780cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003781 return -1;
3782}