blob: 6f08bc940fa8051426ffa67531f548951f6dc7c4 [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,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300416 .rep_prefix = ctxt->rep_prefix,
417 .modrm_mod = ctxt->modrm_mod,
418 .modrm_reg = ctxt->modrm_reg,
419 .modrm_rm = ctxt->modrm_rm,
420 .src_val = ctxt->src.val64,
421 .src_bytes = ctxt->src.bytes,
422 .dst_bytes = ctxt->dst.bytes,
423 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200424 .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
Avi Kivity9dac77f2011-06-01 15:34:25 +0300430static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800431{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300432 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800433}
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
Avi Kivity9dac77f2011-06-01 15:34:25 +0300437address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800438{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300439 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800440 return reg;
441 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300442 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800443}
444
445static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300446register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800447{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300448 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800449}
450
Harvey Harrison7a9572752008-02-19 07:40:41 -0800451static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300452register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800453{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300454 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800455 *reg += inc;
456 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300457 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800458}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459
Avi Kivity9dac77f2011-06-01 15:34:25 +0300460static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800461{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300462 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800463}
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 Kivity9dac77f2011-06-01 15:34:25 +0300472static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300473{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300474 ctxt->has_seg_override = true;
475 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300476}
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 Kivity9dac77f2011-06-01 15:34:25 +0300486static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300487{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300488 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300489 return 0;
490
Avi Kivity9dac77f2011-06-01 15:34:25 +0300491 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300492}
493
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200494static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
495 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300496{
Avi Kivityda9cb572010-11-22 17:53:21 +0200497 ctxt->exception.vector = vec;
498 ctxt->exception.error_code = error;
499 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200500 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300501}
502
Joerg Roedel3b88e412011-04-04 12:39:29 +0200503static int emulate_db(struct x86_emulate_ctxt *ctxt)
504{
505 return emulate_exception(ctxt, DB_VECTOR, 0, false);
506}
507
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200508static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300509{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200510 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300511}
512
Avi Kivity618ff152011-04-03 12:32:09 +0300513static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
514{
515 return emulate_exception(ctxt, SS_VECTOR, err, true);
516}
517
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200518static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300519{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200520 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300521}
522
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200523static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300524{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300526}
527
Avi Kivity34d1f492010-08-26 11:59:01 +0300528static int emulate_de(struct x86_emulate_ctxt *ctxt)
529{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200530 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300531}
532
Avi Kivity12537912011-03-29 11:41:27 +0200533static int emulate_nm(struct x86_emulate_ctxt *ctxt)
534{
535 return emulate_exception(ctxt, NM_VECTOR, 0, false);
536}
537
Avi Kivity1aa36612011-04-27 13:20:30 +0300538static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
539{
540 u16 selector;
541 struct desc_struct desc;
542
543 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
544 return selector;
545}
546
547static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
548 unsigned seg)
549{
550 u16 dummy;
551 u32 base3;
552 struct desc_struct desc;
553
554 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
555 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
556}
557
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400558static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300559 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400560 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300561 ulong *linear)
562{
Avi Kivity618ff152011-04-03 12:32:09 +0300563 struct desc_struct desc;
564 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300565 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300566 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300567 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300568 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300569
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900570 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300571 switch (ctxt->mode) {
572 case X86EMUL_MODE_REAL:
573 break;
574 case X86EMUL_MODE_PROT64:
575 if (((signed long)la << 16) >> 16 != la)
576 return emulate_gp(ctxt, 0);
577 break;
578 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300579 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
580 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300581 if (!usable)
582 goto bad;
583 /* code segment or read-only data segment */
584 if (((desc.type & 8) || !(desc.type & 2)) && write)
585 goto bad;
586 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400587 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300588 goto bad;
589 lim = desc_limit_scaled(&desc);
590 if ((desc.type & 8) || !(desc.type & 4)) {
591 /* expand-up segment */
592 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
593 goto bad;
594 } else {
595 /* exapand-down segment */
596 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
597 goto bad;
598 lim = desc.d ? 0xffffffff : 0xffff;
599 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
600 goto bad;
601 }
Avi Kivity717746e2011-04-20 13:37:53 +0300602 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300603 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300604 cpl = max(cpl, rpl);
605 if (!(desc.type & 8)) {
606 /* data segment */
607 if (cpl > desc.dpl)
608 goto bad;
609 } else if ((desc.type & 8) && !(desc.type & 4)) {
610 /* nonconforming code segment */
611 if (cpl != desc.dpl)
612 goto bad;
613 } else if ((desc.type & 8) && (desc.type & 4)) {
614 /* conforming code segment */
615 if (cpl < desc.dpl)
616 goto bad;
617 }
618 break;
619 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300620 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300621 la &= (u32)-1;
622 *linear = la;
623 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300624bad:
625 if (addr.seg == VCPU_SREG_SS)
626 return emulate_ss(ctxt, addr.seg);
627 else
628 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300629}
630
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400631static int linearize(struct x86_emulate_ctxt *ctxt,
632 struct segmented_address addr,
633 unsigned size, bool write,
634 ulong *linear)
635{
636 return __linearize(ctxt, addr, size, write, false, linear);
637}
638
639
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200640static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
641 struct segmented_address addr,
642 void *data,
643 unsigned size)
644{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200645 int rc;
646 ulong linear;
647
Avi Kivity83b87952011-04-03 11:31:19 +0300648 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200649 if (rc != X86EMUL_CONTINUE)
650 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300651 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200652}
653
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900654static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300655 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200656{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300657 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200658 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300659 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200660
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300661 if (eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400662 unsigned long linear;
663 struct segmented_address addr = { .seg=VCPU_SREG_CS, .ea=eip};
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300664 cur_size = fc->end - fc->start;
665 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400666 rc = __linearize(ctxt, addr, size, false, true, &linear);
667 if (rc != X86EMUL_CONTINUE)
668 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900669 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
670 size, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900671 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200672 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300673 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200674 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300675 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900676 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200677}
678
679static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Avi Kivity62266862007-11-20 13:15:52 +0200680 unsigned long eip, void *dest, unsigned size)
681{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900682 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200683
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200684 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200685 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200686 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200687 while (size--) {
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900688 rc = do_insn_fetch_byte(ctxt, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900689 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200690 return rc;
691 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900692 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200693}
694
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900695/* Fetch next part of the instruction being emulated. */
696#define insn_fetch(_type, _size, _eip) \
697({ unsigned long _x; \
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900698 rc = do_insn_fetch(ctxt, (_eip), &_x, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900699 if (rc != X86EMUL_CONTINUE) \
700 goto done; \
701 (_eip) += (_size); \
702 (_type)_x; \
703})
704
705#define insn_fetch_arr(_arr, _size, _eip) \
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900706({ rc = do_insn_fetch(ctxt, (_eip), _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900707 if (rc != X86EMUL_CONTINUE) \
708 goto done; \
709 (_eip) += (_size); \
710})
711
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000712/*
713 * Given the 'reg' portion of a ModRM byte, and a register block, return a
714 * pointer into the block that addresses the relevant register.
715 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
716 */
717static void *decode_register(u8 modrm_reg, unsigned long *regs,
718 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719{
720 void *p;
721
722 p = &regs[modrm_reg];
723 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
724 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
725 return p;
726}
727
728static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200729 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 u16 *size, unsigned long *address, int op_bytes)
731{
732 int rc;
733
734 if (op_bytes == 2)
735 op_bytes = 3;
736 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200737 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900738 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200740 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200741 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 return rc;
743}
744
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300745static int test_cc(unsigned int condition, unsigned int flags)
746{
747 int rc = 0;
748
749 switch ((condition & 15) >> 1) {
750 case 0: /* o */
751 rc |= (flags & EFLG_OF);
752 break;
753 case 1: /* b/c/nae */
754 rc |= (flags & EFLG_CF);
755 break;
756 case 2: /* z/e */
757 rc |= (flags & EFLG_ZF);
758 break;
759 case 3: /* be/na */
760 rc |= (flags & (EFLG_CF|EFLG_ZF));
761 break;
762 case 4: /* s */
763 rc |= (flags & EFLG_SF);
764 break;
765 case 5: /* p/pe */
766 rc |= (flags & EFLG_PF);
767 break;
768 case 7: /* le/ng */
769 rc |= (flags & EFLG_ZF);
770 /* fall through */
771 case 6: /* l/nge */
772 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
773 break;
774 }
775
776 /* Odd condition identifiers (lsb == 1) have inverted sense. */
777 return (!!rc ^ (condition & 1));
778}
779
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300780static void fetch_register_operand(struct operand *op)
781{
782 switch (op->bytes) {
783 case 1:
784 op->val = *(u8 *)op->addr.reg;
785 break;
786 case 2:
787 op->val = *(u16 *)op->addr.reg;
788 break;
789 case 4:
790 op->val = *(u32 *)op->addr.reg;
791 break;
792 case 8:
793 op->val = *(u64 *)op->addr.reg;
794 break;
795 }
796}
797
Avi Kivity12537912011-03-29 11:41:27 +0200798static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
799{
800 ctxt->ops->get_fpu(ctxt);
801 switch (reg) {
802 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
803 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
804 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
805 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
806 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
807 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
808 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
809 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
810#ifdef CONFIG_X86_64
811 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
812 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
813 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
814 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
815 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
816 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
817 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
818 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
819#endif
820 default: BUG();
821 }
822 ctxt->ops->put_fpu(ctxt);
823}
824
825static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
826 int reg)
827{
828 ctxt->ops->get_fpu(ctxt);
829 switch (reg) {
830 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
831 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
832 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
833 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
834 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
835 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
836 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
837 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
838#ifdef CONFIG_X86_64
839 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
840 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
841 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
842 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
843 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
844 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
845 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
846 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
847#endif
848 default: BUG();
849 }
850 ctxt->ops->put_fpu(ctxt);
851}
852
853static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
854 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200855 int inhibit_bytereg)
856{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300857 unsigned reg = ctxt->modrm_reg;
858 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200859
Avi Kivity9dac77f2011-06-01 15:34:25 +0300860 if (!(ctxt->d & ModRM))
861 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200862
Avi Kivity9dac77f2011-06-01 15:34:25 +0300863 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200864 op->type = OP_XMM;
865 op->bytes = 16;
866 op->addr.xmm = reg;
867 read_sse_reg(ctxt, &op->vec_val, reg);
868 return;
869 }
870
Avi Kivity3c118e22007-10-31 10:27:04 +0200871 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300872 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
873 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200874 op->bytes = 1;
875 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300876 op->addr.reg = decode_register(reg, ctxt->regs, 0);
877 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200878 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300879 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200880 op->orig_val = op->val;
881}
882
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200883static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300884 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200885{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200886 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700887 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900888 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300889 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890
Avi Kivity9dac77f2011-06-01 15:34:25 +0300891 if (ctxt->rex_prefix) {
892 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
893 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
894 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200895 }
896
Avi Kivity9dac77f2011-06-01 15:34:25 +0300897 ctxt->modrm = insn_fetch(u8, 1, ctxt->_eip);
898 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
899 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
900 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
901 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902
Avi Kivity9dac77f2011-06-01 15:34:25 +0300903 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300904 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300905 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
906 op->addr.reg = decode_register(ctxt->modrm_rm,
907 ctxt->regs, ctxt->d & ByteOp);
908 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200909 op->type = OP_XMM;
910 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300911 op->addr.xmm = ctxt->modrm_rm;
912 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200913 return rc;
914 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300915 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200916 return rc;
917 }
918
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300919 op->type = OP_MEM;
920
Avi Kivity9dac77f2011-06-01 15:34:25 +0300921 if (ctxt->ad_bytes == 2) {
922 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
923 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
924 unsigned si = ctxt->regs[VCPU_REGS_RSI];
925 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200926
927 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300928 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200929 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300930 if (ctxt->modrm_rm == 6)
931 modrm_ea += insn_fetch(u16, 2, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932 break;
933 case 1:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300934 modrm_ea += insn_fetch(s8, 1, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 break;
936 case 2:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300937 modrm_ea += insn_fetch(u16, 2, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200938 break;
939 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300940 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200941 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300942 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200943 break;
944 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300945 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200946 break;
947 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300948 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200949 break;
950 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300951 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200952 break;
953 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300954 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200955 break;
956 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300957 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200958 break;
959 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300960 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300961 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200962 break;
963 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300964 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200965 break;
966 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300967 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
968 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
969 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300970 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200971 } else {
972 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300973 if ((ctxt->modrm_rm & 7) == 4) {
974 sib = insn_fetch(u8, 1, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200975 index_reg |= (sib >> 3) & 7;
976 base_reg |= sib & 7;
977 scale = sib >> 6;
978
Avi Kivity9dac77f2011-06-01 15:34:25 +0300979 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
980 modrm_ea += insn_fetch(s32, 4, ctxt->_eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700981 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300982 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700983 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300984 modrm_ea += ctxt->regs[index_reg] << scale;
985 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700986 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300987 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700988 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300989 modrm_ea += ctxt->regs[ctxt->modrm_rm];
990 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200991 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300992 if (ctxt->modrm_rm == 5)
993 modrm_ea += insn_fetch(s32, 4, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200994 break;
995 case 1:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300996 modrm_ea += insn_fetch(s8, 1, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200997 break;
998 case 2:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300999 modrm_ea += insn_fetch(s32, 4, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001000 break;
1001 }
1002 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001003 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004done:
1005 return rc;
1006}
1007
1008static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001009 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001010{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001011 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001012
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001013 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001014 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001015 case 2:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001016 op->addr.mem.ea = insn_fetch(u16, 2, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001017 break;
1018 case 4:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001019 op->addr.mem.ea = insn_fetch(u32, 4, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001020 break;
1021 case 8:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001022 op->addr.mem.ea = insn_fetch(u64, 8, ctxt->_eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001023 break;
1024 }
1025done:
1026 return rc;
1027}
1028
Avi Kivity9dac77f2011-06-01 15:34:25 +03001029static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001030{
Sheng Yang7129eec2010-09-28 16:33:32 +08001031 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001032
Avi Kivity9dac77f2011-06-01 15:34:25 +03001033 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1034 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001035
Avi Kivity9dac77f2011-06-01 15:34:25 +03001036 if (ctxt->src.bytes == 2)
1037 sv = (s16)ctxt->src.val & (s16)mask;
1038 else if (ctxt->src.bytes == 4)
1039 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001040
Avi Kivity9dac77f2011-06-01 15:34:25 +03001041 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001042 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001043
1044 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001045 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001046}
1047
Gleb Natapov9de41572010-04-28 19:15:22 +03001048static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001049 unsigned long addr, void *dest, unsigned size)
1050{
1051 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001052 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001053
1054 while (size) {
1055 int n = min(size, 8u);
1056 size -= n;
1057 if (mc->pos < mc->end)
1058 goto read_cached;
1059
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001060 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1061 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001062 if (rc != X86EMUL_CONTINUE)
1063 return rc;
1064 mc->end += n;
1065
1066 read_cached:
1067 memcpy(dest, mc->data + mc->pos, n);
1068 mc->pos += n;
1069 dest += n;
1070 addr += n;
1071 }
1072 return X86EMUL_CONTINUE;
1073}
1074
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001075static int segmented_read(struct x86_emulate_ctxt *ctxt,
1076 struct segmented_address addr,
1077 void *data,
1078 unsigned size)
1079{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001080 int rc;
1081 ulong linear;
1082
Avi Kivity83b87952011-04-03 11:31:19 +03001083 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001084 if (rc != X86EMUL_CONTINUE)
1085 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001086 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001087}
1088
1089static int segmented_write(struct x86_emulate_ctxt *ctxt,
1090 struct segmented_address addr,
1091 const void *data,
1092 unsigned size)
1093{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001094 int rc;
1095 ulong linear;
1096
Avi Kivity83b87952011-04-03 11:31:19 +03001097 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001098 if (rc != X86EMUL_CONTINUE)
1099 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001100 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1101 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001102}
1103
1104static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1105 struct segmented_address addr,
1106 const void *orig_data, const void *data,
1107 unsigned size)
1108{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001109 int rc;
1110 ulong linear;
1111
Avi Kivity83b87952011-04-03 11:31:19 +03001112 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001113 if (rc != X86EMUL_CONTINUE)
1114 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001115 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1116 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001117}
1118
Gleb Natapov7b262e92010-03-18 15:20:27 +02001119static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001120 unsigned int size, unsigned short port,
1121 void *dest)
1122{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001123 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001124
1125 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001126 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001127 unsigned int count = ctxt->rep_prefix ?
1128 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001129 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001130 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1131 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001132 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1133 count);
1134 if (n == 0)
1135 n = 1;
1136 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001137 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001138 return 0;
1139 rc->end = n * size;
1140 }
1141
1142 memcpy(dest, rc->data + rc->pos, size);
1143 rc->pos += size;
1144 return 1;
1145}
1146
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001147static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001148 u16 selector, struct desc_ptr *dt)
1149{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001150 struct x86_emulate_ops *ops = ctxt->ops;
1151
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001152 if (selector & 1 << 2) {
1153 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001154 u16 sel;
1155
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001156 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001157 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001158 return;
1159
1160 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1161 dt->address = get_desc_base(&desc);
1162 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001163 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001164}
1165
1166/* allowed just for 8 bytes segments */
1167static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001168 u16 selector, struct desc_struct *desc)
1169{
1170 struct desc_ptr dt;
1171 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001172 ulong addr;
1173
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001174 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001175
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001176 if (dt.size < index * 8 + 7)
1177 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001178
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001179 addr = dt.address + index * 8;
1180 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1181 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001182}
1183
1184/* allowed just for 8 bytes segments */
1185static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001186 u16 selector, struct desc_struct *desc)
1187{
1188 struct desc_ptr dt;
1189 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001190 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001191
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001192 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001193
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001194 if (dt.size < index * 8 + 7)
1195 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001196
1197 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001198 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1199 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001200}
1201
Gleb Natapov5601d052011-03-07 14:55:06 +02001202/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001203static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001204 u16 selector, int seg)
1205{
1206 struct desc_struct seg_desc;
1207 u8 dpl, rpl, cpl;
1208 unsigned err_vec = GP_VECTOR;
1209 u32 err_code = 0;
1210 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1211 int ret;
1212
1213 memset(&seg_desc, 0, sizeof seg_desc);
1214
1215 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1216 || ctxt->mode == X86EMUL_MODE_REAL) {
1217 /* set real mode segment descriptor */
1218 set_desc_base(&seg_desc, selector << 4);
1219 set_desc_limit(&seg_desc, 0xffff);
1220 seg_desc.type = 3;
1221 seg_desc.p = 1;
1222 seg_desc.s = 1;
1223 goto load;
1224 }
1225
1226 /* NULL selector is not valid for TR, CS and SS */
1227 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1228 && null_selector)
1229 goto exception;
1230
1231 /* TR should be in GDT only */
1232 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1233 goto exception;
1234
1235 if (null_selector) /* for NULL selector skip all following checks */
1236 goto load;
1237
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001238 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001239 if (ret != X86EMUL_CONTINUE)
1240 return ret;
1241
1242 err_code = selector & 0xfffc;
1243 err_vec = GP_VECTOR;
1244
1245 /* can't load system descriptor into segment selecor */
1246 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1247 goto exception;
1248
1249 if (!seg_desc.p) {
1250 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1251 goto exception;
1252 }
1253
1254 rpl = selector & 3;
1255 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001256 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001257
1258 switch (seg) {
1259 case VCPU_SREG_SS:
1260 /*
1261 * segment is not a writable data segment or segment
1262 * selector's RPL != CPL or segment selector's RPL != CPL
1263 */
1264 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1265 goto exception;
1266 break;
1267 case VCPU_SREG_CS:
1268 if (!(seg_desc.type & 8))
1269 goto exception;
1270
1271 if (seg_desc.type & 4) {
1272 /* conforming */
1273 if (dpl > cpl)
1274 goto exception;
1275 } else {
1276 /* nonconforming */
1277 if (rpl > cpl || dpl != cpl)
1278 goto exception;
1279 }
1280 /* CS(RPL) <- CPL */
1281 selector = (selector & 0xfffc) | cpl;
1282 break;
1283 case VCPU_SREG_TR:
1284 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1285 goto exception;
1286 break;
1287 case VCPU_SREG_LDTR:
1288 if (seg_desc.s || seg_desc.type != 2)
1289 goto exception;
1290 break;
1291 default: /* DS, ES, FS, or GS */
1292 /*
1293 * segment is not a data or readable code segment or
1294 * ((segment is a data or nonconforming code segment)
1295 * and (both RPL and CPL > DPL))
1296 */
1297 if ((seg_desc.type & 0xa) == 0x8 ||
1298 (((seg_desc.type & 0xc) != 0xc) &&
1299 (rpl > dpl && cpl > dpl)))
1300 goto exception;
1301 break;
1302 }
1303
1304 if (seg_desc.s) {
1305 /* mark segment as accessed */
1306 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001307 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001308 if (ret != X86EMUL_CONTINUE)
1309 return ret;
1310 }
1311load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001312 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001313 return X86EMUL_CONTINUE;
1314exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001315 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001316 return X86EMUL_PROPAGATE_FAULT;
1317}
1318
Wei Yongjun31be40b2010-08-17 09:17:30 +08001319static void write_register_operand(struct operand *op)
1320{
1321 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1322 switch (op->bytes) {
1323 case 1:
1324 *(u8 *)op->addr.reg = (u8)op->val;
1325 break;
1326 case 2:
1327 *(u16 *)op->addr.reg = (u16)op->val;
1328 break;
1329 case 4:
1330 *op->addr.reg = (u32)op->val;
1331 break; /* 64b: zero-extend */
1332 case 8:
1333 *op->addr.reg = op->val;
1334 break;
1335 }
1336}
1337
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001338static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001339{
1340 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001341
Avi Kivity9dac77f2011-06-01 15:34:25 +03001342 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001343 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001344 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001345 break;
1346 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001347 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001348 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001349 ctxt->dst.addr.mem,
1350 &ctxt->dst.orig_val,
1351 &ctxt->dst.val,
1352 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001353 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001354 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001355 ctxt->dst.addr.mem,
1356 &ctxt->dst.val,
1357 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001358 if (rc != X86EMUL_CONTINUE)
1359 return rc;
1360 break;
Avi Kivity12537912011-03-29 11:41:27 +02001361 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001362 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001363 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001364 case OP_NONE:
1365 /* no writeback */
1366 break;
1367 default:
1368 break;
1369 }
1370 return X86EMUL_CONTINUE;
1371}
1372
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001373static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001374{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001375 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001376
Avi Kivity9dac77f2011-06-01 15:34:25 +03001377 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1378 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001379 addr.seg = VCPU_SREG_SS;
1380
1381 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001382 ctxt->dst.type = OP_NONE;
1383 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001384}
1385
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001386static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001387 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001388{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001389 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001390 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001391
Avi Kivity9dac77f2011-06-01 15:34:25 +03001392 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001393 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001394 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001395 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396 return rc;
1397
Avi Kivity9dac77f2011-06-01 15:34:25 +03001398 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001399 return rc;
1400}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001401
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001402static int em_pop(struct x86_emulate_ctxt *ctxt)
1403{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001404 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001405}
1406
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001407static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001408 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001409{
1410 int rc;
1411 unsigned long val, change_mask;
1412 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001413 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001414
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001415 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001416 if (rc != X86EMUL_CONTINUE)
1417 return rc;
1418
1419 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1420 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1421
1422 switch(ctxt->mode) {
1423 case X86EMUL_MODE_PROT64:
1424 case X86EMUL_MODE_PROT32:
1425 case X86EMUL_MODE_PROT16:
1426 if (cpl == 0)
1427 change_mask |= EFLG_IOPL;
1428 if (cpl <= iopl)
1429 change_mask |= EFLG_IF;
1430 break;
1431 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001432 if (iopl < 3)
1433 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001434 change_mask |= EFLG_IF;
1435 break;
1436 default: /* real mode */
1437 change_mask |= (EFLG_IOPL | EFLG_IF);
1438 break;
1439 }
1440
1441 *(unsigned long *)dest =
1442 (ctxt->eflags & ~change_mask) | (val & change_mask);
1443
1444 return rc;
1445}
1446
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001447static int em_popf(struct x86_emulate_ctxt *ctxt)
1448{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001449 ctxt->dst.type = OP_REG;
1450 ctxt->dst.addr.reg = &ctxt->eflags;
1451 ctxt->dst.bytes = ctxt->op_bytes;
1452 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001453}
1454
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001455static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001456{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001457 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001458
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001459 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001460}
1461
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001462static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001463{
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001464 unsigned long selector;
1465 int rc;
1466
Avi Kivity9dac77f2011-06-01 15:34:25 +03001467 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001468 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001469 return rc;
1470
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001471 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001472 return rc;
1473}
1474
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001475static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001476{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001477 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001478 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001479 int reg = VCPU_REGS_RAX;
1480
1481 while (reg <= VCPU_REGS_RDI) {
1482 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001483 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001484
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001485 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001486 if (rc != X86EMUL_CONTINUE)
1487 return rc;
1488
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001489 ++reg;
1490 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001491
Wei Yongjunc37eda12010-06-15 09:03:33 +08001492 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001493}
1494
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001495static int em_pushf(struct x86_emulate_ctxt *ctxt)
1496{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001497 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001498 return em_push(ctxt);
1499}
1500
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001501static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001502{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001503 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001504 int reg = VCPU_REGS_RDI;
1505
1506 while (reg >= VCPU_REGS_RAX) {
1507 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001508 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1509 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001510 --reg;
1511 }
1512
Avi Kivity9dac77f2011-06-01 15:34:25 +03001513 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001514 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001515 break;
1516 --reg;
1517 }
1518 return rc;
1519}
1520
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001521int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001522{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001523 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001524 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001525 struct desc_ptr dt;
1526 gva_t cs_addr;
1527 gva_t eip_addr;
1528 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001529
1530 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001531 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001532 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001533 if (rc != X86EMUL_CONTINUE)
1534 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001535
1536 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1537
Avi Kivity9dac77f2011-06-01 15:34:25 +03001538 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001539 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001540 if (rc != X86EMUL_CONTINUE)
1541 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001542
Avi Kivity9dac77f2011-06-01 15:34:25 +03001543 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001544 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001545 if (rc != X86EMUL_CONTINUE)
1546 return rc;
1547
Avi Kivity4bff1e862011-04-20 13:37:53 +03001548 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001549
1550 eip_addr = dt.address + (irq << 2);
1551 cs_addr = dt.address + (irq << 2) + 2;
1552
Avi Kivity0f65dd72011-04-20 13:37:53 +03001553 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001554 if (rc != X86EMUL_CONTINUE)
1555 return rc;
1556
Avi Kivity0f65dd72011-04-20 13:37:53 +03001557 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001558 if (rc != X86EMUL_CONTINUE)
1559 return rc;
1560
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001561 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001562 if (rc != X86EMUL_CONTINUE)
1563 return rc;
1564
Avi Kivity9dac77f2011-06-01 15:34:25 +03001565 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001566
1567 return rc;
1568}
1569
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001570static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001571{
1572 switch(ctxt->mode) {
1573 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001574 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001575 case X86EMUL_MODE_VM86:
1576 case X86EMUL_MODE_PROT16:
1577 case X86EMUL_MODE_PROT32:
1578 case X86EMUL_MODE_PROT64:
1579 default:
1580 /* Protected mode interrupts unimplemented yet */
1581 return X86EMUL_UNHANDLEABLE;
1582 }
1583}
1584
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001585static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001586{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001587 int rc = X86EMUL_CONTINUE;
1588 unsigned long temp_eip = 0;
1589 unsigned long temp_eflags = 0;
1590 unsigned long cs = 0;
1591 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1592 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1593 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1594 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1595
1596 /* TODO: Add stack limit check */
1597
Avi Kivity9dac77f2011-06-01 15:34:25 +03001598 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001599
1600 if (rc != X86EMUL_CONTINUE)
1601 return rc;
1602
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001603 if (temp_eip & ~0xffff)
1604 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001605
Avi Kivity9dac77f2011-06-01 15:34:25 +03001606 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001607
1608 if (rc != X86EMUL_CONTINUE)
1609 return rc;
1610
Avi Kivity9dac77f2011-06-01 15:34:25 +03001611 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001612
1613 if (rc != X86EMUL_CONTINUE)
1614 return rc;
1615
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001616 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001617
1618 if (rc != X86EMUL_CONTINUE)
1619 return rc;
1620
Avi Kivity9dac77f2011-06-01 15:34:25 +03001621 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001622
1623
Avi Kivity9dac77f2011-06-01 15:34:25 +03001624 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001625 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001626 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001627 ctxt->eflags &= ~0xffff;
1628 ctxt->eflags |= temp_eflags;
1629 }
1630
1631 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1632 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1633
1634 return rc;
1635}
1636
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001637static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001638{
1639 switch(ctxt->mode) {
1640 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001641 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001642 case X86EMUL_MODE_VM86:
1643 case X86EMUL_MODE_PROT16:
1644 case X86EMUL_MODE_PROT32:
1645 case X86EMUL_MODE_PROT64:
1646 default:
1647 /* iret from protected mode unimplemented yet */
1648 return X86EMUL_UNHANDLEABLE;
1649 }
1650}
1651
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001652static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1653{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001654 int rc;
1655 unsigned short sel;
1656
Avi Kivity9dac77f2011-06-01 15:34:25 +03001657 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001658
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001659 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001660 if (rc != X86EMUL_CONTINUE)
1661 return rc;
1662
Avi Kivity9dac77f2011-06-01 15:34:25 +03001663 ctxt->_eip = 0;
1664 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001665 return X86EMUL_CONTINUE;
1666}
1667
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001668static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001669{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001670 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001671}
1672
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001673static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001674{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001675 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001676 case 0: /* rol */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001677 emulate_2op_SrcB("rol", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001678 break;
1679 case 1: /* ror */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001680 emulate_2op_SrcB("ror", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681 break;
1682 case 2: /* rcl */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001683 emulate_2op_SrcB("rcl", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001684 break;
1685 case 3: /* rcr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001686 emulate_2op_SrcB("rcr", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001687 break;
1688 case 4: /* sal/shl */
1689 case 6: /* sal/shl */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001690 emulate_2op_SrcB("sal", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001691 break;
1692 case 5: /* shr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001693 emulate_2op_SrcB("shr", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001694 break;
1695 case 7: /* sar */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001696 emulate_2op_SrcB("sar", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001697 break;
1698 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001699 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001700}
1701
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001702static int em_grp3(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001703{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001704 unsigned long *rax = &ctxt->regs[VCPU_REGS_RAX];
1705 unsigned long *rdx = &ctxt->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001706 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001707
Avi Kivity9dac77f2011-06-01 15:34:25 +03001708 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001709 case 0 ... 1: /* test */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001710 emulate_2op_SrcV("test", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001711 break;
1712 case 2: /* not */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001713 ctxt->dst.val = ~ctxt->dst.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714 break;
1715 case 3: /* neg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001716 emulate_1op("neg", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001718 case 4: /* mul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001719 emulate_1op_rax_rdx("mul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001720 break;
1721 case 5: /* imul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001722 emulate_1op_rax_rdx("imul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001723 break;
1724 case 6: /* div */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001725 emulate_1op_rax_rdx_ex("div", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001726 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001727 break;
1728 case 7: /* idiv */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001729 emulate_1op_rax_rdx_ex("idiv", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001730 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001731 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001732 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001733 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001734 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001735 if (de)
1736 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001737 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001738}
1739
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001740static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001741{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001742 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743
Avi Kivity9dac77f2011-06-01 15:34:25 +03001744 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745 case 0: /* inc */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001746 emulate_1op("inc", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747 break;
1748 case 1: /* dec */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001749 emulate_1op("dec", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001750 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001751 case 2: /* call near abs */ {
1752 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001753 old_eip = ctxt->_eip;
1754 ctxt->_eip = ctxt->src.val;
1755 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001756 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001757 break;
1758 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001759 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001760 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001761 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001762 case 5: /* jmp far */
1763 rc = em_jmp_far(ctxt);
1764 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001765 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001766 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001767 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001768 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001769 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770}
1771
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001772static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001773{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001774 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775
Avi Kivity9dac77f2011-06-01 15:34:25 +03001776 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1777 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1778 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1779 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001780 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001781 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001782 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1783 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001784
Laurent Vivier05f086f2007-09-24 11:10:55 +02001785 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001786 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001787 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788}
1789
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001790static int em_ret(struct x86_emulate_ctxt *ctxt)
1791{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001792 ctxt->dst.type = OP_REG;
1793 ctxt->dst.addr.reg = &ctxt->_eip;
1794 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001795 return em_pop(ctxt);
1796}
1797
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001798static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001799{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001800 int rc;
1801 unsigned long cs;
1802
Avi Kivity9dac77f2011-06-01 15:34:25 +03001803 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001804 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001805 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001806 if (ctxt->op_bytes == 4)
1807 ctxt->_eip = (u32)ctxt->_eip;
1808 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001809 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001810 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001811 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001812 return rc;
1813}
1814
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001815static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001816{
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001817 unsigned short sel;
1818 int rc;
1819
Avi Kivity9dac77f2011-06-01 15:34:25 +03001820 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001821
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001822 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001823 if (rc != X86EMUL_CONTINUE)
1824 return rc;
1825
Avi Kivity9dac77f2011-06-01 15:34:25 +03001826 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001827 return rc;
1828}
1829
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001830static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001831setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001832 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001833{
Avi Kivity1aa36612011-04-27 13:20:30 +03001834 u16 selector;
1835
Gleb Natapov79168fd2010-04-28 19:15:30 +03001836 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001837 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001838 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001839
1840 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001841 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001842 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001844 cs->type = 0x0b; /* Read, Execute, Accessed */
1845 cs->s = 1;
1846 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001847 cs->p = 1;
1848 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001849
Gleb Natapov79168fd2010-04-28 19:15:30 +03001850 set_desc_base(ss, 0); /* flat segment */
1851 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001852 ss->g = 1; /* 4kb granularity */
1853 ss->s = 1;
1854 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001855 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001856 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001857 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001858}
1859
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001860static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001861{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001862 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001863 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001864 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001865 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001866 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001867
1868 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001869 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001870 ctxt->mode == X86EMUL_MODE_VM86)
1871 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001872
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001873 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001874 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001875
Avi Kivity717746e2011-04-20 13:37:53 +03001876 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001878 cs_sel = (u16)(msr_data & 0xfffc);
1879 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001880
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001881 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001882 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001883 cs.l = 1;
1884 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001885 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1886 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001887
Avi Kivity9dac77f2011-06-01 15:34:25 +03001888 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001889 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001890#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001891 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001892
Avi Kivity717746e2011-04-20 13:37:53 +03001893 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001894 ctxt->mode == X86EMUL_MODE_PROT64 ?
1895 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001896 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001897
Avi Kivity717746e2011-04-20 13:37:53 +03001898 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001899 ctxt->eflags &= ~(msr_data | EFLG_RF);
1900#endif
1901 } else {
1902 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001903 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001904 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001905
1906 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1907 }
1908
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001909 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001910}
1911
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001912static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001913{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001914 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001915 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001916 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001917 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001918 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001919
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001920 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001921 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001922 if (ctxt->mode == X86EMUL_MODE_REAL)
1923 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001924
1925 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1926 * Therefore, we inject an #UD.
1927 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001928 if (ctxt->mode == X86EMUL_MODE_PROT64)
1929 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001930
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001931 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001932
Avi Kivity717746e2011-04-20 13:37:53 +03001933 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001934 switch (ctxt->mode) {
1935 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001936 if ((msr_data & 0xfffc) == 0x0)
1937 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001938 break;
1939 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001940 if (msr_data == 0x0)
1941 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001942 break;
1943 }
1944
1945 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001946 cs_sel = (u16)msr_data;
1947 cs_sel &= ~SELECTOR_RPL_MASK;
1948 ss_sel = cs_sel + 8;
1949 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001950 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001951 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001952 cs.l = 1;
1953 }
1954
Avi Kivity1aa36612011-04-27 13:20:30 +03001955 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1956 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001957
Avi Kivity717746e2011-04-20 13:37:53 +03001958 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001959 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001960
Avi Kivity717746e2011-04-20 13:37:53 +03001961 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001962 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001963
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001964 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001965}
1966
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001967static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001968{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001969 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001970 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001971 u64 msr_data;
1972 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001973 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001974
Gleb Natapova0044752010-02-10 14:21:31 +02001975 /* inject #GP if in real mode or Virtual 8086 mode */
1976 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001977 ctxt->mode == X86EMUL_MODE_VM86)
1978 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001979
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001980 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001981
Avi Kivity9dac77f2011-06-01 15:34:25 +03001982 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02001983 usermode = X86EMUL_MODE_PROT64;
1984 else
1985 usermode = X86EMUL_MODE_PROT32;
1986
1987 cs.dpl = 3;
1988 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03001989 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001990 switch (usermode) {
1991 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001992 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001993 if ((msr_data & 0xfffc) == 0x0)
1994 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001995 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001996 break;
1997 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001998 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001999 if (msr_data == 0x0)
2000 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002001 ss_sel = cs_sel + 8;
2002 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002003 cs.l = 1;
2004 break;
2005 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002006 cs_sel |= SELECTOR_RPL_MASK;
2007 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002008
Avi Kivity1aa36612011-04-27 13:20:30 +03002009 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2010 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002011
Avi Kivity9dac77f2011-06-01 15:34:25 +03002012 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2013 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002014
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002015 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002016}
2017
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002018static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002019{
2020 int iopl;
2021 if (ctxt->mode == X86EMUL_MODE_REAL)
2022 return false;
2023 if (ctxt->mode == X86EMUL_MODE_VM86)
2024 return true;
2025 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002026 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002027}
2028
2029static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002030 u16 port, u16 len)
2031{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002032 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002033 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002034 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002035 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002036 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002037 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002038 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002039
Avi Kivity1aa36612011-04-27 13:20:30 +03002040 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002041 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002042 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002043 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002044 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002045 base = get_desc_base(&tr_seg);
2046#ifdef CONFIG_X86_64
2047 base |= ((u64)base3) << 32;
2048#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002049 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002050 if (r != X86EMUL_CONTINUE)
2051 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002052 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002053 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002054 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002055 if (r != X86EMUL_CONTINUE)
2056 return false;
2057 if ((perm >> bit_idx) & mask)
2058 return false;
2059 return true;
2060}
2061
2062static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002063 u16 port, u16 len)
2064{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002065 if (ctxt->perm_ok)
2066 return true;
2067
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002068 if (emulator_bad_iopl(ctxt))
2069 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002070 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002071
2072 ctxt->perm_ok = true;
2073
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002074 return true;
2075}
2076
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002077static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002078 struct tss_segment_16 *tss)
2079{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002080 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002081 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002082 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2083 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2084 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2085 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2086 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2087 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2088 tss->si = ctxt->regs[VCPU_REGS_RSI];
2089 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002090
Avi Kivity1aa36612011-04-27 13:20:30 +03002091 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2092 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2093 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2094 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2095 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002096}
2097
2098static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002099 struct tss_segment_16 *tss)
2100{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002101 int ret;
2102
Avi Kivity9dac77f2011-06-01 15:34:25 +03002103 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002104 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002105 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2106 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2107 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2108 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2109 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2110 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2111 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2112 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002113
2114 /*
2115 * SDM says that segment selectors are loaded before segment
2116 * descriptors
2117 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002118 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2119 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2120 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2121 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2122 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002123
2124 /*
2125 * Now load segment descriptors. If fault happenes at this stage
2126 * it is handled in a context of new task
2127 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002128 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002129 if (ret != X86EMUL_CONTINUE)
2130 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002131 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002132 if (ret != X86EMUL_CONTINUE)
2133 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002134 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002135 if (ret != X86EMUL_CONTINUE)
2136 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002137 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002138 if (ret != X86EMUL_CONTINUE)
2139 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002140 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002141 if (ret != X86EMUL_CONTINUE)
2142 return ret;
2143
2144 return X86EMUL_CONTINUE;
2145}
2146
2147static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148 u16 tss_selector, u16 old_tss_sel,
2149 ulong old_tss_base, struct desc_struct *new_desc)
2150{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002151 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 struct tss_segment_16 tss_seg;
2153 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002154 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002155
Avi Kivity0f65dd72011-04-20 13:37:53 +03002156 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002157 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002158 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002159 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002160 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002161
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002162 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002163
Avi Kivity0f65dd72011-04-20 13:37:53 +03002164 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002165 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002166 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002169
Avi Kivity0f65dd72011-04-20 13:37:53 +03002170 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002171 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002172 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002173 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002174 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002175
2176 if (old_tss_sel != 0xffff) {
2177 tss_seg.prev_task_link = old_tss_sel;
2178
Avi Kivity0f65dd72011-04-20 13:37:53 +03002179 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180 &tss_seg.prev_task_link,
2181 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002182 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002183 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002184 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002186 }
2187
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002188 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189}
2190
2191static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002192 struct tss_segment_32 *tss)
2193{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002194 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002195 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002197 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2198 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2199 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2200 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2201 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2202 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2203 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2204 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002205
Avi Kivity1aa36612011-04-27 13:20:30 +03002206 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2207 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2208 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2209 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2210 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2211 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2212 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002213}
2214
2215static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002216 struct tss_segment_32 *tss)
2217{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218 int ret;
2219
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002220 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002221 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002222 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002223 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002224 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2225 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2226 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2227 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2228 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2229 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2230 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2231 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002232
2233 /*
2234 * SDM says that segment selectors are loaded before segment
2235 * descriptors
2236 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002237 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2238 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2239 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2240 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2241 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2242 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2243 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002244
2245 /*
2246 * Now load segment descriptors. If fault happenes at this stage
2247 * it is handled in a context of new task
2248 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002249 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002250 if (ret != X86EMUL_CONTINUE)
2251 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002252 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002253 if (ret != X86EMUL_CONTINUE)
2254 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002255 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002256 if (ret != X86EMUL_CONTINUE)
2257 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002258 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002259 if (ret != X86EMUL_CONTINUE)
2260 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002261 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002262 if (ret != X86EMUL_CONTINUE)
2263 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002264 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002265 if (ret != X86EMUL_CONTINUE)
2266 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002267 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002268 if (ret != X86EMUL_CONTINUE)
2269 return ret;
2270
2271 return X86EMUL_CONTINUE;
2272}
2273
2274static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002275 u16 tss_selector, u16 old_tss_sel,
2276 ulong old_tss_base, struct desc_struct *new_desc)
2277{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002278 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002279 struct tss_segment_32 tss_seg;
2280 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002281 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282
Avi Kivity0f65dd72011-04-20 13:37:53 +03002283 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002284 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002285 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002287 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002288
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002289 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002290
Avi Kivity0f65dd72011-04-20 13:37:53 +03002291 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002292 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002293 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002295 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002296
Avi Kivity0f65dd72011-04-20 13:37:53 +03002297 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002298 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002299 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002300 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002301 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002302
2303 if (old_tss_sel != 0xffff) {
2304 tss_seg.prev_task_link = old_tss_sel;
2305
Avi Kivity0f65dd72011-04-20 13:37:53 +03002306 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002307 &tss_seg.prev_task_link,
2308 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002309 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002310 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002313 }
2314
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002315 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002316}
2317
2318static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002319 u16 tss_selector, int reason,
2320 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002321{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002322 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002323 struct desc_struct curr_tss_desc, next_tss_desc;
2324 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002325 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002327 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002328 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002329
2330 /* FIXME: old_tss_base == ~0 ? */
2331
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002332 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 if (ret != X86EMUL_CONTINUE)
2334 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002335 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002336 if (ret != X86EMUL_CONTINUE)
2337 return ret;
2338
2339 /* FIXME: check that next_tss_desc is tss */
2340
2341 if (reason != TASK_SWITCH_IRET) {
2342 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002343 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002344 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002345 }
2346
Gleb Natapovceffb452010-03-18 15:20:19 +02002347 desc_limit = desc_limit_scaled(&next_tss_desc);
2348 if (!next_tss_desc.p ||
2349 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2350 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002351 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002352 return X86EMUL_PROPAGATE_FAULT;
2353 }
2354
2355 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2356 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002357 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002358 }
2359
2360 if (reason == TASK_SWITCH_IRET)
2361 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2362
2363 /* set back link to prev task only if NT bit is set in eflags
2364 note that old_tss_sel is not used afetr this point */
2365 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2366 old_tss_sel = 0xffff;
2367
2368 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002369 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002370 old_tss_base, &next_tss_desc);
2371 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002372 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002373 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002374 if (ret != X86EMUL_CONTINUE)
2375 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002376
2377 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2378 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2379
2380 if (reason != TASK_SWITCH_IRET) {
2381 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002382 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002383 }
2384
Avi Kivity717746e2011-04-20 13:37:53 +03002385 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002386 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002387
Jan Kiszkae269fb22010-04-14 15:51:09 +02002388 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002389 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2390 ctxt->lock_prefix = 0;
2391 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002392 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002393 }
2394
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002395 return ret;
2396}
2397
2398int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002399 u16 tss_selector, int reason,
2400 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002401{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402 int rc;
2403
Avi Kivity9dac77f2011-06-01 15:34:25 +03002404 ctxt->_eip = ctxt->eip;
2405 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002407 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002408 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002410 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002411 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002412
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002413 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002414}
2415
Avi Kivity90de84f2010-11-17 15:28:21 +02002416static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002417 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002418{
Gleb Natapova682e352010-03-18 15:20:21 +02002419 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2420
Avi Kivity9dac77f2011-06-01 15:34:25 +03002421 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2422 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002423 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002424}
2425
Avi Kivity7af04fc2010-08-18 14:16:35 +03002426static int em_das(struct x86_emulate_ctxt *ctxt)
2427{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002428 u8 al, old_al;
2429 bool af, cf, old_cf;
2430
2431 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002432 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002433
2434 old_al = al;
2435 old_cf = cf;
2436 cf = false;
2437 af = ctxt->eflags & X86_EFLAGS_AF;
2438 if ((al & 0x0f) > 9 || af) {
2439 al -= 6;
2440 cf = old_cf | (al >= 250);
2441 af = true;
2442 } else {
2443 af = false;
2444 }
2445 if (old_al > 0x99 || old_cf) {
2446 al -= 0x60;
2447 cf = true;
2448 }
2449
Avi Kivity9dac77f2011-06-01 15:34:25 +03002450 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002451 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002452 ctxt->src.type = OP_IMM;
2453 ctxt->src.val = 0;
2454 ctxt->src.bytes = 1;
2455 emulate_2op_SrcV("or", ctxt->src, ctxt->dst, ctxt->eflags);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002456 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2457 if (cf)
2458 ctxt->eflags |= X86_EFLAGS_CF;
2459 if (af)
2460 ctxt->eflags |= X86_EFLAGS_AF;
2461 return X86EMUL_CONTINUE;
2462}
2463
Avi Kivity0ef753b2010-08-18 14:51:45 +03002464static int em_call_far(struct x86_emulate_ctxt *ctxt)
2465{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002466 u16 sel, old_cs;
2467 ulong old_eip;
2468 int rc;
2469
Avi Kivity1aa36612011-04-27 13:20:30 +03002470 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002471 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002472
Avi Kivity9dac77f2011-06-01 15:34:25 +03002473 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002474 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002475 return X86EMUL_CONTINUE;
2476
Avi Kivity9dac77f2011-06-01 15:34:25 +03002477 ctxt->_eip = 0;
2478 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002479
Avi Kivity9dac77f2011-06-01 15:34:25 +03002480 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002481 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002482 if (rc != X86EMUL_CONTINUE)
2483 return rc;
2484
Avi Kivity9dac77f2011-06-01 15:34:25 +03002485 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002486 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002487}
2488
Avi Kivity40ece7c2010-08-18 15:12:09 +03002489static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2490{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002491 int rc;
2492
Avi Kivity9dac77f2011-06-01 15:34:25 +03002493 ctxt->dst.type = OP_REG;
2494 ctxt->dst.addr.reg = &ctxt->_eip;
2495 ctxt->dst.bytes = ctxt->op_bytes;
2496 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002497 if (rc != X86EMUL_CONTINUE)
2498 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002499 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002500 return X86EMUL_CONTINUE;
2501}
2502
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002503static int em_add(struct x86_emulate_ctxt *ctxt)
2504{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002505 emulate_2op_SrcV("add", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002506 return X86EMUL_CONTINUE;
2507}
2508
2509static int em_or(struct x86_emulate_ctxt *ctxt)
2510{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002511 emulate_2op_SrcV("or", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002512 return X86EMUL_CONTINUE;
2513}
2514
2515static int em_adc(struct x86_emulate_ctxt *ctxt)
2516{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002517 emulate_2op_SrcV("adc", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002518 return X86EMUL_CONTINUE;
2519}
2520
2521static int em_sbb(struct x86_emulate_ctxt *ctxt)
2522{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002523 emulate_2op_SrcV("sbb", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002524 return X86EMUL_CONTINUE;
2525}
2526
2527static int em_and(struct x86_emulate_ctxt *ctxt)
2528{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002529 emulate_2op_SrcV("and", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002530 return X86EMUL_CONTINUE;
2531}
2532
2533static int em_sub(struct x86_emulate_ctxt *ctxt)
2534{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002535 emulate_2op_SrcV("sub", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002536 return X86EMUL_CONTINUE;
2537}
2538
2539static int em_xor(struct x86_emulate_ctxt *ctxt)
2540{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002541 emulate_2op_SrcV("xor", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002542 return X86EMUL_CONTINUE;
2543}
2544
2545static int em_cmp(struct x86_emulate_ctxt *ctxt)
2546{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002547 emulate_2op_SrcV("cmp", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002548 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002549 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002550 return X86EMUL_CONTINUE;
2551}
2552
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002553static int em_test(struct x86_emulate_ctxt *ctxt)
2554{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002555 emulate_2op_SrcV("test", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002556 return X86EMUL_CONTINUE;
2557}
2558
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002559static int em_xchg(struct x86_emulate_ctxt *ctxt)
2560{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002561 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002562 ctxt->src.val = ctxt->dst.val;
2563 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002564
2565 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002566 ctxt->dst.val = ctxt->src.orig_val;
2567 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002568 return X86EMUL_CONTINUE;
2569}
2570
Avi Kivity5c82aa22010-08-18 18:31:43 +03002571static int em_imul(struct x86_emulate_ctxt *ctxt)
2572{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002573 emulate_2op_SrcV_nobyte("imul", ctxt->src, ctxt->dst, ctxt->eflags);
Avi Kivity5c82aa22010-08-18 18:31:43 +03002574 return X86EMUL_CONTINUE;
2575}
2576
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002577static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2578{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002579 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002580 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002581}
2582
Avi Kivity61429142010-08-19 15:13:00 +03002583static int em_cwd(struct x86_emulate_ctxt *ctxt)
2584{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002585 ctxt->dst.type = OP_REG;
2586 ctxt->dst.bytes = ctxt->src.bytes;
2587 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2588 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002589
2590 return X86EMUL_CONTINUE;
2591}
2592
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002593static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2594{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002595 u64 tsc = 0;
2596
Avi Kivity717746e2011-04-20 13:37:53 +03002597 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002598 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2599 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03002600 return X86EMUL_CONTINUE;
2601}
2602
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002603static int em_mov(struct x86_emulate_ctxt *ctxt)
2604{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002605 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002606 return X86EMUL_CONTINUE;
2607}
2608
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002609static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2610{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002611 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002612 return emulate_ud(ctxt);
2613
Avi Kivity9dac77f2011-06-01 15:34:25 +03002614 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002615 return X86EMUL_CONTINUE;
2616}
2617
2618static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2619{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002620 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002621
Avi Kivity9dac77f2011-06-01 15:34:25 +03002622 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002623 return emulate_ud(ctxt);
2624
Avi Kivity9dac77f2011-06-01 15:34:25 +03002625 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002626 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2627
2628 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002629 ctxt->dst.type = OP_NONE;
2630 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002631}
2632
Avi Kivityaa97bb42010-01-20 18:09:23 +02002633static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2634{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002635 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002636 return X86EMUL_CONTINUE;
2637}
2638
Avi Kivity38503912011-03-31 18:48:09 +02002639static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2640{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002641 int rc;
2642 ulong linear;
2643
Avi Kivity9dac77f2011-06-01 15:34:25 +03002644 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002645 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002646 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002647 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002648 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002649 return X86EMUL_CONTINUE;
2650}
2651
Avi Kivity2d04a052011-04-20 15:32:49 +03002652static int em_clts(struct x86_emulate_ctxt *ctxt)
2653{
2654 ulong cr0;
2655
2656 cr0 = ctxt->ops->get_cr(ctxt, 0);
2657 cr0 &= ~X86_CR0_TS;
2658 ctxt->ops->set_cr(ctxt, 0, cr0);
2659 return X86EMUL_CONTINUE;
2660}
2661
Avi Kivity26d05cc2011-04-21 12:07:59 +03002662static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2663{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002664 int rc;
2665
Avi Kivity9dac77f2011-06-01 15:34:25 +03002666 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002667 return X86EMUL_UNHANDLEABLE;
2668
2669 rc = ctxt->ops->fix_hypercall(ctxt);
2670 if (rc != X86EMUL_CONTINUE)
2671 return rc;
2672
2673 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002674 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002675 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002676 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002677 return X86EMUL_CONTINUE;
2678}
2679
2680static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2681{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002682 struct desc_ptr desc_ptr;
2683 int rc;
2684
Avi Kivity9dac77f2011-06-01 15:34:25 +03002685 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002686 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002687 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002688 if (rc != X86EMUL_CONTINUE)
2689 return rc;
2690 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2691 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002692 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002693 return X86EMUL_CONTINUE;
2694}
2695
Avi Kivity5ef39c72011-04-21 12:21:50 +03002696static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002697{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002698 int rc;
2699
Avi Kivity5ef39c72011-04-21 12:21:50 +03002700 rc = ctxt->ops->fix_hypercall(ctxt);
2701
Avi Kivity26d05cc2011-04-21 12:07:59 +03002702 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002703 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002704 return rc;
2705}
2706
2707static int em_lidt(struct x86_emulate_ctxt *ctxt)
2708{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002709 struct desc_ptr desc_ptr;
2710 int rc;
2711
Avi Kivity9dac77f2011-06-01 15:34:25 +03002712 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002713 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002714 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002715 if (rc != X86EMUL_CONTINUE)
2716 return rc;
2717 ctxt->ops->set_idt(ctxt, &desc_ptr);
2718 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002719 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002720 return X86EMUL_CONTINUE;
2721}
2722
2723static int em_smsw(struct x86_emulate_ctxt *ctxt)
2724{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002725 ctxt->dst.bytes = 2;
2726 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002727 return X86EMUL_CONTINUE;
2728}
2729
2730static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2731{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002732 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002733 | (ctxt->src.val & 0x0f));
2734 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002735 return X86EMUL_CONTINUE;
2736}
2737
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002738static int em_loop(struct x86_emulate_ctxt *ctxt)
2739{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002740 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2741 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2742 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2743 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002744
2745 return X86EMUL_CONTINUE;
2746}
2747
2748static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2749{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002750 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2751 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002752
2753 return X86EMUL_CONTINUE;
2754}
2755
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002756static int em_cli(struct x86_emulate_ctxt *ctxt)
2757{
2758 if (emulator_bad_iopl(ctxt))
2759 return emulate_gp(ctxt, 0);
2760
2761 ctxt->eflags &= ~X86_EFLAGS_IF;
2762 return X86EMUL_CONTINUE;
2763}
2764
2765static int em_sti(struct x86_emulate_ctxt *ctxt)
2766{
2767 if (emulator_bad_iopl(ctxt))
2768 return emulate_gp(ctxt, 0);
2769
2770 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2771 ctxt->eflags |= X86_EFLAGS_IF;
2772 return X86EMUL_CONTINUE;
2773}
2774
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002775static bool valid_cr(int nr)
2776{
2777 switch (nr) {
2778 case 0:
2779 case 2 ... 4:
2780 case 8:
2781 return true;
2782 default:
2783 return false;
2784 }
2785}
2786
2787static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2788{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002789 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002790 return emulate_ud(ctxt);
2791
2792 return X86EMUL_CONTINUE;
2793}
2794
2795static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2796{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002797 u64 new_val = ctxt->src.val64;
2798 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002799 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002800
2801 static u64 cr_reserved_bits[] = {
2802 0xffffffff00000000ULL,
2803 0, 0, 0, /* CR3 checked later */
2804 CR4_RESERVED_BITS,
2805 0, 0, 0,
2806 CR8_RESERVED_BITS,
2807 };
2808
2809 if (!valid_cr(cr))
2810 return emulate_ud(ctxt);
2811
2812 if (new_val & cr_reserved_bits[cr])
2813 return emulate_gp(ctxt, 0);
2814
2815 switch (cr) {
2816 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002817 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002818 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2819 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2820 return emulate_gp(ctxt, 0);
2821
Avi Kivity717746e2011-04-20 13:37:53 +03002822 cr4 = ctxt->ops->get_cr(ctxt, 4);
2823 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002824
2825 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2826 !(cr4 & X86_CR4_PAE))
2827 return emulate_gp(ctxt, 0);
2828
2829 break;
2830 }
2831 case 3: {
2832 u64 rsvd = 0;
2833
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002834 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2835 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002836 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002837 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002838 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002839 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002840 rsvd = CR3_NONPAE_RESERVED_BITS;
2841
2842 if (new_val & rsvd)
2843 return emulate_gp(ctxt, 0);
2844
2845 break;
2846 }
2847 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002848 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002849
Avi Kivity717746e2011-04-20 13:37:53 +03002850 cr4 = ctxt->ops->get_cr(ctxt, 4);
2851 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002852
2853 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2854 return emulate_gp(ctxt, 0);
2855
2856 break;
2857 }
2858 }
2859
2860 return X86EMUL_CONTINUE;
2861}
2862
Joerg Roedel3b88e412011-04-04 12:39:29 +02002863static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2864{
2865 unsigned long dr7;
2866
Avi Kivity717746e2011-04-20 13:37:53 +03002867 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002868
2869 /* Check if DR7.Global_Enable is set */
2870 return dr7 & (1 << 13);
2871}
2872
2873static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2874{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002875 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002876 u64 cr4;
2877
2878 if (dr > 7)
2879 return emulate_ud(ctxt);
2880
Avi Kivity717746e2011-04-20 13:37:53 +03002881 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002882 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2883 return emulate_ud(ctxt);
2884
2885 if (check_dr7_gd(ctxt))
2886 return emulate_db(ctxt);
2887
2888 return X86EMUL_CONTINUE;
2889}
2890
2891static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2892{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002893 u64 new_val = ctxt->src.val64;
2894 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002895
2896 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2897 return emulate_gp(ctxt, 0);
2898
2899 return check_dr_read(ctxt);
2900}
2901
Joerg Roedel01de8b02011-04-04 12:39:31 +02002902static int check_svme(struct x86_emulate_ctxt *ctxt)
2903{
2904 u64 efer;
2905
Avi Kivity717746e2011-04-20 13:37:53 +03002906 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002907
2908 if (!(efer & EFER_SVME))
2909 return emulate_ud(ctxt);
2910
2911 return X86EMUL_CONTINUE;
2912}
2913
2914static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2915{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002916 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002917
2918 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002919 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002920 return emulate_gp(ctxt, 0);
2921
2922 return check_svme(ctxt);
2923}
2924
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002925static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2926{
Avi Kivity717746e2011-04-20 13:37:53 +03002927 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002928
Avi Kivity717746e2011-04-20 13:37:53 +03002929 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002930 return emulate_ud(ctxt);
2931
2932 return X86EMUL_CONTINUE;
2933}
2934
Joerg Roedel80612522011-04-04 12:39:33 +02002935static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2936{
Avi Kivity717746e2011-04-20 13:37:53 +03002937 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002938 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002939
Avi Kivity717746e2011-04-20 13:37:53 +03002940 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002941 (rcx > 3))
2942 return emulate_gp(ctxt, 0);
2943
2944 return X86EMUL_CONTINUE;
2945}
2946
Joerg Roedelf6511932011-04-04 12:39:35 +02002947static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2948{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002949 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2950 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->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{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002958 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2959 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002960 return emulate_gp(ctxt, 0);
2961
2962 return X86EMUL_CONTINUE;
2963}
2964
Avi Kivity73fba5f2010-07-29 15:11:53 +03002965#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002966#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002967#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2968 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002969#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002970#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002971#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002972#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002973#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002974#define II(_f, _e, _i) \
2975 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002976#define IIP(_f, _e, _i, _p) \
2977 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2978 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002979#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002980
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002981#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002982#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002983#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2984
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002985#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2986 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2987 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002988
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002989static struct opcode group7_rm1[] = {
2990 DI(SrcNone | ModRM | Priv, monitor),
2991 DI(SrcNone | ModRM | Priv, mwait),
2992 N, N, N, N, N, N,
2993};
2994
Joerg Roedel01de8b02011-04-04 12:39:31 +02002995static struct opcode group7_rm3[] = {
2996 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03002997 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002998 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
2999 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3000 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3001 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3002 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3003 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3004};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003005
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003006static struct opcode group7_rm7[] = {
3007 N,
3008 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3009 N, N, N, N, N, N,
3010};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003011
Avi Kivity73fba5f2010-07-29 15:11:53 +03003012static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003013 I(Lock, em_add),
3014 I(Lock, em_or),
3015 I(Lock, em_adc),
3016 I(Lock, em_sbb),
3017 I(Lock, em_and),
3018 I(Lock, em_sub),
3019 I(Lock, em_xor),
3020 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003021};
3022
3023static struct opcode group1A[] = {
3024 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3025};
3026
3027static struct opcode group3[] = {
3028 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
3029 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03003030 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003031};
3032
3033static struct opcode group4[] = {
3034 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3035 N, N, N, N, N, N,
3036};
3037
3038static struct opcode group5[] = {
3039 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003040 D(SrcMem | ModRM | Stack),
3041 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003042 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3043 D(SrcMem | ModRM | Stack), N,
3044};
3045
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003046static struct opcode group6[] = {
3047 DI(ModRM | Prot, sldt),
3048 DI(ModRM | Prot, str),
3049 DI(ModRM | Prot | Priv, lldt),
3050 DI(ModRM | Prot | Priv, ltr),
3051 N, N, N, N,
3052};
3053
Avi Kivity73fba5f2010-07-29 15:11:53 +03003054static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003055 DI(ModRM | Mov | DstMem | Priv, sgdt),
3056 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003057 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3058 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3059 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3060 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3061 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003062}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003063 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3064 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003065 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003066 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3067 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003068} };
3069
3070static struct opcode group8[] = {
3071 N, N, N, N,
3072 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3073 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3074};
3075
3076static struct group_dual group9 = { {
3077 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3078}, {
3079 N, N, N, N, N, N, N, N,
3080} };
3081
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003082static struct opcode group11[] = {
3083 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3084};
3085
Avi Kivityaa97bb42010-01-20 18:09:23 +02003086static struct gprefix pfx_0f_6f_0f_7f = {
3087 N, N, N, I(Sse, em_movdqu),
3088};
3089
Avi Kivity73fba5f2010-07-29 15:11:53 +03003090static struct opcode opcode_table[256] = {
3091 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003092 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003093 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3094 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003095 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003096 D(ImplicitOps | Stack | No64), N,
3097 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003098 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003099 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3100 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003101 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003102 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3103 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003104 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003105 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003106 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003107 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003108 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003109 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003110 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003111 /* 0x40 - 0x4F */
3112 X16(D(DstReg)),
3113 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003114 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003115 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003116 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003117 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003118 I(ImplicitOps | Stack | No64, em_pusha),
3119 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003120 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3121 N, N, N, N,
3122 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003123 I(SrcImm | Mov | Stack, em_push),
3124 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003125 I(SrcImmByte | Mov | Stack, em_push),
3126 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003127 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3128 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003129 /* 0x70 - 0x7F */
3130 X16(D(SrcImmByte)),
3131 /* 0x80 - 0x87 */
3132 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3133 G(DstMem | SrcImm | ModRM | Group, group1),
3134 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3135 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003136 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003137 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003138 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003139 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3140 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003141 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3142 D(ModRM | SrcMem | NoAccess | DstReg),
3143 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3144 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003145 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003146 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003147 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003148 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003149 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003150 II(ImplicitOps | Stack, em_pushf, pushf),
3151 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003152 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003153 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3154 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3155 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003156 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003157 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003158 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003159 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3160 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003161 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003162 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003163 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003164 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003165 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003166 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003167 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003168 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003169 I(ImplicitOps | Stack, em_ret),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003170 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003171 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003172 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003173 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003174 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003175 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003176 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003177 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003178 N, N, N, N,
3179 /* 0xD8 - 0xDF */
3180 N, N, N, N, N, N, N, N,
3181 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003182 X3(I(SrcImmByte, em_loop)),
3183 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003184 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3185 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003186 /* 0xE8 - 0xEF */
3187 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003188 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003189 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3190 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003191 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003192 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003193 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3194 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003195 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003196 D(ImplicitOps), D(ImplicitOps),
3197 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003198 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3199};
3200
3201static struct opcode twobyte_table[256] = {
3202 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003203 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003204 N, I(ImplicitOps | VendorSpecific, em_syscall),
3205 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003206 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003207 N, D(ImplicitOps | ModRM), N, N,
3208 /* 0x10 - 0x1F */
3209 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3210 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003211 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003212 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003213 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003214 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003215 N, N, N, N,
3216 N, N, N, N, N, N, N, N,
3217 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003218 DI(ImplicitOps | Priv, wrmsr),
3219 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3220 DI(ImplicitOps | Priv, rdmsr),
3221 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003222 I(ImplicitOps | VendorSpecific, em_sysenter),
3223 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003224 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003225 N, N, N, N, N, N, N, N,
3226 /* 0x40 - 0x4F */
3227 X16(D(DstReg | SrcMem | ModRM | Mov)),
3228 /* 0x50 - 0x5F */
3229 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3230 /* 0x60 - 0x6F */
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(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003235 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003236 N, N, N, N,
3237 N, N, N, N,
3238 N, N, N, N,
3239 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003240 /* 0x80 - 0x8F */
3241 X16(D(SrcImm)),
3242 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003243 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003244 /* 0xA0 - 0xA7 */
3245 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003246 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003247 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3248 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3249 /* 0xA8 - 0xAF */
3250 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003251 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003252 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3253 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003254 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003255 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003256 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003257 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3258 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3259 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003260 /* 0xB8 - 0xBF */
3261 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003262 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003263 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3264 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003265 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003266 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003267 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003268 N, N, N, GD(0, &group9),
3269 N, N, N, N, N, N, N, N,
3270 /* 0xD0 - 0xDF */
3271 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3272 /* 0xE0 - 0xEF */
3273 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3274 /* 0xF0 - 0xFF */
3275 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3276};
3277
3278#undef D
3279#undef N
3280#undef G
3281#undef GD
3282#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003283#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003284#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003285
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003286#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003287#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003288#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003289#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003290
Avi Kivity9dac77f2011-06-01 15:34:25 +03003291static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003292{
3293 unsigned size;
3294
Avi Kivity9dac77f2011-06-01 15:34:25 +03003295 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003296 if (size == 8)
3297 size = 4;
3298 return size;
3299}
3300
3301static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3302 unsigned size, bool sign_extension)
3303{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003304 int rc = X86EMUL_CONTINUE;
3305
3306 op->type = OP_IMM;
3307 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003308 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003309 /* NB. Immediates are sign-extended as necessary. */
3310 switch (op->bytes) {
3311 case 1:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003312 op->val = insn_fetch(s8, 1, ctxt->_eip);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003313 break;
3314 case 2:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 op->val = insn_fetch(s16, 2, ctxt->_eip);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003316 break;
3317 case 4:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003318 op->val = insn_fetch(s32, 4, ctxt->_eip);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003319 break;
3320 }
3321 if (!sign_extension) {
3322 switch (op->bytes) {
3323 case 1:
3324 op->val &= 0xff;
3325 break;
3326 case 2:
3327 op->val &= 0xffff;
3328 break;
3329 case 4:
3330 op->val &= 0xffffffff;
3331 break;
3332 }
3333 }
3334done:
3335 return rc;
3336}
3337
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003338int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003339{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003340 int rc = X86EMUL_CONTINUE;
3341 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003342 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003343 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003344 struct opcode opcode;
Avi Kivitycb16c342011-06-19 19:21:11 +03003345 struct operand memop = { .type = OP_NONE }, *memopp = NULL;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003346
Avi Kivity9dac77f2011-06-01 15:34:25 +03003347 ctxt->_eip = ctxt->eip;
3348 ctxt->fetch.start = ctxt->_eip;
3349 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003350 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003351 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003352
3353 switch (mode) {
3354 case X86EMUL_MODE_REAL:
3355 case X86EMUL_MODE_VM86:
3356 case X86EMUL_MODE_PROT16:
3357 def_op_bytes = def_ad_bytes = 2;
3358 break;
3359 case X86EMUL_MODE_PROT32:
3360 def_op_bytes = def_ad_bytes = 4;
3361 break;
3362#ifdef CONFIG_X86_64
3363 case X86EMUL_MODE_PROT64:
3364 def_op_bytes = 4;
3365 def_ad_bytes = 8;
3366 break;
3367#endif
3368 default:
3369 return -1;
3370 }
3371
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 ctxt->op_bytes = def_op_bytes;
3373 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003374
3375 /* Legacy prefixes. */
3376 for (;;) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003377 switch (ctxt->b = insn_fetch(u8, 1, ctxt->_eip)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003378 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003379 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003380 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003381 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003382 break;
3383 case 0x67: /* address-size override */
3384 if (mode == X86EMUL_MODE_PROT64)
3385 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003386 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003387 else
3388 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003389 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003390 break;
3391 case 0x26: /* ES override */
3392 case 0x2e: /* CS override */
3393 case 0x36: /* SS override */
3394 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003395 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003396 break;
3397 case 0x64: /* FS override */
3398 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003399 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003400 break;
3401 case 0x40 ... 0x4f: /* REX */
3402 if (mode != X86EMUL_MODE_PROT64)
3403 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003404 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003405 continue;
3406 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003407 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003408 break;
3409 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003410 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003411 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003412 break;
3413 default:
3414 goto done_prefixes;
3415 }
3416
3417 /* Any legacy prefix after a REX prefix nullifies its effect. */
3418
Avi Kivity9dac77f2011-06-01 15:34:25 +03003419 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003420 }
3421
3422done_prefixes:
3423
3424 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003425 if (ctxt->rex_prefix & 8)
3426 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003427
3428 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003429 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003430 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003431 if (ctxt->b == 0x0f) {
3432 ctxt->twobyte = 1;
3433 ctxt->b = insn_fetch(u8, 1, ctxt->_eip);
3434 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003435 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003437
Avi Kivity9dac77f2011-06-01 15:34:25 +03003438 while (ctxt->d & GroupMask) {
3439 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003440 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003441 ctxt->modrm = insn_fetch(u8, 1, ctxt->_eip);
3442 --ctxt->_eip;
3443 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003444 opcode = opcode.u.group[goffset];
3445 break;
3446 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003447 ctxt->modrm = insn_fetch(u8, 1, ctxt->_eip);
3448 --ctxt->_eip;
3449 goffset = (ctxt->modrm >> 3) & 7;
3450 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003451 opcode = opcode.u.gdual->mod3[goffset];
3452 else
3453 opcode = opcode.u.gdual->mod012[goffset];
3454 break;
3455 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003456 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003457 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003458 break;
3459 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003460 if (ctxt->rep_prefix && op_prefix)
Avi Kivity46561642011-04-24 14:09:59 +03003461 return X86EMUL_UNHANDLEABLE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003462 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003463 switch (simd_prefix) {
3464 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3465 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3466 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3467 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3468 }
3469 break;
3470 default:
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003471 return X86EMUL_UNHANDLEABLE;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003472 }
Avi Kivity46561642011-04-24 14:09:59 +03003473
Avi Kivity9dac77f2011-06-01 15:34:25 +03003474 ctxt->d &= ~GroupMask;
3475 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003476 }
3477
Avi Kivity9dac77f2011-06-01 15:34:25 +03003478 ctxt->execute = opcode.u.execute;
3479 ctxt->check_perm = opcode.check_perm;
3480 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003481
3482 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003483 if (ctxt->d == 0 || (ctxt->d & Undefined))
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003484 return -1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003485
Avi Kivity9dac77f2011-06-01 15:34:25 +03003486 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Avi Kivityd8671622011-02-01 16:32:03 +02003487 return -1;
3488
Avi Kivity9dac77f2011-06-01 15:34:25 +03003489 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3490 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003491
Avi Kivity9dac77f2011-06-01 15:34:25 +03003492 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003493 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003494 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003495 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003496 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003497 }
3498
Avi Kivity9dac77f2011-06-01 15:34:25 +03003499 if (ctxt->d & Sse)
3500 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003501
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003502 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003503 if (ctxt->d & ModRM) {
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003504 rc = decode_modrm(ctxt, &memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003505 if (!ctxt->has_seg_override)
3506 set_seg_override(ctxt, ctxt->modrm_seg);
3507 } else if (ctxt->d & MemAbs)
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003508 rc = decode_abs(ctxt, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003509 if (rc != X86EMUL_CONTINUE)
3510 goto done;
3511
Avi Kivity9dac77f2011-06-01 15:34:25 +03003512 if (!ctxt->has_seg_override)
3513 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003514
Avi Kivity9dac77f2011-06-01 15:34:25 +03003515 memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003516
Avi Kivity9dac77f2011-06-01 15:34:25 +03003517 if (memop.type == OP_MEM && ctxt->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003518 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003519
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003520 /*
3521 * Decode and fetch the source operand: register, memory
3522 * or immediate.
3523 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003524 switch (ctxt->d & SrcMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003525 case SrcNone:
3526 break;
3527 case SrcReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003528 decode_register_operand(ctxt, &ctxt->src, 0);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003529 break;
3530 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003531 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003532 goto srcmem_common;
3533 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003534 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003535 goto srcmem_common;
3536 case SrcMem:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003537 memop.bytes = (ctxt->d & ByteOp) ? 1 :
3538 ctxt->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003539 srcmem_common:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003540 ctxt->src = memop;
3541 memopp = &ctxt->src;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003542 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003543 case SrcImmU16:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003544 rc = decode_imm(ctxt, &ctxt->src, 2, false);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003545 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003546 case SrcImm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003547 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003548 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003549 case SrcImmU:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003550 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003551 break;
3552 case SrcImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003553 rc = decode_imm(ctxt, &ctxt->src, 1, true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003554 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003555 case SrcImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003556 rc = decode_imm(ctxt, &ctxt->src, 1, false);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003557 break;
3558 case SrcAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003559 ctxt->src.type = OP_REG;
3560 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3561 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3562 fetch_register_operand(&ctxt->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003563 break;
3564 case SrcOne:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003565 ctxt->src.bytes = 1;
3566 ctxt->src.val = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003567 break;
3568 case SrcSI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003569 ctxt->src.type = OP_MEM;
3570 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3571 ctxt->src.addr.mem.ea =
3572 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3573 ctxt->src.addr.mem.seg = seg_override(ctxt);
3574 ctxt->src.val = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003575 break;
3576 case SrcImmFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003577 ctxt->src.type = OP_IMM;
3578 ctxt->src.addr.mem.ea = ctxt->_eip;
3579 ctxt->src.bytes = ctxt->op_bytes + 2;
3580 insn_fetch_arr(ctxt->src.valptr, ctxt->src.bytes, ctxt->_eip);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003581 break;
3582 case SrcMemFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003583 memop.bytes = ctxt->op_bytes + 2;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003584 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003585 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003586 case SrcDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003587 ctxt->src.type = OP_REG;
3588 ctxt->src.bytes = 2;
3589 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3590 fetch_register_operand(&ctxt->src);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003591 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003592 }
3593
Avi Kivity39f21ee2010-08-18 19:20:21 +03003594 if (rc != X86EMUL_CONTINUE)
3595 goto done;
3596
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003597 /*
3598 * Decode and fetch the second source operand: register, memory
3599 * or immediate.
3600 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003601 switch (ctxt->d & Src2Mask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003602 case Src2None:
3603 break;
3604 case Src2CL:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003605 ctxt->src2.bytes = 1;
3606 ctxt->src2.val = ctxt->regs[VCPU_REGS_RCX] & 0x8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003607 break;
3608 case Src2ImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003609 rc = decode_imm(ctxt, &ctxt->src2, 1, true);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003610 break;
3611 case Src2One:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003612 ctxt->src2.bytes = 1;
3613 ctxt->src2.val = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003614 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003615 case Src2Imm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003616 rc = decode_imm(ctxt, &ctxt->src2, imm_size(ctxt), true);
Avi Kivity7db41eb2010-08-18 19:25:28 +03003617 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003618 }
3619
Avi Kivity39f21ee2010-08-18 19:20:21 +03003620 if (rc != X86EMUL_CONTINUE)
3621 goto done;
3622
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003623 /* Decode and fetch the destination operand: register or memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003624 switch (ctxt->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003625 case DstReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003626 decode_register_operand(ctxt, &ctxt->dst,
3627 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003628 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003629 case DstImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003630 ctxt->dst.type = OP_IMM;
3631 ctxt->dst.addr.mem.ea = ctxt->_eip;
3632 ctxt->dst.bytes = 1;
3633 ctxt->dst.val = insn_fetch(u8, 1, ctxt->_eip);
Wei Yongjun943858e2010-08-06 11:36:51 +08003634 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003635 case DstMem:
3636 case DstMem64:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003637 ctxt->dst = memop;
3638 memopp = &ctxt->dst;
3639 if ((ctxt->d & DstMask) == DstMem64)
3640 ctxt->dst.bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003641 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003642 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3643 if (ctxt->d & BitOp)
3644 fetch_bit_operand(ctxt);
3645 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003646 break;
3647 case DstAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003648 ctxt->dst.type = OP_REG;
3649 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3650 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3651 fetch_register_operand(&ctxt->dst);
3652 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003653 break;
3654 case DstDI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003655 ctxt->dst.type = OP_MEM;
3656 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3657 ctxt->dst.addr.mem.ea =
3658 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3659 ctxt->dst.addr.mem.seg = VCPU_SREG_ES;
3660 ctxt->dst.val = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003661 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003662 case DstDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003663 ctxt->dst.type = OP_REG;
3664 ctxt->dst.bytes = 2;
3665 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3666 fetch_register_operand(&ctxt->dst);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003667 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003668 case ImplicitOps:
3669 /* Special instructions do their own operand decoding. */
3670 default:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003671 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivitycb16c342011-06-19 19:21:11 +03003672 break;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003673 }
3674
3675done:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003676 if (memopp && memopp->type == OP_MEM && ctxt->rip_relative)
3677 memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003678
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003679 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03003680}
3681
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003682static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3683{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003684 /* The second termination condition only applies for REPE
3685 * and REPNE. Test if the repeat string operation prefix is
3686 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3687 * corresponding termination condition according to:
3688 * - if REPE/REPZ and ZF = 0 then done
3689 * - if REPNE/REPNZ and ZF = 1 then done
3690 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003691 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3692 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3693 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003694 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003695 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003696 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3697 return true;
3698
3699 return false;
3700}
3701
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003702int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003703{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03003704 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003705 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003706 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003707 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003708
Avi Kivity9dac77f2011-06-01 15:34:25 +03003709 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003710
Avi Kivity9dac77f2011-06-01 15:34:25 +03003711 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003712 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003713 goto done;
3714 }
3715
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003716 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003717 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003718 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003719 goto done;
3720 }
3721
Avi Kivity9dac77f2011-06-01 15:34:25 +03003722 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003723 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003724 goto done;
3725 }
3726
Avi Kivity9dac77f2011-06-01 15:34:25 +03003727 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003728 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3729 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003730 rc = emulate_ud(ctxt);
3731 goto done;
3732 }
3733
Avi Kivity9dac77f2011-06-01 15:34:25 +03003734 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003735 rc = emulate_nm(ctxt);
3736 goto done;
3737 }
3738
Avi Kivity9dac77f2011-06-01 15:34:25 +03003739 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3740 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003741 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003742 if (rc != X86EMUL_CONTINUE)
3743 goto done;
3744 }
3745
Gleb Natapove92805a2010-02-10 14:21:35 +02003746 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003747 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003748 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003749 goto done;
3750 }
3751
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003752 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003753 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003754 rc = emulate_ud(ctxt);
3755 goto done;
3756 }
3757
Joerg Roedeld09beab2011-04-04 12:39:25 +02003758 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003759 if (ctxt->check_perm) {
3760 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003761 if (rc != X86EMUL_CONTINUE)
3762 goto done;
3763 }
3764
Avi Kivity9dac77f2011-06-01 15:34:25 +03003765 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3766 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003767 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003768 if (rc != X86EMUL_CONTINUE)
3769 goto done;
3770 }
3771
Avi Kivity9dac77f2011-06-01 15:34:25 +03003772 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003773 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003774 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3775 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003776 goto done;
3777 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003778 }
3779
Avi Kivity9dac77f2011-06-01 15:34:25 +03003780 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3781 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3782 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003783 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003784 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003785 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003786 }
3787
Avi Kivity9dac77f2011-06-01 15:34:25 +03003788 if (ctxt->src2.type == OP_MEM) {
3789 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3790 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003791 if (rc != X86EMUL_CONTINUE)
3792 goto done;
3793 }
3794
Avi Kivity9dac77f2011-06-01 15:34:25 +03003795 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003796 goto special_insn;
3797
3798
Avi Kivity9dac77f2011-06-01 15:34:25 +03003799 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003800 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003801 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3802 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003803 if (rc != X86EMUL_CONTINUE)
3804 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003805 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003806 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003807
Avi Kivity018a98d2007-11-27 19:30:56 +02003808special_insn:
3809
Avi Kivity9dac77f2011-06-01 15:34:25 +03003810 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3811 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003812 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003813 if (rc != X86EMUL_CONTINUE)
3814 goto done;
3815 }
3816
Avi Kivity9dac77f2011-06-01 15:34:25 +03003817 if (ctxt->execute) {
3818 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003819 if (rc != X86EMUL_CONTINUE)
3820 goto done;
3821 goto writeback;
3822 }
3823
Avi Kivity9dac77f2011-06-01 15:34:25 +03003824 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825 goto twobyte_insn;
3826
Avi Kivity9dac77f2011-06-01 15:34:25 +03003827 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003828 case 0x06: /* push es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003829 rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003830 break;
3831 case 0x07: /* pop es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003832 rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003833 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003834 case 0x0e: /* push cs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003835 rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003836 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003837 case 0x16: /* push ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003838 rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003839 break;
3840 case 0x17: /* pop ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003841 rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003842 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003843 case 0x1e: /* push ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003844 rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003845 break;
3846 case 0x1f: /* pop ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003847 rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003848 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003849 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003850 emulate_1op("inc", ctxt->dst, ctxt->eflags);
Avi Kivity33615aa2007-10-31 11:15:56 +02003851 break;
3852 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003853 emulate_1op("dec", ctxt->dst, ctxt->eflags);
Avi Kivity33615aa2007-10-31 11:15:56 +02003854 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003856 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003858 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003860 case 0x6c: /* insb */
3861 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003862 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003863 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003864 case 0x6e: /* outsb */
3865 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003866 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003867 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003868 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003869 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003870 if (test_cc(ctxt->b, ctxt->eflags))
3871 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003872 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003873 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003874 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003875 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003877 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003879 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003880 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003881 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003882 rc = em_xchg(ctxt);
3883 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003884 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003885 switch (ctxt->op_bytes) {
3886 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
3887 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
3888 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003889 }
3890 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003891 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003892 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003893 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003894 case 0xc4: /* les */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003895 rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003896 break;
3897 case 0xc5: /* lds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003898 rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003899 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003900 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003901 rc = emulate_int(ctxt, 3);
3902 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003903 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003904 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003905 break;
3906 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003907 if (ctxt->eflags & EFLG_OF)
3908 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003909 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003910 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003911 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003912 break;
3913 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003914 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003915 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003916 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003917 case 0xe4: /* inb */
3918 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003919 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003920 case 0xe6: /* outb */
3921 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003922 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003923 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003924 long int rel = ctxt->src.val;
3925 ctxt->src.val = (unsigned long) ctxt->_eip;
3926 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003927 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003928 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003929 }
3930 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003931 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003932 jmp_rel(ctxt, ctxt->src.val);
3933 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003934 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003935 case 0xec: /* in al,dx */
3936 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003937 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003938 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3939 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003940 goto done; /* IO is needed */
3941 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003942 case 0xee: /* out dx,al */
3943 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003944 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003945 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3946 &ctxt->src.val, 1);
3947 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003948 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003949 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003950 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003951 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003952 case 0xf5: /* cmc */
3953 /* complement carry flag from eflags reg */
3954 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003955 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003956 case 0xf6 ... 0xf7: /* Grp3 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003957 rc = em_grp3(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003958 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003959 case 0xf8: /* clc */
3960 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003961 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003962 case 0xf9: /* stc */
3963 ctxt->eflags |= EFLG_CF;
3964 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003965 case 0xfc: /* cld */
3966 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003967 break;
3968 case 0xfd: /* std */
3969 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003970 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003971 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003972 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003973 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003974 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003975 rc = em_grp45(ctxt);
3976 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003977 default:
3978 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003979 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003980
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003981 if (rc != X86EMUL_CONTINUE)
3982 goto done;
3983
Avi Kivity018a98d2007-11-27 19:30:56 +02003984writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003985 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003986 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003987 goto done;
3988
Gleb Natapov5cd21912010-03-18 15:20:26 +02003989 /*
3990 * restore dst type in case the decoding will be reused
3991 * (happens for string instruction )
3992 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003993 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003994
Avi Kivity9dac77f2011-06-01 15:34:25 +03003995 if ((ctxt->d & SrcMask) == SrcSI)
3996 string_addr_inc(ctxt, seg_override(ctxt),
3997 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003998
Avi Kivity9dac77f2011-06-01 15:34:25 +03003999 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004000 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004001 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004002
Avi Kivity9dac77f2011-06-01 15:34:25 +03004003 if (ctxt->rep_prefix && (ctxt->d & String)) {
4004 struct read_cache *r = &ctxt->io_read;
4005 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004006
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004007 if (!string_insn_completed(ctxt)) {
4008 /*
4009 * Re-enter guest when pio read ahead buffer is empty
4010 * or, if it is not used, after each 1024 iteration.
4011 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004012 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004013 (r->end == 0 || r->end != r->pos)) {
4014 /*
4015 * Reset read cache. Usually happens before
4016 * decode, but since instruction is restarted
4017 * we have to do it here.
4018 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004019 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004020 return EMULATION_RESTART;
4021 }
4022 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004023 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004024 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004025
Avi Kivity9dac77f2011-06-01 15:34:25 +03004026 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004027
4028done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004029 if (rc == X86EMUL_PROPAGATE_FAULT)
4030 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004031 if (rc == X86EMUL_INTERCEPTED)
4032 return EMULATION_INTERCEPTED;
4033
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004034 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035
4036twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004037 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004038 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004039 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004040 break;
4041 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004042 case 0x0d: /* GrpP (prefetch) */
4043 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004044 break;
4045 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004046 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004047 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004048 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004049 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004050 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004051 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004052 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004053 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004054 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004055 goto done;
4056 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004057 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004058 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004060 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004061 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004062 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004063 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004064 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004065 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004066 goto done;
4067 }
4068
Avi Kivity9dac77f2011-06-01 15:34:25 +03004069 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004071 case 0x30:
4072 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004073 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4074 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4075 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004076 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004077 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004078 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004079 }
4080 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004081 break;
4082 case 0x32:
4083 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004084 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004085 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004086 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004087 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004088 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004089 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4090 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004091 }
4092 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004093 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004095 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4096 if (!test_cc(ctxt->b, ctxt->eflags))
4097 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004099 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004100 if (test_cc(ctxt->b, ctxt->eflags))
4101 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004102 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004103 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004104 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004105 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004106 case 0xa0: /* push fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004107 rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004108 break;
4109 case 0xa1: /* pop fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004110 rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004111 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004112 case 0xa3:
4113 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004114 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004115 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004116 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
4117 emulate_2op_SrcV_nobyte("bt", ctxt->src, ctxt->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004118 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004119 case 0xa4: /* shld imm8, r, r/m */
4120 case 0xa5: /* shld cl, r, r/m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004121 emulate_2op_cl("shld", ctxt->src2, ctxt->src, ctxt->dst, ctxt->eflags);
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004122 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004123 case 0xa8: /* push gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004124 rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004125 break;
4126 case 0xa9: /* pop gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004127 rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004128 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004129 case 0xab:
4130 bts: /* bts */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004131 emulate_2op_SrcV_nobyte("bts", ctxt->src, ctxt->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004132 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004133 case 0xac: /* shrd imm8, r, r/m */
4134 case 0xad: /* shrd cl, r, r/m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004135 emulate_2op_cl("shrd", ctxt->src2, ctxt->src, ctxt->dst, ctxt->eflags);
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004136 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004137 case 0xae: /* clflush */
4138 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139 case 0xb0 ... 0xb1: /* cmpxchg */
4140 /*
4141 * Save real source value, then compare EAX against
4142 * destination.
4143 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004144 ctxt->src.orig_val = ctxt->src.val;
4145 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
4146 emulate_2op_SrcV("cmp", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier05f086f2007-09-24 11:10:55 +02004147 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004149 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150 } else {
4151 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004152 ctxt->dst.type = OP_REG;
4153 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154 }
4155 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004156 case 0xb2: /* lss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004157 rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004158 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 case 0xb3:
4160 btr: /* btr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004161 emulate_2op_SrcV_nobyte("btr", ctxt->src, ctxt->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004162 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004163 case 0xb4: /* lfs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004164 rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004165 break;
4166 case 0xb5: /* lgs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004167 rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004168 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004170 ctxt->dst.bytes = ctxt->op_bytes;
4171 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4172 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 case 0:
4177 goto bt;
4178 case 1:
4179 goto bts;
4180 case 2:
4181 goto btr;
4182 case 3:
4183 goto btc;
4184 }
4185 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004186 case 0xbb:
4187 btc: /* btc */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004188 emulate_2op_SrcV_nobyte("btc", ctxt->src, ctxt->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004189 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004190 case 0xbc: { /* bsf */
4191 u8 zf;
4192 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004193 : "=r"(ctxt->dst.val), "=q"(zf)
4194 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004195 ctxt->eflags &= ~X86_EFLAGS_ZF;
4196 if (zf) {
4197 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004198 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004199 }
4200 break;
4201 }
4202 case 0xbd: { /* bsr */
4203 u8 zf;
4204 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004205 : "=r"(ctxt->dst.val), "=q"(zf)
4206 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004207 ctxt->eflags &= ~X86_EFLAGS_ZF;
4208 if (zf) {
4209 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004210 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004211 }
4212 break;
4213 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004214 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004215 ctxt->dst.bytes = ctxt->op_bytes;
4216 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4217 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004218 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004219 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004220 emulate_2op_SrcV("add", ctxt->src, ctxt->dst, ctxt->eflags);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004221 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004222 ctxt->src.val = ctxt->dst.orig_val;
4223 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004224 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004225 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004226 ctxt->dst.bytes = ctxt->op_bytes;
4227 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4228 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004229 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004231 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004232 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004233 default:
4234 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004236
4237 if (rc != X86EMUL_CONTINUE)
4238 goto done;
4239
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240 goto writeback;
4241
4242cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004243 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004244}