blob: 223c536b7f71e34cf8a5825b77a093a3817bb70a [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{
Avi Kivityda9cb572010-11-22 17:53:21 +0200472 ctxt->exception.vector = vec;
473 ctxt->exception.error_code = error;
474 ctxt->exception.error_code_valid = valid;
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
Gleb Natapov54b84862010-04-28 19:15:44 +0300482static void emulate_ud(struct x86_emulate_ctxt *ctxt)
483{
484 emulate_exception(ctxt, UD_VECTOR, 0, false);
485}
486
487static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
488{
489 emulate_exception(ctxt, TS_VECTOR, err, true);
490}
491
Avi Kivity34d1f492010-08-26 11:59:01 +0300492static int emulate_de(struct x86_emulate_ctxt *ctxt)
493{
494 emulate_exception(ctxt, DE_VECTOR, 0, false);
495 return X86EMUL_PROPAGATE_FAULT;
496}
497
Avi Kivity62266862007-11-20 13:15:52 +0200498static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
499 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300500 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200501{
502 struct fetch_cache *fc = &ctxt->decode.fetch;
503 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300504 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200505
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300506 if (eip == fc->end) {
507 cur_size = fc->end - fc->start;
508 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
509 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200510 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900511 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200512 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300513 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200514 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300515 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900516 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200517}
518
519static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
520 struct x86_emulate_ops *ops,
521 unsigned long eip, void *dest, unsigned size)
522{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900523 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200524
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200525 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200526 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200527 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200528 while (size--) {
529 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900530 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200531 return rc;
532 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900533 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200534}
535
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000536/*
537 * Given the 'reg' portion of a ModRM byte, and a register block, return a
538 * pointer into the block that addresses the relevant register.
539 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
540 */
541static void *decode_register(u8 modrm_reg, unsigned long *regs,
542 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543{
544 void *p;
545
546 p = &regs[modrm_reg];
547 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
548 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
549 return p;
550}
551
552static int read_descriptor(struct x86_emulate_ctxt *ctxt,
553 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200554 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 u16 *size, unsigned long *address, int op_bytes)
556{
557 int rc;
558
559 if (op_bytes == 2)
560 op_bytes = 3;
561 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200562 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200563 ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900564 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200566 addr.ea += 2;
567 rc = ops->read_std(linear(ctxt, addr), address, op_bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200568 ctxt->vcpu, &ctxt->exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569 return rc;
570}
571
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300572static int test_cc(unsigned int condition, unsigned int flags)
573{
574 int rc = 0;
575
576 switch ((condition & 15) >> 1) {
577 case 0: /* o */
578 rc |= (flags & EFLG_OF);
579 break;
580 case 1: /* b/c/nae */
581 rc |= (flags & EFLG_CF);
582 break;
583 case 2: /* z/e */
584 rc |= (flags & EFLG_ZF);
585 break;
586 case 3: /* be/na */
587 rc |= (flags & (EFLG_CF|EFLG_ZF));
588 break;
589 case 4: /* s */
590 rc |= (flags & EFLG_SF);
591 break;
592 case 5: /* p/pe */
593 rc |= (flags & EFLG_PF);
594 break;
595 case 7: /* le/ng */
596 rc |= (flags & EFLG_ZF);
597 /* fall through */
598 case 6: /* l/nge */
599 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
600 break;
601 }
602
603 /* Odd condition identifiers (lsb == 1) have inverted sense. */
604 return (!!rc ^ (condition & 1));
605}
606
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300607static void fetch_register_operand(struct operand *op)
608{
609 switch (op->bytes) {
610 case 1:
611 op->val = *(u8 *)op->addr.reg;
612 break;
613 case 2:
614 op->val = *(u16 *)op->addr.reg;
615 break;
616 case 4:
617 op->val = *(u32 *)op->addr.reg;
618 break;
619 case 8:
620 op->val = *(u64 *)op->addr.reg;
621 break;
622 }
623}
624
Avi Kivity3c118e22007-10-31 10:27:04 +0200625static void decode_register_operand(struct operand *op,
626 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200627 int inhibit_bytereg)
628{
Avi Kivity33615aa2007-10-31 11:15:56 +0200629 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200630 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200631
632 if (!(c->d & ModRM))
633 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200634 op->type = OP_REG;
635 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300636 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200637 op->bytes = 1;
638 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300639 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200640 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200641 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300642 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200643 op->orig_val = op->val;
644}
645
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200646static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300647 struct x86_emulate_ops *ops,
648 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200649{
650 struct decode_cache *c = &ctxt->decode;
651 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700652 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900653 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300654 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200655
656 if (c->rex_prefix) {
657 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
658 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
659 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
660 }
661
662 c->modrm = insn_fetch(u8, 1, c->eip);
663 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
664 c->modrm_reg |= (c->modrm & 0x38) >> 3;
665 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300666 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200667
668 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300669 op->type = OP_REG;
670 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
671 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300672 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300673 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200674 return rc;
675 }
676
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300677 op->type = OP_MEM;
678
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200679 if (c->ad_bytes == 2) {
680 unsigned bx = c->regs[VCPU_REGS_RBX];
681 unsigned bp = c->regs[VCPU_REGS_RBP];
682 unsigned si = c->regs[VCPU_REGS_RSI];
683 unsigned di = c->regs[VCPU_REGS_RDI];
684
685 /* 16-bit ModR/M decode. */
686 switch (c->modrm_mod) {
687 case 0:
688 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300689 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200690 break;
691 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300692 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200693 break;
694 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300695 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200696 break;
697 }
698 switch (c->modrm_rm) {
699 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300700 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200701 break;
702 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300703 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200704 break;
705 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300706 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200707 break;
708 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300709 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200710 break;
711 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300712 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200713 break;
714 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300715 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200716 break;
717 case 6:
718 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300719 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720 break;
721 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300722 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200723 break;
724 }
725 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
726 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300727 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300728 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200729 } else {
730 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700731 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200732 sib = insn_fetch(u8, 1, c->eip);
733 index_reg |= (sib >> 3) & 7;
734 base_reg |= sib & 7;
735 scale = sib >> 6;
736
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700737 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300738 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700739 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300740 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700741 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300742 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700743 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
744 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700745 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700746 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300747 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200748 switch (c->modrm_mod) {
749 case 0:
750 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300751 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200752 break;
753 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300754 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200755 break;
756 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300757 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200758 break;
759 }
760 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200761 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200762done:
763 return rc;
764}
765
766static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300767 struct x86_emulate_ops *ops,
768 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200769{
770 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900771 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200772
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300773 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200774 switch (c->ad_bytes) {
775 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200776 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200777 break;
778 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200779 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200780 break;
781 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200782 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200783 break;
784 }
785done:
786 return rc;
787}
788
Wei Yongjun35c843c2010-08-09 11:34:56 +0800789static void fetch_bit_operand(struct decode_cache *c)
790{
Sheng Yang7129eec2010-09-28 16:33:32 +0800791 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800792
Wei Yongjun3885f182010-08-09 11:37:37 +0800793 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800794 mask = ~(c->dst.bytes * 8 - 1);
795
796 if (c->src.bytes == 2)
797 sv = (s16)c->src.val & (s16)mask;
798 else if (c->src.bytes == 4)
799 sv = (s32)c->src.val & (s32)mask;
800
Avi Kivity90de84f2010-11-17 15:28:21 +0200801 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800802 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800803
804 /* only subword offset */
805 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800806}
807
Gleb Natapov9de41572010-04-28 19:15:22 +0300808static int read_emulated(struct x86_emulate_ctxt *ctxt,
809 struct x86_emulate_ops *ops,
810 unsigned long addr, void *dest, unsigned size)
811{
812 int rc;
813 struct read_cache *mc = &ctxt->decode.mem_read;
814
815 while (size) {
816 int n = min(size, 8u);
817 size -= n;
818 if (mc->pos < mc->end)
819 goto read_cached;
820
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200821 rc = ops->read_emulated(addr, mc->data + mc->end, n,
822 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +0300823 if (rc != X86EMUL_CONTINUE)
824 return rc;
825 mc->end += n;
826
827 read_cached:
828 memcpy(dest, mc->data + mc->pos, n);
829 mc->pos += n;
830 dest += n;
831 addr += n;
832 }
833 return X86EMUL_CONTINUE;
834}
835
Gleb Natapov7b262e92010-03-18 15:20:27 +0200836static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
837 struct x86_emulate_ops *ops,
838 unsigned int size, unsigned short port,
839 void *dest)
840{
841 struct read_cache *rc = &ctxt->decode.io_read;
842
843 if (rc->pos == rc->end) { /* refill pio read ahead */
844 struct decode_cache *c = &ctxt->decode;
845 unsigned int in_page, n;
846 unsigned int count = c->rep_prefix ?
847 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
848 in_page = (ctxt->eflags & EFLG_DF) ?
849 offset_in_page(c->regs[VCPU_REGS_RDI]) :
850 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
851 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
852 count);
853 if (n == 0)
854 n = 1;
855 rc->pos = rc->end = 0;
856 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
857 return 0;
858 rc->end = n * size;
859 }
860
861 memcpy(dest, rc->data + rc->pos, size);
862 rc->pos += size;
863 return 1;
864}
865
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200866static u32 desc_limit_scaled(struct desc_struct *desc)
867{
868 u32 limit = get_desc_limit(desc);
869
870 return desc->g ? (limit << 12) | 0xfff : limit;
871}
872
873static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
874 struct x86_emulate_ops *ops,
875 u16 selector, struct desc_ptr *dt)
876{
877 if (selector & 1 << 2) {
878 struct desc_struct desc;
879 memset (dt, 0, sizeof *dt);
880 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
881 return;
882
883 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
884 dt->address = get_desc_base(&desc);
885 } else
886 ops->get_gdt(dt, ctxt->vcpu);
887}
888
889/* allowed just for 8 bytes segments */
890static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
891 struct x86_emulate_ops *ops,
892 u16 selector, struct desc_struct *desc)
893{
894 struct desc_ptr dt;
895 u16 index = selector >> 3;
896 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200897 ulong addr;
898
899 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
900
901 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300902 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200903 return X86EMUL_PROPAGATE_FAULT;
904 }
905 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200906 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
907 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200908
909 return ret;
910}
911
912/* allowed just for 8 bytes segments */
913static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
914 struct x86_emulate_ops *ops,
915 u16 selector, struct desc_struct *desc)
916{
917 struct desc_ptr dt;
918 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200919 ulong addr;
920 int ret;
921
922 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
923
924 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300925 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200926 return X86EMUL_PROPAGATE_FAULT;
927 }
928
929 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200930 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
931 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200932
933 return ret;
934}
935
936static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
937 struct x86_emulate_ops *ops,
938 u16 selector, int seg)
939{
940 struct desc_struct seg_desc;
941 u8 dpl, rpl, cpl;
942 unsigned err_vec = GP_VECTOR;
943 u32 err_code = 0;
944 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
945 int ret;
946
947 memset(&seg_desc, 0, sizeof seg_desc);
948
949 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
950 || ctxt->mode == X86EMUL_MODE_REAL) {
951 /* set real mode segment descriptor */
952 set_desc_base(&seg_desc, selector << 4);
953 set_desc_limit(&seg_desc, 0xffff);
954 seg_desc.type = 3;
955 seg_desc.p = 1;
956 seg_desc.s = 1;
957 goto load;
958 }
959
960 /* NULL selector is not valid for TR, CS and SS */
961 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
962 && null_selector)
963 goto exception;
964
965 /* TR should be in GDT only */
966 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
967 goto exception;
968
969 if (null_selector) /* for NULL selector skip all following checks */
970 goto load;
971
972 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
973 if (ret != X86EMUL_CONTINUE)
974 return ret;
975
976 err_code = selector & 0xfffc;
977 err_vec = GP_VECTOR;
978
979 /* can't load system descriptor into segment selecor */
980 if (seg <= VCPU_SREG_GS && !seg_desc.s)
981 goto exception;
982
983 if (!seg_desc.p) {
984 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
985 goto exception;
986 }
987
988 rpl = selector & 3;
989 dpl = seg_desc.dpl;
990 cpl = ops->cpl(ctxt->vcpu);
991
992 switch (seg) {
993 case VCPU_SREG_SS:
994 /*
995 * segment is not a writable data segment or segment
996 * selector's RPL != CPL or segment selector's RPL != CPL
997 */
998 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
999 goto exception;
1000 break;
1001 case VCPU_SREG_CS:
1002 if (!(seg_desc.type & 8))
1003 goto exception;
1004
1005 if (seg_desc.type & 4) {
1006 /* conforming */
1007 if (dpl > cpl)
1008 goto exception;
1009 } else {
1010 /* nonconforming */
1011 if (rpl > cpl || dpl != cpl)
1012 goto exception;
1013 }
1014 /* CS(RPL) <- CPL */
1015 selector = (selector & 0xfffc) | cpl;
1016 break;
1017 case VCPU_SREG_TR:
1018 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1019 goto exception;
1020 break;
1021 case VCPU_SREG_LDTR:
1022 if (seg_desc.s || seg_desc.type != 2)
1023 goto exception;
1024 break;
1025 default: /* DS, ES, FS, or GS */
1026 /*
1027 * segment is not a data or readable code segment or
1028 * ((segment is a data or nonconforming code segment)
1029 * and (both RPL and CPL > DPL))
1030 */
1031 if ((seg_desc.type & 0xa) == 0x8 ||
1032 (((seg_desc.type & 0xc) != 0xc) &&
1033 (rpl > dpl && cpl > dpl)))
1034 goto exception;
1035 break;
1036 }
1037
1038 if (seg_desc.s) {
1039 /* mark segment as accessed */
1040 seg_desc.type |= 1;
1041 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1042 if (ret != X86EMUL_CONTINUE)
1043 return ret;
1044 }
1045load:
1046 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1047 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1048 return X86EMUL_CONTINUE;
1049exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001050 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001051 return X86EMUL_PROPAGATE_FAULT;
1052}
1053
Wei Yongjun31be40b2010-08-17 09:17:30 +08001054static void write_register_operand(struct operand *op)
1055{
1056 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1057 switch (op->bytes) {
1058 case 1:
1059 *(u8 *)op->addr.reg = (u8)op->val;
1060 break;
1061 case 2:
1062 *(u16 *)op->addr.reg = (u16)op->val;
1063 break;
1064 case 4:
1065 *op->addr.reg = (u32)op->val;
1066 break; /* 64b: zero-extend */
1067 case 8:
1068 *op->addr.reg = op->val;
1069 break;
1070 }
1071}
1072
Wei Yongjunc37eda12010-06-15 09:03:33 +08001073static inline int writeback(struct x86_emulate_ctxt *ctxt,
1074 struct x86_emulate_ops *ops)
1075{
1076 int rc;
1077 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001078
1079 switch (c->dst.type) {
1080 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001081 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001082 break;
1083 case OP_MEM:
1084 if (c->lock_prefix)
1085 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001086 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001087 &c->dst.orig_val,
1088 &c->dst.val,
1089 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001090 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001091 ctxt->vcpu);
1092 else
1093 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001094 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001095 &c->dst.val,
1096 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001097 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001098 ctxt->vcpu);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001099 if (rc != X86EMUL_CONTINUE)
1100 return rc;
1101 break;
1102 case OP_NONE:
1103 /* no writeback */
1104 break;
1105 default:
1106 break;
1107 }
1108 return X86EMUL_CONTINUE;
1109}
1110
Gleb Natapov79168fd2010-04-28 19:15:30 +03001111static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1112 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001113{
1114 struct decode_cache *c = &ctxt->decode;
1115
1116 c->dst.type = OP_MEM;
1117 c->dst.bytes = c->op_bytes;
1118 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001119 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001120 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1121 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001122}
1123
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001124static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001125 struct x86_emulate_ops *ops,
1126 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001127{
1128 struct decode_cache *c = &ctxt->decode;
1129 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001130 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001131
Avi Kivity90de84f2010-11-17 15:28:21 +02001132 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1133 addr.seg = VCPU_SREG_SS;
1134 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001135 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001136 return rc;
1137
Avi Kivity350f69d2009-01-05 11:12:40 +02001138 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001139 return rc;
1140}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001141
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001142static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1143 struct x86_emulate_ops *ops,
1144 void *dest, int len)
1145{
1146 int rc;
1147 unsigned long val, change_mask;
1148 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001149 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001150
1151 rc = emulate_pop(ctxt, ops, &val, len);
1152 if (rc != X86EMUL_CONTINUE)
1153 return rc;
1154
1155 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1156 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1157
1158 switch(ctxt->mode) {
1159 case X86EMUL_MODE_PROT64:
1160 case X86EMUL_MODE_PROT32:
1161 case X86EMUL_MODE_PROT16:
1162 if (cpl == 0)
1163 change_mask |= EFLG_IOPL;
1164 if (cpl <= iopl)
1165 change_mask |= EFLG_IF;
1166 break;
1167 case X86EMUL_MODE_VM86:
1168 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001169 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001170 return X86EMUL_PROPAGATE_FAULT;
1171 }
1172 change_mask |= EFLG_IF;
1173 break;
1174 default: /* real mode */
1175 change_mask |= (EFLG_IOPL | EFLG_IF);
1176 break;
1177 }
1178
1179 *(unsigned long *)dest =
1180 (ctxt->eflags & ~change_mask) | (val & change_mask);
1181
1182 return rc;
1183}
1184
Gleb Natapov79168fd2010-04-28 19:15:30 +03001185static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1186 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001187{
1188 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001189
Gleb Natapov79168fd2010-04-28 19:15:30 +03001190 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001191
Gleb Natapov79168fd2010-04-28 19:15:30 +03001192 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001193}
1194
1195static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1196 struct x86_emulate_ops *ops, int seg)
1197{
1198 struct decode_cache *c = &ctxt->decode;
1199 unsigned long selector;
1200 int rc;
1201
1202 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001203 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001204 return rc;
1205
Gleb Natapov2e873022010-03-18 15:20:18 +02001206 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001207 return rc;
1208}
1209
Wei Yongjunc37eda12010-06-15 09:03:33 +08001210static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001211 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001212{
1213 struct decode_cache *c = &ctxt->decode;
1214 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001215 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001216 int reg = VCPU_REGS_RAX;
1217
1218 while (reg <= VCPU_REGS_RDI) {
1219 (reg == VCPU_REGS_RSP) ?
1220 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1221
Gleb Natapov79168fd2010-04-28 19:15:30 +03001222 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001223
1224 rc = writeback(ctxt, ops);
1225 if (rc != X86EMUL_CONTINUE)
1226 return rc;
1227
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001228 ++reg;
1229 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001230
1231 /* Disable writeback. */
1232 c->dst.type = OP_NONE;
1233
1234 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001235}
1236
1237static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1238 struct x86_emulate_ops *ops)
1239{
1240 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001241 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001242 int reg = VCPU_REGS_RDI;
1243
1244 while (reg >= VCPU_REGS_RAX) {
1245 if (reg == VCPU_REGS_RSP) {
1246 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1247 c->op_bytes);
1248 --reg;
1249 }
1250
1251 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001252 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001253 break;
1254 --reg;
1255 }
1256 return rc;
1257}
1258
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001259int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1260 struct x86_emulate_ops *ops, int irq)
1261{
1262 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001263 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001264 struct desc_ptr dt;
1265 gva_t cs_addr;
1266 gva_t eip_addr;
1267 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001268
1269 /* TODO: Add limit checks */
1270 c->src.val = ctxt->eflags;
1271 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001272 rc = writeback(ctxt, ops);
1273 if (rc != X86EMUL_CONTINUE)
1274 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001275
1276 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1277
1278 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1279 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001280 rc = writeback(ctxt, ops);
1281 if (rc != X86EMUL_CONTINUE)
1282 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001283
1284 c->src.val = c->eip;
1285 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001286 rc = writeback(ctxt, ops);
1287 if (rc != X86EMUL_CONTINUE)
1288 return rc;
1289
1290 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001291
1292 ops->get_idt(&dt, ctxt->vcpu);
1293
1294 eip_addr = dt.address + (irq << 2);
1295 cs_addr = dt.address + (irq << 2) + 2;
1296
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001297 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001298 if (rc != X86EMUL_CONTINUE)
1299 return rc;
1300
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001301 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001302 if (rc != X86EMUL_CONTINUE)
1303 return rc;
1304
1305 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1306 if (rc != X86EMUL_CONTINUE)
1307 return rc;
1308
1309 c->eip = eip;
1310
1311 return rc;
1312}
1313
1314static int emulate_int(struct x86_emulate_ctxt *ctxt,
1315 struct x86_emulate_ops *ops, int irq)
1316{
1317 switch(ctxt->mode) {
1318 case X86EMUL_MODE_REAL:
1319 return emulate_int_real(ctxt, ops, irq);
1320 case X86EMUL_MODE_VM86:
1321 case X86EMUL_MODE_PROT16:
1322 case X86EMUL_MODE_PROT32:
1323 case X86EMUL_MODE_PROT64:
1324 default:
1325 /* Protected mode interrupts unimplemented yet */
1326 return X86EMUL_UNHANDLEABLE;
1327 }
1328}
1329
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001330static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1331 struct x86_emulate_ops *ops)
1332{
1333 struct decode_cache *c = &ctxt->decode;
1334 int rc = X86EMUL_CONTINUE;
1335 unsigned long temp_eip = 0;
1336 unsigned long temp_eflags = 0;
1337 unsigned long cs = 0;
1338 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1339 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1340 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1341 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1342
1343 /* TODO: Add stack limit check */
1344
1345 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1346
1347 if (rc != X86EMUL_CONTINUE)
1348 return rc;
1349
1350 if (temp_eip & ~0xffff) {
1351 emulate_gp(ctxt, 0);
1352 return X86EMUL_PROPAGATE_FAULT;
1353 }
1354
1355 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1356
1357 if (rc != X86EMUL_CONTINUE)
1358 return rc;
1359
1360 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1361
1362 if (rc != X86EMUL_CONTINUE)
1363 return rc;
1364
1365 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1366
1367 if (rc != X86EMUL_CONTINUE)
1368 return rc;
1369
1370 c->eip = temp_eip;
1371
1372
1373 if (c->op_bytes == 4)
1374 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1375 else if (c->op_bytes == 2) {
1376 ctxt->eflags &= ~0xffff;
1377 ctxt->eflags |= temp_eflags;
1378 }
1379
1380 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1381 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1382
1383 return rc;
1384}
1385
1386static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1387 struct x86_emulate_ops* ops)
1388{
1389 switch(ctxt->mode) {
1390 case X86EMUL_MODE_REAL:
1391 return emulate_iret_real(ctxt, ops);
1392 case X86EMUL_MODE_VM86:
1393 case X86EMUL_MODE_PROT16:
1394 case X86EMUL_MODE_PROT32:
1395 case X86EMUL_MODE_PROT64:
1396 default:
1397 /* iret from protected mode unimplemented yet */
1398 return X86EMUL_UNHANDLEABLE;
1399 }
1400}
1401
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001402static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1403 struct x86_emulate_ops *ops)
1404{
1405 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001406
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001407 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001408}
1409
Laurent Vivier05f086f2007-09-24 11:10:55 +02001410static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001411{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001412 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001413 switch (c->modrm_reg) {
1414 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001415 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001416 break;
1417 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001418 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001419 break;
1420 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001421 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001422 break;
1423 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001424 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001425 break;
1426 case 4: /* sal/shl */
1427 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001428 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001429 break;
1430 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001431 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001432 break;
1433 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001434 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001435 break;
1436 }
1437}
1438
1439static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001440 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001441{
1442 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001443 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1444 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001445 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001446
1447 switch (c->modrm_reg) {
1448 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001449 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001450 break;
1451 case 2: /* not */
1452 c->dst.val = ~c->dst.val;
1453 break;
1454 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001455 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001456 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001457 case 4: /* mul */
1458 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1459 break;
1460 case 5: /* imul */
1461 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1462 break;
1463 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001464 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1465 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001466 break;
1467 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001468 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1469 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001470 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001471 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001472 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001473 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001474 if (de)
1475 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001476 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001477}
1478
1479static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001480 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001481{
1482 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001483
1484 switch (c->modrm_reg) {
1485 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001486 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001487 break;
1488 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001489 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001490 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001491 case 2: /* call near abs */ {
1492 long int old_eip;
1493 old_eip = c->eip;
1494 c->eip = c->src.val;
1495 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001496 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001497 break;
1498 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001499 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001500 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001501 break;
1502 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001503 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001504 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001505 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001506 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001507}
1508
1509static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001510 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001511{
1512 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001513 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001514
1515 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1516 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001517 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1518 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001519 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001520 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001521 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1522 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001523
Laurent Vivier05f086f2007-09-24 11:10:55 +02001524 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001525 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001526 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001527}
1528
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001529static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1530 struct x86_emulate_ops *ops)
1531{
1532 struct decode_cache *c = &ctxt->decode;
1533 int rc;
1534 unsigned long cs;
1535
1536 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001537 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001538 return rc;
1539 if (c->op_bytes == 4)
1540 c->eip = (u32)c->eip;
1541 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001542 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001543 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001544 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001545 return rc;
1546}
1547
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001548static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1549 struct x86_emulate_ops *ops, int seg)
1550{
1551 struct decode_cache *c = &ctxt->decode;
1552 unsigned short sel;
1553 int rc;
1554
1555 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1556
1557 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1558 if (rc != X86EMUL_CONTINUE)
1559 return rc;
1560
1561 c->dst.val = c->src.val;
1562 return rc;
1563}
1564
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001565static inline void
1566setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001567 struct x86_emulate_ops *ops, struct desc_struct *cs,
1568 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001569{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001570 memset(cs, 0, sizeof(struct desc_struct));
1571 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1572 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001573
1574 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001575 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001576 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001577 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001578 cs->type = 0x0b; /* Read, Execute, Accessed */
1579 cs->s = 1;
1580 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001581 cs->p = 1;
1582 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001583
Gleb Natapov79168fd2010-04-28 19:15:30 +03001584 set_desc_base(ss, 0); /* flat segment */
1585 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001586 ss->g = 1; /* 4kb granularity */
1587 ss->s = 1;
1588 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001589 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001590 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001591 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001592}
1593
1594static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001595emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001596{
1597 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001598 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001599 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001600 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001601
1602 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001603 if (ctxt->mode == X86EMUL_MODE_REAL ||
1604 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001605 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001606 return X86EMUL_PROPAGATE_FAULT;
1607 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001608
Gleb Natapov79168fd2010-04-28 19:15:30 +03001609 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001610
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001611 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001612 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001613 cs_sel = (u16)(msr_data & 0xfffc);
1614 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001615
1616 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001617 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001618 cs.l = 1;
1619 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001620 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1621 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1622 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1623 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001624
1625 c->regs[VCPU_REGS_RCX] = c->eip;
1626 if (is_long_mode(ctxt->vcpu)) {
1627#ifdef CONFIG_X86_64
1628 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1629
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001630 ops->get_msr(ctxt->vcpu,
1631 ctxt->mode == X86EMUL_MODE_PROT64 ?
1632 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001633 c->eip = msr_data;
1634
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001635 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001636 ctxt->eflags &= ~(msr_data | EFLG_RF);
1637#endif
1638 } else {
1639 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001640 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001641 c->eip = (u32)msr_data;
1642
1643 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1644 }
1645
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001646 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001647}
1648
Andre Przywara8c604352009-06-18 12:56:01 +02001649static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001650emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001651{
1652 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001653 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001654 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001655 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001656
Gleb Natapova0044752010-02-10 14:21:31 +02001657 /* inject #GP if in real mode */
1658 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001659 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001660 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001661 }
1662
1663 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1664 * Therefore, we inject an #UD.
1665 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001666 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001667 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001668 return X86EMUL_PROPAGATE_FAULT;
1669 }
Andre Przywara8c604352009-06-18 12:56:01 +02001670
Gleb Natapov79168fd2010-04-28 19:15:30 +03001671 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001672
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001673 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001674 switch (ctxt->mode) {
1675 case X86EMUL_MODE_PROT32:
1676 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001677 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001678 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001679 }
1680 break;
1681 case X86EMUL_MODE_PROT64:
1682 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001683 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001684 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001685 }
1686 break;
1687 }
1688
1689 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001690 cs_sel = (u16)msr_data;
1691 cs_sel &= ~SELECTOR_RPL_MASK;
1692 ss_sel = cs_sel + 8;
1693 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001694 if (ctxt->mode == X86EMUL_MODE_PROT64
1695 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001696 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001697 cs.l = 1;
1698 }
1699
Gleb Natapov79168fd2010-04-28 19:15:30 +03001700 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1701 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1702 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1703 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001704
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001705 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001706 c->eip = msr_data;
1707
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001708 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001709 c->regs[VCPU_REGS_RSP] = msr_data;
1710
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001711 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001712}
1713
Andre Przywara4668f052009-06-18 12:56:02 +02001714static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001715emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001716{
1717 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001718 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001719 u64 msr_data;
1720 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001721 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001722
Gleb Natapova0044752010-02-10 14:21:31 +02001723 /* inject #GP if in real mode or Virtual 8086 mode */
1724 if (ctxt->mode == X86EMUL_MODE_REAL ||
1725 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001726 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001727 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001728 }
1729
Gleb Natapov79168fd2010-04-28 19:15:30 +03001730 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001731
1732 if ((c->rex_prefix & 0x8) != 0x0)
1733 usermode = X86EMUL_MODE_PROT64;
1734 else
1735 usermode = X86EMUL_MODE_PROT32;
1736
1737 cs.dpl = 3;
1738 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001739 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001740 switch (usermode) {
1741 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001742 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001743 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001744 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001745 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001746 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001747 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001748 break;
1749 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001750 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001751 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001752 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001753 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001754 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001755 ss_sel = cs_sel + 8;
1756 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001757 cs.l = 1;
1758 break;
1759 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001760 cs_sel |= SELECTOR_RPL_MASK;
1761 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001762
Gleb Natapov79168fd2010-04-28 19:15:30 +03001763 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1764 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1765 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1766 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001767
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001768 c->eip = c->regs[VCPU_REGS_RDX];
1769 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001770
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001771 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001772}
1773
Gleb Natapov9c537242010-03-18 15:20:05 +02001774static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1775 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001776{
1777 int iopl;
1778 if (ctxt->mode == X86EMUL_MODE_REAL)
1779 return false;
1780 if (ctxt->mode == X86EMUL_MODE_VM86)
1781 return true;
1782 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001783 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001784}
1785
1786static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1787 struct x86_emulate_ops *ops,
1788 u16 port, u16 len)
1789{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001790 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001791 int r;
1792 u16 io_bitmap_ptr;
1793 u8 perm, bit_idx = port & 0x7;
1794 unsigned mask = (1 << len) - 1;
1795
Gleb Natapov79168fd2010-04-28 19:15:30 +03001796 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1797 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001798 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001799 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001800 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001801 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1802 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001803 if (r != X86EMUL_CONTINUE)
1804 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001805 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001806 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001807 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1808 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001809 if (r != X86EMUL_CONTINUE)
1810 return false;
1811 if ((perm >> bit_idx) & mask)
1812 return false;
1813 return true;
1814}
1815
1816static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1817 struct x86_emulate_ops *ops,
1818 u16 port, u16 len)
1819{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001820 if (ctxt->perm_ok)
1821 return true;
1822
Gleb Natapov9c537242010-03-18 15:20:05 +02001823 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001824 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1825 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001826
1827 ctxt->perm_ok = true;
1828
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001829 return true;
1830}
1831
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001832static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1833 struct x86_emulate_ops *ops,
1834 struct tss_segment_16 *tss)
1835{
1836 struct decode_cache *c = &ctxt->decode;
1837
1838 tss->ip = c->eip;
1839 tss->flag = ctxt->eflags;
1840 tss->ax = c->regs[VCPU_REGS_RAX];
1841 tss->cx = c->regs[VCPU_REGS_RCX];
1842 tss->dx = c->regs[VCPU_REGS_RDX];
1843 tss->bx = c->regs[VCPU_REGS_RBX];
1844 tss->sp = c->regs[VCPU_REGS_RSP];
1845 tss->bp = c->regs[VCPU_REGS_RBP];
1846 tss->si = c->regs[VCPU_REGS_RSI];
1847 tss->di = c->regs[VCPU_REGS_RDI];
1848
1849 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1850 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1851 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1852 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1853 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1854}
1855
1856static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1857 struct x86_emulate_ops *ops,
1858 struct tss_segment_16 *tss)
1859{
1860 struct decode_cache *c = &ctxt->decode;
1861 int ret;
1862
1863 c->eip = tss->ip;
1864 ctxt->eflags = tss->flag | 2;
1865 c->regs[VCPU_REGS_RAX] = tss->ax;
1866 c->regs[VCPU_REGS_RCX] = tss->cx;
1867 c->regs[VCPU_REGS_RDX] = tss->dx;
1868 c->regs[VCPU_REGS_RBX] = tss->bx;
1869 c->regs[VCPU_REGS_RSP] = tss->sp;
1870 c->regs[VCPU_REGS_RBP] = tss->bp;
1871 c->regs[VCPU_REGS_RSI] = tss->si;
1872 c->regs[VCPU_REGS_RDI] = tss->di;
1873
1874 /*
1875 * SDM says that segment selectors are loaded before segment
1876 * descriptors
1877 */
1878 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1879 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1880 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1881 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1882 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1883
1884 /*
1885 * Now load segment descriptors. If fault happenes at this stage
1886 * it is handled in a context of new task
1887 */
1888 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1889 if (ret != X86EMUL_CONTINUE)
1890 return ret;
1891 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1892 if (ret != X86EMUL_CONTINUE)
1893 return ret;
1894 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1895 if (ret != X86EMUL_CONTINUE)
1896 return ret;
1897 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1898 if (ret != X86EMUL_CONTINUE)
1899 return ret;
1900 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1901 if (ret != X86EMUL_CONTINUE)
1902 return ret;
1903
1904 return X86EMUL_CONTINUE;
1905}
1906
1907static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1908 struct x86_emulate_ops *ops,
1909 u16 tss_selector, u16 old_tss_sel,
1910 ulong old_tss_base, struct desc_struct *new_desc)
1911{
1912 struct tss_segment_16 tss_seg;
1913 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001914 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001915
1916 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001917 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001918 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001919 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001920 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001921
1922 save_state_to_tss16(ctxt, ops, &tss_seg);
1923
1924 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001925 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001926 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001927 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001928 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001929
1930 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001931 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001932 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001933 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001934 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001935
1936 if (old_tss_sel != 0xffff) {
1937 tss_seg.prev_task_link = old_tss_sel;
1938
1939 ret = ops->write_std(new_tss_base,
1940 &tss_seg.prev_task_link,
1941 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001942 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001943 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001944 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001945 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001946 }
1947
1948 return load_state_from_tss16(ctxt, ops, &tss_seg);
1949}
1950
1951static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1952 struct x86_emulate_ops *ops,
1953 struct tss_segment_32 *tss)
1954{
1955 struct decode_cache *c = &ctxt->decode;
1956
1957 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1958 tss->eip = c->eip;
1959 tss->eflags = ctxt->eflags;
1960 tss->eax = c->regs[VCPU_REGS_RAX];
1961 tss->ecx = c->regs[VCPU_REGS_RCX];
1962 tss->edx = c->regs[VCPU_REGS_RDX];
1963 tss->ebx = c->regs[VCPU_REGS_RBX];
1964 tss->esp = c->regs[VCPU_REGS_RSP];
1965 tss->ebp = c->regs[VCPU_REGS_RBP];
1966 tss->esi = c->regs[VCPU_REGS_RSI];
1967 tss->edi = c->regs[VCPU_REGS_RDI];
1968
1969 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1970 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1971 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1972 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1973 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1974 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1975 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1976}
1977
1978static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1979 struct x86_emulate_ops *ops,
1980 struct tss_segment_32 *tss)
1981{
1982 struct decode_cache *c = &ctxt->decode;
1983 int ret;
1984
Gleb Natapov0f122442010-04-28 19:15:31 +03001985 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001986 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001987 return X86EMUL_PROPAGATE_FAULT;
1988 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001989 c->eip = tss->eip;
1990 ctxt->eflags = tss->eflags | 2;
1991 c->regs[VCPU_REGS_RAX] = tss->eax;
1992 c->regs[VCPU_REGS_RCX] = tss->ecx;
1993 c->regs[VCPU_REGS_RDX] = tss->edx;
1994 c->regs[VCPU_REGS_RBX] = tss->ebx;
1995 c->regs[VCPU_REGS_RSP] = tss->esp;
1996 c->regs[VCPU_REGS_RBP] = tss->ebp;
1997 c->regs[VCPU_REGS_RSI] = tss->esi;
1998 c->regs[VCPU_REGS_RDI] = tss->edi;
1999
2000 /*
2001 * SDM says that segment selectors are loaded before segment
2002 * descriptors
2003 */
2004 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2005 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2006 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2007 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2008 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2009 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2010 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2011
2012 /*
2013 * Now load segment descriptors. If fault happenes at this stage
2014 * it is handled in a context of new task
2015 */
2016 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2017 if (ret != X86EMUL_CONTINUE)
2018 return ret;
2019 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2020 if (ret != X86EMUL_CONTINUE)
2021 return ret;
2022 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2023 if (ret != X86EMUL_CONTINUE)
2024 return ret;
2025 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2026 if (ret != X86EMUL_CONTINUE)
2027 return ret;
2028 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2029 if (ret != X86EMUL_CONTINUE)
2030 return ret;
2031 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2032 if (ret != X86EMUL_CONTINUE)
2033 return ret;
2034 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2035 if (ret != X86EMUL_CONTINUE)
2036 return ret;
2037
2038 return X86EMUL_CONTINUE;
2039}
2040
2041static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2042 struct x86_emulate_ops *ops,
2043 u16 tss_selector, u16 old_tss_sel,
2044 ulong old_tss_base, struct desc_struct *new_desc)
2045{
2046 struct tss_segment_32 tss_seg;
2047 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002048 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002049
2050 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002051 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002052 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002053 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002054 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002055
2056 save_state_to_tss32(ctxt, ops, &tss_seg);
2057
2058 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002059 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002060 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002061 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002062 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002063
2064 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002065 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002066 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002067 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002068 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002069
2070 if (old_tss_sel != 0xffff) {
2071 tss_seg.prev_task_link = old_tss_sel;
2072
2073 ret = ops->write_std(new_tss_base,
2074 &tss_seg.prev_task_link,
2075 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002076 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002077 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002078 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002079 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002080 }
2081
2082 return load_state_from_tss32(ctxt, ops, &tss_seg);
2083}
2084
2085static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002086 struct x86_emulate_ops *ops,
2087 u16 tss_selector, int reason,
2088 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002089{
2090 struct desc_struct curr_tss_desc, next_tss_desc;
2091 int ret;
2092 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2093 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002094 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002095 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002096
2097 /* FIXME: old_tss_base == ~0 ? */
2098
2099 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2100 if (ret != X86EMUL_CONTINUE)
2101 return ret;
2102 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2103 if (ret != X86EMUL_CONTINUE)
2104 return ret;
2105
2106 /* FIXME: check that next_tss_desc is tss */
2107
2108 if (reason != TASK_SWITCH_IRET) {
2109 if ((tss_selector & 3) > next_tss_desc.dpl ||
2110 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002111 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002112 return X86EMUL_PROPAGATE_FAULT;
2113 }
2114 }
2115
Gleb Natapovceffb452010-03-18 15:20:19 +02002116 desc_limit = desc_limit_scaled(&next_tss_desc);
2117 if (!next_tss_desc.p ||
2118 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2119 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002120 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002121 return X86EMUL_PROPAGATE_FAULT;
2122 }
2123
2124 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2125 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2126 write_segment_descriptor(ctxt, ops, old_tss_sel,
2127 &curr_tss_desc);
2128 }
2129
2130 if (reason == TASK_SWITCH_IRET)
2131 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2132
2133 /* set back link to prev task only if NT bit is set in eflags
2134 note that old_tss_sel is not used afetr this point */
2135 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2136 old_tss_sel = 0xffff;
2137
2138 if (next_tss_desc.type & 8)
2139 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2140 old_tss_base, &next_tss_desc);
2141 else
2142 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2143 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002144 if (ret != X86EMUL_CONTINUE)
2145 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002146
2147 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2148 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2149
2150 if (reason != TASK_SWITCH_IRET) {
2151 next_tss_desc.type |= (1 << 1); /* set busy flag */
2152 write_segment_descriptor(ctxt, ops, tss_selector,
2153 &next_tss_desc);
2154 }
2155
2156 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2157 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2158 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2159
Jan Kiszkae269fb22010-04-14 15:51:09 +02002160 if (has_error_code) {
2161 struct decode_cache *c = &ctxt->decode;
2162
2163 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2164 c->lock_prefix = 0;
2165 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002166 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002167 }
2168
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002169 return ret;
2170}
2171
2172int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002173 u16 tss_selector, int reason,
2174 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002175{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002176 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177 struct decode_cache *c = &ctxt->decode;
2178 int rc;
2179
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002181 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002182
Jan Kiszkae269fb22010-04-14 15:51:09 +02002183 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2184 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185
2186 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002187 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002188 if (rc == X86EMUL_CONTINUE)
2189 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190 }
2191
Gleb Natapov19d04432010-04-15 12:29:50 +03002192 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002193}
2194
Avi Kivity90de84f2010-11-17 15:28:21 +02002195static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002196 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002197{
2198 struct decode_cache *c = &ctxt->decode;
2199 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2200
Gleb Natapovd9271122010-03-18 15:20:22 +02002201 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002202 op->addr.mem.ea = register_address(c, c->regs[reg]);
2203 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002204}
2205
Avi Kivity63540382010-07-29 15:11:55 +03002206static int em_push(struct x86_emulate_ctxt *ctxt)
2207{
2208 emulate_push(ctxt, ctxt->ops);
2209 return X86EMUL_CONTINUE;
2210}
2211
Avi Kivity7af04fc2010-08-18 14:16:35 +03002212static int em_das(struct x86_emulate_ctxt *ctxt)
2213{
2214 struct decode_cache *c = &ctxt->decode;
2215 u8 al, old_al;
2216 bool af, cf, old_cf;
2217
2218 cf = ctxt->eflags & X86_EFLAGS_CF;
2219 al = c->dst.val;
2220
2221 old_al = al;
2222 old_cf = cf;
2223 cf = false;
2224 af = ctxt->eflags & X86_EFLAGS_AF;
2225 if ((al & 0x0f) > 9 || af) {
2226 al -= 6;
2227 cf = old_cf | (al >= 250);
2228 af = true;
2229 } else {
2230 af = false;
2231 }
2232 if (old_al > 0x99 || old_cf) {
2233 al -= 0x60;
2234 cf = true;
2235 }
2236
2237 c->dst.val = al;
2238 /* Set PF, ZF, SF */
2239 c->src.type = OP_IMM;
2240 c->src.val = 0;
2241 c->src.bytes = 1;
2242 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2243 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2244 if (cf)
2245 ctxt->eflags |= X86_EFLAGS_CF;
2246 if (af)
2247 ctxt->eflags |= X86_EFLAGS_AF;
2248 return X86EMUL_CONTINUE;
2249}
2250
Avi Kivity0ef753b2010-08-18 14:51:45 +03002251static int em_call_far(struct x86_emulate_ctxt *ctxt)
2252{
2253 struct decode_cache *c = &ctxt->decode;
2254 u16 sel, old_cs;
2255 ulong old_eip;
2256 int rc;
2257
2258 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2259 old_eip = c->eip;
2260
2261 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2262 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2263 return X86EMUL_CONTINUE;
2264
2265 c->eip = 0;
2266 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2267
2268 c->src.val = old_cs;
2269 emulate_push(ctxt, ctxt->ops);
2270 rc = writeback(ctxt, ctxt->ops);
2271 if (rc != X86EMUL_CONTINUE)
2272 return rc;
2273
2274 c->src.val = old_eip;
2275 emulate_push(ctxt, ctxt->ops);
2276 rc = writeback(ctxt, ctxt->ops);
2277 if (rc != X86EMUL_CONTINUE)
2278 return rc;
2279
2280 c->dst.type = OP_NONE;
2281
2282 return X86EMUL_CONTINUE;
2283}
2284
Avi Kivity40ece7c2010-08-18 15:12:09 +03002285static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2286{
2287 struct decode_cache *c = &ctxt->decode;
2288 int rc;
2289
2290 c->dst.type = OP_REG;
2291 c->dst.addr.reg = &c->eip;
2292 c->dst.bytes = c->op_bytes;
2293 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2294 if (rc != X86EMUL_CONTINUE)
2295 return rc;
2296 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2297 return X86EMUL_CONTINUE;
2298}
2299
Avi Kivity5c82aa22010-08-18 18:31:43 +03002300static int em_imul(struct x86_emulate_ctxt *ctxt)
2301{
2302 struct decode_cache *c = &ctxt->decode;
2303
2304 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2305 return X86EMUL_CONTINUE;
2306}
2307
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002308static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2309{
2310 struct decode_cache *c = &ctxt->decode;
2311
2312 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002313 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002314}
2315
Avi Kivity61429142010-08-19 15:13:00 +03002316static int em_cwd(struct x86_emulate_ctxt *ctxt)
2317{
2318 struct decode_cache *c = &ctxt->decode;
2319
2320 c->dst.type = OP_REG;
2321 c->dst.bytes = c->src.bytes;
2322 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2323 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2324
2325 return X86EMUL_CONTINUE;
2326}
2327
Avi Kivity48bb5d32010-08-18 18:54:34 +03002328static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2329{
2330 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2331 struct decode_cache *c = &ctxt->decode;
2332 u64 tsc = 0;
2333
2334 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD)) {
2335 emulate_gp(ctxt, 0);
2336 return X86EMUL_PROPAGATE_FAULT;
2337 }
2338 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2339 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2340 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2341 return X86EMUL_CONTINUE;
2342}
2343
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002344static int em_mov(struct x86_emulate_ctxt *ctxt)
2345{
2346 struct decode_cache *c = &ctxt->decode;
2347 c->dst.val = c->src.val;
2348 return X86EMUL_CONTINUE;
2349}
2350
Avi Kivity73fba5f2010-07-29 15:11:53 +03002351#define D(_y) { .flags = (_y) }
2352#define N D(0)
2353#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2354#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2355#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2356
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002357#define D2bv(_f) D((_f) | ByteOp), D(_f)
2358#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2359
Avi Kivity6230f7f2010-08-26 18:34:55 +03002360#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2361 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2362 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2363
2364
Avi Kivity73fba5f2010-07-29 15:11:53 +03002365static struct opcode group1[] = {
2366 X7(D(Lock)), N
2367};
2368
2369static struct opcode group1A[] = {
2370 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2371};
2372
2373static struct opcode group3[] = {
2374 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2375 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002376 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002377};
2378
2379static struct opcode group4[] = {
2380 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2381 N, N, N, N, N, N,
2382};
2383
2384static struct opcode group5[] = {
2385 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002386 D(SrcMem | ModRM | Stack),
2387 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002388 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2389 D(SrcMem | ModRM | Stack), N,
2390};
2391
2392static struct group_dual group7 = { {
2393 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2394 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002395 D(SrcMem16 | ModRM | Mov | Priv),
2396 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002397}, {
2398 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2399 D(SrcNone | ModRM | DstMem | Mov), N,
2400 D(SrcMem16 | ModRM | Mov | Priv), N,
2401} };
2402
2403static struct opcode group8[] = {
2404 N, N, N, N,
2405 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2406 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2407};
2408
2409static struct group_dual group9 = { {
2410 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2411}, {
2412 N, N, N, N, N, N, N, N,
2413} };
2414
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002415static struct opcode group11[] = {
2416 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2417};
2418
Avi Kivity73fba5f2010-07-29 15:11:53 +03002419static struct opcode opcode_table[256] = {
2420 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002421 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002422 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2423 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002424 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002425 D(ImplicitOps | Stack | No64), N,
2426 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002427 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002428 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2429 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002430 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002431 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2432 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002433 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002434 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002435 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002436 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002437 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002438 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002439 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002440 /* 0x40 - 0x4F */
2441 X16(D(DstReg)),
2442 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002443 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002444 /* 0x58 - 0x5F */
2445 X8(D(DstReg | Stack)),
2446 /* 0x60 - 0x67 */
2447 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2448 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2449 N, N, N, N,
2450 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002451 I(SrcImm | Mov | Stack, em_push),
2452 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002453 I(SrcImmByte | Mov | Stack, em_push),
2454 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002455 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2456 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002457 /* 0x70 - 0x7F */
2458 X16(D(SrcImmByte)),
2459 /* 0x80 - 0x87 */
2460 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2461 G(DstMem | SrcImm | ModRM | Group, group1),
2462 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2463 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002464 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002465 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002466 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2467 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002468 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002469 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2470 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002471 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002472 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002473 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002474 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002475 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2476 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002477 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2478 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2479 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2480 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002481 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002482 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002483 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2484 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002485 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002486 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002487 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002488 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002489 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002490 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002491 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002492 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2493 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002494 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002495 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002496 /* 0xC8 - 0xCF */
2497 N, N, N, D(ImplicitOps | Stack),
2498 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2499 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002500 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002501 N, N, N, N,
2502 /* 0xD8 - 0xDF */
2503 N, N, N, N, N, N, N, N,
2504 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002505 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002506 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002507 /* 0xE8 - 0xEF */
2508 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2509 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002510 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002511 /* 0xF0 - 0xF7 */
2512 N, N, N, N,
2513 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2514 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002515 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002516 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2517};
2518
2519static struct opcode twobyte_table[256] = {
2520 /* 0x00 - 0x0F */
2521 N, GD(0, &group7), N, N,
2522 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2523 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2524 N, D(ImplicitOps | ModRM), N, N,
2525 /* 0x10 - 0x1F */
2526 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2527 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002528 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2529 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002530 N, N, N, N,
2531 N, N, N, N, N, N, N, N,
2532 /* 0x30 - 0x3F */
Avi Kivity48bb5d32010-08-18 18:54:34 +03002533 D(ImplicitOps | Priv), I(ImplicitOps, em_rdtsc),
2534 D(ImplicitOps | Priv), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002535 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2536 N, N, N, N, N, N, N, N,
2537 /* 0x40 - 0x4F */
2538 X16(D(DstReg | SrcMem | ModRM | Mov)),
2539 /* 0x50 - 0x5F */
2540 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2541 /* 0x60 - 0x6F */
2542 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2543 /* 0x70 - 0x7F */
2544 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2545 /* 0x80 - 0x8F */
2546 X16(D(SrcImm)),
2547 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002548 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002549 /* 0xA0 - 0xA7 */
2550 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2551 N, D(DstMem | SrcReg | ModRM | BitOp),
2552 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2553 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2554 /* 0xA8 - 0xAF */
2555 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2556 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2557 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2558 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002559 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002560 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002561 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002562 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2563 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2564 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002565 /* 0xB8 - 0xBF */
2566 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002567 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002568 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2569 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002570 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002571 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002572 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002573 N, N, N, GD(0, &group9),
2574 N, N, N, N, N, N, N, N,
2575 /* 0xD0 - 0xDF */
2576 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2577 /* 0xE0 - 0xEF */
2578 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2579 /* 0xF0 - 0xFF */
2580 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2581};
2582
2583#undef D
2584#undef N
2585#undef G
2586#undef GD
2587#undef I
2588
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002589#undef D2bv
2590#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002591#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002592
Avi Kivity39f21ee2010-08-18 19:20:21 +03002593static unsigned imm_size(struct decode_cache *c)
2594{
2595 unsigned size;
2596
2597 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2598 if (size == 8)
2599 size = 4;
2600 return size;
2601}
2602
2603static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2604 unsigned size, bool sign_extension)
2605{
2606 struct decode_cache *c = &ctxt->decode;
2607 struct x86_emulate_ops *ops = ctxt->ops;
2608 int rc = X86EMUL_CONTINUE;
2609
2610 op->type = OP_IMM;
2611 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002612 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002613 /* NB. Immediates are sign-extended as necessary. */
2614 switch (op->bytes) {
2615 case 1:
2616 op->val = insn_fetch(s8, 1, c->eip);
2617 break;
2618 case 2:
2619 op->val = insn_fetch(s16, 2, c->eip);
2620 break;
2621 case 4:
2622 op->val = insn_fetch(s32, 4, c->eip);
2623 break;
2624 }
2625 if (!sign_extension) {
2626 switch (op->bytes) {
2627 case 1:
2628 op->val &= 0xff;
2629 break;
2630 case 2:
2631 op->val &= 0xffff;
2632 break;
2633 case 4:
2634 op->val &= 0xffffffff;
2635 break;
2636 }
2637 }
2638done:
2639 return rc;
2640}
2641
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002642int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002643x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2644{
2645 struct x86_emulate_ops *ops = ctxt->ops;
2646 struct decode_cache *c = &ctxt->decode;
2647 int rc = X86EMUL_CONTINUE;
2648 int mode = ctxt->mode;
2649 int def_op_bytes, def_ad_bytes, dual, goffset;
2650 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002651 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002652
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002653 c->eip = ctxt->eip;
2654 c->fetch.start = c->fetch.end = c->eip;
2655 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2656
2657 switch (mode) {
2658 case X86EMUL_MODE_REAL:
2659 case X86EMUL_MODE_VM86:
2660 case X86EMUL_MODE_PROT16:
2661 def_op_bytes = def_ad_bytes = 2;
2662 break;
2663 case X86EMUL_MODE_PROT32:
2664 def_op_bytes = def_ad_bytes = 4;
2665 break;
2666#ifdef CONFIG_X86_64
2667 case X86EMUL_MODE_PROT64:
2668 def_op_bytes = 4;
2669 def_ad_bytes = 8;
2670 break;
2671#endif
2672 default:
2673 return -1;
2674 }
2675
2676 c->op_bytes = def_op_bytes;
2677 c->ad_bytes = def_ad_bytes;
2678
2679 /* Legacy prefixes. */
2680 for (;;) {
2681 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2682 case 0x66: /* operand-size override */
2683 /* switch between 2/4 bytes */
2684 c->op_bytes = def_op_bytes ^ 6;
2685 break;
2686 case 0x67: /* address-size override */
2687 if (mode == X86EMUL_MODE_PROT64)
2688 /* switch between 4/8 bytes */
2689 c->ad_bytes = def_ad_bytes ^ 12;
2690 else
2691 /* switch between 2/4 bytes */
2692 c->ad_bytes = def_ad_bytes ^ 6;
2693 break;
2694 case 0x26: /* ES override */
2695 case 0x2e: /* CS override */
2696 case 0x36: /* SS override */
2697 case 0x3e: /* DS override */
2698 set_seg_override(c, (c->b >> 3) & 3);
2699 break;
2700 case 0x64: /* FS override */
2701 case 0x65: /* GS override */
2702 set_seg_override(c, c->b & 7);
2703 break;
2704 case 0x40 ... 0x4f: /* REX */
2705 if (mode != X86EMUL_MODE_PROT64)
2706 goto done_prefixes;
2707 c->rex_prefix = c->b;
2708 continue;
2709 case 0xf0: /* LOCK */
2710 c->lock_prefix = 1;
2711 break;
2712 case 0xf2: /* REPNE/REPNZ */
2713 c->rep_prefix = REPNE_PREFIX;
2714 break;
2715 case 0xf3: /* REP/REPE/REPZ */
2716 c->rep_prefix = REPE_PREFIX;
2717 break;
2718 default:
2719 goto done_prefixes;
2720 }
2721
2722 /* Any legacy prefix after a REX prefix nullifies its effect. */
2723
2724 c->rex_prefix = 0;
2725 }
2726
2727done_prefixes:
2728
2729 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002730 if (c->rex_prefix & 8)
2731 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002732
2733 /* Opcode byte(s). */
2734 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002735 /* Two-byte opcode? */
2736 if (c->b == 0x0f) {
2737 c->twobyte = 1;
2738 c->b = insn_fetch(u8, 1, c->eip);
2739 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002740 }
2741 c->d = opcode.flags;
2742
2743 if (c->d & Group) {
2744 dual = c->d & GroupDual;
2745 c->modrm = insn_fetch(u8, 1, c->eip);
2746 --c->eip;
2747
2748 if (c->d & GroupDual) {
2749 g_mod012 = opcode.u.gdual->mod012;
2750 g_mod3 = opcode.u.gdual->mod3;
2751 } else
2752 g_mod012 = g_mod3 = opcode.u.group;
2753
2754 c->d &= ~(Group | GroupDual);
2755
2756 goffset = (c->modrm >> 3) & 7;
2757
2758 if ((c->modrm >> 6) == 3)
2759 opcode = g_mod3[goffset];
2760 else
2761 opcode = g_mod012[goffset];
2762 c->d |= opcode.flags;
2763 }
2764
2765 c->execute = opcode.u.execute;
2766
2767 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02002768 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002769 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002770
2771 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2772 c->op_bytes = 8;
2773
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002774 if (c->d & Op3264) {
2775 if (mode == X86EMUL_MODE_PROT64)
2776 c->op_bytes = 8;
2777 else
2778 c->op_bytes = 4;
2779 }
2780
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002781 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002782 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002783 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002784 if (!c->has_seg_override)
2785 set_seg_override(c, c->modrm_seg);
2786 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002787 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002788 if (rc != X86EMUL_CONTINUE)
2789 goto done;
2790
2791 if (!c->has_seg_override)
2792 set_seg_override(c, VCPU_SREG_DS);
2793
Avi Kivity90de84f2010-11-17 15:28:21 +02002794 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002795
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002796 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02002797 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002798
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002799 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02002800 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002801
2802 /*
2803 * Decode and fetch the source operand: register, memory
2804 * or immediate.
2805 */
2806 switch (c->d & SrcMask) {
2807 case SrcNone:
2808 break;
2809 case SrcReg:
2810 decode_register_operand(&c->src, c, 0);
2811 break;
2812 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002813 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002814 goto srcmem_common;
2815 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002816 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002817 goto srcmem_common;
2818 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002819 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002820 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002821 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002822 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002823 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002824 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002825 rc = decode_imm(ctxt, &c->src, 2, false);
2826 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002827 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002828 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2829 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002830 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002831 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002832 break;
2833 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002834 rc = decode_imm(ctxt, &c->src, 1, true);
2835 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002836 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002837 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002838 break;
2839 case SrcAcc:
2840 c->src.type = OP_REG;
2841 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002842 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002843 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002844 break;
2845 case SrcOne:
2846 c->src.bytes = 1;
2847 c->src.val = 1;
2848 break;
2849 case SrcSI:
2850 c->src.type = OP_MEM;
2851 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002852 c->src.addr.mem.ea =
2853 register_address(c, c->regs[VCPU_REGS_RSI]);
2854 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002855 c->src.val = 0;
2856 break;
2857 case SrcImmFAddr:
2858 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002859 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002860 c->src.bytes = c->op_bytes + 2;
2861 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2862 break;
2863 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002864 memop.bytes = c->op_bytes + 2;
2865 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002866 break;
2867 }
2868
Avi Kivity39f21ee2010-08-18 19:20:21 +03002869 if (rc != X86EMUL_CONTINUE)
2870 goto done;
2871
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002872 /*
2873 * Decode and fetch the second source operand: register, memory
2874 * or immediate.
2875 */
2876 switch (c->d & Src2Mask) {
2877 case Src2None:
2878 break;
2879 case Src2CL:
2880 c->src2.bytes = 1;
2881 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2882 break;
2883 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002884 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002885 break;
2886 case Src2One:
2887 c->src2.bytes = 1;
2888 c->src2.val = 1;
2889 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03002890 case Src2Imm:
2891 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
2892 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002893 }
2894
Avi Kivity39f21ee2010-08-18 19:20:21 +03002895 if (rc != X86EMUL_CONTINUE)
2896 goto done;
2897
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002898 /* Decode and fetch the destination operand: register or memory. */
2899 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002900 case DstReg:
2901 decode_register_operand(&c->dst, c,
2902 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2903 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08002904 case DstImmUByte:
2905 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002906 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08002907 c->dst.bytes = 1;
2908 c->dst.val = insn_fetch(u8, 1, c->eip);
2909 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002910 case DstMem:
2911 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002912 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002913 if ((c->d & DstMask) == DstMem64)
2914 c->dst.bytes = 8;
2915 else
2916 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002917 if (c->d & BitOp)
2918 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002919 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002920 break;
2921 case DstAcc:
2922 c->dst.type = OP_REG;
2923 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002924 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002925 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002926 c->dst.orig_val = c->dst.val;
2927 break;
2928 case DstDI:
2929 c->dst.type = OP_MEM;
2930 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002931 c->dst.addr.mem.ea =
2932 register_address(c, c->regs[VCPU_REGS_RDI]);
2933 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002934 c->dst.val = 0;
2935 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002936 case ImplicitOps:
2937 /* Special instructions do their own operand decoding. */
2938 default:
2939 c->dst.type = OP_NONE; /* Disable writeback. */
2940 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002941 }
2942
2943done:
2944 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2945}
2946
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03002947static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
2948{
2949 struct decode_cache *c = &ctxt->decode;
2950
2951 /* The second termination condition only applies for REPE
2952 * and REPNE. Test if the repeat string operation prefix is
2953 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2954 * corresponding termination condition according to:
2955 * - if REPE/REPZ and ZF = 0 then done
2956 * - if REPNE/REPNZ and ZF = 1 then done
2957 */
2958 if (((c->b == 0xa6) || (c->b == 0xa7) ||
2959 (c->b == 0xae) || (c->b == 0xaf))
2960 && (((c->rep_prefix == REPE_PREFIX) &&
2961 ((ctxt->eflags & EFLG_ZF) == 0))
2962 || ((c->rep_prefix == REPNE_PREFIX) &&
2963 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
2964 return true;
2965
2966 return false;
2967}
2968
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002969int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002970x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002971{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002972 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002973 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002974 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002975 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002976 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002977 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002978
Gleb Natapov9de41572010-04-28 19:15:22 +03002979 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002980
Gleb Natapov11616242010-02-11 14:43:14 +02002981 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002982 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02002983 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov11616242010-02-11 14:43:14 +02002984 goto done;
2985 }
2986
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002987 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002988 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002989 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02002990 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002991 goto done;
2992 }
2993
Avi Kivity081bca02010-08-26 11:06:15 +03002994 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
2995 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02002996 rc = X86EMUL_PROPAGATE_FAULT;
Avi Kivity081bca02010-08-26 11:06:15 +03002997 goto done;
2998 }
2999
Gleb Natapove92805a2010-02-10 14:21:35 +02003000 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003001 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003002 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003003 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapove92805a2010-02-10 14:21:35 +02003004 goto done;
3005 }
3006
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003007 if (c->rep_prefix && (c->d & String)) {
3008 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003009 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003010 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003011 goto done;
3012 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003013 }
3014
Wei Yongjunc483c022010-08-06 15:36:36 +08003015 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003016 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03003017 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003018 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003019 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003020 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003021 }
3022
Gleb Natapove35b7b92010-02-25 16:36:42 +02003023 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003024 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003025 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003026 if (rc != X86EMUL_CONTINUE)
3027 goto done;
3028 }
3029
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003030 if ((c->d & DstMask) == ImplicitOps)
3031 goto special_insn;
3032
3033
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003034 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3035 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003036 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003037 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003038 if (rc != X86EMUL_CONTINUE)
3039 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003040 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003041 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003042
Avi Kivity018a98d2007-11-27 19:30:56 +02003043special_insn:
3044
Avi Kivityef65c882010-07-29 15:11:51 +03003045 if (c->execute) {
3046 rc = c->execute(ctxt);
3047 if (rc != X86EMUL_CONTINUE)
3048 goto done;
3049 goto writeback;
3050 }
3051
Laurent Viviere4e03de2007-09-18 11:52:50 +02003052 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 goto twobyte_insn;
3054
Laurent Viviere4e03de2007-09-18 11:52:50 +02003055 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056 case 0x00 ... 0x05:
3057 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003058 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003060 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003061 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003062 break;
3063 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003064 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003065 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 case 0x08 ... 0x0d:
3067 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003068 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003070 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003071 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003072 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003073 case 0x10 ... 0x15:
3074 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003075 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003077 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003078 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003079 break;
3080 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003081 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003082 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 case 0x18 ... 0x1d:
3084 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003085 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003087 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003088 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003089 break;
3090 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003091 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003092 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003093 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003095 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 break;
3097 case 0x28 ... 0x2d:
3098 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003099 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 break;
3101 case 0x30 ... 0x35:
3102 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003103 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 break;
3105 case 0x38 ... 0x3d:
3106 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003107 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003109 case 0x40 ... 0x47: /* inc r16/r32 */
3110 emulate_1op("inc", c->dst, ctxt->eflags);
3111 break;
3112 case 0x48 ... 0x4f: /* dec r16/r32 */
3113 emulate_1op("dec", c->dst, ctxt->eflags);
3114 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003115 case 0x58 ... 0x5f: /* pop reg */
3116 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003117 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003118 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003119 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003120 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003121 break;
3122 case 0x61: /* popa */
3123 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003124 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003126 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003128 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003130 case 0x6c: /* insb */
3131 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003132 c->src.val = c->regs[VCPU_REGS_RDX];
3133 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003134 case 0x6e: /* outsb */
3135 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003136 c->dst.val = c->regs[VCPU_REGS_RDX];
3137 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003138 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003139 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003140 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003141 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003142 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003144 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 case 0:
3146 goto add;
3147 case 1:
3148 goto or;
3149 case 2:
3150 goto adc;
3151 case 3:
3152 goto sbb;
3153 case 4:
3154 goto and;
3155 case 5:
3156 goto sub;
3157 case 6:
3158 goto xor;
3159 case 7:
3160 goto cmp;
3161 }
3162 break;
3163 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003164 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003165 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 break;
3167 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003168 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003170 c->src.val = c->dst.val;
3171 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172 /*
3173 * Write back the memory destination with implicit LOCK
3174 * prefix.
3175 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003176 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003177 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003179 case 0x8c: /* mov r/m, sreg */
3180 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003181 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003182 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003183 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003184 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003185 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003186 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003187 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003188 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003189 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003190 case 0x8e: { /* mov seg, r/m16 */
3191 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003192
3193 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003194
Gleb Natapovc6975182010-02-18 12:15:01 +02003195 if (c->modrm_reg == VCPU_SREG_CS ||
3196 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003197 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003198 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003199 goto done;
3200 }
3201
Glauber Costa310b5d32009-05-12 16:21:06 -04003202 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003203 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003204
Gleb Natapov2e873022010-03-18 15:20:18 +02003205 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003206
3207 c->dst.type = OP_NONE; /* Disable writeback. */
3208 break;
3209 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003211 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003213 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3214 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003215 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003216 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003217 case 0x98: /* cbw/cwde/cdqe */
3218 switch (c->op_bytes) {
3219 case 2: c->dst.val = (s8)c->dst.val; break;
3220 case 4: c->dst.val = (s16)c->dst.val; break;
3221 case 8: c->dst.val = (s32)c->dst.val; break;
3222 }
3223 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003224 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003225 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003226 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003227 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003228 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003229 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003230 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003231 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003232 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003233 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003235 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003236 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003237 case 0xa8 ... 0xa9: /* test ax, imm */
3238 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003240 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003241 case 0xc0 ... 0xc1:
3242 emulate_grp2(ctxt);
3243 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003244 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003245 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003246 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003247 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003248 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003249 case 0xc4: /* les */
3250 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003251 break;
3252 case 0xc5: /* lds */
3253 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003254 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003255 case 0xcb: /* ret far */
3256 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003257 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003258 case 0xcc: /* int3 */
3259 irq = 3;
3260 goto do_interrupt;
3261 case 0xcd: /* int n */
3262 irq = c->src.val;
3263 do_interrupt:
3264 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003265 break;
3266 case 0xce: /* into */
3267 if (ctxt->eflags & EFLG_OF) {
3268 irq = 4;
3269 goto do_interrupt;
3270 }
3271 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003272 case 0xcf: /* iret */
3273 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003274 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003275 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003276 emulate_grp2(ctxt);
3277 break;
3278 case 0xd2 ... 0xd3: /* Grp2 */
3279 c->src.val = c->regs[VCPU_REGS_RCX];
3280 emulate_grp2(ctxt);
3281 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003282 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3283 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3284 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3285 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3286 jmp_rel(c, c->src.val);
3287 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003288 case 0xe3: /* jcxz/jecxz/jrcxz */
3289 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3290 jmp_rel(c, c->src.val);
3291 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003292 case 0xe4: /* inb */
3293 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003294 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003295 case 0xe6: /* outb */
3296 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003297 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003298 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003299 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003300 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003301 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003302 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003303 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003304 }
3305 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003306 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003307 case 0xea: { /* jmp far */
3308 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003309 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003310 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3311
3312 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003313 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003314
Gleb Natapov414e6272010-04-28 19:15:26 +03003315 c->eip = 0;
3316 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003317 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003318 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003319 case 0xeb:
3320 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003321 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003322 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003323 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003324 case 0xec: /* in al,dx */
3325 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003326 c->src.val = c->regs[VCPU_REGS_RDX];
3327 do_io_in:
3328 c->dst.bytes = min(c->dst.bytes, 4u);
3329 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003330 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003331 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003332 goto done;
3333 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003334 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3335 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003336 goto done; /* IO is needed */
3337 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003338 case 0xee: /* out dx,al */
3339 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003340 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003341 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003342 c->src.bytes = min(c->src.bytes, 4u);
3343 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3344 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003345 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003346 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003347 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003348 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003349 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3350 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003351 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003352 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003353 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003354 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003355 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003356 case 0xf5: /* cmc */
3357 /* complement carry flag from eflags reg */
3358 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003359 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003360 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003361 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003362 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003363 case 0xf8: /* clc */
3364 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003365 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003366 case 0xf9: /* stc */
3367 ctxt->eflags |= EFLG_CF;
3368 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003369 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003370 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003371 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003372 rc = X86EMUL_PROPAGATE_FAULT;
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003373 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003374 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003375 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003376 break;
3377 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003378 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003379 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003380 rc = X86EMUL_PROPAGATE_FAULT;
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003381 goto done;
3382 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003383 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003384 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003385 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003386 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003387 case 0xfc: /* cld */
3388 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003389 break;
3390 case 0xfd: /* std */
3391 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003392 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003393 case 0xfe: /* Grp4 */
3394 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003395 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003396 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003397 case 0xff: /* Grp5 */
3398 if (c->modrm_reg == 5)
3399 goto jump_far;
3400 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003401 default:
3402 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003404
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003405 if (rc != X86EMUL_CONTINUE)
3406 goto done;
3407
Avi Kivity018a98d2007-11-27 19:30:56 +02003408writeback:
3409 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003410 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003411 goto done;
3412
Gleb Natapov5cd21912010-03-18 15:20:26 +02003413 /*
3414 * restore dst type in case the decoding will be reused
3415 * (happens for string instruction )
3416 */
3417 c->dst.type = saved_dst_type;
3418
Gleb Natapova682e352010-03-18 15:20:21 +02003419 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003420 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003421 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003422
3423 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003424 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003425 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003426
Gleb Natapov5cd21912010-03-18 15:20:26 +02003427 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003428 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003429 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003430
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003431 if (!string_insn_completed(ctxt)) {
3432 /*
3433 * Re-enter guest when pio read ahead buffer is empty
3434 * or, if it is not used, after each 1024 iteration.
3435 */
3436 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3437 (r->end == 0 || r->end != r->pos)) {
3438 /*
3439 * Reset read cache. Usually happens before
3440 * decode, but since instruction is restarted
3441 * we have to do it here.
3442 */
3443 ctxt->decode.mem_read.end = 0;
3444 return EMULATION_RESTART;
3445 }
3446 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003447 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003448 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003449
3450 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003451
3452done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003453 if (rc == X86EMUL_PROPAGATE_FAULT)
3454 ctxt->have_exception = true;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003455 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003456
3457twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003458 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003460 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 u16 size;
3462 unsigned long address;
3463
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003464 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003465 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003466 goto cannot_emulate;
3467
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003468 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003469 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003470 goto done;
3471
Avi Kivity33e38852008-05-21 15:34:25 +03003472 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003473 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003474 /* Disable writeback. */
3475 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003476 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003478 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003479 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003480 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 goto done;
3482 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003483 /* Disable writeback. */
3484 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003486 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003487 if (c->modrm_mod == 3) {
3488 switch (c->modrm_rm) {
3489 case 1:
3490 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003491 break;
3492 default:
3493 goto cannot_emulate;
3494 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003495 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003496 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003497 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003498 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003499 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003500 goto done;
3501 realmode_lidt(ctxt->vcpu, size, address);
3502 }
Avi Kivity16286d02008-04-14 14:40:50 +03003503 /* Disable writeback. */
3504 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505 break;
3506 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003507 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003508 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509 break;
3510 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003511 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003512 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003513 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003515 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003516 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003517 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003518 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003520 emulate_invlpg(ctxt->vcpu,
3521 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003522 /* Disable writeback. */
3523 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 break;
3525 default:
3526 goto cannot_emulate;
3527 }
3528 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003529 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003530 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003531 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003532 case 0x06:
3533 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003534 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003535 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003536 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003537 break;
3538 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003539 case 0x0d: /* GrpP (prefetch) */
3540 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003541 break;
3542 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003543 switch (c->modrm_reg) {
3544 case 1:
3545 case 5 ... 7:
3546 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003547 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003548 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003549 goto done;
3550 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003551 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003552 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003554 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3555 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003556 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003557 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003558 goto done;
3559 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003560 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003562 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003563 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003564 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003565 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03003566 goto done;
3567 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003568 c->dst.type = OP_NONE;
3569 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003570 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003571 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3572 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003573 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003574 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003575 goto done;
3576 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003577
Avi Kivityb27f3852010-08-01 14:25:22 +03003578 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003579 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3580 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3581 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003582 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003583 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03003584 goto done;
3585 }
3586
Laurent Viviera01af5e2007-09-24 11:10:56 +02003587 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003589 case 0x30:
3590 /* wrmsr */
3591 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3592 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003593 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003594 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003595 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003596 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003597 }
3598 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003599 break;
3600 case 0x32:
3601 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003602 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003603 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003604 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003605 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003606 } else {
3607 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3608 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3609 }
3610 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003611 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003612 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003613 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003614 break;
3615 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003616 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003617 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003619 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003620 if (!test_cc(c->b, ctxt->eflags))
3621 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003622 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003623 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003624 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003625 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003626 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003627 case 0x90 ... 0x9f: /* setcc r/m8 */
3628 c->dst.val = test_cc(c->b, ctxt->eflags);
3629 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003630 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003631 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003632 break;
3633 case 0xa1: /* pop fs */
3634 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003635 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003636 case 0xa3:
3637 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003638 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003639 /* only subword offset */
3640 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003641 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003642 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003643 case 0xa4: /* shld imm8, r, r/m */
3644 case 0xa5: /* shld cl, r, r/m */
3645 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3646 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003647 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003648 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003649 break;
3650 case 0xa9: /* pop gs */
3651 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003652 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003653 case 0xab:
3654 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003655 emulate_2op_SrcV_nobyte("bts", 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 0xac: /* shrd imm8, r, r/m */
3658 case 0xad: /* shrd cl, r, r/m */
3659 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3660 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003661 case 0xae: /* clflush */
3662 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003663 case 0xb0 ... 0xb1: /* cmpxchg */
3664 /*
3665 * Save real source value, then compare EAX against
3666 * destination.
3667 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003668 c->src.orig_val = c->src.val;
3669 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003670 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3671 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003672 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003673 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674 } else {
3675 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003676 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003677 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678 }
3679 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003680 case 0xb2: /* lss */
3681 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003682 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683 case 0xb3:
3684 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003685 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003687 case 0xb4: /* lfs */
3688 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003689 break;
3690 case 0xb5: /* lgs */
3691 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003693 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003694 c->dst.bytes = c->op_bytes;
3695 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3696 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003697 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003698 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003699 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700 case 0:
3701 goto bt;
3702 case 1:
3703 goto bts;
3704 case 2:
3705 goto btr;
3706 case 3:
3707 goto btc;
3708 }
3709 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003710 case 0xbb:
3711 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003712 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003713 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003714 case 0xbc: { /* bsf */
3715 u8 zf;
3716 __asm__ ("bsf %2, %0; setz %1"
3717 : "=r"(c->dst.val), "=q"(zf)
3718 : "r"(c->src.val));
3719 ctxt->eflags &= ~X86_EFLAGS_ZF;
3720 if (zf) {
3721 ctxt->eflags |= X86_EFLAGS_ZF;
3722 c->dst.type = OP_NONE; /* Disable writeback. */
3723 }
3724 break;
3725 }
3726 case 0xbd: { /* bsr */
3727 u8 zf;
3728 __asm__ ("bsr %2, %0; setz %1"
3729 : "=r"(c->dst.val), "=q"(zf)
3730 : "r"(c->src.val));
3731 ctxt->eflags &= ~X86_EFLAGS_ZF;
3732 if (zf) {
3733 ctxt->eflags |= X86_EFLAGS_ZF;
3734 c->dst.type = OP_NONE; /* Disable writeback. */
3735 }
3736 break;
3737 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003738 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003739 c->dst.bytes = c->op_bytes;
3740 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3741 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003743 case 0xc0 ... 0xc1: /* xadd */
3744 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3745 /* Write back the register source. */
3746 c->src.val = c->dst.orig_val;
3747 write_register_operand(&c->src);
3748 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003749 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003750 c->dst.bytes = c->op_bytes;
3751 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3752 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003753 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003755 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003756 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003757 default:
3758 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003759 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003760
3761 if (rc != X86EMUL_CONTINUE)
3762 goto done;
3763
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764 goto writeback;
3765
3766cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003767 return -1;
3768}