blob: cc32e72fe175622963592fa8f8b8a032afb3fc8a [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 */
Joerg Roedel01de8b02011-04-04 12:39:31 +020080#define RMExt (1<<18) /* Opcode extension in ModRM r/m if mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +020082#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +020083#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030084#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030085#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030086#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020087#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020088#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030089#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010090/* Source 2 operand type */
91#define Src2None (0<<29)
92#define Src2CL (1<<29)
93#define Src2ImmByte (2<<29)
94#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030095#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010096#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
Avi Kivityd0e53322010-07-29 15:11:54 +030098#define X2(x...) x, x
99#define X3(x...) X2(x), x
100#define X4(x...) X2(x), X2(x)
101#define X5(x...) X4(x), x
102#define X6(x...) X4(x), X2(x)
103#define X7(x...) X4(x), X3(x)
104#define X8(x...) X4(x), X4(x)
105#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300106
Avi Kivityd65b1de2010-07-29 15:11:35 +0300107struct opcode {
108 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200109 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300110 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300111 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300112 struct opcode *group;
113 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200114 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300115 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200116 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300117};
118
119struct group_dual {
120 struct opcode mod012[8];
121 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300122};
123
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200124struct gprefix {
125 struct opcode pfx_no;
126 struct opcode pfx_66;
127 struct opcode pfx_f2;
128 struct opcode pfx_f3;
129};
130
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200132#define EFLG_ID (1<<21)
133#define EFLG_VIP (1<<20)
134#define EFLG_VIF (1<<19)
135#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200136#define EFLG_VM (1<<17)
137#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200138#define EFLG_IOPL (3<<12)
139#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140#define EFLG_OF (1<<11)
141#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200142#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200143#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144#define EFLG_SF (1<<7)
145#define EFLG_ZF (1<<6)
146#define EFLG_AF (1<<4)
147#define EFLG_PF (1<<2)
148#define EFLG_CF (1<<0)
149
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300150#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
151#define EFLG_RESERVED_ONE_MASK 2
152
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153/*
154 * Instruction emulation:
155 * Most instructions are emulated directly via a fragment of inline assembly
156 * code. This allows us to save/restore EFLAGS and thus very easily pick up
157 * any modified flags.
158 */
159
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800160#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161#define _LO32 "k" /* force 32-bit operand */
162#define _STK "%%rsp" /* stack pointer */
163#elif defined(__i386__)
164#define _LO32 "" /* force 32-bit operand */
165#define _STK "%%esp" /* stack pointer */
166#endif
167
168/*
169 * These EFLAGS bits are restored from saved value during emulation, and
170 * any changes are written back to the saved value after emulation.
171 */
172#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
173
174/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200175#define _PRE_EFLAGS(_sav, _msk, _tmp) \
176 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
177 "movl %"_sav",%"_LO32 _tmp"; " \
178 "push %"_tmp"; " \
179 "push %"_tmp"; " \
180 "movl %"_msk",%"_LO32 _tmp"; " \
181 "andl %"_LO32 _tmp",("_STK"); " \
182 "pushf; " \
183 "notl %"_LO32 _tmp"; " \
184 "andl %"_LO32 _tmp",("_STK"); " \
185 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
186 "pop %"_tmp"; " \
187 "orl %"_LO32 _tmp",("_STK"); " \
188 "popf; " \
189 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190
191/* After executing instruction: write-back necessary bits in EFLAGS. */
192#define _POST_EFLAGS(_sav, _msk, _tmp) \
193 /* _sav |= EFLAGS & _msk; */ \
194 "pushf; " \
195 "pop %"_tmp"; " \
196 "andl %"_msk",%"_LO32 _tmp"; " \
197 "orl %"_LO32 _tmp",%"_sav"; "
198
Avi Kivitydda96d82008-11-26 15:14:10 +0200199#ifdef CONFIG_X86_64
200#define ON64(x) x
201#else
202#define ON64(x)
203#endif
204
Avi Kivityb3b3d252010-08-16 17:49:52 +0300205#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200206 do { \
207 __asm__ __volatile__ ( \
208 _PRE_EFLAGS("0", "4", "2") \
209 _op _suffix " %"_x"3,%1; " \
210 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300211 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200212 "=&r" (_tmp) \
213 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200214 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200215
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217/* Raw emulation: instruction has two explicit operands. */
218#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200219 do { \
220 unsigned long _tmp; \
221 \
222 switch ((_dst).bytes) { \
223 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300224 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200225 break; \
226 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300227 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200228 break; \
229 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300230 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200231 break; \
232 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 } while (0)
234
235#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
236 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200237 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400238 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300240 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241 break; \
242 default: \
243 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
244 _wx, _wy, _lx, _ly, _qx, _qy); \
245 break; \
246 } \
247 } while (0)
248
249/* Source operand is byte-sized and may be restricted to just %cl. */
250#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
251 __emulate_2op(_op, _src, _dst, _eflags, \
252 "b", "c", "b", "c", "b", "c", "b", "c")
253
254/* Source operand is byte, word, long or quad sized. */
255#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
256 __emulate_2op(_op, _src, _dst, _eflags, \
257 "b", "q", "w", "r", _LO32, "r", "", "r")
258
259/* Source operand is word, long or quad sized. */
260#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
261 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
262 "w", "r", _LO32, "r", "", "r")
263
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100264/* Instruction has three operands and one operand is stored in ECX register */
265#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
266 do { \
267 unsigned long _tmp; \
268 _type _clv = (_cl).val; \
269 _type _srcv = (_src).val; \
270 _type _dstv = (_dst).val; \
271 \
272 __asm__ __volatile__ ( \
273 _PRE_EFLAGS("0", "5", "2") \
274 _op _suffix " %4,%1 \n" \
275 _POST_EFLAGS("0", "5", "2") \
276 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
277 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
278 ); \
279 \
280 (_cl).val = (unsigned long) _clv; \
281 (_src).val = (unsigned long) _srcv; \
282 (_dst).val = (unsigned long) _dstv; \
283 } while (0)
284
285#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
286 do { \
287 switch ((_dst).bytes) { \
288 case 2: \
289 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
290 "w", unsigned short); \
291 break; \
292 case 4: \
293 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
294 "l", unsigned int); \
295 break; \
296 case 8: \
297 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
298 "q", unsigned long)); \
299 break; \
300 } \
301 } while (0)
302
Avi Kivitydda96d82008-11-26 15:14:10 +0200303#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 do { \
305 unsigned long _tmp; \
306 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200307 __asm__ __volatile__ ( \
308 _PRE_EFLAGS("0", "3", "2") \
309 _op _suffix " %1; " \
310 _POST_EFLAGS("0", "3", "2") \
311 : "=m" (_eflags), "+m" ((_dst).val), \
312 "=&r" (_tmp) \
313 : "i" (EFLAGS_MASK)); \
314 } while (0)
315
316/* Instruction has only one explicit operand (no source operand). */
317#define emulate_1op(_op, _dst, _eflags) \
318 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400319 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200320 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
321 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
322 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
323 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324 } \
325 } while (0)
326
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300327#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
328 do { \
329 unsigned long _tmp; \
330 \
331 __asm__ __volatile__ ( \
332 _PRE_EFLAGS("0", "4", "1") \
333 _op _suffix " %5; " \
334 _POST_EFLAGS("0", "4", "1") \
335 : "=m" (_eflags), "=&r" (_tmp), \
336 "+a" (_rax), "+d" (_rdx) \
337 : "i" (EFLAGS_MASK), "m" ((_src).val), \
338 "a" (_rax), "d" (_rdx)); \
339 } while (0)
340
Avi Kivityf6b35972010-08-26 11:59:00 +0300341#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
342 do { \
343 unsigned long _tmp; \
344 \
345 __asm__ __volatile__ ( \
346 _PRE_EFLAGS("0", "5", "1") \
347 "1: \n\t" \
348 _op _suffix " %6; " \
349 "2: \n\t" \
350 _POST_EFLAGS("0", "5", "1") \
351 ".pushsection .fixup,\"ax\" \n\t" \
352 "3: movb $1, %4 \n\t" \
353 "jmp 2b \n\t" \
354 ".popsection \n\t" \
355 _ASM_EXTABLE(1b, 3b) \
356 : "=m" (_eflags), "=&r" (_tmp), \
357 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
358 : "i" (EFLAGS_MASK), "m" ((_src).val), \
359 "a" (_rax), "d" (_rdx)); \
360 } while (0)
361
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300362/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
363#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
364 do { \
365 switch((_src).bytes) { \
366 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
367 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
368 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
369 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
370 } \
371 } while (0)
372
Avi Kivityf6b35972010-08-26 11:59:00 +0300373#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
374 do { \
375 switch((_src).bytes) { \
376 case 1: \
377 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
378 _eflags, "b", _ex); \
379 break; \
380 case 2: \
381 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
382 _eflags, "w", _ex); \
383 break; \
384 case 4: \
385 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
386 _eflags, "l", _ex); \
387 break; \
388 case 8: ON64( \
389 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
390 _eflags, "q", _ex)); \
391 break; \
392 } \
393 } while (0)
394
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395/* Fetch next part of the instruction being emulated. */
396#define insn_fetch(_type, _size, _eip) \
397({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200398 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200399 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 goto done; \
401 (_eip) += (_size); \
402 (_type)_x; \
403})
404
Gleb Natapov414e6272010-04-28 19:15:26 +0300405#define insn_fetch_arr(_arr, _size, _eip) \
406({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
407 if (rc != X86EMUL_CONTINUE) \
408 goto done; \
409 (_eip) += (_size); \
410})
411
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200412static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
413 enum x86_intercept intercept,
414 enum x86_intercept_stage stage)
415{
416 struct x86_instruction_info info = {
417 .intercept = intercept,
418 .rep_prefix = ctxt->decode.rep_prefix,
419 .modrm_mod = ctxt->decode.modrm_mod,
420 .modrm_reg = ctxt->decode.modrm_reg,
421 .modrm_rm = ctxt->decode.modrm_rm,
422 .src_val = ctxt->decode.src.val64,
423 .src_bytes = ctxt->decode.src.bytes,
424 .dst_bytes = ctxt->decode.dst.bytes,
425 .ad_bytes = ctxt->decode.ad_bytes,
426 .next_rip = ctxt->eip,
427 };
428
429 return ctxt->ops->intercept(ctxt->vcpu, &info, stage);
430}
431
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800432static inline unsigned long ad_mask(struct decode_cache *c)
433{
434 return (1UL << (c->ad_bytes << 3)) - 1;
435}
436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800438static inline unsigned long
439address_mask(struct decode_cache *c, unsigned long reg)
440{
441 if (c->ad_bytes == sizeof(unsigned long))
442 return reg;
443 else
444 return reg & ad_mask(c);
445}
446
447static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200448register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800449{
Avi Kivity90de84f2010-11-17 15:28:21 +0200450 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800451}
452
Harvey Harrison7a9572752008-02-19 07:40:41 -0800453static inline void
454register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
455{
456 if (c->ad_bytes == sizeof(unsigned long))
457 *reg += inc;
458 else
459 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
460}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461
Harvey Harrison7a9572752008-02-19 07:40:41 -0800462static inline void jmp_rel(struct decode_cache *c, int rel)
463{
464 register_address_increment(c, &c->eip, rel);
465}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300466
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300467static void set_seg_override(struct decode_cache *c, int seg)
468{
469 c->has_seg_override = true;
470 c->seg_override = seg;
471}
472
Gleb Natapov79168fd2010-04-28 19:15:30 +0300473static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
474 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300475{
476 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
477 return 0;
478
Gleb Natapov79168fd2010-04-28 19:15:30 +0300479 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300480}
481
Avi Kivity90de84f2010-11-17 15:28:21 +0200482static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
483 struct x86_emulate_ops *ops,
484 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300485{
486 if (!c->has_seg_override)
487 return 0;
488
Avi Kivity90de84f2010-11-17 15:28:21 +0200489 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300490}
491
Avi Kivity90de84f2010-11-17 15:28:21 +0200492static ulong linear(struct x86_emulate_ctxt *ctxt,
493 struct segmented_address addr)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300494{
Avi Kivity90de84f2010-11-17 15:28:21 +0200495 struct decode_cache *c = &ctxt->decode;
496 ulong la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300497
Avi Kivity90de84f2010-11-17 15:28:21 +0200498 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
499 if (c->ad_bytes != 8)
500 la &= (u32)-1;
501 return la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300502}
503
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200504static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
505 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300506{
Avi Kivityda9cb572010-11-22 17:53:21 +0200507 ctxt->exception.vector = vec;
508 ctxt->exception.error_code = error;
509 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200510 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300511}
512
Joerg Roedel3b88e412011-04-04 12:39:29 +0200513static int emulate_db(struct x86_emulate_ctxt *ctxt)
514{
515 return emulate_exception(ctxt, DB_VECTOR, 0, false);
516}
517
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200518static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300519{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200520 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300521}
522
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200523static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300524{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300526}
527
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200528static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300529{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200530 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300531}
532
Avi Kivity34d1f492010-08-26 11:59:01 +0300533static int emulate_de(struct x86_emulate_ctxt *ctxt)
534{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200535 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300536}
537
Avi Kivity12537912011-03-29 11:41:27 +0200538static int emulate_nm(struct x86_emulate_ctxt *ctxt)
539{
540 return emulate_exception(ctxt, NM_VECTOR, 0, false);
541}
542
Avi Kivity62266862007-11-20 13:15:52 +0200543static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
544 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300545 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200546{
547 struct fetch_cache *fc = &ctxt->decode.fetch;
548 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300549 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200550
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300551 if (eip == fc->end) {
552 cur_size = fc->end - fc->start;
553 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
554 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200555 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900556 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200557 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300558 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200559 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300560 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900561 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200562}
563
564static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
565 struct x86_emulate_ops *ops,
566 unsigned long eip, void *dest, unsigned size)
567{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900568 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200569
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200570 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200571 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200572 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200573 while (size--) {
574 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900575 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200576 return rc;
577 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900578 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200579}
580
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000581/*
582 * Given the 'reg' portion of a ModRM byte, and a register block, return a
583 * pointer into the block that addresses the relevant register.
584 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
585 */
586static void *decode_register(u8 modrm_reg, unsigned long *regs,
587 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588{
589 void *p;
590
591 p = &regs[modrm_reg];
592 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
593 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
594 return p;
595}
596
597static int read_descriptor(struct x86_emulate_ctxt *ctxt,
598 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200599 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 u16 *size, unsigned long *address, int op_bytes)
601{
602 int rc;
603
604 if (op_bytes == 2)
605 op_bytes = 3;
606 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200607 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200608 ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900609 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200611 addr.ea += 2;
612 rc = ops->read_std(linear(ctxt, addr), address, op_bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200613 ctxt->vcpu, &ctxt->exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614 return rc;
615}
616
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300617static int test_cc(unsigned int condition, unsigned int flags)
618{
619 int rc = 0;
620
621 switch ((condition & 15) >> 1) {
622 case 0: /* o */
623 rc |= (flags & EFLG_OF);
624 break;
625 case 1: /* b/c/nae */
626 rc |= (flags & EFLG_CF);
627 break;
628 case 2: /* z/e */
629 rc |= (flags & EFLG_ZF);
630 break;
631 case 3: /* be/na */
632 rc |= (flags & (EFLG_CF|EFLG_ZF));
633 break;
634 case 4: /* s */
635 rc |= (flags & EFLG_SF);
636 break;
637 case 5: /* p/pe */
638 rc |= (flags & EFLG_PF);
639 break;
640 case 7: /* le/ng */
641 rc |= (flags & EFLG_ZF);
642 /* fall through */
643 case 6: /* l/nge */
644 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
645 break;
646 }
647
648 /* Odd condition identifiers (lsb == 1) have inverted sense. */
649 return (!!rc ^ (condition & 1));
650}
651
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300652static void fetch_register_operand(struct operand *op)
653{
654 switch (op->bytes) {
655 case 1:
656 op->val = *(u8 *)op->addr.reg;
657 break;
658 case 2:
659 op->val = *(u16 *)op->addr.reg;
660 break;
661 case 4:
662 op->val = *(u32 *)op->addr.reg;
663 break;
664 case 8:
665 op->val = *(u64 *)op->addr.reg;
666 break;
667 }
668}
669
Avi Kivity12537912011-03-29 11:41:27 +0200670static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
671{
672 ctxt->ops->get_fpu(ctxt);
673 switch (reg) {
674 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
675 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
676 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
677 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
678 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
679 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
680 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
681 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
682#ifdef CONFIG_X86_64
683 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
684 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
685 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
686 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
687 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
688 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
689 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
690 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
691#endif
692 default: BUG();
693 }
694 ctxt->ops->put_fpu(ctxt);
695}
696
697static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
698 int reg)
699{
700 ctxt->ops->get_fpu(ctxt);
701 switch (reg) {
702 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
703 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
704 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
705 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
706 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
707 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
708 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
709 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
710#ifdef CONFIG_X86_64
711 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
712 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
713 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
714 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
715 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
716 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
717 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
718 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
719#endif
720 default: BUG();
721 }
722 ctxt->ops->put_fpu(ctxt);
723}
724
725static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
726 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200727 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200728 int inhibit_bytereg)
729{
Avi Kivity33615aa2007-10-31 11:15:56 +0200730 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200731 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200732
733 if (!(c->d & ModRM))
734 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200735
736 if (c->d & Sse) {
737 op->type = OP_XMM;
738 op->bytes = 16;
739 op->addr.xmm = reg;
740 read_sse_reg(ctxt, &op->vec_val, reg);
741 return;
742 }
743
Avi Kivity3c118e22007-10-31 10:27:04 +0200744 op->type = OP_REG;
745 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300746 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200747 op->bytes = 1;
748 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300749 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200750 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200751 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300752 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200753 op->orig_val = op->val;
754}
755
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200756static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300757 struct x86_emulate_ops *ops,
758 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200759{
760 struct decode_cache *c = &ctxt->decode;
761 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700762 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900763 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300764 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200765
766 if (c->rex_prefix) {
767 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
768 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
769 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
770 }
771
772 c->modrm = insn_fetch(u8, 1, c->eip);
773 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
774 c->modrm_reg |= (c->modrm & 0x38) >> 3;
775 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300776 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200777
778 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300779 op->type = OP_REG;
780 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
781 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300782 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200783 if (c->d & Sse) {
784 op->type = OP_XMM;
785 op->bytes = 16;
786 op->addr.xmm = c->modrm_rm;
787 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
788 return rc;
789 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300790 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200791 return rc;
792 }
793
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300794 op->type = OP_MEM;
795
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200796 if (c->ad_bytes == 2) {
797 unsigned bx = c->regs[VCPU_REGS_RBX];
798 unsigned bp = c->regs[VCPU_REGS_RBP];
799 unsigned si = c->regs[VCPU_REGS_RSI];
800 unsigned di = c->regs[VCPU_REGS_RDI];
801
802 /* 16-bit ModR/M decode. */
803 switch (c->modrm_mod) {
804 case 0:
805 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300806 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200807 break;
808 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300809 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200810 break;
811 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300812 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200813 break;
814 }
815 switch (c->modrm_rm) {
816 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300817 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200818 break;
819 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300820 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200821 break;
822 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300823 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200824 break;
825 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300826 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200827 break;
828 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300829 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200830 break;
831 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300832 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200833 break;
834 case 6:
835 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300836 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200837 break;
838 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300839 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200840 break;
841 }
842 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
843 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300844 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300845 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200846 } else {
847 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700848 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200849 sib = insn_fetch(u8, 1, c->eip);
850 index_reg |= (sib >> 3) & 7;
851 base_reg |= sib & 7;
852 scale = sib >> 6;
853
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700854 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300855 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700856 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300857 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700858 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300859 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700860 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
861 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700862 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700863 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300864 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200865 switch (c->modrm_mod) {
866 case 0:
867 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300868 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200869 break;
870 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300871 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200872 break;
873 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300874 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200875 break;
876 }
877 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200878 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200879done:
880 return rc;
881}
882
883static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300884 struct x86_emulate_ops *ops,
885 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200886{
887 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900888 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200889
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300890 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200891 switch (c->ad_bytes) {
892 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200893 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894 break;
895 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200896 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200897 break;
898 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200899 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200900 break;
901 }
902done:
903 return rc;
904}
905
Wei Yongjun35c843c2010-08-09 11:34:56 +0800906static void fetch_bit_operand(struct decode_cache *c)
907{
Sheng Yang7129eec2010-09-28 16:33:32 +0800908 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800909
Wei Yongjun3885f182010-08-09 11:37:37 +0800910 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800911 mask = ~(c->dst.bytes * 8 - 1);
912
913 if (c->src.bytes == 2)
914 sv = (s16)c->src.val & (s16)mask;
915 else if (c->src.bytes == 4)
916 sv = (s32)c->src.val & (s32)mask;
917
Avi Kivity90de84f2010-11-17 15:28:21 +0200918 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800919 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800920
921 /* only subword offset */
922 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800923}
924
Gleb Natapov9de41572010-04-28 19:15:22 +0300925static int read_emulated(struct x86_emulate_ctxt *ctxt,
926 struct x86_emulate_ops *ops,
927 unsigned long addr, void *dest, unsigned size)
928{
929 int rc;
930 struct read_cache *mc = &ctxt->decode.mem_read;
931
932 while (size) {
933 int n = min(size, 8u);
934 size -= n;
935 if (mc->pos < mc->end)
936 goto read_cached;
937
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200938 rc = ops->read_emulated(addr, mc->data + mc->end, n,
939 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +0300940 if (rc != X86EMUL_CONTINUE)
941 return rc;
942 mc->end += n;
943
944 read_cached:
945 memcpy(dest, mc->data + mc->pos, n);
946 mc->pos += n;
947 dest += n;
948 addr += n;
949 }
950 return X86EMUL_CONTINUE;
951}
952
Gleb Natapov7b262e92010-03-18 15:20:27 +0200953static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
954 struct x86_emulate_ops *ops,
955 unsigned int size, unsigned short port,
956 void *dest)
957{
958 struct read_cache *rc = &ctxt->decode.io_read;
959
960 if (rc->pos == rc->end) { /* refill pio read ahead */
961 struct decode_cache *c = &ctxt->decode;
962 unsigned int in_page, n;
963 unsigned int count = c->rep_prefix ?
964 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
965 in_page = (ctxt->eflags & EFLG_DF) ?
966 offset_in_page(c->regs[VCPU_REGS_RDI]) :
967 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
968 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
969 count);
970 if (n == 0)
971 n = 1;
972 rc->pos = rc->end = 0;
973 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
974 return 0;
975 rc->end = n * size;
976 }
977
978 memcpy(dest, rc->data + rc->pos, size);
979 rc->pos += size;
980 return 1;
981}
982
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200983static u32 desc_limit_scaled(struct desc_struct *desc)
984{
985 u32 limit = get_desc_limit(desc);
986
987 return desc->g ? (limit << 12) | 0xfff : limit;
988}
989
990static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
991 struct x86_emulate_ops *ops,
992 u16 selector, struct desc_ptr *dt)
993{
994 if (selector & 1 << 2) {
995 struct desc_struct desc;
996 memset (dt, 0, sizeof *dt);
Gleb Natapov5601d052011-03-07 14:55:06 +0200997 if (!ops->get_cached_descriptor(&desc, NULL, VCPU_SREG_LDTR,
998 ctxt->vcpu))
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200999 return;
1000
1001 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1002 dt->address = get_desc_base(&desc);
1003 } else
1004 ops->get_gdt(dt, ctxt->vcpu);
1005}
1006
1007/* allowed just for 8 bytes segments */
1008static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1009 struct x86_emulate_ops *ops,
1010 u16 selector, struct desc_struct *desc)
1011{
1012 struct desc_ptr dt;
1013 u16 index = selector >> 3;
1014 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001015 ulong addr;
1016
1017 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1018
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001019 if (dt.size < index * 8 + 7)
1020 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001021 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001022 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
1023 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001024
1025 return ret;
1026}
1027
1028/* allowed just for 8 bytes segments */
1029static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1030 struct x86_emulate_ops *ops,
1031 u16 selector, struct desc_struct *desc)
1032{
1033 struct desc_ptr dt;
1034 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001035 ulong addr;
1036 int ret;
1037
1038 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1039
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001040 if (dt.size < index * 8 + 7)
1041 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001042
1043 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001044 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
1045 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001046
1047 return ret;
1048}
1049
Gleb Natapov5601d052011-03-07 14:55:06 +02001050/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001051static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1052 struct x86_emulate_ops *ops,
1053 u16 selector, int seg)
1054{
1055 struct desc_struct seg_desc;
1056 u8 dpl, rpl, cpl;
1057 unsigned err_vec = GP_VECTOR;
1058 u32 err_code = 0;
1059 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1060 int ret;
1061
1062 memset(&seg_desc, 0, sizeof seg_desc);
1063
1064 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1065 || ctxt->mode == X86EMUL_MODE_REAL) {
1066 /* set real mode segment descriptor */
1067 set_desc_base(&seg_desc, selector << 4);
1068 set_desc_limit(&seg_desc, 0xffff);
1069 seg_desc.type = 3;
1070 seg_desc.p = 1;
1071 seg_desc.s = 1;
1072 goto load;
1073 }
1074
1075 /* NULL selector is not valid for TR, CS and SS */
1076 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1077 && null_selector)
1078 goto exception;
1079
1080 /* TR should be in GDT only */
1081 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1082 goto exception;
1083
1084 if (null_selector) /* for NULL selector skip all following checks */
1085 goto load;
1086
1087 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1088 if (ret != X86EMUL_CONTINUE)
1089 return ret;
1090
1091 err_code = selector & 0xfffc;
1092 err_vec = GP_VECTOR;
1093
1094 /* can't load system descriptor into segment selecor */
1095 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1096 goto exception;
1097
1098 if (!seg_desc.p) {
1099 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1100 goto exception;
1101 }
1102
1103 rpl = selector & 3;
1104 dpl = seg_desc.dpl;
1105 cpl = ops->cpl(ctxt->vcpu);
1106
1107 switch (seg) {
1108 case VCPU_SREG_SS:
1109 /*
1110 * segment is not a writable data segment or segment
1111 * selector's RPL != CPL or segment selector's RPL != CPL
1112 */
1113 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1114 goto exception;
1115 break;
1116 case VCPU_SREG_CS:
1117 if (!(seg_desc.type & 8))
1118 goto exception;
1119
1120 if (seg_desc.type & 4) {
1121 /* conforming */
1122 if (dpl > cpl)
1123 goto exception;
1124 } else {
1125 /* nonconforming */
1126 if (rpl > cpl || dpl != cpl)
1127 goto exception;
1128 }
1129 /* CS(RPL) <- CPL */
1130 selector = (selector & 0xfffc) | cpl;
1131 break;
1132 case VCPU_SREG_TR:
1133 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1134 goto exception;
1135 break;
1136 case VCPU_SREG_LDTR:
1137 if (seg_desc.s || seg_desc.type != 2)
1138 goto exception;
1139 break;
1140 default: /* DS, ES, FS, or GS */
1141 /*
1142 * segment is not a data or readable code segment or
1143 * ((segment is a data or nonconforming code segment)
1144 * and (both RPL and CPL > DPL))
1145 */
1146 if ((seg_desc.type & 0xa) == 0x8 ||
1147 (((seg_desc.type & 0xc) != 0xc) &&
1148 (rpl > dpl && cpl > dpl)))
1149 goto exception;
1150 break;
1151 }
1152
1153 if (seg_desc.s) {
1154 /* mark segment as accessed */
1155 seg_desc.type |= 1;
1156 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1157 if (ret != X86EMUL_CONTINUE)
1158 return ret;
1159 }
1160load:
1161 ops->set_segment_selector(selector, seg, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001162 ops->set_cached_descriptor(&seg_desc, 0, seg, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001163 return X86EMUL_CONTINUE;
1164exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001165 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001166 return X86EMUL_PROPAGATE_FAULT;
1167}
1168
Wei Yongjun31be40b2010-08-17 09:17:30 +08001169static void write_register_operand(struct operand *op)
1170{
1171 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1172 switch (op->bytes) {
1173 case 1:
1174 *(u8 *)op->addr.reg = (u8)op->val;
1175 break;
1176 case 2:
1177 *(u16 *)op->addr.reg = (u16)op->val;
1178 break;
1179 case 4:
1180 *op->addr.reg = (u32)op->val;
1181 break; /* 64b: zero-extend */
1182 case 8:
1183 *op->addr.reg = op->val;
1184 break;
1185 }
1186}
1187
Wei Yongjunc37eda12010-06-15 09:03:33 +08001188static inline int writeback(struct x86_emulate_ctxt *ctxt,
1189 struct x86_emulate_ops *ops)
1190{
1191 int rc;
1192 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001193
1194 switch (c->dst.type) {
1195 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001196 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001197 break;
1198 case OP_MEM:
1199 if (c->lock_prefix)
1200 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001201 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001202 &c->dst.orig_val,
1203 &c->dst.val,
1204 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001205 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001206 ctxt->vcpu);
1207 else
1208 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001209 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001210 &c->dst.val,
1211 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001212 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001213 ctxt->vcpu);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001214 if (rc != X86EMUL_CONTINUE)
1215 return rc;
1216 break;
Avi Kivity12537912011-03-29 11:41:27 +02001217 case OP_XMM:
1218 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1219 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001220 case OP_NONE:
1221 /* no writeback */
1222 break;
1223 default:
1224 break;
1225 }
1226 return X86EMUL_CONTINUE;
1227}
1228
Gleb Natapov79168fd2010-04-28 19:15:30 +03001229static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1230 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001231{
1232 struct decode_cache *c = &ctxt->decode;
1233
1234 c->dst.type = OP_MEM;
1235 c->dst.bytes = c->op_bytes;
1236 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001237 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001238 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1239 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001240}
1241
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001242static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001243 struct x86_emulate_ops *ops,
1244 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001245{
1246 struct decode_cache *c = &ctxt->decode;
1247 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001248 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001249
Avi Kivity90de84f2010-11-17 15:28:21 +02001250 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1251 addr.seg = VCPU_SREG_SS;
1252 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001253 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001254 return rc;
1255
Avi Kivity350f69d2009-01-05 11:12:40 +02001256 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001257 return rc;
1258}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001259
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001260static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1261 struct x86_emulate_ops *ops,
1262 void *dest, int len)
1263{
1264 int rc;
1265 unsigned long val, change_mask;
1266 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001267 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001268
1269 rc = emulate_pop(ctxt, ops, &val, len);
1270 if (rc != X86EMUL_CONTINUE)
1271 return rc;
1272
1273 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1274 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1275
1276 switch(ctxt->mode) {
1277 case X86EMUL_MODE_PROT64:
1278 case X86EMUL_MODE_PROT32:
1279 case X86EMUL_MODE_PROT16:
1280 if (cpl == 0)
1281 change_mask |= EFLG_IOPL;
1282 if (cpl <= iopl)
1283 change_mask |= EFLG_IF;
1284 break;
1285 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001286 if (iopl < 3)
1287 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001288 change_mask |= EFLG_IF;
1289 break;
1290 default: /* real mode */
1291 change_mask |= (EFLG_IOPL | EFLG_IF);
1292 break;
1293 }
1294
1295 *(unsigned long *)dest =
1296 (ctxt->eflags & ~change_mask) | (val & change_mask);
1297
1298 return rc;
1299}
1300
Gleb Natapov79168fd2010-04-28 19:15:30 +03001301static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1302 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001303{
1304 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001305
Gleb Natapov79168fd2010-04-28 19:15:30 +03001306 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001307
Gleb Natapov79168fd2010-04-28 19:15:30 +03001308 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001309}
1310
1311static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1312 struct x86_emulate_ops *ops, int seg)
1313{
1314 struct decode_cache *c = &ctxt->decode;
1315 unsigned long selector;
1316 int rc;
1317
1318 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001319 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001320 return rc;
1321
Gleb Natapov2e873022010-03-18 15:20:18 +02001322 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001323 return rc;
1324}
1325
Wei Yongjunc37eda12010-06-15 09:03:33 +08001326static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001327 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001328{
1329 struct decode_cache *c = &ctxt->decode;
1330 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001331 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001332 int reg = VCPU_REGS_RAX;
1333
1334 while (reg <= VCPU_REGS_RDI) {
1335 (reg == VCPU_REGS_RSP) ?
1336 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1337
Gleb Natapov79168fd2010-04-28 19:15:30 +03001338 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001339
1340 rc = writeback(ctxt, ops);
1341 if (rc != X86EMUL_CONTINUE)
1342 return rc;
1343
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001344 ++reg;
1345 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001346
1347 /* Disable writeback. */
1348 c->dst.type = OP_NONE;
1349
1350 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001351}
1352
1353static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1354 struct x86_emulate_ops *ops)
1355{
1356 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001357 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001358 int reg = VCPU_REGS_RDI;
1359
1360 while (reg >= VCPU_REGS_RAX) {
1361 if (reg == VCPU_REGS_RSP) {
1362 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1363 c->op_bytes);
1364 --reg;
1365 }
1366
1367 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001368 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001369 break;
1370 --reg;
1371 }
1372 return rc;
1373}
1374
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001375int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1376 struct x86_emulate_ops *ops, int irq)
1377{
1378 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001379 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001380 struct desc_ptr dt;
1381 gva_t cs_addr;
1382 gva_t eip_addr;
1383 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001384
1385 /* TODO: Add limit checks */
1386 c->src.val = ctxt->eflags;
1387 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001388 rc = writeback(ctxt, ops);
1389 if (rc != X86EMUL_CONTINUE)
1390 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001391
1392 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1393
1394 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1395 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001396 rc = writeback(ctxt, ops);
1397 if (rc != X86EMUL_CONTINUE)
1398 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001399
1400 c->src.val = c->eip;
1401 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001402 rc = writeback(ctxt, ops);
1403 if (rc != X86EMUL_CONTINUE)
1404 return rc;
1405
1406 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001407
1408 ops->get_idt(&dt, ctxt->vcpu);
1409
1410 eip_addr = dt.address + (irq << 2);
1411 cs_addr = dt.address + (irq << 2) + 2;
1412
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001413 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001414 if (rc != X86EMUL_CONTINUE)
1415 return rc;
1416
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001417 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001418 if (rc != X86EMUL_CONTINUE)
1419 return rc;
1420
1421 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1422 if (rc != X86EMUL_CONTINUE)
1423 return rc;
1424
1425 c->eip = eip;
1426
1427 return rc;
1428}
1429
1430static int emulate_int(struct x86_emulate_ctxt *ctxt,
1431 struct x86_emulate_ops *ops, int irq)
1432{
1433 switch(ctxt->mode) {
1434 case X86EMUL_MODE_REAL:
1435 return emulate_int_real(ctxt, ops, irq);
1436 case X86EMUL_MODE_VM86:
1437 case X86EMUL_MODE_PROT16:
1438 case X86EMUL_MODE_PROT32:
1439 case X86EMUL_MODE_PROT64:
1440 default:
1441 /* Protected mode interrupts unimplemented yet */
1442 return X86EMUL_UNHANDLEABLE;
1443 }
1444}
1445
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001446static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1447 struct x86_emulate_ops *ops)
1448{
1449 struct decode_cache *c = &ctxt->decode;
1450 int rc = X86EMUL_CONTINUE;
1451 unsigned long temp_eip = 0;
1452 unsigned long temp_eflags = 0;
1453 unsigned long cs = 0;
1454 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1455 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1456 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1457 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1458
1459 /* TODO: Add stack limit check */
1460
1461 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1462
1463 if (rc != X86EMUL_CONTINUE)
1464 return rc;
1465
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001466 if (temp_eip & ~0xffff)
1467 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001468
1469 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1470
1471 if (rc != X86EMUL_CONTINUE)
1472 return rc;
1473
1474 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1475
1476 if (rc != X86EMUL_CONTINUE)
1477 return rc;
1478
1479 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1480
1481 if (rc != X86EMUL_CONTINUE)
1482 return rc;
1483
1484 c->eip = temp_eip;
1485
1486
1487 if (c->op_bytes == 4)
1488 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1489 else if (c->op_bytes == 2) {
1490 ctxt->eflags &= ~0xffff;
1491 ctxt->eflags |= temp_eflags;
1492 }
1493
1494 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1495 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1496
1497 return rc;
1498}
1499
1500static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1501 struct x86_emulate_ops* ops)
1502{
1503 switch(ctxt->mode) {
1504 case X86EMUL_MODE_REAL:
1505 return emulate_iret_real(ctxt, ops);
1506 case X86EMUL_MODE_VM86:
1507 case X86EMUL_MODE_PROT16:
1508 case X86EMUL_MODE_PROT32:
1509 case X86EMUL_MODE_PROT64:
1510 default:
1511 /* iret from protected mode unimplemented yet */
1512 return X86EMUL_UNHANDLEABLE;
1513 }
1514}
1515
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001516static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1517 struct x86_emulate_ops *ops)
1518{
1519 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001520
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001521 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001522}
1523
Laurent Vivier05f086f2007-09-24 11:10:55 +02001524static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001525{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001526 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001527 switch (c->modrm_reg) {
1528 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001529 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001530 break;
1531 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001532 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001533 break;
1534 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001535 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001536 break;
1537 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001538 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001539 break;
1540 case 4: /* sal/shl */
1541 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001542 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001543 break;
1544 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001545 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001546 break;
1547 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001548 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001549 break;
1550 }
1551}
1552
1553static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001554 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001555{
1556 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001557 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1558 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001559 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001560
1561 switch (c->modrm_reg) {
1562 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001563 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001564 break;
1565 case 2: /* not */
1566 c->dst.val = ~c->dst.val;
1567 break;
1568 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001569 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001570 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001571 case 4: /* mul */
1572 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1573 break;
1574 case 5: /* imul */
1575 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1576 break;
1577 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001578 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1579 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001580 break;
1581 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001582 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1583 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001584 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001585 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001586 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001587 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001588 if (de)
1589 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001590 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001591}
1592
1593static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001594 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001595{
1596 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001597
1598 switch (c->modrm_reg) {
1599 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001600 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001601 break;
1602 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001603 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001604 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001605 case 2: /* call near abs */ {
1606 long int old_eip;
1607 old_eip = c->eip;
1608 c->eip = c->src.val;
1609 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001610 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001611 break;
1612 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001613 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001614 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001615 break;
1616 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001617 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001618 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001619 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001620 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001621}
1622
1623static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001624 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001625{
1626 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001627 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001628
1629 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1630 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001631 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1632 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001633 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001634 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001635 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1636 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001637
Laurent Vivier05f086f2007-09-24 11:10:55 +02001638 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001639 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001640 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001641}
1642
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001643static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1644 struct x86_emulate_ops *ops)
1645{
1646 struct decode_cache *c = &ctxt->decode;
1647 int rc;
1648 unsigned long cs;
1649
1650 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001651 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001652 return rc;
1653 if (c->op_bytes == 4)
1654 c->eip = (u32)c->eip;
1655 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001656 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001657 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001658 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001659 return rc;
1660}
1661
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001662static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1663 struct x86_emulate_ops *ops, int seg)
1664{
1665 struct decode_cache *c = &ctxt->decode;
1666 unsigned short sel;
1667 int rc;
1668
1669 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1670
1671 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1672 if (rc != X86EMUL_CONTINUE)
1673 return rc;
1674
1675 c->dst.val = c->src.val;
1676 return rc;
1677}
1678
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001679static inline void
1680setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001681 struct x86_emulate_ops *ops, struct desc_struct *cs,
1682 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001683{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001684 memset(cs, 0, sizeof(struct desc_struct));
Gleb Natapov5601d052011-03-07 14:55:06 +02001685 ops->get_cached_descriptor(cs, NULL, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001686 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001687
1688 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001689 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001690 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001691 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001692 cs->type = 0x0b; /* Read, Execute, Accessed */
1693 cs->s = 1;
1694 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001695 cs->p = 1;
1696 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001697
Gleb Natapov79168fd2010-04-28 19:15:30 +03001698 set_desc_base(ss, 0); /* flat segment */
1699 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001700 ss->g = 1; /* 4kb granularity */
1701 ss->s = 1;
1702 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001703 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001704 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001705 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001706}
1707
1708static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001709emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001710{
1711 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001712 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001713 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001714 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001715
1716 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001717 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001718 ctxt->mode == X86EMUL_MODE_VM86)
1719 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001720
Gleb Natapov79168fd2010-04-28 19:15:30 +03001721 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001722
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001723 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001724 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001725 cs_sel = (u16)(msr_data & 0xfffc);
1726 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001727
1728 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001729 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001730 cs.l = 1;
1731 }
Gleb Natapov5601d052011-03-07 14:55:06 +02001732 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001733 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001734 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001735 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001736
1737 c->regs[VCPU_REGS_RCX] = c->eip;
1738 if (is_long_mode(ctxt->vcpu)) {
1739#ifdef CONFIG_X86_64
1740 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1741
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001742 ops->get_msr(ctxt->vcpu,
1743 ctxt->mode == X86EMUL_MODE_PROT64 ?
1744 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001745 c->eip = msr_data;
1746
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001747 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001748 ctxt->eflags &= ~(msr_data | EFLG_RF);
1749#endif
1750 } else {
1751 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001752 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001753 c->eip = (u32)msr_data;
1754
1755 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1756 }
1757
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001758 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001759}
1760
Andre Przywara8c604352009-06-18 12:56:01 +02001761static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001762emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001763{
1764 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001765 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001766 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001767 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001768
Gleb Natapova0044752010-02-10 14:21:31 +02001769 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001770 if (ctxt->mode == X86EMUL_MODE_REAL)
1771 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001772
1773 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1774 * Therefore, we inject an #UD.
1775 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001776 if (ctxt->mode == X86EMUL_MODE_PROT64)
1777 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001778
Gleb Natapov79168fd2010-04-28 19:15:30 +03001779 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001780
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001781 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001782 switch (ctxt->mode) {
1783 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001784 if ((msr_data & 0xfffc) == 0x0)
1785 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001786 break;
1787 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001788 if (msr_data == 0x0)
1789 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001790 break;
1791 }
1792
1793 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001794 cs_sel = (u16)msr_data;
1795 cs_sel &= ~SELECTOR_RPL_MASK;
1796 ss_sel = cs_sel + 8;
1797 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001798 if (ctxt->mode == X86EMUL_MODE_PROT64
1799 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001800 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001801 cs.l = 1;
1802 }
1803
Gleb Natapov5601d052011-03-07 14:55:06 +02001804 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001805 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001806 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001807 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001808
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001809 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001810 c->eip = msr_data;
1811
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001812 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001813 c->regs[VCPU_REGS_RSP] = msr_data;
1814
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001815 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001816}
1817
Andre Przywara4668f052009-06-18 12:56:02 +02001818static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001819emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001820{
1821 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001822 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001823 u64 msr_data;
1824 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001825 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001826
Gleb Natapova0044752010-02-10 14:21:31 +02001827 /* inject #GP if in real mode or Virtual 8086 mode */
1828 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001829 ctxt->mode == X86EMUL_MODE_VM86)
1830 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001831
Gleb Natapov79168fd2010-04-28 19:15:30 +03001832 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001833
1834 if ((c->rex_prefix & 0x8) != 0x0)
1835 usermode = X86EMUL_MODE_PROT64;
1836 else
1837 usermode = X86EMUL_MODE_PROT32;
1838
1839 cs.dpl = 3;
1840 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001841 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001842 switch (usermode) {
1843 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001844 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001845 if ((msr_data & 0xfffc) == 0x0)
1846 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001847 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001848 break;
1849 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001850 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001851 if (msr_data == 0x0)
1852 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001853 ss_sel = cs_sel + 8;
1854 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001855 cs.l = 1;
1856 break;
1857 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001858 cs_sel |= SELECTOR_RPL_MASK;
1859 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001860
Gleb Natapov5601d052011-03-07 14:55:06 +02001861 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001862 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001863 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001864 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001865
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001866 c->eip = c->regs[VCPU_REGS_RDX];
1867 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001868
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001869 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001870}
1871
Gleb Natapov9c537242010-03-18 15:20:05 +02001872static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1873 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001874{
1875 int iopl;
1876 if (ctxt->mode == X86EMUL_MODE_REAL)
1877 return false;
1878 if (ctxt->mode == X86EMUL_MODE_VM86)
1879 return true;
1880 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001881 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001882}
1883
1884static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1885 struct x86_emulate_ops *ops,
1886 u16 port, u16 len)
1887{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001888 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02001889 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001890 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001891 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001892 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02001893 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001894
Gleb Natapov5601d052011-03-07 14:55:06 +02001895 ops->get_cached_descriptor(&tr_seg, &base3, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001896 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001897 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001898 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001899 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02001900 base = get_desc_base(&tr_seg);
1901#ifdef CONFIG_X86_64
1902 base |= ((u64)base3) << 32;
1903#endif
1904 r = ops->read_std(base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001905 if (r != X86EMUL_CONTINUE)
1906 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001907 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001908 return false;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001909 r = ops->read_std(base + io_bitmap_ptr + port/8, &perm, 2, ctxt->vcpu,
Gleb Natapov5601d052011-03-07 14:55:06 +02001910 NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001911 if (r != X86EMUL_CONTINUE)
1912 return false;
1913 if ((perm >> bit_idx) & mask)
1914 return false;
1915 return true;
1916}
1917
1918static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1919 struct x86_emulate_ops *ops,
1920 u16 port, u16 len)
1921{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001922 if (ctxt->perm_ok)
1923 return true;
1924
Gleb Natapov9c537242010-03-18 15:20:05 +02001925 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001926 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1927 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001928
1929 ctxt->perm_ok = true;
1930
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001931 return true;
1932}
1933
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001934static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1935 struct x86_emulate_ops *ops,
1936 struct tss_segment_16 *tss)
1937{
1938 struct decode_cache *c = &ctxt->decode;
1939
1940 tss->ip = c->eip;
1941 tss->flag = ctxt->eflags;
1942 tss->ax = c->regs[VCPU_REGS_RAX];
1943 tss->cx = c->regs[VCPU_REGS_RCX];
1944 tss->dx = c->regs[VCPU_REGS_RDX];
1945 tss->bx = c->regs[VCPU_REGS_RBX];
1946 tss->sp = c->regs[VCPU_REGS_RSP];
1947 tss->bp = c->regs[VCPU_REGS_RBP];
1948 tss->si = c->regs[VCPU_REGS_RSI];
1949 tss->di = c->regs[VCPU_REGS_RDI];
1950
1951 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1952 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1953 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1954 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1955 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1956}
1957
1958static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1959 struct x86_emulate_ops *ops,
1960 struct tss_segment_16 *tss)
1961{
1962 struct decode_cache *c = &ctxt->decode;
1963 int ret;
1964
1965 c->eip = tss->ip;
1966 ctxt->eflags = tss->flag | 2;
1967 c->regs[VCPU_REGS_RAX] = tss->ax;
1968 c->regs[VCPU_REGS_RCX] = tss->cx;
1969 c->regs[VCPU_REGS_RDX] = tss->dx;
1970 c->regs[VCPU_REGS_RBX] = tss->bx;
1971 c->regs[VCPU_REGS_RSP] = tss->sp;
1972 c->regs[VCPU_REGS_RBP] = tss->bp;
1973 c->regs[VCPU_REGS_RSI] = tss->si;
1974 c->regs[VCPU_REGS_RDI] = tss->di;
1975
1976 /*
1977 * SDM says that segment selectors are loaded before segment
1978 * descriptors
1979 */
1980 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1981 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1982 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1983 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1984 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1985
1986 /*
1987 * Now load segment descriptors. If fault happenes at this stage
1988 * it is handled in a context of new task
1989 */
1990 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1991 if (ret != X86EMUL_CONTINUE)
1992 return ret;
1993 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1994 if (ret != X86EMUL_CONTINUE)
1995 return ret;
1996 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1997 if (ret != X86EMUL_CONTINUE)
1998 return ret;
1999 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2000 if (ret != X86EMUL_CONTINUE)
2001 return ret;
2002 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2003 if (ret != X86EMUL_CONTINUE)
2004 return ret;
2005
2006 return X86EMUL_CONTINUE;
2007}
2008
2009static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2010 struct x86_emulate_ops *ops,
2011 u16 tss_selector, u16 old_tss_sel,
2012 ulong old_tss_base, struct desc_struct *new_desc)
2013{
2014 struct tss_segment_16 tss_seg;
2015 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002016 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002017
2018 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002019 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002020 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002021 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002022 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002023
2024 save_state_to_tss16(ctxt, ops, &tss_seg);
2025
2026 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002027 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002028 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002029 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002030 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002031
2032 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002033 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002034 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002035 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002036 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002037
2038 if (old_tss_sel != 0xffff) {
2039 tss_seg.prev_task_link = old_tss_sel;
2040
2041 ret = ops->write_std(new_tss_base,
2042 &tss_seg.prev_task_link,
2043 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002044 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002045 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002046 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002047 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002048 }
2049
2050 return load_state_from_tss16(ctxt, ops, &tss_seg);
2051}
2052
2053static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2054 struct x86_emulate_ops *ops,
2055 struct tss_segment_32 *tss)
2056{
2057 struct decode_cache *c = &ctxt->decode;
2058
2059 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2060 tss->eip = c->eip;
2061 tss->eflags = ctxt->eflags;
2062 tss->eax = c->regs[VCPU_REGS_RAX];
2063 tss->ecx = c->regs[VCPU_REGS_RCX];
2064 tss->edx = c->regs[VCPU_REGS_RDX];
2065 tss->ebx = c->regs[VCPU_REGS_RBX];
2066 tss->esp = c->regs[VCPU_REGS_RSP];
2067 tss->ebp = c->regs[VCPU_REGS_RBP];
2068 tss->esi = c->regs[VCPU_REGS_RSI];
2069 tss->edi = c->regs[VCPU_REGS_RDI];
2070
2071 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2072 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2073 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2074 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2075 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2076 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2077 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2078}
2079
2080static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2081 struct x86_emulate_ops *ops,
2082 struct tss_segment_32 *tss)
2083{
2084 struct decode_cache *c = &ctxt->decode;
2085 int ret;
2086
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002087 if (ops->set_cr(3, tss->cr3, ctxt->vcpu))
2088 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002089 c->eip = tss->eip;
2090 ctxt->eflags = tss->eflags | 2;
2091 c->regs[VCPU_REGS_RAX] = tss->eax;
2092 c->regs[VCPU_REGS_RCX] = tss->ecx;
2093 c->regs[VCPU_REGS_RDX] = tss->edx;
2094 c->regs[VCPU_REGS_RBX] = tss->ebx;
2095 c->regs[VCPU_REGS_RSP] = tss->esp;
2096 c->regs[VCPU_REGS_RBP] = tss->ebp;
2097 c->regs[VCPU_REGS_RSI] = tss->esi;
2098 c->regs[VCPU_REGS_RDI] = tss->edi;
2099
2100 /*
2101 * SDM says that segment selectors are loaded before segment
2102 * descriptors
2103 */
2104 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2105 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2106 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2107 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2108 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2109 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2110 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2111
2112 /*
2113 * Now load segment descriptors. If fault happenes at this stage
2114 * it is handled in a context of new task
2115 */
2116 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2117 if (ret != X86EMUL_CONTINUE)
2118 return ret;
2119 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2120 if (ret != X86EMUL_CONTINUE)
2121 return ret;
2122 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2123 if (ret != X86EMUL_CONTINUE)
2124 return ret;
2125 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2126 if (ret != X86EMUL_CONTINUE)
2127 return ret;
2128 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2129 if (ret != X86EMUL_CONTINUE)
2130 return ret;
2131 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2132 if (ret != X86EMUL_CONTINUE)
2133 return ret;
2134 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2135 if (ret != X86EMUL_CONTINUE)
2136 return ret;
2137
2138 return X86EMUL_CONTINUE;
2139}
2140
2141static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2142 struct x86_emulate_ops *ops,
2143 u16 tss_selector, u16 old_tss_sel,
2144 ulong old_tss_base, struct desc_struct *new_desc)
2145{
2146 struct tss_segment_32 tss_seg;
2147 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002148 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002149
2150 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002151 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002152 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002155
2156 save_state_to_tss32(ctxt, ops, &tss_seg);
2157
2158 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002159 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002160 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002161 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163
2164 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002165 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002166 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002169
2170 if (old_tss_sel != 0xffff) {
2171 tss_seg.prev_task_link = old_tss_sel;
2172
2173 ret = ops->write_std(new_tss_base,
2174 &tss_seg.prev_task_link,
2175 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002176 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002177 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002179 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180 }
2181
2182 return load_state_from_tss32(ctxt, ops, &tss_seg);
2183}
2184
2185static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002186 struct x86_emulate_ops *ops,
2187 u16 tss_selector, int reason,
2188 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189{
2190 struct desc_struct curr_tss_desc, next_tss_desc;
2191 int ret;
2192 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2193 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002194 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002195 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196
2197 /* FIXME: old_tss_base == ~0 ? */
2198
2199 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2200 if (ret != X86EMUL_CONTINUE)
2201 return ret;
2202 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2203 if (ret != X86EMUL_CONTINUE)
2204 return ret;
2205
2206 /* FIXME: check that next_tss_desc is tss */
2207
2208 if (reason != TASK_SWITCH_IRET) {
2209 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002210 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl)
2211 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002212 }
2213
Gleb Natapovceffb452010-03-18 15:20:19 +02002214 desc_limit = desc_limit_scaled(&next_tss_desc);
2215 if (!next_tss_desc.p ||
2216 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2217 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002218 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002219 return X86EMUL_PROPAGATE_FAULT;
2220 }
2221
2222 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2223 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2224 write_segment_descriptor(ctxt, ops, old_tss_sel,
2225 &curr_tss_desc);
2226 }
2227
2228 if (reason == TASK_SWITCH_IRET)
2229 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2230
2231 /* set back link to prev task only if NT bit is set in eflags
2232 note that old_tss_sel is not used afetr this point */
2233 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2234 old_tss_sel = 0xffff;
2235
2236 if (next_tss_desc.type & 8)
2237 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2238 old_tss_base, &next_tss_desc);
2239 else
2240 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2241 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002242 if (ret != X86EMUL_CONTINUE)
2243 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002244
2245 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2246 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2247
2248 if (reason != TASK_SWITCH_IRET) {
2249 next_tss_desc.type |= (1 << 1); /* set busy flag */
2250 write_segment_descriptor(ctxt, ops, tss_selector,
2251 &next_tss_desc);
2252 }
2253
2254 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02002255 ops->set_cached_descriptor(&next_tss_desc, 0, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002256 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2257
Jan Kiszkae269fb22010-04-14 15:51:09 +02002258 if (has_error_code) {
2259 struct decode_cache *c = &ctxt->decode;
2260
2261 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2262 c->lock_prefix = 0;
2263 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002264 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002265 }
2266
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002267 return ret;
2268}
2269
2270int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002271 u16 tss_selector, int reason,
2272 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002273{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002274 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002275 struct decode_cache *c = &ctxt->decode;
2276 int rc;
2277
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002278 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002279 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002280
Jan Kiszkae269fb22010-04-14 15:51:09 +02002281 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2282 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002283
2284 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002285 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002286 if (rc == X86EMUL_CONTINUE)
2287 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002288 }
2289
Gleb Natapov19d04432010-04-15 12:29:50 +03002290 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002291}
2292
Avi Kivity90de84f2010-11-17 15:28:21 +02002293static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002294 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002295{
2296 struct decode_cache *c = &ctxt->decode;
2297 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2298
Gleb Natapovd9271122010-03-18 15:20:22 +02002299 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002300 op->addr.mem.ea = register_address(c, c->regs[reg]);
2301 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002302}
2303
Avi Kivity63540382010-07-29 15:11:55 +03002304static int em_push(struct x86_emulate_ctxt *ctxt)
2305{
2306 emulate_push(ctxt, ctxt->ops);
2307 return X86EMUL_CONTINUE;
2308}
2309
Avi Kivity7af04fc2010-08-18 14:16:35 +03002310static int em_das(struct x86_emulate_ctxt *ctxt)
2311{
2312 struct decode_cache *c = &ctxt->decode;
2313 u8 al, old_al;
2314 bool af, cf, old_cf;
2315
2316 cf = ctxt->eflags & X86_EFLAGS_CF;
2317 al = c->dst.val;
2318
2319 old_al = al;
2320 old_cf = cf;
2321 cf = false;
2322 af = ctxt->eflags & X86_EFLAGS_AF;
2323 if ((al & 0x0f) > 9 || af) {
2324 al -= 6;
2325 cf = old_cf | (al >= 250);
2326 af = true;
2327 } else {
2328 af = false;
2329 }
2330 if (old_al > 0x99 || old_cf) {
2331 al -= 0x60;
2332 cf = true;
2333 }
2334
2335 c->dst.val = al;
2336 /* Set PF, ZF, SF */
2337 c->src.type = OP_IMM;
2338 c->src.val = 0;
2339 c->src.bytes = 1;
2340 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2341 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2342 if (cf)
2343 ctxt->eflags |= X86_EFLAGS_CF;
2344 if (af)
2345 ctxt->eflags |= X86_EFLAGS_AF;
2346 return X86EMUL_CONTINUE;
2347}
2348
Avi Kivity0ef753b2010-08-18 14:51:45 +03002349static int em_call_far(struct x86_emulate_ctxt *ctxt)
2350{
2351 struct decode_cache *c = &ctxt->decode;
2352 u16 sel, old_cs;
2353 ulong old_eip;
2354 int rc;
2355
2356 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2357 old_eip = c->eip;
2358
2359 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2360 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2361 return X86EMUL_CONTINUE;
2362
2363 c->eip = 0;
2364 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2365
2366 c->src.val = old_cs;
2367 emulate_push(ctxt, ctxt->ops);
2368 rc = writeback(ctxt, ctxt->ops);
2369 if (rc != X86EMUL_CONTINUE)
2370 return rc;
2371
2372 c->src.val = old_eip;
2373 emulate_push(ctxt, ctxt->ops);
2374 rc = writeback(ctxt, ctxt->ops);
2375 if (rc != X86EMUL_CONTINUE)
2376 return rc;
2377
2378 c->dst.type = OP_NONE;
2379
2380 return X86EMUL_CONTINUE;
2381}
2382
Avi Kivity40ece7c2010-08-18 15:12:09 +03002383static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2384{
2385 struct decode_cache *c = &ctxt->decode;
2386 int rc;
2387
2388 c->dst.type = OP_REG;
2389 c->dst.addr.reg = &c->eip;
2390 c->dst.bytes = c->op_bytes;
2391 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2392 if (rc != X86EMUL_CONTINUE)
2393 return rc;
2394 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2395 return X86EMUL_CONTINUE;
2396}
2397
Avi Kivity5c82aa22010-08-18 18:31:43 +03002398static int em_imul(struct x86_emulate_ctxt *ctxt)
2399{
2400 struct decode_cache *c = &ctxt->decode;
2401
2402 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2403 return X86EMUL_CONTINUE;
2404}
2405
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002406static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2407{
2408 struct decode_cache *c = &ctxt->decode;
2409
2410 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002411 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002412}
2413
Avi Kivity61429142010-08-19 15:13:00 +03002414static int em_cwd(struct x86_emulate_ctxt *ctxt)
2415{
2416 struct decode_cache *c = &ctxt->decode;
2417
2418 c->dst.type = OP_REG;
2419 c->dst.bytes = c->src.bytes;
2420 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2421 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2422
2423 return X86EMUL_CONTINUE;
2424}
2425
Avi Kivity48bb5d32010-08-18 18:54:34 +03002426static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2427{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002428 struct decode_cache *c = &ctxt->decode;
2429 u64 tsc = 0;
2430
Avi Kivity48bb5d32010-08-18 18:54:34 +03002431 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2432 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2433 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2434 return X86EMUL_CONTINUE;
2435}
2436
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002437static int em_mov(struct x86_emulate_ctxt *ctxt)
2438{
2439 struct decode_cache *c = &ctxt->decode;
2440 c->dst.val = c->src.val;
2441 return X86EMUL_CONTINUE;
2442}
2443
Avi Kivityaa97bb42010-01-20 18:09:23 +02002444static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2445{
2446 struct decode_cache *c = &ctxt->decode;
2447 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2448 return X86EMUL_CONTINUE;
2449}
2450
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002451static bool valid_cr(int nr)
2452{
2453 switch (nr) {
2454 case 0:
2455 case 2 ... 4:
2456 case 8:
2457 return true;
2458 default:
2459 return false;
2460 }
2461}
2462
2463static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2464{
2465 struct decode_cache *c = &ctxt->decode;
2466
2467 if (!valid_cr(c->modrm_reg))
2468 return emulate_ud(ctxt);
2469
2470 return X86EMUL_CONTINUE;
2471}
2472
2473static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2474{
2475 struct decode_cache *c = &ctxt->decode;
2476 u64 new_val = c->src.val64;
2477 int cr = c->modrm_reg;
2478
2479 static u64 cr_reserved_bits[] = {
2480 0xffffffff00000000ULL,
2481 0, 0, 0, /* CR3 checked later */
2482 CR4_RESERVED_BITS,
2483 0, 0, 0,
2484 CR8_RESERVED_BITS,
2485 };
2486
2487 if (!valid_cr(cr))
2488 return emulate_ud(ctxt);
2489
2490 if (new_val & cr_reserved_bits[cr])
2491 return emulate_gp(ctxt, 0);
2492
2493 switch (cr) {
2494 case 0: {
2495 u64 cr4, efer;
2496 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2497 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2498 return emulate_gp(ctxt, 0);
2499
2500 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2501 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2502
2503 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2504 !(cr4 & X86_CR4_PAE))
2505 return emulate_gp(ctxt, 0);
2506
2507 break;
2508 }
2509 case 3: {
2510 u64 rsvd = 0;
2511
2512 if (is_long_mode(ctxt->vcpu))
2513 rsvd = CR3_L_MODE_RESERVED_BITS;
2514 else if (is_pae(ctxt->vcpu))
2515 rsvd = CR3_PAE_RESERVED_BITS;
2516 else if (is_paging(ctxt->vcpu))
2517 rsvd = CR3_NONPAE_RESERVED_BITS;
2518
2519 if (new_val & rsvd)
2520 return emulate_gp(ctxt, 0);
2521
2522 break;
2523 }
2524 case 4: {
2525 u64 cr4, efer;
2526
2527 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2528 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2529
2530 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2531 return emulate_gp(ctxt, 0);
2532
2533 break;
2534 }
2535 }
2536
2537 return X86EMUL_CONTINUE;
2538}
2539
Joerg Roedel3b88e412011-04-04 12:39:29 +02002540static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2541{
2542 unsigned long dr7;
2543
2544 ctxt->ops->get_dr(7, &dr7, ctxt->vcpu);
2545
2546 /* Check if DR7.Global_Enable is set */
2547 return dr7 & (1 << 13);
2548}
2549
2550static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2551{
2552 struct decode_cache *c = &ctxt->decode;
2553 int dr = c->modrm_reg;
2554 u64 cr4;
2555
2556 if (dr > 7)
2557 return emulate_ud(ctxt);
2558
2559 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2560 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2561 return emulate_ud(ctxt);
2562
2563 if (check_dr7_gd(ctxt))
2564 return emulate_db(ctxt);
2565
2566 return X86EMUL_CONTINUE;
2567}
2568
2569static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2570{
2571 struct decode_cache *c = &ctxt->decode;
2572 u64 new_val = c->src.val64;
2573 int dr = c->modrm_reg;
2574
2575 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2576 return emulate_gp(ctxt, 0);
2577
2578 return check_dr_read(ctxt);
2579}
2580
Joerg Roedel01de8b02011-04-04 12:39:31 +02002581static int check_svme(struct x86_emulate_ctxt *ctxt)
2582{
2583 u64 efer;
2584
2585 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2586
2587 if (!(efer & EFER_SVME))
2588 return emulate_ud(ctxt);
2589
2590 return X86EMUL_CONTINUE;
2591}
2592
2593static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2594{
2595 u64 rax = kvm_register_read(ctxt->vcpu, VCPU_REGS_RAX);
2596
2597 /* Valid physical address? */
2598 if (rax & 0xffff000000000000)
2599 return emulate_gp(ctxt, 0);
2600
2601 return check_svme(ctxt);
2602}
2603
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002604static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2605{
2606 u64 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2607
2608 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt->vcpu))
2609 return emulate_ud(ctxt);
2610
2611 return X86EMUL_CONTINUE;
2612}
2613
Joerg Roedel80612522011-04-04 12:39:33 +02002614static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2615{
2616 u64 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2617 u64 rcx = kvm_register_read(ctxt->vcpu, VCPU_REGS_RCX);
2618
2619 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt->vcpu)) ||
2620 (rcx > 3))
2621 return emulate_gp(ctxt, 0);
2622
2623 return X86EMUL_CONTINUE;
2624}
2625
Avi Kivity73fba5f2010-07-29 15:11:53 +03002626#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002627#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002628#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2629 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002630#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002631#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002632#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2633#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2634#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002635#define II(_f, _e, _i) \
2636 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002637#define IIP(_f, _e, _i, _p) \
2638 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2639 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002640#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002641
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002642#define D2bv(_f) D((_f) | ByteOp), D(_f)
2643#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2644
Avi Kivity6230f7f2010-08-26 18:34:55 +03002645#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2646 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2647 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2648
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002649static struct opcode group7_rm1[] = {
2650 DI(SrcNone | ModRM | Priv, monitor),
2651 DI(SrcNone | ModRM | Priv, mwait),
2652 N, N, N, N, N, N,
2653};
2654
Joerg Roedel01de8b02011-04-04 12:39:31 +02002655static struct opcode group7_rm3[] = {
2656 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
2657 DIP(SrcNone | ModRM | Prot , vmmcall, check_svme),
2658 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
2659 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
2660 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
2661 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
2662 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
2663 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
2664};
Avi Kivity6230f7f2010-08-26 18:34:55 +03002665
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002666static struct opcode group7_rm7[] = {
2667 N,
2668 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
2669 N, N, N, N, N, N,
2670};
Avi Kivity73fba5f2010-07-29 15:11:53 +03002671static struct opcode group1[] = {
2672 X7(D(Lock)), N
2673};
2674
2675static struct opcode group1A[] = {
2676 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2677};
2678
2679static struct opcode group3[] = {
2680 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2681 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002682 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002683};
2684
2685static struct opcode group4[] = {
2686 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2687 N, N, N, N, N, N,
2688};
2689
2690static struct opcode group5[] = {
2691 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002692 D(SrcMem | ModRM | Stack),
2693 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002694 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2695 D(SrcMem | ModRM | Stack), N,
2696};
2697
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002698static struct opcode group6[] = {
2699 DI(ModRM | Prot, sldt),
2700 DI(ModRM | Prot, str),
2701 DI(ModRM | Prot | Priv, lldt),
2702 DI(ModRM | Prot | Priv, ltr),
2703 N, N, N, N,
2704};
2705
Avi Kivity73fba5f2010-07-29 15:11:53 +03002706static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002707 DI(ModRM | Mov | DstMem | Priv, sgdt),
2708 DI(ModRM | Mov | DstMem | Priv, sidt),
2709 DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002710 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2711 DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
2712 DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002713}, {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002714 D(SrcNone | ModRM | Priv | VendorSpecific), EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002715 N, EXT(0, group7_rm3),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002716 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002717 DI(SrcMem16 | ModRM | Mov | Priv, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002718} };
2719
2720static struct opcode group8[] = {
2721 N, N, N, N,
2722 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2723 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2724};
2725
2726static struct group_dual group9 = { {
2727 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2728}, {
2729 N, N, N, N, N, N, N, N,
2730} };
2731
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002732static struct opcode group11[] = {
2733 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2734};
2735
Avi Kivityaa97bb42010-01-20 18:09:23 +02002736static struct gprefix pfx_0f_6f_0f_7f = {
2737 N, N, N, I(Sse, em_movdqu),
2738};
2739
Avi Kivity73fba5f2010-07-29 15:11:53 +03002740static struct opcode opcode_table[256] = {
2741 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002742 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002743 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2744 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002745 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002746 D(ImplicitOps | Stack | No64), N,
2747 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002748 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002749 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2750 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002751 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002752 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2753 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002754 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002755 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002756 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002757 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002758 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002759 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002760 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002761 /* 0x40 - 0x4F */
2762 X16(D(DstReg)),
2763 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002764 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002765 /* 0x58 - 0x5F */
2766 X8(D(DstReg | Stack)),
2767 /* 0x60 - 0x67 */
2768 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2769 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2770 N, N, N, N,
2771 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002772 I(SrcImm | Mov | Stack, em_push),
2773 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002774 I(SrcImmByte | Mov | Stack, em_push),
2775 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002776 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2777 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002778 /* 0x70 - 0x7F */
2779 X16(D(SrcImmByte)),
2780 /* 0x80 - 0x87 */
2781 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2782 G(DstMem | SrcImm | ModRM | Group, group1),
2783 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2784 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002785 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002786 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002787 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2788 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002789 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002790 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2791 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02002792 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002793 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002794 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002795 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002796 DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002797 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002798 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2799 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2800 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2801 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002802 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002803 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002804 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2805 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002806 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002807 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002808 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002809 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002810 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002811 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002812 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002813 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2814 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002815 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002816 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002817 /* 0xC8 - 0xCF */
2818 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002819 D(ImplicitOps), DI(SrcImmByte, intn),
2820 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002821 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002822 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002823 N, N, N, N,
2824 /* 0xD8 - 0xDF */
2825 N, N, N, N, N, N, N, N,
2826 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002827 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002828 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002829 /* 0xE8 - 0xEF */
2830 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2831 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002832 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002833 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02002834 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002835 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
2836 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002837 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002838 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002839 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2840};
2841
2842static struct opcode twobyte_table[256] = {
2843 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002844 G(0, group6), GD(0, &group7), N, N,
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002845 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002846 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002847 N, D(ImplicitOps | ModRM), N, N,
2848 /* 0x10 - 0x1F */
2849 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2850 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002851 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02002852 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002853 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02002854 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002855 N, N, N, N,
2856 N, N, N, N, N, N, N, N,
2857 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02002858 DI(ImplicitOps | Priv, wrmsr),
2859 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
2860 DI(ImplicitOps | Priv, rdmsr),
2861 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Avi Kivityd8671622011-02-01 16:32:03 +02002862 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
2863 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002864 N, N, N, N, N, N, N, N,
2865 /* 0x40 - 0x4F */
2866 X16(D(DstReg | SrcMem | ModRM | Mov)),
2867 /* 0x50 - 0x5F */
2868 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2869 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002870 N, N, N, N,
2871 N, N, N, N,
2872 N, N, N, N,
2873 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002874 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002875 N, N, N, N,
2876 N, N, N, N,
2877 N, N, N, N,
2878 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002879 /* 0x80 - 0x8F */
2880 X16(D(SrcImm)),
2881 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002882 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002883 /* 0xA0 - 0xA7 */
2884 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02002885 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002886 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2887 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2888 /* 0xA8 - 0xAF */
2889 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02002890 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002891 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2892 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002893 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002894 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002895 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002896 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2897 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2898 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002899 /* 0xB8 - 0xBF */
2900 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002901 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002902 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2903 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002904 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002905 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002906 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002907 N, N, N, GD(0, &group9),
2908 N, N, N, N, N, N, N, N,
2909 /* 0xD0 - 0xDF */
2910 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2911 /* 0xE0 - 0xEF */
2912 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2913 /* 0xF0 - 0xFF */
2914 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2915};
2916
2917#undef D
2918#undef N
2919#undef G
2920#undef GD
2921#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02002922#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02002923#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03002924
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002925#undef D2bv
2926#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002927#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002928
Avi Kivity39f21ee2010-08-18 19:20:21 +03002929static unsigned imm_size(struct decode_cache *c)
2930{
2931 unsigned size;
2932
2933 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2934 if (size == 8)
2935 size = 4;
2936 return size;
2937}
2938
2939static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2940 unsigned size, bool sign_extension)
2941{
2942 struct decode_cache *c = &ctxt->decode;
2943 struct x86_emulate_ops *ops = ctxt->ops;
2944 int rc = X86EMUL_CONTINUE;
2945
2946 op->type = OP_IMM;
2947 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002948 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002949 /* NB. Immediates are sign-extended as necessary. */
2950 switch (op->bytes) {
2951 case 1:
2952 op->val = insn_fetch(s8, 1, c->eip);
2953 break;
2954 case 2:
2955 op->val = insn_fetch(s16, 2, c->eip);
2956 break;
2957 case 4:
2958 op->val = insn_fetch(s32, 4, c->eip);
2959 break;
2960 }
2961 if (!sign_extension) {
2962 switch (op->bytes) {
2963 case 1:
2964 op->val &= 0xff;
2965 break;
2966 case 2:
2967 op->val &= 0xffff;
2968 break;
2969 case 4:
2970 op->val &= 0xffffffff;
2971 break;
2972 }
2973 }
2974done:
2975 return rc;
2976}
2977
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002978int
Andre Przywaradc25e892010-12-21 11:12:07 +01002979x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002980{
2981 struct x86_emulate_ops *ops = ctxt->ops;
2982 struct decode_cache *c = &ctxt->decode;
2983 int rc = X86EMUL_CONTINUE;
2984 int mode = ctxt->mode;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002985 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
2986 bool op_prefix = false;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002987 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002988 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002989
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002990 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01002991 c->fetch.start = c->eip;
2992 c->fetch.end = c->fetch.start + insn_len;
2993 if (insn_len > 0)
2994 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002995 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2996
2997 switch (mode) {
2998 case X86EMUL_MODE_REAL:
2999 case X86EMUL_MODE_VM86:
3000 case X86EMUL_MODE_PROT16:
3001 def_op_bytes = def_ad_bytes = 2;
3002 break;
3003 case X86EMUL_MODE_PROT32:
3004 def_op_bytes = def_ad_bytes = 4;
3005 break;
3006#ifdef CONFIG_X86_64
3007 case X86EMUL_MODE_PROT64:
3008 def_op_bytes = 4;
3009 def_ad_bytes = 8;
3010 break;
3011#endif
3012 default:
3013 return -1;
3014 }
3015
3016 c->op_bytes = def_op_bytes;
3017 c->ad_bytes = def_ad_bytes;
3018
3019 /* Legacy prefixes. */
3020 for (;;) {
3021 switch (c->b = insn_fetch(u8, 1, c->eip)) {
3022 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003023 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003024 /* switch between 2/4 bytes */
3025 c->op_bytes = def_op_bytes ^ 6;
3026 break;
3027 case 0x67: /* address-size override */
3028 if (mode == X86EMUL_MODE_PROT64)
3029 /* switch between 4/8 bytes */
3030 c->ad_bytes = def_ad_bytes ^ 12;
3031 else
3032 /* switch between 2/4 bytes */
3033 c->ad_bytes = def_ad_bytes ^ 6;
3034 break;
3035 case 0x26: /* ES override */
3036 case 0x2e: /* CS override */
3037 case 0x36: /* SS override */
3038 case 0x3e: /* DS override */
3039 set_seg_override(c, (c->b >> 3) & 3);
3040 break;
3041 case 0x64: /* FS override */
3042 case 0x65: /* GS override */
3043 set_seg_override(c, c->b & 7);
3044 break;
3045 case 0x40 ... 0x4f: /* REX */
3046 if (mode != X86EMUL_MODE_PROT64)
3047 goto done_prefixes;
3048 c->rex_prefix = c->b;
3049 continue;
3050 case 0xf0: /* LOCK */
3051 c->lock_prefix = 1;
3052 break;
3053 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003054 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02003055 c->rep_prefix = c->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003056 break;
3057 default:
3058 goto done_prefixes;
3059 }
3060
3061 /* Any legacy prefix after a REX prefix nullifies its effect. */
3062
3063 c->rex_prefix = 0;
3064 }
3065
3066done_prefixes:
3067
3068 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03003069 if (c->rex_prefix & 8)
3070 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003071
3072 /* Opcode byte(s). */
3073 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003074 /* Two-byte opcode? */
3075 if (c->b == 0x0f) {
3076 c->twobyte = 1;
3077 c->b = insn_fetch(u8, 1, c->eip);
3078 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003079 }
3080 c->d = opcode.flags;
3081
3082 if (c->d & Group) {
3083 dual = c->d & GroupDual;
3084 c->modrm = insn_fetch(u8, 1, c->eip);
3085 --c->eip;
3086
3087 if (c->d & GroupDual) {
3088 g_mod012 = opcode.u.gdual->mod012;
3089 g_mod3 = opcode.u.gdual->mod3;
3090 } else
3091 g_mod012 = g_mod3 = opcode.u.group;
3092
3093 c->d &= ~(Group | GroupDual);
3094
3095 goffset = (c->modrm >> 3) & 7;
3096
3097 if ((c->modrm >> 6) == 3)
3098 opcode = g_mod3[goffset];
3099 else
3100 opcode = g_mod012[goffset];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003101
3102 if (opcode.flags & RMExt) {
3103 goffset = c->modrm & 7;
3104 opcode = opcode.u.group[goffset];
3105 }
3106
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003107 c->d |= opcode.flags;
3108 }
3109
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003110 if (c->d & Prefix) {
3111 if (c->rep_prefix && op_prefix)
3112 return X86EMUL_UNHANDLEABLE;
3113 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3114 switch (simd_prefix) {
3115 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3116 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3117 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3118 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3119 }
3120 c->d |= opcode.flags;
3121 }
3122
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003123 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02003124 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02003125 c->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003126
3127 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02003128 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003129 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003130
Avi Kivityd8671622011-02-01 16:32:03 +02003131 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3132 return -1;
3133
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003134 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3135 c->op_bytes = 8;
3136
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003137 if (c->d & Op3264) {
3138 if (mode == X86EMUL_MODE_PROT64)
3139 c->op_bytes = 8;
3140 else
3141 c->op_bytes = 4;
3142 }
3143
Avi Kivity12537912011-03-29 11:41:27 +02003144 if (c->d & Sse)
3145 c->op_bytes = 16;
3146
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003147 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03003148 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003149 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03003150 if (!c->has_seg_override)
3151 set_seg_override(c, c->modrm_seg);
3152 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003153 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003154 if (rc != X86EMUL_CONTINUE)
3155 goto done;
3156
3157 if (!c->has_seg_override)
3158 set_seg_override(c, VCPU_SREG_DS);
3159
Avi Kivity90de84f2010-11-17 15:28:21 +02003160 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003161
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003162 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003163 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003164
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003165 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02003166 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003167
3168 /*
3169 * Decode and fetch the source operand: register, memory
3170 * or immediate.
3171 */
3172 switch (c->d & SrcMask) {
3173 case SrcNone:
3174 break;
3175 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02003176 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003177 break;
3178 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003179 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003180 goto srcmem_common;
3181 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003182 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003183 goto srcmem_common;
3184 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003185 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003186 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003187 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003188 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003189 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003190 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003191 rc = decode_imm(ctxt, &c->src, 2, false);
3192 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003193 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003194 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3195 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003196 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003197 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003198 break;
3199 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003200 rc = decode_imm(ctxt, &c->src, 1, true);
3201 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003202 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003203 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003204 break;
3205 case SrcAcc:
3206 c->src.type = OP_REG;
3207 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003208 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003209 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003210 break;
3211 case SrcOne:
3212 c->src.bytes = 1;
3213 c->src.val = 1;
3214 break;
3215 case SrcSI:
3216 c->src.type = OP_MEM;
3217 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003218 c->src.addr.mem.ea =
3219 register_address(c, c->regs[VCPU_REGS_RSI]);
3220 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003221 c->src.val = 0;
3222 break;
3223 case SrcImmFAddr:
3224 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003225 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003226 c->src.bytes = c->op_bytes + 2;
3227 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3228 break;
3229 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003230 memop.bytes = c->op_bytes + 2;
3231 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003232 break;
3233 }
3234
Avi Kivity39f21ee2010-08-18 19:20:21 +03003235 if (rc != X86EMUL_CONTINUE)
3236 goto done;
3237
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003238 /*
3239 * Decode and fetch the second source operand: register, memory
3240 * or immediate.
3241 */
3242 switch (c->d & Src2Mask) {
3243 case Src2None:
3244 break;
3245 case Src2CL:
3246 c->src2.bytes = 1;
3247 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3248 break;
3249 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003250 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003251 break;
3252 case Src2One:
3253 c->src2.bytes = 1;
3254 c->src2.val = 1;
3255 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003256 case Src2Imm:
3257 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3258 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003259 }
3260
Avi Kivity39f21ee2010-08-18 19:20:21 +03003261 if (rc != X86EMUL_CONTINUE)
3262 goto done;
3263
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003264 /* Decode and fetch the destination operand: register or memory. */
3265 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003266 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003267 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003268 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3269 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003270 case DstImmUByte:
3271 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003272 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003273 c->dst.bytes = 1;
3274 c->dst.val = insn_fetch(u8, 1, c->eip);
3275 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003276 case DstMem:
3277 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003278 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003279 if ((c->d & DstMask) == DstMem64)
3280 c->dst.bytes = 8;
3281 else
3282 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003283 if (c->d & BitOp)
3284 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003285 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003286 break;
3287 case DstAcc:
3288 c->dst.type = OP_REG;
3289 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003290 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003291 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003292 c->dst.orig_val = c->dst.val;
3293 break;
3294 case DstDI:
3295 c->dst.type = OP_MEM;
3296 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003297 c->dst.addr.mem.ea =
3298 register_address(c, c->regs[VCPU_REGS_RDI]);
3299 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003300 c->dst.val = 0;
3301 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003302 case ImplicitOps:
3303 /* Special instructions do their own operand decoding. */
3304 default:
3305 c->dst.type = OP_NONE; /* Disable writeback. */
3306 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003307 }
3308
3309done:
3310 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
3311}
3312
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003313static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3314{
3315 struct decode_cache *c = &ctxt->decode;
3316
3317 /* The second termination condition only applies for REPE
3318 * and REPNE. Test if the repeat string operation prefix is
3319 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3320 * corresponding termination condition according to:
3321 * - if REPE/REPZ and ZF = 0 then done
3322 * - if REPNE/REPNZ and ZF = 1 then done
3323 */
3324 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3325 (c->b == 0xae) || (c->b == 0xaf))
3326 && (((c->rep_prefix == REPE_PREFIX) &&
3327 ((ctxt->eflags & EFLG_ZF) == 0))
3328 || ((c->rep_prefix == REPNE_PREFIX) &&
3329 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3330 return true;
3331
3332 return false;
3333}
3334
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003335int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003336x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003337{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003338 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003339 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003340 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003341 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003342 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003343 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003344
Gleb Natapov9de41572010-04-28 19:15:22 +03003345 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003346
Gleb Natapov11616242010-02-11 14:43:14 +02003347 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003348 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003349 goto done;
3350 }
3351
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003352 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003353 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003354 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003355 goto done;
3356 }
3357
Avi Kivity081bca02010-08-26 11:06:15 +03003358 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003359 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003360 goto done;
3361 }
3362
Avi Kivity12537912011-03-29 11:41:27 +02003363 if ((c->d & Sse)
3364 && ((ops->get_cr(0, ctxt->vcpu) & X86_CR0_EM)
3365 || !(ops->get_cr(4, ctxt->vcpu) & X86_CR4_OSFXSR))) {
3366 rc = emulate_ud(ctxt);
3367 goto done;
3368 }
3369
3370 if ((c->d & Sse) && (ops->get_cr(0, ctxt->vcpu) & X86_CR0_TS)) {
3371 rc = emulate_nm(ctxt);
3372 goto done;
3373 }
3374
Avi Kivityc4f035c2011-04-04 12:39:22 +02003375 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003376 rc = emulator_check_intercept(ctxt, c->intercept,
3377 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003378 if (rc != X86EMUL_CONTINUE)
3379 goto done;
3380 }
3381
Gleb Natapove92805a2010-02-10 14:21:35 +02003382 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003383 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003384 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003385 goto done;
3386 }
3387
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003388 /* Instruction can only be executed in protected mode */
3389 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3390 rc = emulate_ud(ctxt);
3391 goto done;
3392 }
3393
Joerg Roedeld09beab2011-04-04 12:39:25 +02003394 /* Do instruction specific permission checks */
3395 if (c->check_perm) {
3396 rc = c->check_perm(ctxt);
3397 if (rc != X86EMUL_CONTINUE)
3398 goto done;
3399 }
3400
Avi Kivityc4f035c2011-04-04 12:39:22 +02003401 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003402 rc = emulator_check_intercept(ctxt, c->intercept,
3403 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003404 if (rc != X86EMUL_CONTINUE)
3405 goto done;
3406 }
3407
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003408 if (c->rep_prefix && (c->d & String)) {
3409 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003410 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003411 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003412 goto done;
3413 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003414 }
3415
Wei Yongjunc483c022010-08-06 15:36:36 +08003416 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003417 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03003418 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003419 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003420 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003421 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003422 }
3423
Gleb Natapove35b7b92010-02-25 16:36:42 +02003424 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003425 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003426 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003427 if (rc != X86EMUL_CONTINUE)
3428 goto done;
3429 }
3430
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003431 if ((c->d & DstMask) == ImplicitOps)
3432 goto special_insn;
3433
3434
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003435 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3436 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003437 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003438 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003439 if (rc != X86EMUL_CONTINUE)
3440 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003441 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003442 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003443
Avi Kivity018a98d2007-11-27 19:30:56 +02003444special_insn:
3445
Avi Kivityc4f035c2011-04-04 12:39:22 +02003446 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003447 rc = emulator_check_intercept(ctxt, c->intercept,
3448 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003449 if (rc != X86EMUL_CONTINUE)
3450 goto done;
3451 }
3452
Avi Kivityef65c882010-07-29 15:11:51 +03003453 if (c->execute) {
3454 rc = c->execute(ctxt);
3455 if (rc != X86EMUL_CONTINUE)
3456 goto done;
3457 goto writeback;
3458 }
3459
Laurent Viviere4e03de2007-09-18 11:52:50 +02003460 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 goto twobyte_insn;
3462
Laurent Viviere4e03de2007-09-18 11:52:50 +02003463 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464 case 0x00 ... 0x05:
3465 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003466 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003468 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003469 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003470 break;
3471 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003472 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003473 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 case 0x08 ... 0x0d:
3475 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003476 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003478 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003479 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003480 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 case 0x10 ... 0x15:
3482 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003483 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003485 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003486 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003487 break;
3488 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003489 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003490 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003491 case 0x18 ... 0x1d:
3492 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003493 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003495 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003496 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003497 break;
3498 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003499 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003500 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003501 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003503 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504 break;
3505 case 0x28 ... 0x2d:
3506 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003507 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508 break;
3509 case 0x30 ... 0x35:
3510 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003511 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512 break;
3513 case 0x38 ... 0x3d:
3514 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003515 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003517 case 0x40 ... 0x47: /* inc r16/r32 */
3518 emulate_1op("inc", c->dst, ctxt->eflags);
3519 break;
3520 case 0x48 ... 0x4f: /* dec r16/r32 */
3521 emulate_1op("dec", c->dst, ctxt->eflags);
3522 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003523 case 0x58 ... 0x5f: /* pop reg */
3524 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003525 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003526 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003527 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003528 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003529 break;
3530 case 0x61: /* popa */
3531 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003532 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003534 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003535 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003536 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003538 case 0x6c: /* insb */
3539 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003540 c->src.val = c->regs[VCPU_REGS_RDX];
3541 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003542 case 0x6e: /* outsb */
3543 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003544 c->dst.val = c->regs[VCPU_REGS_RDX];
3545 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003546 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003547 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003548 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003549 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003550 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003552 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553 case 0:
3554 goto add;
3555 case 1:
3556 goto or;
3557 case 2:
3558 goto adc;
3559 case 3:
3560 goto sbb;
3561 case 4:
3562 goto and;
3563 case 5:
3564 goto sub;
3565 case 6:
3566 goto xor;
3567 case 7:
3568 goto cmp;
3569 }
3570 break;
3571 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003572 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003573 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003574 break;
3575 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003576 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003577 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003578 c->src.val = c->dst.val;
3579 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580 /*
3581 * Write back the memory destination with implicit LOCK
3582 * prefix.
3583 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003584 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003585 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003587 case 0x8c: /* mov r/m, sreg */
3588 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003589 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003590 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003591 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003592 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003593 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003594 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003595 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003596 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003597 case 0x8e: { /* mov seg, r/m16 */
3598 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003599
3600 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003601
Gleb Natapovc6975182010-02-18 12:15:01 +02003602 if (c->modrm_reg == VCPU_SREG_CS ||
3603 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003604 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003605 goto done;
3606 }
3607
Glauber Costa310b5d32009-05-12 16:21:06 -04003608 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003609 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003610
Gleb Natapov2e873022010-03-18 15:20:18 +02003611 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003612
3613 c->dst.type = OP_NONE; /* Disable writeback. */
3614 break;
3615 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003616 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003617 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003619 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3620 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003621 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003622 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003623 case 0x98: /* cbw/cwde/cdqe */
3624 switch (c->op_bytes) {
3625 case 2: c->dst.val = (s8)c->dst.val; break;
3626 case 4: c->dst.val = (s16)c->dst.val; break;
3627 case 8: c->dst.val = (s32)c->dst.val; break;
3628 }
3629 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003630 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003631 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003632 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003633 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003634 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003635 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003636 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003637 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003638 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003639 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003641 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003642 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003643 case 0xa8 ... 0xa9: /* test ax, imm */
3644 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003646 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003647 case 0xc0 ... 0xc1:
3648 emulate_grp2(ctxt);
3649 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003650 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003651 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003652 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003653 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003654 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003655 case 0xc4: /* les */
3656 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003657 break;
3658 case 0xc5: /* lds */
3659 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003660 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003661 case 0xcb: /* ret far */
3662 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003663 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003664 case 0xcc: /* int3 */
3665 irq = 3;
3666 goto do_interrupt;
3667 case 0xcd: /* int n */
3668 irq = c->src.val;
3669 do_interrupt:
3670 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003671 break;
3672 case 0xce: /* into */
3673 if (ctxt->eflags & EFLG_OF) {
3674 irq = 4;
3675 goto do_interrupt;
3676 }
3677 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003678 case 0xcf: /* iret */
3679 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003680 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003681 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003682 emulate_grp2(ctxt);
3683 break;
3684 case 0xd2 ... 0xd3: /* Grp2 */
3685 c->src.val = c->regs[VCPU_REGS_RCX];
3686 emulate_grp2(ctxt);
3687 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003688 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3689 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3690 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3691 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3692 jmp_rel(c, c->src.val);
3693 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003694 case 0xe3: /* jcxz/jecxz/jrcxz */
3695 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3696 jmp_rel(c, c->src.val);
3697 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003698 case 0xe4: /* inb */
3699 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003700 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003701 case 0xe6: /* outb */
3702 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003703 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003704 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003705 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003706 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003707 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003708 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003709 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003710 }
3711 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003712 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003713 case 0xea: { /* jmp far */
3714 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003715 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003716 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3717
3718 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003719 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003720
Gleb Natapov414e6272010-04-28 19:15:26 +03003721 c->eip = 0;
3722 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003723 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003724 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003725 case 0xeb:
3726 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003727 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003728 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003729 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003730 case 0xec: /* in al,dx */
3731 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003732 c->src.val = c->regs[VCPU_REGS_RDX];
3733 do_io_in:
3734 c->dst.bytes = min(c->dst.bytes, 4u);
3735 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003736 rc = emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003737 goto done;
3738 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003739 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3740 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003741 goto done; /* IO is needed */
3742 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003743 case 0xee: /* out dx,al */
3744 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003745 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003746 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003747 c->src.bytes = min(c->src.bytes, 4u);
3748 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3749 c->src.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003750 rc = emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003751 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003752 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003753 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3754 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003755 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003756 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003757 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003758 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003759 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003760 case 0xf5: /* cmc */
3761 /* complement carry flag from eflags reg */
3762 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003763 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003764 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003765 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003766 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003767 case 0xf8: /* clc */
3768 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003769 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003770 case 0xf9: /* stc */
3771 ctxt->eflags |= EFLG_CF;
3772 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003773 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003774 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003775 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003776 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003777 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003778 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003779 break;
3780 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003781 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003782 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003783 goto done;
3784 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003785 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003786 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003787 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003788 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003789 case 0xfc: /* cld */
3790 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003791 break;
3792 case 0xfd: /* std */
3793 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003794 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003795 case 0xfe: /* Grp4 */
3796 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003797 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003798 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003799 case 0xff: /* Grp5 */
3800 if (c->modrm_reg == 5)
3801 goto jump_far;
3802 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003803 default:
3804 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003806
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003807 if (rc != X86EMUL_CONTINUE)
3808 goto done;
3809
Avi Kivity018a98d2007-11-27 19:30:56 +02003810writeback:
3811 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003812 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003813 goto done;
3814
Gleb Natapov5cd21912010-03-18 15:20:26 +02003815 /*
3816 * restore dst type in case the decoding will be reused
3817 * (happens for string instruction )
3818 */
3819 c->dst.type = saved_dst_type;
3820
Gleb Natapova682e352010-03-18 15:20:21 +02003821 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003822 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003823 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003824
3825 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003826 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003827 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003828
Gleb Natapov5cd21912010-03-18 15:20:26 +02003829 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003830 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003831 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003832
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003833 if (!string_insn_completed(ctxt)) {
3834 /*
3835 * Re-enter guest when pio read ahead buffer is empty
3836 * or, if it is not used, after each 1024 iteration.
3837 */
3838 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3839 (r->end == 0 || r->end != r->pos)) {
3840 /*
3841 * Reset read cache. Usually happens before
3842 * decode, but since instruction is restarted
3843 * we have to do it here.
3844 */
3845 ctxt->decode.mem_read.end = 0;
3846 return EMULATION_RESTART;
3847 }
3848 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003849 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003850 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003851
3852 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003853
3854done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003855 if (rc == X86EMUL_PROPAGATE_FAULT)
3856 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02003857 if (rc == X86EMUL_INTERCEPTED)
3858 return EMULATION_INTERCEPTED;
3859
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003860 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861
3862twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003863 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003865 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 u16 size;
3867 unsigned long address;
3868
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003869 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003870 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003871 goto cannot_emulate;
3872
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003873 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003874 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003875 goto done;
3876
Avi Kivity33e38852008-05-21 15:34:25 +03003877 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003878 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003879 /* Disable writeback. */
3880 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003883 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003884 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003885 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886 goto done;
3887 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003888 /* Disable writeback. */
3889 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003890 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003891 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003892 if (c->modrm_mod == 3) {
3893 switch (c->modrm_rm) {
3894 case 1:
3895 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003896 break;
3897 default:
3898 goto cannot_emulate;
3899 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003900 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003901 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003902 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003903 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003904 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003905 goto done;
3906 realmode_lidt(ctxt->vcpu, size, address);
3907 }
Avi Kivity16286d02008-04-14 14:40:50 +03003908 /* Disable writeback. */
3909 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
3911 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003912 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003913 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914 break;
3915 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003916 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003917 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003918 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003920 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003921 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003922 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003923 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003925 emulate_invlpg(ctxt->vcpu,
3926 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003927 /* Disable writeback. */
3928 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929 break;
3930 default:
3931 goto cannot_emulate;
3932 }
3933 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003934 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003935 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003936 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003937 case 0x06:
3938 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003939 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003940 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003941 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003942 break;
3943 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003944 case 0x0d: /* GrpP (prefetch) */
3945 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003946 break;
3947 case 0x20: /* mov cr, reg */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003948 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003949 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950 case 0x21: /* mov from dr to reg */
Avi Kivityb27f3852010-08-01 14:25:22 +03003951 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003953 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003954 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003955 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003956 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03003957 goto done;
3958 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003959 c->dst.type = OP_NONE;
3960 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961 case 0x23: /* mov from reg to dr */
Avi Kivityb27f3852010-08-01 14:25:22 +03003962 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003963 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3964 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3965 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003966 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003967 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03003968 goto done;
3969 }
3970
Laurent Viviera01af5e2007-09-24 11:10:56 +02003971 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003973 case 0x30:
3974 /* wrmsr */
3975 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3976 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003977 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003978 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003979 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003980 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003981 }
3982 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003983 break;
3984 case 0x32:
3985 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003986 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003987 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003988 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003989 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003990 } else {
3991 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3992 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3993 }
3994 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003995 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003996 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003997 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003998 break;
3999 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004000 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004001 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004002 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004003 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02004004 if (!test_cc(c->b, ctxt->eflags))
4005 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004007 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02004008 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03004009 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004010 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004011 case 0x90 ... 0x9f: /* setcc r/m8 */
4012 c->dst.val = test_cc(c->b, ctxt->eflags);
4013 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004014 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03004015 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004016 break;
4017 case 0xa1: /* pop fs */
4018 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004019 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004020 case 0xa3:
4021 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08004022 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004023 /* only subword offset */
4024 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02004025 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004026 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004027 case 0xa4: /* shld imm8, r, r/m */
4028 case 0xa5: /* shld cl, r, r/m */
4029 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
4030 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004031 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03004032 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004033 break;
4034 case 0xa9: /* pop gs */
4035 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004036 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004037 case 0xab:
4038 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004039 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004040 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004041 case 0xac: /* shrd imm8, r, r/m */
4042 case 0xad: /* shrd cl, r, r/m */
4043 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
4044 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004045 case 0xae: /* clflush */
4046 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004047 case 0xb0 ... 0xb1: /* cmpxchg */
4048 /*
4049 * Save real source value, then compare EAX against
4050 * destination.
4051 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004052 c->src.orig_val = c->src.val;
4053 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02004054 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
4055 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004057 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004058 } else {
4059 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004060 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03004061 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 }
4063 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004064 case 0xb2: /* lss */
4065 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004066 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 case 0xb3:
4068 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004069 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004071 case 0xb4: /* lfs */
4072 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004073 break;
4074 case 0xb5: /* lgs */
4075 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004076 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004078 c->dst.bytes = c->op_bytes;
4079 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
4080 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004082 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004083 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 case 0:
4085 goto bt;
4086 case 1:
4087 goto bts;
4088 case 2:
4089 goto btr;
4090 case 3:
4091 goto btc;
4092 }
4093 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004094 case 0xbb:
4095 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004096 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004097 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004098 case 0xbc: { /* bsf */
4099 u8 zf;
4100 __asm__ ("bsf %2, %0; setz %1"
4101 : "=r"(c->dst.val), "=q"(zf)
4102 : "r"(c->src.val));
4103 ctxt->eflags &= ~X86_EFLAGS_ZF;
4104 if (zf) {
4105 ctxt->eflags |= X86_EFLAGS_ZF;
4106 c->dst.type = OP_NONE; /* Disable writeback. */
4107 }
4108 break;
4109 }
4110 case 0xbd: { /* bsr */
4111 u8 zf;
4112 __asm__ ("bsr %2, %0; setz %1"
4113 : "=r"(c->dst.val), "=q"(zf)
4114 : "r"(c->src.val));
4115 ctxt->eflags &= ~X86_EFLAGS_ZF;
4116 if (zf) {
4117 ctxt->eflags |= X86_EFLAGS_ZF;
4118 c->dst.type = OP_NONE; /* Disable writeback. */
4119 }
4120 break;
4121 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004123 c->dst.bytes = c->op_bytes;
4124 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4125 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004127 case 0xc0 ... 0xc1: /* xadd */
4128 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4129 /* Write back the register source. */
4130 c->src.val = c->dst.orig_val;
4131 write_register_operand(&c->src);
4132 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004133 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004134 c->dst.bytes = c->op_bytes;
4135 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4136 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004137 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004139 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004140 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004141 default:
4142 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004144
4145 if (rc != X86EMUL_CONTINUE)
4146 goto done;
4147
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148 goto writeback;
4149
4150cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151 return -1;
4152}