blob: 4af94424fe870a0d80246262a4bd1f2ac53124e8 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030050#define DstDX (8<<1) /* Destination is in DX register */
51#define DstMask (0xf<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080052/* Source operand type. */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030053#define SrcNone (0<<5) /* No source operand. */
54#define SrcReg (1<<5) /* Register operand. */
55#define SrcMem (2<<5) /* Memory operand. */
56#define SrcMem16 (3<<5) /* Memory operand (16-bit). */
57#define SrcMem32 (4<<5) /* Memory operand (32-bit). */
58#define SrcImm (5<<5) /* Immediate operand. */
59#define SrcImmByte (6<<5) /* 8-bit sign-extended immediate operand. */
60#define SrcOne (7<<5) /* Implied '1' */
61#define SrcImmUByte (8<<5) /* 8-bit unsigned immediate operand. */
62#define SrcImmU (9<<5) /* Immediate operand, unsigned */
63#define SrcSI (0xa<<5) /* Source is in the DS:RSI */
64#define SrcImmFAddr (0xb<<5) /* Source is immediate far address */
65#define SrcMemFAddr (0xc<<5) /* Source is far address in memory */
66#define SrcAcc (0xd<<5) /* Source Accumulator */
67#define SrcImmU16 (0xe<<5) /* Immediate operand, unsigned, 16 bits */
68#define SrcDX (0xf<<5) /* Source is in DX register */
69#define SrcMask (0xf<<5)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Generic ModRM decode. */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030071#define ModRM (1<<9)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072/* Destination is only written; never read. */
Marcelo Tosatti221192b2011-05-30 15:23:14 -030073#define Mov (1<<10)
74#define BitOp (1<<11)
75#define MemAbs (1<<12) /* Memory operand is absolute displacement */
76#define String (1<<13) /* String instruction (rep capable) */
77#define Stack (1<<14) /* Stack instruction (push/pop) */
78#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
79#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
80#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
81#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
82#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
83#define Sse (1<<18) /* SSE Vector instruction */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030084/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +020085#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +020086#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030087#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030088#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030089#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020090#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020091#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030092#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010093/* Source 2 operand type */
94#define Src2None (0<<29)
95#define Src2CL (1<<29)
96#define Src2ImmByte (2<<29)
97#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030098#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010099#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100
Avi Kivityd0e53322010-07-29 15:11:54 +0300101#define X2(x...) x, x
102#define X3(x...) X2(x), x
103#define X4(x...) X2(x), X2(x)
104#define X5(x...) X4(x), x
105#define X6(x...) X4(x), X2(x)
106#define X7(x...) X4(x), X3(x)
107#define X8(x...) X4(x), X4(x)
108#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300109
Avi Kivityd65b1de2010-07-29 15:11:35 +0300110struct opcode {
111 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200112 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300113 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300114 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300115 struct opcode *group;
116 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200117 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300118 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200119 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300120};
121
122struct group_dual {
123 struct opcode mod012[8];
124 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300125};
126
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200127struct gprefix {
128 struct opcode pfx_no;
129 struct opcode pfx_66;
130 struct opcode pfx_f2;
131 struct opcode pfx_f3;
132};
133
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200135#define EFLG_ID (1<<21)
136#define EFLG_VIP (1<<20)
137#define EFLG_VIF (1<<19)
138#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200139#define EFLG_VM (1<<17)
140#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200141#define EFLG_IOPL (3<<12)
142#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143#define EFLG_OF (1<<11)
144#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200145#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200146#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147#define EFLG_SF (1<<7)
148#define EFLG_ZF (1<<6)
149#define EFLG_AF (1<<4)
150#define EFLG_PF (1<<2)
151#define EFLG_CF (1<<0)
152
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300153#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
154#define EFLG_RESERVED_ONE_MASK 2
155
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156/*
157 * Instruction emulation:
158 * Most instructions are emulated directly via a fragment of inline assembly
159 * code. This allows us to save/restore EFLAGS and thus very easily pick up
160 * any modified flags.
161 */
162
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800163#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164#define _LO32 "k" /* force 32-bit operand */
165#define _STK "%%rsp" /* stack pointer */
166#elif defined(__i386__)
167#define _LO32 "" /* force 32-bit operand */
168#define _STK "%%esp" /* stack pointer */
169#endif
170
171/*
172 * These EFLAGS bits are restored from saved value during emulation, and
173 * any changes are written back to the saved value after emulation.
174 */
175#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
176
177/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200178#define _PRE_EFLAGS(_sav, _msk, _tmp) \
179 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
180 "movl %"_sav",%"_LO32 _tmp"; " \
181 "push %"_tmp"; " \
182 "push %"_tmp"; " \
183 "movl %"_msk",%"_LO32 _tmp"; " \
184 "andl %"_LO32 _tmp",("_STK"); " \
185 "pushf; " \
186 "notl %"_LO32 _tmp"; " \
187 "andl %"_LO32 _tmp",("_STK"); " \
188 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
189 "pop %"_tmp"; " \
190 "orl %"_LO32 _tmp",("_STK"); " \
191 "popf; " \
192 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193
194/* After executing instruction: write-back necessary bits in EFLAGS. */
195#define _POST_EFLAGS(_sav, _msk, _tmp) \
196 /* _sav |= EFLAGS & _msk; */ \
197 "pushf; " \
198 "pop %"_tmp"; " \
199 "andl %"_msk",%"_LO32 _tmp"; " \
200 "orl %"_LO32 _tmp",%"_sav"; "
201
Avi Kivitydda96d82008-11-26 15:14:10 +0200202#ifdef CONFIG_X86_64
203#define ON64(x) x
204#else
205#define ON64(x)
206#endif
207
Avi Kivityb3b3d252010-08-16 17:49:52 +0300208#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200209 do { \
210 __asm__ __volatile__ ( \
211 _PRE_EFLAGS("0", "4", "2") \
212 _op _suffix " %"_x"3,%1; " \
213 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300214 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200215 "=&r" (_tmp) \
216 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200217 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200218
219
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220/* Raw emulation: instruction has two explicit operands. */
221#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200222 do { \
223 unsigned long _tmp; \
224 \
225 switch ((_dst).bytes) { \
226 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300227 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200228 break; \
229 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300230 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200231 break; \
232 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300233 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200234 break; \
235 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236 } while (0)
237
238#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
239 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200240 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400241 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300243 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244 break; \
245 default: \
246 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
247 _wx, _wy, _lx, _ly, _qx, _qy); \
248 break; \
249 } \
250 } while (0)
251
252/* Source operand is byte-sized and may be restricted to just %cl. */
253#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
254 __emulate_2op(_op, _src, _dst, _eflags, \
255 "b", "c", "b", "c", "b", "c", "b", "c")
256
257/* Source operand is byte, word, long or quad sized. */
258#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
259 __emulate_2op(_op, _src, _dst, _eflags, \
260 "b", "q", "w", "r", _LO32, "r", "", "r")
261
262/* Source operand is word, long or quad sized. */
263#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
264 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
265 "w", "r", _LO32, "r", "", "r")
266
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100267/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity72952612011-04-20 13:12:27 +0300268#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
269 do { \
270 unsigned long _tmp; \
271 _type _clv = (_cl).val; \
272 _type _srcv = (_src).val; \
273 _type _dstv = (_dst).val; \
274 \
275 __asm__ __volatile__ ( \
276 _PRE_EFLAGS("0", "5", "2") \
277 _op _suffix " %4,%1 \n" \
278 _POST_EFLAGS("0", "5", "2") \
279 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
280 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
281 ); \
282 \
283 (_cl).val = (unsigned long) _clv; \
284 (_src).val = (unsigned long) _srcv; \
285 (_dst).val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100286 } while (0)
287
Avi Kivity72952612011-04-20 13:12:27 +0300288#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
289 do { \
290 switch ((_dst).bytes) { \
291 case 2: \
292 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
293 "w", unsigned short); \
294 break; \
295 case 4: \
296 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
297 "l", unsigned int); \
298 break; \
299 case 8: \
300 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
301 "q", unsigned long)); \
302 break; \
303 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100304 } while (0)
305
Avi Kivitydda96d82008-11-26 15:14:10 +0200306#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 do { \
308 unsigned long _tmp; \
309 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200310 __asm__ __volatile__ ( \
311 _PRE_EFLAGS("0", "3", "2") \
312 _op _suffix " %1; " \
313 _POST_EFLAGS("0", "3", "2") \
314 : "=m" (_eflags), "+m" ((_dst).val), \
315 "=&r" (_tmp) \
316 : "i" (EFLAGS_MASK)); \
317 } while (0)
318
319/* Instruction has only one explicit operand (no source operand). */
320#define emulate_1op(_op, _dst, _eflags) \
321 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400322 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200323 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
324 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
325 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
326 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 } \
328 } while (0)
329
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300330#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
331 do { \
332 unsigned long _tmp; \
333 \
334 __asm__ __volatile__ ( \
335 _PRE_EFLAGS("0", "4", "1") \
336 _op _suffix " %5; " \
337 _POST_EFLAGS("0", "4", "1") \
338 : "=m" (_eflags), "=&r" (_tmp), \
339 "+a" (_rax), "+d" (_rdx) \
340 : "i" (EFLAGS_MASK), "m" ((_src).val), \
341 "a" (_rax), "d" (_rdx)); \
342 } while (0)
343
Avi Kivityf6b35972010-08-26 11:59:00 +0300344#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
345 do { \
346 unsigned long _tmp; \
347 \
348 __asm__ __volatile__ ( \
349 _PRE_EFLAGS("0", "5", "1") \
350 "1: \n\t" \
351 _op _suffix " %6; " \
352 "2: \n\t" \
353 _POST_EFLAGS("0", "5", "1") \
354 ".pushsection .fixup,\"ax\" \n\t" \
355 "3: movb $1, %4 \n\t" \
356 "jmp 2b \n\t" \
357 ".popsection \n\t" \
358 _ASM_EXTABLE(1b, 3b) \
359 : "=m" (_eflags), "=&r" (_tmp), \
360 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
361 : "i" (EFLAGS_MASK), "m" ((_src).val), \
362 "a" (_rax), "d" (_rdx)); \
363 } while (0)
364
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300365/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivity72952612011-04-20 13:12:27 +0300366#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
367 do { \
368 switch((_src).bytes) { \
369 case 1: \
370 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
371 _eflags, "b"); \
372 break; \
373 case 2: \
374 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
375 _eflags, "w"); \
376 break; \
377 case 4: \
378 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
379 _eflags, "l"); \
380 break; \
381 case 8: \
382 ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
383 _eflags, "q")); \
384 break; \
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300385 } \
386 } while (0)
387
Avi Kivityf6b35972010-08-26 11:59:00 +0300388#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
389 do { \
390 switch((_src).bytes) { \
391 case 1: \
392 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
393 _eflags, "b", _ex); \
394 break; \
395 case 2: \
396 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
397 _eflags, "w", _ex); \
398 break; \
399 case 4: \
400 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
401 _eflags, "l", _ex); \
402 break; \
403 case 8: ON64( \
404 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
405 _eflags, "q", _ex)); \
406 break; \
407 } \
408 } while (0)
409
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200410static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
411 enum x86_intercept intercept,
412 enum x86_intercept_stage stage)
413{
414 struct x86_instruction_info info = {
415 .intercept = intercept,
416 .rep_prefix = ctxt->decode.rep_prefix,
417 .modrm_mod = ctxt->decode.modrm_mod,
418 .modrm_reg = ctxt->decode.modrm_reg,
419 .modrm_rm = ctxt->decode.modrm_rm,
420 .src_val = ctxt->decode.src.val64,
421 .src_bytes = ctxt->decode.src.bytes,
422 .dst_bytes = ctxt->decode.dst.bytes,
423 .ad_bytes = ctxt->decode.ad_bytes,
424 .next_rip = ctxt->eip,
425 };
426
Avi Kivity29535382011-04-20 13:37:53 +0300427 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200428}
429
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800430static inline unsigned long ad_mask(struct decode_cache *c)
431{
432 return (1UL << (c->ad_bytes << 3)) - 1;
433}
434
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800436static inline unsigned long
437address_mask(struct decode_cache *c, unsigned long reg)
438{
439 if (c->ad_bytes == sizeof(unsigned long))
440 return reg;
441 else
442 return reg & ad_mask(c);
443}
444
445static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200446register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800447{
Avi Kivity90de84f2010-11-17 15:28:21 +0200448 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800449}
450
Harvey Harrison7a9572752008-02-19 07:40:41 -0800451static inline void
452register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
453{
454 if (c->ad_bytes == sizeof(unsigned long))
455 *reg += inc;
456 else
457 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
458}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459
Harvey Harrison7a9572752008-02-19 07:40:41 -0800460static inline void jmp_rel(struct decode_cache *c, int rel)
461{
462 register_address_increment(c, &c->eip, rel);
463}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300464
Avi Kivity56697682011-04-03 14:08:51 +0300465static u32 desc_limit_scaled(struct desc_struct *desc)
466{
467 u32 limit = get_desc_limit(desc);
468
469 return desc->g ? (limit << 12) | 0xfff : limit;
470}
471
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300472static void set_seg_override(struct decode_cache *c, int seg)
473{
474 c->has_seg_override = true;
475 c->seg_override = seg;
476}
477
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900478static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300479{
480 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
481 return 0;
482
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900483 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300484}
485
Avi Kivity90de84f2010-11-17 15:28:21 +0200486static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200487 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300488{
489 if (!c->has_seg_override)
490 return 0;
491
Avi Kivity90de84f2010-11-17 15:28:21 +0200492 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300493}
494
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200495static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
496 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300497{
Avi Kivityda9cb572010-11-22 17:53:21 +0200498 ctxt->exception.vector = vec;
499 ctxt->exception.error_code = error;
500 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200501 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300502}
503
Joerg Roedel3b88e412011-04-04 12:39:29 +0200504static int emulate_db(struct x86_emulate_ctxt *ctxt)
505{
506 return emulate_exception(ctxt, DB_VECTOR, 0, false);
507}
508
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200509static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300510{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200511 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300512}
513
Avi Kivity618ff152011-04-03 12:32:09 +0300514static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
515{
516 return emulate_exception(ctxt, SS_VECTOR, err, true);
517}
518
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200519static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300520{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200521 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300522}
523
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200524static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300525{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200526 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300527}
528
Avi Kivity34d1f492010-08-26 11:59:01 +0300529static int emulate_de(struct x86_emulate_ctxt *ctxt)
530{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200531 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300532}
533
Avi Kivity12537912011-03-29 11:41:27 +0200534static int emulate_nm(struct x86_emulate_ctxt *ctxt)
535{
536 return emulate_exception(ctxt, NM_VECTOR, 0, false);
537}
538
Avi Kivity1aa36612011-04-27 13:20:30 +0300539static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
540{
541 u16 selector;
542 struct desc_struct desc;
543
544 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
545 return selector;
546}
547
548static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
549 unsigned seg)
550{
551 u16 dummy;
552 u32 base3;
553 struct desc_struct desc;
554
555 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
556 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
557}
558
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400559static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300560 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400561 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300562 ulong *linear)
563{
564 struct decode_cache *c = &ctxt->decode;
Avi Kivity618ff152011-04-03 12:32:09 +0300565 struct desc_struct desc;
566 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300567 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300568 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300569 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300570 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300571
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900572 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300573 switch (ctxt->mode) {
574 case X86EMUL_MODE_REAL:
575 break;
576 case X86EMUL_MODE_PROT64:
577 if (((signed long)la << 16) >> 16 != la)
578 return emulate_gp(ctxt, 0);
579 break;
580 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300581 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
582 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300583 if (!usable)
584 goto bad;
585 /* code segment or read-only data segment */
586 if (((desc.type & 8) || !(desc.type & 2)) && write)
587 goto bad;
588 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400589 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300590 goto bad;
591 lim = desc_limit_scaled(&desc);
592 if ((desc.type & 8) || !(desc.type & 4)) {
593 /* expand-up segment */
594 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
595 goto bad;
596 } else {
597 /* exapand-down segment */
598 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
599 goto bad;
600 lim = desc.d ? 0xffffffff : 0xffff;
601 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
602 goto bad;
603 }
Avi Kivity717746e2011-04-20 13:37:53 +0300604 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300605 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300606 cpl = max(cpl, rpl);
607 if (!(desc.type & 8)) {
608 /* data segment */
609 if (cpl > desc.dpl)
610 goto bad;
611 } else if ((desc.type & 8) && !(desc.type & 4)) {
612 /* nonconforming code segment */
613 if (cpl != desc.dpl)
614 goto bad;
615 } else if ((desc.type & 8) && (desc.type & 4)) {
616 /* conforming code segment */
617 if (cpl < desc.dpl)
618 goto bad;
619 }
620 break;
621 }
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400622 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : c->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300623 la &= (u32)-1;
624 *linear = la;
625 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300626bad:
627 if (addr.seg == VCPU_SREG_SS)
628 return emulate_ss(ctxt, addr.seg);
629 else
630 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300631}
632
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400633static int linearize(struct x86_emulate_ctxt *ctxt,
634 struct segmented_address addr,
635 unsigned size, bool write,
636 ulong *linear)
637{
638 return __linearize(ctxt, addr, size, write, false, linear);
639}
640
641
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200642static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
643 struct segmented_address addr,
644 void *data,
645 unsigned size)
646{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200647 int rc;
648 ulong linear;
649
Avi Kivity83b87952011-04-03 11:31:19 +0300650 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200651 if (rc != X86EMUL_CONTINUE)
652 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300653 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200654}
655
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900656static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300657 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200658{
659 struct fetch_cache *fc = &ctxt->decode.fetch;
660 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300661 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200662
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300663 if (eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400664 unsigned long linear;
665 struct segmented_address addr = { .seg=VCPU_SREG_CS, .ea=eip};
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300666 cur_size = fc->end - fc->start;
667 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400668 rc = __linearize(ctxt, addr, size, false, true, &linear);
669 if (rc != X86EMUL_CONTINUE)
670 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900671 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
672 size, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900673 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200674 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300675 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200676 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300677 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900678 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200679}
680
681static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Avi Kivity62266862007-11-20 13:15:52 +0200682 unsigned long eip, void *dest, unsigned size)
683{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900684 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200685
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200686 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200687 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200688 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200689 while (size--) {
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900690 rc = do_insn_fetch_byte(ctxt, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900691 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200692 return rc;
693 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900694 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200695}
696
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900697/* Fetch next part of the instruction being emulated. */
698#define insn_fetch(_type, _size, _eip) \
699({ unsigned long _x; \
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900700 rc = do_insn_fetch(ctxt, (_eip), &_x, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900701 if (rc != X86EMUL_CONTINUE) \
702 goto done; \
703 (_eip) += (_size); \
704 (_type)_x; \
705})
706
707#define insn_fetch_arr(_arr, _size, _eip) \
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900708({ rc = do_insn_fetch(ctxt, (_eip), _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900709 if (rc != X86EMUL_CONTINUE) \
710 goto done; \
711 (_eip) += (_size); \
712})
713
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000714/*
715 * Given the 'reg' portion of a ModRM byte, and a register block, return a
716 * pointer into the block that addresses the relevant register.
717 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
718 */
719static void *decode_register(u8 modrm_reg, unsigned long *regs,
720 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721{
722 void *p;
723
724 p = &regs[modrm_reg];
725 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
726 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
727 return p;
728}
729
730static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200731 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732 u16 *size, unsigned long *address, int op_bytes)
733{
734 int rc;
735
736 if (op_bytes == 2)
737 op_bytes = 3;
738 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200739 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900740 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200742 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200743 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 return rc;
745}
746
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300747static int test_cc(unsigned int condition, unsigned int flags)
748{
749 int rc = 0;
750
751 switch ((condition & 15) >> 1) {
752 case 0: /* o */
753 rc |= (flags & EFLG_OF);
754 break;
755 case 1: /* b/c/nae */
756 rc |= (flags & EFLG_CF);
757 break;
758 case 2: /* z/e */
759 rc |= (flags & EFLG_ZF);
760 break;
761 case 3: /* be/na */
762 rc |= (flags & (EFLG_CF|EFLG_ZF));
763 break;
764 case 4: /* s */
765 rc |= (flags & EFLG_SF);
766 break;
767 case 5: /* p/pe */
768 rc |= (flags & EFLG_PF);
769 break;
770 case 7: /* le/ng */
771 rc |= (flags & EFLG_ZF);
772 /* fall through */
773 case 6: /* l/nge */
774 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
775 break;
776 }
777
778 /* Odd condition identifiers (lsb == 1) have inverted sense. */
779 return (!!rc ^ (condition & 1));
780}
781
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300782static void fetch_register_operand(struct operand *op)
783{
784 switch (op->bytes) {
785 case 1:
786 op->val = *(u8 *)op->addr.reg;
787 break;
788 case 2:
789 op->val = *(u16 *)op->addr.reg;
790 break;
791 case 4:
792 op->val = *(u32 *)op->addr.reg;
793 break;
794 case 8:
795 op->val = *(u64 *)op->addr.reg;
796 break;
797 }
798}
799
Avi Kivity12537912011-03-29 11:41:27 +0200800static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
801{
802 ctxt->ops->get_fpu(ctxt);
803 switch (reg) {
804 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
805 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
806 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
807 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
808 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
809 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
810 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
811 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
812#ifdef CONFIG_X86_64
813 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
814 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
815 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
816 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
817 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
818 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
819 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
820 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
821#endif
822 default: BUG();
823 }
824 ctxt->ops->put_fpu(ctxt);
825}
826
827static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
828 int reg)
829{
830 ctxt->ops->get_fpu(ctxt);
831 switch (reg) {
832 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
833 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
834 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
835 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
836 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
837 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
838 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
839 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
840#ifdef CONFIG_X86_64
841 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
842 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
843 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
844 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
845 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
846 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
847 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
848 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
849#endif
850 default: BUG();
851 }
852 ctxt->ops->put_fpu(ctxt);
853}
854
855static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
856 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200857 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200858 int inhibit_bytereg)
859{
Avi Kivity33615aa2007-10-31 11:15:56 +0200860 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200861 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200862
863 if (!(c->d & ModRM))
864 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200865
866 if (c->d & Sse) {
867 op->type = OP_XMM;
868 op->bytes = 16;
869 op->addr.xmm = reg;
870 read_sse_reg(ctxt, &op->vec_val, reg);
871 return;
872 }
873
Avi Kivity3c118e22007-10-31 10:27:04 +0200874 op->type = OP_REG;
875 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300876 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200877 op->bytes = 1;
878 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300879 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200880 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200881 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300882 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200883 op->orig_val = op->val;
884}
885
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200886static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300887 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888{
889 struct decode_cache *c = &ctxt->decode;
890 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700891 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900892 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300893 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200894
895 if (c->rex_prefix) {
896 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
897 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
898 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
899 }
900
901 c->modrm = insn_fetch(u8, 1, c->eip);
902 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
903 c->modrm_reg |= (c->modrm & 0x38) >> 3;
904 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300905 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200906
907 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300908 op->type = OP_REG;
909 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
910 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300911 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200912 if (c->d & Sse) {
913 op->type = OP_XMM;
914 op->bytes = 16;
915 op->addr.xmm = c->modrm_rm;
916 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
917 return rc;
918 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300919 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200920 return rc;
921 }
922
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300923 op->type = OP_MEM;
924
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200925 if (c->ad_bytes == 2) {
926 unsigned bx = c->regs[VCPU_REGS_RBX];
927 unsigned bp = c->regs[VCPU_REGS_RBP];
928 unsigned si = c->regs[VCPU_REGS_RSI];
929 unsigned di = c->regs[VCPU_REGS_RDI];
930
931 /* 16-bit ModR/M decode. */
932 switch (c->modrm_mod) {
933 case 0:
934 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300935 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200936 break;
937 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300938 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200939 break;
940 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300941 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200942 break;
943 }
944 switch (c->modrm_rm) {
945 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300946 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200947 break;
948 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300949 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200950 break;
951 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300952 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200953 break;
954 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300955 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200956 break;
957 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300958 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200959 break;
960 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300961 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200962 break;
963 case 6:
964 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300965 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200966 break;
967 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300968 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200969 break;
970 }
971 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
972 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300973 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300974 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200975 } else {
976 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700977 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200978 sib = insn_fetch(u8, 1, c->eip);
979 index_reg |= (sib >> 3) & 7;
980 base_reg |= sib & 7;
981 scale = sib >> 6;
982
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700983 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300984 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700985 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300986 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700987 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300988 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700989 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
990 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700991 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700992 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300993 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200994 switch (c->modrm_mod) {
995 case 0:
996 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300997 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200998 break;
999 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001000 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001 break;
1002 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001003 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004 break;
1005 }
1006 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001007 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001008done:
1009 return rc;
1010}
1011
1012static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001013 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001014{
1015 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001016 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001017
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001018 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001019 switch (c->ad_bytes) {
1020 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +02001021 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001022 break;
1023 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +02001024 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001025 break;
1026 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +02001027 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001028 break;
1029 }
1030done:
1031 return rc;
1032}
1033
Wei Yongjun35c843c2010-08-09 11:34:56 +08001034static void fetch_bit_operand(struct decode_cache *c)
1035{
Sheng Yang7129eec2010-09-28 16:33:32 +08001036 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001037
Wei Yongjun3885f182010-08-09 11:37:37 +08001038 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +08001039 mask = ~(c->dst.bytes * 8 - 1);
1040
1041 if (c->src.bytes == 2)
1042 sv = (s16)c->src.val & (s16)mask;
1043 else if (c->src.bytes == 4)
1044 sv = (s32)c->src.val & (s32)mask;
1045
Avi Kivity90de84f2010-11-17 15:28:21 +02001046 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001047 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001048
1049 /* only subword offset */
1050 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001051}
1052
Gleb Natapov9de41572010-04-28 19:15:22 +03001053static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001054 unsigned long addr, void *dest, unsigned size)
1055{
1056 int rc;
1057 struct read_cache *mc = &ctxt->decode.mem_read;
1058
1059 while (size) {
1060 int n = min(size, 8u);
1061 size -= n;
1062 if (mc->pos < mc->end)
1063 goto read_cached;
1064
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001065 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1066 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001067 if (rc != X86EMUL_CONTINUE)
1068 return rc;
1069 mc->end += n;
1070
1071 read_cached:
1072 memcpy(dest, mc->data + mc->pos, n);
1073 mc->pos += n;
1074 dest += n;
1075 addr += n;
1076 }
1077 return X86EMUL_CONTINUE;
1078}
1079
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001080static int segmented_read(struct x86_emulate_ctxt *ctxt,
1081 struct segmented_address addr,
1082 void *data,
1083 unsigned size)
1084{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001085 int rc;
1086 ulong linear;
1087
Avi Kivity83b87952011-04-03 11:31:19 +03001088 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001089 if (rc != X86EMUL_CONTINUE)
1090 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001091 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001092}
1093
1094static int segmented_write(struct x86_emulate_ctxt *ctxt,
1095 struct segmented_address addr,
1096 const void *data,
1097 unsigned size)
1098{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001099 int rc;
1100 ulong linear;
1101
Avi Kivity83b87952011-04-03 11:31:19 +03001102 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001103 if (rc != X86EMUL_CONTINUE)
1104 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001105 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1106 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001107}
1108
1109static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1110 struct segmented_address addr,
1111 const void *orig_data, const void *data,
1112 unsigned size)
1113{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001114 int rc;
1115 ulong linear;
1116
Avi Kivity83b87952011-04-03 11:31:19 +03001117 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001118 if (rc != X86EMUL_CONTINUE)
1119 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001120 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1121 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001122}
1123
Gleb Natapov7b262e92010-03-18 15:20:27 +02001124static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001125 unsigned int size, unsigned short port,
1126 void *dest)
1127{
1128 struct read_cache *rc = &ctxt->decode.io_read;
1129
1130 if (rc->pos == rc->end) { /* refill pio read ahead */
1131 struct decode_cache *c = &ctxt->decode;
1132 unsigned int in_page, n;
1133 unsigned int count = c->rep_prefix ?
1134 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1135 in_page = (ctxt->eflags & EFLG_DF) ?
1136 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1137 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1138 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1139 count);
1140 if (n == 0)
1141 n = 1;
1142 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001143 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001144 return 0;
1145 rc->end = n * size;
1146 }
1147
1148 memcpy(dest, rc->data + rc->pos, size);
1149 rc->pos += size;
1150 return 1;
1151}
1152
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001153static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001154 u16 selector, struct desc_ptr *dt)
1155{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001156 struct x86_emulate_ops *ops = ctxt->ops;
1157
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001158 if (selector & 1 << 2) {
1159 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001160 u16 sel;
1161
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001162 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001163 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001164 return;
1165
1166 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1167 dt->address = get_desc_base(&desc);
1168 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001169 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001170}
1171
1172/* allowed just for 8 bytes segments */
1173static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001174 u16 selector, struct desc_struct *desc)
1175{
1176 struct desc_ptr dt;
1177 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001178 ulong addr;
1179
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001180 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001181
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001182 if (dt.size < index * 8 + 7)
1183 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001184
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001185 addr = dt.address + index * 8;
1186 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1187 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001188}
1189
1190/* allowed just for 8 bytes segments */
1191static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001192 u16 selector, struct desc_struct *desc)
1193{
1194 struct desc_ptr dt;
1195 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001196 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001197
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001198 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001199
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001200 if (dt.size < index * 8 + 7)
1201 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001202
1203 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001204 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1205 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001206}
1207
Gleb Natapov5601d052011-03-07 14:55:06 +02001208/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001209static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001210 u16 selector, int seg)
1211{
1212 struct desc_struct seg_desc;
1213 u8 dpl, rpl, cpl;
1214 unsigned err_vec = GP_VECTOR;
1215 u32 err_code = 0;
1216 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1217 int ret;
1218
1219 memset(&seg_desc, 0, sizeof seg_desc);
1220
1221 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1222 || ctxt->mode == X86EMUL_MODE_REAL) {
1223 /* set real mode segment descriptor */
1224 set_desc_base(&seg_desc, selector << 4);
1225 set_desc_limit(&seg_desc, 0xffff);
1226 seg_desc.type = 3;
1227 seg_desc.p = 1;
1228 seg_desc.s = 1;
1229 goto load;
1230 }
1231
1232 /* NULL selector is not valid for TR, CS and SS */
1233 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1234 && null_selector)
1235 goto exception;
1236
1237 /* TR should be in GDT only */
1238 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1239 goto exception;
1240
1241 if (null_selector) /* for NULL selector skip all following checks */
1242 goto load;
1243
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001244 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001245 if (ret != X86EMUL_CONTINUE)
1246 return ret;
1247
1248 err_code = selector & 0xfffc;
1249 err_vec = GP_VECTOR;
1250
1251 /* can't load system descriptor into segment selecor */
1252 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1253 goto exception;
1254
1255 if (!seg_desc.p) {
1256 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1257 goto exception;
1258 }
1259
1260 rpl = selector & 3;
1261 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001262 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001263
1264 switch (seg) {
1265 case VCPU_SREG_SS:
1266 /*
1267 * segment is not a writable data segment or segment
1268 * selector's RPL != CPL or segment selector's RPL != CPL
1269 */
1270 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1271 goto exception;
1272 break;
1273 case VCPU_SREG_CS:
1274 if (!(seg_desc.type & 8))
1275 goto exception;
1276
1277 if (seg_desc.type & 4) {
1278 /* conforming */
1279 if (dpl > cpl)
1280 goto exception;
1281 } else {
1282 /* nonconforming */
1283 if (rpl > cpl || dpl != cpl)
1284 goto exception;
1285 }
1286 /* CS(RPL) <- CPL */
1287 selector = (selector & 0xfffc) | cpl;
1288 break;
1289 case VCPU_SREG_TR:
1290 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1291 goto exception;
1292 break;
1293 case VCPU_SREG_LDTR:
1294 if (seg_desc.s || seg_desc.type != 2)
1295 goto exception;
1296 break;
1297 default: /* DS, ES, FS, or GS */
1298 /*
1299 * segment is not a data or readable code segment or
1300 * ((segment is a data or nonconforming code segment)
1301 * and (both RPL and CPL > DPL))
1302 */
1303 if ((seg_desc.type & 0xa) == 0x8 ||
1304 (((seg_desc.type & 0xc) != 0xc) &&
1305 (rpl > dpl && cpl > dpl)))
1306 goto exception;
1307 break;
1308 }
1309
1310 if (seg_desc.s) {
1311 /* mark segment as accessed */
1312 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001313 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001314 if (ret != X86EMUL_CONTINUE)
1315 return ret;
1316 }
1317load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001318 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001319 return X86EMUL_CONTINUE;
1320exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001321 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001322 return X86EMUL_PROPAGATE_FAULT;
1323}
1324
Wei Yongjun31be40b2010-08-17 09:17:30 +08001325static void write_register_operand(struct operand *op)
1326{
1327 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1328 switch (op->bytes) {
1329 case 1:
1330 *(u8 *)op->addr.reg = (u8)op->val;
1331 break;
1332 case 2:
1333 *(u16 *)op->addr.reg = (u16)op->val;
1334 break;
1335 case 4:
1336 *op->addr.reg = (u32)op->val;
1337 break; /* 64b: zero-extend */
1338 case 8:
1339 *op->addr.reg = op->val;
1340 break;
1341 }
1342}
1343
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001344static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001345{
1346 int rc;
1347 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001348
1349 switch (c->dst.type) {
1350 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001351 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001352 break;
1353 case OP_MEM:
1354 if (c->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001355 rc = segmented_cmpxchg(ctxt,
1356 c->dst.addr.mem,
1357 &c->dst.orig_val,
1358 &c->dst.val,
1359 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001360 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001361 rc = segmented_write(ctxt,
1362 c->dst.addr.mem,
1363 &c->dst.val,
1364 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001365 if (rc != X86EMUL_CONTINUE)
1366 return rc;
1367 break;
Avi Kivity12537912011-03-29 11:41:27 +02001368 case OP_XMM:
1369 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1370 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001371 case OP_NONE:
1372 /* no writeback */
1373 break;
1374 default:
1375 break;
1376 }
1377 return X86EMUL_CONTINUE;
1378}
1379
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001380static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001381{
1382 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001383 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001384
Harvey Harrison7a9572752008-02-19 07:40:41 -08001385 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001386 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1387 addr.seg = VCPU_SREG_SS;
1388
1389 /* Disable writeback. */
1390 c->dst.type = OP_NONE;
1391 return segmented_write(ctxt, addr, &c->src.val, c->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392}
1393
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001394static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001395 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396{
1397 struct decode_cache *c = &ctxt->decode;
1398 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001399 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001400
Avi Kivity90de84f2010-11-17 15:28:21 +02001401 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1402 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001403 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001404 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001405 return rc;
1406
Avi Kivity350f69d2009-01-05 11:12:40 +02001407 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001408 return rc;
1409}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001410
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001411static int em_pop(struct x86_emulate_ctxt *ctxt)
1412{
1413 struct decode_cache *c = &ctxt->decode;
1414
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001415 return emulate_pop(ctxt, &c->dst.val, c->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001416}
1417
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001418static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001419 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001420{
1421 int rc;
1422 unsigned long val, change_mask;
1423 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001424 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001425
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001426 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001427 if (rc != X86EMUL_CONTINUE)
1428 return rc;
1429
1430 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1431 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1432
1433 switch(ctxt->mode) {
1434 case X86EMUL_MODE_PROT64:
1435 case X86EMUL_MODE_PROT32:
1436 case X86EMUL_MODE_PROT16:
1437 if (cpl == 0)
1438 change_mask |= EFLG_IOPL;
1439 if (cpl <= iopl)
1440 change_mask |= EFLG_IF;
1441 break;
1442 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001443 if (iopl < 3)
1444 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001445 change_mask |= EFLG_IF;
1446 break;
1447 default: /* real mode */
1448 change_mask |= (EFLG_IOPL | EFLG_IF);
1449 break;
1450 }
1451
1452 *(unsigned long *)dest =
1453 (ctxt->eflags & ~change_mask) | (val & change_mask);
1454
1455 return rc;
1456}
1457
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001458static int em_popf(struct x86_emulate_ctxt *ctxt)
1459{
1460 struct decode_cache *c = &ctxt->decode;
1461
1462 c->dst.type = OP_REG;
1463 c->dst.addr.reg = &ctxt->eflags;
1464 c->dst.bytes = c->op_bytes;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001465 return emulate_popf(ctxt, &c->dst.val, c->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001466}
1467
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001468static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001469{
1470 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001471
Avi Kivity1aa36612011-04-27 13:20:30 +03001472 c->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001473
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001474 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001475}
1476
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001477static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001478{
1479 struct decode_cache *c = &ctxt->decode;
1480 unsigned long selector;
1481 int rc;
1482
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001483 rc = emulate_pop(ctxt, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001484 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001485 return rc;
1486
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001487 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001488 return rc;
1489}
1490
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001491static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001492{
1493 struct decode_cache *c = &ctxt->decode;
1494 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001495 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001496 int reg = VCPU_REGS_RAX;
1497
1498 while (reg <= VCPU_REGS_RDI) {
1499 (reg == VCPU_REGS_RSP) ?
1500 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1501
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001502 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001503 if (rc != X86EMUL_CONTINUE)
1504 return rc;
1505
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001506 ++reg;
1507 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001508
Wei Yongjunc37eda12010-06-15 09:03:33 +08001509 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001510}
1511
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001512static int em_pushf(struct x86_emulate_ctxt *ctxt)
1513{
1514 struct decode_cache *c = &ctxt->decode;
1515
1516 c->src.val = (unsigned long)ctxt->eflags;
1517 return em_push(ctxt);
1518}
1519
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001520static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001521{
1522 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001523 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001524 int reg = VCPU_REGS_RDI;
1525
1526 while (reg >= VCPU_REGS_RAX) {
1527 if (reg == VCPU_REGS_RSP) {
1528 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1529 c->op_bytes);
1530 --reg;
1531 }
1532
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001533 rc = emulate_pop(ctxt, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001534 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001535 break;
1536 --reg;
1537 }
1538 return rc;
1539}
1540
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001541int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001542{
1543 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001544 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001545 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001546 struct desc_ptr dt;
1547 gva_t cs_addr;
1548 gva_t eip_addr;
1549 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001550
1551 /* TODO: Add limit checks */
1552 c->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001553 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001554 if (rc != X86EMUL_CONTINUE)
1555 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001556
1557 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1558
Avi Kivity1aa36612011-04-27 13:20:30 +03001559 c->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001560 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001561 if (rc != X86EMUL_CONTINUE)
1562 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001563
1564 c->src.val = c->eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001565 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001566 if (rc != X86EMUL_CONTINUE)
1567 return rc;
1568
Avi Kivity4bff1e862011-04-20 13:37:53 +03001569 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001570
1571 eip_addr = dt.address + (irq << 2);
1572 cs_addr = dt.address + (irq << 2) + 2;
1573
Avi Kivity0f65dd72011-04-20 13:37:53 +03001574 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001575 if (rc != X86EMUL_CONTINUE)
1576 return rc;
1577
Avi Kivity0f65dd72011-04-20 13:37:53 +03001578 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001579 if (rc != X86EMUL_CONTINUE)
1580 return rc;
1581
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001582 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001583 if (rc != X86EMUL_CONTINUE)
1584 return rc;
1585
1586 c->eip = eip;
1587
1588 return rc;
1589}
1590
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001591static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001592{
1593 switch(ctxt->mode) {
1594 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001595 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001596 case X86EMUL_MODE_VM86:
1597 case X86EMUL_MODE_PROT16:
1598 case X86EMUL_MODE_PROT32:
1599 case X86EMUL_MODE_PROT64:
1600 default:
1601 /* Protected mode interrupts unimplemented yet */
1602 return X86EMUL_UNHANDLEABLE;
1603 }
1604}
1605
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001606static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001607{
1608 struct decode_cache *c = &ctxt->decode;
1609 int rc = X86EMUL_CONTINUE;
1610 unsigned long temp_eip = 0;
1611 unsigned long temp_eflags = 0;
1612 unsigned long cs = 0;
1613 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1614 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1615 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1616 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1617
1618 /* TODO: Add stack limit check */
1619
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001620 rc = emulate_pop(ctxt, &temp_eip, c->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001621
1622 if (rc != X86EMUL_CONTINUE)
1623 return rc;
1624
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001625 if (temp_eip & ~0xffff)
1626 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001627
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001628 rc = emulate_pop(ctxt, &cs, c->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001629
1630 if (rc != X86EMUL_CONTINUE)
1631 return rc;
1632
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001633 rc = emulate_pop(ctxt, &temp_eflags, c->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001634
1635 if (rc != X86EMUL_CONTINUE)
1636 return rc;
1637
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001638 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001639
1640 if (rc != X86EMUL_CONTINUE)
1641 return rc;
1642
1643 c->eip = temp_eip;
1644
1645
1646 if (c->op_bytes == 4)
1647 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1648 else if (c->op_bytes == 2) {
1649 ctxt->eflags &= ~0xffff;
1650 ctxt->eflags |= temp_eflags;
1651 }
1652
1653 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1654 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1655
1656 return rc;
1657}
1658
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001659static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001660{
1661 switch(ctxt->mode) {
1662 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001663 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001664 case X86EMUL_MODE_VM86:
1665 case X86EMUL_MODE_PROT16:
1666 case X86EMUL_MODE_PROT32:
1667 case X86EMUL_MODE_PROT64:
1668 default:
1669 /* iret from protected mode unimplemented yet */
1670 return X86EMUL_UNHANDLEABLE;
1671 }
1672}
1673
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001674static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1675{
1676 struct decode_cache *c = &ctxt->decode;
1677 int rc;
1678 unsigned short sel;
1679
1680 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1681
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001682 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001683 if (rc != X86EMUL_CONTINUE)
1684 return rc;
1685
1686 c->eip = 0;
1687 memcpy(&c->eip, c->src.valptr, c->op_bytes);
1688 return X86EMUL_CONTINUE;
1689}
1690
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001691static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001692{
1693 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001694
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001695 return emulate_pop(ctxt, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696}
1697
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001698static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001700 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001701 switch (c->modrm_reg) {
1702 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001703 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001704 break;
1705 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001706 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001707 break;
1708 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001709 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710 break;
1711 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001712 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001713 break;
1714 case 4: /* sal/shl */
1715 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001716 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717 break;
1718 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001719 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001720 break;
1721 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001722 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001723 break;
1724 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001725 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726}
1727
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001728static int em_grp3(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001729{
1730 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001731 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1732 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001733 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001734
1735 switch (c->modrm_reg) {
1736 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001737 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001738 break;
1739 case 2: /* not */
1740 c->dst.val = ~c->dst.val;
1741 break;
1742 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001743 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001744 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001745 case 4: /* mul */
1746 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1747 break;
1748 case 5: /* imul */
1749 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1750 break;
1751 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001752 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1753 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001754 break;
1755 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001756 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1757 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001758 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001759 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001760 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001762 if (de)
1763 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001764 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001765}
1766
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001767static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001768{
1769 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001770 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771
1772 switch (c->modrm_reg) {
1773 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001774 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 break;
1776 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001777 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001778 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001779 case 2: /* call near abs */ {
1780 long int old_eip;
1781 old_eip = c->eip;
1782 c->eip = c->src.val;
1783 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001784 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001785 break;
1786 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001787 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001788 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001790 case 5: /* jmp far */
1791 rc = em_jmp_far(ctxt);
1792 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001794 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001797 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001798}
1799
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001800static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001801{
1802 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001803 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001804
1805 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1806 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001807 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1808 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001809 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001810 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001811 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1812 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001813
Laurent Vivier05f086f2007-09-24 11:10:55 +02001814 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001815 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001816 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001817}
1818
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001819static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001820{
1821 struct decode_cache *c = &ctxt->decode;
1822 int rc;
1823 unsigned long cs;
1824
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001825 rc = emulate_pop(ctxt, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001826 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001827 return rc;
1828 if (c->op_bytes == 4)
1829 c->eip = (u32)c->eip;
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001830 rc = emulate_pop(ctxt, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001831 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001832 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001833 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001834 return rc;
1835}
1836
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001837static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001838{
1839 struct decode_cache *c = &ctxt->decode;
1840 unsigned short sel;
1841 int rc;
1842
1843 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1844
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001845 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001846 if (rc != X86EMUL_CONTINUE)
1847 return rc;
1848
1849 c->dst.val = c->src.val;
1850 return rc;
1851}
1852
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001853static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001854setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001855 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001856{
Avi Kivity1aa36612011-04-27 13:20:30 +03001857 u16 selector;
1858
Gleb Natapov79168fd2010-04-28 19:15:30 +03001859 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001860 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001861 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001862
1863 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001864 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001865 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001866 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001867 cs->type = 0x0b; /* Read, Execute, Accessed */
1868 cs->s = 1;
1869 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001870 cs->p = 1;
1871 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001872
Gleb Natapov79168fd2010-04-28 19:15:30 +03001873 set_desc_base(ss, 0); /* flat segment */
1874 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001875 ss->g = 1; /* 4kb granularity */
1876 ss->s = 1;
1877 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001878 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001880 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001881}
1882
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001883static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001884{
1885 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001886 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001887 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001888 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001889 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001890 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891
1892 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001893 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001894 ctxt->mode == X86EMUL_MODE_VM86)
1895 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001896
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001897 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001898 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001899
Avi Kivity717746e2011-04-20 13:37:53 +03001900 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001901 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001902 cs_sel = (u16)(msr_data & 0xfffc);
1903 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001904
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001905 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001906 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001907 cs.l = 1;
1908 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001909 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1910 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001911
1912 c->regs[VCPU_REGS_RCX] = c->eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001913 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001914#ifdef CONFIG_X86_64
1915 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1916
Avi Kivity717746e2011-04-20 13:37:53 +03001917 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001918 ctxt->mode == X86EMUL_MODE_PROT64 ?
1919 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001920 c->eip = msr_data;
1921
Avi Kivity717746e2011-04-20 13:37:53 +03001922 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001923 ctxt->eflags &= ~(msr_data | EFLG_RF);
1924#endif
1925 } else {
1926 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001927 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001928 c->eip = (u32)msr_data;
1929
1930 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1931 }
1932
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001933 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001934}
1935
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001936static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001937{
1938 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001939 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001940 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001941 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001942 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001943 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001944
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001945 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001946 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001947 if (ctxt->mode == X86EMUL_MODE_REAL)
1948 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001949
1950 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1951 * Therefore, we inject an #UD.
1952 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001953 if (ctxt->mode == X86EMUL_MODE_PROT64)
1954 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001955
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001956 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001957
Avi Kivity717746e2011-04-20 13:37:53 +03001958 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001959 switch (ctxt->mode) {
1960 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001961 if ((msr_data & 0xfffc) == 0x0)
1962 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001963 break;
1964 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001965 if (msr_data == 0x0)
1966 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001967 break;
1968 }
1969
1970 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001971 cs_sel = (u16)msr_data;
1972 cs_sel &= ~SELECTOR_RPL_MASK;
1973 ss_sel = cs_sel + 8;
1974 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001975 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001976 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001977 cs.l = 1;
1978 }
1979
Avi Kivity1aa36612011-04-27 13:20:30 +03001980 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1981 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001982
Avi Kivity717746e2011-04-20 13:37:53 +03001983 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001984 c->eip = msr_data;
1985
Avi Kivity717746e2011-04-20 13:37:53 +03001986 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001987 c->regs[VCPU_REGS_RSP] = msr_data;
1988
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001989 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001990}
1991
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001992static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001993{
1994 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001995 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001996 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001997 u64 msr_data;
1998 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001999 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002000
Gleb Natapova0044752010-02-10 14:21:31 +02002001 /* inject #GP if in real mode or Virtual 8086 mode */
2002 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002003 ctxt->mode == X86EMUL_MODE_VM86)
2004 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002005
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002006 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002007
2008 if ((c->rex_prefix & 0x8) != 0x0)
2009 usermode = X86EMUL_MODE_PROT64;
2010 else
2011 usermode = X86EMUL_MODE_PROT32;
2012
2013 cs.dpl = 3;
2014 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002015 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002016 switch (usermode) {
2017 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002018 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002019 if ((msr_data & 0xfffc) == 0x0)
2020 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002021 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002022 break;
2023 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002024 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002025 if (msr_data == 0x0)
2026 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002027 ss_sel = cs_sel + 8;
2028 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002029 cs.l = 1;
2030 break;
2031 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002032 cs_sel |= SELECTOR_RPL_MASK;
2033 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002034
Avi Kivity1aa36612011-04-27 13:20:30 +03002035 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2036 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002037
Gleb Natapovbdb475a2010-04-28 19:15:41 +03002038 c->eip = c->regs[VCPU_REGS_RDX];
2039 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002040
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002041 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002042}
2043
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002044static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002045{
2046 int iopl;
2047 if (ctxt->mode == X86EMUL_MODE_REAL)
2048 return false;
2049 if (ctxt->mode == X86EMUL_MODE_VM86)
2050 return true;
2051 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002052 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002053}
2054
2055static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002056 u16 port, u16 len)
2057{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002058 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002059 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002060 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002061 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002062 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002063 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002064 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002065
Avi Kivity1aa36612011-04-27 13:20:30 +03002066 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002067 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002068 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002069 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002070 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002071 base = get_desc_base(&tr_seg);
2072#ifdef CONFIG_X86_64
2073 base |= ((u64)base3) << 32;
2074#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002075 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002076 if (r != X86EMUL_CONTINUE)
2077 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002078 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002079 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002080 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002081 if (r != X86EMUL_CONTINUE)
2082 return false;
2083 if ((perm >> bit_idx) & mask)
2084 return false;
2085 return true;
2086}
2087
2088static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002089 u16 port, u16 len)
2090{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002091 if (ctxt->perm_ok)
2092 return true;
2093
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002094 if (emulator_bad_iopl(ctxt))
2095 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002096 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002097
2098 ctxt->perm_ok = true;
2099
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002100 return true;
2101}
2102
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002103static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002104 struct tss_segment_16 *tss)
2105{
2106 struct decode_cache *c = &ctxt->decode;
2107
2108 tss->ip = c->eip;
2109 tss->flag = ctxt->eflags;
2110 tss->ax = c->regs[VCPU_REGS_RAX];
2111 tss->cx = c->regs[VCPU_REGS_RCX];
2112 tss->dx = c->regs[VCPU_REGS_RDX];
2113 tss->bx = c->regs[VCPU_REGS_RBX];
2114 tss->sp = c->regs[VCPU_REGS_RSP];
2115 tss->bp = c->regs[VCPU_REGS_RBP];
2116 tss->si = c->regs[VCPU_REGS_RSI];
2117 tss->di = c->regs[VCPU_REGS_RDI];
2118
Avi Kivity1aa36612011-04-27 13:20:30 +03002119 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2120 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2121 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2122 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2123 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002124}
2125
2126static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002127 struct tss_segment_16 *tss)
2128{
2129 struct decode_cache *c = &ctxt->decode;
2130 int ret;
2131
2132 c->eip = tss->ip;
2133 ctxt->eflags = tss->flag | 2;
2134 c->regs[VCPU_REGS_RAX] = tss->ax;
2135 c->regs[VCPU_REGS_RCX] = tss->cx;
2136 c->regs[VCPU_REGS_RDX] = tss->dx;
2137 c->regs[VCPU_REGS_RBX] = tss->bx;
2138 c->regs[VCPU_REGS_RSP] = tss->sp;
2139 c->regs[VCPU_REGS_RBP] = tss->bp;
2140 c->regs[VCPU_REGS_RSI] = tss->si;
2141 c->regs[VCPU_REGS_RDI] = tss->di;
2142
2143 /*
2144 * SDM says that segment selectors are loaded before segment
2145 * descriptors
2146 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002147 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2148 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2149 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2150 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2151 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152
2153 /*
2154 * Now load segment descriptors. If fault happenes at this stage
2155 * it is handled in a context of new task
2156 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002157 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002158 if (ret != X86EMUL_CONTINUE)
2159 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002160 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002161 if (ret != X86EMUL_CONTINUE)
2162 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002163 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002164 if (ret != X86EMUL_CONTINUE)
2165 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002166 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 if (ret != X86EMUL_CONTINUE)
2168 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002169 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002170 if (ret != X86EMUL_CONTINUE)
2171 return ret;
2172
2173 return X86EMUL_CONTINUE;
2174}
2175
2176static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177 u16 tss_selector, u16 old_tss_sel,
2178 ulong old_tss_base, struct desc_struct *new_desc)
2179{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002180 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002181 struct tss_segment_16 tss_seg;
2182 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002183 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002184
Avi Kivity0f65dd72011-04-20 13:37:53 +03002185 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002186 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002187 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002188 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002191 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002192
Avi Kivity0f65dd72011-04-20 13:37:53 +03002193 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002194 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002195 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002197 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002198
Avi Kivity0f65dd72011-04-20 13:37:53 +03002199 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002200 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002201 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002202 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002203 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002204
2205 if (old_tss_sel != 0xffff) {
2206 tss_seg.prev_task_link = old_tss_sel;
2207
Avi Kivity0f65dd72011-04-20 13:37:53 +03002208 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002209 &tss_seg.prev_task_link,
2210 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002211 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002212 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002213 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002214 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002215 }
2216
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002217 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218}
2219
2220static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002221 struct tss_segment_32 *tss)
2222{
2223 struct decode_cache *c = &ctxt->decode;
2224
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002225 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002226 tss->eip = c->eip;
2227 tss->eflags = ctxt->eflags;
2228 tss->eax = c->regs[VCPU_REGS_RAX];
2229 tss->ecx = c->regs[VCPU_REGS_RCX];
2230 tss->edx = c->regs[VCPU_REGS_RDX];
2231 tss->ebx = c->regs[VCPU_REGS_RBX];
2232 tss->esp = c->regs[VCPU_REGS_RSP];
2233 tss->ebp = c->regs[VCPU_REGS_RBP];
2234 tss->esi = c->regs[VCPU_REGS_RSI];
2235 tss->edi = c->regs[VCPU_REGS_RDI];
2236
Avi Kivity1aa36612011-04-27 13:20:30 +03002237 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2238 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2239 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2240 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2241 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2242 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2243 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002244}
2245
2246static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002247 struct tss_segment_32 *tss)
2248{
2249 struct decode_cache *c = &ctxt->decode;
2250 int ret;
2251
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002252 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002253 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002254 c->eip = tss->eip;
2255 ctxt->eflags = tss->eflags | 2;
2256 c->regs[VCPU_REGS_RAX] = tss->eax;
2257 c->regs[VCPU_REGS_RCX] = tss->ecx;
2258 c->regs[VCPU_REGS_RDX] = tss->edx;
2259 c->regs[VCPU_REGS_RBX] = tss->ebx;
2260 c->regs[VCPU_REGS_RSP] = tss->esp;
2261 c->regs[VCPU_REGS_RBP] = tss->ebp;
2262 c->regs[VCPU_REGS_RSI] = tss->esi;
2263 c->regs[VCPU_REGS_RDI] = tss->edi;
2264
2265 /*
2266 * SDM says that segment selectors are loaded before segment
2267 * descriptors
2268 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002269 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2270 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2271 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2272 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2273 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2274 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2275 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002276
2277 /*
2278 * Now load segment descriptors. If fault happenes at this stage
2279 * it is handled in a context of new task
2280 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002281 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282 if (ret != X86EMUL_CONTINUE)
2283 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002284 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002285 if (ret != X86EMUL_CONTINUE)
2286 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002287 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002288 if (ret != X86EMUL_CONTINUE)
2289 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002290 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002291 if (ret != X86EMUL_CONTINUE)
2292 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002293 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294 if (ret != X86EMUL_CONTINUE)
2295 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002296 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002297 if (ret != X86EMUL_CONTINUE)
2298 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002299 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002300 if (ret != X86EMUL_CONTINUE)
2301 return ret;
2302
2303 return X86EMUL_CONTINUE;
2304}
2305
2306static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002307 u16 tss_selector, u16 old_tss_sel,
2308 ulong old_tss_base, struct desc_struct *new_desc)
2309{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002310 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311 struct tss_segment_32 tss_seg;
2312 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002313 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002314
Avi Kivity0f65dd72011-04-20 13:37:53 +03002315 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002316 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002317 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002319 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002320
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002321 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002322
Avi Kivity0f65dd72011-04-20 13:37:53 +03002323 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002324 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002325 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328
Avi Kivity0f65dd72011-04-20 13:37:53 +03002329 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002330 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002331 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002332 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002334
2335 if (old_tss_sel != 0xffff) {
2336 tss_seg.prev_task_link = old_tss_sel;
2337
Avi Kivity0f65dd72011-04-20 13:37:53 +03002338 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002339 &tss_seg.prev_task_link,
2340 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002341 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002342 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002343 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002344 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002345 }
2346
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002347 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002348}
2349
2350static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002351 u16 tss_selector, int reason,
2352 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002353{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002354 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002355 struct desc_struct curr_tss_desc, next_tss_desc;
2356 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002357 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002358 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002359 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002360 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002361
2362 /* FIXME: old_tss_base == ~0 ? */
2363
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002364 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002365 if (ret != X86EMUL_CONTINUE)
2366 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002367 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002368 if (ret != X86EMUL_CONTINUE)
2369 return ret;
2370
2371 /* FIXME: check that next_tss_desc is tss */
2372
2373 if (reason != TASK_SWITCH_IRET) {
2374 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002375 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002376 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002377 }
2378
Gleb Natapovceffb452010-03-18 15:20:19 +02002379 desc_limit = desc_limit_scaled(&next_tss_desc);
2380 if (!next_tss_desc.p ||
2381 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2382 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002383 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002384 return X86EMUL_PROPAGATE_FAULT;
2385 }
2386
2387 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2388 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002389 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002390 }
2391
2392 if (reason == TASK_SWITCH_IRET)
2393 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2394
2395 /* set back link to prev task only if NT bit is set in eflags
2396 note that old_tss_sel is not used afetr this point */
2397 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2398 old_tss_sel = 0xffff;
2399
2400 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002401 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402 old_tss_base, &next_tss_desc);
2403 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002404 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002405 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002406 if (ret != X86EMUL_CONTINUE)
2407 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408
2409 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2410 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2411
2412 if (reason != TASK_SWITCH_IRET) {
2413 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002414 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002415 }
2416
Avi Kivity717746e2011-04-20 13:37:53 +03002417 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002418 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419
Jan Kiszkae269fb22010-04-14 15:51:09 +02002420 if (has_error_code) {
2421 struct decode_cache *c = &ctxt->decode;
2422
2423 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2424 c->lock_prefix = 0;
2425 c->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002426 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002427 }
2428
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002429 return ret;
2430}
2431
2432int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002433 u16 tss_selector, int reason,
2434 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002435{
2436 struct decode_cache *c = &ctxt->decode;
2437 int rc;
2438
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002439 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002440 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002441
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002442 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002443 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002444
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002445 if (rc == X86EMUL_CONTINUE)
2446 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002447
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002448 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002449}
2450
Avi Kivity90de84f2010-11-17 15:28:21 +02002451static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002452 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002453{
2454 struct decode_cache *c = &ctxt->decode;
2455 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2456
Gleb Natapovd9271122010-03-18 15:20:22 +02002457 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002458 op->addr.mem.ea = register_address(c, c->regs[reg]);
2459 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002460}
2461
Avi Kivity7af04fc2010-08-18 14:16:35 +03002462static int em_das(struct x86_emulate_ctxt *ctxt)
2463{
2464 struct decode_cache *c = &ctxt->decode;
2465 u8 al, old_al;
2466 bool af, cf, old_cf;
2467
2468 cf = ctxt->eflags & X86_EFLAGS_CF;
2469 al = c->dst.val;
2470
2471 old_al = al;
2472 old_cf = cf;
2473 cf = false;
2474 af = ctxt->eflags & X86_EFLAGS_AF;
2475 if ((al & 0x0f) > 9 || af) {
2476 al -= 6;
2477 cf = old_cf | (al >= 250);
2478 af = true;
2479 } else {
2480 af = false;
2481 }
2482 if (old_al > 0x99 || old_cf) {
2483 al -= 0x60;
2484 cf = true;
2485 }
2486
2487 c->dst.val = al;
2488 /* Set PF, ZF, SF */
2489 c->src.type = OP_IMM;
2490 c->src.val = 0;
2491 c->src.bytes = 1;
2492 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2493 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2494 if (cf)
2495 ctxt->eflags |= X86_EFLAGS_CF;
2496 if (af)
2497 ctxt->eflags |= X86_EFLAGS_AF;
2498 return X86EMUL_CONTINUE;
2499}
2500
Avi Kivity0ef753b2010-08-18 14:51:45 +03002501static int em_call_far(struct x86_emulate_ctxt *ctxt)
2502{
2503 struct decode_cache *c = &ctxt->decode;
2504 u16 sel, old_cs;
2505 ulong old_eip;
2506 int rc;
2507
Avi Kivity1aa36612011-04-27 13:20:30 +03002508 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002509 old_eip = c->eip;
2510
2511 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002512 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002513 return X86EMUL_CONTINUE;
2514
2515 c->eip = 0;
2516 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2517
2518 c->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002519 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002520 if (rc != X86EMUL_CONTINUE)
2521 return rc;
2522
2523 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002524 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002525}
2526
Avi Kivity40ece7c2010-08-18 15:12:09 +03002527static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2528{
2529 struct decode_cache *c = &ctxt->decode;
2530 int rc;
2531
2532 c->dst.type = OP_REG;
2533 c->dst.addr.reg = &c->eip;
2534 c->dst.bytes = c->op_bytes;
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09002535 rc = emulate_pop(ctxt, &c->dst.val, c->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002536 if (rc != X86EMUL_CONTINUE)
2537 return rc;
2538 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2539 return X86EMUL_CONTINUE;
2540}
2541
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002542static int em_add(struct x86_emulate_ctxt *ctxt)
2543{
2544 struct decode_cache *c = &ctxt->decode;
2545
2546 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
2547 return X86EMUL_CONTINUE;
2548}
2549
2550static int em_or(struct x86_emulate_ctxt *ctxt)
2551{
2552 struct decode_cache *c = &ctxt->decode;
2553
2554 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2555 return X86EMUL_CONTINUE;
2556}
2557
2558static int em_adc(struct x86_emulate_ctxt *ctxt)
2559{
2560 struct decode_cache *c = &ctxt->decode;
2561
2562 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
2563 return X86EMUL_CONTINUE;
2564}
2565
2566static int em_sbb(struct x86_emulate_ctxt *ctxt)
2567{
2568 struct decode_cache *c = &ctxt->decode;
2569
2570 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
2571 return X86EMUL_CONTINUE;
2572}
2573
2574static int em_and(struct x86_emulate_ctxt *ctxt)
2575{
2576 struct decode_cache *c = &ctxt->decode;
2577
2578 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
2579 return X86EMUL_CONTINUE;
2580}
2581
2582static int em_sub(struct x86_emulate_ctxt *ctxt)
2583{
2584 struct decode_cache *c = &ctxt->decode;
2585
2586 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
2587 return X86EMUL_CONTINUE;
2588}
2589
2590static int em_xor(struct x86_emulate_ctxt *ctxt)
2591{
2592 struct decode_cache *c = &ctxt->decode;
2593
2594 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
2595 return X86EMUL_CONTINUE;
2596}
2597
2598static int em_cmp(struct x86_emulate_ctxt *ctxt)
2599{
2600 struct decode_cache *c = &ctxt->decode;
2601
2602 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
2603 /* Disable writeback. */
2604 c->dst.type = OP_NONE;
2605 return X86EMUL_CONTINUE;
2606}
2607
Avi Kivity5c82aa22010-08-18 18:31:43 +03002608static int em_imul(struct x86_emulate_ctxt *ctxt)
2609{
2610 struct decode_cache *c = &ctxt->decode;
2611
2612 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2613 return X86EMUL_CONTINUE;
2614}
2615
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002616static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2617{
2618 struct decode_cache *c = &ctxt->decode;
2619
2620 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002621 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002622}
2623
Avi Kivity61429142010-08-19 15:13:00 +03002624static int em_cwd(struct x86_emulate_ctxt *ctxt)
2625{
2626 struct decode_cache *c = &ctxt->decode;
2627
2628 c->dst.type = OP_REG;
2629 c->dst.bytes = c->src.bytes;
2630 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2631 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2632
2633 return X86EMUL_CONTINUE;
2634}
2635
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002636static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2637{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002638 struct decode_cache *c = &ctxt->decode;
2639 u64 tsc = 0;
2640
Avi Kivity717746e2011-04-20 13:37:53 +03002641 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002642 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2643 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2644 return X86EMUL_CONTINUE;
2645}
2646
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002647static int em_mov(struct x86_emulate_ctxt *ctxt)
2648{
2649 struct decode_cache *c = &ctxt->decode;
2650 c->dst.val = c->src.val;
2651 return X86EMUL_CONTINUE;
2652}
2653
Avi Kivityaa97bb42010-01-20 18:09:23 +02002654static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2655{
2656 struct decode_cache *c = &ctxt->decode;
2657 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2658 return X86EMUL_CONTINUE;
2659}
2660
Avi Kivity38503912011-03-31 18:48:09 +02002661static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2662{
2663 struct decode_cache *c = &ctxt->decode;
Avi Kivity9fa088f2011-03-31 18:54:30 +02002664 int rc;
2665 ulong linear;
2666
Avi Kivity83b87952011-04-03 11:31:19 +03002667 rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002668 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002669 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002670 /* Disable writeback. */
2671 c->dst.type = OP_NONE;
2672 return X86EMUL_CONTINUE;
2673}
2674
Avi Kivity2d04a052011-04-20 15:32:49 +03002675static int em_clts(struct x86_emulate_ctxt *ctxt)
2676{
2677 ulong cr0;
2678
2679 cr0 = ctxt->ops->get_cr(ctxt, 0);
2680 cr0 &= ~X86_CR0_TS;
2681 ctxt->ops->set_cr(ctxt, 0, cr0);
2682 return X86EMUL_CONTINUE;
2683}
2684
Avi Kivity26d05cc2011-04-21 12:07:59 +03002685static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2686{
2687 struct decode_cache *c = &ctxt->decode;
2688 int rc;
2689
2690 if (c->modrm_mod != 3 || c->modrm_rm != 1)
2691 return X86EMUL_UNHANDLEABLE;
2692
2693 rc = ctxt->ops->fix_hypercall(ctxt);
2694 if (rc != X86EMUL_CONTINUE)
2695 return rc;
2696
2697 /* Let the processor re-execute the fixed hypercall */
2698 c->eip = ctxt->eip;
2699 /* Disable writeback. */
2700 c->dst.type = OP_NONE;
2701 return X86EMUL_CONTINUE;
2702}
2703
2704static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2705{
2706 struct decode_cache *c = &ctxt->decode;
2707 struct desc_ptr desc_ptr;
2708 int rc;
2709
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002710 rc = read_descriptor(ctxt, c->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002711 &desc_ptr.size, &desc_ptr.address,
2712 c->op_bytes);
2713 if (rc != X86EMUL_CONTINUE)
2714 return rc;
2715 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2716 /* Disable writeback. */
2717 c->dst.type = OP_NONE;
2718 return X86EMUL_CONTINUE;
2719}
2720
Avi Kivity5ef39c72011-04-21 12:21:50 +03002721static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002722{
2723 struct decode_cache *c = &ctxt->decode;
2724 int rc;
2725
Avi Kivity5ef39c72011-04-21 12:21:50 +03002726 rc = ctxt->ops->fix_hypercall(ctxt);
2727
Avi Kivity26d05cc2011-04-21 12:07:59 +03002728 /* Disable writeback. */
2729 c->dst.type = OP_NONE;
2730 return rc;
2731}
2732
2733static int em_lidt(struct x86_emulate_ctxt *ctxt)
2734{
2735 struct decode_cache *c = &ctxt->decode;
2736 struct desc_ptr desc_ptr;
2737 int rc;
2738
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002739 rc = read_descriptor(ctxt, c->src.addr.mem,
2740 &desc_ptr.size, &desc_ptr.address,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002741 c->op_bytes);
2742 if (rc != X86EMUL_CONTINUE)
2743 return rc;
2744 ctxt->ops->set_idt(ctxt, &desc_ptr);
2745 /* Disable writeback. */
2746 c->dst.type = OP_NONE;
2747 return X86EMUL_CONTINUE;
2748}
2749
2750static int em_smsw(struct x86_emulate_ctxt *ctxt)
2751{
2752 struct decode_cache *c = &ctxt->decode;
2753
2754 c->dst.bytes = 2;
2755 c->dst.val = ctxt->ops->get_cr(ctxt, 0);
2756 return X86EMUL_CONTINUE;
2757}
2758
2759static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2760{
2761 struct decode_cache *c = &ctxt->decode;
2762 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
2763 | (c->src.val & 0x0f));
2764 c->dst.type = OP_NONE;
2765 return X86EMUL_CONTINUE;
2766}
2767
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002768static bool valid_cr(int nr)
2769{
2770 switch (nr) {
2771 case 0:
2772 case 2 ... 4:
2773 case 8:
2774 return true;
2775 default:
2776 return false;
2777 }
2778}
2779
2780static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2781{
2782 struct decode_cache *c = &ctxt->decode;
2783
2784 if (!valid_cr(c->modrm_reg))
2785 return emulate_ud(ctxt);
2786
2787 return X86EMUL_CONTINUE;
2788}
2789
2790static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2791{
2792 struct decode_cache *c = &ctxt->decode;
2793 u64 new_val = c->src.val64;
2794 int cr = c->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002795 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002796
2797 static u64 cr_reserved_bits[] = {
2798 0xffffffff00000000ULL,
2799 0, 0, 0, /* CR3 checked later */
2800 CR4_RESERVED_BITS,
2801 0, 0, 0,
2802 CR8_RESERVED_BITS,
2803 };
2804
2805 if (!valid_cr(cr))
2806 return emulate_ud(ctxt);
2807
2808 if (new_val & cr_reserved_bits[cr])
2809 return emulate_gp(ctxt, 0);
2810
2811 switch (cr) {
2812 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002813 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002814 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2815 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2816 return emulate_gp(ctxt, 0);
2817
Avi Kivity717746e2011-04-20 13:37:53 +03002818 cr4 = ctxt->ops->get_cr(ctxt, 4);
2819 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002820
2821 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2822 !(cr4 & X86_CR4_PAE))
2823 return emulate_gp(ctxt, 0);
2824
2825 break;
2826 }
2827 case 3: {
2828 u64 rsvd = 0;
2829
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002830 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2831 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002832 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002833 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002834 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002835 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002836 rsvd = CR3_NONPAE_RESERVED_BITS;
2837
2838 if (new_val & rsvd)
2839 return emulate_gp(ctxt, 0);
2840
2841 break;
2842 }
2843 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002844 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002845
Avi Kivity717746e2011-04-20 13:37:53 +03002846 cr4 = ctxt->ops->get_cr(ctxt, 4);
2847 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002848
2849 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2850 return emulate_gp(ctxt, 0);
2851
2852 break;
2853 }
2854 }
2855
2856 return X86EMUL_CONTINUE;
2857}
2858
Joerg Roedel3b88e412011-04-04 12:39:29 +02002859static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2860{
2861 unsigned long dr7;
2862
Avi Kivity717746e2011-04-20 13:37:53 +03002863 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002864
2865 /* Check if DR7.Global_Enable is set */
2866 return dr7 & (1 << 13);
2867}
2868
2869static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2870{
2871 struct decode_cache *c = &ctxt->decode;
2872 int dr = c->modrm_reg;
2873 u64 cr4;
2874
2875 if (dr > 7)
2876 return emulate_ud(ctxt);
2877
Avi Kivity717746e2011-04-20 13:37:53 +03002878 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002879 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2880 return emulate_ud(ctxt);
2881
2882 if (check_dr7_gd(ctxt))
2883 return emulate_db(ctxt);
2884
2885 return X86EMUL_CONTINUE;
2886}
2887
2888static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2889{
2890 struct decode_cache *c = &ctxt->decode;
2891 u64 new_val = c->src.val64;
2892 int dr = c->modrm_reg;
2893
2894 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2895 return emulate_gp(ctxt, 0);
2896
2897 return check_dr_read(ctxt);
2898}
2899
Joerg Roedel01de8b02011-04-04 12:39:31 +02002900static int check_svme(struct x86_emulate_ctxt *ctxt)
2901{
2902 u64 efer;
2903
Avi Kivity717746e2011-04-20 13:37:53 +03002904 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002905
2906 if (!(efer & EFER_SVME))
2907 return emulate_ud(ctxt);
2908
2909 return X86EMUL_CONTINUE;
2910}
2911
2912static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2913{
Avi Kivityfe870ab2011-04-20 15:01:23 +03002914 u64 rax = ctxt->decode.regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002915
2916 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002917 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002918 return emulate_gp(ctxt, 0);
2919
2920 return check_svme(ctxt);
2921}
2922
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002923static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2924{
Avi Kivity717746e2011-04-20 13:37:53 +03002925 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002926
Avi Kivity717746e2011-04-20 13:37:53 +03002927 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002928 return emulate_ud(ctxt);
2929
2930 return X86EMUL_CONTINUE;
2931}
2932
Joerg Roedel80612522011-04-04 12:39:33 +02002933static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2934{
Avi Kivity717746e2011-04-20 13:37:53 +03002935 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivityfe870ab2011-04-20 15:01:23 +03002936 u64 rcx = ctxt->decode.regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002937
Avi Kivity717746e2011-04-20 13:37:53 +03002938 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002939 (rcx > 3))
2940 return emulate_gp(ctxt, 0);
2941
2942 return X86EMUL_CONTINUE;
2943}
2944
Joerg Roedelf6511932011-04-04 12:39:35 +02002945static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2946{
2947 struct decode_cache *c = &ctxt->decode;
2948
2949 c->dst.bytes = min(c->dst.bytes, 4u);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002950 if (!emulator_io_permited(ctxt, c->src.val, c->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002951 return emulate_gp(ctxt, 0);
2952
2953 return X86EMUL_CONTINUE;
2954}
2955
2956static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2957{
2958 struct decode_cache *c = &ctxt->decode;
2959
2960 c->src.bytes = min(c->src.bytes, 4u);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002961 if (!emulator_io_permited(ctxt, c->dst.val, c->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002962 return emulate_gp(ctxt, 0);
2963
2964 return X86EMUL_CONTINUE;
2965}
2966
Avi Kivity73fba5f2010-07-29 15:11:53 +03002967#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002968#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002969#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2970 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002971#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002972#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002973#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002974#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002975#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002976#define II(_f, _e, _i) \
2977 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002978#define IIP(_f, _e, _i, _p) \
2979 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2980 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002981#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002982
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002983#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002984#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002985#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2986
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002987#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2988 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2989 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002990
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002991static struct opcode group7_rm1[] = {
2992 DI(SrcNone | ModRM | Priv, monitor),
2993 DI(SrcNone | ModRM | Priv, mwait),
2994 N, N, N, N, N, N,
2995};
2996
Joerg Roedel01de8b02011-04-04 12:39:31 +02002997static struct opcode group7_rm3[] = {
2998 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03002999 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003000 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3001 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3002 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3003 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3004 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3005 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3006};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003007
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003008static struct opcode group7_rm7[] = {
3009 N,
3010 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3011 N, N, N, N, N, N,
3012};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003013
Avi Kivity73fba5f2010-07-29 15:11:53 +03003014static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003015 I(Lock, em_add),
3016 I(Lock, em_or),
3017 I(Lock, em_adc),
3018 I(Lock, em_sbb),
3019 I(Lock, em_and),
3020 I(Lock, em_sub),
3021 I(Lock, em_xor),
3022 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003023};
3024
3025static struct opcode group1A[] = {
3026 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3027};
3028
3029static struct opcode group3[] = {
3030 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
3031 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03003032 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003033};
3034
3035static struct opcode group4[] = {
3036 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3037 N, N, N, N, N, N,
3038};
3039
3040static struct opcode group5[] = {
3041 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003042 D(SrcMem | ModRM | Stack),
3043 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003044 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3045 D(SrcMem | ModRM | Stack), N,
3046};
3047
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003048static struct opcode group6[] = {
3049 DI(ModRM | Prot, sldt),
3050 DI(ModRM | Prot, str),
3051 DI(ModRM | Prot | Priv, lldt),
3052 DI(ModRM | Prot | Priv, ltr),
3053 N, N, N, N,
3054};
3055
Avi Kivity73fba5f2010-07-29 15:11:53 +03003056static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003057 DI(ModRM | Mov | DstMem | Priv, sgdt),
3058 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003059 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3060 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3061 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3062 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3063 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003064}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003065 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3066 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003067 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003068 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3069 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003070} };
3071
3072static struct opcode group8[] = {
3073 N, N, N, N,
3074 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3075 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3076};
3077
3078static struct group_dual group9 = { {
3079 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3080}, {
3081 N, N, N, N, N, N, N, N,
3082} };
3083
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003084static struct opcode group11[] = {
3085 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3086};
3087
Avi Kivityaa97bb42010-01-20 18:09:23 +02003088static struct gprefix pfx_0f_6f_0f_7f = {
3089 N, N, N, I(Sse, em_movdqu),
3090};
3091
Avi Kivity73fba5f2010-07-29 15:11:53 +03003092static struct opcode opcode_table[256] = {
3093 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003094 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003095 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3096 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003097 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003098 D(ImplicitOps | Stack | No64), N,
3099 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003100 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003101 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3102 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003103 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003104 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3105 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003106 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003107 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003108 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003109 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003110 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003111 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003112 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003113 /* 0x40 - 0x4F */
3114 X16(D(DstReg)),
3115 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003116 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003117 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003118 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003119 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003120 I(ImplicitOps | Stack | No64, em_pusha),
3121 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003122 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3123 N, N, N, N,
3124 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003125 I(SrcImm | Mov | Stack, em_push),
3126 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003127 I(SrcImmByte | Mov | Stack, em_push),
3128 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003129 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3130 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003131 /* 0x70 - 0x7F */
3132 X16(D(SrcImmByte)),
3133 /* 0x80 - 0x87 */
3134 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3135 G(DstMem | SrcImm | ModRM | Group, group1),
3136 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3137 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03003138 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003139 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003140 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3141 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03003142 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003143 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
3144 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003145 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003146 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003147 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003148 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003149 II(ImplicitOps | Stack, em_pushf, pushf),
3150 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003151 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003152 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3153 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3154 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003155 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003156 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03003157 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003158 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3159 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003160 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003161 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003162 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003163 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003164 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003165 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003166 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003167 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
3168 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003169 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003170 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003171 /* 0xC8 - 0xCF */
3172 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003173 D(ImplicitOps), DI(SrcImmByte, intn),
3174 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003175 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003176 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003177 N, N, N, N,
3178 /* 0xD8 - 0xDF */
3179 N, N, N, N, N, N, N, N,
3180 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08003181 X4(D(SrcImmByte)),
Joerg Roedelf6511932011-04-04 12:39:35 +02003182 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3183 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003184 /* 0xE8 - 0xEF */
3185 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
3186 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003187 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3188 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003189 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003190 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003191 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3192 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003193 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003194 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003195 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3196};
3197
3198static struct opcode twobyte_table[256] = {
3199 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003200 G(0, group6), GD(0, &group7), N, N,
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003201 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003202 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003203 N, D(ImplicitOps | ModRM), N, N,
3204 /* 0x10 - 0x1F */
3205 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3206 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003207 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003208 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003209 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003210 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003211 N, N, N, N,
3212 N, N, N, N, N, N, N, N,
3213 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003214 DI(ImplicitOps | Priv, wrmsr),
3215 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3216 DI(ImplicitOps | Priv, rdmsr),
3217 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Avi Kivityd8671622011-02-01 16:32:03 +02003218 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
3219 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003220 N, N, N, N, N, N, N, N,
3221 /* 0x40 - 0x4F */
3222 X16(D(DstReg | SrcMem | ModRM | Mov)),
3223 /* 0x50 - 0x5F */
3224 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3225 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003226 N, N, N, N,
3227 N, N, N, N,
3228 N, N, N, N,
3229 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003230 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003231 N, N, N, N,
3232 N, N, N, N,
3233 N, N, N, N,
3234 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003235 /* 0x80 - 0x8F */
3236 X16(D(SrcImm)),
3237 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003238 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003239 /* 0xA0 - 0xA7 */
3240 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003241 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003242 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3243 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3244 /* 0xA8 - 0xAF */
3245 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003246 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003247 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3248 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003249 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003250 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003251 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003252 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3253 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3254 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003255 /* 0xB8 - 0xBF */
3256 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003257 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003258 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3259 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003260 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003261 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003262 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003263 N, N, N, GD(0, &group9),
3264 N, N, N, N, N, N, N, N,
3265 /* 0xD0 - 0xDF */
3266 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3267 /* 0xE0 - 0xEF */
3268 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3269 /* 0xF0 - 0xFF */
3270 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3271};
3272
3273#undef D
3274#undef N
3275#undef G
3276#undef GD
3277#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003278#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003279#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003280
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003281#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003282#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003283#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003284#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003285
Avi Kivity39f21ee2010-08-18 19:20:21 +03003286static unsigned imm_size(struct decode_cache *c)
3287{
3288 unsigned size;
3289
3290 size = (c->d & ByteOp) ? 1 : c->op_bytes;
3291 if (size == 8)
3292 size = 4;
3293 return size;
3294}
3295
3296static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3297 unsigned size, bool sign_extension)
3298{
3299 struct decode_cache *c = &ctxt->decode;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003300 int rc = X86EMUL_CONTINUE;
3301
3302 op->type = OP_IMM;
3303 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02003304 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003305 /* NB. Immediates are sign-extended as necessary. */
3306 switch (op->bytes) {
3307 case 1:
3308 op->val = insn_fetch(s8, 1, c->eip);
3309 break;
3310 case 2:
3311 op->val = insn_fetch(s16, 2, c->eip);
3312 break;
3313 case 4:
3314 op->val = insn_fetch(s32, 4, c->eip);
3315 break;
3316 }
3317 if (!sign_extension) {
3318 switch (op->bytes) {
3319 case 1:
3320 op->val &= 0xff;
3321 break;
3322 case 2:
3323 op->val &= 0xffff;
3324 break;
3325 case 4:
3326 op->val &= 0xffffffff;
3327 break;
3328 }
3329 }
3330done:
3331 return rc;
3332}
3333
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003334int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003335{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003336 struct decode_cache *c = &ctxt->decode;
3337 int rc = X86EMUL_CONTINUE;
3338 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003339 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003340 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003341 struct opcode opcode;
Avi Kivitycb16c342011-06-19 19:21:11 +03003342 struct operand memop = { .type = OP_NONE }, *memopp = NULL;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003343
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003344 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01003345 c->fetch.start = c->eip;
3346 c->fetch.end = c->fetch.start + insn_len;
3347 if (insn_len > 0)
3348 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003349
3350 switch (mode) {
3351 case X86EMUL_MODE_REAL:
3352 case X86EMUL_MODE_VM86:
3353 case X86EMUL_MODE_PROT16:
3354 def_op_bytes = def_ad_bytes = 2;
3355 break;
3356 case X86EMUL_MODE_PROT32:
3357 def_op_bytes = def_ad_bytes = 4;
3358 break;
3359#ifdef CONFIG_X86_64
3360 case X86EMUL_MODE_PROT64:
3361 def_op_bytes = 4;
3362 def_ad_bytes = 8;
3363 break;
3364#endif
3365 default:
3366 return -1;
3367 }
3368
3369 c->op_bytes = def_op_bytes;
3370 c->ad_bytes = def_ad_bytes;
3371
3372 /* Legacy prefixes. */
3373 for (;;) {
3374 switch (c->b = insn_fetch(u8, 1, c->eip)) {
3375 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003376 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003377 /* switch between 2/4 bytes */
3378 c->op_bytes = def_op_bytes ^ 6;
3379 break;
3380 case 0x67: /* address-size override */
3381 if (mode == X86EMUL_MODE_PROT64)
3382 /* switch between 4/8 bytes */
3383 c->ad_bytes = def_ad_bytes ^ 12;
3384 else
3385 /* switch between 2/4 bytes */
3386 c->ad_bytes = def_ad_bytes ^ 6;
3387 break;
3388 case 0x26: /* ES override */
3389 case 0x2e: /* CS override */
3390 case 0x36: /* SS override */
3391 case 0x3e: /* DS override */
3392 set_seg_override(c, (c->b >> 3) & 3);
3393 break;
3394 case 0x64: /* FS override */
3395 case 0x65: /* GS override */
3396 set_seg_override(c, c->b & 7);
3397 break;
3398 case 0x40 ... 0x4f: /* REX */
3399 if (mode != X86EMUL_MODE_PROT64)
3400 goto done_prefixes;
3401 c->rex_prefix = c->b;
3402 continue;
3403 case 0xf0: /* LOCK */
3404 c->lock_prefix = 1;
3405 break;
3406 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003407 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02003408 c->rep_prefix = c->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003409 break;
3410 default:
3411 goto done_prefixes;
3412 }
3413
3414 /* Any legacy prefix after a REX prefix nullifies its effect. */
3415
3416 c->rex_prefix = 0;
3417 }
3418
3419done_prefixes:
3420
3421 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03003422 if (c->rex_prefix & 8)
3423 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003424
3425 /* Opcode byte(s). */
3426 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003427 /* Two-byte opcode? */
3428 if (c->b == 0x0f) {
3429 c->twobyte = 1;
3430 c->b = insn_fetch(u8, 1, c->eip);
3431 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003432 }
3433 c->d = opcode.flags;
3434
Avi Kivity46561642011-04-24 14:09:59 +03003435 while (c->d & GroupMask) {
3436 switch (c->d & GroupMask) {
3437 case Group:
3438 c->modrm = insn_fetch(u8, 1, c->eip);
3439 --c->eip;
3440 goffset = (c->modrm >> 3) & 7;
3441 opcode = opcode.u.group[goffset];
3442 break;
3443 case GroupDual:
3444 c->modrm = insn_fetch(u8, 1, c->eip);
3445 --c->eip;
3446 goffset = (c->modrm >> 3) & 7;
3447 if ((c->modrm >> 6) == 3)
3448 opcode = opcode.u.gdual->mod3[goffset];
3449 else
3450 opcode = opcode.u.gdual->mod012[goffset];
3451 break;
3452 case RMExt:
Joerg Roedel01de8b02011-04-04 12:39:31 +02003453 goffset = c->modrm & 7;
3454 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003455 break;
3456 case Prefix:
3457 if (c->rep_prefix && op_prefix)
3458 return X86EMUL_UNHANDLEABLE;
3459 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3460 switch (simd_prefix) {
3461 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3462 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3463 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3464 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3465 }
3466 break;
3467 default:
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003468 return X86EMUL_UNHANDLEABLE;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003469 }
Avi Kivity46561642011-04-24 14:09:59 +03003470
3471 c->d &= ~GroupMask;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003472 c->d |= opcode.flags;
3473 }
3474
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003475 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02003476 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02003477 c->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003478
3479 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02003480 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003481 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003482
Avi Kivityd8671622011-02-01 16:32:03 +02003483 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3484 return -1;
3485
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003486 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3487 c->op_bytes = 8;
3488
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003489 if (c->d & Op3264) {
3490 if (mode == X86EMUL_MODE_PROT64)
3491 c->op_bytes = 8;
3492 else
3493 c->op_bytes = 4;
3494 }
3495
Avi Kivity12537912011-03-29 11:41:27 +02003496 if (c->d & Sse)
3497 c->op_bytes = 16;
3498
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003499 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03003500 if (c->d & ModRM) {
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003501 rc = decode_modrm(ctxt, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03003502 if (!c->has_seg_override)
3503 set_seg_override(c, c->modrm_seg);
3504 } else if (c->d & MemAbs)
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003505 rc = decode_abs(ctxt, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003506 if (rc != X86EMUL_CONTINUE)
3507 goto done;
3508
3509 if (!c->has_seg_override)
3510 set_seg_override(c, VCPU_SREG_DS);
3511
Takuya Yoshikawac1ed6de2011-05-02 02:23:13 +09003512 memop.addr.mem.seg = seg_override(ctxt, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003513
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003514 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003515 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003516
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003517 /*
3518 * Decode and fetch the source operand: register, memory
3519 * or immediate.
3520 */
3521 switch (c->d & SrcMask) {
3522 case SrcNone:
3523 break;
3524 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02003525 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003526 break;
3527 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003528 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003529 goto srcmem_common;
3530 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003531 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003532 goto srcmem_common;
3533 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003534 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003535 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003536 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003537 c->src = memop;
Avi Kivitycb16c342011-06-19 19:21:11 +03003538 memopp = &c->src;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003539 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003540 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003541 rc = decode_imm(ctxt, &c->src, 2, false);
3542 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003543 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003544 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3545 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003546 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003547 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003548 break;
3549 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003550 rc = decode_imm(ctxt, &c->src, 1, true);
3551 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003552 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003553 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003554 break;
3555 case SrcAcc:
3556 c->src.type = OP_REG;
3557 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003558 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003559 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003560 break;
3561 case SrcOne:
3562 c->src.bytes = 1;
3563 c->src.val = 1;
3564 break;
3565 case SrcSI:
3566 c->src.type = OP_MEM;
3567 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003568 c->src.addr.mem.ea =
3569 register_address(c, c->regs[VCPU_REGS_RSI]);
Takuya Yoshikawac1ed6de2011-05-02 02:23:13 +09003570 c->src.addr.mem.seg = seg_override(ctxt, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003571 c->src.val = 0;
3572 break;
3573 case SrcImmFAddr:
3574 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003575 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003576 c->src.bytes = c->op_bytes + 2;
3577 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3578 break;
3579 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003580 memop.bytes = c->op_bytes + 2;
3581 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003582 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003583 case SrcDX:
3584 c->src.type = OP_REG;
3585 c->src.bytes = 2;
3586 c->src.addr.reg = &c->regs[VCPU_REGS_RDX];
3587 fetch_register_operand(&c->src);
3588 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003589 }
3590
Avi Kivity39f21ee2010-08-18 19:20:21 +03003591 if (rc != X86EMUL_CONTINUE)
3592 goto done;
3593
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003594 /*
3595 * Decode and fetch the second source operand: register, memory
3596 * or immediate.
3597 */
3598 switch (c->d & Src2Mask) {
3599 case Src2None:
3600 break;
3601 case Src2CL:
3602 c->src2.bytes = 1;
3603 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3604 break;
3605 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003606 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003607 break;
3608 case Src2One:
3609 c->src2.bytes = 1;
3610 c->src2.val = 1;
3611 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003612 case Src2Imm:
3613 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3614 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003615 }
3616
Avi Kivity39f21ee2010-08-18 19:20:21 +03003617 if (rc != X86EMUL_CONTINUE)
3618 goto done;
3619
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003620 /* Decode and fetch the destination operand: register or memory. */
3621 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003622 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003623 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003624 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3625 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003626 case DstImmUByte:
3627 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003628 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003629 c->dst.bytes = 1;
3630 c->dst.val = insn_fetch(u8, 1, c->eip);
3631 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003632 case DstMem:
3633 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003634 c->dst = memop;
Avi Kivitycb16c342011-06-19 19:21:11 +03003635 memopp = &c->dst;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003636 if ((c->d & DstMask) == DstMem64)
3637 c->dst.bytes = 8;
3638 else
3639 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003640 if (c->d & BitOp)
3641 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003642 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003643 break;
3644 case DstAcc:
3645 c->dst.type = OP_REG;
3646 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003647 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003648 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003649 c->dst.orig_val = c->dst.val;
3650 break;
3651 case DstDI:
3652 c->dst.type = OP_MEM;
3653 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003654 c->dst.addr.mem.ea =
3655 register_address(c, c->regs[VCPU_REGS_RDI]);
3656 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003657 c->dst.val = 0;
3658 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003659 case DstDX:
3660 c->dst.type = OP_REG;
3661 c->dst.bytes = 2;
3662 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
3663 fetch_register_operand(&c->dst);
3664 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003665 case ImplicitOps:
3666 /* Special instructions do their own operand decoding. */
3667 default:
3668 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivitycb16c342011-06-19 19:21:11 +03003669 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003670 }
3671
3672done:
Avi Kivitycb16c342011-06-19 19:21:11 +03003673 if (memopp && memopp->type == OP_MEM && c->rip_relative)
3674 memopp->addr.mem.ea += c->eip;
3675
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003676 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003677}
3678
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003679static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3680{
3681 struct decode_cache *c = &ctxt->decode;
3682
3683 /* The second termination condition only applies for REPE
3684 * and REPNE. Test if the repeat string operation prefix is
3685 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3686 * corresponding termination condition according to:
3687 * - if REPE/REPZ and ZF = 0 then done
3688 * - if REPNE/REPNZ and ZF = 1 then done
3689 */
3690 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3691 (c->b == 0xae) || (c->b == 0xaf))
3692 && (((c->rep_prefix == REPE_PREFIX) &&
3693 ((ctxt->eflags & EFLG_ZF) == 0))
3694 || ((c->rep_prefix == REPNE_PREFIX) &&
3695 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3696 return true;
3697
3698 return false;
3699}
3700
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003701int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003702{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003703 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003704 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003705 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003706 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003707 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003708 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003709
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09003710 c->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003711
Gleb Natapov11616242010-02-11 14:43:14 +02003712 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003713 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003714 goto done;
3715 }
3716
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003717 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003718 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003719 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003720 goto done;
3721 }
3722
Avi Kivity081bca02010-08-26 11:06:15 +03003723 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003724 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003725 goto done;
3726 }
3727
Avi Kivity12537912011-03-29 11:41:27 +02003728 if ((c->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003729 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3730 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003731 rc = emulate_ud(ctxt);
3732 goto done;
3733 }
3734
Avi Kivity717746e2011-04-20 13:37:53 +03003735 if ((c->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003736 rc = emulate_nm(ctxt);
3737 goto done;
3738 }
3739
Avi Kivityc4f035c2011-04-04 12:39:22 +02003740 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003741 rc = emulator_check_intercept(ctxt, c->intercept,
3742 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003743 if (rc != X86EMUL_CONTINUE)
3744 goto done;
3745 }
3746
Gleb Natapove92805a2010-02-10 14:21:35 +02003747 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity717746e2011-04-20 13:37:53 +03003748 if ((c->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003749 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003750 goto done;
3751 }
3752
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003753 /* Instruction can only be executed in protected mode */
3754 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3755 rc = emulate_ud(ctxt);
3756 goto done;
3757 }
3758
Joerg Roedeld09beab2011-04-04 12:39:25 +02003759 /* Do instruction specific permission checks */
3760 if (c->check_perm) {
3761 rc = c->check_perm(ctxt);
3762 if (rc != X86EMUL_CONTINUE)
3763 goto done;
3764 }
3765
Avi Kivityc4f035c2011-04-04 12:39:22 +02003766 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003767 rc = emulator_check_intercept(ctxt, c->intercept,
3768 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003769 if (rc != X86EMUL_CONTINUE)
3770 goto done;
3771 }
3772
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003773 if (c->rep_prefix && (c->d & String)) {
3774 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003775 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003776 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003777 goto done;
3778 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003779 }
3780
Wei Yongjunc483c022010-08-06 15:36:36 +08003781 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003782 rc = segmented_read(ctxt, c->src.addr.mem,
3783 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003784 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003785 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003786 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003787 }
3788
Gleb Natapove35b7b92010-02-25 16:36:42 +02003789 if (c->src2.type == OP_MEM) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003790 rc = segmented_read(ctxt, c->src2.addr.mem,
3791 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003792 if (rc != X86EMUL_CONTINUE)
3793 goto done;
3794 }
3795
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003796 if ((c->d & DstMask) == ImplicitOps)
3797 goto special_insn;
3798
3799
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003800 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3801 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003802 rc = segmented_read(ctxt, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03003803 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003804 if (rc != X86EMUL_CONTINUE)
3805 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003806 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003807 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003808
Avi Kivity018a98d2007-11-27 19:30:56 +02003809special_insn:
3810
Avi Kivityc4f035c2011-04-04 12:39:22 +02003811 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003812 rc = emulator_check_intercept(ctxt, c->intercept,
3813 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003814 if (rc != X86EMUL_CONTINUE)
3815 goto done;
3816 }
3817
Avi Kivityef65c882010-07-29 15:11:51 +03003818 if (c->execute) {
3819 rc = c->execute(ctxt);
3820 if (rc != X86EMUL_CONTINUE)
3821 goto done;
3822 goto writeback;
3823 }
3824
Laurent Viviere4e03de2007-09-18 11:52:50 +02003825 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826 goto twobyte_insn;
3827
Laurent Viviere4e03de2007-09-18 11:52:50 +02003828 switch (c->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003829 case 0x06: /* push es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003830 rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003831 break;
3832 case 0x07: /* pop es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003833 rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003834 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003835 case 0x0e: /* push cs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003836 rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003837 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003838 case 0x16: /* push ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003839 rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003840 break;
3841 case 0x17: /* pop ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003842 rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003843 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003844 case 0x1e: /* push ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003845 rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003846 break;
3847 case 0x1f: /* pop ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003848 rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003849 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003850 case 0x40 ... 0x47: /* inc r16/r32 */
3851 emulate_1op("inc", c->dst, ctxt->eflags);
3852 break;
3853 case 0x48 ... 0x4f: /* dec r16/r32 */
3854 emulate_1op("dec", c->dst, ctxt->eflags);
3855 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003856 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003857 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003859 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003861 case 0x6c: /* insb */
3862 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003863 c->src.val = c->regs[VCPU_REGS_RDX];
3864 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003865 case 0x6e: /* outsb */
3866 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003867 c->dst.val = c->regs[VCPU_REGS_RDX];
3868 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003869 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003870 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003871 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003872 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003873 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003875 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003876 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 break;
3878 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003879 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003881 c->src.val = c->dst.val;
3882 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883 /*
3884 * Write back the memory destination with implicit LOCK
3885 * prefix.
3886 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003887 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003888 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003890 case 0x8c: /* mov r/m, sreg */
3891 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003892 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003893 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003894 }
Avi Kivity1aa36612011-04-27 13:20:30 +03003895 c->dst.val = get_segment_selector(ctxt, c->modrm_reg);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003896 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003897 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003898 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003899 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003900 case 0x8e: { /* mov seg, r/m16 */
3901 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003902
3903 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003904
Gleb Natapovc6975182010-02-18 12:15:01 +02003905 if (c->modrm_reg == VCPU_SREG_CS ||
3906 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003907 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003908 goto done;
3909 }
3910
Glauber Costa310b5d32009-05-12 16:21:06 -04003911 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003912 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003913
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003914 rc = load_segment_descriptor(ctxt, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003915
3916 c->dst.type = OP_NONE; /* Disable writeback. */
3917 break;
3918 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003920 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003922 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3923 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003924 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003925 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003926 case 0x98: /* cbw/cwde/cdqe */
3927 switch (c->op_bytes) {
3928 case 2: c->dst.val = (s8)c->dst.val; break;
3929 case 4: c->dst.val = (s16)c->dst.val; break;
3930 case 8: c->dst.val = (s32)c->dst.val; break;
3931 }
3932 break;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003933 case 0xa8 ... 0xa9: /* test ax, imm */
3934 goto test;
Avi Kivity018a98d2007-11-27 19:30:56 +02003935 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003936 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003937 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003938 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003939 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003940 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003941 c->dst.bytes = c->op_bytes;
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003942 rc = em_pop(ctxt);
3943 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003944 case 0xc4: /* les */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003945 rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003946 break;
3947 case 0xc5: /* lds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003948 rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003949 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003950 case 0xcb: /* ret far */
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09003951 rc = em_ret_far(ctxt);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003952 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003953 case 0xcc: /* int3 */
3954 irq = 3;
3955 goto do_interrupt;
3956 case 0xcd: /* int n */
3957 irq = c->src.val;
3958 do_interrupt:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003959 rc = emulate_int(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003960 break;
3961 case 0xce: /* into */
3962 if (ctxt->eflags & EFLG_OF) {
3963 irq = 4;
3964 goto do_interrupt;
3965 }
3966 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003967 case 0xcf: /* iret */
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09003968 rc = em_iret(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003969 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003970 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003971 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003972 break;
3973 case 0xd2 ... 0xd3: /* Grp2 */
3974 c->src.val = c->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003975 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003976 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003977 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3978 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3979 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3980 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3981 jmp_rel(c, c->src.val);
3982 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003983 case 0xe3: /* jcxz/jecxz/jrcxz */
3984 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3985 jmp_rel(c, c->src.val);
3986 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003987 case 0xe4: /* inb */
3988 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003989 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003990 case 0xe6: /* outb */
3991 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003992 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003993 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003994 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003995 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003996 jmp_rel(c, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003997 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003998 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003999 }
4000 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004001 goto jmp;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09004002 case 0xea: /* jmp far */
4003 rc = em_jmp_far(ctxt);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004004 break;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02004005 case 0xeb:
4006 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08004007 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02004008 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004009 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03004010 case 0xec: /* in al,dx */
4011 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004012 do_io_in:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004013 if (!pio_in_emulated(ctxt, c->dst.bytes, c->src.val,
Gleb Natapov7b262e92010-03-18 15:20:27 +02004014 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004015 goto done; /* IO is needed */
4016 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08004017 case 0xee: /* out dx,al */
4018 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004019 do_io_out:
Avi Kivityca1d4a92011-04-20 13:37:53 +03004020 ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
4021 &c->src.val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004022 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004023 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004024 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004025 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004026 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004027 case 0xf5: /* cmc */
4028 /* complement carry flag from eflags reg */
4029 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004030 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004031 case 0xf6 ... 0xf7: /* Grp3 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004032 rc = em_grp3(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004033 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004034 case 0xf8: /* clc */
4035 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004036 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004037 case 0xf9: /* stc */
4038 ctxt->eflags |= EFLG_CF;
4039 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004040 case 0xfa: /* cli */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004041 if (emulator_bad_iopl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004042 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004043 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08004044 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004045 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004046 break;
4047 case 0xfb: /* sti */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004048 if (emulator_bad_iopl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004049 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08004050 goto done;
4051 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03004052 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004053 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02004054 }
Avi Kivity111de5d2007-11-27 19:14:21 +02004055 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004056 case 0xfc: /* cld */
4057 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004058 break;
4059 case 0xfd: /* std */
4060 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004061 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02004062 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004063 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004064 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02004065 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004066 rc = em_grp45(ctxt);
4067 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004068 default:
4069 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004071
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004072 if (rc != X86EMUL_CONTINUE)
4073 goto done;
4074
Avi Kivity018a98d2007-11-27 19:30:56 +02004075writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004076 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004077 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004078 goto done;
4079
Gleb Natapov5cd21912010-03-18 15:20:26 +02004080 /*
4081 * restore dst type in case the decoding will be reused
4082 * (happens for string instruction )
4083 */
4084 c->dst.type = saved_dst_type;
4085
Gleb Natapova682e352010-03-18 15:20:21 +02004086 if ((c->d & SrcMask) == SrcSI)
Takuya Yoshikawac1ed6de2011-05-02 02:23:13 +09004087 string_addr_inc(ctxt, seg_override(ctxt, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03004088 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004089
4090 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004091 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03004092 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004093
Gleb Natapov5cd21912010-03-18 15:20:26 +02004094 if (c->rep_prefix && (c->d & String)) {
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004095 struct read_cache *r = &c->io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02004096 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004097
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004098 if (!string_insn_completed(ctxt)) {
4099 /*
4100 * Re-enter guest when pio read ahead buffer is empty
4101 * or, if it is not used, after each 1024 iteration.
4102 */
4103 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
4104 (r->end == 0 || r->end != r->pos)) {
4105 /*
4106 * Reset read cache. Usually happens before
4107 * decode, but since instruction is restarted
4108 * we have to do it here.
4109 */
Takuya Yoshikawa9d741912011-05-29 21:53:48 +09004110 c->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004111 return EMULATION_RESTART;
4112 }
4113 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004114 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004115 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004116
4117 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004118
4119done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004120 if (rc == X86EMUL_PROPAGATE_FAULT)
4121 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004122 if (rc == X86EMUL_INTERCEPTED)
4123 return EMULATION_INTERCEPTED;
4124
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004125 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126
4127twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02004128 switch (c->b) {
Andre Przywarae99f0502009-06-17 15:50:33 +02004129 case 0x05: /* syscall */
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09004130 rc = em_syscall(ctxt);
Andre Przywarae99f0502009-06-17 15:50:33 +02004131 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004132 case 0x06:
Avi Kivity2d04a052011-04-20 15:32:49 +03004133 rc = em_clts(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004134 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004135 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004136 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004137 break;
4138 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004139 case 0x0d: /* GrpP (prefetch) */
4140 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004141 break;
4142 case 0x20: /* mov cr, reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004143 c->dst.val = ops->get_cr(ctxt, c->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004144 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145 case 0x21: /* mov from dr to reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004146 ops->get_dr(ctxt, c->modrm_reg, &c->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004148 case 0x22: /* mov reg, cr */
Avi Kivity717746e2011-04-20 13:37:53 +03004149 if (ops->set_cr(ctxt, c->modrm_reg, c->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004150 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004151 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004152 goto done;
4153 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004154 c->dst.type = OP_NONE;
4155 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156 case 0x23: /* mov from reg to dr */
Avi Kivity717746e2011-04-20 13:37:53 +03004157 if (ops->set_dr(ctxt, c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004158 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004159 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004160 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004161 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004162 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004163 goto done;
4164 }
4165
Laurent Viviera01af5e2007-09-24 11:10:56 +02004166 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004167 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004168 case 0x30:
4169 /* wrmsr */
4170 msr_data = (u32)c->regs[VCPU_REGS_RAX]
4171 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Avi Kivity717746e2011-04-20 13:37:53 +03004172 if (ops->set_msr(ctxt, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004173 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004174 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004175 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004176 }
4177 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004178 break;
4179 case 0x32:
4180 /* rdmsr */
Avi Kivity717746e2011-04-20 13:37:53 +03004181 if (ops->get_msr(ctxt, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004182 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004183 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004184 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004185 } else {
4186 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
4187 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
4188 }
4189 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004190 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02004191 case 0x34: /* sysenter */
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09004192 rc = em_sysenter(ctxt);
Andre Przywarae99f0502009-06-17 15:50:33 +02004193 break;
4194 case 0x35: /* sysexit */
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09004195 rc = em_sysexit(ctxt);
Andre Przywarae99f0502009-06-17 15:50:33 +02004196 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004198 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02004199 if (!test_cc(c->b, ctxt->eflags))
4200 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004202 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02004203 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03004204 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004205 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004206 case 0x90 ... 0x9f: /* setcc r/m8 */
4207 c->dst.val = test_cc(c->b, ctxt->eflags);
4208 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004209 case 0xa0: /* push fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004210 rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004211 break;
4212 case 0xa1: /* pop fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004213 rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004214 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004215 case 0xa3:
4216 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08004217 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004218 /* only subword offset */
4219 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02004220 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004221 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004222 case 0xa4: /* shld imm8, r, r/m */
4223 case 0xa5: /* shld cl, r, r/m */
4224 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
4225 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004226 case 0xa8: /* push gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004227 rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004228 break;
4229 case 0xa9: /* pop gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004230 rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004231 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004232 case 0xab:
4233 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004234 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004235 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004236 case 0xac: /* shrd imm8, r, r/m */
4237 case 0xad: /* shrd cl, r, r/m */
4238 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
4239 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004240 case 0xae: /* clflush */
4241 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004242 case 0xb0 ... 0xb1: /* cmpxchg */
4243 /*
4244 * Save real source value, then compare EAX against
4245 * destination.
4246 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004247 c->src.orig_val = c->src.val;
4248 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02004249 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
4250 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004252 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253 } else {
4254 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004255 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03004256 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004257 }
4258 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004259 case 0xb2: /* lss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004260 rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004261 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262 case 0xb3:
4263 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004264 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004265 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004266 case 0xb4: /* lfs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004267 rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004268 break;
4269 case 0xb5: /* lgs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004270 rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004271 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004273 c->dst.bytes = c->op_bytes;
4274 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
4275 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004278 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004279 case 0:
4280 goto bt;
4281 case 1:
4282 goto bts;
4283 case 2:
4284 goto btr;
4285 case 3:
4286 goto btc;
4287 }
4288 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004289 case 0xbb:
4290 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004291 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004292 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004293 case 0xbc: { /* bsf */
4294 u8 zf;
4295 __asm__ ("bsf %2, %0; setz %1"
4296 : "=r"(c->dst.val), "=q"(zf)
4297 : "r"(c->src.val));
4298 ctxt->eflags &= ~X86_EFLAGS_ZF;
4299 if (zf) {
4300 ctxt->eflags |= X86_EFLAGS_ZF;
4301 c->dst.type = OP_NONE; /* Disable writeback. */
4302 }
4303 break;
4304 }
4305 case 0xbd: { /* bsr */
4306 u8 zf;
4307 __asm__ ("bsr %2, %0; setz %1"
4308 : "=r"(c->dst.val), "=q"(zf)
4309 : "r"(c->src.val));
4310 ctxt->eflags &= ~X86_EFLAGS_ZF;
4311 if (zf) {
4312 ctxt->eflags |= X86_EFLAGS_ZF;
4313 c->dst.type = OP_NONE; /* Disable writeback. */
4314 }
4315 break;
4316 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004317 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004318 c->dst.bytes = c->op_bytes;
4319 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4320 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004321 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004322 case 0xc0 ... 0xc1: /* xadd */
4323 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4324 /* Write back the register source. */
4325 c->src.val = c->dst.orig_val;
4326 write_register_operand(&c->src);
4327 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004328 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004329 c->dst.bytes = c->op_bytes;
4330 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4331 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004332 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004334 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004335 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004336 default:
4337 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004339
4340 if (rc != X86EMUL_CONTINUE)
4341 goto done;
4342
Avi Kivity6aa8b732006-12-10 02:21:36 -08004343 goto writeback;
4344
4345cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004346 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004347}