blob: 2ec69bc85846bfd68e480e1ea4426c7f3b7e85d4 [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 Kivity56697682011-04-03 14:08:51 +0300467static u32 desc_limit_scaled(struct desc_struct *desc)
468{
469 u32 limit = get_desc_limit(desc);
470
471 return desc->g ? (limit << 12) | 0xfff : limit;
472}
473
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300474static void set_seg_override(struct decode_cache *c, int seg)
475{
476 c->has_seg_override = true;
477 c->seg_override = seg;
478}
479
Gleb Natapov79168fd2010-04-28 19:15:30 +0300480static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
481 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300482{
483 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
484 return 0;
485
Gleb Natapov79168fd2010-04-28 19:15:30 +0300486 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300487}
488
Avi Kivity90de84f2010-11-17 15:28:21 +0200489static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
490 struct x86_emulate_ops *ops,
491 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300492{
493 if (!c->has_seg_override)
494 return 0;
495
Avi Kivity90de84f2010-11-17 15:28:21 +0200496 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300497}
498
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200499static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
500 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300501{
Avi Kivityda9cb572010-11-22 17:53:21 +0200502 ctxt->exception.vector = vec;
503 ctxt->exception.error_code = error;
504 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200505 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300506}
507
Joerg Roedel3b88e412011-04-04 12:39:29 +0200508static int emulate_db(struct x86_emulate_ctxt *ctxt)
509{
510 return emulate_exception(ctxt, DB_VECTOR, 0, false);
511}
512
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200513static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300514{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200515 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300516}
517
Avi Kivity618ff152011-04-03 12:32:09 +0300518static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
519{
520 return emulate_exception(ctxt, SS_VECTOR, err, true);
521}
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 Kivity52fd8b42011-04-03 12:33:12 +0300543static int linearize(struct x86_emulate_ctxt *ctxt,
544 struct segmented_address addr,
545 unsigned size, bool write,
546 ulong *linear)
547{
548 struct decode_cache *c = &ctxt->decode;
Avi Kivity618ff152011-04-03 12:32:09 +0300549 struct desc_struct desc;
550 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300551 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300552 u32 lim;
553 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300554
555 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300556 switch (ctxt->mode) {
557 case X86EMUL_MODE_REAL:
558 break;
559 case X86EMUL_MODE_PROT64:
560 if (((signed long)la << 16) >> 16 != la)
561 return emulate_gp(ctxt, 0);
562 break;
563 default:
564 usable = ctxt->ops->get_cached_descriptor(&desc, NULL, addr.seg,
565 ctxt->vcpu);
566 if (!usable)
567 goto bad;
568 /* code segment or read-only data segment */
569 if (((desc.type & 8) || !(desc.type & 2)) && write)
570 goto bad;
571 /* unreadable code segment */
572 if ((desc.type & 8) && !(desc.type & 2))
573 goto bad;
574 lim = desc_limit_scaled(&desc);
575 if ((desc.type & 8) || !(desc.type & 4)) {
576 /* expand-up segment */
577 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
578 goto bad;
579 } else {
580 /* exapand-down segment */
581 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
582 goto bad;
583 lim = desc.d ? 0xffffffff : 0xffff;
584 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
585 goto bad;
586 }
587 cpl = ctxt->ops->cpl(ctxt->vcpu);
588 rpl = ctxt->ops->get_segment_selector(addr.seg, ctxt->vcpu) & 3;
589 cpl = max(cpl, rpl);
590 if (!(desc.type & 8)) {
591 /* data segment */
592 if (cpl > desc.dpl)
593 goto bad;
594 } else if ((desc.type & 8) && !(desc.type & 4)) {
595 /* nonconforming code segment */
596 if (cpl != desc.dpl)
597 goto bad;
598 } else if ((desc.type & 8) && (desc.type & 4)) {
599 /* conforming code segment */
600 if (cpl < desc.dpl)
601 goto bad;
602 }
603 break;
604 }
Avi Kivity52fd8b42011-04-03 12:33:12 +0300605 if (c->ad_bytes != 8)
606 la &= (u32)-1;
607 *linear = la;
608 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300609bad:
610 if (addr.seg == VCPU_SREG_SS)
611 return emulate_ss(ctxt, addr.seg);
612 else
613 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300614}
615
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200616static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
617 struct segmented_address addr,
618 void *data,
619 unsigned size)
620{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200621 int rc;
622 ulong linear;
623
Avi Kivity83b87952011-04-03 11:31:19 +0300624 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200625 if (rc != X86EMUL_CONTINUE)
626 return rc;
627 return ctxt->ops->read_std(linear, data, size, ctxt->vcpu,
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200628 &ctxt->exception);
629}
630
Avi Kivity62266862007-11-20 13:15:52 +0200631static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
632 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300633 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200634{
635 struct fetch_cache *fc = &ctxt->decode.fetch;
636 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300637 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200638
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300639 if (eip == fc->end) {
640 cur_size = fc->end - fc->start;
641 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
642 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200643 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900644 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200645 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300646 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200647 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300648 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900649 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200650}
651
652static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
653 struct x86_emulate_ops *ops,
654 unsigned long eip, void *dest, unsigned size)
655{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900656 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200657
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200658 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200659 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200660 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200661 while (size--) {
662 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900663 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200664 return rc;
665 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900666 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200667}
668
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000669/*
670 * Given the 'reg' portion of a ModRM byte, and a register block, return a
671 * pointer into the block that addresses the relevant register.
672 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
673 */
674static void *decode_register(u8 modrm_reg, unsigned long *regs,
675 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676{
677 void *p;
678
679 p = &regs[modrm_reg];
680 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
681 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
682 return p;
683}
684
685static int read_descriptor(struct x86_emulate_ctxt *ctxt,
686 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200687 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 u16 *size, unsigned long *address, int op_bytes)
689{
690 int rc;
691
692 if (op_bytes == 2)
693 op_bytes = 3;
694 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200695 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900696 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200698 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200699 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 return rc;
701}
702
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300703static int test_cc(unsigned int condition, unsigned int flags)
704{
705 int rc = 0;
706
707 switch ((condition & 15) >> 1) {
708 case 0: /* o */
709 rc |= (flags & EFLG_OF);
710 break;
711 case 1: /* b/c/nae */
712 rc |= (flags & EFLG_CF);
713 break;
714 case 2: /* z/e */
715 rc |= (flags & EFLG_ZF);
716 break;
717 case 3: /* be/na */
718 rc |= (flags & (EFLG_CF|EFLG_ZF));
719 break;
720 case 4: /* s */
721 rc |= (flags & EFLG_SF);
722 break;
723 case 5: /* p/pe */
724 rc |= (flags & EFLG_PF);
725 break;
726 case 7: /* le/ng */
727 rc |= (flags & EFLG_ZF);
728 /* fall through */
729 case 6: /* l/nge */
730 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
731 break;
732 }
733
734 /* Odd condition identifiers (lsb == 1) have inverted sense. */
735 return (!!rc ^ (condition & 1));
736}
737
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300738static void fetch_register_operand(struct operand *op)
739{
740 switch (op->bytes) {
741 case 1:
742 op->val = *(u8 *)op->addr.reg;
743 break;
744 case 2:
745 op->val = *(u16 *)op->addr.reg;
746 break;
747 case 4:
748 op->val = *(u32 *)op->addr.reg;
749 break;
750 case 8:
751 op->val = *(u64 *)op->addr.reg;
752 break;
753 }
754}
755
Avi Kivity12537912011-03-29 11:41:27 +0200756static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
757{
758 ctxt->ops->get_fpu(ctxt);
759 switch (reg) {
760 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
761 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
762 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
763 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
764 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
765 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
766 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
767 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
768#ifdef CONFIG_X86_64
769 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
770 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
771 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
772 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
773 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
774 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
775 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
776 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
777#endif
778 default: BUG();
779 }
780 ctxt->ops->put_fpu(ctxt);
781}
782
783static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
784 int reg)
785{
786 ctxt->ops->get_fpu(ctxt);
787 switch (reg) {
788 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
789 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
790 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
791 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
792 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
793 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
794 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
795 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
796#ifdef CONFIG_X86_64
797 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
798 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
799 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
800 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
801 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
802 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
803 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
804 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
805#endif
806 default: BUG();
807 }
808 ctxt->ops->put_fpu(ctxt);
809}
810
811static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
812 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200813 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200814 int inhibit_bytereg)
815{
Avi Kivity33615aa2007-10-31 11:15:56 +0200816 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200817 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200818
819 if (!(c->d & ModRM))
820 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200821
822 if (c->d & Sse) {
823 op->type = OP_XMM;
824 op->bytes = 16;
825 op->addr.xmm = reg;
826 read_sse_reg(ctxt, &op->vec_val, reg);
827 return;
828 }
829
Avi Kivity3c118e22007-10-31 10:27:04 +0200830 op->type = OP_REG;
831 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300832 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200833 op->bytes = 1;
834 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300835 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200836 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200837 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300838 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200839 op->orig_val = op->val;
840}
841
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200842static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300843 struct x86_emulate_ops *ops,
844 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200845{
846 struct decode_cache *c = &ctxt->decode;
847 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700848 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900849 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300850 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200851
852 if (c->rex_prefix) {
853 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
854 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
855 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
856 }
857
858 c->modrm = insn_fetch(u8, 1, c->eip);
859 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
860 c->modrm_reg |= (c->modrm & 0x38) >> 3;
861 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300862 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200863
864 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300865 op->type = OP_REG;
866 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
867 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300868 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200869 if (c->d & Sse) {
870 op->type = OP_XMM;
871 op->bytes = 16;
872 op->addr.xmm = c->modrm_rm;
873 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
874 return rc;
875 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300876 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200877 return rc;
878 }
879
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300880 op->type = OP_MEM;
881
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200882 if (c->ad_bytes == 2) {
883 unsigned bx = c->regs[VCPU_REGS_RBX];
884 unsigned bp = c->regs[VCPU_REGS_RBP];
885 unsigned si = c->regs[VCPU_REGS_RSI];
886 unsigned di = c->regs[VCPU_REGS_RDI];
887
888 /* 16-bit ModR/M decode. */
889 switch (c->modrm_mod) {
890 case 0:
891 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300892 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200893 break;
894 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300895 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200896 break;
897 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300898 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200899 break;
900 }
901 switch (c->modrm_rm) {
902 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300903 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200904 break;
905 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300906 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200907 break;
908 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300909 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200910 break;
911 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300912 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200913 break;
914 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300915 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200916 break;
917 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300918 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200919 break;
920 case 6:
921 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300922 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200923 break;
924 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300925 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200926 break;
927 }
928 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
929 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300930 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300931 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932 } else {
933 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700934 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 sib = insn_fetch(u8, 1, c->eip);
936 index_reg |= (sib >> 3) & 7;
937 base_reg |= sib & 7;
938 scale = sib >> 6;
939
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700940 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300941 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700942 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300943 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700944 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300945 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700946 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
947 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700948 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700949 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300950 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200951 switch (c->modrm_mod) {
952 case 0:
953 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300954 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200955 break;
956 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300957 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200958 break;
959 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300960 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200961 break;
962 }
963 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200964 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200965done:
966 return rc;
967}
968
969static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300970 struct x86_emulate_ops *ops,
971 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200972{
973 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900974 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200975
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300976 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200977 switch (c->ad_bytes) {
978 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200979 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200980 break;
981 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200982 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200983 break;
984 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200985 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200986 break;
987 }
988done:
989 return rc;
990}
991
Wei Yongjun35c843c2010-08-09 11:34:56 +0800992static void fetch_bit_operand(struct decode_cache *c)
993{
Sheng Yang7129eec2010-09-28 16:33:32 +0800994 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800995
Wei Yongjun3885f182010-08-09 11:37:37 +0800996 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800997 mask = ~(c->dst.bytes * 8 - 1);
998
999 if (c->src.bytes == 2)
1000 sv = (s16)c->src.val & (s16)mask;
1001 else if (c->src.bytes == 4)
1002 sv = (s32)c->src.val & (s32)mask;
1003
Avi Kivity90de84f2010-11-17 15:28:21 +02001004 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001005 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001006
1007 /* only subword offset */
1008 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001009}
1010
Gleb Natapov9de41572010-04-28 19:15:22 +03001011static int read_emulated(struct x86_emulate_ctxt *ctxt,
1012 struct x86_emulate_ops *ops,
1013 unsigned long addr, void *dest, unsigned size)
1014{
1015 int rc;
1016 struct read_cache *mc = &ctxt->decode.mem_read;
1017
1018 while (size) {
1019 int n = min(size, 8u);
1020 size -= n;
1021 if (mc->pos < mc->end)
1022 goto read_cached;
1023
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001024 rc = ops->read_emulated(addr, mc->data + mc->end, n,
1025 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +03001026 if (rc != X86EMUL_CONTINUE)
1027 return rc;
1028 mc->end += n;
1029
1030 read_cached:
1031 memcpy(dest, mc->data + mc->pos, n);
1032 mc->pos += n;
1033 dest += n;
1034 addr += n;
1035 }
1036 return X86EMUL_CONTINUE;
1037}
1038
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001039static int segmented_read(struct x86_emulate_ctxt *ctxt,
1040 struct segmented_address addr,
1041 void *data,
1042 unsigned size)
1043{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001044 int rc;
1045 ulong linear;
1046
Avi Kivity83b87952011-04-03 11:31:19 +03001047 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001048 if (rc != X86EMUL_CONTINUE)
1049 return rc;
1050 return read_emulated(ctxt, ctxt->ops, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001051}
1052
1053static int segmented_write(struct x86_emulate_ctxt *ctxt,
1054 struct segmented_address addr,
1055 const void *data,
1056 unsigned size)
1057{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001058 int rc;
1059 ulong linear;
1060
Avi Kivity83b87952011-04-03 11:31:19 +03001061 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001062 if (rc != X86EMUL_CONTINUE)
1063 return rc;
1064 return ctxt->ops->write_emulated(linear, data, size,
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001065 &ctxt->exception, ctxt->vcpu);
1066}
1067
1068static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1069 struct segmented_address addr,
1070 const void *orig_data, const void *data,
1071 unsigned size)
1072{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001073 int rc;
1074 ulong linear;
1075
Avi Kivity83b87952011-04-03 11:31:19 +03001076 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001077 if (rc != X86EMUL_CONTINUE)
1078 return rc;
1079 return ctxt->ops->cmpxchg_emulated(linear, orig_data, data,
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001080 size, &ctxt->exception, ctxt->vcpu);
1081}
1082
Gleb Natapov7b262e92010-03-18 15:20:27 +02001083static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1084 struct x86_emulate_ops *ops,
1085 unsigned int size, unsigned short port,
1086 void *dest)
1087{
1088 struct read_cache *rc = &ctxt->decode.io_read;
1089
1090 if (rc->pos == rc->end) { /* refill pio read ahead */
1091 struct decode_cache *c = &ctxt->decode;
1092 unsigned int in_page, n;
1093 unsigned int count = c->rep_prefix ?
1094 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1095 in_page = (ctxt->eflags & EFLG_DF) ?
1096 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1097 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1098 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1099 count);
1100 if (n == 0)
1101 n = 1;
1102 rc->pos = rc->end = 0;
1103 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
1104 return 0;
1105 rc->end = n * size;
1106 }
1107
1108 memcpy(dest, rc->data + rc->pos, size);
1109 rc->pos += size;
1110 return 1;
1111}
1112
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001113static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1114 struct x86_emulate_ops *ops,
1115 u16 selector, struct desc_ptr *dt)
1116{
1117 if (selector & 1 << 2) {
1118 struct desc_struct desc;
1119 memset (dt, 0, sizeof *dt);
Gleb Natapov5601d052011-03-07 14:55:06 +02001120 if (!ops->get_cached_descriptor(&desc, NULL, VCPU_SREG_LDTR,
1121 ctxt->vcpu))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001122 return;
1123
1124 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1125 dt->address = get_desc_base(&desc);
1126 } else
1127 ops->get_gdt(dt, ctxt->vcpu);
1128}
1129
1130/* allowed just for 8 bytes segments */
1131static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1132 struct x86_emulate_ops *ops,
1133 u16 selector, struct desc_struct *desc)
1134{
1135 struct desc_ptr dt;
1136 u16 index = selector >> 3;
1137 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001138 ulong addr;
1139
1140 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1141
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001142 if (dt.size < index * 8 + 7)
1143 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001144 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001145 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
1146 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001147
1148 return ret;
1149}
1150
1151/* allowed just for 8 bytes segments */
1152static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1153 struct x86_emulate_ops *ops,
1154 u16 selector, struct desc_struct *desc)
1155{
1156 struct desc_ptr dt;
1157 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001158 ulong addr;
1159 int ret;
1160
1161 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1162
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001163 if (dt.size < index * 8 + 7)
1164 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001165
1166 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001167 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
1168 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001169
1170 return ret;
1171}
1172
Gleb Natapov5601d052011-03-07 14:55:06 +02001173/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001174static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1175 struct x86_emulate_ops *ops,
1176 u16 selector, int seg)
1177{
1178 struct desc_struct seg_desc;
1179 u8 dpl, rpl, cpl;
1180 unsigned err_vec = GP_VECTOR;
1181 u32 err_code = 0;
1182 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1183 int ret;
1184
1185 memset(&seg_desc, 0, sizeof seg_desc);
1186
1187 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1188 || ctxt->mode == X86EMUL_MODE_REAL) {
1189 /* set real mode segment descriptor */
1190 set_desc_base(&seg_desc, selector << 4);
1191 set_desc_limit(&seg_desc, 0xffff);
1192 seg_desc.type = 3;
1193 seg_desc.p = 1;
1194 seg_desc.s = 1;
1195 goto load;
1196 }
1197
1198 /* NULL selector is not valid for TR, CS and SS */
1199 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1200 && null_selector)
1201 goto exception;
1202
1203 /* TR should be in GDT only */
1204 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1205 goto exception;
1206
1207 if (null_selector) /* for NULL selector skip all following checks */
1208 goto load;
1209
1210 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1211 if (ret != X86EMUL_CONTINUE)
1212 return ret;
1213
1214 err_code = selector & 0xfffc;
1215 err_vec = GP_VECTOR;
1216
1217 /* can't load system descriptor into segment selecor */
1218 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1219 goto exception;
1220
1221 if (!seg_desc.p) {
1222 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1223 goto exception;
1224 }
1225
1226 rpl = selector & 3;
1227 dpl = seg_desc.dpl;
1228 cpl = ops->cpl(ctxt->vcpu);
1229
1230 switch (seg) {
1231 case VCPU_SREG_SS:
1232 /*
1233 * segment is not a writable data segment or segment
1234 * selector's RPL != CPL or segment selector's RPL != CPL
1235 */
1236 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1237 goto exception;
1238 break;
1239 case VCPU_SREG_CS:
1240 if (!(seg_desc.type & 8))
1241 goto exception;
1242
1243 if (seg_desc.type & 4) {
1244 /* conforming */
1245 if (dpl > cpl)
1246 goto exception;
1247 } else {
1248 /* nonconforming */
1249 if (rpl > cpl || dpl != cpl)
1250 goto exception;
1251 }
1252 /* CS(RPL) <- CPL */
1253 selector = (selector & 0xfffc) | cpl;
1254 break;
1255 case VCPU_SREG_TR:
1256 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1257 goto exception;
1258 break;
1259 case VCPU_SREG_LDTR:
1260 if (seg_desc.s || seg_desc.type != 2)
1261 goto exception;
1262 break;
1263 default: /* DS, ES, FS, or GS */
1264 /*
1265 * segment is not a data or readable code segment or
1266 * ((segment is a data or nonconforming code segment)
1267 * and (both RPL and CPL > DPL))
1268 */
1269 if ((seg_desc.type & 0xa) == 0x8 ||
1270 (((seg_desc.type & 0xc) != 0xc) &&
1271 (rpl > dpl && cpl > dpl)))
1272 goto exception;
1273 break;
1274 }
1275
1276 if (seg_desc.s) {
1277 /* mark segment as accessed */
1278 seg_desc.type |= 1;
1279 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1280 if (ret != X86EMUL_CONTINUE)
1281 return ret;
1282 }
1283load:
1284 ops->set_segment_selector(selector, seg, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001285 ops->set_cached_descriptor(&seg_desc, 0, seg, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001286 return X86EMUL_CONTINUE;
1287exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001288 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001289 return X86EMUL_PROPAGATE_FAULT;
1290}
1291
Wei Yongjun31be40b2010-08-17 09:17:30 +08001292static void write_register_operand(struct operand *op)
1293{
1294 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1295 switch (op->bytes) {
1296 case 1:
1297 *(u8 *)op->addr.reg = (u8)op->val;
1298 break;
1299 case 2:
1300 *(u16 *)op->addr.reg = (u16)op->val;
1301 break;
1302 case 4:
1303 *op->addr.reg = (u32)op->val;
1304 break; /* 64b: zero-extend */
1305 case 8:
1306 *op->addr.reg = op->val;
1307 break;
1308 }
1309}
1310
Wei Yongjunc37eda12010-06-15 09:03:33 +08001311static inline int writeback(struct x86_emulate_ctxt *ctxt,
1312 struct x86_emulate_ops *ops)
1313{
1314 int rc;
1315 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001316
1317 switch (c->dst.type) {
1318 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001319 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001320 break;
1321 case OP_MEM:
1322 if (c->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001323 rc = segmented_cmpxchg(ctxt,
1324 c->dst.addr.mem,
1325 &c->dst.orig_val,
1326 &c->dst.val,
1327 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001328 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001329 rc = segmented_write(ctxt,
1330 c->dst.addr.mem,
1331 &c->dst.val,
1332 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001333 if (rc != X86EMUL_CONTINUE)
1334 return rc;
1335 break;
Avi Kivity12537912011-03-29 11:41:27 +02001336 case OP_XMM:
1337 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1338 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001339 case OP_NONE:
1340 /* no writeback */
1341 break;
1342 default:
1343 break;
1344 }
1345 return X86EMUL_CONTINUE;
1346}
1347
Gleb Natapov79168fd2010-04-28 19:15:30 +03001348static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1349 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001350{
1351 struct decode_cache *c = &ctxt->decode;
1352
1353 c->dst.type = OP_MEM;
1354 c->dst.bytes = c->op_bytes;
1355 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001356 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001357 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1358 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001359}
1360
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001361static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001362 struct x86_emulate_ops *ops,
1363 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001364{
1365 struct decode_cache *c = &ctxt->decode;
1366 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001367 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001368
Avi Kivity90de84f2010-11-17 15:28:21 +02001369 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1370 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001371 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001372 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001373 return rc;
1374
Avi Kivity350f69d2009-01-05 11:12:40 +02001375 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001376 return rc;
1377}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001378
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001379static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1380 struct x86_emulate_ops *ops,
1381 void *dest, int len)
1382{
1383 int rc;
1384 unsigned long val, change_mask;
1385 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001386 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001387
1388 rc = emulate_pop(ctxt, ops, &val, len);
1389 if (rc != X86EMUL_CONTINUE)
1390 return rc;
1391
1392 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1393 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1394
1395 switch(ctxt->mode) {
1396 case X86EMUL_MODE_PROT64:
1397 case X86EMUL_MODE_PROT32:
1398 case X86EMUL_MODE_PROT16:
1399 if (cpl == 0)
1400 change_mask |= EFLG_IOPL;
1401 if (cpl <= iopl)
1402 change_mask |= EFLG_IF;
1403 break;
1404 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001405 if (iopl < 3)
1406 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001407 change_mask |= EFLG_IF;
1408 break;
1409 default: /* real mode */
1410 change_mask |= (EFLG_IOPL | EFLG_IF);
1411 break;
1412 }
1413
1414 *(unsigned long *)dest =
1415 (ctxt->eflags & ~change_mask) | (val & change_mask);
1416
1417 return rc;
1418}
1419
Gleb Natapov79168fd2010-04-28 19:15:30 +03001420static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1421 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001422{
1423 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001424
Gleb Natapov79168fd2010-04-28 19:15:30 +03001425 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001426
Gleb Natapov79168fd2010-04-28 19:15:30 +03001427 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001428}
1429
1430static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1431 struct x86_emulate_ops *ops, int seg)
1432{
1433 struct decode_cache *c = &ctxt->decode;
1434 unsigned long selector;
1435 int rc;
1436
1437 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001438 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001439 return rc;
1440
Gleb Natapov2e873022010-03-18 15:20:18 +02001441 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001442 return rc;
1443}
1444
Wei Yongjunc37eda12010-06-15 09:03:33 +08001445static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001446 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001447{
1448 struct decode_cache *c = &ctxt->decode;
1449 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001450 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001451 int reg = VCPU_REGS_RAX;
1452
1453 while (reg <= VCPU_REGS_RDI) {
1454 (reg == VCPU_REGS_RSP) ?
1455 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1456
Gleb Natapov79168fd2010-04-28 19:15:30 +03001457 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001458
1459 rc = writeback(ctxt, ops);
1460 if (rc != X86EMUL_CONTINUE)
1461 return rc;
1462
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001463 ++reg;
1464 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001465
1466 /* Disable writeback. */
1467 c->dst.type = OP_NONE;
1468
1469 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001470}
1471
1472static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1473 struct x86_emulate_ops *ops)
1474{
1475 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001476 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001477 int reg = VCPU_REGS_RDI;
1478
1479 while (reg >= VCPU_REGS_RAX) {
1480 if (reg == VCPU_REGS_RSP) {
1481 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1482 c->op_bytes);
1483 --reg;
1484 }
1485
1486 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001487 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001488 break;
1489 --reg;
1490 }
1491 return rc;
1492}
1493
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001494int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1495 struct x86_emulate_ops *ops, int irq)
1496{
1497 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001498 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001499 struct desc_ptr dt;
1500 gva_t cs_addr;
1501 gva_t eip_addr;
1502 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001503
1504 /* TODO: Add limit checks */
1505 c->src.val = ctxt->eflags;
1506 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001507 rc = writeback(ctxt, ops);
1508 if (rc != X86EMUL_CONTINUE)
1509 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001510
1511 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1512
1513 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1514 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001515 rc = writeback(ctxt, ops);
1516 if (rc != X86EMUL_CONTINUE)
1517 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001518
1519 c->src.val = c->eip;
1520 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001521 rc = writeback(ctxt, ops);
1522 if (rc != X86EMUL_CONTINUE)
1523 return rc;
1524
1525 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001526
1527 ops->get_idt(&dt, ctxt->vcpu);
1528
1529 eip_addr = dt.address + (irq << 2);
1530 cs_addr = dt.address + (irq << 2) + 2;
1531
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001532 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001533 if (rc != X86EMUL_CONTINUE)
1534 return rc;
1535
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001536 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001537 if (rc != X86EMUL_CONTINUE)
1538 return rc;
1539
1540 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1541 if (rc != X86EMUL_CONTINUE)
1542 return rc;
1543
1544 c->eip = eip;
1545
1546 return rc;
1547}
1548
1549static int emulate_int(struct x86_emulate_ctxt *ctxt,
1550 struct x86_emulate_ops *ops, int irq)
1551{
1552 switch(ctxt->mode) {
1553 case X86EMUL_MODE_REAL:
1554 return emulate_int_real(ctxt, ops, irq);
1555 case X86EMUL_MODE_VM86:
1556 case X86EMUL_MODE_PROT16:
1557 case X86EMUL_MODE_PROT32:
1558 case X86EMUL_MODE_PROT64:
1559 default:
1560 /* Protected mode interrupts unimplemented yet */
1561 return X86EMUL_UNHANDLEABLE;
1562 }
1563}
1564
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001565static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1566 struct x86_emulate_ops *ops)
1567{
1568 struct decode_cache *c = &ctxt->decode;
1569 int rc = X86EMUL_CONTINUE;
1570 unsigned long temp_eip = 0;
1571 unsigned long temp_eflags = 0;
1572 unsigned long cs = 0;
1573 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1574 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1575 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1576 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1577
1578 /* TODO: Add stack limit check */
1579
1580 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1581
1582 if (rc != X86EMUL_CONTINUE)
1583 return rc;
1584
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001585 if (temp_eip & ~0xffff)
1586 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001587
1588 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1589
1590 if (rc != X86EMUL_CONTINUE)
1591 return rc;
1592
1593 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1594
1595 if (rc != X86EMUL_CONTINUE)
1596 return rc;
1597
1598 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1599
1600 if (rc != X86EMUL_CONTINUE)
1601 return rc;
1602
1603 c->eip = temp_eip;
1604
1605
1606 if (c->op_bytes == 4)
1607 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1608 else if (c->op_bytes == 2) {
1609 ctxt->eflags &= ~0xffff;
1610 ctxt->eflags |= temp_eflags;
1611 }
1612
1613 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1614 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1615
1616 return rc;
1617}
1618
1619static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1620 struct x86_emulate_ops* ops)
1621{
1622 switch(ctxt->mode) {
1623 case X86EMUL_MODE_REAL:
1624 return emulate_iret_real(ctxt, ops);
1625 case X86EMUL_MODE_VM86:
1626 case X86EMUL_MODE_PROT16:
1627 case X86EMUL_MODE_PROT32:
1628 case X86EMUL_MODE_PROT64:
1629 default:
1630 /* iret from protected mode unimplemented yet */
1631 return X86EMUL_UNHANDLEABLE;
1632 }
1633}
1634
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001635static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1636 struct x86_emulate_ops *ops)
1637{
1638 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001639
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001640 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001641}
1642
Laurent Vivier05f086f2007-09-24 11:10:55 +02001643static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001644{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001645 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001646 switch (c->modrm_reg) {
1647 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001648 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001649 break;
1650 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001651 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001652 break;
1653 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001654 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001655 break;
1656 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001657 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001658 break;
1659 case 4: /* sal/shl */
1660 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001661 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001662 break;
1663 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001664 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665 break;
1666 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001667 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001668 break;
1669 }
1670}
1671
1672static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001673 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001674{
1675 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001676 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1677 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001678 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001679
1680 switch (c->modrm_reg) {
1681 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001682 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 break;
1684 case 2: /* not */
1685 c->dst.val = ~c->dst.val;
1686 break;
1687 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001688 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001689 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001690 case 4: /* mul */
1691 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1692 break;
1693 case 5: /* imul */
1694 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1695 break;
1696 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001697 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1698 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001699 break;
1700 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001701 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1702 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001703 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001704 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001705 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001707 if (de)
1708 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001709 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710}
1711
1712static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001713 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714{
1715 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716
1717 switch (c->modrm_reg) {
1718 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001719 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001720 break;
1721 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001722 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001723 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001724 case 2: /* call near abs */ {
1725 long int old_eip;
1726 old_eip = c->eip;
1727 c->eip = c->src.val;
1728 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001729 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001730 break;
1731 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001732 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001733 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001734 break;
1735 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001736 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001738 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001739 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001740}
1741
1742static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001743 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001744{
1745 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001746 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747
1748 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1749 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001750 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1751 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001752 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001753 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001754 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1755 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001756
Laurent Vivier05f086f2007-09-24 11:10:55 +02001757 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001758 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001759 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001760}
1761
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001762static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1763 struct x86_emulate_ops *ops)
1764{
1765 struct decode_cache *c = &ctxt->decode;
1766 int rc;
1767 unsigned long cs;
1768
1769 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001770 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001771 return rc;
1772 if (c->op_bytes == 4)
1773 c->eip = (u32)c->eip;
1774 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001775 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001776 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001777 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001778 return rc;
1779}
1780
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001781static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1782 struct x86_emulate_ops *ops, int seg)
1783{
1784 struct decode_cache *c = &ctxt->decode;
1785 unsigned short sel;
1786 int rc;
1787
1788 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1789
1790 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1791 if (rc != X86EMUL_CONTINUE)
1792 return rc;
1793
1794 c->dst.val = c->src.val;
1795 return rc;
1796}
1797
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001798static inline void
1799setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001800 struct x86_emulate_ops *ops, struct desc_struct *cs,
1801 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001802{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001803 memset(cs, 0, sizeof(struct desc_struct));
Gleb Natapov5601d052011-03-07 14:55:06 +02001804 ops->get_cached_descriptor(cs, NULL, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001805 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001806
1807 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001808 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001809 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001810 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001811 cs->type = 0x0b; /* Read, Execute, Accessed */
1812 cs->s = 1;
1813 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001814 cs->p = 1;
1815 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001816
Gleb Natapov79168fd2010-04-28 19:15:30 +03001817 set_desc_base(ss, 0); /* flat segment */
1818 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001819 ss->g = 1; /* 4kb granularity */
1820 ss->s = 1;
1821 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001822 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001823 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001824 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001825}
1826
1827static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001828emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001829{
1830 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001831 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001832 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001833 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001834
1835 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001836 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001837 ctxt->mode == X86EMUL_MODE_VM86)
1838 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001839
Gleb Natapov79168fd2010-04-28 19:15:30 +03001840 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001841
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001842 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001843 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001844 cs_sel = (u16)(msr_data & 0xfffc);
1845 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001846
1847 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001848 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001849 cs.l = 1;
1850 }
Gleb Natapov5601d052011-03-07 14:55:06 +02001851 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001852 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001853 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001854 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001855
1856 c->regs[VCPU_REGS_RCX] = c->eip;
1857 if (is_long_mode(ctxt->vcpu)) {
1858#ifdef CONFIG_X86_64
1859 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1860
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001861 ops->get_msr(ctxt->vcpu,
1862 ctxt->mode == X86EMUL_MODE_PROT64 ?
1863 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001864 c->eip = msr_data;
1865
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001866 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001867 ctxt->eflags &= ~(msr_data | EFLG_RF);
1868#endif
1869 } else {
1870 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001871 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001872 c->eip = (u32)msr_data;
1873
1874 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1875 }
1876
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001877 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001878}
1879
Andre Przywara8c604352009-06-18 12:56:01 +02001880static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001881emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001882{
1883 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001884 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001885 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001886 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001887
Gleb Natapova0044752010-02-10 14:21:31 +02001888 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001889 if (ctxt->mode == X86EMUL_MODE_REAL)
1890 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001891
1892 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1893 * Therefore, we inject an #UD.
1894 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001895 if (ctxt->mode == X86EMUL_MODE_PROT64)
1896 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001897
Gleb Natapov79168fd2010-04-28 19:15:30 +03001898 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001899
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001900 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001901 switch (ctxt->mode) {
1902 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001903 if ((msr_data & 0xfffc) == 0x0)
1904 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001905 break;
1906 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001907 if (msr_data == 0x0)
1908 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001909 break;
1910 }
1911
1912 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001913 cs_sel = (u16)msr_data;
1914 cs_sel &= ~SELECTOR_RPL_MASK;
1915 ss_sel = cs_sel + 8;
1916 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001917 if (ctxt->mode == X86EMUL_MODE_PROT64
1918 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001919 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001920 cs.l = 1;
1921 }
1922
Gleb Natapov5601d052011-03-07 14:55:06 +02001923 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001924 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001925 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001926 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001927
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001928 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001929 c->eip = msr_data;
1930
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001931 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001932 c->regs[VCPU_REGS_RSP] = msr_data;
1933
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001934 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001935}
1936
Andre Przywara4668f052009-06-18 12:56:02 +02001937static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001938emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001939{
1940 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001941 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001942 u64 msr_data;
1943 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001944 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001945
Gleb Natapova0044752010-02-10 14:21:31 +02001946 /* inject #GP if in real mode or Virtual 8086 mode */
1947 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001948 ctxt->mode == X86EMUL_MODE_VM86)
1949 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001950
Gleb Natapov79168fd2010-04-28 19:15:30 +03001951 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001952
1953 if ((c->rex_prefix & 0x8) != 0x0)
1954 usermode = X86EMUL_MODE_PROT64;
1955 else
1956 usermode = X86EMUL_MODE_PROT32;
1957
1958 cs.dpl = 3;
1959 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001960 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001961 switch (usermode) {
1962 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001963 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001964 if ((msr_data & 0xfffc) == 0x0)
1965 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001966 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001967 break;
1968 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001969 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001970 if (msr_data == 0x0)
1971 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001972 ss_sel = cs_sel + 8;
1973 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001974 cs.l = 1;
1975 break;
1976 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001977 cs_sel |= SELECTOR_RPL_MASK;
1978 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001979
Gleb Natapov5601d052011-03-07 14:55:06 +02001980 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001981 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001982 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001983 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001984
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001985 c->eip = c->regs[VCPU_REGS_RDX];
1986 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001987
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001988 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001989}
1990
Gleb Natapov9c537242010-03-18 15:20:05 +02001991static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1992 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001993{
1994 int iopl;
1995 if (ctxt->mode == X86EMUL_MODE_REAL)
1996 return false;
1997 if (ctxt->mode == X86EMUL_MODE_VM86)
1998 return true;
1999 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02002000 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002001}
2002
2003static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2004 struct x86_emulate_ops *ops,
2005 u16 port, u16 len)
2006{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002007 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002008 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002009 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02002010 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002011 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002012 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002013
Gleb Natapov5601d052011-03-07 14:55:06 +02002014 ops->get_cached_descriptor(&tr_seg, &base3, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002015 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002016 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002017 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002018 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002019 base = get_desc_base(&tr_seg);
2020#ifdef CONFIG_X86_64
2021 base |= ((u64)base3) << 32;
2022#endif
2023 r = ops->read_std(base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002024 if (r != X86EMUL_CONTINUE)
2025 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002026 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002027 return false;
Gleb Natapov399a40c2011-03-07 14:55:07 +02002028 r = ops->read_std(base + io_bitmap_ptr + port/8, &perm, 2, ctxt->vcpu,
Gleb Natapov5601d052011-03-07 14:55:06 +02002029 NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002030 if (r != X86EMUL_CONTINUE)
2031 return false;
2032 if ((perm >> bit_idx) & mask)
2033 return false;
2034 return true;
2035}
2036
2037static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2038 struct x86_emulate_ops *ops,
2039 u16 port, u16 len)
2040{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002041 if (ctxt->perm_ok)
2042 return true;
2043
Gleb Natapov9c537242010-03-18 15:20:05 +02002044 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002045 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2046 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002047
2048 ctxt->perm_ok = true;
2049
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002050 return true;
2051}
2052
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002053static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2054 struct x86_emulate_ops *ops,
2055 struct tss_segment_16 *tss)
2056{
2057 struct decode_cache *c = &ctxt->decode;
2058
2059 tss->ip = c->eip;
2060 tss->flag = ctxt->eflags;
2061 tss->ax = c->regs[VCPU_REGS_RAX];
2062 tss->cx = c->regs[VCPU_REGS_RCX];
2063 tss->dx = c->regs[VCPU_REGS_RDX];
2064 tss->bx = c->regs[VCPU_REGS_RBX];
2065 tss->sp = c->regs[VCPU_REGS_RSP];
2066 tss->bp = c->regs[VCPU_REGS_RBP];
2067 tss->si = c->regs[VCPU_REGS_RSI];
2068 tss->di = c->regs[VCPU_REGS_RDI];
2069
2070 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2071 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2072 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2073 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2074 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2075}
2076
2077static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2078 struct x86_emulate_ops *ops,
2079 struct tss_segment_16 *tss)
2080{
2081 struct decode_cache *c = &ctxt->decode;
2082 int ret;
2083
2084 c->eip = tss->ip;
2085 ctxt->eflags = tss->flag | 2;
2086 c->regs[VCPU_REGS_RAX] = tss->ax;
2087 c->regs[VCPU_REGS_RCX] = tss->cx;
2088 c->regs[VCPU_REGS_RDX] = tss->dx;
2089 c->regs[VCPU_REGS_RBX] = tss->bx;
2090 c->regs[VCPU_REGS_RSP] = tss->sp;
2091 c->regs[VCPU_REGS_RBP] = tss->bp;
2092 c->regs[VCPU_REGS_RSI] = tss->si;
2093 c->regs[VCPU_REGS_RDI] = tss->di;
2094
2095 /*
2096 * SDM says that segment selectors are loaded before segment
2097 * descriptors
2098 */
2099 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
2100 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2101 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2102 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2103 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2104
2105 /*
2106 * Now load segment descriptors. If fault happenes at this stage
2107 * it is handled in a context of new task
2108 */
2109 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2110 if (ret != X86EMUL_CONTINUE)
2111 return ret;
2112 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2113 if (ret != X86EMUL_CONTINUE)
2114 return ret;
2115 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2116 if (ret != X86EMUL_CONTINUE)
2117 return ret;
2118 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2119 if (ret != X86EMUL_CONTINUE)
2120 return ret;
2121 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2122 if (ret != X86EMUL_CONTINUE)
2123 return ret;
2124
2125 return X86EMUL_CONTINUE;
2126}
2127
2128static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2129 struct x86_emulate_ops *ops,
2130 u16 tss_selector, u16 old_tss_sel,
2131 ulong old_tss_base, struct desc_struct *new_desc)
2132{
2133 struct tss_segment_16 tss_seg;
2134 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002135 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002136
2137 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002138 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002139 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002140 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002141 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002142
2143 save_state_to_tss16(ctxt, ops, &tss_seg);
2144
2145 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002146 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002147 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002149 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002150
2151 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002152 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002153 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002155 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002156
2157 if (old_tss_sel != 0xffff) {
2158 tss_seg.prev_task_link = old_tss_sel;
2159
2160 ret = ops->write_std(new_tss_base,
2161 &tss_seg.prev_task_link,
2162 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002163 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002164 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002166 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 }
2168
2169 return load_state_from_tss16(ctxt, ops, &tss_seg);
2170}
2171
2172static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2173 struct x86_emulate_ops *ops,
2174 struct tss_segment_32 *tss)
2175{
2176 struct decode_cache *c = &ctxt->decode;
2177
2178 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2179 tss->eip = c->eip;
2180 tss->eflags = ctxt->eflags;
2181 tss->eax = c->regs[VCPU_REGS_RAX];
2182 tss->ecx = c->regs[VCPU_REGS_RCX];
2183 tss->edx = c->regs[VCPU_REGS_RDX];
2184 tss->ebx = c->regs[VCPU_REGS_RBX];
2185 tss->esp = c->regs[VCPU_REGS_RSP];
2186 tss->ebp = c->regs[VCPU_REGS_RBP];
2187 tss->esi = c->regs[VCPU_REGS_RSI];
2188 tss->edi = c->regs[VCPU_REGS_RDI];
2189
2190 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2191 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2192 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2193 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2194 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2195 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2196 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2197}
2198
2199static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2200 struct x86_emulate_ops *ops,
2201 struct tss_segment_32 *tss)
2202{
2203 struct decode_cache *c = &ctxt->decode;
2204 int ret;
2205
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002206 if (ops->set_cr(3, tss->cr3, ctxt->vcpu))
2207 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002208 c->eip = tss->eip;
2209 ctxt->eflags = tss->eflags | 2;
2210 c->regs[VCPU_REGS_RAX] = tss->eax;
2211 c->regs[VCPU_REGS_RCX] = tss->ecx;
2212 c->regs[VCPU_REGS_RDX] = tss->edx;
2213 c->regs[VCPU_REGS_RBX] = tss->ebx;
2214 c->regs[VCPU_REGS_RSP] = tss->esp;
2215 c->regs[VCPU_REGS_RBP] = tss->ebp;
2216 c->regs[VCPU_REGS_RSI] = tss->esi;
2217 c->regs[VCPU_REGS_RDI] = tss->edi;
2218
2219 /*
2220 * SDM says that segment selectors are loaded before segment
2221 * descriptors
2222 */
2223 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2224 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2225 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2226 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2227 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2228 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2229 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2230
2231 /*
2232 * Now load segment descriptors. If fault happenes at this stage
2233 * it is handled in a context of new task
2234 */
2235 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2236 if (ret != X86EMUL_CONTINUE)
2237 return ret;
2238 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2239 if (ret != X86EMUL_CONTINUE)
2240 return ret;
2241 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2242 if (ret != X86EMUL_CONTINUE)
2243 return ret;
2244 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2245 if (ret != X86EMUL_CONTINUE)
2246 return ret;
2247 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2248 if (ret != X86EMUL_CONTINUE)
2249 return ret;
2250 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2251 if (ret != X86EMUL_CONTINUE)
2252 return ret;
2253 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2254 if (ret != X86EMUL_CONTINUE)
2255 return ret;
2256
2257 return X86EMUL_CONTINUE;
2258}
2259
2260static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2261 struct x86_emulate_ops *ops,
2262 u16 tss_selector, u16 old_tss_sel,
2263 ulong old_tss_base, struct desc_struct *new_desc)
2264{
2265 struct tss_segment_32 tss_seg;
2266 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002267 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002268
2269 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002270 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002271 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002272 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002273 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002274
2275 save_state_to_tss32(ctxt, ops, &tss_seg);
2276
2277 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002278 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002279 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002280 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002281 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282
2283 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002284 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002285 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002287 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002288
2289 if (old_tss_sel != 0xffff) {
2290 tss_seg.prev_task_link = old_tss_sel;
2291
2292 ret = ops->write_std(new_tss_base,
2293 &tss_seg.prev_task_link,
2294 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002295 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002296 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002297 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002299 }
2300
2301 return load_state_from_tss32(ctxt, ops, &tss_seg);
2302}
2303
2304static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002305 struct x86_emulate_ops *ops,
2306 u16 tss_selector, int reason,
2307 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002308{
2309 struct desc_struct curr_tss_desc, next_tss_desc;
2310 int ret;
2311 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2312 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002313 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002314 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002315
2316 /* FIXME: old_tss_base == ~0 ? */
2317
2318 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2319 if (ret != X86EMUL_CONTINUE)
2320 return ret;
2321 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2322 if (ret != X86EMUL_CONTINUE)
2323 return ret;
2324
2325 /* FIXME: check that next_tss_desc is tss */
2326
2327 if (reason != TASK_SWITCH_IRET) {
2328 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002329 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl)
2330 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002331 }
2332
Gleb Natapovceffb452010-03-18 15:20:19 +02002333 desc_limit = desc_limit_scaled(&next_tss_desc);
2334 if (!next_tss_desc.p ||
2335 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2336 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002337 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002338 return X86EMUL_PROPAGATE_FAULT;
2339 }
2340
2341 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2342 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2343 write_segment_descriptor(ctxt, ops, old_tss_sel,
2344 &curr_tss_desc);
2345 }
2346
2347 if (reason == TASK_SWITCH_IRET)
2348 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2349
2350 /* set back link to prev task only if NT bit is set in eflags
2351 note that old_tss_sel is not used afetr this point */
2352 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2353 old_tss_sel = 0xffff;
2354
2355 if (next_tss_desc.type & 8)
2356 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2357 old_tss_base, &next_tss_desc);
2358 else
2359 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2360 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002361 if (ret != X86EMUL_CONTINUE)
2362 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002363
2364 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2365 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2366
2367 if (reason != TASK_SWITCH_IRET) {
2368 next_tss_desc.type |= (1 << 1); /* set busy flag */
2369 write_segment_descriptor(ctxt, ops, tss_selector,
2370 &next_tss_desc);
2371 }
2372
2373 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02002374 ops->set_cached_descriptor(&next_tss_desc, 0, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2376
Jan Kiszkae269fb22010-04-14 15:51:09 +02002377 if (has_error_code) {
2378 struct decode_cache *c = &ctxt->decode;
2379
2380 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2381 c->lock_prefix = 0;
2382 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002383 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002384 }
2385
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002386 return ret;
2387}
2388
2389int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002390 u16 tss_selector, int reason,
2391 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002392{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002393 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002394 struct decode_cache *c = &ctxt->decode;
2395 int rc;
2396
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002397 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002398 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002399
Jan Kiszkae269fb22010-04-14 15:51:09 +02002400 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2401 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402
2403 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002404 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002405 if (rc == X86EMUL_CONTINUE)
2406 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407 }
2408
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002409 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002410}
2411
Avi Kivity90de84f2010-11-17 15:28:21 +02002412static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002413 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002414{
2415 struct decode_cache *c = &ctxt->decode;
2416 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2417
Gleb Natapovd9271122010-03-18 15:20:22 +02002418 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002419 op->addr.mem.ea = register_address(c, c->regs[reg]);
2420 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002421}
2422
Avi Kivity63540382010-07-29 15:11:55 +03002423static int em_push(struct x86_emulate_ctxt *ctxt)
2424{
2425 emulate_push(ctxt, ctxt->ops);
2426 return X86EMUL_CONTINUE;
2427}
2428
Avi Kivity7af04fc2010-08-18 14:16:35 +03002429static int em_das(struct x86_emulate_ctxt *ctxt)
2430{
2431 struct decode_cache *c = &ctxt->decode;
2432 u8 al, old_al;
2433 bool af, cf, old_cf;
2434
2435 cf = ctxt->eflags & X86_EFLAGS_CF;
2436 al = c->dst.val;
2437
2438 old_al = al;
2439 old_cf = cf;
2440 cf = false;
2441 af = ctxt->eflags & X86_EFLAGS_AF;
2442 if ((al & 0x0f) > 9 || af) {
2443 al -= 6;
2444 cf = old_cf | (al >= 250);
2445 af = true;
2446 } else {
2447 af = false;
2448 }
2449 if (old_al > 0x99 || old_cf) {
2450 al -= 0x60;
2451 cf = true;
2452 }
2453
2454 c->dst.val = al;
2455 /* Set PF, ZF, SF */
2456 c->src.type = OP_IMM;
2457 c->src.val = 0;
2458 c->src.bytes = 1;
2459 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2460 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2461 if (cf)
2462 ctxt->eflags |= X86_EFLAGS_CF;
2463 if (af)
2464 ctxt->eflags |= X86_EFLAGS_AF;
2465 return X86EMUL_CONTINUE;
2466}
2467
Avi Kivity0ef753b2010-08-18 14:51:45 +03002468static int em_call_far(struct x86_emulate_ctxt *ctxt)
2469{
2470 struct decode_cache *c = &ctxt->decode;
2471 u16 sel, old_cs;
2472 ulong old_eip;
2473 int rc;
2474
2475 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2476 old_eip = c->eip;
2477
2478 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2479 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2480 return X86EMUL_CONTINUE;
2481
2482 c->eip = 0;
2483 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2484
2485 c->src.val = old_cs;
2486 emulate_push(ctxt, ctxt->ops);
2487 rc = writeback(ctxt, ctxt->ops);
2488 if (rc != X86EMUL_CONTINUE)
2489 return rc;
2490
2491 c->src.val = old_eip;
2492 emulate_push(ctxt, ctxt->ops);
2493 rc = writeback(ctxt, ctxt->ops);
2494 if (rc != X86EMUL_CONTINUE)
2495 return rc;
2496
2497 c->dst.type = OP_NONE;
2498
2499 return X86EMUL_CONTINUE;
2500}
2501
Avi Kivity40ece7c2010-08-18 15:12:09 +03002502static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2503{
2504 struct decode_cache *c = &ctxt->decode;
2505 int rc;
2506
2507 c->dst.type = OP_REG;
2508 c->dst.addr.reg = &c->eip;
2509 c->dst.bytes = c->op_bytes;
2510 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2511 if (rc != X86EMUL_CONTINUE)
2512 return rc;
2513 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2514 return X86EMUL_CONTINUE;
2515}
2516
Avi Kivity5c82aa22010-08-18 18:31:43 +03002517static int em_imul(struct x86_emulate_ctxt *ctxt)
2518{
2519 struct decode_cache *c = &ctxt->decode;
2520
2521 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2522 return X86EMUL_CONTINUE;
2523}
2524
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002525static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2526{
2527 struct decode_cache *c = &ctxt->decode;
2528
2529 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002530 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002531}
2532
Avi Kivity61429142010-08-19 15:13:00 +03002533static int em_cwd(struct x86_emulate_ctxt *ctxt)
2534{
2535 struct decode_cache *c = &ctxt->decode;
2536
2537 c->dst.type = OP_REG;
2538 c->dst.bytes = c->src.bytes;
2539 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2540 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2541
2542 return X86EMUL_CONTINUE;
2543}
2544
Avi Kivity48bb5d32010-08-18 18:54:34 +03002545static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2546{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002547 struct decode_cache *c = &ctxt->decode;
2548 u64 tsc = 0;
2549
Avi Kivity48bb5d32010-08-18 18:54:34 +03002550 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2551 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2552 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2553 return X86EMUL_CONTINUE;
2554}
2555
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002556static int em_mov(struct x86_emulate_ctxt *ctxt)
2557{
2558 struct decode_cache *c = &ctxt->decode;
2559 c->dst.val = c->src.val;
2560 return X86EMUL_CONTINUE;
2561}
2562
Avi Kivityaa97bb42010-01-20 18:09:23 +02002563static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2564{
2565 struct decode_cache *c = &ctxt->decode;
2566 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2567 return X86EMUL_CONTINUE;
2568}
2569
Avi Kivity38503912011-03-31 18:48:09 +02002570static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2571{
2572 struct decode_cache *c = &ctxt->decode;
Avi Kivity9fa088f2011-03-31 18:54:30 +02002573 int rc;
2574 ulong linear;
2575
Avi Kivity83b87952011-04-03 11:31:19 +03002576 rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002577 if (rc == X86EMUL_CONTINUE)
2578 emulate_invlpg(ctxt->vcpu, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002579 /* Disable writeback. */
2580 c->dst.type = OP_NONE;
2581 return X86EMUL_CONTINUE;
2582}
2583
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002584static bool valid_cr(int nr)
2585{
2586 switch (nr) {
2587 case 0:
2588 case 2 ... 4:
2589 case 8:
2590 return true;
2591 default:
2592 return false;
2593 }
2594}
2595
2596static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2597{
2598 struct decode_cache *c = &ctxt->decode;
2599
2600 if (!valid_cr(c->modrm_reg))
2601 return emulate_ud(ctxt);
2602
2603 return X86EMUL_CONTINUE;
2604}
2605
2606static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2607{
2608 struct decode_cache *c = &ctxt->decode;
2609 u64 new_val = c->src.val64;
2610 int cr = c->modrm_reg;
2611
2612 static u64 cr_reserved_bits[] = {
2613 0xffffffff00000000ULL,
2614 0, 0, 0, /* CR3 checked later */
2615 CR4_RESERVED_BITS,
2616 0, 0, 0,
2617 CR8_RESERVED_BITS,
2618 };
2619
2620 if (!valid_cr(cr))
2621 return emulate_ud(ctxt);
2622
2623 if (new_val & cr_reserved_bits[cr])
2624 return emulate_gp(ctxt, 0);
2625
2626 switch (cr) {
2627 case 0: {
2628 u64 cr4, efer;
2629 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2630 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2631 return emulate_gp(ctxt, 0);
2632
2633 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2634 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2635
2636 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2637 !(cr4 & X86_CR4_PAE))
2638 return emulate_gp(ctxt, 0);
2639
2640 break;
2641 }
2642 case 3: {
2643 u64 rsvd = 0;
2644
2645 if (is_long_mode(ctxt->vcpu))
2646 rsvd = CR3_L_MODE_RESERVED_BITS;
2647 else if (is_pae(ctxt->vcpu))
2648 rsvd = CR3_PAE_RESERVED_BITS;
2649 else if (is_paging(ctxt->vcpu))
2650 rsvd = CR3_NONPAE_RESERVED_BITS;
2651
2652 if (new_val & rsvd)
2653 return emulate_gp(ctxt, 0);
2654
2655 break;
2656 }
2657 case 4: {
2658 u64 cr4, efer;
2659
2660 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2661 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2662
2663 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2664 return emulate_gp(ctxt, 0);
2665
2666 break;
2667 }
2668 }
2669
2670 return X86EMUL_CONTINUE;
2671}
2672
Joerg Roedel3b88e412011-04-04 12:39:29 +02002673static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2674{
2675 unsigned long dr7;
2676
2677 ctxt->ops->get_dr(7, &dr7, ctxt->vcpu);
2678
2679 /* Check if DR7.Global_Enable is set */
2680 return dr7 & (1 << 13);
2681}
2682
2683static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2684{
2685 struct decode_cache *c = &ctxt->decode;
2686 int dr = c->modrm_reg;
2687 u64 cr4;
2688
2689 if (dr > 7)
2690 return emulate_ud(ctxt);
2691
2692 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2693 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2694 return emulate_ud(ctxt);
2695
2696 if (check_dr7_gd(ctxt))
2697 return emulate_db(ctxt);
2698
2699 return X86EMUL_CONTINUE;
2700}
2701
2702static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2703{
2704 struct decode_cache *c = &ctxt->decode;
2705 u64 new_val = c->src.val64;
2706 int dr = c->modrm_reg;
2707
2708 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2709 return emulate_gp(ctxt, 0);
2710
2711 return check_dr_read(ctxt);
2712}
2713
Joerg Roedel01de8b02011-04-04 12:39:31 +02002714static int check_svme(struct x86_emulate_ctxt *ctxt)
2715{
2716 u64 efer;
2717
2718 ctxt->ops->get_msr(ctxt->vcpu, MSR_EFER, &efer);
2719
2720 if (!(efer & EFER_SVME))
2721 return emulate_ud(ctxt);
2722
2723 return X86EMUL_CONTINUE;
2724}
2725
2726static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2727{
2728 u64 rax = kvm_register_read(ctxt->vcpu, VCPU_REGS_RAX);
2729
2730 /* Valid physical address? */
2731 if (rax & 0xffff000000000000)
2732 return emulate_gp(ctxt, 0);
2733
2734 return check_svme(ctxt);
2735}
2736
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002737static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2738{
2739 u64 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2740
2741 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt->vcpu))
2742 return emulate_ud(ctxt);
2743
2744 return X86EMUL_CONTINUE;
2745}
2746
Joerg Roedel80612522011-04-04 12:39:33 +02002747static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2748{
2749 u64 cr4 = ctxt->ops->get_cr(4, ctxt->vcpu);
2750 u64 rcx = kvm_register_read(ctxt->vcpu, VCPU_REGS_RCX);
2751
2752 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt->vcpu)) ||
2753 (rcx > 3))
2754 return emulate_gp(ctxt, 0);
2755
2756 return X86EMUL_CONTINUE;
2757}
2758
Joerg Roedelf6511932011-04-04 12:39:35 +02002759static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2760{
2761 struct decode_cache *c = &ctxt->decode;
2762
2763 c->dst.bytes = min(c->dst.bytes, 4u);
2764 if (!emulator_io_permited(ctxt, ctxt->ops, c->src.val, c->dst.bytes))
2765 return emulate_gp(ctxt, 0);
2766
2767 return X86EMUL_CONTINUE;
2768}
2769
2770static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2771{
2772 struct decode_cache *c = &ctxt->decode;
2773
2774 c->src.bytes = min(c->src.bytes, 4u);
2775 if (!emulator_io_permited(ctxt, ctxt->ops, c->dst.val, c->src.bytes))
2776 return emulate_gp(ctxt, 0);
2777
2778 return X86EMUL_CONTINUE;
2779}
2780
Avi Kivity73fba5f2010-07-29 15:11:53 +03002781#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002782#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002783#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2784 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002785#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002786#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002787#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2788#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2789#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002790#define II(_f, _e, _i) \
2791 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002792#define IIP(_f, _e, _i, _p) \
2793 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2794 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002795#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002796
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002797#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002798#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002799#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2800
Avi Kivity6230f7f2010-08-26 18:34:55 +03002801#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2802 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2803 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2804
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002805static struct opcode group7_rm1[] = {
2806 DI(SrcNone | ModRM | Priv, monitor),
2807 DI(SrcNone | ModRM | Priv, mwait),
2808 N, N, N, N, N, N,
2809};
2810
Joerg Roedel01de8b02011-04-04 12:39:31 +02002811static struct opcode group7_rm3[] = {
2812 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivitybfeed292011-04-05 16:25:20 +03002813 DI(SrcNone | ModRM | Prot | VendorSpecific, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002814 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
2815 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
2816 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
2817 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
2818 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
2819 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
2820};
Avi Kivity6230f7f2010-08-26 18:34:55 +03002821
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002822static struct opcode group7_rm7[] = {
2823 N,
2824 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
2825 N, N, N, N, N, N,
2826};
Avi Kivity73fba5f2010-07-29 15:11:53 +03002827static struct opcode group1[] = {
2828 X7(D(Lock)), N
2829};
2830
2831static struct opcode group1A[] = {
2832 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2833};
2834
2835static struct opcode group3[] = {
2836 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2837 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002838 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002839};
2840
2841static struct opcode group4[] = {
2842 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2843 N, N, N, N, N, N,
2844};
2845
2846static struct opcode group5[] = {
2847 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002848 D(SrcMem | ModRM | Stack),
2849 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002850 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2851 D(SrcMem | ModRM | Stack), N,
2852};
2853
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002854static struct opcode group6[] = {
2855 DI(ModRM | Prot, sldt),
2856 DI(ModRM | Prot, str),
2857 DI(ModRM | Prot | Priv, lldt),
2858 DI(ModRM | Prot | Priv, ltr),
2859 N, N, N, N,
2860};
2861
Avi Kivity73fba5f2010-07-29 15:11:53 +03002862static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002863 DI(ModRM | Mov | DstMem | Priv, sgdt),
2864 DI(ModRM | Mov | DstMem | Priv, sidt),
2865 DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002866 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2867 DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
2868 DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002869}, {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002870 D(SrcNone | ModRM | Priv | VendorSpecific), EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002871 N, EXT(0, group7_rm3),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002872 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002873 DI(SrcMem16 | ModRM | Mov | Priv, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002874} };
2875
2876static struct opcode group8[] = {
2877 N, N, N, N,
2878 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2879 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2880};
2881
2882static struct group_dual group9 = { {
2883 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2884}, {
2885 N, N, N, N, N, N, N, N,
2886} };
2887
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002888static struct opcode group11[] = {
2889 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2890};
2891
Avi Kivityaa97bb42010-01-20 18:09:23 +02002892static struct gprefix pfx_0f_6f_0f_7f = {
2893 N, N, N, I(Sse, em_movdqu),
2894};
2895
Avi Kivity73fba5f2010-07-29 15:11:53 +03002896static struct opcode opcode_table[256] = {
2897 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002898 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002899 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2900 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002901 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002902 D(ImplicitOps | Stack | No64), N,
2903 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002904 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002905 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2906 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002907 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002908 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2909 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002910 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002911 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002912 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002913 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002914 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002915 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002916 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002917 /* 0x40 - 0x4F */
2918 X16(D(DstReg)),
2919 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002920 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002921 /* 0x58 - 0x5F */
2922 X8(D(DstReg | Stack)),
2923 /* 0x60 - 0x67 */
2924 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2925 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2926 N, N, N, N,
2927 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002928 I(SrcImm | Mov | Stack, em_push),
2929 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002930 I(SrcImmByte | Mov | Stack, em_push),
2931 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Joerg Roedelf6511932011-04-04 12:39:35 +02002932 D2bvIP(DstDI | Mov | String, ins, check_perm_in), /* insb, insw/insd */
2933 D2bvIP(SrcSI | ImplicitOps | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002934 /* 0x70 - 0x7F */
2935 X16(D(SrcImmByte)),
2936 /* 0x80 - 0x87 */
2937 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2938 G(DstMem | SrcImm | ModRM | Group, group1),
2939 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2940 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002941 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002942 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002943 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2944 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002945 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002946 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2947 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02002948 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002949 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002950 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002951 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002952 DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002953 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002954 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2955 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2956 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2957 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002958 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002959 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002960 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2961 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002962 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002963 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002964 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002965 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002966 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002967 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002968 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002969 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2970 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002971 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002972 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002973 /* 0xC8 - 0xCF */
2974 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002975 D(ImplicitOps), DI(SrcImmByte, intn),
2976 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002977 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002978 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002979 N, N, N, N,
2980 /* 0xD8 - 0xDF */
2981 N, N, N, N, N, N, N, N,
2982 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002983 X4(D(SrcImmByte)),
Joerg Roedelf6511932011-04-04 12:39:35 +02002984 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
2985 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002986 /* 0xE8 - 0xEF */
2987 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2988 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Joerg Roedelf6511932011-04-04 12:39:35 +02002989 D2bvIP(SrcNone | DstAcc, in, check_perm_in),
2990 D2bvIP(SrcAcc | ImplicitOps, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002991 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02002992 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002993 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
2994 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002995 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002996 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002997 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2998};
2999
3000static struct opcode twobyte_table[256] = {
3001 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003002 G(0, group6), GD(0, &group7), N, N,
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003003 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003004 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003005 N, D(ImplicitOps | ModRM), N, N,
3006 /* 0x10 - 0x1F */
3007 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3008 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003009 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003010 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003011 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003012 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003013 N, N, N, N,
3014 N, N, N, N, N, N, N, N,
3015 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003016 DI(ImplicitOps | Priv, wrmsr),
3017 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3018 DI(ImplicitOps | Priv, rdmsr),
3019 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Avi Kivityd8671622011-02-01 16:32:03 +02003020 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
3021 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003022 N, N, N, N, N, N, N, N,
3023 /* 0x40 - 0x4F */
3024 X16(D(DstReg | SrcMem | ModRM | Mov)),
3025 /* 0x50 - 0x5F */
3026 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3027 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003028 N, N, N, N,
3029 N, N, N, N,
3030 N, N, N, N,
3031 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003032 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003033 N, N, N, N,
3034 N, N, N, N,
3035 N, N, N, N,
3036 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003037 /* 0x80 - 0x8F */
3038 X16(D(SrcImm)),
3039 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003040 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003041 /* 0xA0 - 0xA7 */
3042 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003043 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003044 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3045 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3046 /* 0xA8 - 0xAF */
3047 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003048 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003049 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3050 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003051 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003052 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003053 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003054 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3055 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3056 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003057 /* 0xB8 - 0xBF */
3058 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003059 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003060 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3061 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003062 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003063 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003064 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003065 N, N, N, GD(0, &group9),
3066 N, N, N, N, N, N, N, N,
3067 /* 0xD0 - 0xDF */
3068 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3069 /* 0xE0 - 0xEF */
3070 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3071 /* 0xF0 - 0xFF */
3072 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3073};
3074
3075#undef D
3076#undef N
3077#undef G
3078#undef GD
3079#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003080#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003081#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003082
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003083#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003084#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003085#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03003086#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003087
Avi Kivity39f21ee2010-08-18 19:20:21 +03003088static unsigned imm_size(struct decode_cache *c)
3089{
3090 unsigned size;
3091
3092 size = (c->d & ByteOp) ? 1 : c->op_bytes;
3093 if (size == 8)
3094 size = 4;
3095 return size;
3096}
3097
3098static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3099 unsigned size, bool sign_extension)
3100{
3101 struct decode_cache *c = &ctxt->decode;
3102 struct x86_emulate_ops *ops = ctxt->ops;
3103 int rc = X86EMUL_CONTINUE;
3104
3105 op->type = OP_IMM;
3106 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02003107 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003108 /* NB. Immediates are sign-extended as necessary. */
3109 switch (op->bytes) {
3110 case 1:
3111 op->val = insn_fetch(s8, 1, c->eip);
3112 break;
3113 case 2:
3114 op->val = insn_fetch(s16, 2, c->eip);
3115 break;
3116 case 4:
3117 op->val = insn_fetch(s32, 4, c->eip);
3118 break;
3119 }
3120 if (!sign_extension) {
3121 switch (op->bytes) {
3122 case 1:
3123 op->val &= 0xff;
3124 break;
3125 case 2:
3126 op->val &= 0xffff;
3127 break;
3128 case 4:
3129 op->val &= 0xffffffff;
3130 break;
3131 }
3132 }
3133done:
3134 return rc;
3135}
3136
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003137int
Andre Przywaradc25e892010-12-21 11:12:07 +01003138x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003139{
3140 struct x86_emulate_ops *ops = ctxt->ops;
3141 struct decode_cache *c = &ctxt->decode;
3142 int rc = X86EMUL_CONTINUE;
3143 int mode = ctxt->mode;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003144 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
3145 bool op_prefix = false;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003146 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003147 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003148
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003149 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01003150 c->fetch.start = c->eip;
3151 c->fetch.end = c->fetch.start + insn_len;
3152 if (insn_len > 0)
3153 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003154 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
3155
3156 switch (mode) {
3157 case X86EMUL_MODE_REAL:
3158 case X86EMUL_MODE_VM86:
3159 case X86EMUL_MODE_PROT16:
3160 def_op_bytes = def_ad_bytes = 2;
3161 break;
3162 case X86EMUL_MODE_PROT32:
3163 def_op_bytes = def_ad_bytes = 4;
3164 break;
3165#ifdef CONFIG_X86_64
3166 case X86EMUL_MODE_PROT64:
3167 def_op_bytes = 4;
3168 def_ad_bytes = 8;
3169 break;
3170#endif
3171 default:
3172 return -1;
3173 }
3174
3175 c->op_bytes = def_op_bytes;
3176 c->ad_bytes = def_ad_bytes;
3177
3178 /* Legacy prefixes. */
3179 for (;;) {
3180 switch (c->b = insn_fetch(u8, 1, c->eip)) {
3181 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003182 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003183 /* switch between 2/4 bytes */
3184 c->op_bytes = def_op_bytes ^ 6;
3185 break;
3186 case 0x67: /* address-size override */
3187 if (mode == X86EMUL_MODE_PROT64)
3188 /* switch between 4/8 bytes */
3189 c->ad_bytes = def_ad_bytes ^ 12;
3190 else
3191 /* switch between 2/4 bytes */
3192 c->ad_bytes = def_ad_bytes ^ 6;
3193 break;
3194 case 0x26: /* ES override */
3195 case 0x2e: /* CS override */
3196 case 0x36: /* SS override */
3197 case 0x3e: /* DS override */
3198 set_seg_override(c, (c->b >> 3) & 3);
3199 break;
3200 case 0x64: /* FS override */
3201 case 0x65: /* GS override */
3202 set_seg_override(c, c->b & 7);
3203 break;
3204 case 0x40 ... 0x4f: /* REX */
3205 if (mode != X86EMUL_MODE_PROT64)
3206 goto done_prefixes;
3207 c->rex_prefix = c->b;
3208 continue;
3209 case 0xf0: /* LOCK */
3210 c->lock_prefix = 1;
3211 break;
3212 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003213 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02003214 c->rep_prefix = c->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003215 break;
3216 default:
3217 goto done_prefixes;
3218 }
3219
3220 /* Any legacy prefix after a REX prefix nullifies its effect. */
3221
3222 c->rex_prefix = 0;
3223 }
3224
3225done_prefixes:
3226
3227 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03003228 if (c->rex_prefix & 8)
3229 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003230
3231 /* Opcode byte(s). */
3232 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003233 /* Two-byte opcode? */
3234 if (c->b == 0x0f) {
3235 c->twobyte = 1;
3236 c->b = insn_fetch(u8, 1, c->eip);
3237 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003238 }
3239 c->d = opcode.flags;
3240
3241 if (c->d & Group) {
3242 dual = c->d & GroupDual;
3243 c->modrm = insn_fetch(u8, 1, c->eip);
3244 --c->eip;
3245
3246 if (c->d & GroupDual) {
3247 g_mod012 = opcode.u.gdual->mod012;
3248 g_mod3 = opcode.u.gdual->mod3;
3249 } else
3250 g_mod012 = g_mod3 = opcode.u.group;
3251
3252 c->d &= ~(Group | GroupDual);
3253
3254 goffset = (c->modrm >> 3) & 7;
3255
3256 if ((c->modrm >> 6) == 3)
3257 opcode = g_mod3[goffset];
3258 else
3259 opcode = g_mod012[goffset];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003260
3261 if (opcode.flags & RMExt) {
3262 goffset = c->modrm & 7;
3263 opcode = opcode.u.group[goffset];
3264 }
3265
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003266 c->d |= opcode.flags;
3267 }
3268
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003269 if (c->d & Prefix) {
3270 if (c->rep_prefix && op_prefix)
3271 return X86EMUL_UNHANDLEABLE;
3272 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3273 switch (simd_prefix) {
3274 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3275 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3276 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3277 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3278 }
3279 c->d |= opcode.flags;
3280 }
3281
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003282 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02003283 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02003284 c->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003285
3286 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02003287 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003288 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003289
Avi Kivityd8671622011-02-01 16:32:03 +02003290 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3291 return -1;
3292
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003293 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3294 c->op_bytes = 8;
3295
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003296 if (c->d & Op3264) {
3297 if (mode == X86EMUL_MODE_PROT64)
3298 c->op_bytes = 8;
3299 else
3300 c->op_bytes = 4;
3301 }
3302
Avi Kivity12537912011-03-29 11:41:27 +02003303 if (c->d & Sse)
3304 c->op_bytes = 16;
3305
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003306 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03003307 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003308 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03003309 if (!c->has_seg_override)
3310 set_seg_override(c, c->modrm_seg);
3311 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003312 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003313 if (rc != X86EMUL_CONTINUE)
3314 goto done;
3315
3316 if (!c->has_seg_override)
3317 set_seg_override(c, VCPU_SREG_DS);
3318
Avi Kivity90de84f2010-11-17 15:28:21 +02003319 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003320
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003321 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003322 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003323
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003324 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02003325 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003326
3327 /*
3328 * Decode and fetch the source operand: register, memory
3329 * or immediate.
3330 */
3331 switch (c->d & SrcMask) {
3332 case SrcNone:
3333 break;
3334 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02003335 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003336 break;
3337 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003338 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003339 goto srcmem_common;
3340 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003341 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003342 goto srcmem_common;
3343 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003344 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003345 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003346 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003347 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003348 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003349 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003350 rc = decode_imm(ctxt, &c->src, 2, false);
3351 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003352 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003353 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3354 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003355 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003356 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003357 break;
3358 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003359 rc = decode_imm(ctxt, &c->src, 1, true);
3360 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003361 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003362 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003363 break;
3364 case SrcAcc:
3365 c->src.type = OP_REG;
3366 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003367 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003368 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003369 break;
3370 case SrcOne:
3371 c->src.bytes = 1;
3372 c->src.val = 1;
3373 break;
3374 case SrcSI:
3375 c->src.type = OP_MEM;
3376 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003377 c->src.addr.mem.ea =
3378 register_address(c, c->regs[VCPU_REGS_RSI]);
3379 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003380 c->src.val = 0;
3381 break;
3382 case SrcImmFAddr:
3383 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003384 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003385 c->src.bytes = c->op_bytes + 2;
3386 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3387 break;
3388 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003389 memop.bytes = c->op_bytes + 2;
3390 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003391 break;
3392 }
3393
Avi Kivity39f21ee2010-08-18 19:20:21 +03003394 if (rc != X86EMUL_CONTINUE)
3395 goto done;
3396
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003397 /*
3398 * Decode and fetch the second source operand: register, memory
3399 * or immediate.
3400 */
3401 switch (c->d & Src2Mask) {
3402 case Src2None:
3403 break;
3404 case Src2CL:
3405 c->src2.bytes = 1;
3406 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3407 break;
3408 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003409 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003410 break;
3411 case Src2One:
3412 c->src2.bytes = 1;
3413 c->src2.val = 1;
3414 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003415 case Src2Imm:
3416 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3417 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003418 }
3419
Avi Kivity39f21ee2010-08-18 19:20:21 +03003420 if (rc != X86EMUL_CONTINUE)
3421 goto done;
3422
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003423 /* Decode and fetch the destination operand: register or memory. */
3424 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003425 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003426 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003427 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3428 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003429 case DstImmUByte:
3430 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003431 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003432 c->dst.bytes = 1;
3433 c->dst.val = insn_fetch(u8, 1, c->eip);
3434 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003435 case DstMem:
3436 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003437 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003438 if ((c->d & DstMask) == DstMem64)
3439 c->dst.bytes = 8;
3440 else
3441 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003442 if (c->d & BitOp)
3443 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003444 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003445 break;
3446 case DstAcc:
3447 c->dst.type = OP_REG;
3448 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003449 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003450 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003451 c->dst.orig_val = c->dst.val;
3452 break;
3453 case DstDI:
3454 c->dst.type = OP_MEM;
3455 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003456 c->dst.addr.mem.ea =
3457 register_address(c, c->regs[VCPU_REGS_RDI]);
3458 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003459 c->dst.val = 0;
3460 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003461 case ImplicitOps:
3462 /* Special instructions do their own operand decoding. */
3463 default:
3464 c->dst.type = OP_NONE; /* Disable writeback. */
3465 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003466 }
3467
3468done:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003469 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003470}
3471
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003472static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3473{
3474 struct decode_cache *c = &ctxt->decode;
3475
3476 /* The second termination condition only applies for REPE
3477 * and REPNE. Test if the repeat string operation prefix is
3478 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3479 * corresponding termination condition according to:
3480 * - if REPE/REPZ and ZF = 0 then done
3481 * - if REPNE/REPNZ and ZF = 1 then done
3482 */
3483 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3484 (c->b == 0xae) || (c->b == 0xaf))
3485 && (((c->rep_prefix == REPE_PREFIX) &&
3486 ((ctxt->eflags & EFLG_ZF) == 0))
3487 || ((c->rep_prefix == REPNE_PREFIX) &&
3488 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3489 return true;
3490
3491 return false;
3492}
3493
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003494int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003495x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003496{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003497 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003498 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003499 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003500 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003501 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003502 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003503
Gleb Natapov9de41572010-04-28 19:15:22 +03003504 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003505
Gleb Natapov11616242010-02-11 14:43:14 +02003506 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003507 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003508 goto done;
3509 }
3510
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003511 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003512 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003513 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003514 goto done;
3515 }
3516
Avi Kivity081bca02010-08-26 11:06:15 +03003517 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003518 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003519 goto done;
3520 }
3521
Avi Kivity12537912011-03-29 11:41:27 +02003522 if ((c->d & Sse)
3523 && ((ops->get_cr(0, ctxt->vcpu) & X86_CR0_EM)
3524 || !(ops->get_cr(4, ctxt->vcpu) & X86_CR4_OSFXSR))) {
3525 rc = emulate_ud(ctxt);
3526 goto done;
3527 }
3528
3529 if ((c->d & Sse) && (ops->get_cr(0, ctxt->vcpu) & X86_CR0_TS)) {
3530 rc = emulate_nm(ctxt);
3531 goto done;
3532 }
3533
Avi Kivityc4f035c2011-04-04 12:39:22 +02003534 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003535 rc = emulator_check_intercept(ctxt, c->intercept,
3536 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003537 if (rc != X86EMUL_CONTINUE)
3538 goto done;
3539 }
3540
Gleb Natapove92805a2010-02-10 14:21:35 +02003541 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003542 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003543 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003544 goto done;
3545 }
3546
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003547 /* Instruction can only be executed in protected mode */
3548 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3549 rc = emulate_ud(ctxt);
3550 goto done;
3551 }
3552
Joerg Roedeld09beab2011-04-04 12:39:25 +02003553 /* Do instruction specific permission checks */
3554 if (c->check_perm) {
3555 rc = c->check_perm(ctxt);
3556 if (rc != X86EMUL_CONTINUE)
3557 goto done;
3558 }
3559
Avi Kivityc4f035c2011-04-04 12:39:22 +02003560 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003561 rc = emulator_check_intercept(ctxt, c->intercept,
3562 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003563 if (rc != X86EMUL_CONTINUE)
3564 goto done;
3565 }
3566
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003567 if (c->rep_prefix && (c->d & String)) {
3568 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003569 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003570 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003571 goto done;
3572 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003573 }
3574
Wei Yongjunc483c022010-08-06 15:36:36 +08003575 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003576 rc = segmented_read(ctxt, c->src.addr.mem,
3577 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003578 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003579 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003580 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003581 }
3582
Gleb Natapove35b7b92010-02-25 16:36:42 +02003583 if (c->src2.type == OP_MEM) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003584 rc = segmented_read(ctxt, c->src2.addr.mem,
3585 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003586 if (rc != X86EMUL_CONTINUE)
3587 goto done;
3588 }
3589
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003590 if ((c->d & DstMask) == ImplicitOps)
3591 goto special_insn;
3592
3593
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003594 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3595 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003596 rc = segmented_read(ctxt, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03003597 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003598 if (rc != X86EMUL_CONTINUE)
3599 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003600 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003601 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003602
Avi Kivity018a98d2007-11-27 19:30:56 +02003603special_insn:
3604
Avi Kivityc4f035c2011-04-04 12:39:22 +02003605 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003606 rc = emulator_check_intercept(ctxt, c->intercept,
3607 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003608 if (rc != X86EMUL_CONTINUE)
3609 goto done;
3610 }
3611
Avi Kivityef65c882010-07-29 15:11:51 +03003612 if (c->execute) {
3613 rc = c->execute(ctxt);
3614 if (rc != X86EMUL_CONTINUE)
3615 goto done;
3616 goto writeback;
3617 }
3618
Laurent Viviere4e03de2007-09-18 11:52:50 +02003619 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620 goto twobyte_insn;
3621
Laurent Viviere4e03de2007-09-18 11:52:50 +02003622 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003623 case 0x00 ... 0x05:
3624 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003625 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003626 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003627 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003628 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003629 break;
3630 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003631 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003632 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 case 0x08 ... 0x0d:
3634 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003635 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003637 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003638 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003639 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640 case 0x10 ... 0x15:
3641 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003642 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003644 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003645 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003646 break;
3647 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003648 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003649 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003650 case 0x18 ... 0x1d:
3651 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003652 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003653 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003654 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003655 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003656 break;
3657 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003658 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003659 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003660 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003661 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003662 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003663 break;
3664 case 0x28 ... 0x2d:
3665 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003666 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003667 break;
3668 case 0x30 ... 0x35:
3669 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003670 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 break;
3672 case 0x38 ... 0x3d:
3673 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003674 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003676 case 0x40 ... 0x47: /* inc r16/r32 */
3677 emulate_1op("inc", c->dst, ctxt->eflags);
3678 break;
3679 case 0x48 ... 0x4f: /* dec r16/r32 */
3680 emulate_1op("dec", c->dst, ctxt->eflags);
3681 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003682 case 0x58 ... 0x5f: /* pop reg */
3683 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003684 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003685 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003686 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003687 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003688 break;
3689 case 0x61: /* popa */
3690 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003691 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003693 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003694 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003695 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003697 case 0x6c: /* insb */
3698 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003699 c->src.val = c->regs[VCPU_REGS_RDX];
3700 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003701 case 0x6e: /* outsb */
3702 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003703 c->dst.val = c->regs[VCPU_REGS_RDX];
3704 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003705 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003706 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003707 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003708 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003709 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003711 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712 case 0:
3713 goto add;
3714 case 1:
3715 goto or;
3716 case 2:
3717 goto adc;
3718 case 3:
3719 goto sbb;
3720 case 4:
3721 goto and;
3722 case 5:
3723 goto sub;
3724 case 6:
3725 goto xor;
3726 case 7:
3727 goto cmp;
3728 }
3729 break;
3730 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003731 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003732 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003733 break;
3734 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003735 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003737 c->src.val = c->dst.val;
3738 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003739 /*
3740 * Write back the memory destination with implicit LOCK
3741 * prefix.
3742 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003743 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003744 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003746 case 0x8c: /* mov r/m, sreg */
3747 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003748 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003749 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003750 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003751 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003752 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003753 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003754 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003755 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003756 case 0x8e: { /* mov seg, r/m16 */
3757 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003758
3759 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003760
Gleb Natapovc6975182010-02-18 12:15:01 +02003761 if (c->modrm_reg == VCPU_SREG_CS ||
3762 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003763 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003764 goto done;
3765 }
3766
Glauber Costa310b5d32009-05-12 16:21:06 -04003767 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003768 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003769
Gleb Natapov2e873022010-03-18 15:20:18 +02003770 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003771
3772 c->dst.type = OP_NONE; /* Disable writeback. */
3773 break;
3774 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003776 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003778 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3779 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003780 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003781 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003782 case 0x98: /* cbw/cwde/cdqe */
3783 switch (c->op_bytes) {
3784 case 2: c->dst.val = (s8)c->dst.val; break;
3785 case 4: c->dst.val = (s16)c->dst.val; break;
3786 case 8: c->dst.val = (s32)c->dst.val; break;
3787 }
3788 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003789 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003790 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003791 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003792 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003793 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003794 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003795 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003796 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003797 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003798 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003800 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003801 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003802 case 0xa8 ... 0xa9: /* test ax, imm */
3803 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003805 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003806 case 0xc0 ... 0xc1:
3807 emulate_grp2(ctxt);
3808 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003809 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003810 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003811 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003812 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003813 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003814 case 0xc4: /* les */
3815 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003816 break;
3817 case 0xc5: /* lds */
3818 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003819 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003820 case 0xcb: /* ret far */
3821 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003822 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003823 case 0xcc: /* int3 */
3824 irq = 3;
3825 goto do_interrupt;
3826 case 0xcd: /* int n */
3827 irq = c->src.val;
3828 do_interrupt:
3829 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003830 break;
3831 case 0xce: /* into */
3832 if (ctxt->eflags & EFLG_OF) {
3833 irq = 4;
3834 goto do_interrupt;
3835 }
3836 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003837 case 0xcf: /* iret */
3838 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003839 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003840 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003841 emulate_grp2(ctxt);
3842 break;
3843 case 0xd2 ... 0xd3: /* Grp2 */
3844 c->src.val = c->regs[VCPU_REGS_RCX];
3845 emulate_grp2(ctxt);
3846 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003847 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3848 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3849 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3850 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3851 jmp_rel(c, c->src.val);
3852 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003853 case 0xe3: /* jcxz/jecxz/jrcxz */
3854 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3855 jmp_rel(c, c->src.val);
3856 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003857 case 0xe4: /* inb */
3858 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003859 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003860 case 0xe6: /* outb */
3861 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003862 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003863 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003864 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003865 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003866 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003867 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003868 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003869 }
3870 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003871 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003872 case 0xea: { /* jmp far */
3873 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003874 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003875 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3876
3877 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003878 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003879
Gleb Natapov414e6272010-04-28 19:15:26 +03003880 c->eip = 0;
3881 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003882 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003883 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003884 case 0xeb:
3885 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003886 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003887 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003888 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003889 case 0xec: /* in al,dx */
3890 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003891 c->src.val = c->regs[VCPU_REGS_RDX];
3892 do_io_in:
Gleb Natapov7b262e92010-03-18 15:20:27 +02003893 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3894 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003895 goto done; /* IO is needed */
3896 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003897 case 0xee: /* out dx,al */
3898 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003899 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003900 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003901 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3902 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003903 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003904 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003905 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003906 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003907 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003908 case 0xf5: /* cmc */
3909 /* complement carry flag from eflags reg */
3910 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003911 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003912 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003913 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003914 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003915 case 0xf8: /* clc */
3916 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003917 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003918 case 0xf9: /* stc */
3919 ctxt->eflags |= EFLG_CF;
3920 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003921 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003922 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003923 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003924 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003925 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003926 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003927 break;
3928 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003929 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003930 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003931 goto done;
3932 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003933 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003934 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003935 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003936 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003937 case 0xfc: /* cld */
3938 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003939 break;
3940 case 0xfd: /* std */
3941 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003942 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003943 case 0xfe: /* Grp4 */
3944 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003945 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003946 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003947 case 0xff: /* Grp5 */
3948 if (c->modrm_reg == 5)
3949 goto jump_far;
3950 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003951 default:
3952 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003954
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003955 if (rc != X86EMUL_CONTINUE)
3956 goto done;
3957
Avi Kivity018a98d2007-11-27 19:30:56 +02003958writeback:
3959 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003960 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003961 goto done;
3962
Gleb Natapov5cd21912010-03-18 15:20:26 +02003963 /*
3964 * restore dst type in case the decoding will be reused
3965 * (happens for string instruction )
3966 */
3967 c->dst.type = saved_dst_type;
3968
Gleb Natapova682e352010-03-18 15:20:21 +02003969 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003970 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003971 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003972
3973 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003974 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003975 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003976
Gleb Natapov5cd21912010-03-18 15:20:26 +02003977 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003978 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003979 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003980
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003981 if (!string_insn_completed(ctxt)) {
3982 /*
3983 * Re-enter guest when pio read ahead buffer is empty
3984 * or, if it is not used, after each 1024 iteration.
3985 */
3986 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3987 (r->end == 0 || r->end != r->pos)) {
3988 /*
3989 * Reset read cache. Usually happens before
3990 * decode, but since instruction is restarted
3991 * we have to do it here.
3992 */
3993 ctxt->decode.mem_read.end = 0;
3994 return EMULATION_RESTART;
3995 }
3996 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003997 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003998 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003999
4000 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004001
4002done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004003 if (rc == X86EMUL_PROPAGATE_FAULT)
4004 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004005 if (rc == X86EMUL_INTERCEPTED)
4006 return EMULATION_INTERCEPTED;
4007
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004008 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004009
4010twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02004011 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004012 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004013 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004014 u16 size;
4015 unsigned long address;
4016
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004017 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004018 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004019 goto cannot_emulate;
4020
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004021 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004022 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004023 goto done;
4024
Avi Kivity33e38852008-05-21 15:34:25 +03004025 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02004026 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03004027 /* Disable writeback. */
4028 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004029 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004030 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03004031 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02004032 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004033 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034 goto done;
4035 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03004036 /* Disable writeback. */
4037 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004039 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02004040 if (c->modrm_mod == 3) {
4041 switch (c->modrm_rm) {
4042 case 1:
4043 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02004044 break;
4045 default:
4046 goto cannot_emulate;
4047 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004048 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03004049 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004050 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02004051 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004052 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004053 goto done;
4054 realmode_lidt(ctxt->vcpu, size, address);
4055 }
Avi Kivity16286d02008-04-14 14:40:50 +03004056 /* Disable writeback. */
4057 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004058 break;
4059 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03004060 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02004061 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 break;
4063 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03004064 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02004065 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03004066 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02004068 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03004069 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02004070 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02004071 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072 case 7: /* invlpg*/
Avi Kivity38503912011-03-31 18:48:09 +02004073 rc = em_invlpg(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 break;
4075 default:
4076 goto cannot_emulate;
4077 }
4078 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02004079 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004080 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004081 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004082 case 0x06:
4083 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02004084 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004085 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004086 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004087 break;
4088 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004089 case 0x0d: /* GrpP (prefetch) */
4090 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004091 break;
4092 case 0x20: /* mov cr, reg */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03004093 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02004094 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 case 0x21: /* mov from dr to reg */
Avi Kivityb27f3852010-08-01 14:25:22 +03004096 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004098 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03004099 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004100 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004101 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004102 goto done;
4103 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004104 c->dst.type = OP_NONE;
4105 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106 case 0x23: /* mov from reg to dr */
Avi Kivityb27f3852010-08-01 14:25:22 +03004107 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004108 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
4109 ~0ULL : ~0U), ctxt->vcpu) < 0) {
4110 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004111 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004112 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004113 goto done;
4114 }
4115
Laurent Viviera01af5e2007-09-24 11:10:56 +02004116 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004118 case 0x30:
4119 /* wrmsr */
4120 msr_data = (u32)c->regs[VCPU_REGS_RAX]
4121 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004122 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004123 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004124 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004125 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004126 }
4127 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004128 break;
4129 case 0x32:
4130 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004131 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004132 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004133 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004134 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004135 } else {
4136 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
4137 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
4138 }
4139 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004140 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02004141 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004142 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004143 break;
4144 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004145 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004146 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004148 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02004149 if (!test_cc(c->b, ctxt->eflags))
4150 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004152 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02004153 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03004154 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004155 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004156 case 0x90 ... 0x9f: /* setcc r/m8 */
4157 c->dst.val = test_cc(c->b, ctxt->eflags);
4158 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004159 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03004160 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004161 break;
4162 case 0xa1: /* pop fs */
4163 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004164 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004165 case 0xa3:
4166 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08004167 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004168 /* only subword offset */
4169 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02004170 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004171 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004172 case 0xa4: /* shld imm8, r, r/m */
4173 case 0xa5: /* shld cl, r, r/m */
4174 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
4175 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004176 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03004177 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004178 break;
4179 case 0xa9: /* pop gs */
4180 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004181 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004182 case 0xab:
4183 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004184 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004185 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004186 case 0xac: /* shrd imm8, r, r/m */
4187 case 0xad: /* shrd cl, r, r/m */
4188 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
4189 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004190 case 0xae: /* clflush */
4191 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004192 case 0xb0 ... 0xb1: /* cmpxchg */
4193 /*
4194 * Save real source value, then compare EAX against
4195 * destination.
4196 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004197 c->src.orig_val = c->src.val;
4198 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02004199 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
4200 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004202 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203 } else {
4204 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004205 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03004206 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207 }
4208 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004209 case 0xb2: /* lss */
4210 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004211 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212 case 0xb3:
4213 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004214 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004216 case 0xb4: /* lfs */
4217 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004218 break;
4219 case 0xb5: /* lgs */
4220 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004221 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004222 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004223 c->dst.bytes = c->op_bytes;
4224 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
4225 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004226 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004227 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004228 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 case 0:
4230 goto bt;
4231 case 1:
4232 goto bts;
4233 case 2:
4234 goto btr;
4235 case 3:
4236 goto btc;
4237 }
4238 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004239 case 0xbb:
4240 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004241 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004242 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004243 case 0xbc: { /* bsf */
4244 u8 zf;
4245 __asm__ ("bsf %2, %0; setz %1"
4246 : "=r"(c->dst.val), "=q"(zf)
4247 : "r"(c->src.val));
4248 ctxt->eflags &= ~X86_EFLAGS_ZF;
4249 if (zf) {
4250 ctxt->eflags |= X86_EFLAGS_ZF;
4251 c->dst.type = OP_NONE; /* Disable writeback. */
4252 }
4253 break;
4254 }
4255 case 0xbd: { /* bsr */
4256 u8 zf;
4257 __asm__ ("bsr %2, %0; setz %1"
4258 : "=r"(c->dst.val), "=q"(zf)
4259 : "r"(c->src.val));
4260 ctxt->eflags &= ~X86_EFLAGS_ZF;
4261 if (zf) {
4262 ctxt->eflags |= X86_EFLAGS_ZF;
4263 c->dst.type = OP_NONE; /* Disable writeback. */
4264 }
4265 break;
4266 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004267 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004268 c->dst.bytes = c->op_bytes;
4269 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4270 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004271 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004272 case 0xc0 ... 0xc1: /* xadd */
4273 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4274 /* Write back the register source. */
4275 c->src.val = c->dst.orig_val;
4276 write_register_operand(&c->src);
4277 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004278 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004279 c->dst.bytes = c->op_bytes;
4280 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4281 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004282 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004283 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004284 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004285 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004286 default:
4287 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004288 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004289
4290 if (rc != X86EMUL_CONTINUE)
4291 goto done;
4292
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293 goto writeback;
4294
4295cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004296 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004297}