blob: cd9ed9f4527587df3c65418877315e8cfe2c415e [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Avi Kivityab85b12b2010-07-29 15:11:49 +030050#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080051/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define SrcNone (0<<4) /* No source operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcReg (1<<4) /* Register operand. */
54#define SrcMem (2<<4) /* Memory operand. */
55#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
57#define SrcImm (5<<4) /* Immediate operand. */
58#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010059#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030060#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030061#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020062#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030063#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
64#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080065#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030066#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020076#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Avi Kivity0d7cdee2011-03-29 11:34:38 +020078#define Prefix (1<<16) /* Instruction varies with 66/f2/f3 prefix */
Avi Kivity12537912011-03-29 11:41:27 +020079#define Sse (1<<17) /* SSE Vector instruction */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030080/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +020081#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +020082#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030083#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030084#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030085#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020086#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020087#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030088#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010089/* Source 2 operand type */
90#define Src2None (0<<29)
91#define Src2CL (1<<29)
92#define Src2ImmByte (2<<29)
93#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030094#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010095#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
Avi Kivityd0e53322010-07-29 15:11:54 +030097#define X2(x...) x, x
98#define X3(x...) X2(x), x
99#define X4(x...) X2(x), X2(x)
100#define X5(x...) X4(x), x
101#define X6(x...) X4(x), X2(x)
102#define X7(x...) X4(x), X3(x)
103#define X8(x...) X4(x), X4(x)
104#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300105
Avi Kivityd65b1de2010-07-29 15:11:35 +0300106struct opcode {
107 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200108 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300109 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300110 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300111 struct opcode *group;
112 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200113 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300114 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200115 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300116};
117
118struct group_dual {
119 struct opcode mod012[8];
120 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300121};
122
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200123struct gprefix {
124 struct opcode pfx_no;
125 struct opcode pfx_66;
126 struct opcode pfx_f2;
127 struct opcode pfx_f3;
128};
129
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200131#define EFLG_ID (1<<21)
132#define EFLG_VIP (1<<20)
133#define EFLG_VIF (1<<19)
134#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200135#define EFLG_VM (1<<17)
136#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200137#define EFLG_IOPL (3<<12)
138#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139#define EFLG_OF (1<<11)
140#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200141#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200142#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143#define EFLG_SF (1<<7)
144#define EFLG_ZF (1<<6)
145#define EFLG_AF (1<<4)
146#define EFLG_PF (1<<2)
147#define EFLG_CF (1<<0)
148
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300149#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
150#define EFLG_RESERVED_ONE_MASK 2
151
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152/*
153 * Instruction emulation:
154 * Most instructions are emulated directly via a fragment of inline assembly
155 * code. This allows us to save/restore EFLAGS and thus very easily pick up
156 * any modified flags.
157 */
158
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800159#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800160#define _LO32 "k" /* force 32-bit operand */
161#define _STK "%%rsp" /* stack pointer */
162#elif defined(__i386__)
163#define _LO32 "" /* force 32-bit operand */
164#define _STK "%%esp" /* stack pointer */
165#endif
166
167/*
168 * These EFLAGS bits are restored from saved value during emulation, and
169 * any changes are written back to the saved value after emulation.
170 */
171#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
172
173/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200174#define _PRE_EFLAGS(_sav, _msk, _tmp) \
175 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
176 "movl %"_sav",%"_LO32 _tmp"; " \
177 "push %"_tmp"; " \
178 "push %"_tmp"; " \
179 "movl %"_msk",%"_LO32 _tmp"; " \
180 "andl %"_LO32 _tmp",("_STK"); " \
181 "pushf; " \
182 "notl %"_LO32 _tmp"; " \
183 "andl %"_LO32 _tmp",("_STK"); " \
184 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
185 "pop %"_tmp"; " \
186 "orl %"_LO32 _tmp",("_STK"); " \
187 "popf; " \
188 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189
190/* After executing instruction: write-back necessary bits in EFLAGS. */
191#define _POST_EFLAGS(_sav, _msk, _tmp) \
192 /* _sav |= EFLAGS & _msk; */ \
193 "pushf; " \
194 "pop %"_tmp"; " \
195 "andl %"_msk",%"_LO32 _tmp"; " \
196 "orl %"_LO32 _tmp",%"_sav"; "
197
Avi Kivitydda96d82008-11-26 15:14:10 +0200198#ifdef CONFIG_X86_64
199#define ON64(x) x
200#else
201#define ON64(x)
202#endif
203
Avi Kivityb3b3d252010-08-16 17:49:52 +0300204#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200205 do { \
206 __asm__ __volatile__ ( \
207 _PRE_EFLAGS("0", "4", "2") \
208 _op _suffix " %"_x"3,%1; " \
209 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300210 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200211 "=&r" (_tmp) \
212 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200213 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200214
215
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216/* Raw emulation: instruction has two explicit operands. */
217#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200218 do { \
219 unsigned long _tmp; \
220 \
221 switch ((_dst).bytes) { \
222 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300223 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200224 break; \
225 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300226 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200227 break; \
228 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300229 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200230 break; \
231 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 } while (0)
233
234#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
235 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200236 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400237 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300239 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 break; \
241 default: \
242 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
243 _wx, _wy, _lx, _ly, _qx, _qy); \
244 break; \
245 } \
246 } while (0)
247
248/* Source operand is byte-sized and may be restricted to just %cl. */
249#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
250 __emulate_2op(_op, _src, _dst, _eflags, \
251 "b", "c", "b", "c", "b", "c", "b", "c")
252
253/* Source operand is byte, word, long or quad sized. */
254#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
255 __emulate_2op(_op, _src, _dst, _eflags, \
256 "b", "q", "w", "r", _LO32, "r", "", "r")
257
258/* Source operand is word, long or quad sized. */
259#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
260 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
261 "w", "r", _LO32, "r", "", "r")
262
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100263/* Instruction has three operands and one operand is stored in ECX register */
264#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
265 do { \
266 unsigned long _tmp; \
267 _type _clv = (_cl).val; \
268 _type _srcv = (_src).val; \
269 _type _dstv = (_dst).val; \
270 \
271 __asm__ __volatile__ ( \
272 _PRE_EFLAGS("0", "5", "2") \
273 _op _suffix " %4,%1 \n" \
274 _POST_EFLAGS("0", "5", "2") \
275 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
276 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
277 ); \
278 \
279 (_cl).val = (unsigned long) _clv; \
280 (_src).val = (unsigned long) _srcv; \
281 (_dst).val = (unsigned long) _dstv; \
282 } while (0)
283
284#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
285 do { \
286 switch ((_dst).bytes) { \
287 case 2: \
288 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
289 "w", unsigned short); \
290 break; \
291 case 4: \
292 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
293 "l", unsigned int); \
294 break; \
295 case 8: \
296 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
297 "q", unsigned long)); \
298 break; \
299 } \
300 } while (0)
301
Avi Kivitydda96d82008-11-26 15:14:10 +0200302#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303 do { \
304 unsigned long _tmp; \
305 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200306 __asm__ __volatile__ ( \
307 _PRE_EFLAGS("0", "3", "2") \
308 _op _suffix " %1; " \
309 _POST_EFLAGS("0", "3", "2") \
310 : "=m" (_eflags), "+m" ((_dst).val), \
311 "=&r" (_tmp) \
312 : "i" (EFLAGS_MASK)); \
313 } while (0)
314
315/* Instruction has only one explicit operand (no source operand). */
316#define emulate_1op(_op, _dst, _eflags) \
317 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400318 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200319 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
320 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
321 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
322 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323 } \
324 } while (0)
325
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300326#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
327 do { \
328 unsigned long _tmp; \
329 \
330 __asm__ __volatile__ ( \
331 _PRE_EFLAGS("0", "4", "1") \
332 _op _suffix " %5; " \
333 _POST_EFLAGS("0", "4", "1") \
334 : "=m" (_eflags), "=&r" (_tmp), \
335 "+a" (_rax), "+d" (_rdx) \
336 : "i" (EFLAGS_MASK), "m" ((_src).val), \
337 "a" (_rax), "d" (_rdx)); \
338 } while (0)
339
Avi Kivityf6b35972010-08-26 11:59:00 +0300340#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
341 do { \
342 unsigned long _tmp; \
343 \
344 __asm__ __volatile__ ( \
345 _PRE_EFLAGS("0", "5", "1") \
346 "1: \n\t" \
347 _op _suffix " %6; " \
348 "2: \n\t" \
349 _POST_EFLAGS("0", "5", "1") \
350 ".pushsection .fixup,\"ax\" \n\t" \
351 "3: movb $1, %4 \n\t" \
352 "jmp 2b \n\t" \
353 ".popsection \n\t" \
354 _ASM_EXTABLE(1b, 3b) \
355 : "=m" (_eflags), "=&r" (_tmp), \
356 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
357 : "i" (EFLAGS_MASK), "m" ((_src).val), \
358 "a" (_rax), "d" (_rdx)); \
359 } while (0)
360
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300361/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
362#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
363 do { \
364 switch((_src).bytes) { \
365 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
366 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
367 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
368 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
369 } \
370 } while (0)
371
Avi Kivityf6b35972010-08-26 11:59:00 +0300372#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
373 do { \
374 switch((_src).bytes) { \
375 case 1: \
376 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
377 _eflags, "b", _ex); \
378 break; \
379 case 2: \
380 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
381 _eflags, "w", _ex); \
382 break; \
383 case 4: \
384 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
385 _eflags, "l", _ex); \
386 break; \
387 case 8: ON64( \
388 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
389 _eflags, "q", _ex)); \
390 break; \
391 } \
392 } while (0)
393
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394/* Fetch next part of the instruction being emulated. */
395#define insn_fetch(_type, _size, _eip) \
396({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200397 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200398 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 goto done; \
400 (_eip) += (_size); \
401 (_type)_x; \
402})
403
Gleb Natapov414e6272010-04-28 19:15:26 +0300404#define insn_fetch_arr(_arr, _size, _eip) \
405({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
406 if (rc != X86EMUL_CONTINUE) \
407 goto done; \
408 (_eip) += (_size); \
409})
410
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200411static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
412 enum x86_intercept intercept,
413 enum x86_intercept_stage stage)
414{
415 struct x86_instruction_info info = {
416 .intercept = intercept,
417 .rep_prefix = ctxt->decode.rep_prefix,
418 .modrm_mod = ctxt->decode.modrm_mod,
419 .modrm_reg = ctxt->decode.modrm_reg,
420 .modrm_rm = ctxt->decode.modrm_rm,
421 .src_val = ctxt->decode.src.val64,
422 .src_bytes = ctxt->decode.src.bytes,
423 .dst_bytes = ctxt->decode.dst.bytes,
424 .ad_bytes = ctxt->decode.ad_bytes,
425 .next_rip = ctxt->eip,
426 };
427
428 return ctxt->ops->intercept(ctxt->vcpu, &info, stage);
429}
430
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800431static inline unsigned long ad_mask(struct decode_cache *c)
432{
433 return (1UL << (c->ad_bytes << 3)) - 1;
434}
435
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800437static inline unsigned long
438address_mask(struct decode_cache *c, unsigned long reg)
439{
440 if (c->ad_bytes == sizeof(unsigned long))
441 return reg;
442 else
443 return reg & ad_mask(c);
444}
445
446static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200447register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800448{
Avi Kivity90de84f2010-11-17 15:28:21 +0200449 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800450}
451
Harvey Harrison7a9572752008-02-19 07:40:41 -0800452static inline void
453register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
454{
455 if (c->ad_bytes == sizeof(unsigned long))
456 *reg += inc;
457 else
458 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
459}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460
Harvey Harrison7a9572752008-02-19 07:40:41 -0800461static inline void jmp_rel(struct decode_cache *c, int rel)
462{
463 register_address_increment(c, &c->eip, rel);
464}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300465
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300466static void set_seg_override(struct decode_cache *c, int seg)
467{
468 c->has_seg_override = true;
469 c->seg_override = seg;
470}
471
Gleb Natapov79168fd2010-04-28 19:15:30 +0300472static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
473 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300474{
475 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
476 return 0;
477
Gleb Natapov79168fd2010-04-28 19:15:30 +0300478 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300479}
480
Avi Kivity90de84f2010-11-17 15:28:21 +0200481static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
482 struct x86_emulate_ops *ops,
483 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300484{
485 if (!c->has_seg_override)
486 return 0;
487
Avi Kivity90de84f2010-11-17 15:28:21 +0200488 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300489}
490
Avi Kivity90de84f2010-11-17 15:28:21 +0200491static ulong linear(struct x86_emulate_ctxt *ctxt,
492 struct segmented_address addr)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300493{
Avi Kivity90de84f2010-11-17 15:28:21 +0200494 struct decode_cache *c = &ctxt->decode;
495 ulong la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300496
Avi Kivity90de84f2010-11-17 15:28:21 +0200497 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
498 if (c->ad_bytes != 8)
499 la &= (u32)-1;
500 return la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300501}
502
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200503static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
504 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300505{
Avi Kivityda9cb572010-11-22 17:53:21 +0200506 ctxt->exception.vector = vec;
507 ctxt->exception.error_code = error;
508 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200509 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300510}
511
Joerg Roedel3b88e412011-04-04 12:39:29 +0200512static int emulate_db(struct x86_emulate_ctxt *ctxt)
513{
514 return emulate_exception(ctxt, DB_VECTOR, 0, false);
515}
516
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200517static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300518{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200519 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300520}
521
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200522static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300523{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200524 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300525}
526
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200527static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300528{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200529 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300530}
531
Avi Kivity34d1f492010-08-26 11:59:01 +0300532static int emulate_de(struct x86_emulate_ctxt *ctxt)
533{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200534 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300535}
536
Avi Kivity12537912011-03-29 11:41:27 +0200537static int emulate_nm(struct x86_emulate_ctxt *ctxt)
538{
539 return emulate_exception(ctxt, NM_VECTOR, 0, false);
540}
541
Avi Kivity62266862007-11-20 13:15:52 +0200542static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
543 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300544 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200545{
546 struct fetch_cache *fc = &ctxt->decode.fetch;
547 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300548 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200549
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300550 if (eip == fc->end) {
551 cur_size = fc->end - fc->start;
552 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
553 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200554 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900555 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200556 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300557 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200558 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300559 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900560 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200561}
562
563static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
564 struct x86_emulate_ops *ops,
565 unsigned long eip, void *dest, unsigned size)
566{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900567 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200568
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200569 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200570 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200571 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200572 while (size--) {
573 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900574 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200575 return rc;
576 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900577 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200578}
579
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000580/*
581 * Given the 'reg' portion of a ModRM byte, and a register block, return a
582 * pointer into the block that addresses the relevant register.
583 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
584 */
585static void *decode_register(u8 modrm_reg, unsigned long *regs,
586 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587{
588 void *p;
589
590 p = &regs[modrm_reg];
591 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
592 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
593 return p;
594}
595
596static int read_descriptor(struct x86_emulate_ctxt *ctxt,
597 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200598 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 u16 *size, unsigned long *address, int op_bytes)
600{
601 int rc;
602
603 if (op_bytes == 2)
604 op_bytes = 3;
605 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200606 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200607 ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900608 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200610 addr.ea += 2;
611 rc = ops->read_std(linear(ctxt, addr), address, op_bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200612 ctxt->vcpu, &ctxt->exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613 return rc;
614}
615
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300616static int test_cc(unsigned int condition, unsigned int flags)
617{
618 int rc = 0;
619
620 switch ((condition & 15) >> 1) {
621 case 0: /* o */
622 rc |= (flags & EFLG_OF);
623 break;
624 case 1: /* b/c/nae */
625 rc |= (flags & EFLG_CF);
626 break;
627 case 2: /* z/e */
628 rc |= (flags & EFLG_ZF);
629 break;
630 case 3: /* be/na */
631 rc |= (flags & (EFLG_CF|EFLG_ZF));
632 break;
633 case 4: /* s */
634 rc |= (flags & EFLG_SF);
635 break;
636 case 5: /* p/pe */
637 rc |= (flags & EFLG_PF);
638 break;
639 case 7: /* le/ng */
640 rc |= (flags & EFLG_ZF);
641 /* fall through */
642 case 6: /* l/nge */
643 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
644 break;
645 }
646
647 /* Odd condition identifiers (lsb == 1) have inverted sense. */
648 return (!!rc ^ (condition & 1));
649}
650
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300651static void fetch_register_operand(struct operand *op)
652{
653 switch (op->bytes) {
654 case 1:
655 op->val = *(u8 *)op->addr.reg;
656 break;
657 case 2:
658 op->val = *(u16 *)op->addr.reg;
659 break;
660 case 4:
661 op->val = *(u32 *)op->addr.reg;
662 break;
663 case 8:
664 op->val = *(u64 *)op->addr.reg;
665 break;
666 }
667}
668
Avi Kivity12537912011-03-29 11:41:27 +0200669static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
670{
671 ctxt->ops->get_fpu(ctxt);
672 switch (reg) {
673 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
674 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
675 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
676 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
677 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
678 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
679 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
680 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
681#ifdef CONFIG_X86_64
682 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
683 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
684 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
685 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
686 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
687 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
688 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
689 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
690#endif
691 default: BUG();
692 }
693 ctxt->ops->put_fpu(ctxt);
694}
695
696static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
697 int reg)
698{
699 ctxt->ops->get_fpu(ctxt);
700 switch (reg) {
701 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
702 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
703 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
704 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
705 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
706 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
707 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
708 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
709#ifdef CONFIG_X86_64
710 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
711 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
712 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
713 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
714 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
715 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
716 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
717 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
718#endif
719 default: BUG();
720 }
721 ctxt->ops->put_fpu(ctxt);
722}
723
724static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
725 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200726 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200727 int inhibit_bytereg)
728{
Avi Kivity33615aa2007-10-31 11:15:56 +0200729 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200730 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200731
732 if (!(c->d & ModRM))
733 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200734
735 if (c->d & Sse) {
736 op->type = OP_XMM;
737 op->bytes = 16;
738 op->addr.xmm = reg;
739 read_sse_reg(ctxt, &op->vec_val, reg);
740 return;
741 }
742
Avi Kivity3c118e22007-10-31 10:27:04 +0200743 op->type = OP_REG;
744 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300745 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200746 op->bytes = 1;
747 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300748 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200749 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200750 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300751 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200752 op->orig_val = op->val;
753}
754
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200755static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300756 struct x86_emulate_ops *ops,
757 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200758{
759 struct decode_cache *c = &ctxt->decode;
760 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700761 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900762 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300763 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200764
765 if (c->rex_prefix) {
766 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
767 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
768 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
769 }
770
771 c->modrm = insn_fetch(u8, 1, c->eip);
772 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
773 c->modrm_reg |= (c->modrm & 0x38) >> 3;
774 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300775 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200776
777 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300778 op->type = OP_REG;
779 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
780 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300781 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200782 if (c->d & Sse) {
783 op->type = OP_XMM;
784 op->bytes = 16;
785 op->addr.xmm = c->modrm_rm;
786 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
787 return rc;
788 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300789 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200790 return rc;
791 }
792
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300793 op->type = OP_MEM;
794
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200795 if (c->ad_bytes == 2) {
796 unsigned bx = c->regs[VCPU_REGS_RBX];
797 unsigned bp = c->regs[VCPU_REGS_RBP];
798 unsigned si = c->regs[VCPU_REGS_RSI];
799 unsigned di = c->regs[VCPU_REGS_RDI];
800
801 /* 16-bit ModR/M decode. */
802 switch (c->modrm_mod) {
803 case 0:
804 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300805 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200806 break;
807 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300808 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200809 break;
810 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300811 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200812 break;
813 }
814 switch (c->modrm_rm) {
815 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300816 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200817 break;
818 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300819 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200820 break;
821 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300822 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200823 break;
824 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300825 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200826 break;
827 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300828 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200829 break;
830 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300831 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200832 break;
833 case 6:
834 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300835 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200836 break;
837 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300838 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200839 break;
840 }
841 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
842 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300843 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300844 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200845 } else {
846 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700847 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200848 sib = insn_fetch(u8, 1, c->eip);
849 index_reg |= (sib >> 3) & 7;
850 base_reg |= sib & 7;
851 scale = sib >> 6;
852
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700853 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300854 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700855 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300856 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700857 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300858 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700859 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
860 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700861 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700862 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300863 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200864 switch (c->modrm_mod) {
865 case 0:
866 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300867 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200868 break;
869 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300870 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200871 break;
872 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300873 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200874 break;
875 }
876 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200877 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200878done:
879 return rc;
880}
881
882static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300883 struct x86_emulate_ops *ops,
884 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200885{
886 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900887 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300889 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890 switch (c->ad_bytes) {
891 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200892 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200893 break;
894 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200895 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200896 break;
897 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200898 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200899 break;
900 }
901done:
902 return rc;
903}
904
Wei Yongjun35c843c2010-08-09 11:34:56 +0800905static void fetch_bit_operand(struct decode_cache *c)
906{
Sheng Yang7129eec2010-09-28 16:33:32 +0800907 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800908
Wei Yongjun3885f182010-08-09 11:37:37 +0800909 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800910 mask = ~(c->dst.bytes * 8 - 1);
911
912 if (c->src.bytes == 2)
913 sv = (s16)c->src.val & (s16)mask;
914 else if (c->src.bytes == 4)
915 sv = (s32)c->src.val & (s32)mask;
916
Avi Kivity90de84f2010-11-17 15:28:21 +0200917 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800918 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800919
920 /* only subword offset */
921 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800922}
923
Gleb Natapov9de41572010-04-28 19:15:22 +0300924static int read_emulated(struct x86_emulate_ctxt *ctxt,
925 struct x86_emulate_ops *ops,
926 unsigned long addr, void *dest, unsigned size)
927{
928 int rc;
929 struct read_cache *mc = &ctxt->decode.mem_read;
930
931 while (size) {
932 int n = min(size, 8u);
933 size -= n;
934 if (mc->pos < mc->end)
935 goto read_cached;
936
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200937 rc = ops->read_emulated(addr, mc->data + mc->end, n,
938 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +0300939 if (rc != X86EMUL_CONTINUE)
940 return rc;
941 mc->end += n;
942
943 read_cached:
944 memcpy(dest, mc->data + mc->pos, n);
945 mc->pos += n;
946 dest += n;
947 addr += n;
948 }
949 return X86EMUL_CONTINUE;
950}
951
Gleb Natapov7b262e92010-03-18 15:20:27 +0200952static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
953 struct x86_emulate_ops *ops,
954 unsigned int size, unsigned short port,
955 void *dest)
956{
957 struct read_cache *rc = &ctxt->decode.io_read;
958
959 if (rc->pos == rc->end) { /* refill pio read ahead */
960 struct decode_cache *c = &ctxt->decode;
961 unsigned int in_page, n;
962 unsigned int count = c->rep_prefix ?
963 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
964 in_page = (ctxt->eflags & EFLG_DF) ?
965 offset_in_page(c->regs[VCPU_REGS_RDI]) :
966 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
967 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
968 count);
969 if (n == 0)
970 n = 1;
971 rc->pos = rc->end = 0;
972 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
973 return 0;
974 rc->end = n * size;
975 }
976
977 memcpy(dest, rc->data + rc->pos, size);
978 rc->pos += size;
979 return 1;
980}
981
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200982static u32 desc_limit_scaled(struct desc_struct *desc)
983{
984 u32 limit = get_desc_limit(desc);
985
986 return desc->g ? (limit << 12) | 0xfff : limit;
987}
988
989static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
990 struct x86_emulate_ops *ops,
991 u16 selector, struct desc_ptr *dt)
992{
993 if (selector & 1 << 2) {
994 struct desc_struct desc;
995 memset (dt, 0, sizeof *dt);
Gleb Natapov5601d052011-03-07 14:55:06 +0200996 if (!ops->get_cached_descriptor(&desc, NULL, VCPU_SREG_LDTR,
997 ctxt->vcpu))
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200998 return;
999
1000 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1001 dt->address = get_desc_base(&desc);
1002 } else
1003 ops->get_gdt(dt, ctxt->vcpu);
1004}
1005
1006/* allowed just for 8 bytes segments */
1007static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1008 struct x86_emulate_ops *ops,
1009 u16 selector, struct desc_struct *desc)
1010{
1011 struct desc_ptr dt;
1012 u16 index = selector >> 3;
1013 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001014 ulong addr;
1015
1016 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1017
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001018 if (dt.size < index * 8 + 7)
1019 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001020 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001021 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
1022 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001023
1024 return ret;
1025}
1026
1027/* allowed just for 8 bytes segments */
1028static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1029 struct x86_emulate_ops *ops,
1030 u16 selector, struct desc_struct *desc)
1031{
1032 struct desc_ptr dt;
1033 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001034 ulong addr;
1035 int ret;
1036
1037 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1038
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001039 if (dt.size < index * 8 + 7)
1040 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001041
1042 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001043 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
1044 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001045
1046 return ret;
1047}
1048
Gleb Natapov5601d052011-03-07 14:55:06 +02001049/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001050static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1051 struct x86_emulate_ops *ops,
1052 u16 selector, int seg)
1053{
1054 struct desc_struct seg_desc;
1055 u8 dpl, rpl, cpl;
1056 unsigned err_vec = GP_VECTOR;
1057 u32 err_code = 0;
1058 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1059 int ret;
1060
1061 memset(&seg_desc, 0, sizeof seg_desc);
1062
1063 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1064 || ctxt->mode == X86EMUL_MODE_REAL) {
1065 /* set real mode segment descriptor */
1066 set_desc_base(&seg_desc, selector << 4);
1067 set_desc_limit(&seg_desc, 0xffff);
1068 seg_desc.type = 3;
1069 seg_desc.p = 1;
1070 seg_desc.s = 1;
1071 goto load;
1072 }
1073
1074 /* NULL selector is not valid for TR, CS and SS */
1075 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1076 && null_selector)
1077 goto exception;
1078
1079 /* TR should be in GDT only */
1080 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1081 goto exception;
1082
1083 if (null_selector) /* for NULL selector skip all following checks */
1084 goto load;
1085
1086 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1087 if (ret != X86EMUL_CONTINUE)
1088 return ret;
1089
1090 err_code = selector & 0xfffc;
1091 err_vec = GP_VECTOR;
1092
1093 /* can't load system descriptor into segment selecor */
1094 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1095 goto exception;
1096
1097 if (!seg_desc.p) {
1098 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1099 goto exception;
1100 }
1101
1102 rpl = selector & 3;
1103 dpl = seg_desc.dpl;
1104 cpl = ops->cpl(ctxt->vcpu);
1105
1106 switch (seg) {
1107 case VCPU_SREG_SS:
1108 /*
1109 * segment is not a writable data segment or segment
1110 * selector's RPL != CPL or segment selector's RPL != CPL
1111 */
1112 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1113 goto exception;
1114 break;
1115 case VCPU_SREG_CS:
1116 if (!(seg_desc.type & 8))
1117 goto exception;
1118
1119 if (seg_desc.type & 4) {
1120 /* conforming */
1121 if (dpl > cpl)
1122 goto exception;
1123 } else {
1124 /* nonconforming */
1125 if (rpl > cpl || dpl != cpl)
1126 goto exception;
1127 }
1128 /* CS(RPL) <- CPL */
1129 selector = (selector & 0xfffc) | cpl;
1130 break;
1131 case VCPU_SREG_TR:
1132 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1133 goto exception;
1134 break;
1135 case VCPU_SREG_LDTR:
1136 if (seg_desc.s || seg_desc.type != 2)
1137 goto exception;
1138 break;
1139 default: /* DS, ES, FS, or GS */
1140 /*
1141 * segment is not a data or readable code segment or
1142 * ((segment is a data or nonconforming code segment)
1143 * and (both RPL and CPL > DPL))
1144 */
1145 if ((seg_desc.type & 0xa) == 0x8 ||
1146 (((seg_desc.type & 0xc) != 0xc) &&
1147 (rpl > dpl && cpl > dpl)))
1148 goto exception;
1149 break;
1150 }
1151
1152 if (seg_desc.s) {
1153 /* mark segment as accessed */
1154 seg_desc.type |= 1;
1155 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1156 if (ret != X86EMUL_CONTINUE)
1157 return ret;
1158 }
1159load:
1160 ops->set_segment_selector(selector, seg, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001161 ops->set_cached_descriptor(&seg_desc, 0, seg, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001162 return X86EMUL_CONTINUE;
1163exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001164 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001165 return X86EMUL_PROPAGATE_FAULT;
1166}
1167
Wei Yongjun31be40b2010-08-17 09:17:30 +08001168static void write_register_operand(struct operand *op)
1169{
1170 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1171 switch (op->bytes) {
1172 case 1:
1173 *(u8 *)op->addr.reg = (u8)op->val;
1174 break;
1175 case 2:
1176 *(u16 *)op->addr.reg = (u16)op->val;
1177 break;
1178 case 4:
1179 *op->addr.reg = (u32)op->val;
1180 break; /* 64b: zero-extend */
1181 case 8:
1182 *op->addr.reg = op->val;
1183 break;
1184 }
1185}
1186
Wei Yongjunc37eda12010-06-15 09:03:33 +08001187static inline int writeback(struct x86_emulate_ctxt *ctxt,
1188 struct x86_emulate_ops *ops)
1189{
1190 int rc;
1191 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001192
1193 switch (c->dst.type) {
1194 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001195 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001196 break;
1197 case OP_MEM:
1198 if (c->lock_prefix)
1199 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001200 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001201 &c->dst.orig_val,
1202 &c->dst.val,
1203 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001204 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001205 ctxt->vcpu);
1206 else
1207 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001208 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001209 &c->dst.val,
1210 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001211 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001212 ctxt->vcpu);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001213 if (rc != X86EMUL_CONTINUE)
1214 return rc;
1215 break;
Avi Kivity12537912011-03-29 11:41:27 +02001216 case OP_XMM:
1217 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1218 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001219 case OP_NONE:
1220 /* no writeback */
1221 break;
1222 default:
1223 break;
1224 }
1225 return X86EMUL_CONTINUE;
1226}
1227
Gleb Natapov79168fd2010-04-28 19:15:30 +03001228static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1229 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001230{
1231 struct decode_cache *c = &ctxt->decode;
1232
1233 c->dst.type = OP_MEM;
1234 c->dst.bytes = c->op_bytes;
1235 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001236 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001237 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1238 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001239}
1240
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001241static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001242 struct x86_emulate_ops *ops,
1243 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001244{
1245 struct decode_cache *c = &ctxt->decode;
1246 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001247 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001248
Avi Kivity90de84f2010-11-17 15:28:21 +02001249 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1250 addr.seg = VCPU_SREG_SS;
1251 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001252 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253 return rc;
1254
Avi Kivity350f69d2009-01-05 11:12:40 +02001255 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001256 return rc;
1257}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001258
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001259static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1260 struct x86_emulate_ops *ops,
1261 void *dest, int len)
1262{
1263 int rc;
1264 unsigned long val, change_mask;
1265 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001266 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001267
1268 rc = emulate_pop(ctxt, ops, &val, len);
1269 if (rc != X86EMUL_CONTINUE)
1270 return rc;
1271
1272 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1273 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1274
1275 switch(ctxt->mode) {
1276 case X86EMUL_MODE_PROT64:
1277 case X86EMUL_MODE_PROT32:
1278 case X86EMUL_MODE_PROT16:
1279 if (cpl == 0)
1280 change_mask |= EFLG_IOPL;
1281 if (cpl <= iopl)
1282 change_mask |= EFLG_IF;
1283 break;
1284 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001285 if (iopl < 3)
1286 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001287 change_mask |= EFLG_IF;
1288 break;
1289 default: /* real mode */
1290 change_mask |= (EFLG_IOPL | EFLG_IF);
1291 break;
1292 }
1293
1294 *(unsigned long *)dest =
1295 (ctxt->eflags & ~change_mask) | (val & change_mask);
1296
1297 return rc;
1298}
1299
Gleb Natapov79168fd2010-04-28 19:15:30 +03001300static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1301 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001302{
1303 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001304
Gleb Natapov79168fd2010-04-28 19:15:30 +03001305 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001306
Gleb Natapov79168fd2010-04-28 19:15:30 +03001307 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001308}
1309
1310static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1311 struct x86_emulate_ops *ops, int seg)
1312{
1313 struct decode_cache *c = &ctxt->decode;
1314 unsigned long selector;
1315 int rc;
1316
1317 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001318 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001319 return rc;
1320
Gleb Natapov2e873022010-03-18 15:20:18 +02001321 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001322 return rc;
1323}
1324
Wei Yongjunc37eda12010-06-15 09:03:33 +08001325static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001326 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001327{
1328 struct decode_cache *c = &ctxt->decode;
1329 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001330 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001331 int reg = VCPU_REGS_RAX;
1332
1333 while (reg <= VCPU_REGS_RDI) {
1334 (reg == VCPU_REGS_RSP) ?
1335 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1336
Gleb Natapov79168fd2010-04-28 19:15:30 +03001337 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001338
1339 rc = writeback(ctxt, ops);
1340 if (rc != X86EMUL_CONTINUE)
1341 return rc;
1342
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001343 ++reg;
1344 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001345
1346 /* Disable writeback. */
1347 c->dst.type = OP_NONE;
1348
1349 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001350}
1351
1352static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1353 struct x86_emulate_ops *ops)
1354{
1355 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001356 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001357 int reg = VCPU_REGS_RDI;
1358
1359 while (reg >= VCPU_REGS_RAX) {
1360 if (reg == VCPU_REGS_RSP) {
1361 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1362 c->op_bytes);
1363 --reg;
1364 }
1365
1366 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001367 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001368 break;
1369 --reg;
1370 }
1371 return rc;
1372}
1373
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001374int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1375 struct x86_emulate_ops *ops, int irq)
1376{
1377 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001378 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001379 struct desc_ptr dt;
1380 gva_t cs_addr;
1381 gva_t eip_addr;
1382 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001383
1384 /* TODO: Add limit checks */
1385 c->src.val = ctxt->eflags;
1386 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001387 rc = writeback(ctxt, ops);
1388 if (rc != X86EMUL_CONTINUE)
1389 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001390
1391 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1392
1393 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1394 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001395 rc = writeback(ctxt, ops);
1396 if (rc != X86EMUL_CONTINUE)
1397 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001398
1399 c->src.val = c->eip;
1400 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001401 rc = writeback(ctxt, ops);
1402 if (rc != X86EMUL_CONTINUE)
1403 return rc;
1404
1405 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001406
1407 ops->get_idt(&dt, ctxt->vcpu);
1408
1409 eip_addr = dt.address + (irq << 2);
1410 cs_addr = dt.address + (irq << 2) + 2;
1411
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001412 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001413 if (rc != X86EMUL_CONTINUE)
1414 return rc;
1415
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001416 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001417 if (rc != X86EMUL_CONTINUE)
1418 return rc;
1419
1420 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1421 if (rc != X86EMUL_CONTINUE)
1422 return rc;
1423
1424 c->eip = eip;
1425
1426 return rc;
1427}
1428
1429static int emulate_int(struct x86_emulate_ctxt *ctxt,
1430 struct x86_emulate_ops *ops, int irq)
1431{
1432 switch(ctxt->mode) {
1433 case X86EMUL_MODE_REAL:
1434 return emulate_int_real(ctxt, ops, irq);
1435 case X86EMUL_MODE_VM86:
1436 case X86EMUL_MODE_PROT16:
1437 case X86EMUL_MODE_PROT32:
1438 case X86EMUL_MODE_PROT64:
1439 default:
1440 /* Protected mode interrupts unimplemented yet */
1441 return X86EMUL_UNHANDLEABLE;
1442 }
1443}
1444
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001445static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1446 struct x86_emulate_ops *ops)
1447{
1448 struct decode_cache *c = &ctxt->decode;
1449 int rc = X86EMUL_CONTINUE;
1450 unsigned long temp_eip = 0;
1451 unsigned long temp_eflags = 0;
1452 unsigned long cs = 0;
1453 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1454 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1455 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1456 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1457
1458 /* TODO: Add stack limit check */
1459
1460 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1461
1462 if (rc != X86EMUL_CONTINUE)
1463 return rc;
1464
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001465 if (temp_eip & ~0xffff)
1466 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001467
1468 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1469
1470 if (rc != X86EMUL_CONTINUE)
1471 return rc;
1472
1473 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1474
1475 if (rc != X86EMUL_CONTINUE)
1476 return rc;
1477
1478 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1479
1480 if (rc != X86EMUL_CONTINUE)
1481 return rc;
1482
1483 c->eip = temp_eip;
1484
1485
1486 if (c->op_bytes == 4)
1487 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1488 else if (c->op_bytes == 2) {
1489 ctxt->eflags &= ~0xffff;
1490 ctxt->eflags |= temp_eflags;
1491 }
1492
1493 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1494 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1495
1496 return rc;
1497}
1498
1499static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1500 struct x86_emulate_ops* ops)
1501{
1502 switch(ctxt->mode) {
1503 case X86EMUL_MODE_REAL:
1504 return emulate_iret_real(ctxt, ops);
1505 case X86EMUL_MODE_VM86:
1506 case X86EMUL_MODE_PROT16:
1507 case X86EMUL_MODE_PROT32:
1508 case X86EMUL_MODE_PROT64:
1509 default:
1510 /* iret from protected mode unimplemented yet */
1511 return X86EMUL_UNHANDLEABLE;
1512 }
1513}
1514
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001515static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1516 struct x86_emulate_ops *ops)
1517{
1518 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001519
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001520 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001521}
1522
Laurent Vivier05f086f2007-09-24 11:10:55 +02001523static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001524{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001525 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001526 switch (c->modrm_reg) {
1527 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001528 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001529 break;
1530 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001531 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001532 break;
1533 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001534 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001535 break;
1536 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001537 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001538 break;
1539 case 4: /* sal/shl */
1540 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001541 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001542 break;
1543 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001544 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001545 break;
1546 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001547 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001548 break;
1549 }
1550}
1551
1552static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001553 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001554{
1555 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001556 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1557 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001558 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001559
1560 switch (c->modrm_reg) {
1561 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001562 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001563 break;
1564 case 2: /* not */
1565 c->dst.val = ~c->dst.val;
1566 break;
1567 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001568 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001569 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001570 case 4: /* mul */
1571 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1572 break;
1573 case 5: /* imul */
1574 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1575 break;
1576 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001577 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1578 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001579 break;
1580 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001581 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1582 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001583 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001584 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001585 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001586 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001587 if (de)
1588 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001589 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001590}
1591
1592static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001593 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001594{
1595 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001596
1597 switch (c->modrm_reg) {
1598 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001599 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001600 break;
1601 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001602 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001603 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001604 case 2: /* call near abs */ {
1605 long int old_eip;
1606 old_eip = c->eip;
1607 c->eip = c->src.val;
1608 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001609 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001610 break;
1611 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001612 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001613 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001614 break;
1615 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001616 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001617 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001618 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001619 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001620}
1621
1622static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001623 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001624{
1625 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001626 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001627
1628 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1629 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001630 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1631 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001632 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001633 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001634 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1635 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001636
Laurent Vivier05f086f2007-09-24 11:10:55 +02001637 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001638 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001639 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001640}
1641
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001642static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1643 struct x86_emulate_ops *ops)
1644{
1645 struct decode_cache *c = &ctxt->decode;
1646 int rc;
1647 unsigned long cs;
1648
1649 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001650 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001651 return rc;
1652 if (c->op_bytes == 4)
1653 c->eip = (u32)c->eip;
1654 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001655 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001656 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001657 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001658 return rc;
1659}
1660
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001661static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1662 struct x86_emulate_ops *ops, int seg)
1663{
1664 struct decode_cache *c = &ctxt->decode;
1665 unsigned short sel;
1666 int rc;
1667
1668 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1669
1670 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1671 if (rc != X86EMUL_CONTINUE)
1672 return rc;
1673
1674 c->dst.val = c->src.val;
1675 return rc;
1676}
1677
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001678static inline void
1679setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001680 struct x86_emulate_ops *ops, struct desc_struct *cs,
1681 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001682{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001683 memset(cs, 0, sizeof(struct desc_struct));
Gleb Natapov5601d052011-03-07 14:55:06 +02001684 ops->get_cached_descriptor(cs, NULL, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001685 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001686
1687 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001688 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001689 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001690 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001691 cs->type = 0x0b; /* Read, Execute, Accessed */
1692 cs->s = 1;
1693 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001694 cs->p = 1;
1695 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001696
Gleb Natapov79168fd2010-04-28 19:15:30 +03001697 set_desc_base(ss, 0); /* flat segment */
1698 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001699 ss->g = 1; /* 4kb granularity */
1700 ss->s = 1;
1701 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001702 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001703 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001704 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001705}
1706
1707static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001708emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001709{
1710 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001711 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001712 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001713 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001714
1715 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001716 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001717 ctxt->mode == X86EMUL_MODE_VM86)
1718 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001719
Gleb Natapov79168fd2010-04-28 19:15:30 +03001720 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001721
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001722 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001723 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001724 cs_sel = (u16)(msr_data & 0xfffc);
1725 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001726
1727 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001728 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001729 cs.l = 1;
1730 }
Gleb Natapov5601d052011-03-07 14:55:06 +02001731 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001732 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001733 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001734 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001735
1736 c->regs[VCPU_REGS_RCX] = c->eip;
1737 if (is_long_mode(ctxt->vcpu)) {
1738#ifdef CONFIG_X86_64
1739 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1740
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001741 ops->get_msr(ctxt->vcpu,
1742 ctxt->mode == X86EMUL_MODE_PROT64 ?
1743 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001744 c->eip = msr_data;
1745
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001746 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001747 ctxt->eflags &= ~(msr_data | EFLG_RF);
1748#endif
1749 } else {
1750 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001751 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001752 c->eip = (u32)msr_data;
1753
1754 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1755 }
1756
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001757 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001758}
1759
Andre Przywara8c604352009-06-18 12:56:01 +02001760static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001761emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001762{
1763 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001764 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001765 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001766 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001767
Gleb Natapova0044752010-02-10 14:21:31 +02001768 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001769 if (ctxt->mode == X86EMUL_MODE_REAL)
1770 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001771
1772 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1773 * Therefore, we inject an #UD.
1774 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001775 if (ctxt->mode == X86EMUL_MODE_PROT64)
1776 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001777
Gleb Natapov79168fd2010-04-28 19:15:30 +03001778 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001779
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001780 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001781 switch (ctxt->mode) {
1782 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001783 if ((msr_data & 0xfffc) == 0x0)
1784 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001785 break;
1786 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001787 if (msr_data == 0x0)
1788 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001789 break;
1790 }
1791
1792 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001793 cs_sel = (u16)msr_data;
1794 cs_sel &= ~SELECTOR_RPL_MASK;
1795 ss_sel = cs_sel + 8;
1796 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001797 if (ctxt->mode == X86EMUL_MODE_PROT64
1798 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001799 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001800 cs.l = 1;
1801 }
1802
Gleb Natapov5601d052011-03-07 14:55:06 +02001803 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001804 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001805 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001806 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001807
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001808 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001809 c->eip = msr_data;
1810
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001811 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001812 c->regs[VCPU_REGS_RSP] = msr_data;
1813
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001814 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001815}
1816
Andre Przywara4668f052009-06-18 12:56:02 +02001817static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001818emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001819{
1820 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001821 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001822 u64 msr_data;
1823 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001824 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001825
Gleb Natapova0044752010-02-10 14:21:31 +02001826 /* inject #GP if in real mode or Virtual 8086 mode */
1827 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001828 ctxt->mode == X86EMUL_MODE_VM86)
1829 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001830
Gleb Natapov79168fd2010-04-28 19:15:30 +03001831 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001832
1833 if ((c->rex_prefix & 0x8) != 0x0)
1834 usermode = X86EMUL_MODE_PROT64;
1835 else
1836 usermode = X86EMUL_MODE_PROT32;
1837
1838 cs.dpl = 3;
1839 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001840 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001841 switch (usermode) {
1842 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001844 if ((msr_data & 0xfffc) == 0x0)
1845 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001846 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001847 break;
1848 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001849 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001850 if (msr_data == 0x0)
1851 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001852 ss_sel = cs_sel + 8;
1853 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001854 cs.l = 1;
1855 break;
1856 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001857 cs_sel |= SELECTOR_RPL_MASK;
1858 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001859
Gleb Natapov5601d052011-03-07 14:55:06 +02001860 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001861 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001862 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001863 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001864
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001865 c->eip = c->regs[VCPU_REGS_RDX];
1866 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001867
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001868 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001869}
1870
Gleb Natapov9c537242010-03-18 15:20:05 +02001871static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1872 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001873{
1874 int iopl;
1875 if (ctxt->mode == X86EMUL_MODE_REAL)
1876 return false;
1877 if (ctxt->mode == X86EMUL_MODE_VM86)
1878 return true;
1879 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001880 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001881}
1882
1883static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1884 struct x86_emulate_ops *ops,
1885 u16 port, u16 len)
1886{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001887 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02001888 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001889 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001890 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001891 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02001892 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001893
Gleb Natapov5601d052011-03-07 14:55:06 +02001894 ops->get_cached_descriptor(&tr_seg, &base3, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001895 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001896 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001897 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001898 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02001899 base = get_desc_base(&tr_seg);
1900#ifdef CONFIG_X86_64
1901 base |= ((u64)base3) << 32;
1902#endif
1903 r = ops->read_std(base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001904 if (r != X86EMUL_CONTINUE)
1905 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001906 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001907 return false;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001908 r = ops->read_std(base + io_bitmap_ptr + port/8, &perm, 2, ctxt->vcpu,
Gleb Natapov5601d052011-03-07 14:55:06 +02001909 NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001910 if (r != X86EMUL_CONTINUE)
1911 return false;
1912 if ((perm >> bit_idx) & mask)
1913 return false;
1914 return true;
1915}
1916
1917static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1918 struct x86_emulate_ops *ops,
1919 u16 port, u16 len)
1920{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001921 if (ctxt->perm_ok)
1922 return true;
1923
Gleb Natapov9c537242010-03-18 15:20:05 +02001924 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001925 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1926 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001927
1928 ctxt->perm_ok = true;
1929
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001930 return true;
1931}
1932
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001933static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1934 struct x86_emulate_ops *ops,
1935 struct tss_segment_16 *tss)
1936{
1937 struct decode_cache *c = &ctxt->decode;
1938
1939 tss->ip = c->eip;
1940 tss->flag = ctxt->eflags;
1941 tss->ax = c->regs[VCPU_REGS_RAX];
1942 tss->cx = c->regs[VCPU_REGS_RCX];
1943 tss->dx = c->regs[VCPU_REGS_RDX];
1944 tss->bx = c->regs[VCPU_REGS_RBX];
1945 tss->sp = c->regs[VCPU_REGS_RSP];
1946 tss->bp = c->regs[VCPU_REGS_RBP];
1947 tss->si = c->regs[VCPU_REGS_RSI];
1948 tss->di = c->regs[VCPU_REGS_RDI];
1949
1950 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1951 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1952 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1953 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1954 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1955}
1956
1957static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1958 struct x86_emulate_ops *ops,
1959 struct tss_segment_16 *tss)
1960{
1961 struct decode_cache *c = &ctxt->decode;
1962 int ret;
1963
1964 c->eip = tss->ip;
1965 ctxt->eflags = tss->flag | 2;
1966 c->regs[VCPU_REGS_RAX] = tss->ax;
1967 c->regs[VCPU_REGS_RCX] = tss->cx;
1968 c->regs[VCPU_REGS_RDX] = tss->dx;
1969 c->regs[VCPU_REGS_RBX] = tss->bx;
1970 c->regs[VCPU_REGS_RSP] = tss->sp;
1971 c->regs[VCPU_REGS_RBP] = tss->bp;
1972 c->regs[VCPU_REGS_RSI] = tss->si;
1973 c->regs[VCPU_REGS_RDI] = tss->di;
1974
1975 /*
1976 * SDM says that segment selectors are loaded before segment
1977 * descriptors
1978 */
1979 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1980 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1981 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1982 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1983 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1984
1985 /*
1986 * Now load segment descriptors. If fault happenes at this stage
1987 * it is handled in a context of new task
1988 */
1989 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1990 if (ret != X86EMUL_CONTINUE)
1991 return ret;
1992 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1993 if (ret != X86EMUL_CONTINUE)
1994 return ret;
1995 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1996 if (ret != X86EMUL_CONTINUE)
1997 return ret;
1998 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1999 if (ret != X86EMUL_CONTINUE)
2000 return ret;
2001 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2002 if (ret != X86EMUL_CONTINUE)
2003 return ret;
2004
2005 return X86EMUL_CONTINUE;
2006}
2007
2008static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2009 struct x86_emulate_ops *ops,
2010 u16 tss_selector, u16 old_tss_sel,
2011 ulong old_tss_base, struct desc_struct *new_desc)
2012{
2013 struct tss_segment_16 tss_seg;
2014 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002015 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002016
2017 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002018 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002019 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002020 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002021 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002022
2023 save_state_to_tss16(ctxt, ops, &tss_seg);
2024
2025 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002026 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002027 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002028 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002029 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002030
2031 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002032 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002033 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002034 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002035 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002036
2037 if (old_tss_sel != 0xffff) {
2038 tss_seg.prev_task_link = old_tss_sel;
2039
2040 ret = ops->write_std(new_tss_base,
2041 &tss_seg.prev_task_link,
2042 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002043 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002044 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002045 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002046 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002047 }
2048
2049 return load_state_from_tss16(ctxt, ops, &tss_seg);
2050}
2051
2052static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2053 struct x86_emulate_ops *ops,
2054 struct tss_segment_32 *tss)
2055{
2056 struct decode_cache *c = &ctxt->decode;
2057
2058 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2059 tss->eip = c->eip;
2060 tss->eflags = ctxt->eflags;
2061 tss->eax = c->regs[VCPU_REGS_RAX];
2062 tss->ecx = c->regs[VCPU_REGS_RCX];
2063 tss->edx = c->regs[VCPU_REGS_RDX];
2064 tss->ebx = c->regs[VCPU_REGS_RBX];
2065 tss->esp = c->regs[VCPU_REGS_RSP];
2066 tss->ebp = c->regs[VCPU_REGS_RBP];
2067 tss->esi = c->regs[VCPU_REGS_RSI];
2068 tss->edi = c->regs[VCPU_REGS_RDI];
2069
2070 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2071 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2072 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2073 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2074 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2075 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2076 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2077}
2078
2079static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2080 struct x86_emulate_ops *ops,
2081 struct tss_segment_32 *tss)
2082{
2083 struct decode_cache *c = &ctxt->decode;
2084 int ret;
2085
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002086 if (ops->set_cr(3, tss->cr3, ctxt->vcpu))
2087 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002088 c->eip = tss->eip;
2089 ctxt->eflags = tss->eflags | 2;
2090 c->regs[VCPU_REGS_RAX] = tss->eax;
2091 c->regs[VCPU_REGS_RCX] = tss->ecx;
2092 c->regs[VCPU_REGS_RDX] = tss->edx;
2093 c->regs[VCPU_REGS_RBX] = tss->ebx;
2094 c->regs[VCPU_REGS_RSP] = tss->esp;
2095 c->regs[VCPU_REGS_RBP] = tss->ebp;
2096 c->regs[VCPU_REGS_RSI] = tss->esi;
2097 c->regs[VCPU_REGS_RDI] = tss->edi;
2098
2099 /*
2100 * SDM says that segment selectors are loaded before segment
2101 * descriptors
2102 */
2103 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2104 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2105 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2106 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2107 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2108 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2109 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2110
2111 /*
2112 * Now load segment descriptors. If fault happenes at this stage
2113 * it is handled in a context of new task
2114 */
2115 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2116 if (ret != X86EMUL_CONTINUE)
2117 return ret;
2118 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2119 if (ret != X86EMUL_CONTINUE)
2120 return ret;
2121 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2122 if (ret != X86EMUL_CONTINUE)
2123 return ret;
2124 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2125 if (ret != X86EMUL_CONTINUE)
2126 return ret;
2127 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2128 if (ret != X86EMUL_CONTINUE)
2129 return ret;
2130 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2131 if (ret != X86EMUL_CONTINUE)
2132 return ret;
2133 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2134 if (ret != X86EMUL_CONTINUE)
2135 return ret;
2136
2137 return X86EMUL_CONTINUE;
2138}
2139
2140static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2141 struct x86_emulate_ops *ops,
2142 u16 tss_selector, u16 old_tss_sel,
2143 ulong old_tss_base, struct desc_struct *new_desc)
2144{
2145 struct tss_segment_32 tss_seg;
2146 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002147 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148
2149 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002150 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002151 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154
2155 save_state_to_tss32(ctxt, ops, &tss_seg);
2156
2157 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002158 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002159 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002160 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002161 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162
2163 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002164 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002165 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002166 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168
2169 if (old_tss_sel != 0xffff) {
2170 tss_seg.prev_task_link = old_tss_sel;
2171
2172 ret = ops->write_std(new_tss_base,
2173 &tss_seg.prev_task_link,
2174 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002175 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002176 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002179 }
2180
2181 return load_state_from_tss32(ctxt, ops, &tss_seg);
2182}
2183
2184static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002185 struct x86_emulate_ops *ops,
2186 u16 tss_selector, int reason,
2187 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002188{
2189 struct desc_struct curr_tss_desc, next_tss_desc;
2190 int ret;
2191 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2192 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002193 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002194 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002195
2196 /* FIXME: old_tss_base == ~0 ? */
2197
2198 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2199 if (ret != X86EMUL_CONTINUE)
2200 return ret;
2201 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2202 if (ret != X86EMUL_CONTINUE)
2203 return ret;
2204
2205 /* FIXME: check that next_tss_desc is tss */
2206
2207 if (reason != TASK_SWITCH_IRET) {
2208 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002209 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl)
2210 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002211 }
2212
Gleb Natapovceffb452010-03-18 15:20:19 +02002213 desc_limit = desc_limit_scaled(&next_tss_desc);
2214 if (!next_tss_desc.p ||
2215 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2216 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002217 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218 return X86EMUL_PROPAGATE_FAULT;
2219 }
2220
2221 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2222 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2223 write_segment_descriptor(ctxt, ops, old_tss_sel,
2224 &curr_tss_desc);
2225 }
2226
2227 if (reason == TASK_SWITCH_IRET)
2228 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2229
2230 /* set back link to prev task only if NT bit is set in eflags
2231 note that old_tss_sel is not used afetr this point */
2232 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2233 old_tss_sel = 0xffff;
2234
2235 if (next_tss_desc.type & 8)
2236 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2237 old_tss_base, &next_tss_desc);
2238 else
2239 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2240 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002241 if (ret != X86EMUL_CONTINUE)
2242 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002243
2244 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2245 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2246
2247 if (reason != TASK_SWITCH_IRET) {
2248 next_tss_desc.type |= (1 << 1); /* set busy flag */
2249 write_segment_descriptor(ctxt, ops, tss_selector,
2250 &next_tss_desc);
2251 }
2252
2253 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02002254 ops->set_cached_descriptor(&next_tss_desc, 0, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002255 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2256
Jan Kiszkae269fb22010-04-14 15:51:09 +02002257 if (has_error_code) {
2258 struct decode_cache *c = &ctxt->decode;
2259
2260 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2261 c->lock_prefix = 0;
2262 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002263 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002264 }
2265
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002266 return ret;
2267}
2268
2269int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002270 u16 tss_selector, int reason,
2271 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002272{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002273 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002274 struct decode_cache *c = &ctxt->decode;
2275 int rc;
2276
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002277 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002278 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002279
Jan Kiszkae269fb22010-04-14 15:51:09 +02002280 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2281 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282
2283 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002284 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002285 if (rc == X86EMUL_CONTINUE)
2286 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002287 }
2288
Gleb Natapov19d04432010-04-15 12:29:50 +03002289 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002290}
2291
Avi Kivity90de84f2010-11-17 15:28:21 +02002292static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002293 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002294{
2295 struct decode_cache *c = &ctxt->decode;
2296 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2297
Gleb Natapovd9271122010-03-18 15:20:22 +02002298 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002299 op->addr.mem.ea = register_address(c, c->regs[reg]);
2300 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002301}
2302
Avi Kivity63540382010-07-29 15:11:55 +03002303static int em_push(struct x86_emulate_ctxt *ctxt)
2304{
2305 emulate_push(ctxt, ctxt->ops);
2306 return X86EMUL_CONTINUE;
2307}
2308
Avi Kivity7af04fc2010-08-18 14:16:35 +03002309static int em_das(struct x86_emulate_ctxt *ctxt)
2310{
2311 struct decode_cache *c = &ctxt->decode;
2312 u8 al, old_al;
2313 bool af, cf, old_cf;
2314
2315 cf = ctxt->eflags & X86_EFLAGS_CF;
2316 al = c->dst.val;
2317
2318 old_al = al;
2319 old_cf = cf;
2320 cf = false;
2321 af = ctxt->eflags & X86_EFLAGS_AF;
2322 if ((al & 0x0f) > 9 || af) {
2323 al -= 6;
2324 cf = old_cf | (al >= 250);
2325 af = true;
2326 } else {
2327 af = false;
2328 }
2329 if (old_al > 0x99 || old_cf) {
2330 al -= 0x60;
2331 cf = true;
2332 }
2333
2334 c->dst.val = al;
2335 /* Set PF, ZF, SF */
2336 c->src.type = OP_IMM;
2337 c->src.val = 0;
2338 c->src.bytes = 1;
2339 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2340 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2341 if (cf)
2342 ctxt->eflags |= X86_EFLAGS_CF;
2343 if (af)
2344 ctxt->eflags |= X86_EFLAGS_AF;
2345 return X86EMUL_CONTINUE;
2346}
2347
Avi Kivity0ef753b2010-08-18 14:51:45 +03002348static int em_call_far(struct x86_emulate_ctxt *ctxt)
2349{
2350 struct decode_cache *c = &ctxt->decode;
2351 u16 sel, old_cs;
2352 ulong old_eip;
2353 int rc;
2354
2355 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2356 old_eip = c->eip;
2357
2358 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2359 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2360 return X86EMUL_CONTINUE;
2361
2362 c->eip = 0;
2363 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2364
2365 c->src.val = old_cs;
2366 emulate_push(ctxt, ctxt->ops);
2367 rc = writeback(ctxt, ctxt->ops);
2368 if (rc != X86EMUL_CONTINUE)
2369 return rc;
2370
2371 c->src.val = old_eip;
2372 emulate_push(ctxt, ctxt->ops);
2373 rc = writeback(ctxt, ctxt->ops);
2374 if (rc != X86EMUL_CONTINUE)
2375 return rc;
2376
2377 c->dst.type = OP_NONE;
2378
2379 return X86EMUL_CONTINUE;
2380}
2381
Avi Kivity40ece7c2010-08-18 15:12:09 +03002382static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2383{
2384 struct decode_cache *c = &ctxt->decode;
2385 int rc;
2386
2387 c->dst.type = OP_REG;
2388 c->dst.addr.reg = &c->eip;
2389 c->dst.bytes = c->op_bytes;
2390 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2391 if (rc != X86EMUL_CONTINUE)
2392 return rc;
2393 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2394 return X86EMUL_CONTINUE;
2395}
2396
Avi Kivity5c82aa22010-08-18 18:31:43 +03002397static int em_imul(struct x86_emulate_ctxt *ctxt)
2398{
2399 struct decode_cache *c = &ctxt->decode;
2400
2401 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2402 return X86EMUL_CONTINUE;
2403}
2404
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002405static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2406{
2407 struct decode_cache *c = &ctxt->decode;
2408
2409 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002410 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002411}
2412
Avi Kivity61429142010-08-19 15:13:00 +03002413static int em_cwd(struct x86_emulate_ctxt *ctxt)
2414{
2415 struct decode_cache *c = &ctxt->decode;
2416
2417 c->dst.type = OP_REG;
2418 c->dst.bytes = c->src.bytes;
2419 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2420 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2421
2422 return X86EMUL_CONTINUE;
2423}
2424
Avi Kivity48bb5d32010-08-18 18:54:34 +03002425static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2426{
2427 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2428 struct decode_cache *c = &ctxt->decode;
2429 u64 tsc = 0;
2430
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002431 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD))
2432 return emulate_gp(ctxt, 0);
Avi Kivity48bb5d32010-08-18 18:54:34 +03002433 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2434 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2435 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2436 return X86EMUL_CONTINUE;
2437}
2438
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002439static int em_mov(struct x86_emulate_ctxt *ctxt)
2440{
2441 struct decode_cache *c = &ctxt->decode;
2442 c->dst.val = c->src.val;
2443 return X86EMUL_CONTINUE;
2444}
2445
Avi Kivityaa97bb42010-01-20 18:09:23 +02002446static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2447{
2448 struct decode_cache *c = &ctxt->decode;
2449 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2450 return X86EMUL_CONTINUE;
2451}
2452
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002453static bool valid_cr(int nr)
2454{
2455 switch (nr) {
2456 case 0:
2457 case 2 ... 4:
2458 case 8:
2459 return true;
2460 default:
2461 return false;
2462 }
2463}
2464
2465static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2466{
2467 struct decode_cache *c = &ctxt->decode;
2468
2469 if (!valid_cr(c->modrm_reg))
2470 return emulate_ud(ctxt);
2471
2472 return X86EMUL_CONTINUE;
2473}
2474
2475static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2476{
2477 struct decode_cache *c = &ctxt->decode;
2478 u64 new_val = c->src.val64;
2479 int cr = c->modrm_reg;
2480
2481 static u64 cr_reserved_bits[] = {
2482 0xffffffff00000000ULL,
2483 0, 0, 0, /* CR3 checked later */
2484 CR4_RESERVED_BITS,
2485 0, 0, 0,
2486 CR8_RESERVED_BITS,
2487 };
2488
2489 if (!valid_cr(cr))
2490 return emulate_ud(ctxt);
2491
2492 if (new_val & cr_reserved_bits[cr])
2493 return emulate_gp(ctxt, 0);
2494
2495 switch (cr) {
2496 case 0: {
2497 u64 cr4, efer;
2498 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2499 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2500 return emulate_gp(ctxt, 0);
2501
2502 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2503 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2504
2505 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2506 !(cr4 & X86_CR4_PAE))
2507 return emulate_gp(ctxt, 0);
2508
2509 break;
2510 }
2511 case 3: {
2512 u64 rsvd = 0;
2513
2514 if (is_long_mode(ctxt->vcpu))
2515 rsvd = CR3_L_MODE_RESERVED_BITS;
2516 else if (is_pae(ctxt->vcpu))
2517 rsvd = CR3_PAE_RESERVED_BITS;
2518 else if (is_paging(ctxt->vcpu))
2519 rsvd = CR3_NONPAE_RESERVED_BITS;
2520
2521 if (new_val & rsvd)
2522 return emulate_gp(ctxt, 0);
2523
2524 break;
2525 }
2526 case 4: {
2527 u64 cr4, efer;
2528
2529 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2530 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2531
2532 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2533 return emulate_gp(ctxt, 0);
2534
2535 break;
2536 }
2537 }
2538
2539 return X86EMUL_CONTINUE;
2540}
2541
Joerg Roedel3b88e412011-04-04 12:39:29 +02002542static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2543{
2544 unsigned long dr7;
2545
2546 ctxt->ops->get_dr(7, &dr7, ctxt->vcpu);
2547
2548 /* Check if DR7.Global_Enable is set */
2549 return dr7 & (1 << 13);
2550}
2551
2552static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2553{
2554 struct decode_cache *c = &ctxt->decode;
2555 int dr = c->modrm_reg;
2556 u64 cr4;
2557
2558 if (dr > 7)
2559 return emulate_ud(ctxt);
2560
2561 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2562 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2563 return emulate_ud(ctxt);
2564
2565 if (check_dr7_gd(ctxt))
2566 return emulate_db(ctxt);
2567
2568 return X86EMUL_CONTINUE;
2569}
2570
2571static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2572{
2573 struct decode_cache *c = &ctxt->decode;
2574 u64 new_val = c->src.val64;
2575 int dr = c->modrm_reg;
2576
2577 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2578 return emulate_gp(ctxt, 0);
2579
2580 return check_dr_read(ctxt);
2581}
2582
Avi Kivity73fba5f2010-07-29 15:11:53 +03002583#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002584#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002585#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2586 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002587#define N D(0)
2588#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2589#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2590#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002591#define II(_f, _e, _i) \
2592 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002593#define IIP(_f, _e, _i, _p) \
2594 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2595 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002596#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002597
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002598#define D2bv(_f) D((_f) | ByteOp), D(_f)
2599#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2600
Avi Kivity6230f7f2010-08-26 18:34:55 +03002601#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2602 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2603 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2604
2605
Avi Kivity73fba5f2010-07-29 15:11:53 +03002606static struct opcode group1[] = {
2607 X7(D(Lock)), N
2608};
2609
2610static struct opcode group1A[] = {
2611 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2612};
2613
2614static struct opcode group3[] = {
2615 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2616 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002617 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002618};
2619
2620static struct opcode group4[] = {
2621 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2622 N, N, N, N, N, N,
2623};
2624
2625static struct opcode group5[] = {
2626 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002627 D(SrcMem | ModRM | Stack),
2628 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002629 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2630 D(SrcMem | ModRM | Stack), N,
2631};
2632
2633static struct group_dual group7 = { {
Avi Kivity3c6e2762011-04-04 12:39:23 +02002634 N, N, DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
2635 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2636 DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
2637 DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002638}, {
Avi Kivityd8671622011-02-01 16:32:03 +02002639 D(SrcNone | ModRM | Priv | VendorSpecific), N,
2640 N, D(SrcNone | ModRM | Priv | VendorSpecific),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002641 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2642 DI(SrcMem16 | ModRM | Mov | Priv, lmsw), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002643} };
2644
2645static struct opcode group8[] = {
2646 N, N, N, N,
2647 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2648 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2649};
2650
2651static struct group_dual group9 = { {
2652 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2653}, {
2654 N, N, N, N, N, N, N, N,
2655} };
2656
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002657static struct opcode group11[] = {
2658 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2659};
2660
Avi Kivityaa97bb42010-01-20 18:09:23 +02002661static struct gprefix pfx_0f_6f_0f_7f = {
2662 N, N, N, I(Sse, em_movdqu),
2663};
2664
Avi Kivity73fba5f2010-07-29 15:11:53 +03002665static struct opcode opcode_table[256] = {
2666 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002667 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002668 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2669 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002670 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002671 D(ImplicitOps | Stack | No64), N,
2672 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002673 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002674 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2675 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002676 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002677 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2678 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002679 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002680 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002681 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002682 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002683 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002684 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002685 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002686 /* 0x40 - 0x4F */
2687 X16(D(DstReg)),
2688 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002689 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002690 /* 0x58 - 0x5F */
2691 X8(D(DstReg | Stack)),
2692 /* 0x60 - 0x67 */
2693 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2694 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2695 N, N, N, N,
2696 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002697 I(SrcImm | Mov | Stack, em_push),
2698 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002699 I(SrcImmByte | Mov | Stack, em_push),
2700 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002701 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2702 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002703 /* 0x70 - 0x7F */
2704 X16(D(SrcImmByte)),
2705 /* 0x80 - 0x87 */
2706 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2707 G(DstMem | SrcImm | ModRM | Group, group1),
2708 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2709 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002710 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002711 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002712 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2713 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002714 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002715 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2716 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002717 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002718 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002719 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002720 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002721 DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002722 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002723 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2724 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2725 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2726 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002727 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002728 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002729 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2730 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002731 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002732 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002733 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002734 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002735 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002736 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002737 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002738 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2739 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002740 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002741 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002742 /* 0xC8 - 0xCF */
2743 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002744 D(ImplicitOps), DI(SrcImmByte, intn),
2745 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002746 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002747 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002748 N, N, N, N,
2749 /* 0xD8 - 0xDF */
2750 N, N, N, N, N, N, N, N,
2751 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002752 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002753 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002754 /* 0xE8 - 0xEF */
2755 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2756 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002757 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002758 /* 0xF0 - 0xF7 */
2759 N, N, N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002760 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
2761 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002762 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002763 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002764 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2765};
2766
2767static struct opcode twobyte_table[256] = {
2768 /* 0x00 - 0x0F */
2769 N, GD(0, &group7), N, N,
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002770 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002771 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002772 N, D(ImplicitOps | ModRM), N, N,
2773 /* 0x10 - 0x1F */
2774 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2775 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002776 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02002777 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002778 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02002779 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002780 N, N, N, N,
2781 N, N, N, N, N, N, N, N,
2782 /* 0x30 - 0x3F */
Avi Kivity3c6e2762011-04-04 12:39:23 +02002783 D(ImplicitOps | Priv), II(ImplicitOps, em_rdtsc, rdtsc),
Avi Kivity48bb5d32010-08-18 18:54:34 +03002784 D(ImplicitOps | Priv), N,
Avi Kivityd8671622011-02-01 16:32:03 +02002785 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
2786 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002787 N, N, N, N, N, N, N, N,
2788 /* 0x40 - 0x4F */
2789 X16(D(DstReg | SrcMem | ModRM | Mov)),
2790 /* 0x50 - 0x5F */
2791 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2792 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002793 N, N, N, N,
2794 N, N, N, N,
2795 N, N, N, N,
2796 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002797 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002798 N, N, N, N,
2799 N, N, N, N,
2800 N, N, N, N,
2801 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002802 /* 0x80 - 0x8F */
2803 X16(D(SrcImm)),
2804 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002805 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002806 /* 0xA0 - 0xA7 */
2807 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2808 N, D(DstMem | SrcReg | ModRM | BitOp),
2809 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2810 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2811 /* 0xA8 - 0xAF */
2812 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2813 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2814 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2815 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002816 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002817 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002818 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002819 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2820 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2821 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002822 /* 0xB8 - 0xBF */
2823 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002824 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002825 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2826 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002827 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002828 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002829 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002830 N, N, N, GD(0, &group9),
2831 N, N, N, N, N, N, N, N,
2832 /* 0xD0 - 0xDF */
2833 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2834 /* 0xE0 - 0xEF */
2835 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2836 /* 0xF0 - 0xFF */
2837 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2838};
2839
2840#undef D
2841#undef N
2842#undef G
2843#undef GD
2844#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02002845#undef GP
Avi Kivity73fba5f2010-07-29 15:11:53 +03002846
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002847#undef D2bv
2848#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002849#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002850
Avi Kivity39f21ee2010-08-18 19:20:21 +03002851static unsigned imm_size(struct decode_cache *c)
2852{
2853 unsigned size;
2854
2855 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2856 if (size == 8)
2857 size = 4;
2858 return size;
2859}
2860
2861static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2862 unsigned size, bool sign_extension)
2863{
2864 struct decode_cache *c = &ctxt->decode;
2865 struct x86_emulate_ops *ops = ctxt->ops;
2866 int rc = X86EMUL_CONTINUE;
2867
2868 op->type = OP_IMM;
2869 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002870 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002871 /* NB. Immediates are sign-extended as necessary. */
2872 switch (op->bytes) {
2873 case 1:
2874 op->val = insn_fetch(s8, 1, c->eip);
2875 break;
2876 case 2:
2877 op->val = insn_fetch(s16, 2, c->eip);
2878 break;
2879 case 4:
2880 op->val = insn_fetch(s32, 4, c->eip);
2881 break;
2882 }
2883 if (!sign_extension) {
2884 switch (op->bytes) {
2885 case 1:
2886 op->val &= 0xff;
2887 break;
2888 case 2:
2889 op->val &= 0xffff;
2890 break;
2891 case 4:
2892 op->val &= 0xffffffff;
2893 break;
2894 }
2895 }
2896done:
2897 return rc;
2898}
2899
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002900int
Andre Przywaradc25e892010-12-21 11:12:07 +01002901x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002902{
2903 struct x86_emulate_ops *ops = ctxt->ops;
2904 struct decode_cache *c = &ctxt->decode;
2905 int rc = X86EMUL_CONTINUE;
2906 int mode = ctxt->mode;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002907 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
2908 bool op_prefix = false;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002909 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002910 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002911
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002912 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01002913 c->fetch.start = c->eip;
2914 c->fetch.end = c->fetch.start + insn_len;
2915 if (insn_len > 0)
2916 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002917 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2918
2919 switch (mode) {
2920 case X86EMUL_MODE_REAL:
2921 case X86EMUL_MODE_VM86:
2922 case X86EMUL_MODE_PROT16:
2923 def_op_bytes = def_ad_bytes = 2;
2924 break;
2925 case X86EMUL_MODE_PROT32:
2926 def_op_bytes = def_ad_bytes = 4;
2927 break;
2928#ifdef CONFIG_X86_64
2929 case X86EMUL_MODE_PROT64:
2930 def_op_bytes = 4;
2931 def_ad_bytes = 8;
2932 break;
2933#endif
2934 default:
2935 return -1;
2936 }
2937
2938 c->op_bytes = def_op_bytes;
2939 c->ad_bytes = def_ad_bytes;
2940
2941 /* Legacy prefixes. */
2942 for (;;) {
2943 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2944 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002945 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002946 /* switch between 2/4 bytes */
2947 c->op_bytes = def_op_bytes ^ 6;
2948 break;
2949 case 0x67: /* address-size override */
2950 if (mode == X86EMUL_MODE_PROT64)
2951 /* switch between 4/8 bytes */
2952 c->ad_bytes = def_ad_bytes ^ 12;
2953 else
2954 /* switch between 2/4 bytes */
2955 c->ad_bytes = def_ad_bytes ^ 6;
2956 break;
2957 case 0x26: /* ES override */
2958 case 0x2e: /* CS override */
2959 case 0x36: /* SS override */
2960 case 0x3e: /* DS override */
2961 set_seg_override(c, (c->b >> 3) & 3);
2962 break;
2963 case 0x64: /* FS override */
2964 case 0x65: /* GS override */
2965 set_seg_override(c, c->b & 7);
2966 break;
2967 case 0x40 ... 0x4f: /* REX */
2968 if (mode != X86EMUL_MODE_PROT64)
2969 goto done_prefixes;
2970 c->rex_prefix = c->b;
2971 continue;
2972 case 0xf0: /* LOCK */
2973 c->lock_prefix = 1;
2974 break;
2975 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002976 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02002977 c->rep_prefix = c->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002978 break;
2979 default:
2980 goto done_prefixes;
2981 }
2982
2983 /* Any legacy prefix after a REX prefix nullifies its effect. */
2984
2985 c->rex_prefix = 0;
2986 }
2987
2988done_prefixes:
2989
2990 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002991 if (c->rex_prefix & 8)
2992 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002993
2994 /* Opcode byte(s). */
2995 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002996 /* Two-byte opcode? */
2997 if (c->b == 0x0f) {
2998 c->twobyte = 1;
2999 c->b = insn_fetch(u8, 1, c->eip);
3000 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003001 }
3002 c->d = opcode.flags;
3003
3004 if (c->d & Group) {
3005 dual = c->d & GroupDual;
3006 c->modrm = insn_fetch(u8, 1, c->eip);
3007 --c->eip;
3008
3009 if (c->d & GroupDual) {
3010 g_mod012 = opcode.u.gdual->mod012;
3011 g_mod3 = opcode.u.gdual->mod3;
3012 } else
3013 g_mod012 = g_mod3 = opcode.u.group;
3014
3015 c->d &= ~(Group | GroupDual);
3016
3017 goffset = (c->modrm >> 3) & 7;
3018
3019 if ((c->modrm >> 6) == 3)
3020 opcode = g_mod3[goffset];
3021 else
3022 opcode = g_mod012[goffset];
3023 c->d |= opcode.flags;
3024 }
3025
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003026 if (c->d & Prefix) {
3027 if (c->rep_prefix && op_prefix)
3028 return X86EMUL_UNHANDLEABLE;
3029 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3030 switch (simd_prefix) {
3031 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3032 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3033 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3034 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3035 }
3036 c->d |= opcode.flags;
3037 }
3038
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003039 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02003040 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02003041 c->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003042
3043 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02003044 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003045 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003046
Avi Kivityd8671622011-02-01 16:32:03 +02003047 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3048 return -1;
3049
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003050 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3051 c->op_bytes = 8;
3052
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003053 if (c->d & Op3264) {
3054 if (mode == X86EMUL_MODE_PROT64)
3055 c->op_bytes = 8;
3056 else
3057 c->op_bytes = 4;
3058 }
3059
Avi Kivity12537912011-03-29 11:41:27 +02003060 if (c->d & Sse)
3061 c->op_bytes = 16;
3062
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003063 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03003064 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003065 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03003066 if (!c->has_seg_override)
3067 set_seg_override(c, c->modrm_seg);
3068 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003069 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003070 if (rc != X86EMUL_CONTINUE)
3071 goto done;
3072
3073 if (!c->has_seg_override)
3074 set_seg_override(c, VCPU_SREG_DS);
3075
Avi Kivity90de84f2010-11-17 15:28:21 +02003076 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003077
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003078 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003079 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003080
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003081 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02003082 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003083
3084 /*
3085 * Decode and fetch the source operand: register, memory
3086 * or immediate.
3087 */
3088 switch (c->d & SrcMask) {
3089 case SrcNone:
3090 break;
3091 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02003092 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003093 break;
3094 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003095 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003096 goto srcmem_common;
3097 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003098 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003099 goto srcmem_common;
3100 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003101 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003102 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003103 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003104 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003105 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003106 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003107 rc = decode_imm(ctxt, &c->src, 2, false);
3108 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003109 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003110 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3111 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003112 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003113 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003114 break;
3115 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003116 rc = decode_imm(ctxt, &c->src, 1, true);
3117 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003118 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003119 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003120 break;
3121 case SrcAcc:
3122 c->src.type = OP_REG;
3123 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003124 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003125 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003126 break;
3127 case SrcOne:
3128 c->src.bytes = 1;
3129 c->src.val = 1;
3130 break;
3131 case SrcSI:
3132 c->src.type = OP_MEM;
3133 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003134 c->src.addr.mem.ea =
3135 register_address(c, c->regs[VCPU_REGS_RSI]);
3136 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003137 c->src.val = 0;
3138 break;
3139 case SrcImmFAddr:
3140 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003141 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003142 c->src.bytes = c->op_bytes + 2;
3143 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3144 break;
3145 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003146 memop.bytes = c->op_bytes + 2;
3147 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003148 break;
3149 }
3150
Avi Kivity39f21ee2010-08-18 19:20:21 +03003151 if (rc != X86EMUL_CONTINUE)
3152 goto done;
3153
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003154 /*
3155 * Decode and fetch the second source operand: register, memory
3156 * or immediate.
3157 */
3158 switch (c->d & Src2Mask) {
3159 case Src2None:
3160 break;
3161 case Src2CL:
3162 c->src2.bytes = 1;
3163 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3164 break;
3165 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003166 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003167 break;
3168 case Src2One:
3169 c->src2.bytes = 1;
3170 c->src2.val = 1;
3171 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003172 case Src2Imm:
3173 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3174 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003175 }
3176
Avi Kivity39f21ee2010-08-18 19:20:21 +03003177 if (rc != X86EMUL_CONTINUE)
3178 goto done;
3179
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003180 /* Decode and fetch the destination operand: register or memory. */
3181 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003182 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003183 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003184 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3185 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003186 case DstImmUByte:
3187 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003188 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003189 c->dst.bytes = 1;
3190 c->dst.val = insn_fetch(u8, 1, c->eip);
3191 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003192 case DstMem:
3193 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003194 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003195 if ((c->d & DstMask) == DstMem64)
3196 c->dst.bytes = 8;
3197 else
3198 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003199 if (c->d & BitOp)
3200 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003201 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003202 break;
3203 case DstAcc:
3204 c->dst.type = OP_REG;
3205 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003206 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003207 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003208 c->dst.orig_val = c->dst.val;
3209 break;
3210 case DstDI:
3211 c->dst.type = OP_MEM;
3212 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003213 c->dst.addr.mem.ea =
3214 register_address(c, c->regs[VCPU_REGS_RDI]);
3215 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003216 c->dst.val = 0;
3217 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003218 case ImplicitOps:
3219 /* Special instructions do their own operand decoding. */
3220 default:
3221 c->dst.type = OP_NONE; /* Disable writeback. */
3222 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003223 }
3224
3225done:
3226 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
3227}
3228
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003229static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3230{
3231 struct decode_cache *c = &ctxt->decode;
3232
3233 /* The second termination condition only applies for REPE
3234 * and REPNE. Test if the repeat string operation prefix is
3235 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3236 * corresponding termination condition according to:
3237 * - if REPE/REPZ and ZF = 0 then done
3238 * - if REPNE/REPNZ and ZF = 1 then done
3239 */
3240 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3241 (c->b == 0xae) || (c->b == 0xaf))
3242 && (((c->rep_prefix == REPE_PREFIX) &&
3243 ((ctxt->eflags & EFLG_ZF) == 0))
3244 || ((c->rep_prefix == REPNE_PREFIX) &&
3245 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3246 return true;
3247
3248 return false;
3249}
3250
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003251int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003252x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003253{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003254 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003255 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003256 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003257 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003258 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003259 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003260
Gleb Natapov9de41572010-04-28 19:15:22 +03003261 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003262
Gleb Natapov11616242010-02-11 14:43:14 +02003263 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003264 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003265 goto done;
3266 }
3267
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003268 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003269 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003270 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003271 goto done;
3272 }
3273
Avi Kivity081bca02010-08-26 11:06:15 +03003274 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003275 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003276 goto done;
3277 }
3278
Avi Kivity12537912011-03-29 11:41:27 +02003279 if ((c->d & Sse)
3280 && ((ops->get_cr(0, ctxt->vcpu) & X86_CR0_EM)
3281 || !(ops->get_cr(4, ctxt->vcpu) & X86_CR4_OSFXSR))) {
3282 rc = emulate_ud(ctxt);
3283 goto done;
3284 }
3285
3286 if ((c->d & Sse) && (ops->get_cr(0, ctxt->vcpu) & X86_CR0_TS)) {
3287 rc = emulate_nm(ctxt);
3288 goto done;
3289 }
3290
Avi Kivityc4f035c2011-04-04 12:39:22 +02003291 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003292 rc = emulator_check_intercept(ctxt, c->intercept,
3293 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003294 if (rc != X86EMUL_CONTINUE)
3295 goto done;
3296 }
3297
Gleb Natapove92805a2010-02-10 14:21:35 +02003298 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003299 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003300 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003301 goto done;
3302 }
3303
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003304 /* Instruction can only be executed in protected mode */
3305 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3306 rc = emulate_ud(ctxt);
3307 goto done;
3308 }
3309
Joerg Roedeld09beab2011-04-04 12:39:25 +02003310 /* Do instruction specific permission checks */
3311 if (c->check_perm) {
3312 rc = c->check_perm(ctxt);
3313 if (rc != X86EMUL_CONTINUE)
3314 goto done;
3315 }
3316
Avi Kivityc4f035c2011-04-04 12:39:22 +02003317 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003318 rc = emulator_check_intercept(ctxt, c->intercept,
3319 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003320 if (rc != X86EMUL_CONTINUE)
3321 goto done;
3322 }
3323
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003324 if (c->rep_prefix && (c->d & String)) {
3325 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003326 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003327 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003328 goto done;
3329 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003330 }
3331
Wei Yongjunc483c022010-08-06 15:36:36 +08003332 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003333 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03003334 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003335 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003336 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003337 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003338 }
3339
Gleb Natapove35b7b92010-02-25 16:36:42 +02003340 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003341 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003342 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003343 if (rc != X86EMUL_CONTINUE)
3344 goto done;
3345 }
3346
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003347 if ((c->d & DstMask) == ImplicitOps)
3348 goto special_insn;
3349
3350
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003351 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3352 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003353 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003354 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003355 if (rc != X86EMUL_CONTINUE)
3356 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003357 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003358 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003359
Avi Kivity018a98d2007-11-27 19:30:56 +02003360special_insn:
3361
Avi Kivityc4f035c2011-04-04 12:39:22 +02003362 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003363 rc = emulator_check_intercept(ctxt, c->intercept,
3364 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003365 if (rc != X86EMUL_CONTINUE)
3366 goto done;
3367 }
3368
Avi Kivityef65c882010-07-29 15:11:51 +03003369 if (c->execute) {
3370 rc = c->execute(ctxt);
3371 if (rc != X86EMUL_CONTINUE)
3372 goto done;
3373 goto writeback;
3374 }
3375
Laurent Viviere4e03de2007-09-18 11:52:50 +02003376 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 goto twobyte_insn;
3378
Laurent Viviere4e03de2007-09-18 11:52:50 +02003379 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 case 0x00 ... 0x05:
3381 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003382 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003384 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003385 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003386 break;
3387 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003388 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003389 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 case 0x08 ... 0x0d:
3391 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003392 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003394 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003395 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003396 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 case 0x10 ... 0x15:
3398 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003399 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003401 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003402 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003403 break;
3404 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003405 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003406 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407 case 0x18 ... 0x1d:
3408 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003409 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003411 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003412 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003413 break;
3414 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003415 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003416 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003417 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003419 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420 break;
3421 case 0x28 ... 0x2d:
3422 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003423 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424 break;
3425 case 0x30 ... 0x35:
3426 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003427 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428 break;
3429 case 0x38 ... 0x3d:
3430 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003431 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003433 case 0x40 ... 0x47: /* inc r16/r32 */
3434 emulate_1op("inc", c->dst, ctxt->eflags);
3435 break;
3436 case 0x48 ... 0x4f: /* dec r16/r32 */
3437 emulate_1op("dec", c->dst, ctxt->eflags);
3438 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003439 case 0x58 ... 0x5f: /* pop reg */
3440 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003441 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003442 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003443 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003444 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003445 break;
3446 case 0x61: /* popa */
3447 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003448 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003450 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003452 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003454 case 0x6c: /* insb */
3455 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003456 c->src.val = c->regs[VCPU_REGS_RDX];
3457 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003458 case 0x6e: /* outsb */
3459 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003460 c->dst.val = c->regs[VCPU_REGS_RDX];
3461 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003462 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003463 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003464 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003465 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003466 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003468 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469 case 0:
3470 goto add;
3471 case 1:
3472 goto or;
3473 case 2:
3474 goto adc;
3475 case 3:
3476 goto sbb;
3477 case 4:
3478 goto and;
3479 case 5:
3480 goto sub;
3481 case 6:
3482 goto xor;
3483 case 7:
3484 goto cmp;
3485 }
3486 break;
3487 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003488 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003489 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 break;
3491 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003492 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003494 c->src.val = c->dst.val;
3495 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496 /*
3497 * Write back the memory destination with implicit LOCK
3498 * prefix.
3499 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003500 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003501 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003503 case 0x8c: /* mov r/m, sreg */
3504 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003505 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003506 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003507 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003508 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003509 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003510 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003511 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003512 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003513 case 0x8e: { /* mov seg, r/m16 */
3514 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003515
3516 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003517
Gleb Natapovc6975182010-02-18 12:15:01 +02003518 if (c->modrm_reg == VCPU_SREG_CS ||
3519 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003520 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003521 goto done;
3522 }
3523
Glauber Costa310b5d32009-05-12 16:21:06 -04003524 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003525 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003526
Gleb Natapov2e873022010-03-18 15:20:18 +02003527 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003528
3529 c->dst.type = OP_NONE; /* Disable writeback. */
3530 break;
3531 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003533 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003535 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3536 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003537 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003538 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003539 case 0x98: /* cbw/cwde/cdqe */
3540 switch (c->op_bytes) {
3541 case 2: c->dst.val = (s8)c->dst.val; break;
3542 case 4: c->dst.val = (s16)c->dst.val; break;
3543 case 8: c->dst.val = (s32)c->dst.val; break;
3544 }
3545 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003546 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003547 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003548 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003549 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003550 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003551 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003552 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003553 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003554 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003555 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003557 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003558 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003559 case 0xa8 ... 0xa9: /* test ax, imm */
3560 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003561 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003562 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003563 case 0xc0 ... 0xc1:
3564 emulate_grp2(ctxt);
3565 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003566 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003567 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003568 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003569 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003570 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003571 case 0xc4: /* les */
3572 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003573 break;
3574 case 0xc5: /* lds */
3575 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003576 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003577 case 0xcb: /* ret far */
3578 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003579 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003580 case 0xcc: /* int3 */
3581 irq = 3;
3582 goto do_interrupt;
3583 case 0xcd: /* int n */
3584 irq = c->src.val;
3585 do_interrupt:
3586 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003587 break;
3588 case 0xce: /* into */
3589 if (ctxt->eflags & EFLG_OF) {
3590 irq = 4;
3591 goto do_interrupt;
3592 }
3593 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003594 case 0xcf: /* iret */
3595 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003596 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003597 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003598 emulate_grp2(ctxt);
3599 break;
3600 case 0xd2 ... 0xd3: /* Grp2 */
3601 c->src.val = c->regs[VCPU_REGS_RCX];
3602 emulate_grp2(ctxt);
3603 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003604 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3605 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3606 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3607 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3608 jmp_rel(c, c->src.val);
3609 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003610 case 0xe3: /* jcxz/jecxz/jrcxz */
3611 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3612 jmp_rel(c, c->src.val);
3613 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003614 case 0xe4: /* inb */
3615 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003616 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003617 case 0xe6: /* outb */
3618 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003619 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003620 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003621 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003622 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003623 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003624 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003625 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003626 }
3627 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003628 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003629 case 0xea: { /* jmp far */
3630 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003631 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003632 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3633
3634 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003635 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003636
Gleb Natapov414e6272010-04-28 19:15:26 +03003637 c->eip = 0;
3638 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003639 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003640 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003641 case 0xeb:
3642 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003643 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003644 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003645 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003646 case 0xec: /* in al,dx */
3647 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003648 c->src.val = c->regs[VCPU_REGS_RDX];
3649 do_io_in:
3650 c->dst.bytes = min(c->dst.bytes, 4u);
3651 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003652 rc = emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003653 goto done;
3654 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003655 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3656 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003657 goto done; /* IO is needed */
3658 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003659 case 0xee: /* out dx,al */
3660 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003661 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003662 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003663 c->src.bytes = min(c->src.bytes, 4u);
3664 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3665 c->src.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003666 rc = emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003667 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003668 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003669 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3670 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003671 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003672 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003673 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003674 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003675 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003676 case 0xf5: /* cmc */
3677 /* complement carry flag from eflags reg */
3678 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003679 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003680 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003681 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003682 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003683 case 0xf8: /* clc */
3684 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003685 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003686 case 0xf9: /* stc */
3687 ctxt->eflags |= EFLG_CF;
3688 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003689 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003690 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003691 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003692 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003693 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003694 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003695 break;
3696 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003697 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003698 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003699 goto done;
3700 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003701 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003702 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003703 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003704 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003705 case 0xfc: /* cld */
3706 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003707 break;
3708 case 0xfd: /* std */
3709 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003710 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003711 case 0xfe: /* Grp4 */
3712 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003713 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003714 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003715 case 0xff: /* Grp5 */
3716 if (c->modrm_reg == 5)
3717 goto jump_far;
3718 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003719 default:
3720 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003722
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003723 if (rc != X86EMUL_CONTINUE)
3724 goto done;
3725
Avi Kivity018a98d2007-11-27 19:30:56 +02003726writeback:
3727 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003728 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003729 goto done;
3730
Gleb Natapov5cd21912010-03-18 15:20:26 +02003731 /*
3732 * restore dst type in case the decoding will be reused
3733 * (happens for string instruction )
3734 */
3735 c->dst.type = saved_dst_type;
3736
Gleb Natapova682e352010-03-18 15:20:21 +02003737 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003738 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003739 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003740
3741 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003742 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003743 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003744
Gleb Natapov5cd21912010-03-18 15:20:26 +02003745 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003746 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003747 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003748
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003749 if (!string_insn_completed(ctxt)) {
3750 /*
3751 * Re-enter guest when pio read ahead buffer is empty
3752 * or, if it is not used, after each 1024 iteration.
3753 */
3754 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3755 (r->end == 0 || r->end != r->pos)) {
3756 /*
3757 * Reset read cache. Usually happens before
3758 * decode, but since instruction is restarted
3759 * we have to do it here.
3760 */
3761 ctxt->decode.mem_read.end = 0;
3762 return EMULATION_RESTART;
3763 }
3764 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003765 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003766 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003767
3768 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003769
3770done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003771 if (rc == X86EMUL_PROPAGATE_FAULT)
3772 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02003773 if (rc == X86EMUL_INTERCEPTED)
3774 return EMULATION_INTERCEPTED;
3775
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003776 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777
3778twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003779 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003781 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 u16 size;
3783 unsigned long address;
3784
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003785 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003786 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003787 goto cannot_emulate;
3788
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003789 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003790 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003791 goto done;
3792
Avi Kivity33e38852008-05-21 15:34:25 +03003793 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003794 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003795 /* Disable writeback. */
3796 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003797 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003799 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003800 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003801 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802 goto done;
3803 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003804 /* Disable writeback. */
3805 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003807 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003808 if (c->modrm_mod == 3) {
3809 switch (c->modrm_rm) {
3810 case 1:
3811 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003812 break;
3813 default:
3814 goto cannot_emulate;
3815 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003816 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003817 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003818 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003819 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003820 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003821 goto done;
3822 realmode_lidt(ctxt->vcpu, size, address);
3823 }
Avi Kivity16286d02008-04-14 14:40:50 +03003824 /* Disable writeback. */
3825 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826 break;
3827 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003828 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003829 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830 break;
3831 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003832 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003833 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003834 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003836 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003837 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003838 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003839 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003840 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003841 emulate_invlpg(ctxt->vcpu,
3842 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003843 /* Disable writeback. */
3844 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 break;
3846 default:
3847 goto cannot_emulate;
3848 }
3849 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003850 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003851 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003852 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003853 case 0x06:
3854 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003855 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003856 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003857 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003858 break;
3859 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003860 case 0x0d: /* GrpP (prefetch) */
3861 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003862 break;
3863 case 0x20: /* mov cr, reg */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003864 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 case 0x21: /* mov from dr to reg */
Avi Kivityb27f3852010-08-01 14:25:22 +03003867 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003869 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003870 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003871 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003872 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03003873 goto done;
3874 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003875 c->dst.type = OP_NONE;
3876 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 case 0x23: /* mov from reg to dr */
Avi Kivityb27f3852010-08-01 14:25:22 +03003878 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003879 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3880 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3881 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003882 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003883 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03003884 goto done;
3885 }
3886
Laurent Viviera01af5e2007-09-24 11:10:56 +02003887 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003889 case 0x30:
3890 /* wrmsr */
3891 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3892 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003893 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003894 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003895 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003896 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003897 }
3898 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003899 break;
3900 case 0x32:
3901 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003902 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003903 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003904 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003905 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003906 } else {
3907 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3908 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3909 }
3910 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003911 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003912 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003913 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003914 break;
3915 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003916 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003917 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003919 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003920 if (!test_cc(c->b, ctxt->eflags))
3921 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003923 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003924 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003925 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003926 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003927 case 0x90 ... 0x9f: /* setcc r/m8 */
3928 c->dst.val = test_cc(c->b, ctxt->eflags);
3929 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003930 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003931 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003932 break;
3933 case 0xa1: /* pop fs */
3934 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003935 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003936 case 0xa3:
3937 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003938 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003939 /* only subword offset */
3940 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003941 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003942 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003943 case 0xa4: /* shld imm8, r, r/m */
3944 case 0xa5: /* shld cl, r, r/m */
3945 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3946 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003947 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003948 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003949 break;
3950 case 0xa9: /* pop gs */
3951 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003952 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003953 case 0xab:
3954 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003955 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003956 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003957 case 0xac: /* shrd imm8, r, r/m */
3958 case 0xad: /* shrd cl, r, r/m */
3959 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3960 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003961 case 0xae: /* clflush */
3962 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963 case 0xb0 ... 0xb1: /* cmpxchg */
3964 /*
3965 * Save real source value, then compare EAX against
3966 * destination.
3967 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003968 c->src.orig_val = c->src.val;
3969 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003970 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3971 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003973 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003974 } else {
3975 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003976 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003977 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 }
3979 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003980 case 0xb2: /* lss */
3981 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003982 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983 case 0xb3:
3984 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003985 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003987 case 0xb4: /* lfs */
3988 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003989 break;
3990 case 0xb5: /* lgs */
3991 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003992 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003994 c->dst.bytes = c->op_bytes;
3995 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3996 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003999 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000 case 0:
4001 goto bt;
4002 case 1:
4003 goto bts;
4004 case 2:
4005 goto btr;
4006 case 3:
4007 goto btc;
4008 }
4009 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004010 case 0xbb:
4011 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004012 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004013 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004014 case 0xbc: { /* bsf */
4015 u8 zf;
4016 __asm__ ("bsf %2, %0; setz %1"
4017 : "=r"(c->dst.val), "=q"(zf)
4018 : "r"(c->src.val));
4019 ctxt->eflags &= ~X86_EFLAGS_ZF;
4020 if (zf) {
4021 ctxt->eflags |= X86_EFLAGS_ZF;
4022 c->dst.type = OP_NONE; /* Disable writeback. */
4023 }
4024 break;
4025 }
4026 case 0xbd: { /* bsr */
4027 u8 zf;
4028 __asm__ ("bsr %2, %0; setz %1"
4029 : "=r"(c->dst.val), "=q"(zf)
4030 : "r"(c->src.val));
4031 ctxt->eflags &= ~X86_EFLAGS_ZF;
4032 if (zf) {
4033 ctxt->eflags |= X86_EFLAGS_ZF;
4034 c->dst.type = OP_NONE; /* Disable writeback. */
4035 }
4036 break;
4037 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004039 c->dst.bytes = c->op_bytes;
4040 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4041 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004043 case 0xc0 ... 0xc1: /* xadd */
4044 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4045 /* Write back the register source. */
4046 c->src.val = c->dst.orig_val;
4047 write_register_operand(&c->src);
4048 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004049 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004050 c->dst.bytes = c->op_bytes;
4051 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4052 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004053 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004055 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004056 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004057 default:
4058 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004060
4061 if (rc != X86EMUL_CONTINUE)
4062 goto done;
4063
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 goto writeback;
4065
4066cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 return -1;
4068}