blob: 4822824b608b189166727c5e4d495f5c031c6190 [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 Kivityab85b122010-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 Kivityab85b122010-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 Kivityab85b122010-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 Kivityc9eaf202009-05-18 16:13:45 +030061#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020062#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030063#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
64#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080065#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030066#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020076#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Avi Kivity0d7cdee2011-03-29 11:34:38 +020078#define Prefix (1<<16) /* Instruction varies with 66/f2/f3 prefix */
Avi Kivity12537912011-03-29 11:41:27 +020079#define Sse (1<<17) /* SSE Vector instruction */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030080/* Misc flags */
Avi Kivityd8671622011-02-01 16:32:03 +020081#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030082#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030083#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030084#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020085#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020086#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030087#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010088/* Source 2 operand type */
89#define Src2None (0<<29)
90#define Src2CL (1<<29)
91#define Src2ImmByte (2<<29)
92#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030093#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010094#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
Avi Kivityd0e53322010-07-29 15:11:54 +030096#define X2(x...) x, x
97#define X3(x...) X2(x), x
98#define X4(x...) X2(x), X2(x)
99#define X5(x...) X4(x), x
100#define X6(x...) X4(x), X2(x)
101#define X7(x...) X4(x), X3(x)
102#define X8(x...) X4(x), X4(x)
103#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300104
Avi Kivityd65b1de2010-07-29 15:11:35 +0300105struct opcode {
106 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200107 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300108 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300109 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300110 struct opcode *group;
111 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200112 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300113 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200114 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300115};
116
117struct group_dual {
118 struct opcode mod012[8];
119 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300120};
121
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200122struct gprefix {
123 struct opcode pfx_no;
124 struct opcode pfx_66;
125 struct opcode pfx_f2;
126 struct opcode pfx_f3;
127};
128
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200130#define EFLG_ID (1<<21)
131#define EFLG_VIP (1<<20)
132#define EFLG_VIF (1<<19)
133#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200134#define EFLG_VM (1<<17)
135#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200136#define EFLG_IOPL (3<<12)
137#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138#define EFLG_OF (1<<11)
139#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200140#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200141#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142#define EFLG_SF (1<<7)
143#define EFLG_ZF (1<<6)
144#define EFLG_AF (1<<4)
145#define EFLG_PF (1<<2)
146#define EFLG_CF (1<<0)
147
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300148#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
149#define EFLG_RESERVED_ONE_MASK 2
150
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151/*
152 * Instruction emulation:
153 * Most instructions are emulated directly via a fragment of inline assembly
154 * code. This allows us to save/restore EFLAGS and thus very easily pick up
155 * any modified flags.
156 */
157
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800158#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159#define _LO32 "k" /* force 32-bit operand */
160#define _STK "%%rsp" /* stack pointer */
161#elif defined(__i386__)
162#define _LO32 "" /* force 32-bit operand */
163#define _STK "%%esp" /* stack pointer */
164#endif
165
166/*
167 * These EFLAGS bits are restored from saved value during emulation, and
168 * any changes are written back to the saved value after emulation.
169 */
170#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
171
172/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200173#define _PRE_EFLAGS(_sav, _msk, _tmp) \
174 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
175 "movl %"_sav",%"_LO32 _tmp"; " \
176 "push %"_tmp"; " \
177 "push %"_tmp"; " \
178 "movl %"_msk",%"_LO32 _tmp"; " \
179 "andl %"_LO32 _tmp",("_STK"); " \
180 "pushf; " \
181 "notl %"_LO32 _tmp"; " \
182 "andl %"_LO32 _tmp",("_STK"); " \
183 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
184 "pop %"_tmp"; " \
185 "orl %"_LO32 _tmp",("_STK"); " \
186 "popf; " \
187 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188
189/* After executing instruction: write-back necessary bits in EFLAGS. */
190#define _POST_EFLAGS(_sav, _msk, _tmp) \
191 /* _sav |= EFLAGS & _msk; */ \
192 "pushf; " \
193 "pop %"_tmp"; " \
194 "andl %"_msk",%"_LO32 _tmp"; " \
195 "orl %"_LO32 _tmp",%"_sav"; "
196
Avi Kivitydda96d82008-11-26 15:14:10 +0200197#ifdef CONFIG_X86_64
198#define ON64(x) x
199#else
200#define ON64(x)
201#endif
202
Avi Kivityb3b3d252010-08-16 17:49:52 +0300203#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200204 do { \
205 __asm__ __volatile__ ( \
206 _PRE_EFLAGS("0", "4", "2") \
207 _op _suffix " %"_x"3,%1; " \
208 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300209 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200210 "=&r" (_tmp) \
211 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200212 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200213
214
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215/* Raw emulation: instruction has two explicit operands. */
216#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200217 do { \
218 unsigned long _tmp; \
219 \
220 switch ((_dst).bytes) { \
221 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300222 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200223 break; \
224 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300225 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200226 break; \
227 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300228 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200229 break; \
230 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 } while (0)
232
233#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
234 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200235 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400236 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300238 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 break; \
240 default: \
241 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
242 _wx, _wy, _lx, _ly, _qx, _qy); \
243 break; \
244 } \
245 } while (0)
246
247/* Source operand is byte-sized and may be restricted to just %cl. */
248#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
249 __emulate_2op(_op, _src, _dst, _eflags, \
250 "b", "c", "b", "c", "b", "c", "b", "c")
251
252/* Source operand is byte, word, long or quad sized. */
253#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
254 __emulate_2op(_op, _src, _dst, _eflags, \
255 "b", "q", "w", "r", _LO32, "r", "", "r")
256
257/* Source operand is word, long or quad sized. */
258#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
259 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
260 "w", "r", _LO32, "r", "", "r")
261
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100262/* Instruction has three operands and one operand is stored in ECX register */
263#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
264 do { \
265 unsigned long _tmp; \
266 _type _clv = (_cl).val; \
267 _type _srcv = (_src).val; \
268 _type _dstv = (_dst).val; \
269 \
270 __asm__ __volatile__ ( \
271 _PRE_EFLAGS("0", "5", "2") \
272 _op _suffix " %4,%1 \n" \
273 _POST_EFLAGS("0", "5", "2") \
274 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
275 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
276 ); \
277 \
278 (_cl).val = (unsigned long) _clv; \
279 (_src).val = (unsigned long) _srcv; \
280 (_dst).val = (unsigned long) _dstv; \
281 } while (0)
282
283#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
284 do { \
285 switch ((_dst).bytes) { \
286 case 2: \
287 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
288 "w", unsigned short); \
289 break; \
290 case 4: \
291 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
292 "l", unsigned int); \
293 break; \
294 case 8: \
295 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
296 "q", unsigned long)); \
297 break; \
298 } \
299 } while (0)
300
Avi Kivitydda96d82008-11-26 15:14:10 +0200301#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 do { \
303 unsigned long _tmp; \
304 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200305 __asm__ __volatile__ ( \
306 _PRE_EFLAGS("0", "3", "2") \
307 _op _suffix " %1; " \
308 _POST_EFLAGS("0", "3", "2") \
309 : "=m" (_eflags), "+m" ((_dst).val), \
310 "=&r" (_tmp) \
311 : "i" (EFLAGS_MASK)); \
312 } while (0)
313
314/* Instruction has only one explicit operand (no source operand). */
315#define emulate_1op(_op, _dst, _eflags) \
316 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400317 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200318 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
319 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
320 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
321 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 } \
323 } while (0)
324
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300325#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
326 do { \
327 unsigned long _tmp; \
328 \
329 __asm__ __volatile__ ( \
330 _PRE_EFLAGS("0", "4", "1") \
331 _op _suffix " %5; " \
332 _POST_EFLAGS("0", "4", "1") \
333 : "=m" (_eflags), "=&r" (_tmp), \
334 "+a" (_rax), "+d" (_rdx) \
335 : "i" (EFLAGS_MASK), "m" ((_src).val), \
336 "a" (_rax), "d" (_rdx)); \
337 } while (0)
338
Avi Kivityf6b35972010-08-26 11:59:00 +0300339#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
340 do { \
341 unsigned long _tmp; \
342 \
343 __asm__ __volatile__ ( \
344 _PRE_EFLAGS("0", "5", "1") \
345 "1: \n\t" \
346 _op _suffix " %6; " \
347 "2: \n\t" \
348 _POST_EFLAGS("0", "5", "1") \
349 ".pushsection .fixup,\"ax\" \n\t" \
350 "3: movb $1, %4 \n\t" \
351 "jmp 2b \n\t" \
352 ".popsection \n\t" \
353 _ASM_EXTABLE(1b, 3b) \
354 : "=m" (_eflags), "=&r" (_tmp), \
355 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
356 : "i" (EFLAGS_MASK), "m" ((_src).val), \
357 "a" (_rax), "d" (_rdx)); \
358 } while (0)
359
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300360/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
361#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
362 do { \
363 switch((_src).bytes) { \
364 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
365 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
366 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
367 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
368 } \
369 } while (0)
370
Avi Kivityf6b35972010-08-26 11:59:00 +0300371#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
372 do { \
373 switch((_src).bytes) { \
374 case 1: \
375 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
376 _eflags, "b", _ex); \
377 break; \
378 case 2: \
379 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
380 _eflags, "w", _ex); \
381 break; \
382 case 4: \
383 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
384 _eflags, "l", _ex); \
385 break; \
386 case 8: ON64( \
387 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
388 _eflags, "q", _ex)); \
389 break; \
390 } \
391 } while (0)
392
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393/* Fetch next part of the instruction being emulated. */
394#define insn_fetch(_type, _size, _eip) \
395({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200396 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200397 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 goto done; \
399 (_eip) += (_size); \
400 (_type)_x; \
401})
402
Gleb Natapov414e6272010-04-28 19:15:26 +0300403#define insn_fetch_arr(_arr, _size, _eip) \
404({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
405 if (rc != X86EMUL_CONTINUE) \
406 goto done; \
407 (_eip) += (_size); \
408})
409
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800410static inline unsigned long ad_mask(struct decode_cache *c)
411{
412 return (1UL << (c->ad_bytes << 3)) - 1;
413}
414
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800416static inline unsigned long
417address_mask(struct decode_cache *c, unsigned long reg)
418{
419 if (c->ad_bytes == sizeof(unsigned long))
420 return reg;
421 else
422 return reg & ad_mask(c);
423}
424
425static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200426register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800427{
Avi Kivity90de84f2010-11-17 15:28:21 +0200428 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800429}
430
Harvey Harrison7a9572752008-02-19 07:40:41 -0800431static inline void
432register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
433{
434 if (c->ad_bytes == sizeof(unsigned long))
435 *reg += inc;
436 else
437 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
438}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439
Harvey Harrison7a9572752008-02-19 07:40:41 -0800440static inline void jmp_rel(struct decode_cache *c, int rel)
441{
442 register_address_increment(c, &c->eip, rel);
443}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300444
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300445static void set_seg_override(struct decode_cache *c, int seg)
446{
447 c->has_seg_override = true;
448 c->seg_override = seg;
449}
450
Gleb Natapov79168fd2010-04-28 19:15:30 +0300451static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
452 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300453{
454 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
455 return 0;
456
Gleb Natapov79168fd2010-04-28 19:15:30 +0300457 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300458}
459
Avi Kivity90de84f2010-11-17 15:28:21 +0200460static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
461 struct x86_emulate_ops *ops,
462 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300463{
464 if (!c->has_seg_override)
465 return 0;
466
Avi Kivity90de84f2010-11-17 15:28:21 +0200467 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300468}
469
Avi Kivity90de84f2010-11-17 15:28:21 +0200470static ulong linear(struct x86_emulate_ctxt *ctxt,
471 struct segmented_address addr)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300472{
Avi Kivity90de84f2010-11-17 15:28:21 +0200473 struct decode_cache *c = &ctxt->decode;
474 ulong la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300475
Avi Kivity90de84f2010-11-17 15:28:21 +0200476 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
477 if (c->ad_bytes != 8)
478 la &= (u32)-1;
479 return la;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300480}
481
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200482static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
483 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300484{
Avi Kivityda9cb572010-11-22 17:53:21 +0200485 ctxt->exception.vector = vec;
486 ctxt->exception.error_code = error;
487 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200488 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300489}
490
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200491static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300492{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200493 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300494}
495
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200496static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300497{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200498 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300499}
500
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200501static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300502{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200503 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300504}
505
Avi Kivity34d1f492010-08-26 11:59:01 +0300506static int emulate_de(struct x86_emulate_ctxt *ctxt)
507{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200508 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300509}
510
Avi Kivity12537912011-03-29 11:41:27 +0200511static int emulate_nm(struct x86_emulate_ctxt *ctxt)
512{
513 return emulate_exception(ctxt, NM_VECTOR, 0, false);
514}
515
Avi Kivity62266862007-11-20 13:15:52 +0200516static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
517 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300518 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200519{
520 struct fetch_cache *fc = &ctxt->decode.fetch;
521 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300522 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200523
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300524 if (eip == fc->end) {
525 cur_size = fc->end - fc->start;
526 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
527 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200528 size, ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900529 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200530 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300531 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200532 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300533 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900534 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200535}
536
537static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
538 struct x86_emulate_ops *ops,
539 unsigned long eip, void *dest, unsigned size)
540{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900541 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200542
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200543 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200544 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200545 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200546 while (size--) {
547 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900548 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200549 return rc;
550 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900551 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200552}
553
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000554/*
555 * Given the 'reg' portion of a ModRM byte, and a register block, return a
556 * pointer into the block that addresses the relevant register.
557 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
558 */
559static void *decode_register(u8 modrm_reg, unsigned long *regs,
560 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561{
562 void *p;
563
564 p = &regs[modrm_reg];
565 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
566 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
567 return p;
568}
569
570static int read_descriptor(struct x86_emulate_ctxt *ctxt,
571 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200572 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 u16 *size, unsigned long *address, int op_bytes)
574{
575 int rc;
576
577 if (op_bytes == 2)
578 op_bytes = 3;
579 *address = 0;
Avi Kivity90de84f2010-11-17 15:28:21 +0200580 rc = ops->read_std(linear(ctxt, addr), (unsigned long *)size, 2,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200581 ctxt->vcpu, &ctxt->exception);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900582 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200584 addr.ea += 2;
585 rc = ops->read_std(linear(ctxt, addr), address, op_bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200586 ctxt->vcpu, &ctxt->exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 return rc;
588}
589
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300590static int test_cc(unsigned int condition, unsigned int flags)
591{
592 int rc = 0;
593
594 switch ((condition & 15) >> 1) {
595 case 0: /* o */
596 rc |= (flags & EFLG_OF);
597 break;
598 case 1: /* b/c/nae */
599 rc |= (flags & EFLG_CF);
600 break;
601 case 2: /* z/e */
602 rc |= (flags & EFLG_ZF);
603 break;
604 case 3: /* be/na */
605 rc |= (flags & (EFLG_CF|EFLG_ZF));
606 break;
607 case 4: /* s */
608 rc |= (flags & EFLG_SF);
609 break;
610 case 5: /* p/pe */
611 rc |= (flags & EFLG_PF);
612 break;
613 case 7: /* le/ng */
614 rc |= (flags & EFLG_ZF);
615 /* fall through */
616 case 6: /* l/nge */
617 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
618 break;
619 }
620
621 /* Odd condition identifiers (lsb == 1) have inverted sense. */
622 return (!!rc ^ (condition & 1));
623}
624
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300625static void fetch_register_operand(struct operand *op)
626{
627 switch (op->bytes) {
628 case 1:
629 op->val = *(u8 *)op->addr.reg;
630 break;
631 case 2:
632 op->val = *(u16 *)op->addr.reg;
633 break;
634 case 4:
635 op->val = *(u32 *)op->addr.reg;
636 break;
637 case 8:
638 op->val = *(u64 *)op->addr.reg;
639 break;
640 }
641}
642
Avi Kivity12537912011-03-29 11:41:27 +0200643static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
644{
645 ctxt->ops->get_fpu(ctxt);
646 switch (reg) {
647 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
648 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
649 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
650 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
651 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
652 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
653 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
654 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
655#ifdef CONFIG_X86_64
656 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
657 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
658 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
659 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
660 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
661 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
662 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
663 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
664#endif
665 default: BUG();
666 }
667 ctxt->ops->put_fpu(ctxt);
668}
669
670static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
671 int reg)
672{
673 ctxt->ops->get_fpu(ctxt);
674 switch (reg) {
675 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
676 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
677 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
678 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
679 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
680 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
681 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
682 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
683#ifdef CONFIG_X86_64
684 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
685 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
686 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
687 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
688 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
689 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
690 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
691 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
692#endif
693 default: BUG();
694 }
695 ctxt->ops->put_fpu(ctxt);
696}
697
698static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
699 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200700 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200701 int inhibit_bytereg)
702{
Avi Kivity33615aa2007-10-31 11:15:56 +0200703 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200704 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200705
706 if (!(c->d & ModRM))
707 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200708
709 if (c->d & Sse) {
710 op->type = OP_XMM;
711 op->bytes = 16;
712 op->addr.xmm = reg;
713 read_sse_reg(ctxt, &op->vec_val, reg);
714 return;
715 }
716
Avi Kivity3c118e22007-10-31 10:27:04 +0200717 op->type = OP_REG;
718 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300719 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200720 op->bytes = 1;
721 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300722 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200723 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200724 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300725 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200726 op->orig_val = op->val;
727}
728
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200729static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300730 struct x86_emulate_ops *ops,
731 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200732{
733 struct decode_cache *c = &ctxt->decode;
734 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700735 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900736 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300737 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200738
739 if (c->rex_prefix) {
740 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
741 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
742 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
743 }
744
745 c->modrm = insn_fetch(u8, 1, c->eip);
746 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
747 c->modrm_reg |= (c->modrm & 0x38) >> 3;
748 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300749 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200750
751 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300752 op->type = OP_REG;
753 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
754 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300755 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200756 if (c->d & Sse) {
757 op->type = OP_XMM;
758 op->bytes = 16;
759 op->addr.xmm = c->modrm_rm;
760 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
761 return rc;
762 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300763 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200764 return rc;
765 }
766
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300767 op->type = OP_MEM;
768
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200769 if (c->ad_bytes == 2) {
770 unsigned bx = c->regs[VCPU_REGS_RBX];
771 unsigned bp = c->regs[VCPU_REGS_RBP];
772 unsigned si = c->regs[VCPU_REGS_RSI];
773 unsigned di = c->regs[VCPU_REGS_RDI];
774
775 /* 16-bit ModR/M decode. */
776 switch (c->modrm_mod) {
777 case 0:
778 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300779 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200780 break;
781 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300782 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200783 break;
784 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300785 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200786 break;
787 }
788 switch (c->modrm_rm) {
789 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300790 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200791 break;
792 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300793 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200794 break;
795 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300796 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200797 break;
798 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300799 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200800 break;
801 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300802 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200803 break;
804 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300805 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200806 break;
807 case 6:
808 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300809 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200810 break;
811 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300812 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200813 break;
814 }
815 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
816 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300817 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300818 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200819 } else {
820 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700821 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200822 sib = insn_fetch(u8, 1, c->eip);
823 index_reg |= (sib >> 3) & 7;
824 base_reg |= sib & 7;
825 scale = sib >> 6;
826
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700827 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300828 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700829 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300830 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700831 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300832 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700833 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
834 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700835 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700836 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300837 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200838 switch (c->modrm_mod) {
839 case 0:
840 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300841 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200842 break;
843 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300844 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200845 break;
846 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300847 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200848 break;
849 }
850 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200851 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200852done:
853 return rc;
854}
855
856static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300857 struct x86_emulate_ops *ops,
858 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200859{
860 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900861 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200862
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300863 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200864 switch (c->ad_bytes) {
865 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +0200866 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200867 break;
868 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +0200869 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200870 break;
871 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +0200872 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200873 break;
874 }
875done:
876 return rc;
877}
878
Wei Yongjun35c843c2010-08-09 11:34:56 +0800879static void fetch_bit_operand(struct decode_cache *c)
880{
Sheng Yang7129eec2010-09-28 16:33:32 +0800881 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800882
Wei Yongjun3885f182010-08-09 11:37:37 +0800883 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800884 mask = ~(c->dst.bytes * 8 - 1);
885
886 if (c->src.bytes == 2)
887 sv = (s16)c->src.val & (s16)mask;
888 else if (c->src.bytes == 4)
889 sv = (s32)c->src.val & (s32)mask;
890
Avi Kivity90de84f2010-11-17 15:28:21 +0200891 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +0800892 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800893
894 /* only subword offset */
895 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800896}
897
Gleb Natapov9de41572010-04-28 19:15:22 +0300898static int read_emulated(struct x86_emulate_ctxt *ctxt,
899 struct x86_emulate_ops *ops,
900 unsigned long addr, void *dest, unsigned size)
901{
902 int rc;
903 struct read_cache *mc = &ctxt->decode.mem_read;
904
905 while (size) {
906 int n = min(size, 8u);
907 size -= n;
908 if (mc->pos < mc->end)
909 goto read_cached;
910
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200911 rc = ops->read_emulated(addr, mc->data + mc->end, n,
912 &ctxt->exception, ctxt->vcpu);
Gleb Natapov9de41572010-04-28 19:15:22 +0300913 if (rc != X86EMUL_CONTINUE)
914 return rc;
915 mc->end += n;
916
917 read_cached:
918 memcpy(dest, mc->data + mc->pos, n);
919 mc->pos += n;
920 dest += n;
921 addr += n;
922 }
923 return X86EMUL_CONTINUE;
924}
925
Gleb Natapov7b262e92010-03-18 15:20:27 +0200926static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
927 struct x86_emulate_ops *ops,
928 unsigned int size, unsigned short port,
929 void *dest)
930{
931 struct read_cache *rc = &ctxt->decode.io_read;
932
933 if (rc->pos == rc->end) { /* refill pio read ahead */
934 struct decode_cache *c = &ctxt->decode;
935 unsigned int in_page, n;
936 unsigned int count = c->rep_prefix ?
937 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
938 in_page = (ctxt->eflags & EFLG_DF) ?
939 offset_in_page(c->regs[VCPU_REGS_RDI]) :
940 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
941 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
942 count);
943 if (n == 0)
944 n = 1;
945 rc->pos = rc->end = 0;
946 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
947 return 0;
948 rc->end = n * size;
949 }
950
951 memcpy(dest, rc->data + rc->pos, size);
952 rc->pos += size;
953 return 1;
954}
955
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200956static u32 desc_limit_scaled(struct desc_struct *desc)
957{
958 u32 limit = get_desc_limit(desc);
959
960 return desc->g ? (limit << 12) | 0xfff : limit;
961}
962
963static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
964 struct x86_emulate_ops *ops,
965 u16 selector, struct desc_ptr *dt)
966{
967 if (selector & 1 << 2) {
968 struct desc_struct desc;
969 memset (dt, 0, sizeof *dt);
Gleb Natapov5601d052011-03-07 14:55:06 +0200970 if (!ops->get_cached_descriptor(&desc, NULL, VCPU_SREG_LDTR,
971 ctxt->vcpu))
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200972 return;
973
974 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
975 dt->address = get_desc_base(&desc);
976 } else
977 ops->get_gdt(dt, ctxt->vcpu);
978}
979
980/* allowed just for 8 bytes segments */
981static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
982 struct x86_emulate_ops *ops,
983 u16 selector, struct desc_struct *desc)
984{
985 struct desc_ptr dt;
986 u16 index = selector >> 3;
987 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200988 ulong addr;
989
990 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
991
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200992 if (dt.size < index * 8 + 7)
993 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200994 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200995 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu,
996 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200997
998 return ret;
999}
1000
1001/* allowed just for 8 bytes segments */
1002static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1003 struct x86_emulate_ops *ops,
1004 u16 selector, struct desc_struct *desc)
1005{
1006 struct desc_ptr dt;
1007 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001008 ulong addr;
1009 int ret;
1010
1011 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1012
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001013 if (dt.size < index * 8 + 7)
1014 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001015
1016 addr = dt.address + index * 8;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001017 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu,
1018 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001019
1020 return ret;
1021}
1022
Gleb Natapov5601d052011-03-07 14:55:06 +02001023/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001024static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1025 struct x86_emulate_ops *ops,
1026 u16 selector, int seg)
1027{
1028 struct desc_struct seg_desc;
1029 u8 dpl, rpl, cpl;
1030 unsigned err_vec = GP_VECTOR;
1031 u32 err_code = 0;
1032 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1033 int ret;
1034
1035 memset(&seg_desc, 0, sizeof seg_desc);
1036
1037 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1038 || ctxt->mode == X86EMUL_MODE_REAL) {
1039 /* set real mode segment descriptor */
1040 set_desc_base(&seg_desc, selector << 4);
1041 set_desc_limit(&seg_desc, 0xffff);
1042 seg_desc.type = 3;
1043 seg_desc.p = 1;
1044 seg_desc.s = 1;
1045 goto load;
1046 }
1047
1048 /* NULL selector is not valid for TR, CS and SS */
1049 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1050 && null_selector)
1051 goto exception;
1052
1053 /* TR should be in GDT only */
1054 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1055 goto exception;
1056
1057 if (null_selector) /* for NULL selector skip all following checks */
1058 goto load;
1059
1060 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1061 if (ret != X86EMUL_CONTINUE)
1062 return ret;
1063
1064 err_code = selector & 0xfffc;
1065 err_vec = GP_VECTOR;
1066
1067 /* can't load system descriptor into segment selecor */
1068 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1069 goto exception;
1070
1071 if (!seg_desc.p) {
1072 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1073 goto exception;
1074 }
1075
1076 rpl = selector & 3;
1077 dpl = seg_desc.dpl;
1078 cpl = ops->cpl(ctxt->vcpu);
1079
1080 switch (seg) {
1081 case VCPU_SREG_SS:
1082 /*
1083 * segment is not a writable data segment or segment
1084 * selector's RPL != CPL or segment selector's RPL != CPL
1085 */
1086 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1087 goto exception;
1088 break;
1089 case VCPU_SREG_CS:
1090 if (!(seg_desc.type & 8))
1091 goto exception;
1092
1093 if (seg_desc.type & 4) {
1094 /* conforming */
1095 if (dpl > cpl)
1096 goto exception;
1097 } else {
1098 /* nonconforming */
1099 if (rpl > cpl || dpl != cpl)
1100 goto exception;
1101 }
1102 /* CS(RPL) <- CPL */
1103 selector = (selector & 0xfffc) | cpl;
1104 break;
1105 case VCPU_SREG_TR:
1106 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1107 goto exception;
1108 break;
1109 case VCPU_SREG_LDTR:
1110 if (seg_desc.s || seg_desc.type != 2)
1111 goto exception;
1112 break;
1113 default: /* DS, ES, FS, or GS */
1114 /*
1115 * segment is not a data or readable code segment or
1116 * ((segment is a data or nonconforming code segment)
1117 * and (both RPL and CPL > DPL))
1118 */
1119 if ((seg_desc.type & 0xa) == 0x8 ||
1120 (((seg_desc.type & 0xc) != 0xc) &&
1121 (rpl > dpl && cpl > dpl)))
1122 goto exception;
1123 break;
1124 }
1125
1126 if (seg_desc.s) {
1127 /* mark segment as accessed */
1128 seg_desc.type |= 1;
1129 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1130 if (ret != X86EMUL_CONTINUE)
1131 return ret;
1132 }
1133load:
1134 ops->set_segment_selector(selector, seg, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001135 ops->set_cached_descriptor(&seg_desc, 0, seg, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001136 return X86EMUL_CONTINUE;
1137exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001138 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001139 return X86EMUL_PROPAGATE_FAULT;
1140}
1141
Wei Yongjun31be40b2010-08-17 09:17:30 +08001142static void write_register_operand(struct operand *op)
1143{
1144 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1145 switch (op->bytes) {
1146 case 1:
1147 *(u8 *)op->addr.reg = (u8)op->val;
1148 break;
1149 case 2:
1150 *(u16 *)op->addr.reg = (u16)op->val;
1151 break;
1152 case 4:
1153 *op->addr.reg = (u32)op->val;
1154 break; /* 64b: zero-extend */
1155 case 8:
1156 *op->addr.reg = op->val;
1157 break;
1158 }
1159}
1160
Wei Yongjunc37eda12010-06-15 09:03:33 +08001161static inline int writeback(struct x86_emulate_ctxt *ctxt,
1162 struct x86_emulate_ops *ops)
1163{
1164 int rc;
1165 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001166
1167 switch (c->dst.type) {
1168 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001169 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001170 break;
1171 case OP_MEM:
1172 if (c->lock_prefix)
1173 rc = ops->cmpxchg_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001174 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001175 &c->dst.orig_val,
1176 &c->dst.val,
1177 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001178 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001179 ctxt->vcpu);
1180 else
1181 rc = ops->write_emulated(
Avi Kivity90de84f2010-11-17 15:28:21 +02001182 linear(ctxt, c->dst.addr.mem),
Wei Yongjunc37eda12010-06-15 09:03:33 +08001183 &c->dst.val,
1184 c->dst.bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001185 &ctxt->exception,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001186 ctxt->vcpu);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001187 if (rc != X86EMUL_CONTINUE)
1188 return rc;
1189 break;
Avi Kivity12537912011-03-29 11:41:27 +02001190 case OP_XMM:
1191 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1192 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001193 case OP_NONE:
1194 /* no writeback */
1195 break;
1196 default:
1197 break;
1198 }
1199 return X86EMUL_CONTINUE;
1200}
1201
Gleb Natapov79168fd2010-04-28 19:15:30 +03001202static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1203 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001204{
1205 struct decode_cache *c = &ctxt->decode;
1206
1207 c->dst.type = OP_MEM;
1208 c->dst.bytes = c->op_bytes;
1209 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001210 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02001211 c->dst.addr.mem.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1212 c->dst.addr.mem.seg = VCPU_SREG_SS;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001213}
1214
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001215static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001216 struct x86_emulate_ops *ops,
1217 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001218{
1219 struct decode_cache *c = &ctxt->decode;
1220 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001221 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001222
Avi Kivity90de84f2010-11-17 15:28:21 +02001223 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1224 addr.seg = VCPU_SREG_SS;
1225 rc = read_emulated(ctxt, ops, linear(ctxt, addr), dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001226 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001227 return rc;
1228
Avi Kivity350f69d2009-01-05 11:12:40 +02001229 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001230 return rc;
1231}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001232
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001233static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1234 struct x86_emulate_ops *ops,
1235 void *dest, int len)
1236{
1237 int rc;
1238 unsigned long val, change_mask;
1239 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001240 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001241
1242 rc = emulate_pop(ctxt, ops, &val, len);
1243 if (rc != X86EMUL_CONTINUE)
1244 return rc;
1245
1246 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1247 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1248
1249 switch(ctxt->mode) {
1250 case X86EMUL_MODE_PROT64:
1251 case X86EMUL_MODE_PROT32:
1252 case X86EMUL_MODE_PROT16:
1253 if (cpl == 0)
1254 change_mask |= EFLG_IOPL;
1255 if (cpl <= iopl)
1256 change_mask |= EFLG_IF;
1257 break;
1258 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001259 if (iopl < 3)
1260 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001261 change_mask |= EFLG_IF;
1262 break;
1263 default: /* real mode */
1264 change_mask |= (EFLG_IOPL | EFLG_IF);
1265 break;
1266 }
1267
1268 *(unsigned long *)dest =
1269 (ctxt->eflags & ~change_mask) | (val & change_mask);
1270
1271 return rc;
1272}
1273
Gleb Natapov79168fd2010-04-28 19:15:30 +03001274static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1275 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001276{
1277 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001278
Gleb Natapov79168fd2010-04-28 19:15:30 +03001279 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001280
Gleb Natapov79168fd2010-04-28 19:15:30 +03001281 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001282}
1283
1284static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1285 struct x86_emulate_ops *ops, int seg)
1286{
1287 struct decode_cache *c = &ctxt->decode;
1288 unsigned long selector;
1289 int rc;
1290
1291 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001292 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001293 return rc;
1294
Gleb Natapov2e873022010-03-18 15:20:18 +02001295 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001296 return rc;
1297}
1298
Wei Yongjunc37eda12010-06-15 09:03:33 +08001299static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001300 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001301{
1302 struct decode_cache *c = &ctxt->decode;
1303 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001304 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001305 int reg = VCPU_REGS_RAX;
1306
1307 while (reg <= VCPU_REGS_RDI) {
1308 (reg == VCPU_REGS_RSP) ?
1309 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1310
Gleb Natapov79168fd2010-04-28 19:15:30 +03001311 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001312
1313 rc = writeback(ctxt, ops);
1314 if (rc != X86EMUL_CONTINUE)
1315 return rc;
1316
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001317 ++reg;
1318 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001319
1320 /* Disable writeback. */
1321 c->dst.type = OP_NONE;
1322
1323 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001324}
1325
1326static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1327 struct x86_emulate_ops *ops)
1328{
1329 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001330 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001331 int reg = VCPU_REGS_RDI;
1332
1333 while (reg >= VCPU_REGS_RAX) {
1334 if (reg == VCPU_REGS_RSP) {
1335 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1336 c->op_bytes);
1337 --reg;
1338 }
1339
1340 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001341 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001342 break;
1343 --reg;
1344 }
1345 return rc;
1346}
1347
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001348int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1349 struct x86_emulate_ops *ops, int irq)
1350{
1351 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001352 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001353 struct desc_ptr dt;
1354 gva_t cs_addr;
1355 gva_t eip_addr;
1356 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001357
1358 /* TODO: Add limit checks */
1359 c->src.val = ctxt->eflags;
1360 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001361 rc = writeback(ctxt, ops);
1362 if (rc != X86EMUL_CONTINUE)
1363 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001364
1365 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1366
1367 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1368 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001369 rc = writeback(ctxt, ops);
1370 if (rc != X86EMUL_CONTINUE)
1371 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001372
1373 c->src.val = c->eip;
1374 emulate_push(ctxt, ops);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001375 rc = writeback(ctxt, ops);
1376 if (rc != X86EMUL_CONTINUE)
1377 return rc;
1378
1379 c->dst.type = OP_NONE;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001380
1381 ops->get_idt(&dt, ctxt->vcpu);
1382
1383 eip_addr = dt.address + (irq << 2);
1384 cs_addr = dt.address + (irq << 2) + 2;
1385
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001386 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001387 if (rc != X86EMUL_CONTINUE)
1388 return rc;
1389
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001390 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001391 if (rc != X86EMUL_CONTINUE)
1392 return rc;
1393
1394 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1395 if (rc != X86EMUL_CONTINUE)
1396 return rc;
1397
1398 c->eip = eip;
1399
1400 return rc;
1401}
1402
1403static int emulate_int(struct x86_emulate_ctxt *ctxt,
1404 struct x86_emulate_ops *ops, int irq)
1405{
1406 switch(ctxt->mode) {
1407 case X86EMUL_MODE_REAL:
1408 return emulate_int_real(ctxt, ops, irq);
1409 case X86EMUL_MODE_VM86:
1410 case X86EMUL_MODE_PROT16:
1411 case X86EMUL_MODE_PROT32:
1412 case X86EMUL_MODE_PROT64:
1413 default:
1414 /* Protected mode interrupts unimplemented yet */
1415 return X86EMUL_UNHANDLEABLE;
1416 }
1417}
1418
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001419static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1420 struct x86_emulate_ops *ops)
1421{
1422 struct decode_cache *c = &ctxt->decode;
1423 int rc = X86EMUL_CONTINUE;
1424 unsigned long temp_eip = 0;
1425 unsigned long temp_eflags = 0;
1426 unsigned long cs = 0;
1427 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1428 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1429 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1430 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1431
1432 /* TODO: Add stack limit check */
1433
1434 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1435
1436 if (rc != X86EMUL_CONTINUE)
1437 return rc;
1438
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001439 if (temp_eip & ~0xffff)
1440 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001441
1442 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1443
1444 if (rc != X86EMUL_CONTINUE)
1445 return rc;
1446
1447 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1448
1449 if (rc != X86EMUL_CONTINUE)
1450 return rc;
1451
1452 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1453
1454 if (rc != X86EMUL_CONTINUE)
1455 return rc;
1456
1457 c->eip = temp_eip;
1458
1459
1460 if (c->op_bytes == 4)
1461 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1462 else if (c->op_bytes == 2) {
1463 ctxt->eflags &= ~0xffff;
1464 ctxt->eflags |= temp_eflags;
1465 }
1466
1467 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1468 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1469
1470 return rc;
1471}
1472
1473static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1474 struct x86_emulate_ops* ops)
1475{
1476 switch(ctxt->mode) {
1477 case X86EMUL_MODE_REAL:
1478 return emulate_iret_real(ctxt, ops);
1479 case X86EMUL_MODE_VM86:
1480 case X86EMUL_MODE_PROT16:
1481 case X86EMUL_MODE_PROT32:
1482 case X86EMUL_MODE_PROT64:
1483 default:
1484 /* iret from protected mode unimplemented yet */
1485 return X86EMUL_UNHANDLEABLE;
1486 }
1487}
1488
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001489static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1490 struct x86_emulate_ops *ops)
1491{
1492 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001493
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001494 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001495}
1496
Laurent Vivier05f086f2007-09-24 11:10:55 +02001497static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001498{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001499 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001500 switch (c->modrm_reg) {
1501 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001502 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001503 break;
1504 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001505 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001506 break;
1507 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001508 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001509 break;
1510 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001511 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001512 break;
1513 case 4: /* sal/shl */
1514 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001515 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001516 break;
1517 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001518 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001519 break;
1520 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001521 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001522 break;
1523 }
1524}
1525
1526static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001527 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001528{
1529 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001530 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1531 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001532 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001533
1534 switch (c->modrm_reg) {
1535 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001536 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001537 break;
1538 case 2: /* not */
1539 c->dst.val = ~c->dst.val;
1540 break;
1541 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001542 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001543 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001544 case 4: /* mul */
1545 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1546 break;
1547 case 5: /* imul */
1548 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1549 break;
1550 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001551 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1552 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001553 break;
1554 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001555 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1556 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001557 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001558 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001559 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001560 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001561 if (de)
1562 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001563 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001564}
1565
1566static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001567 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001568{
1569 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001570
1571 switch (c->modrm_reg) {
1572 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001573 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001574 break;
1575 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001576 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001577 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001578 case 2: /* call near abs */ {
1579 long int old_eip;
1580 old_eip = c->eip;
1581 c->eip = c->src.val;
1582 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001583 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001584 break;
1585 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001586 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001587 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001588 break;
1589 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001590 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001591 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001592 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001593 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001594}
1595
1596static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001597 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001598{
1599 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001600 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001601
1602 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1603 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001604 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1605 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001606 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001607 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001608 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1609 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001610
Laurent Vivier05f086f2007-09-24 11:10:55 +02001611 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001612 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001613 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001614}
1615
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001616static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1617 struct x86_emulate_ops *ops)
1618{
1619 struct decode_cache *c = &ctxt->decode;
1620 int rc;
1621 unsigned long cs;
1622
1623 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001624 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001625 return rc;
1626 if (c->op_bytes == 4)
1627 c->eip = (u32)c->eip;
1628 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001629 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001630 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001631 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001632 return rc;
1633}
1634
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001635static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1636 struct x86_emulate_ops *ops, int seg)
1637{
1638 struct decode_cache *c = &ctxt->decode;
1639 unsigned short sel;
1640 int rc;
1641
1642 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1643
1644 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1645 if (rc != X86EMUL_CONTINUE)
1646 return rc;
1647
1648 c->dst.val = c->src.val;
1649 return rc;
1650}
1651
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001652static inline void
1653setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001654 struct x86_emulate_ops *ops, struct desc_struct *cs,
1655 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001656{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001657 memset(cs, 0, sizeof(struct desc_struct));
Gleb Natapov5601d052011-03-07 14:55:06 +02001658 ops->get_cached_descriptor(cs, NULL, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001659 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001660
1661 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001662 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001663 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001664 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001665 cs->type = 0x0b; /* Read, Execute, Accessed */
1666 cs->s = 1;
1667 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001668 cs->p = 1;
1669 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001670
Gleb Natapov79168fd2010-04-28 19:15:30 +03001671 set_desc_base(ss, 0); /* flat segment */
1672 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001673 ss->g = 1; /* 4kb granularity */
1674 ss->s = 1;
1675 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001676 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001677 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001678 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001679}
1680
1681static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001682emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001683{
1684 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001685 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001686 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001687 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001688
1689 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001690 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001691 ctxt->mode == X86EMUL_MODE_VM86)
1692 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001693
Gleb Natapov79168fd2010-04-28 19:15:30 +03001694 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001695
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001696 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001697 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001698 cs_sel = (u16)(msr_data & 0xfffc);
1699 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001700
1701 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001702 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001703 cs.l = 1;
1704 }
Gleb Natapov5601d052011-03-07 14:55:06 +02001705 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001706 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001707 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001708 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001709
1710 c->regs[VCPU_REGS_RCX] = c->eip;
1711 if (is_long_mode(ctxt->vcpu)) {
1712#ifdef CONFIG_X86_64
1713 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1714
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001715 ops->get_msr(ctxt->vcpu,
1716 ctxt->mode == X86EMUL_MODE_PROT64 ?
1717 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001718 c->eip = msr_data;
1719
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001720 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001721 ctxt->eflags &= ~(msr_data | EFLG_RF);
1722#endif
1723 } else {
1724 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001725 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001726 c->eip = (u32)msr_data;
1727
1728 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1729 }
1730
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001731 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001732}
1733
Andre Przywara8c604352009-06-18 12:56:01 +02001734static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001735emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001736{
1737 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001738 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001739 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001740 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001741
Gleb Natapova0044752010-02-10 14:21:31 +02001742 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001743 if (ctxt->mode == X86EMUL_MODE_REAL)
1744 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001745
1746 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1747 * Therefore, we inject an #UD.
1748 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001749 if (ctxt->mode == X86EMUL_MODE_PROT64)
1750 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001751
Gleb Natapov79168fd2010-04-28 19:15:30 +03001752 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001753
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001754 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001755 switch (ctxt->mode) {
1756 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001757 if ((msr_data & 0xfffc) == 0x0)
1758 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001759 break;
1760 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001761 if (msr_data == 0x0)
1762 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001763 break;
1764 }
1765
1766 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001767 cs_sel = (u16)msr_data;
1768 cs_sel &= ~SELECTOR_RPL_MASK;
1769 ss_sel = cs_sel + 8;
1770 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001771 if (ctxt->mode == X86EMUL_MODE_PROT64
1772 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001773 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001774 cs.l = 1;
1775 }
1776
Gleb Natapov5601d052011-03-07 14:55:06 +02001777 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001778 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001779 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001780 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001781
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001782 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001783 c->eip = msr_data;
1784
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001785 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001786 c->regs[VCPU_REGS_RSP] = msr_data;
1787
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001788 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001789}
1790
Andre Przywara4668f052009-06-18 12:56:02 +02001791static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001792emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001793{
1794 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001795 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001796 u64 msr_data;
1797 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001798 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001799
Gleb Natapova0044752010-02-10 14:21:31 +02001800 /* inject #GP if in real mode or Virtual 8086 mode */
1801 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001802 ctxt->mode == X86EMUL_MODE_VM86)
1803 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001804
Gleb Natapov79168fd2010-04-28 19:15:30 +03001805 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001806
1807 if ((c->rex_prefix & 0x8) != 0x0)
1808 usermode = X86EMUL_MODE_PROT64;
1809 else
1810 usermode = X86EMUL_MODE_PROT32;
1811
1812 cs.dpl = 3;
1813 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001814 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001815 switch (usermode) {
1816 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001817 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001818 if ((msr_data & 0xfffc) == 0x0)
1819 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001820 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001821 break;
1822 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001823 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001824 if (msr_data == 0x0)
1825 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001826 ss_sel = cs_sel + 8;
1827 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001828 cs.l = 1;
1829 break;
1830 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001831 cs_sel |= SELECTOR_RPL_MASK;
1832 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001833
Gleb Natapov5601d052011-03-07 14:55:06 +02001834 ops->set_cached_descriptor(&cs, 0, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001835 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02001836 ops->set_cached_descriptor(&ss, 0, VCPU_SREG_SS, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001837 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001838
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001839 c->eip = c->regs[VCPU_REGS_RDX];
1840 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001841
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001842 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001843}
1844
Gleb Natapov9c537242010-03-18 15:20:05 +02001845static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1846 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001847{
1848 int iopl;
1849 if (ctxt->mode == X86EMUL_MODE_REAL)
1850 return false;
1851 if (ctxt->mode == X86EMUL_MODE_VM86)
1852 return true;
1853 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001854 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001855}
1856
1857static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1858 struct x86_emulate_ops *ops,
1859 u16 port, u16 len)
1860{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001861 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02001862 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001863 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001864 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001865 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02001866 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001867
Gleb Natapov5601d052011-03-07 14:55:06 +02001868 ops->get_cached_descriptor(&tr_seg, &base3, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001869 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001870 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001871 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001872 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02001873 base = get_desc_base(&tr_seg);
1874#ifdef CONFIG_X86_64
1875 base |= ((u64)base3) << 32;
1876#endif
1877 r = ops->read_std(base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001878 if (r != X86EMUL_CONTINUE)
1879 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001880 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001881 return false;
Gleb Natapov399a40c2011-03-07 14:55:07 +02001882 r = ops->read_std(base + io_bitmap_ptr + port/8, &perm, 2, ctxt->vcpu,
Gleb Natapov5601d052011-03-07 14:55:06 +02001883 NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001884 if (r != X86EMUL_CONTINUE)
1885 return false;
1886 if ((perm >> bit_idx) & mask)
1887 return false;
1888 return true;
1889}
1890
1891static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1892 struct x86_emulate_ops *ops,
1893 u16 port, u16 len)
1894{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001895 if (ctxt->perm_ok)
1896 return true;
1897
Gleb Natapov9c537242010-03-18 15:20:05 +02001898 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001899 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1900 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001901
1902 ctxt->perm_ok = true;
1903
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001904 return true;
1905}
1906
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001907static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1908 struct x86_emulate_ops *ops,
1909 struct tss_segment_16 *tss)
1910{
1911 struct decode_cache *c = &ctxt->decode;
1912
1913 tss->ip = c->eip;
1914 tss->flag = ctxt->eflags;
1915 tss->ax = c->regs[VCPU_REGS_RAX];
1916 tss->cx = c->regs[VCPU_REGS_RCX];
1917 tss->dx = c->regs[VCPU_REGS_RDX];
1918 tss->bx = c->regs[VCPU_REGS_RBX];
1919 tss->sp = c->regs[VCPU_REGS_RSP];
1920 tss->bp = c->regs[VCPU_REGS_RBP];
1921 tss->si = c->regs[VCPU_REGS_RSI];
1922 tss->di = c->regs[VCPU_REGS_RDI];
1923
1924 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1925 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1926 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1927 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1928 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1929}
1930
1931static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1932 struct x86_emulate_ops *ops,
1933 struct tss_segment_16 *tss)
1934{
1935 struct decode_cache *c = &ctxt->decode;
1936 int ret;
1937
1938 c->eip = tss->ip;
1939 ctxt->eflags = tss->flag | 2;
1940 c->regs[VCPU_REGS_RAX] = tss->ax;
1941 c->regs[VCPU_REGS_RCX] = tss->cx;
1942 c->regs[VCPU_REGS_RDX] = tss->dx;
1943 c->regs[VCPU_REGS_RBX] = tss->bx;
1944 c->regs[VCPU_REGS_RSP] = tss->sp;
1945 c->regs[VCPU_REGS_RBP] = tss->bp;
1946 c->regs[VCPU_REGS_RSI] = tss->si;
1947 c->regs[VCPU_REGS_RDI] = tss->di;
1948
1949 /*
1950 * SDM says that segment selectors are loaded before segment
1951 * descriptors
1952 */
1953 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1954 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1955 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1956 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1957 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1958
1959 /*
1960 * Now load segment descriptors. If fault happenes at this stage
1961 * it is handled in a context of new task
1962 */
1963 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1964 if (ret != X86EMUL_CONTINUE)
1965 return ret;
1966 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1967 if (ret != X86EMUL_CONTINUE)
1968 return ret;
1969 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1970 if (ret != X86EMUL_CONTINUE)
1971 return ret;
1972 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1973 if (ret != X86EMUL_CONTINUE)
1974 return ret;
1975 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1976 if (ret != X86EMUL_CONTINUE)
1977 return ret;
1978
1979 return X86EMUL_CONTINUE;
1980}
1981
1982static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1983 struct x86_emulate_ops *ops,
1984 u16 tss_selector, u16 old_tss_sel,
1985 ulong old_tss_base, struct desc_struct *new_desc)
1986{
1987 struct tss_segment_16 tss_seg;
1988 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001989 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001990
1991 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02001992 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02001993 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001994 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001995 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001996
1997 save_state_to_tss16(ctxt, ops, &tss_seg);
1998
1999 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002000 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002001 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002002 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002003 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002004
2005 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002006 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002007 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002008 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002009 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002010
2011 if (old_tss_sel != 0xffff) {
2012 tss_seg.prev_task_link = old_tss_sel;
2013
2014 ret = ops->write_std(new_tss_base,
2015 &tss_seg.prev_task_link,
2016 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002017 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002018 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002019 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002020 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002021 }
2022
2023 return load_state_from_tss16(ctxt, ops, &tss_seg);
2024}
2025
2026static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2027 struct x86_emulate_ops *ops,
2028 struct tss_segment_32 *tss)
2029{
2030 struct decode_cache *c = &ctxt->decode;
2031
2032 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
2033 tss->eip = c->eip;
2034 tss->eflags = ctxt->eflags;
2035 tss->eax = c->regs[VCPU_REGS_RAX];
2036 tss->ecx = c->regs[VCPU_REGS_RCX];
2037 tss->edx = c->regs[VCPU_REGS_RDX];
2038 tss->ebx = c->regs[VCPU_REGS_RBX];
2039 tss->esp = c->regs[VCPU_REGS_RSP];
2040 tss->ebp = c->regs[VCPU_REGS_RBP];
2041 tss->esi = c->regs[VCPU_REGS_RSI];
2042 tss->edi = c->regs[VCPU_REGS_RDI];
2043
2044 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
2045 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2046 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
2047 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
2048 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
2049 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
2050 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
2051}
2052
2053static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2054 struct x86_emulate_ops *ops,
2055 struct tss_segment_32 *tss)
2056{
2057 struct decode_cache *c = &ctxt->decode;
2058 int ret;
2059
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002060 if (ops->set_cr(3, tss->cr3, ctxt->vcpu))
2061 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002062 c->eip = tss->eip;
2063 ctxt->eflags = tss->eflags | 2;
2064 c->regs[VCPU_REGS_RAX] = tss->eax;
2065 c->regs[VCPU_REGS_RCX] = tss->ecx;
2066 c->regs[VCPU_REGS_RDX] = tss->edx;
2067 c->regs[VCPU_REGS_RBX] = tss->ebx;
2068 c->regs[VCPU_REGS_RSP] = tss->esp;
2069 c->regs[VCPU_REGS_RBP] = tss->ebp;
2070 c->regs[VCPU_REGS_RSI] = tss->esi;
2071 c->regs[VCPU_REGS_RDI] = tss->edi;
2072
2073 /*
2074 * SDM says that segment selectors are loaded before segment
2075 * descriptors
2076 */
2077 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
2078 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
2079 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
2080 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
2081 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
2082 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
2083 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
2084
2085 /*
2086 * Now load segment descriptors. If fault happenes at this stage
2087 * it is handled in a context of new task
2088 */
2089 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2090 if (ret != X86EMUL_CONTINUE)
2091 return ret;
2092 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2093 if (ret != X86EMUL_CONTINUE)
2094 return ret;
2095 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2096 if (ret != X86EMUL_CONTINUE)
2097 return ret;
2098 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2099 if (ret != X86EMUL_CONTINUE)
2100 return ret;
2101 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2102 if (ret != X86EMUL_CONTINUE)
2103 return ret;
2104 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2105 if (ret != X86EMUL_CONTINUE)
2106 return ret;
2107 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2108 if (ret != X86EMUL_CONTINUE)
2109 return ret;
2110
2111 return X86EMUL_CONTINUE;
2112}
2113
2114static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2115 struct x86_emulate_ops *ops,
2116 u16 tss_selector, u16 old_tss_sel,
2117 ulong old_tss_base, struct desc_struct *new_desc)
2118{
2119 struct tss_segment_32 tss_seg;
2120 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002121 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002122
2123 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002124 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002125 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002126 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002127 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002128
2129 save_state_to_tss32(ctxt, ops, &tss_seg);
2130
2131 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002132 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002133 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002134 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002135 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002136
2137 ret = ops->read_std(new_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 if (old_tss_sel != 0xffff) {
2144 tss_seg.prev_task_link = old_tss_sel;
2145
2146 ret = ops->write_std(new_tss_base,
2147 &tss_seg.prev_task_link,
2148 sizeof tss_seg.prev_task_link,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002149 ctxt->vcpu, &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002150 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002151 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153 }
2154
2155 return load_state_from_tss32(ctxt, ops, &tss_seg);
2156}
2157
2158static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002159 struct x86_emulate_ops *ops,
2160 u16 tss_selector, int reason,
2161 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162{
2163 struct desc_struct curr_tss_desc, next_tss_desc;
2164 int ret;
2165 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2166 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002167 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002168 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002169
2170 /* FIXME: old_tss_base == ~0 ? */
2171
2172 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2173 if (ret != X86EMUL_CONTINUE)
2174 return ret;
2175 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2176 if (ret != X86EMUL_CONTINUE)
2177 return ret;
2178
2179 /* FIXME: check that next_tss_desc is tss */
2180
2181 if (reason != TASK_SWITCH_IRET) {
2182 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002183 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl)
2184 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185 }
2186
Gleb Natapovceffb452010-03-18 15:20:19 +02002187 desc_limit = desc_limit_scaled(&next_tss_desc);
2188 if (!next_tss_desc.p ||
2189 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2190 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002191 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002192 return X86EMUL_PROPAGATE_FAULT;
2193 }
2194
2195 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2196 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2197 write_segment_descriptor(ctxt, ops, old_tss_sel,
2198 &curr_tss_desc);
2199 }
2200
2201 if (reason == TASK_SWITCH_IRET)
2202 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2203
2204 /* set back link to prev task only if NT bit is set in eflags
2205 note that old_tss_sel is not used afetr this point */
2206 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2207 old_tss_sel = 0xffff;
2208
2209 if (next_tss_desc.type & 8)
2210 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2211 old_tss_base, &next_tss_desc);
2212 else
2213 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2214 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002215 if (ret != X86EMUL_CONTINUE)
2216 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002217
2218 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2219 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2220
2221 if (reason != TASK_SWITCH_IRET) {
2222 next_tss_desc.type |= (1 << 1); /* set busy flag */
2223 write_segment_descriptor(ctxt, ops, tss_selector,
2224 &next_tss_desc);
2225 }
2226
2227 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +02002228 ops->set_cached_descriptor(&next_tss_desc, 0, VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002229 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2230
Jan Kiszkae269fb22010-04-14 15:51:09 +02002231 if (has_error_code) {
2232 struct decode_cache *c = &ctxt->decode;
2233
2234 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2235 c->lock_prefix = 0;
2236 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002237 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002238 }
2239
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002240 return ret;
2241}
2242
2243int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002244 u16 tss_selector, int reason,
2245 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002246{
Avi Kivity9aabc882010-07-29 15:11:50 +03002247 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002248 struct decode_cache *c = &ctxt->decode;
2249 int rc;
2250
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002251 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002252 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002253
Jan Kiszkae269fb22010-04-14 15:51:09 +02002254 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2255 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002256
2257 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002258 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002259 if (rc == X86EMUL_CONTINUE)
2260 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002261 }
2262
Gleb Natapov19d04432010-04-15 12:29:50 +03002263 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002264}
2265
Avi Kivity90de84f2010-11-17 15:28:21 +02002266static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002267 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002268{
2269 struct decode_cache *c = &ctxt->decode;
2270 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2271
Gleb Natapovd9271122010-03-18 15:20:22 +02002272 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002273 op->addr.mem.ea = register_address(c, c->regs[reg]);
2274 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002275}
2276
Avi Kivity63540382010-07-29 15:11:55 +03002277static int em_push(struct x86_emulate_ctxt *ctxt)
2278{
2279 emulate_push(ctxt, ctxt->ops);
2280 return X86EMUL_CONTINUE;
2281}
2282
Avi Kivity7af04fc2010-08-18 14:16:35 +03002283static int em_das(struct x86_emulate_ctxt *ctxt)
2284{
2285 struct decode_cache *c = &ctxt->decode;
2286 u8 al, old_al;
2287 bool af, cf, old_cf;
2288
2289 cf = ctxt->eflags & X86_EFLAGS_CF;
2290 al = c->dst.val;
2291
2292 old_al = al;
2293 old_cf = cf;
2294 cf = false;
2295 af = ctxt->eflags & X86_EFLAGS_AF;
2296 if ((al & 0x0f) > 9 || af) {
2297 al -= 6;
2298 cf = old_cf | (al >= 250);
2299 af = true;
2300 } else {
2301 af = false;
2302 }
2303 if (old_al > 0x99 || old_cf) {
2304 al -= 0x60;
2305 cf = true;
2306 }
2307
2308 c->dst.val = al;
2309 /* Set PF, ZF, SF */
2310 c->src.type = OP_IMM;
2311 c->src.val = 0;
2312 c->src.bytes = 1;
2313 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2314 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2315 if (cf)
2316 ctxt->eflags |= X86_EFLAGS_CF;
2317 if (af)
2318 ctxt->eflags |= X86_EFLAGS_AF;
2319 return X86EMUL_CONTINUE;
2320}
2321
Avi Kivity0ef753b2010-08-18 14:51:45 +03002322static int em_call_far(struct x86_emulate_ctxt *ctxt)
2323{
2324 struct decode_cache *c = &ctxt->decode;
2325 u16 sel, old_cs;
2326 ulong old_eip;
2327 int rc;
2328
2329 old_cs = ctxt->ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
2330 old_eip = c->eip;
2331
2332 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2333 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2334 return X86EMUL_CONTINUE;
2335
2336 c->eip = 0;
2337 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2338
2339 c->src.val = old_cs;
2340 emulate_push(ctxt, ctxt->ops);
2341 rc = writeback(ctxt, ctxt->ops);
2342 if (rc != X86EMUL_CONTINUE)
2343 return rc;
2344
2345 c->src.val = old_eip;
2346 emulate_push(ctxt, ctxt->ops);
2347 rc = writeback(ctxt, ctxt->ops);
2348 if (rc != X86EMUL_CONTINUE)
2349 return rc;
2350
2351 c->dst.type = OP_NONE;
2352
2353 return X86EMUL_CONTINUE;
2354}
2355
Avi Kivity40ece7c2010-08-18 15:12:09 +03002356static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2357{
2358 struct decode_cache *c = &ctxt->decode;
2359 int rc;
2360
2361 c->dst.type = OP_REG;
2362 c->dst.addr.reg = &c->eip;
2363 c->dst.bytes = c->op_bytes;
2364 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2365 if (rc != X86EMUL_CONTINUE)
2366 return rc;
2367 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2368 return X86EMUL_CONTINUE;
2369}
2370
Avi Kivity5c82aa22010-08-18 18:31:43 +03002371static int em_imul(struct x86_emulate_ctxt *ctxt)
2372{
2373 struct decode_cache *c = &ctxt->decode;
2374
2375 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2376 return X86EMUL_CONTINUE;
2377}
2378
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002379static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2380{
2381 struct decode_cache *c = &ctxt->decode;
2382
2383 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002384 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002385}
2386
Avi Kivity61429142010-08-19 15:13:00 +03002387static int em_cwd(struct x86_emulate_ctxt *ctxt)
2388{
2389 struct decode_cache *c = &ctxt->decode;
2390
2391 c->dst.type = OP_REG;
2392 c->dst.bytes = c->src.bytes;
2393 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2394 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2395
2396 return X86EMUL_CONTINUE;
2397}
2398
Avi Kivity48bb5d32010-08-18 18:54:34 +03002399static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2400{
2401 unsigned cpl = ctxt->ops->cpl(ctxt->vcpu);
2402 struct decode_cache *c = &ctxt->decode;
2403 u64 tsc = 0;
2404
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002405 if (cpl > 0 && (ctxt->ops->get_cr(4, ctxt->vcpu) & X86_CR4_TSD))
2406 return emulate_gp(ctxt, 0);
Avi Kivity48bb5d32010-08-18 18:54:34 +03002407 ctxt->ops->get_msr(ctxt->vcpu, MSR_IA32_TSC, &tsc);
2408 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2409 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2410 return X86EMUL_CONTINUE;
2411}
2412
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002413static int em_mov(struct x86_emulate_ctxt *ctxt)
2414{
2415 struct decode_cache *c = &ctxt->decode;
2416 c->dst.val = c->src.val;
2417 return X86EMUL_CONTINUE;
2418}
2419
Avi Kivityaa97bb42010-01-20 18:09:23 +02002420static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2421{
2422 struct decode_cache *c = &ctxt->decode;
2423 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2424 return X86EMUL_CONTINUE;
2425}
2426
Avi Kivity73fba5f2010-07-29 15:11:53 +03002427#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002428#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002429#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2430 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002431#define N D(0)
2432#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2433#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2434#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002435#define II(_f, _e, _i) \
2436 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002437#define IIP(_f, _e, _i, _p) \
2438 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2439 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002440#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002441
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002442#define D2bv(_f) D((_f) | ByteOp), D(_f)
2443#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2444
Avi Kivity6230f7f2010-08-26 18:34:55 +03002445#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2446 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2447 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2448
2449
Avi Kivity73fba5f2010-07-29 15:11:53 +03002450static struct opcode group1[] = {
2451 X7(D(Lock)), N
2452};
2453
2454static struct opcode group1A[] = {
2455 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2456};
2457
2458static struct opcode group3[] = {
2459 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2460 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002461 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002462};
2463
2464static struct opcode group4[] = {
2465 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2466 N, N, N, N, N, N,
2467};
2468
2469static struct opcode group5[] = {
2470 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002471 D(SrcMem | ModRM | Stack),
2472 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002473 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2474 D(SrcMem | ModRM | Stack), N,
2475};
2476
2477static struct group_dual group7 = { {
Avi Kivity3c6e2762011-04-04 12:39:23 +02002478 N, N, DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
2479 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2480 DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
2481 DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002482}, {
Avi Kivityd8671622011-02-01 16:32:03 +02002483 D(SrcNone | ModRM | Priv | VendorSpecific), N,
2484 N, D(SrcNone | ModRM | Priv | VendorSpecific),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002485 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2486 DI(SrcMem16 | ModRM | Mov | Priv, lmsw), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002487} };
2488
2489static struct opcode group8[] = {
2490 N, N, N, N,
2491 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2492 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2493};
2494
2495static struct group_dual group9 = { {
2496 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2497}, {
2498 N, N, N, N, N, N, N, N,
2499} };
2500
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002501static struct opcode group11[] = {
2502 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2503};
2504
Avi Kivityaa97bb42010-01-20 18:09:23 +02002505static struct gprefix pfx_0f_6f_0f_7f = {
2506 N, N, N, I(Sse, em_movdqu),
2507};
2508
Avi Kivity73fba5f2010-07-29 15:11:53 +03002509static struct opcode opcode_table[256] = {
2510 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002511 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002512 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2513 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002514 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002515 D(ImplicitOps | Stack | No64), N,
2516 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002517 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002518 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2519 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002520 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002521 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2522 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002523 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002524 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002525 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002526 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002527 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002528 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002529 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002530 /* 0x40 - 0x4F */
2531 X16(D(DstReg)),
2532 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002533 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002534 /* 0x58 - 0x5F */
2535 X8(D(DstReg | Stack)),
2536 /* 0x60 - 0x67 */
2537 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2538 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2539 N, N, N, N,
2540 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002541 I(SrcImm | Mov | Stack, em_push),
2542 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002543 I(SrcImmByte | Mov | Stack, em_push),
2544 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002545 D2bv(DstDI | Mov | String), /* insb, insw/insd */
2546 D2bv(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002547 /* 0x70 - 0x7F */
2548 X16(D(SrcImmByte)),
2549 /* 0x80 - 0x87 */
2550 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2551 G(DstMem | SrcImm | ModRM | Group, group1),
2552 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2553 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002554 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002555 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002556 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2557 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002558 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002559 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2560 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002561 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002562 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002563 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002564 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002565 DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002566 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002567 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2568 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2569 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2570 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002571 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002572 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002573 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2574 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002575 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002576 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002577 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002578 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002579 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002580 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002581 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002582 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2583 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002584 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002585 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002586 /* 0xC8 - 0xCF */
2587 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002588 D(ImplicitOps), DI(SrcImmByte, intn),
2589 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002590 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002591 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002592 N, N, N, N,
2593 /* 0xD8 - 0xDF */
2594 N, N, N, N, N, N, N, N,
2595 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002596 X4(D(SrcImmByte)),
Avi Kivityd269e392010-08-26 11:56:12 +03002597 D2bv(SrcImmUByte | DstAcc), D2bv(SrcAcc | DstImmUByte),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002598 /* 0xE8 - 0xEF */
2599 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2600 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Avi Kivityd269e392010-08-26 11:56:12 +03002601 D2bv(SrcNone | DstAcc), D2bv(SrcAcc | ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002602 /* 0xF0 - 0xF7 */
2603 N, N, N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002604 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
2605 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002606 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002607 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002608 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2609};
2610
2611static struct opcode twobyte_table[256] = {
2612 /* 0x00 - 0x0F */
2613 N, GD(0, &group7), N, N,
Avi Kivityd8671622011-02-01 16:32:03 +02002614 N, D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002615 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002616 N, D(ImplicitOps | ModRM), N, N,
2617 /* 0x10 - 0x1F */
2618 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2619 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002620 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2621 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002622 N, N, N, N,
2623 N, N, N, N, N, N, N, N,
2624 /* 0x30 - 0x3F */
Avi Kivity3c6e2762011-04-04 12:39:23 +02002625 D(ImplicitOps | Priv), II(ImplicitOps, em_rdtsc, rdtsc),
Avi Kivity48bb5d32010-08-18 18:54:34 +03002626 D(ImplicitOps | Priv), N,
Avi Kivityd8671622011-02-01 16:32:03 +02002627 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
2628 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002629 N, N, N, N, N, N, N, N,
2630 /* 0x40 - 0x4F */
2631 X16(D(DstReg | SrcMem | ModRM | Mov)),
2632 /* 0x50 - 0x5F */
2633 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2634 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002635 N, N, N, N,
2636 N, N, N, N,
2637 N, N, N, N,
2638 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002639 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02002640 N, N, N, N,
2641 N, N, N, N,
2642 N, N, N, N,
2643 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002644 /* 0x80 - 0x8F */
2645 X16(D(SrcImm)),
2646 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08002647 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002648 /* 0xA0 - 0xA7 */
2649 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2650 N, D(DstMem | SrcReg | ModRM | BitOp),
2651 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2652 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2653 /* 0xA8 - 0xAF */
2654 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2655 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2656 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2657 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03002658 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002659 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03002660 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002661 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2662 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
2663 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002664 /* 0xB8 - 0xBF */
2665 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002666 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08002667 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2668 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002669 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03002670 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08002671 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002672 N, N, N, GD(0, &group9),
2673 N, N, N, N, N, N, N, N,
2674 /* 0xD0 - 0xDF */
2675 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2676 /* 0xE0 - 0xEF */
2677 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2678 /* 0xF0 - 0xFF */
2679 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2680};
2681
2682#undef D
2683#undef N
2684#undef G
2685#undef GD
2686#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02002687#undef GP
Avi Kivity73fba5f2010-07-29 15:11:53 +03002688
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002689#undef D2bv
2690#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03002691#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002692
Avi Kivity39f21ee2010-08-18 19:20:21 +03002693static unsigned imm_size(struct decode_cache *c)
2694{
2695 unsigned size;
2696
2697 size = (c->d & ByteOp) ? 1 : c->op_bytes;
2698 if (size == 8)
2699 size = 4;
2700 return size;
2701}
2702
2703static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
2704 unsigned size, bool sign_extension)
2705{
2706 struct decode_cache *c = &ctxt->decode;
2707 struct x86_emulate_ops *ops = ctxt->ops;
2708 int rc = X86EMUL_CONTINUE;
2709
2710 op->type = OP_IMM;
2711 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02002712 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03002713 /* NB. Immediates are sign-extended as necessary. */
2714 switch (op->bytes) {
2715 case 1:
2716 op->val = insn_fetch(s8, 1, c->eip);
2717 break;
2718 case 2:
2719 op->val = insn_fetch(s16, 2, c->eip);
2720 break;
2721 case 4:
2722 op->val = insn_fetch(s32, 4, c->eip);
2723 break;
2724 }
2725 if (!sign_extension) {
2726 switch (op->bytes) {
2727 case 1:
2728 op->val &= 0xff;
2729 break;
2730 case 2:
2731 op->val &= 0xffff;
2732 break;
2733 case 4:
2734 op->val &= 0xffffffff;
2735 break;
2736 }
2737 }
2738done:
2739 return rc;
2740}
2741
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002742int
Andre Przywaradc25e892010-12-21 11:12:07 +01002743x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002744{
2745 struct x86_emulate_ops *ops = ctxt->ops;
2746 struct decode_cache *c = &ctxt->decode;
2747 int rc = X86EMUL_CONTINUE;
2748 int mode = ctxt->mode;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002749 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
2750 bool op_prefix = false;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002751 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002752 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002753
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002754 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01002755 c->fetch.start = c->eip;
2756 c->fetch.end = c->fetch.start + insn_len;
2757 if (insn_len > 0)
2758 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002759 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2760
2761 switch (mode) {
2762 case X86EMUL_MODE_REAL:
2763 case X86EMUL_MODE_VM86:
2764 case X86EMUL_MODE_PROT16:
2765 def_op_bytes = def_ad_bytes = 2;
2766 break;
2767 case X86EMUL_MODE_PROT32:
2768 def_op_bytes = def_ad_bytes = 4;
2769 break;
2770#ifdef CONFIG_X86_64
2771 case X86EMUL_MODE_PROT64:
2772 def_op_bytes = 4;
2773 def_ad_bytes = 8;
2774 break;
2775#endif
2776 default:
2777 return -1;
2778 }
2779
2780 c->op_bytes = def_op_bytes;
2781 c->ad_bytes = def_ad_bytes;
2782
2783 /* Legacy prefixes. */
2784 for (;;) {
2785 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2786 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002787 op_prefix = true;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002788 /* switch between 2/4 bytes */
2789 c->op_bytes = def_op_bytes ^ 6;
2790 break;
2791 case 0x67: /* address-size override */
2792 if (mode == X86EMUL_MODE_PROT64)
2793 /* switch between 4/8 bytes */
2794 c->ad_bytes = def_ad_bytes ^ 12;
2795 else
2796 /* switch between 2/4 bytes */
2797 c->ad_bytes = def_ad_bytes ^ 6;
2798 break;
2799 case 0x26: /* ES override */
2800 case 0x2e: /* CS override */
2801 case 0x36: /* SS override */
2802 case 0x3e: /* DS override */
2803 set_seg_override(c, (c->b >> 3) & 3);
2804 break;
2805 case 0x64: /* FS override */
2806 case 0x65: /* GS override */
2807 set_seg_override(c, c->b & 7);
2808 break;
2809 case 0x40 ... 0x4f: /* REX */
2810 if (mode != X86EMUL_MODE_PROT64)
2811 goto done_prefixes;
2812 c->rex_prefix = c->b;
2813 continue;
2814 case 0xf0: /* LOCK */
2815 c->lock_prefix = 1;
2816 break;
2817 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002818 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02002819 c->rep_prefix = c->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002820 break;
2821 default:
2822 goto done_prefixes;
2823 }
2824
2825 /* Any legacy prefix after a REX prefix nullifies its effect. */
2826
2827 c->rex_prefix = 0;
2828 }
2829
2830done_prefixes:
2831
2832 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002833 if (c->rex_prefix & 8)
2834 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002835
2836 /* Opcode byte(s). */
2837 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002838 /* Two-byte opcode? */
2839 if (c->b == 0x0f) {
2840 c->twobyte = 1;
2841 c->b = insn_fetch(u8, 1, c->eip);
2842 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002843 }
2844 c->d = opcode.flags;
2845
2846 if (c->d & Group) {
2847 dual = c->d & GroupDual;
2848 c->modrm = insn_fetch(u8, 1, c->eip);
2849 --c->eip;
2850
2851 if (c->d & GroupDual) {
2852 g_mod012 = opcode.u.gdual->mod012;
2853 g_mod3 = opcode.u.gdual->mod3;
2854 } else
2855 g_mod012 = g_mod3 = opcode.u.group;
2856
2857 c->d &= ~(Group | GroupDual);
2858
2859 goffset = (c->modrm >> 3) & 7;
2860
2861 if ((c->modrm >> 6) == 3)
2862 opcode = g_mod3[goffset];
2863 else
2864 opcode = g_mod012[goffset];
2865 c->d |= opcode.flags;
2866 }
2867
Avi Kivity0d7cdee2011-03-29 11:34:38 +02002868 if (c->d & Prefix) {
2869 if (c->rep_prefix && op_prefix)
2870 return X86EMUL_UNHANDLEABLE;
2871 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
2872 switch (simd_prefix) {
2873 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
2874 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
2875 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
2876 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
2877 }
2878 c->d |= opcode.flags;
2879 }
2880
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002881 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02002882 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02002883 c->intercept = opcode.intercept;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002884
2885 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02002886 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002887 return -1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002888
Avi Kivityd8671622011-02-01 16:32:03 +02002889 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
2890 return -1;
2891
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002892 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2893 c->op_bytes = 8;
2894
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002895 if (c->d & Op3264) {
2896 if (mode == X86EMUL_MODE_PROT64)
2897 c->op_bytes = 8;
2898 else
2899 c->op_bytes = 4;
2900 }
2901
Avi Kivity12537912011-03-29 11:41:27 +02002902 if (c->d & Sse)
2903 c->op_bytes = 16;
2904
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002905 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002906 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002907 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002908 if (!c->has_seg_override)
2909 set_seg_override(c, c->modrm_seg);
2910 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002911 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002912 if (rc != X86EMUL_CONTINUE)
2913 goto done;
2914
2915 if (!c->has_seg_override)
2916 set_seg_override(c, VCPU_SREG_DS);
2917
Avi Kivity90de84f2010-11-17 15:28:21 +02002918 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002919
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002920 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02002921 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002922
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002923 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02002924 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002925
2926 /*
2927 * Decode and fetch the source operand: register, memory
2928 * or immediate.
2929 */
2930 switch (c->d & SrcMask) {
2931 case SrcNone:
2932 break;
2933 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02002934 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002935 break;
2936 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002937 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002938 goto srcmem_common;
2939 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002940 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002941 goto srcmem_common;
2942 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002943 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002944 c->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002945 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002946 c->src = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002947 break;
Avi Kivityb250e602010-08-18 15:11:24 +03002948 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002949 rc = decode_imm(ctxt, &c->src, 2, false);
2950 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002951 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002952 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
2953 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002954 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002955 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002956 break;
2957 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002958 rc = decode_imm(ctxt, &c->src, 1, true);
2959 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002960 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03002961 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002962 break;
2963 case SrcAcc:
2964 c->src.type = OP_REG;
2965 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002966 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002967 fetch_register_operand(&c->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002968 break;
2969 case SrcOne:
2970 c->src.bytes = 1;
2971 c->src.val = 1;
2972 break;
2973 case SrcSI:
2974 c->src.type = OP_MEM;
2975 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02002976 c->src.addr.mem.ea =
2977 register_address(c, c->regs[VCPU_REGS_RSI]);
2978 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002979 c->src.val = 0;
2980 break;
2981 case SrcImmFAddr:
2982 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02002983 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002984 c->src.bytes = c->op_bytes + 2;
2985 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2986 break;
2987 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002988 memop.bytes = c->op_bytes + 2;
2989 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002990 break;
2991 }
2992
Avi Kivity39f21ee2010-08-18 19:20:21 +03002993 if (rc != X86EMUL_CONTINUE)
2994 goto done;
2995
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002996 /*
2997 * Decode and fetch the second source operand: register, memory
2998 * or immediate.
2999 */
3000 switch (c->d & Src2Mask) {
3001 case Src2None:
3002 break;
3003 case Src2CL:
3004 c->src2.bytes = 1;
3005 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3006 break;
3007 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003008 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003009 break;
3010 case Src2One:
3011 c->src2.bytes = 1;
3012 c->src2.val = 1;
3013 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003014 case Src2Imm:
3015 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3016 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003017 }
3018
Avi Kivity39f21ee2010-08-18 19:20:21 +03003019 if (rc != X86EMUL_CONTINUE)
3020 goto done;
3021
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003022 /* Decode and fetch the destination operand: register or memory. */
3023 switch (c->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003024 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003025 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003026 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3027 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003028 case DstImmUByte:
3029 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003030 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003031 c->dst.bytes = 1;
3032 c->dst.val = insn_fetch(u8, 1, c->eip);
3033 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003034 case DstMem:
3035 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003036 c->dst = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003037 if ((c->d & DstMask) == DstMem64)
3038 c->dst.bytes = 8;
3039 else
3040 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003041 if (c->d & BitOp)
3042 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003043 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003044 break;
3045 case DstAcc:
3046 c->dst.type = OP_REG;
3047 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003048 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003049 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003050 c->dst.orig_val = c->dst.val;
3051 break;
3052 case DstDI:
3053 c->dst.type = OP_MEM;
3054 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003055 c->dst.addr.mem.ea =
3056 register_address(c, c->regs[VCPU_REGS_RDI]);
3057 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003058 c->dst.val = 0;
3059 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003060 case ImplicitOps:
3061 /* Special instructions do their own operand decoding. */
3062 default:
3063 c->dst.type = OP_NONE; /* Disable writeback. */
3064 return 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003065 }
3066
3067done:
3068 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
3069}
3070
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003071static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3072{
3073 struct decode_cache *c = &ctxt->decode;
3074
3075 /* The second termination condition only applies for REPE
3076 * and REPNE. Test if the repeat string operation prefix is
3077 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3078 * corresponding termination condition according to:
3079 * - if REPE/REPZ and ZF = 0 then done
3080 * - if REPNE/REPNZ and ZF = 1 then done
3081 */
3082 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3083 (c->b == 0xae) || (c->b == 0xaf))
3084 && (((c->rep_prefix == REPE_PREFIX) &&
3085 ((ctxt->eflags & EFLG_ZF) == 0))
3086 || ((c->rep_prefix == REPNE_PREFIX) &&
3087 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3088 return true;
3089
3090 return false;
3091}
3092
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003093int
Avi Kivity9aabc882010-07-29 15:11:50 +03003094x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003095{
Avi Kivity9aabc882010-07-29 15:11:50 +03003096 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003097 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003098 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003099 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003100 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003101 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003102
Gleb Natapov9de41572010-04-28 19:15:22 +03003103 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003104
Gleb Natapov11616242010-02-11 14:43:14 +02003105 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003106 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003107 goto done;
3108 }
3109
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003110 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003111 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003112 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003113 goto done;
3114 }
3115
Avi Kivity081bca02010-08-26 11:06:15 +03003116 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003117 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003118 goto done;
3119 }
3120
Avi Kivity12537912011-03-29 11:41:27 +02003121 if ((c->d & Sse)
3122 && ((ops->get_cr(0, ctxt->vcpu) & X86_CR0_EM)
3123 || !(ops->get_cr(4, ctxt->vcpu) & X86_CR4_OSFXSR))) {
3124 rc = emulate_ud(ctxt);
3125 goto done;
3126 }
3127
3128 if ((c->d & Sse) && (ops->get_cr(0, ctxt->vcpu) & X86_CR0_TS)) {
3129 rc = emulate_nm(ctxt);
3130 goto done;
3131 }
3132
Avi Kivityc4f035c2011-04-04 12:39:22 +02003133 if (unlikely(ctxt->guest_mode) && c->intercept) {
3134 rc = ops->intercept(ctxt, c->intercept,
3135 X86_ICPT_PRE_EXCEPT);
3136 if (rc != X86EMUL_CONTINUE)
3137 goto done;
3138 }
3139
Gleb Natapove92805a2010-02-10 14:21:35 +02003140 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02003141 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003142 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003143 goto done;
3144 }
3145
Joerg Roedeld09beab2011-04-04 12:39:25 +02003146 /* Do instruction specific permission checks */
3147 if (c->check_perm) {
3148 rc = c->check_perm(ctxt);
3149 if (rc != X86EMUL_CONTINUE)
3150 goto done;
3151 }
3152
Avi Kivityc4f035c2011-04-04 12:39:22 +02003153 if (unlikely(ctxt->guest_mode) && c->intercept) {
3154 rc = ops->intercept(ctxt, c->intercept,
3155 X86_ICPT_POST_EXCEPT);
3156 if (rc != X86EMUL_CONTINUE)
3157 goto done;
3158 }
3159
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003160 if (c->rep_prefix && (c->d & String)) {
3161 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003162 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003163 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003164 goto done;
3165 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003166 }
3167
Wei Yongjunc483c022010-08-06 15:36:36 +08003168 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003169 rc = read_emulated(ctxt, ops, linear(ctxt, c->src.addr.mem),
Gleb Natapov414e6272010-04-28 19:15:26 +03003170 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003171 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003172 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003173 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003174 }
3175
Gleb Natapove35b7b92010-02-25 16:36:42 +02003176 if (c->src2.type == OP_MEM) {
Avi Kivity90de84f2010-11-17 15:28:21 +02003177 rc = read_emulated(ctxt, ops, linear(ctxt, c->src2.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003178 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003179 if (rc != X86EMUL_CONTINUE)
3180 goto done;
3181 }
3182
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003183 if ((c->d & DstMask) == ImplicitOps)
3184 goto special_insn;
3185
3186
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003187 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3188 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity90de84f2010-11-17 15:28:21 +02003189 rc = read_emulated(ctxt, ops, linear(ctxt, c->dst.addr.mem),
Gleb Natapov9de41572010-04-28 19:15:22 +03003190 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003191 if (rc != X86EMUL_CONTINUE)
3192 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003193 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003194 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003195
Avi Kivity018a98d2007-11-27 19:30:56 +02003196special_insn:
3197
Avi Kivityc4f035c2011-04-04 12:39:22 +02003198 if (unlikely(ctxt->guest_mode) && c->intercept) {
3199 rc = ops->intercept(ctxt, c->intercept,
3200 X86_ICPT_POST_MEMACCESS);
3201 if (rc != X86EMUL_CONTINUE)
3202 goto done;
3203 }
3204
Avi Kivityef65c882010-07-29 15:11:51 +03003205 if (c->execute) {
3206 rc = c->execute(ctxt);
3207 if (rc != X86EMUL_CONTINUE)
3208 goto done;
3209 goto writeback;
3210 }
3211
Laurent Viviere4e03de2007-09-18 11:52:50 +02003212 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 goto twobyte_insn;
3214
Laurent Viviere4e03de2007-09-18 11:52:50 +02003215 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 case 0x00 ... 0x05:
3217 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003218 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003220 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003221 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003222 break;
3223 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003224 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003225 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 case 0x08 ... 0x0d:
3227 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003228 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003230 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003231 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003232 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 case 0x10 ... 0x15:
3234 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003235 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003237 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003238 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003239 break;
3240 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003241 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003242 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 case 0x18 ... 0x1d:
3244 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003245 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003247 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003248 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003249 break;
3250 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003251 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003252 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003253 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003255 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 break;
3257 case 0x28 ... 0x2d:
3258 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003259 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260 break;
3261 case 0x30 ... 0x35:
3262 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003263 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264 break;
3265 case 0x38 ... 0x3d:
3266 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003267 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003269 case 0x40 ... 0x47: /* inc r16/r32 */
3270 emulate_1op("inc", c->dst, ctxt->eflags);
3271 break;
3272 case 0x48 ... 0x4f: /* dec r16/r32 */
3273 emulate_1op("dec", c->dst, ctxt->eflags);
3274 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003275 case 0x58 ... 0x5f: /* pop reg */
3276 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003277 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003278 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003279 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08003280 rc = emulate_pusha(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003281 break;
3282 case 0x61: /* popa */
3283 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003284 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003286 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003288 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003290 case 0x6c: /* insb */
3291 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003292 c->src.val = c->regs[VCPU_REGS_RDX];
3293 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003294 case 0x6e: /* outsb */
3295 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003296 c->dst.val = c->regs[VCPU_REGS_RDX];
3297 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003298 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003299 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003300 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003301 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003302 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003304 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 case 0:
3306 goto add;
3307 case 1:
3308 goto or;
3309 case 2:
3310 goto adc;
3311 case 3:
3312 goto sbb;
3313 case 4:
3314 goto and;
3315 case 5:
3316 goto sub;
3317 case 6:
3318 goto xor;
3319 case 7:
3320 goto cmp;
3321 }
3322 break;
3323 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003324 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003325 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326 break;
3327 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003328 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003330 c->src.val = c->dst.val;
3331 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 /*
3333 * Write back the memory destination with implicit LOCK
3334 * prefix.
3335 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003336 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003337 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003338 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003339 case 0x8c: /* mov r/m, sreg */
3340 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003341 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003342 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003343 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003344 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003345 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003346 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003347 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003348 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003349 case 0x8e: { /* mov seg, r/m16 */
3350 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003351
3352 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003353
Gleb Natapovc6975182010-02-18 12:15:01 +02003354 if (c->modrm_reg == VCPU_SREG_CS ||
3355 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003356 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003357 goto done;
3358 }
3359
Glauber Costa310b5d32009-05-12 16:21:06 -04003360 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003361 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003362
Gleb Natapov2e873022010-03-18 15:20:18 +02003363 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003364
3365 c->dst.type = OP_NONE; /* Disable writeback. */
3366 break;
3367 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003369 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003371 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3372 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003373 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003374 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003375 case 0x98: /* cbw/cwde/cdqe */
3376 switch (c->op_bytes) {
3377 case 2: c->dst.val = (s8)c->dst.val; break;
3378 case 4: c->dst.val = (s16)c->dst.val; break;
3379 case 8: c->dst.val = (s32)c->dst.val; break;
3380 }
3381 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003382 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003383 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003384 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003385 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003386 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003387 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003388 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003389 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003390 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003391 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003393 c->dst.type = OP_NONE; /* Disable writeback. */
Gleb Natapova682e352010-03-18 15:20:21 +02003394 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003395 case 0xa8 ... 0xa9: /* test ax, imm */
3396 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003398 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003399 case 0xc0 ... 0xc1:
3400 emulate_grp2(ctxt);
3401 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003402 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003403 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003404 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003405 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003406 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003407 case 0xc4: /* les */
3408 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003409 break;
3410 case 0xc5: /* lds */
3411 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003412 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003413 case 0xcb: /* ret far */
3414 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003415 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003416 case 0xcc: /* int3 */
3417 irq = 3;
3418 goto do_interrupt;
3419 case 0xcd: /* int n */
3420 irq = c->src.val;
3421 do_interrupt:
3422 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003423 break;
3424 case 0xce: /* into */
3425 if (ctxt->eflags & EFLG_OF) {
3426 irq = 4;
3427 goto do_interrupt;
3428 }
3429 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003430 case 0xcf: /* iret */
3431 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003432 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003433 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003434 emulate_grp2(ctxt);
3435 break;
3436 case 0xd2 ... 0xd3: /* Grp2 */
3437 c->src.val = c->regs[VCPU_REGS_RCX];
3438 emulate_grp2(ctxt);
3439 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003440 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3441 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3442 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3443 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3444 jmp_rel(c, c->src.val);
3445 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003446 case 0xe3: /* jcxz/jecxz/jrcxz */
3447 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3448 jmp_rel(c, c->src.val);
3449 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003450 case 0xe4: /* inb */
3451 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003452 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003453 case 0xe6: /* outb */
3454 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003455 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003456 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003457 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003458 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003459 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003460 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003461 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003462 }
3463 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003464 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003465 case 0xea: { /* jmp far */
3466 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02003467 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003468 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3469
3470 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003471 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003472
Gleb Natapov414e6272010-04-28 19:15:26 +03003473 c->eip = 0;
3474 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003475 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003476 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003477 case 0xeb:
3478 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003479 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003480 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003481 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003482 case 0xec: /* in al,dx */
3483 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003484 c->src.val = c->regs[VCPU_REGS_RDX];
3485 do_io_in:
3486 c->dst.bytes = min(c->dst.bytes, 4u);
3487 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003488 rc = emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003489 goto done;
3490 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003491 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3492 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003493 goto done; /* IO is needed */
3494 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003495 case 0xee: /* out dx,al */
3496 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003497 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003498 do_io_out:
Wei Yongjun41167be2010-08-06 11:45:12 +08003499 c->src.bytes = min(c->src.bytes, 4u);
3500 if (!emulator_io_permited(ctxt, ops, c->dst.val,
3501 c->src.bytes)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003502 rc = emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003503 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003504 }
Wei Yongjun41167be2010-08-06 11:45:12 +08003505 ops->pio_out_emulated(c->src.bytes, c->dst.val,
3506 &c->src.val, 1, ctxt->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003507 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003508 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003509 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003510 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003511 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003512 case 0xf5: /* cmc */
3513 /* complement carry flag from eflags reg */
3514 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003515 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003516 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003517 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003518 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003519 case 0xf8: /* clc */
3520 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003521 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003522 case 0xf9: /* stc */
3523 ctxt->eflags |= EFLG_CF;
3524 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003525 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003526 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003527 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003528 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003529 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003530 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003531 break;
3532 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003533 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003534 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003535 goto done;
3536 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003537 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003538 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003539 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003540 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003541 case 0xfc: /* cld */
3542 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003543 break;
3544 case 0xfd: /* std */
3545 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003546 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003547 case 0xfe: /* Grp4 */
3548 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003549 rc = emulate_grp45(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003550 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003551 case 0xff: /* Grp5 */
3552 if (c->modrm_reg == 5)
3553 goto jump_far;
3554 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003555 default:
3556 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003558
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003559 if (rc != X86EMUL_CONTINUE)
3560 goto done;
3561
Avi Kivity018a98d2007-11-27 19:30:56 +02003562writeback:
3563 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003564 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003565 goto done;
3566
Gleb Natapov5cd21912010-03-18 15:20:26 +02003567 /*
3568 * restore dst type in case the decoding will be reused
3569 * (happens for string instruction )
3570 */
3571 c->dst.type = saved_dst_type;
3572
Gleb Natapova682e352010-03-18 15:20:21 +02003573 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003574 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003575 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003576
3577 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003578 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003579 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003580
Gleb Natapov5cd21912010-03-18 15:20:26 +02003581 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003582 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003583 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003584
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003585 if (!string_insn_completed(ctxt)) {
3586 /*
3587 * Re-enter guest when pio read ahead buffer is empty
3588 * or, if it is not used, after each 1024 iteration.
3589 */
3590 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3591 (r->end == 0 || r->end != r->pos)) {
3592 /*
3593 * Reset read cache. Usually happens before
3594 * decode, but since instruction is restarted
3595 * we have to do it here.
3596 */
3597 ctxt->decode.mem_read.end = 0;
3598 return EMULATION_RESTART;
3599 }
3600 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003601 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003602 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003603
3604 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003605
3606done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003607 if (rc == X86EMUL_PROPAGATE_FAULT)
3608 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02003609 if (rc == X86EMUL_INTERCEPTED)
3610 return EMULATION_INTERCEPTED;
3611
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003612 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613
3614twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003615 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003616 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003617 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618 u16 size;
3619 unsigned long address;
3620
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003621 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003622 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003623 goto cannot_emulate;
3624
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003625 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003626 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003627 goto done;
3628
Avi Kivity33e38852008-05-21 15:34:25 +03003629 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003630 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003631 /* Disable writeback. */
3632 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003633 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634 case 2: /* lgdt */
Avi Kivity1a6440a2010-08-01 12:35:10 +03003635 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003636 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003637 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638 goto done;
3639 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003640 /* Disable writeback. */
3641 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003643 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003644 if (c->modrm_mod == 3) {
3645 switch (c->modrm_rm) {
3646 case 1:
3647 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003648 break;
3649 default:
3650 goto cannot_emulate;
3651 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003652 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +03003653 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003654 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003655 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003656 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003657 goto done;
3658 realmode_lidt(ctxt->vcpu, size, address);
3659 }
Avi Kivity16286d02008-04-14 14:40:50 +03003660 /* Disable writeback. */
3661 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003662 break;
3663 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003664 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003665 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666 break;
3667 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003668 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003669 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003670 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003672 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003673 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003674 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003675 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676 case 7: /* invlpg*/
Avi Kivity90de84f2010-11-17 15:28:21 +02003677 emulate_invlpg(ctxt->vcpu,
3678 linear(ctxt, c->src.addr.mem));
Avi Kivity16286d02008-04-14 14:40:50 +03003679 /* Disable writeback. */
3680 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681 break;
3682 default:
3683 goto cannot_emulate;
3684 }
3685 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003686 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003687 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003688 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003689 case 0x06:
3690 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003691 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003692 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003693 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003694 break;
3695 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003696 case 0x0d: /* GrpP (prefetch) */
3697 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003698 break;
3699 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003700 switch (c->modrm_reg) {
3701 case 1:
3702 case 5 ... 7:
3703 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003704 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003705 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003706 goto done;
3707 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003708 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003709 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003711 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3712 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003713 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003714 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003715 goto done;
3716 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003717 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003719 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003720 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003721 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003722 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03003723 goto done;
3724 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003725 c->dst.type = OP_NONE;
3726 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003728 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3729 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003730 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02003731 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov1e470be2010-03-18 15:20:11 +02003732 goto done;
3733 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003734
Avi Kivityb27f3852010-08-01 14:25:22 +03003735 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003736 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3737 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3738 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003739 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003740 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03003741 goto done;
3742 }
3743
Laurent Viviera01af5e2007-09-24 11:10:56 +02003744 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003746 case 0x30:
3747 /* wrmsr */
3748 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3749 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003750 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003751 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003752 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003753 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003754 }
3755 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003756 break;
3757 case 0x32:
3758 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003759 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003760 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02003761 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02003762 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003763 } else {
3764 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3765 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3766 }
3767 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003768 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003769 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003770 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003771 break;
3772 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003773 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02003774 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003776 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003777 if (!test_cc(c->b, ctxt->eflags))
3778 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003780 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003781 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003782 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003783 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08003784 case 0x90 ... 0x9f: /* setcc r/m8 */
3785 c->dst.val = test_cc(c->b, ctxt->eflags);
3786 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003787 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003788 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003789 break;
3790 case 0xa1: /* pop fs */
3791 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003792 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003793 case 0xa3:
3794 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003795 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003796 /* only subword offset */
3797 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003798 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003799 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003800 case 0xa4: /* shld imm8, r, r/m */
3801 case 0xa5: /* shld cl, r, r/m */
3802 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3803 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003804 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003805 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003806 break;
3807 case 0xa9: /* pop gs */
3808 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003809 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003810 case 0xab:
3811 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003812 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003813 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003814 case 0xac: /* shrd imm8, r, r/m */
3815 case 0xad: /* shrd cl, r, r/m */
3816 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3817 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003818 case 0xae: /* clflush */
3819 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820 case 0xb0 ... 0xb1: /* cmpxchg */
3821 /*
3822 * Save real source value, then compare EAX against
3823 * destination.
3824 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003825 c->src.orig_val = c->src.val;
3826 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003827 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3828 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003829 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003830 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 } else {
3832 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003833 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003834 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 }
3836 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003837 case 0xb2: /* lss */
3838 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003839 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003840 case 0xb3:
3841 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003842 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003843 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003844 case 0xb4: /* lfs */
3845 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003846 break;
3847 case 0xb5: /* lgs */
3848 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003849 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003851 c->dst.bytes = c->op_bytes;
3852 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3853 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003856 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 case 0:
3858 goto bt;
3859 case 1:
3860 goto bts;
3861 case 2:
3862 goto btr;
3863 case 3:
3864 goto btc;
3865 }
3866 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003867 case 0xbb:
3868 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003869 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003870 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08003871 case 0xbc: { /* bsf */
3872 u8 zf;
3873 __asm__ ("bsf %2, %0; setz %1"
3874 : "=r"(c->dst.val), "=q"(zf)
3875 : "r"(c->src.val));
3876 ctxt->eflags &= ~X86_EFLAGS_ZF;
3877 if (zf) {
3878 ctxt->eflags |= X86_EFLAGS_ZF;
3879 c->dst.type = OP_NONE; /* Disable writeback. */
3880 }
3881 break;
3882 }
3883 case 0xbd: { /* bsr */
3884 u8 zf;
3885 __asm__ ("bsr %2, %0; setz %1"
3886 : "=r"(c->dst.val), "=q"(zf)
3887 : "r"(c->src.val));
3888 ctxt->eflags &= ~X86_EFLAGS_ZF;
3889 if (zf) {
3890 ctxt->eflags |= X86_EFLAGS_ZF;
3891 c->dst.type = OP_NONE; /* Disable writeback. */
3892 }
3893 break;
3894 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003895 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003896 c->dst.bytes = c->op_bytes;
3897 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3898 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08003900 case 0xc0 ... 0xc1: /* xadd */
3901 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
3902 /* Write back the register source. */
3903 c->src.val = c->dst.orig_val;
3904 write_register_operand(&c->src);
3905 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003906 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003907 c->dst.bytes = c->op_bytes;
3908 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3909 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003910 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003912 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003913 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003914 default:
3915 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003917
3918 if (rc != X86EMUL_CONTINUE)
3919 goto done;
3920
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921 goto writeback;
3922
3923cannot_emulate:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924 return -1;
3925}