blob: ce48dc4e6ed0bfaabd5b24243399d3d65e836f64 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b122010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b122010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
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 Yoshikawa807941b2011-07-30 18:00:17 +0900654/*
655 * Fetch the next byte of the instruction being emulated which is pointed to
656 * by ctxt->_eip, then increment ctxt->_eip.
657 *
658 * Also prefetch the remaining bytes of the instruction without crossing page
659 * boundary if they are not in fetch_cache yet.
660 */
661static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200662{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300663 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200664 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300665 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200666
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900667 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400668 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900669 struct segmented_address addr = { .seg = VCPU_SREG_CS,
670 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300671 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900672 size = min(15UL - cur_size,
673 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400674 rc = __linearize(ctxt, addr, size, false, true, &linear);
675 if (rc != X86EMUL_CONTINUE)
676 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900677 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
678 size, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900679 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200680 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300681 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200682 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900683 *dest = fc->data[ctxt->_eip - fc->start];
684 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900685 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200686}
687
688static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900689 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200690{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900691 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200692
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200693 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900694 if (ctxt->_eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200695 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200696 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900697 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900698 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200699 return rc;
700 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900701 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200702}
703
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900704/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900705#define insn_fetch(_type, _size, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900706({ unsigned long _x; \
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900707 rc = do_insn_fetch(_ctxt, &_x, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900708 if (rc != X86EMUL_CONTINUE) \
709 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900710 (_type)_x; \
711})
712
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900713#define insn_fetch_arr(_arr, _size, _ctxt) \
714({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900715 if (rc != X86EMUL_CONTINUE) \
716 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900717})
718
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000719/*
720 * Given the 'reg' portion of a ModRM byte, and a register block, return a
721 * pointer into the block that addresses the relevant register.
722 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
723 */
724static void *decode_register(u8 modrm_reg, unsigned long *regs,
725 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726{
727 void *p;
728
729 p = &regs[modrm_reg];
730 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
731 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
732 return p;
733}
734
735static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200736 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 u16 *size, unsigned long *address, int op_bytes)
738{
739 int rc;
740
741 if (op_bytes == 2)
742 op_bytes = 3;
743 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200744 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900745 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200747 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200748 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 return rc;
750}
751
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300752static int test_cc(unsigned int condition, unsigned int flags)
753{
754 int rc = 0;
755
756 switch ((condition & 15) >> 1) {
757 case 0: /* o */
758 rc |= (flags & EFLG_OF);
759 break;
760 case 1: /* b/c/nae */
761 rc |= (flags & EFLG_CF);
762 break;
763 case 2: /* z/e */
764 rc |= (flags & EFLG_ZF);
765 break;
766 case 3: /* be/na */
767 rc |= (flags & (EFLG_CF|EFLG_ZF));
768 break;
769 case 4: /* s */
770 rc |= (flags & EFLG_SF);
771 break;
772 case 5: /* p/pe */
773 rc |= (flags & EFLG_PF);
774 break;
775 case 7: /* le/ng */
776 rc |= (flags & EFLG_ZF);
777 /* fall through */
778 case 6: /* l/nge */
779 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
780 break;
781 }
782
783 /* Odd condition identifiers (lsb == 1) have inverted sense. */
784 return (!!rc ^ (condition & 1));
785}
786
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300787static void fetch_register_operand(struct operand *op)
788{
789 switch (op->bytes) {
790 case 1:
791 op->val = *(u8 *)op->addr.reg;
792 break;
793 case 2:
794 op->val = *(u16 *)op->addr.reg;
795 break;
796 case 4:
797 op->val = *(u32 *)op->addr.reg;
798 break;
799 case 8:
800 op->val = *(u64 *)op->addr.reg;
801 break;
802 }
803}
804
Avi Kivity12537912011-03-29 11:41:27 +0200805static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
806{
807 ctxt->ops->get_fpu(ctxt);
808 switch (reg) {
809 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
810 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
811 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
812 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
813 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
814 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
815 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
816 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
817#ifdef CONFIG_X86_64
818 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
819 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
820 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
821 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
822 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
823 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
824 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
825 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
826#endif
827 default: BUG();
828 }
829 ctxt->ops->put_fpu(ctxt);
830}
831
832static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
833 int reg)
834{
835 ctxt->ops->get_fpu(ctxt);
836 switch (reg) {
837 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
838 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
839 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
840 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
841 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
842 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
843 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
844 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
845#ifdef CONFIG_X86_64
846 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
847 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
848 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
849 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
850 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
851 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
852 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
853 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
854#endif
855 default: BUG();
856 }
857 ctxt->ops->put_fpu(ctxt);
858}
859
860static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
861 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200862 int inhibit_bytereg)
863{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300864 unsigned reg = ctxt->modrm_reg;
865 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200866
Avi Kivity9dac77f2011-06-01 15:34:25 +0300867 if (!(ctxt->d & ModRM))
868 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200869
Avi Kivity9dac77f2011-06-01 15:34:25 +0300870 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200871 op->type = OP_XMM;
872 op->bytes = 16;
873 op->addr.xmm = reg;
874 read_sse_reg(ctxt, &op->vec_val, reg);
875 return;
876 }
877
Avi Kivity3c118e22007-10-31 10:27:04 +0200878 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300879 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
880 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200881 op->bytes = 1;
882 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300883 op->addr.reg = decode_register(reg, ctxt->regs, 0);
884 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200885 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300886 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200887 op->orig_val = op->val;
888}
889
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300891 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200892{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200893 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700894 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900895 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300896 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200897
Avi Kivity9dac77f2011-06-01 15:34:25 +0300898 if (ctxt->rex_prefix) {
899 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
900 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
901 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902 }
903
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900904 ctxt->modrm = insn_fetch(u8, 1, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300905 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
906 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
907 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
908 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200909
Avi Kivity9dac77f2011-06-01 15:34:25 +0300910 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300911 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300912 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
913 op->addr.reg = decode_register(ctxt->modrm_rm,
914 ctxt->regs, ctxt->d & ByteOp);
915 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200916 op->type = OP_XMM;
917 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300918 op->addr.xmm = ctxt->modrm_rm;
919 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200920 return rc;
921 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300922 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200923 return rc;
924 }
925
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300926 op->type = OP_MEM;
927
Avi Kivity9dac77f2011-06-01 15:34:25 +0300928 if (ctxt->ad_bytes == 2) {
929 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
930 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
931 unsigned si = ctxt->regs[VCPU_REGS_RSI];
932 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200933
934 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300935 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200936 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300937 if (ctxt->modrm_rm == 6)
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900938 modrm_ea += insn_fetch(u16, 2, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200939 break;
940 case 1:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900941 modrm_ea += insn_fetch(s8, 1, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200942 break;
943 case 2:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900944 modrm_ea += insn_fetch(u16, 2, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200945 break;
946 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300947 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200948 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300949 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200950 break;
951 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300952 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200953 break;
954 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300955 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200956 break;
957 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300958 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200959 break;
960 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300961 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200962 break;
963 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300964 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200965 break;
966 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300967 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300968 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200969 break;
970 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300971 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200972 break;
973 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300974 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
975 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
976 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300977 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200978 } else {
979 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300980 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900981 sib = insn_fetch(u8, 1, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200982 index_reg |= (sib >> 3) & 7;
983 base_reg |= sib & 7;
984 scale = sib >> 6;
985
Avi Kivity9dac77f2011-06-01 15:34:25 +0300986 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900987 modrm_ea += insn_fetch(s32, 4, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700988 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300989 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700990 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300991 modrm_ea += ctxt->regs[index_reg] << scale;
992 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700993 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300994 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700995 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300996 modrm_ea += ctxt->regs[ctxt->modrm_rm];
997 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200998 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300999 if (ctxt->modrm_rm == 5)
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09001000 modrm_ea += insn_fetch(s32, 4, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001001 break;
1002 case 1:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09001003 modrm_ea += insn_fetch(s8, 1, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001004 break;
1005 case 2:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09001006 modrm_ea += insn_fetch(s32, 4, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001007 break;
1008 }
1009 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001010 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001011done:
1012 return rc;
1013}
1014
1015static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001016 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001017{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001018 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001019
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001020 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001021 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001022 case 2:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09001023 op->addr.mem.ea = insn_fetch(u16, 2, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001024 break;
1025 case 4:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09001026 op->addr.mem.ea = insn_fetch(u32, 4, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001027 break;
1028 case 8:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09001029 op->addr.mem.ea = insn_fetch(u64, 8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001030 break;
1031 }
1032done:
1033 return rc;
1034}
1035
Avi Kivity9dac77f2011-06-01 15:34:25 +03001036static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001037{
Sheng Yang7129eec2010-09-28 16:33:32 +08001038 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001039
Avi Kivity9dac77f2011-06-01 15:34:25 +03001040 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1041 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001042
Avi Kivity9dac77f2011-06-01 15:34:25 +03001043 if (ctxt->src.bytes == 2)
1044 sv = (s16)ctxt->src.val & (s16)mask;
1045 else if (ctxt->src.bytes == 4)
1046 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001047
Avi Kivity9dac77f2011-06-01 15:34:25 +03001048 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001049 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001050
1051 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001052 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001053}
1054
Gleb Natapov9de41572010-04-28 19:15:22 +03001055static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001056 unsigned long addr, void *dest, unsigned size)
1057{
1058 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001059 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001060
1061 while (size) {
1062 int n = min(size, 8u);
1063 size -= n;
1064 if (mc->pos < mc->end)
1065 goto read_cached;
1066
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001067 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1068 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001069 if (rc != X86EMUL_CONTINUE)
1070 return rc;
1071 mc->end += n;
1072
1073 read_cached:
1074 memcpy(dest, mc->data + mc->pos, n);
1075 mc->pos += n;
1076 dest += n;
1077 addr += n;
1078 }
1079 return X86EMUL_CONTINUE;
1080}
1081
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001082static int segmented_read(struct x86_emulate_ctxt *ctxt,
1083 struct segmented_address addr,
1084 void *data,
1085 unsigned size)
1086{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001087 int rc;
1088 ulong linear;
1089
Avi Kivity83b87952011-04-03 11:31:19 +03001090 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001091 if (rc != X86EMUL_CONTINUE)
1092 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001093 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001094}
1095
1096static int segmented_write(struct x86_emulate_ctxt *ctxt,
1097 struct segmented_address addr,
1098 const void *data,
1099 unsigned size)
1100{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001101 int rc;
1102 ulong linear;
1103
Avi Kivity83b87952011-04-03 11:31:19 +03001104 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001105 if (rc != X86EMUL_CONTINUE)
1106 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001107 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1108 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001109}
1110
1111static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1112 struct segmented_address addr,
1113 const void *orig_data, const void *data,
1114 unsigned size)
1115{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001116 int rc;
1117 ulong linear;
1118
Avi Kivity83b87952011-04-03 11:31:19 +03001119 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001120 if (rc != X86EMUL_CONTINUE)
1121 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001122 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1123 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001124}
1125
Gleb Natapov7b262e92010-03-18 15:20:27 +02001126static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001127 unsigned int size, unsigned short port,
1128 void *dest)
1129{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001130 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001131
1132 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001133 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001134 unsigned int count = ctxt->rep_prefix ?
1135 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001136 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001137 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1138 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001139 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1140 count);
1141 if (n == 0)
1142 n = 1;
1143 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001144 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001145 return 0;
1146 rc->end = n * size;
1147 }
1148
1149 memcpy(dest, rc->data + rc->pos, size);
1150 rc->pos += size;
1151 return 1;
1152}
1153
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001154static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001155 u16 selector, struct desc_ptr *dt)
1156{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001157 struct x86_emulate_ops *ops = ctxt->ops;
1158
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001159 if (selector & 1 << 2) {
1160 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001161 u16 sel;
1162
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001163 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001164 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001165 return;
1166
1167 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1168 dt->address = get_desc_base(&desc);
1169 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001170 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001171}
1172
1173/* allowed just for 8 bytes segments */
1174static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001175 u16 selector, struct desc_struct *desc)
1176{
1177 struct desc_ptr dt;
1178 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001179 ulong addr;
1180
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001181 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001182
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001183 if (dt.size < index * 8 + 7)
1184 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001185
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001186 addr = dt.address + index * 8;
1187 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1188 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001189}
1190
1191/* allowed just for 8 bytes segments */
1192static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001193 u16 selector, struct desc_struct *desc)
1194{
1195 struct desc_ptr dt;
1196 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001197 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001199 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001200
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001201 if (dt.size < index * 8 + 7)
1202 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001203
1204 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001205 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1206 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001207}
1208
Gleb Natapov5601d052011-03-07 14:55:06 +02001209/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001210static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001211 u16 selector, int seg)
1212{
1213 struct desc_struct seg_desc;
1214 u8 dpl, rpl, cpl;
1215 unsigned err_vec = GP_VECTOR;
1216 u32 err_code = 0;
1217 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1218 int ret;
1219
1220 memset(&seg_desc, 0, sizeof seg_desc);
1221
1222 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1223 || ctxt->mode == X86EMUL_MODE_REAL) {
1224 /* set real mode segment descriptor */
1225 set_desc_base(&seg_desc, selector << 4);
1226 set_desc_limit(&seg_desc, 0xffff);
1227 seg_desc.type = 3;
1228 seg_desc.p = 1;
1229 seg_desc.s = 1;
1230 goto load;
1231 }
1232
1233 /* NULL selector is not valid for TR, CS and SS */
1234 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1235 && null_selector)
1236 goto exception;
1237
1238 /* TR should be in GDT only */
1239 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1240 goto exception;
1241
1242 if (null_selector) /* for NULL selector skip all following checks */
1243 goto load;
1244
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001245 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001246 if (ret != X86EMUL_CONTINUE)
1247 return ret;
1248
1249 err_code = selector & 0xfffc;
1250 err_vec = GP_VECTOR;
1251
1252 /* can't load system descriptor into segment selecor */
1253 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1254 goto exception;
1255
1256 if (!seg_desc.p) {
1257 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1258 goto exception;
1259 }
1260
1261 rpl = selector & 3;
1262 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001263 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001264
1265 switch (seg) {
1266 case VCPU_SREG_SS:
1267 /*
1268 * segment is not a writable data segment or segment
1269 * selector's RPL != CPL or segment selector's RPL != CPL
1270 */
1271 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1272 goto exception;
1273 break;
1274 case VCPU_SREG_CS:
1275 if (!(seg_desc.type & 8))
1276 goto exception;
1277
1278 if (seg_desc.type & 4) {
1279 /* conforming */
1280 if (dpl > cpl)
1281 goto exception;
1282 } else {
1283 /* nonconforming */
1284 if (rpl > cpl || dpl != cpl)
1285 goto exception;
1286 }
1287 /* CS(RPL) <- CPL */
1288 selector = (selector & 0xfffc) | cpl;
1289 break;
1290 case VCPU_SREG_TR:
1291 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1292 goto exception;
1293 break;
1294 case VCPU_SREG_LDTR:
1295 if (seg_desc.s || seg_desc.type != 2)
1296 goto exception;
1297 break;
1298 default: /* DS, ES, FS, or GS */
1299 /*
1300 * segment is not a data or readable code segment or
1301 * ((segment is a data or nonconforming code segment)
1302 * and (both RPL and CPL > DPL))
1303 */
1304 if ((seg_desc.type & 0xa) == 0x8 ||
1305 (((seg_desc.type & 0xc) != 0xc) &&
1306 (rpl > dpl && cpl > dpl)))
1307 goto exception;
1308 break;
1309 }
1310
1311 if (seg_desc.s) {
1312 /* mark segment as accessed */
1313 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001314 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001315 if (ret != X86EMUL_CONTINUE)
1316 return ret;
1317 }
1318load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001319 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001320 return X86EMUL_CONTINUE;
1321exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001322 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001323 return X86EMUL_PROPAGATE_FAULT;
1324}
1325
Wei Yongjun31be40b2010-08-17 09:17:30 +08001326static void write_register_operand(struct operand *op)
1327{
1328 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1329 switch (op->bytes) {
1330 case 1:
1331 *(u8 *)op->addr.reg = (u8)op->val;
1332 break;
1333 case 2:
1334 *(u16 *)op->addr.reg = (u16)op->val;
1335 break;
1336 case 4:
1337 *op->addr.reg = (u32)op->val;
1338 break; /* 64b: zero-extend */
1339 case 8:
1340 *op->addr.reg = op->val;
1341 break;
1342 }
1343}
1344
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001345static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001346{
1347 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001348
Avi Kivity9dac77f2011-06-01 15:34:25 +03001349 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001350 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001351 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001352 break;
1353 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001354 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001355 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001356 ctxt->dst.addr.mem,
1357 &ctxt->dst.orig_val,
1358 &ctxt->dst.val,
1359 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001360 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001361 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001362 ctxt->dst.addr.mem,
1363 &ctxt->dst.val,
1364 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001365 if (rc != X86EMUL_CONTINUE)
1366 return rc;
1367 break;
Avi Kivity12537912011-03-29 11:41:27 +02001368 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001369 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001370 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001371 case OP_NONE:
1372 /* no writeback */
1373 break;
1374 default:
1375 break;
1376 }
1377 return X86EMUL_CONTINUE;
1378}
1379
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001380static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001381{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001382 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001383
Avi Kivity9dac77f2011-06-01 15:34:25 +03001384 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1385 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001386 addr.seg = VCPU_SREG_SS;
1387
1388 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001389 ctxt->dst.type = OP_NONE;
1390 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001391}
1392
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001393static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001394 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001395{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001397 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398
Avi Kivity9dac77f2011-06-01 15:34:25 +03001399 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001400 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001401 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001402 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403 return rc;
1404
Avi Kivity9dac77f2011-06-01 15:34:25 +03001405 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001406 return rc;
1407}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001408
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001409static int em_pop(struct x86_emulate_ctxt *ctxt)
1410{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001411 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001412}
1413
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001414static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001415 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001416{
1417 int rc;
1418 unsigned long val, change_mask;
1419 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001420 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001421
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001422 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001423 if (rc != X86EMUL_CONTINUE)
1424 return rc;
1425
1426 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1427 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1428
1429 switch(ctxt->mode) {
1430 case X86EMUL_MODE_PROT64:
1431 case X86EMUL_MODE_PROT32:
1432 case X86EMUL_MODE_PROT16:
1433 if (cpl == 0)
1434 change_mask |= EFLG_IOPL;
1435 if (cpl <= iopl)
1436 change_mask |= EFLG_IF;
1437 break;
1438 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001439 if (iopl < 3)
1440 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001441 change_mask |= EFLG_IF;
1442 break;
1443 default: /* real mode */
1444 change_mask |= (EFLG_IOPL | EFLG_IF);
1445 break;
1446 }
1447
1448 *(unsigned long *)dest =
1449 (ctxt->eflags & ~change_mask) | (val & change_mask);
1450
1451 return rc;
1452}
1453
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001454static int em_popf(struct x86_emulate_ctxt *ctxt)
1455{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001456 ctxt->dst.type = OP_REG;
1457 ctxt->dst.addr.reg = &ctxt->eflags;
1458 ctxt->dst.bytes = ctxt->op_bytes;
1459 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001460}
1461
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001462static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001463{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001464 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001465
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001466 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001467}
1468
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001469static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001470{
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001471 unsigned long selector;
1472 int rc;
1473
Avi Kivity9dac77f2011-06-01 15:34:25 +03001474 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001475 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001476 return rc;
1477
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001478 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001479 return rc;
1480}
1481
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001482static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001483{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001484 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001485 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001486 int reg = VCPU_REGS_RAX;
1487
1488 while (reg <= VCPU_REGS_RDI) {
1489 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001490 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001491
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001492 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001493 if (rc != X86EMUL_CONTINUE)
1494 return rc;
1495
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001496 ++reg;
1497 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001498
Wei Yongjunc37eda12010-06-15 09:03:33 +08001499 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001500}
1501
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001502static int em_pushf(struct x86_emulate_ctxt *ctxt)
1503{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001504 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001505 return em_push(ctxt);
1506}
1507
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001508static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001509{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001510 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001511 int reg = VCPU_REGS_RDI;
1512
1513 while (reg >= VCPU_REGS_RAX) {
1514 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001515 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1516 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001517 --reg;
1518 }
1519
Avi Kivity9dac77f2011-06-01 15:34:25 +03001520 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001521 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001522 break;
1523 --reg;
1524 }
1525 return rc;
1526}
1527
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001528int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001529{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001530 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001531 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001532 struct desc_ptr dt;
1533 gva_t cs_addr;
1534 gva_t eip_addr;
1535 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001536
1537 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001538 ctxt->src.val = ctxt->eflags;
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
1543 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1544
Avi Kivity9dac77f2011-06-01 15:34:25 +03001545 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001546 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001547 if (rc != X86EMUL_CONTINUE)
1548 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001549
Avi Kivity9dac77f2011-06-01 15:34:25 +03001550 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001551 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001552 if (rc != X86EMUL_CONTINUE)
1553 return rc;
1554
Avi Kivity4bff1e862011-04-20 13:37:53 +03001555 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001556
1557 eip_addr = dt.address + (irq << 2);
1558 cs_addr = dt.address + (irq << 2) + 2;
1559
Avi Kivity0f65dd72011-04-20 13:37:53 +03001560 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001561 if (rc != X86EMUL_CONTINUE)
1562 return rc;
1563
Avi Kivity0f65dd72011-04-20 13:37:53 +03001564 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001565 if (rc != X86EMUL_CONTINUE)
1566 return rc;
1567
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001568 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001569 if (rc != X86EMUL_CONTINUE)
1570 return rc;
1571
Avi Kivity9dac77f2011-06-01 15:34:25 +03001572 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001573
1574 return rc;
1575}
1576
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001577static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001578{
1579 switch(ctxt->mode) {
1580 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001581 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001582 case X86EMUL_MODE_VM86:
1583 case X86EMUL_MODE_PROT16:
1584 case X86EMUL_MODE_PROT32:
1585 case X86EMUL_MODE_PROT64:
1586 default:
1587 /* Protected mode interrupts unimplemented yet */
1588 return X86EMUL_UNHANDLEABLE;
1589 }
1590}
1591
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001592static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001593{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001594 int rc = X86EMUL_CONTINUE;
1595 unsigned long temp_eip = 0;
1596 unsigned long temp_eflags = 0;
1597 unsigned long cs = 0;
1598 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1599 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1600 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1601 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1602
1603 /* TODO: Add stack limit check */
1604
Avi Kivity9dac77f2011-06-01 15:34:25 +03001605 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001606
1607 if (rc != X86EMUL_CONTINUE)
1608 return rc;
1609
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001610 if (temp_eip & ~0xffff)
1611 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001612
Avi Kivity9dac77f2011-06-01 15:34:25 +03001613 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001614
1615 if (rc != X86EMUL_CONTINUE)
1616 return rc;
1617
Avi Kivity9dac77f2011-06-01 15:34:25 +03001618 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001619
1620 if (rc != X86EMUL_CONTINUE)
1621 return rc;
1622
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001623 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001624
1625 if (rc != X86EMUL_CONTINUE)
1626 return rc;
1627
Avi Kivity9dac77f2011-06-01 15:34:25 +03001628 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001629
1630
Avi Kivity9dac77f2011-06-01 15:34:25 +03001631 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001632 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001633 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001634 ctxt->eflags &= ~0xffff;
1635 ctxt->eflags |= temp_eflags;
1636 }
1637
1638 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1639 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1640
1641 return rc;
1642}
1643
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001644static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001645{
1646 switch(ctxt->mode) {
1647 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001648 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001649 case X86EMUL_MODE_VM86:
1650 case X86EMUL_MODE_PROT16:
1651 case X86EMUL_MODE_PROT32:
1652 case X86EMUL_MODE_PROT64:
1653 default:
1654 /* iret from protected mode unimplemented yet */
1655 return X86EMUL_UNHANDLEABLE;
1656 }
1657}
1658
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001659static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1660{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001661 int rc;
1662 unsigned short sel;
1663
Avi Kivity9dac77f2011-06-01 15:34:25 +03001664 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001665
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001666 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001667 if (rc != X86EMUL_CONTINUE)
1668 return rc;
1669
Avi Kivity9dac77f2011-06-01 15:34:25 +03001670 ctxt->_eip = 0;
1671 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001672 return X86EMUL_CONTINUE;
1673}
1674
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001675static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001676{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001677 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001678}
1679
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001680static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001682 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 case 0: /* rol */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001684 emulate_2op_SrcB("rol", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001685 break;
1686 case 1: /* ror */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001687 emulate_2op_SrcB("ror", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001688 break;
1689 case 2: /* rcl */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001690 emulate_2op_SrcB("rcl", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001691 break;
1692 case 3: /* rcr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001693 emulate_2op_SrcB("rcr", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001694 break;
1695 case 4: /* sal/shl */
1696 case 6: /* sal/shl */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001697 emulate_2op_SrcB("sal", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001698 break;
1699 case 5: /* shr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001700 emulate_2op_SrcB("shr", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001701 break;
1702 case 7: /* sar */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001703 emulate_2op_SrcB("sar", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001704 break;
1705 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001706 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001707}
1708
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001709static int em_grp3(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001711 unsigned long *rax = &ctxt->regs[VCPU_REGS_RAX];
1712 unsigned long *rdx = &ctxt->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001713 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714
Avi Kivity9dac77f2011-06-01 15:34:25 +03001715 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716 case 0 ... 1: /* test */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001717 emulate_2op_SrcV("test", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001718 break;
1719 case 2: /* not */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001720 ctxt->dst.val = ~ctxt->dst.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001721 break;
1722 case 3: /* neg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001723 emulate_1op("neg", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001724 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001725 case 4: /* mul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001726 emulate_1op_rax_rdx("mul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001727 break;
1728 case 5: /* imul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001729 emulate_1op_rax_rdx("imul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001730 break;
1731 case 6: /* div */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001732 emulate_1op_rax_rdx_ex("div", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001733 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001734 break;
1735 case 7: /* idiv */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001736 emulate_1op_rax_rdx_ex("idiv", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001737 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001738 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001740 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001741 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001742 if (de)
1743 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001744 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745}
1746
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001747static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001748{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001749 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001750
Avi Kivity9dac77f2011-06-01 15:34:25 +03001751 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001752 case 0: /* inc */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001753 emulate_1op("inc", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001754 break;
1755 case 1: /* dec */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001756 emulate_1op("dec", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001758 case 2: /* call near abs */ {
1759 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001760 old_eip = ctxt->_eip;
1761 ctxt->_eip = ctxt->src.val;
1762 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001763 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001764 break;
1765 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001766 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001767 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001768 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001769 case 5: /* jmp far */
1770 rc = em_jmp_far(ctxt);
1771 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001773 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001776 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001777}
1778
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001779static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001780{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001781 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001782
Avi Kivity9dac77f2011-06-01 15:34:25 +03001783 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1784 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1785 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1786 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001787 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001788 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001789 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1790 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791
Laurent Vivier05f086f2007-09-24 11:10:55 +02001792 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001794 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001795}
1796
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001797static int em_ret(struct x86_emulate_ctxt *ctxt)
1798{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001799 ctxt->dst.type = OP_REG;
1800 ctxt->dst.addr.reg = &ctxt->_eip;
1801 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001802 return em_pop(ctxt);
1803}
1804
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001805static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001806{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001807 int rc;
1808 unsigned long cs;
1809
Avi Kivity9dac77f2011-06-01 15:34:25 +03001810 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001811 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001812 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001813 if (ctxt->op_bytes == 4)
1814 ctxt->_eip = (u32)ctxt->_eip;
1815 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001816 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001817 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001818 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001819 return rc;
1820}
1821
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001822static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001823{
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001824 unsigned short sel;
1825 int rc;
1826
Avi Kivity9dac77f2011-06-01 15:34:25 +03001827 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001828
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001829 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001830 if (rc != X86EMUL_CONTINUE)
1831 return rc;
1832
Avi Kivity9dac77f2011-06-01 15:34:25 +03001833 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001834 return rc;
1835}
1836
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001837static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001838setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001839 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001840{
Avi Kivity1aa36612011-04-27 13:20:30 +03001841 u16 selector;
1842
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001844 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001845 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001846
1847 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001848 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001849 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001850 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001851 cs->type = 0x0b; /* Read, Execute, Accessed */
1852 cs->s = 1;
1853 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001854 cs->p = 1;
1855 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001856
Gleb Natapov79168fd2010-04-28 19:15:30 +03001857 set_desc_base(ss, 0); /* flat segment */
1858 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001859 ss->g = 1; /* 4kb granularity */
1860 ss->s = 1;
1861 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001862 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001863 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001864 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001865}
1866
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001867static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001869 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001870 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001871 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001872 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001873 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001874
1875 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001876 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001877 ctxt->mode == X86EMUL_MODE_VM86)
1878 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001879
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001880 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001881 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001882
Avi Kivity717746e2011-04-20 13:37:53 +03001883 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001884 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001885 cs_sel = (u16)(msr_data & 0xfffc);
1886 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001887
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001888 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001889 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001890 cs.l = 1;
1891 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001892 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1893 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001894
Avi Kivity9dac77f2011-06-01 15:34:25 +03001895 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001896 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001897#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001898 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001899
Avi Kivity717746e2011-04-20 13:37:53 +03001900 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001901 ctxt->mode == X86EMUL_MODE_PROT64 ?
1902 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001903 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001904
Avi Kivity717746e2011-04-20 13:37:53 +03001905 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001906 ctxt->eflags &= ~(msr_data | EFLG_RF);
1907#endif
1908 } else {
1909 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001910 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001911 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001912
1913 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1914 }
1915
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001916 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001917}
1918
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001919static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001920{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001921 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001922 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001923 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001924 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001925 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001926
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001927 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001928 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001929 if (ctxt->mode == X86EMUL_MODE_REAL)
1930 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001931
1932 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1933 * Therefore, we inject an #UD.
1934 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001935 if (ctxt->mode == X86EMUL_MODE_PROT64)
1936 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001937
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001938 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001939
Avi Kivity717746e2011-04-20 13:37:53 +03001940 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001941 switch (ctxt->mode) {
1942 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001943 if ((msr_data & 0xfffc) == 0x0)
1944 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001945 break;
1946 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001947 if (msr_data == 0x0)
1948 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001949 break;
1950 }
1951
1952 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001953 cs_sel = (u16)msr_data;
1954 cs_sel &= ~SELECTOR_RPL_MASK;
1955 ss_sel = cs_sel + 8;
1956 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001957 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001958 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001959 cs.l = 1;
1960 }
1961
Avi Kivity1aa36612011-04-27 13:20:30 +03001962 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1963 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001964
Avi Kivity717746e2011-04-20 13:37:53 +03001965 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001966 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001967
Avi Kivity717746e2011-04-20 13:37:53 +03001968 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001969 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001970
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001971 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001972}
1973
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001974static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001975{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001976 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001977 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001978 u64 msr_data;
1979 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001980 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001981
Gleb Natapova0044752010-02-10 14:21:31 +02001982 /* inject #GP if in real mode or Virtual 8086 mode */
1983 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001984 ctxt->mode == X86EMUL_MODE_VM86)
1985 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001986
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001987 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001988
Avi Kivity9dac77f2011-06-01 15:34:25 +03001989 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02001990 usermode = X86EMUL_MODE_PROT64;
1991 else
1992 usermode = X86EMUL_MODE_PROT32;
1993
1994 cs.dpl = 3;
1995 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03001996 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001997 switch (usermode) {
1998 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001999 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002000 if ((msr_data & 0xfffc) == 0x0)
2001 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002002 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002003 break;
2004 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002005 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002006 if (msr_data == 0x0)
2007 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002008 ss_sel = cs_sel + 8;
2009 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002010 cs.l = 1;
2011 break;
2012 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002013 cs_sel |= SELECTOR_RPL_MASK;
2014 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002015
Avi Kivity1aa36612011-04-27 13:20:30 +03002016 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2017 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002018
Avi Kivity9dac77f2011-06-01 15:34:25 +03002019 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2020 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002021
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002022 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002023}
2024
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002025static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002026{
2027 int iopl;
2028 if (ctxt->mode == X86EMUL_MODE_REAL)
2029 return false;
2030 if (ctxt->mode == X86EMUL_MODE_VM86)
2031 return true;
2032 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002033 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002034}
2035
2036static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002037 u16 port, u16 len)
2038{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002039 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002040 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002041 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002042 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002043 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002044 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002045 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002046
Avi Kivity1aa36612011-04-27 13:20:30 +03002047 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002048 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002049 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002050 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002051 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002052 base = get_desc_base(&tr_seg);
2053#ifdef CONFIG_X86_64
2054 base |= ((u64)base3) << 32;
2055#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002056 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002057 if (r != X86EMUL_CONTINUE)
2058 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002059 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002060 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002061 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002062 if (r != X86EMUL_CONTINUE)
2063 return false;
2064 if ((perm >> bit_idx) & mask)
2065 return false;
2066 return true;
2067}
2068
2069static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002070 u16 port, u16 len)
2071{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002072 if (ctxt->perm_ok)
2073 return true;
2074
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002075 if (emulator_bad_iopl(ctxt))
2076 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002077 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002078
2079 ctxt->perm_ok = true;
2080
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002081 return true;
2082}
2083
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002084static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002085 struct tss_segment_16 *tss)
2086{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002088 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002089 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2090 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2091 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2092 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2093 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2094 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2095 tss->si = ctxt->regs[VCPU_REGS_RSI];
2096 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002097
Avi Kivity1aa36612011-04-27 13:20:30 +03002098 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2099 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2100 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2101 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2102 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002103}
2104
2105static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002106 struct tss_segment_16 *tss)
2107{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002108 int ret;
2109
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002111 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002112 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2113 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2114 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2115 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2116 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2117 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2118 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2119 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002120
2121 /*
2122 * SDM says that segment selectors are loaded before segment
2123 * descriptors
2124 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002125 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2126 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2127 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2128 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2129 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002130
2131 /*
2132 * Now load segment descriptors. If fault happenes at this stage
2133 * it is handled in a context of new task
2134 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002135 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002136 if (ret != X86EMUL_CONTINUE)
2137 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002138 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002139 if (ret != X86EMUL_CONTINUE)
2140 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002141 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002142 if (ret != X86EMUL_CONTINUE)
2143 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002144 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002145 if (ret != X86EMUL_CONTINUE)
2146 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002147 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148 if (ret != X86EMUL_CONTINUE)
2149 return ret;
2150
2151 return X86EMUL_CONTINUE;
2152}
2153
2154static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002155 u16 tss_selector, u16 old_tss_sel,
2156 ulong old_tss_base, struct desc_struct *new_desc)
2157{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002158 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002159 struct tss_segment_16 tss_seg;
2160 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002161 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162
Avi Kivity0f65dd72011-04-20 13:37:53 +03002163 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002164 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002165 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002166 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002169 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002170
Avi Kivity0f65dd72011-04-20 13:37:53 +03002171 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002172 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002173 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002174 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002175 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002176
Avi Kivity0f65dd72011-04-20 13:37:53 +03002177 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002178 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002179 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002181 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002182
2183 if (old_tss_sel != 0xffff) {
2184 tss_seg.prev_task_link = old_tss_sel;
2185
Avi Kivity0f65dd72011-04-20 13:37:53 +03002186 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002187 &tss_seg.prev_task_link,
2188 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002189 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002190 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002191 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002192 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002193 }
2194
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002195 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002196}
2197
2198static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002199 struct tss_segment_32 *tss)
2200{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002201 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002202 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002203 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002204 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2205 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2206 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2207 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2208 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2209 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2210 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2211 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002212
Avi Kivity1aa36612011-04-27 13:20:30 +03002213 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2214 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2215 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2216 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2217 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2218 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2219 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002220}
2221
2222static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002223 struct tss_segment_32 *tss)
2224{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002225 int ret;
2226
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002227 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002228 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002229 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002230 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002231 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2232 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2233 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2234 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2235 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2236 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2237 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2238 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002239
2240 /*
2241 * SDM says that segment selectors are loaded before segment
2242 * descriptors
2243 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002244 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2245 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2246 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2247 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2248 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2249 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2250 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002251
2252 /*
2253 * Now load segment descriptors. If fault happenes at this stage
2254 * it is handled in a context of new task
2255 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002256 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002257 if (ret != X86EMUL_CONTINUE)
2258 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002259 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002260 if (ret != X86EMUL_CONTINUE)
2261 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002262 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002263 if (ret != X86EMUL_CONTINUE)
2264 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002265 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002266 if (ret != X86EMUL_CONTINUE)
2267 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002268 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002269 if (ret != X86EMUL_CONTINUE)
2270 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002271 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002272 if (ret != X86EMUL_CONTINUE)
2273 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002274 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002275 if (ret != X86EMUL_CONTINUE)
2276 return ret;
2277
2278 return X86EMUL_CONTINUE;
2279}
2280
2281static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002282 u16 tss_selector, u16 old_tss_sel,
2283 ulong old_tss_base, struct desc_struct *new_desc)
2284{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002285 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286 struct tss_segment_32 tss_seg;
2287 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002288 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002289
Avi Kivity0f65dd72011-04-20 13:37:53 +03002290 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002291 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002292 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002293 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002295
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002296 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002297
Avi Kivity0f65dd72011-04-20 13:37:53 +03002298 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002299 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002300 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002301 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002302 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002303
Avi Kivity0f65dd72011-04-20 13:37:53 +03002304 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002305 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002306 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002307 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002308 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002309
2310 if (old_tss_sel != 0xffff) {
2311 tss_seg.prev_task_link = old_tss_sel;
2312
Avi Kivity0f65dd72011-04-20 13:37:53 +03002313 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002314 &tss_seg.prev_task_link,
2315 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002316 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002317 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002319 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002320 }
2321
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002322 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002323}
2324
2325static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002326 u16 tss_selector, int reason,
2327 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002329 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002330 struct desc_struct curr_tss_desc, next_tss_desc;
2331 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002332 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002333 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002334 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002335 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002336
2337 /* FIXME: old_tss_base == ~0 ? */
2338
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002339 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002340 if (ret != X86EMUL_CONTINUE)
2341 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002342 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002343 if (ret != X86EMUL_CONTINUE)
2344 return ret;
2345
2346 /* FIXME: check that next_tss_desc is tss */
2347
2348 if (reason != TASK_SWITCH_IRET) {
2349 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002350 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002351 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002352 }
2353
Gleb Natapovceffb452010-03-18 15:20:19 +02002354 desc_limit = desc_limit_scaled(&next_tss_desc);
2355 if (!next_tss_desc.p ||
2356 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2357 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002358 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002359 return X86EMUL_PROPAGATE_FAULT;
2360 }
2361
2362 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2363 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002364 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002365 }
2366
2367 if (reason == TASK_SWITCH_IRET)
2368 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2369
2370 /* set back link to prev task only if NT bit is set in eflags
2371 note that old_tss_sel is not used afetr this point */
2372 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2373 old_tss_sel = 0xffff;
2374
2375 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002376 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002377 old_tss_base, &next_tss_desc);
2378 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002379 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002380 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002381 if (ret != X86EMUL_CONTINUE)
2382 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002383
2384 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2385 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2386
2387 if (reason != TASK_SWITCH_IRET) {
2388 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002389 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002390 }
2391
Avi Kivity717746e2011-04-20 13:37:53 +03002392 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002393 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002394
Jan Kiszkae269fb22010-04-14 15:51:09 +02002395 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002396 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2397 ctxt->lock_prefix = 0;
2398 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002399 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002400 }
2401
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002402 return ret;
2403}
2404
2405int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002406 u16 tss_selector, int reason,
2407 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002408{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 int rc;
2410
Avi Kivity9dac77f2011-06-01 15:34:25 +03002411 ctxt->_eip = ctxt->eip;
2412 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002413
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002414 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002415 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002416
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002417 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002418 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002420 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002421}
2422
Avi Kivity90de84f2010-11-17 15:28:21 +02002423static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002424 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002425{
Gleb Natapova682e352010-03-18 15:20:21 +02002426 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2427
Avi Kivity9dac77f2011-06-01 15:34:25 +03002428 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2429 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002430 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002431}
2432
Avi Kivity7af04fc2010-08-18 14:16:35 +03002433static int em_das(struct x86_emulate_ctxt *ctxt)
2434{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002435 u8 al, old_al;
2436 bool af, cf, old_cf;
2437
2438 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002439 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002440
2441 old_al = al;
2442 old_cf = cf;
2443 cf = false;
2444 af = ctxt->eflags & X86_EFLAGS_AF;
2445 if ((al & 0x0f) > 9 || af) {
2446 al -= 6;
2447 cf = old_cf | (al >= 250);
2448 af = true;
2449 } else {
2450 af = false;
2451 }
2452 if (old_al > 0x99 || old_cf) {
2453 al -= 0x60;
2454 cf = true;
2455 }
2456
Avi Kivity9dac77f2011-06-01 15:34:25 +03002457 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002458 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002459 ctxt->src.type = OP_IMM;
2460 ctxt->src.val = 0;
2461 ctxt->src.bytes = 1;
2462 emulate_2op_SrcV("or", ctxt->src, ctxt->dst, ctxt->eflags);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002463 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2464 if (cf)
2465 ctxt->eflags |= X86_EFLAGS_CF;
2466 if (af)
2467 ctxt->eflags |= X86_EFLAGS_AF;
2468 return X86EMUL_CONTINUE;
2469}
2470
Avi Kivity0ef753b2010-08-18 14:51:45 +03002471static int em_call_far(struct x86_emulate_ctxt *ctxt)
2472{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002473 u16 sel, old_cs;
2474 ulong old_eip;
2475 int rc;
2476
Avi Kivity1aa36612011-04-27 13:20:30 +03002477 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002478 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002479
Avi Kivity9dac77f2011-06-01 15:34:25 +03002480 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002481 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002482 return X86EMUL_CONTINUE;
2483
Avi Kivity9dac77f2011-06-01 15:34:25 +03002484 ctxt->_eip = 0;
2485 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002486
Avi Kivity9dac77f2011-06-01 15:34:25 +03002487 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002488 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002489 if (rc != X86EMUL_CONTINUE)
2490 return rc;
2491
Avi Kivity9dac77f2011-06-01 15:34:25 +03002492 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002493 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002494}
2495
Avi Kivity40ece7c2010-08-18 15:12:09 +03002496static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2497{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002498 int rc;
2499
Avi Kivity9dac77f2011-06-01 15:34:25 +03002500 ctxt->dst.type = OP_REG;
2501 ctxt->dst.addr.reg = &ctxt->_eip;
2502 ctxt->dst.bytes = ctxt->op_bytes;
2503 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002504 if (rc != X86EMUL_CONTINUE)
2505 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002506 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002507 return X86EMUL_CONTINUE;
2508}
2509
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002510static int em_add(struct x86_emulate_ctxt *ctxt)
2511{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002512 emulate_2op_SrcV("add", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002513 return X86EMUL_CONTINUE;
2514}
2515
2516static int em_or(struct x86_emulate_ctxt *ctxt)
2517{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002518 emulate_2op_SrcV("or", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002519 return X86EMUL_CONTINUE;
2520}
2521
2522static int em_adc(struct x86_emulate_ctxt *ctxt)
2523{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002524 emulate_2op_SrcV("adc", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002525 return X86EMUL_CONTINUE;
2526}
2527
2528static int em_sbb(struct x86_emulate_ctxt *ctxt)
2529{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002530 emulate_2op_SrcV("sbb", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002531 return X86EMUL_CONTINUE;
2532}
2533
2534static int em_and(struct x86_emulate_ctxt *ctxt)
2535{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002536 emulate_2op_SrcV("and", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002537 return X86EMUL_CONTINUE;
2538}
2539
2540static int em_sub(struct x86_emulate_ctxt *ctxt)
2541{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002542 emulate_2op_SrcV("sub", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002543 return X86EMUL_CONTINUE;
2544}
2545
2546static int em_xor(struct x86_emulate_ctxt *ctxt)
2547{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002548 emulate_2op_SrcV("xor", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002549 return X86EMUL_CONTINUE;
2550}
2551
2552static int em_cmp(struct x86_emulate_ctxt *ctxt)
2553{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002554 emulate_2op_SrcV("cmp", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002555 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002556 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002557 return X86EMUL_CONTINUE;
2558}
2559
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002560static int em_test(struct x86_emulate_ctxt *ctxt)
2561{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002562 emulate_2op_SrcV("test", ctxt->src, ctxt->dst, ctxt->eflags);
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002563 return X86EMUL_CONTINUE;
2564}
2565
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002566static int em_xchg(struct x86_emulate_ctxt *ctxt)
2567{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002568 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002569 ctxt->src.val = ctxt->dst.val;
2570 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002571
2572 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002573 ctxt->dst.val = ctxt->src.orig_val;
2574 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002575 return X86EMUL_CONTINUE;
2576}
2577
Avi Kivity5c82aa22010-08-18 18:31:43 +03002578static int em_imul(struct x86_emulate_ctxt *ctxt)
2579{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002580 emulate_2op_SrcV_nobyte("imul", ctxt->src, ctxt->dst, ctxt->eflags);
Avi Kivity5c82aa22010-08-18 18:31:43 +03002581 return X86EMUL_CONTINUE;
2582}
2583
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002584static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2585{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002586 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002587 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002588}
2589
Avi Kivity61429142010-08-19 15:13:00 +03002590static int em_cwd(struct x86_emulate_ctxt *ctxt)
2591{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002592 ctxt->dst.type = OP_REG;
2593 ctxt->dst.bytes = ctxt->src.bytes;
2594 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2595 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002596
2597 return X86EMUL_CONTINUE;
2598}
2599
Avi Kivity48bb5d32010-08-18 18:54:34 +03002600static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2601{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002602 u64 tsc = 0;
2603
Avi Kivity717746e2011-04-20 13:37:53 +03002604 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002605 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2606 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002607 return X86EMUL_CONTINUE;
2608}
2609
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002610static int em_mov(struct x86_emulate_ctxt *ctxt)
2611{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002612 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002613 return X86EMUL_CONTINUE;
2614}
2615
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002616static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2617{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002618 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002619 return emulate_ud(ctxt);
2620
Avi Kivity9dac77f2011-06-01 15:34:25 +03002621 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002622 return X86EMUL_CONTINUE;
2623}
2624
2625static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2626{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002627 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002628
Avi Kivity9dac77f2011-06-01 15:34:25 +03002629 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002630 return emulate_ud(ctxt);
2631
Avi Kivity9dac77f2011-06-01 15:34:25 +03002632 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002633 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2634
2635 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002636 ctxt->dst.type = OP_NONE;
2637 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002638}
2639
Avi Kivityaa97bb42010-01-20 18:09:23 +02002640static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2641{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002642 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002643 return X86EMUL_CONTINUE;
2644}
2645
Avi Kivity38503912011-03-31 18:48:09 +02002646static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2647{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002648 int rc;
2649 ulong linear;
2650
Avi Kivity9dac77f2011-06-01 15:34:25 +03002651 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002652 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002653 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002654 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002655 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002656 return X86EMUL_CONTINUE;
2657}
2658
Avi Kivity2d04a052011-04-20 15:32:49 +03002659static int em_clts(struct x86_emulate_ctxt *ctxt)
2660{
2661 ulong cr0;
2662
2663 cr0 = ctxt->ops->get_cr(ctxt, 0);
2664 cr0 &= ~X86_CR0_TS;
2665 ctxt->ops->set_cr(ctxt, 0, cr0);
2666 return X86EMUL_CONTINUE;
2667}
2668
Avi Kivity26d05cc2011-04-21 12:07:59 +03002669static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2670{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002671 int rc;
2672
Avi Kivity9dac77f2011-06-01 15:34:25 +03002673 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002674 return X86EMUL_UNHANDLEABLE;
2675
2676 rc = ctxt->ops->fix_hypercall(ctxt);
2677 if (rc != X86EMUL_CONTINUE)
2678 return rc;
2679
2680 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002681 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002682 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002683 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002684 return X86EMUL_CONTINUE;
2685}
2686
2687static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2688{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002689 struct desc_ptr desc_ptr;
2690 int rc;
2691
Avi Kivity9dac77f2011-06-01 15:34:25 +03002692 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002693 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002694 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002695 if (rc != X86EMUL_CONTINUE)
2696 return rc;
2697 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2698 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002699 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002700 return X86EMUL_CONTINUE;
2701}
2702
Avi Kivity5ef39c72011-04-21 12:21:50 +03002703static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002704{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002705 int rc;
2706
Avi Kivity5ef39c72011-04-21 12:21:50 +03002707 rc = ctxt->ops->fix_hypercall(ctxt);
2708
Avi Kivity26d05cc2011-04-21 12:07:59 +03002709 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002710 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002711 return rc;
2712}
2713
2714static int em_lidt(struct x86_emulate_ctxt *ctxt)
2715{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002716 struct desc_ptr desc_ptr;
2717 int rc;
2718
Avi Kivity9dac77f2011-06-01 15:34:25 +03002719 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002720 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002721 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002722 if (rc != X86EMUL_CONTINUE)
2723 return rc;
2724 ctxt->ops->set_idt(ctxt, &desc_ptr);
2725 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002726 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002727 return X86EMUL_CONTINUE;
2728}
2729
2730static int em_smsw(struct x86_emulate_ctxt *ctxt)
2731{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002732 ctxt->dst.bytes = 2;
2733 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002734 return X86EMUL_CONTINUE;
2735}
2736
2737static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2738{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002739 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002740 | (ctxt->src.val & 0x0f));
2741 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002742 return X86EMUL_CONTINUE;
2743}
2744
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002745static int em_loop(struct x86_emulate_ctxt *ctxt)
2746{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002747 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2748 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2749 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2750 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002751
2752 return X86EMUL_CONTINUE;
2753}
2754
2755static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2756{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002757 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2758 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002759
2760 return X86EMUL_CONTINUE;
2761}
2762
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002763static int em_cli(struct x86_emulate_ctxt *ctxt)
2764{
2765 if (emulator_bad_iopl(ctxt))
2766 return emulate_gp(ctxt, 0);
2767
2768 ctxt->eflags &= ~X86_EFLAGS_IF;
2769 return X86EMUL_CONTINUE;
2770}
2771
2772static int em_sti(struct x86_emulate_ctxt *ctxt)
2773{
2774 if (emulator_bad_iopl(ctxt))
2775 return emulate_gp(ctxt, 0);
2776
2777 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2778 ctxt->eflags |= X86_EFLAGS_IF;
2779 return X86EMUL_CONTINUE;
2780}
2781
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002782static bool valid_cr(int nr)
2783{
2784 switch (nr) {
2785 case 0:
2786 case 2 ... 4:
2787 case 8:
2788 return true;
2789 default:
2790 return false;
2791 }
2792}
2793
2794static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2795{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002796 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002797 return emulate_ud(ctxt);
2798
2799 return X86EMUL_CONTINUE;
2800}
2801
2802static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2803{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002804 u64 new_val = ctxt->src.val64;
2805 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002806 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002807
2808 static u64 cr_reserved_bits[] = {
2809 0xffffffff00000000ULL,
2810 0, 0, 0, /* CR3 checked later */
2811 CR4_RESERVED_BITS,
2812 0, 0, 0,
2813 CR8_RESERVED_BITS,
2814 };
2815
2816 if (!valid_cr(cr))
2817 return emulate_ud(ctxt);
2818
2819 if (new_val & cr_reserved_bits[cr])
2820 return emulate_gp(ctxt, 0);
2821
2822 switch (cr) {
2823 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002824 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002825 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2826 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2827 return emulate_gp(ctxt, 0);
2828
Avi Kivity717746e2011-04-20 13:37:53 +03002829 cr4 = ctxt->ops->get_cr(ctxt, 4);
2830 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002831
2832 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2833 !(cr4 & X86_CR4_PAE))
2834 return emulate_gp(ctxt, 0);
2835
2836 break;
2837 }
2838 case 3: {
2839 u64 rsvd = 0;
2840
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002841 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2842 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002843 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002844 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002845 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002846 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002847 rsvd = CR3_NONPAE_RESERVED_BITS;
2848
2849 if (new_val & rsvd)
2850 return emulate_gp(ctxt, 0);
2851
2852 break;
2853 }
2854 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002855 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002856
Avi Kivity717746e2011-04-20 13:37:53 +03002857 cr4 = ctxt->ops->get_cr(ctxt, 4);
2858 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002859
2860 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2861 return emulate_gp(ctxt, 0);
2862
2863 break;
2864 }
2865 }
2866
2867 return X86EMUL_CONTINUE;
2868}
2869
Joerg Roedel3b88e412011-04-04 12:39:29 +02002870static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2871{
2872 unsigned long dr7;
2873
Avi Kivity717746e2011-04-20 13:37:53 +03002874 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002875
2876 /* Check if DR7.Global_Enable is set */
2877 return dr7 & (1 << 13);
2878}
2879
2880static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2881{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002882 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002883 u64 cr4;
2884
2885 if (dr > 7)
2886 return emulate_ud(ctxt);
2887
Avi Kivity717746e2011-04-20 13:37:53 +03002888 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002889 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2890 return emulate_ud(ctxt);
2891
2892 if (check_dr7_gd(ctxt))
2893 return emulate_db(ctxt);
2894
2895 return X86EMUL_CONTINUE;
2896}
2897
2898static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2899{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002900 u64 new_val = ctxt->src.val64;
2901 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002902
2903 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2904 return emulate_gp(ctxt, 0);
2905
2906 return check_dr_read(ctxt);
2907}
2908
Joerg Roedel01de8b02011-04-04 12:39:31 +02002909static int check_svme(struct x86_emulate_ctxt *ctxt)
2910{
2911 u64 efer;
2912
Avi Kivity717746e2011-04-20 13:37:53 +03002913 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002914
2915 if (!(efer & EFER_SVME))
2916 return emulate_ud(ctxt);
2917
2918 return X86EMUL_CONTINUE;
2919}
2920
2921static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2922{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002923 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002924
2925 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002926 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002927 return emulate_gp(ctxt, 0);
2928
2929 return check_svme(ctxt);
2930}
2931
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002932static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2933{
Avi Kivity717746e2011-04-20 13:37:53 +03002934 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002935
Avi Kivity717746e2011-04-20 13:37:53 +03002936 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002937 return emulate_ud(ctxt);
2938
2939 return X86EMUL_CONTINUE;
2940}
2941
Joerg Roedel80612522011-04-04 12:39:33 +02002942static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2943{
Avi Kivity717746e2011-04-20 13:37:53 +03002944 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002945 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002946
Avi Kivity717746e2011-04-20 13:37:53 +03002947 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002948 (rcx > 3))
2949 return emulate_gp(ctxt, 0);
2950
2951 return X86EMUL_CONTINUE;
2952}
2953
Joerg Roedelf6511932011-04-04 12:39:35 +02002954static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2955{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002956 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2957 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002958 return emulate_gp(ctxt, 0);
2959
2960 return X86EMUL_CONTINUE;
2961}
2962
2963static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2964{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002965 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2966 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002967 return emulate_gp(ctxt, 0);
2968
2969 return X86EMUL_CONTINUE;
2970}
2971
Avi Kivity73fba5f2010-07-29 15:11:53 +03002972#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002973#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002974#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2975 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002976#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002977#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002978#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002979#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002980#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002981#define II(_f, _e, _i) \
2982 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002983#define IIP(_f, _e, _i, _p) \
2984 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2985 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002986#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002987
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002988#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002989#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002990#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2991
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002992#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2993 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2994 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002995
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002996static struct opcode group7_rm1[] = {
2997 DI(SrcNone | ModRM | Priv, monitor),
2998 DI(SrcNone | ModRM | Priv, mwait),
2999 N, N, N, N, N, N,
3000};
3001
Joerg Roedel01de8b02011-04-04 12:39:31 +02003002static struct opcode group7_rm3[] = {
3003 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003004 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003005 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3006 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3007 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3008 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3009 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3010 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3011};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003012
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003013static struct opcode group7_rm7[] = {
3014 N,
3015 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3016 N, N, N, N, N, N,
3017};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003018
Avi Kivity73fba5f2010-07-29 15:11:53 +03003019static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003020 I(Lock, em_add),
3021 I(Lock, em_or),
3022 I(Lock, em_adc),
3023 I(Lock, em_sbb),
3024 I(Lock, em_and),
3025 I(Lock, em_sub),
3026 I(Lock, em_xor),
3027 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003028};
3029
3030static struct opcode group1A[] = {
3031 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3032};
3033
3034static struct opcode group3[] = {
3035 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
3036 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03003037 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003038};
3039
3040static struct opcode group4[] = {
3041 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3042 N, N, N, N, N, N,
3043};
3044
3045static struct opcode group5[] = {
3046 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003047 D(SrcMem | ModRM | Stack),
3048 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003049 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3050 D(SrcMem | ModRM | Stack), N,
3051};
3052
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003053static struct opcode group6[] = {
3054 DI(ModRM | Prot, sldt),
3055 DI(ModRM | Prot, str),
3056 DI(ModRM | Prot | Priv, lldt),
3057 DI(ModRM | Prot | Priv, ltr),
3058 N, N, N, N,
3059};
3060
Avi Kivity73fba5f2010-07-29 15:11:53 +03003061static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003062 DI(ModRM | Mov | DstMem | Priv, sgdt),
3063 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003064 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3065 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3066 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3067 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3068 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003069}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003070 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3071 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003072 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003073 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3074 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003075} };
3076
3077static struct opcode group8[] = {
3078 N, N, N, N,
3079 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3080 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3081};
3082
3083static struct group_dual group9 = { {
3084 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3085}, {
3086 N, N, N, N, N, N, N, N,
3087} };
3088
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003089static struct opcode group11[] = {
3090 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3091};
3092
Avi Kivityaa97bb42010-01-20 18:09:23 +02003093static struct gprefix pfx_0f_6f_0f_7f = {
3094 N, N, N, I(Sse, em_movdqu),
3095};
3096
Avi Kivity73fba5f2010-07-29 15:11:53 +03003097static struct opcode opcode_table[256] = {
3098 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003099 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003100 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3101 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003102 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003103 D(ImplicitOps | Stack | No64), N,
3104 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003105 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003106 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3107 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003108 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003109 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3110 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003111 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003112 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003113 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003114 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003115 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003116 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003117 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003118 /* 0x40 - 0x4F */
3119 X16(D(DstReg)),
3120 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003121 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003122 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003123 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003124 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003125 I(ImplicitOps | Stack | No64, em_pusha),
3126 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003127 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3128 N, N, N, N,
3129 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003130 I(SrcImm | Mov | Stack, em_push),
3131 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003132 I(SrcImmByte | Mov | Stack, em_push),
3133 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003134 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3135 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003136 /* 0x70 - 0x7F */
3137 X16(D(SrcImmByte)),
3138 /* 0x80 - 0x87 */
3139 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3140 G(DstMem | SrcImm | ModRM | Group, group1),
3141 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3142 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003143 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003144 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003145 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003146 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3147 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003148 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3149 D(ModRM | SrcMem | NoAccess | DstReg),
3150 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3151 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003152 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003153 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003154 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003155 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003156 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003157 II(ImplicitOps | Stack, em_pushf, pushf),
3158 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003159 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003160 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3161 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3162 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003163 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003164 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003165 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003166 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3167 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003168 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003169 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003170 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003171 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003172 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003173 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003174 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003175 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003176 I(ImplicitOps | Stack, em_ret),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003177 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003178 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003179 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003180 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003181 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003182 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003183 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003184 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003185 N, N, N, N,
3186 /* 0xD8 - 0xDF */
3187 N, N, N, N, N, N, N, N,
3188 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003189 X3(I(SrcImmByte, em_loop)),
3190 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003191 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3192 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003193 /* 0xE8 - 0xEF */
3194 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003195 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003196 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3197 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003198 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003199 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003200 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3201 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003202 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003203 D(ImplicitOps), D(ImplicitOps),
3204 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003205 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3206};
3207
3208static struct opcode twobyte_table[256] = {
3209 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003210 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003211 N, I(ImplicitOps | VendorSpecific, em_syscall),
3212 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003213 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003214 N, D(ImplicitOps | ModRM), N, N,
3215 /* 0x10 - 0x1F */
3216 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3217 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003218 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003219 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003220 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003221 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003222 N, N, N, N,
3223 N, N, N, N, N, N, N, N,
3224 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003225 DI(ImplicitOps | Priv, wrmsr),
3226 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3227 DI(ImplicitOps | Priv, rdmsr),
3228 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003229 I(ImplicitOps | VendorSpecific, em_sysenter),
3230 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003231 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003232 N, N, N, N, N, N, N, N,
3233 /* 0x40 - 0x4F */
3234 X16(D(DstReg | SrcMem | ModRM | Mov)),
3235 /* 0x50 - 0x5F */
3236 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3237 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003238 N, N, N, N,
3239 N, N, N, N,
3240 N, N, N, N,
3241 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003242 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003243 N, N, N, N,
3244 N, N, N, N,
3245 N, N, N, N,
3246 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003247 /* 0x80 - 0x8F */
3248 X16(D(SrcImm)),
3249 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003250 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003251 /* 0xA0 - 0xA7 */
3252 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003253 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003254 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3255 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3256 /* 0xA8 - 0xAF */
3257 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003258 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003259 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3260 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003261 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003262 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003263 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003264 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3265 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3266 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003267 /* 0xB8 - 0xBF */
3268 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003269 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003270 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3271 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003272 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003273 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003274 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003275 N, N, N, GD(0, &group9),
3276 N, N, N, N, N, N, N, N,
3277 /* 0xD0 - 0xDF */
3278 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3279 /* 0xE0 - 0xEF */
3280 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3281 /* 0xF0 - 0xFF */
3282 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3283};
3284
3285#undef D
3286#undef N
3287#undef G
3288#undef GD
3289#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003290#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003291#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003292
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003293#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003294#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003295#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003296#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003297
Avi Kivity9dac77f2011-06-01 15:34:25 +03003298static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003299{
3300 unsigned size;
3301
Avi Kivity9dac77f2011-06-01 15:34:25 +03003302 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003303 if (size == 8)
3304 size = 4;
3305 return size;
3306}
3307
3308static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3309 unsigned size, bool sign_extension)
3310{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003311 int rc = X86EMUL_CONTINUE;
3312
3313 op->type = OP_IMM;
3314 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003316 /* NB. Immediates are sign-extended as necessary. */
3317 switch (op->bytes) {
3318 case 1:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003319 op->val = insn_fetch(s8, 1, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003320 break;
3321 case 2:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003322 op->val = insn_fetch(s16, 2, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003323 break;
3324 case 4:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003325 op->val = insn_fetch(s32, 4, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003326 break;
3327 }
3328 if (!sign_extension) {
3329 switch (op->bytes) {
3330 case 1:
3331 op->val &= 0xff;
3332 break;
3333 case 2:
3334 op->val &= 0xffff;
3335 break;
3336 case 4:
3337 op->val &= 0xffffffff;
3338 break;
3339 }
3340 }
3341done:
3342 return rc;
3343}
3344
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003345int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003346{
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003347 int rc = X86EMUL_CONTINUE;
3348 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003349 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003350 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003351 struct opcode opcode;
Avi Kivitycb16c342011-06-19 19:21:11 +03003352 struct operand memop = { .type = OP_NONE }, *memopp = NULL;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003353
Avi Kivity9dac77f2011-06-01 15:34:25 +03003354 ctxt->_eip = ctxt->eip;
3355 ctxt->fetch.start = ctxt->_eip;
3356 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003357 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003358 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003359
3360 switch (mode) {
3361 case X86EMUL_MODE_REAL:
3362 case X86EMUL_MODE_VM86:
3363 case X86EMUL_MODE_PROT16:
3364 def_op_bytes = def_ad_bytes = 2;
3365 break;
3366 case X86EMUL_MODE_PROT32:
3367 def_op_bytes = def_ad_bytes = 4;
3368 break;
3369#ifdef CONFIG_X86_64
3370 case X86EMUL_MODE_PROT64:
3371 def_op_bytes = 4;
3372 def_ad_bytes = 8;
3373 break;
3374#endif
3375 default:
3376 return -1;
3377 }
3378
Avi Kivity9dac77f2011-06-01 15:34:25 +03003379 ctxt->op_bytes = def_op_bytes;
3380 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003381
3382 /* Legacy prefixes. */
3383 for (;;) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003384 switch (ctxt->b = insn_fetch(u8, 1, ctxt)) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003385 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003386 op_prefix = true;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003387 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003388 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003389 break;
3390 case 0x67: /* address-size override */
3391 if (mode == X86EMUL_MODE_PROT64)
3392 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003393 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003394 else
3395 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003396 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003397 break;
3398 case 0x26: /* ES override */
3399 case 0x2e: /* CS override */
3400 case 0x36: /* SS override */
3401 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003402 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003403 break;
3404 case 0x64: /* FS override */
3405 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003406 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003407 break;
3408 case 0x40 ... 0x4f: /* REX */
3409 if (mode != X86EMUL_MODE_PROT64)
3410 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003411 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003412 continue;
3413 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003414 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003415 break;
3416 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003417 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003418 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003419 break;
3420 default:
3421 goto done_prefixes;
3422 }
3423
3424 /* Any legacy prefix after a REX prefix nullifies its effect. */
3425
Avi Kivity9dac77f2011-06-01 15:34:25 +03003426 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003427 }
3428
3429done_prefixes:
3430
3431 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003432 if (ctxt->rex_prefix & 8)
3433 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003434
3435 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003437 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003438 if (ctxt->b == 0x0f) {
3439 ctxt->twobyte = 1;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003440 ctxt->b = insn_fetch(u8, 1, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003441 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003442 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003443 ctxt->d = opcode.flags;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003444
Avi Kivity9dac77f2011-06-01 15:34:25 +03003445 while (ctxt->d & GroupMask) {
3446 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003447 case Group:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003448 ctxt->modrm = insn_fetch(u8, 1, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003449 --ctxt->_eip;
3450 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003451 opcode = opcode.u.group[goffset];
3452 break;
3453 case GroupDual:
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003454 ctxt->modrm = insn_fetch(u8, 1, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003455 --ctxt->_eip;
3456 goffset = (ctxt->modrm >> 3) & 7;
3457 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003458 opcode = opcode.u.gdual->mod3[goffset];
3459 else
3460 opcode = opcode.u.gdual->mod012[goffset];
3461 break;
3462 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003463 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003464 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003465 break;
3466 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003467 if (ctxt->rep_prefix && op_prefix)
Avi Kivity46561642011-04-24 14:09:59 +03003468 return X86EMUL_UNHANDLEABLE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003469 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003470 switch (simd_prefix) {
3471 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3472 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3473 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3474 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3475 }
3476 break;
3477 default:
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003478 return X86EMUL_UNHANDLEABLE;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003479 }
Avi Kivity46561642011-04-24 14:09:59 +03003480
Avi Kivity9dac77f2011-06-01 15:34:25 +03003481 ctxt->d &= ~GroupMask;
3482 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003483 }
3484
Avi Kivity9dac77f2011-06-01 15:34:25 +03003485 ctxt->execute = opcode.u.execute;
3486 ctxt->check_perm = opcode.check_perm;
3487 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003488
3489 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003490 if (ctxt->d == 0 || (ctxt->d & Undefined))
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003491 return -1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003492
Avi Kivity9dac77f2011-06-01 15:34:25 +03003493 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Avi Kivityd8671622011-02-01 16:32:03 +02003494 return -1;
3495
Avi Kivity9dac77f2011-06-01 15:34:25 +03003496 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3497 ctxt->op_bytes = 8;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003498
Avi Kivity9dac77f2011-06-01 15:34:25 +03003499 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003500 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003501 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003502 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003503 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003504 }
3505
Avi Kivity9dac77f2011-06-01 15:34:25 +03003506 if (ctxt->d & Sse)
3507 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003508
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003509 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003510 if (ctxt->d & ModRM) {
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003511 rc = decode_modrm(ctxt, &memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003512 if (!ctxt->has_seg_override)
3513 set_seg_override(ctxt, ctxt->modrm_seg);
3514 } else if (ctxt->d & MemAbs)
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003515 rc = decode_abs(ctxt, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003516 if (rc != X86EMUL_CONTINUE)
3517 goto done;
3518
Avi Kivity9dac77f2011-06-01 15:34:25 +03003519 if (!ctxt->has_seg_override)
3520 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003521
Avi Kivity9dac77f2011-06-01 15:34:25 +03003522 memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003523
Avi Kivity9dac77f2011-06-01 15:34:25 +03003524 if (memop.type == OP_MEM && ctxt->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003525 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003526
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003527 /*
3528 * Decode and fetch the source operand: register, memory
3529 * or immediate.
3530 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003531 switch (ctxt->d & SrcMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003532 case SrcNone:
3533 break;
3534 case SrcReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003535 decode_register_operand(ctxt, &ctxt->src, 0);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003536 break;
3537 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003538 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003539 goto srcmem_common;
3540 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003541 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003542 goto srcmem_common;
3543 case SrcMem:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003544 memop.bytes = (ctxt->d & ByteOp) ? 1 :
3545 ctxt->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003546 srcmem_common:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003547 ctxt->src = memop;
3548 memopp = &ctxt->src;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003549 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003550 case SrcImmU16:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003551 rc = decode_imm(ctxt, &ctxt->src, 2, false);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003552 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003553 case SrcImm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003554 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003555 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003556 case SrcImmU:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003557 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003558 break;
3559 case SrcImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003560 rc = decode_imm(ctxt, &ctxt->src, 1, true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003561 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003562 case SrcImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003563 rc = decode_imm(ctxt, &ctxt->src, 1, false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003564 break;
3565 case SrcAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003566 ctxt->src.type = OP_REG;
3567 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3568 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3569 fetch_register_operand(&ctxt->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003570 break;
3571 case SrcOne:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003572 ctxt->src.bytes = 1;
3573 ctxt->src.val = 1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003574 break;
3575 case SrcSI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003576 ctxt->src.type = OP_MEM;
3577 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3578 ctxt->src.addr.mem.ea =
3579 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3580 ctxt->src.addr.mem.seg = seg_override(ctxt);
3581 ctxt->src.val = 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003582 break;
3583 case SrcImmFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003584 ctxt->src.type = OP_IMM;
3585 ctxt->src.addr.mem.ea = ctxt->_eip;
3586 ctxt->src.bytes = ctxt->op_bytes + 2;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003587 insn_fetch_arr(ctxt->src.valptr, ctxt->src.bytes, ctxt);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003588 break;
3589 case SrcMemFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003590 memop.bytes = ctxt->op_bytes + 2;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003591 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003592 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003593 case SrcDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003594 ctxt->src.type = OP_REG;
3595 ctxt->src.bytes = 2;
3596 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3597 fetch_register_operand(&ctxt->src);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003598 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003599 }
3600
Avi Kivity39f21ee2010-08-18 19:20:21 +03003601 if (rc != X86EMUL_CONTINUE)
3602 goto done;
3603
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003604 /*
3605 * Decode and fetch the second source operand: register, memory
3606 * or immediate.
3607 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003608 switch (ctxt->d & Src2Mask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003609 case Src2None:
3610 break;
3611 case Src2CL:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003612 ctxt->src2.bytes = 1;
Avi Kivity9be3be12011-09-13 10:45:38 +03003613 ctxt->src2.val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003614 break;
3615 case Src2ImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003616 rc = decode_imm(ctxt, &ctxt->src2, 1, true);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003617 break;
3618 case Src2One:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003619 ctxt->src2.bytes = 1;
3620 ctxt->src2.val = 1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003621 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003622 case Src2Imm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003623 rc = decode_imm(ctxt, &ctxt->src2, imm_size(ctxt), true);
Avi Kivity7db41eb2010-08-18 19:25:28 +03003624 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003625 }
3626
Avi Kivity39f21ee2010-08-18 19:20:21 +03003627 if (rc != X86EMUL_CONTINUE)
3628 goto done;
3629
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003630 /* Decode and fetch the destination operand: register or memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003631 switch (ctxt->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003632 case DstReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003633 decode_register_operand(ctxt, &ctxt->dst,
3634 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003635 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003636 case DstImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003637 ctxt->dst.type = OP_IMM;
3638 ctxt->dst.addr.mem.ea = ctxt->_eip;
3639 ctxt->dst.bytes = 1;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003640 ctxt->dst.val = insn_fetch(u8, 1, ctxt);
Wei Yongjun943858e2010-08-06 11:36:51 +08003641 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003642 case DstMem:
3643 case DstMem64:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003644 ctxt->dst = memop;
3645 memopp = &ctxt->dst;
3646 if ((ctxt->d & DstMask) == DstMem64)
3647 ctxt->dst.bytes = 8;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003648 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003649 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3650 if (ctxt->d & BitOp)
3651 fetch_bit_operand(ctxt);
3652 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003653 break;
3654 case DstAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003655 ctxt->dst.type = OP_REG;
3656 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3657 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3658 fetch_register_operand(&ctxt->dst);
3659 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003660 break;
3661 case DstDI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003662 ctxt->dst.type = OP_MEM;
3663 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3664 ctxt->dst.addr.mem.ea =
3665 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3666 ctxt->dst.addr.mem.seg = VCPU_SREG_ES;
3667 ctxt->dst.val = 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003668 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003669 case DstDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003670 ctxt->dst.type = OP_REG;
3671 ctxt->dst.bytes = 2;
3672 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3673 fetch_register_operand(&ctxt->dst);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003674 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003675 case ImplicitOps:
3676 /* Special instructions do their own operand decoding. */
3677 default:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003678 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivitycb16c342011-06-19 19:21:11 +03003679 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003680 }
3681
3682done:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003683 if (memopp && memopp->type == OP_MEM && ctxt->rip_relative)
3684 memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003685
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003686 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003687}
3688
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003689static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3690{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003691 /* The second termination condition only applies for REPE
3692 * and REPNE. Test if the repeat string operation prefix is
3693 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3694 * corresponding termination condition according to:
3695 * - if REPE/REPZ and ZF = 0 then done
3696 * - if REPNE/REPNZ and ZF = 1 then done
3697 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003698 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3699 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3700 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003701 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003702 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003703 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3704 return true;
3705
3706 return false;
3707}
3708
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003709int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003710{
Avi Kivity9aabc882010-07-29 15:11:50 +03003711 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003712 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003713 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003714 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003715
Avi Kivity9dac77f2011-06-01 15:34:25 +03003716 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003717
Avi Kivity9dac77f2011-06-01 15:34:25 +03003718 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003719 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003720 goto done;
3721 }
3722
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003723 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003724 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003725 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003726 goto done;
3727 }
3728
Avi Kivity9dac77f2011-06-01 15:34:25 +03003729 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003730 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003731 goto done;
3732 }
3733
Avi Kivity9dac77f2011-06-01 15:34:25 +03003734 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003735 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3736 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003737 rc = emulate_ud(ctxt);
3738 goto done;
3739 }
3740
Avi Kivity9dac77f2011-06-01 15:34:25 +03003741 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003742 rc = emulate_nm(ctxt);
3743 goto done;
3744 }
3745
Avi Kivity9dac77f2011-06-01 15:34:25 +03003746 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3747 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003748 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003749 if (rc != X86EMUL_CONTINUE)
3750 goto done;
3751 }
3752
Gleb Natapove92805a2010-02-10 14:21:35 +02003753 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003754 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003755 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003756 goto done;
3757 }
3758
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003759 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003760 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003761 rc = emulate_ud(ctxt);
3762 goto done;
3763 }
3764
Joerg Roedeld09beab2011-04-04 12:39:25 +02003765 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003766 if (ctxt->check_perm) {
3767 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003768 if (rc != X86EMUL_CONTINUE)
3769 goto done;
3770 }
3771
Avi Kivity9dac77f2011-06-01 15:34:25 +03003772 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3773 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003774 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003775 if (rc != X86EMUL_CONTINUE)
3776 goto done;
3777 }
3778
Avi Kivity9dac77f2011-06-01 15:34:25 +03003779 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003780 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003781 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3782 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003783 goto done;
3784 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003785 }
3786
Avi Kivity9dac77f2011-06-01 15:34:25 +03003787 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3788 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3789 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003790 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003791 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003792 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003793 }
3794
Avi Kivity9dac77f2011-06-01 15:34:25 +03003795 if (ctxt->src2.type == OP_MEM) {
3796 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3797 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003798 if (rc != X86EMUL_CONTINUE)
3799 goto done;
3800 }
3801
Avi Kivity9dac77f2011-06-01 15:34:25 +03003802 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003803 goto special_insn;
3804
3805
Avi Kivity9dac77f2011-06-01 15:34:25 +03003806 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003807 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003808 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3809 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003810 if (rc != X86EMUL_CONTINUE)
3811 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003812 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003813 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003814
Avi Kivity018a98d2007-11-27 19:30:56 +02003815special_insn:
3816
Avi Kivity9dac77f2011-06-01 15:34:25 +03003817 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3818 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003819 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003820 if (rc != X86EMUL_CONTINUE)
3821 goto done;
3822 }
3823
Avi Kivity9dac77f2011-06-01 15:34:25 +03003824 if (ctxt->execute) {
3825 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003826 if (rc != X86EMUL_CONTINUE)
3827 goto done;
3828 goto writeback;
3829 }
3830
Avi Kivity9dac77f2011-06-01 15:34:25 +03003831 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003832 goto twobyte_insn;
3833
Avi Kivity9dac77f2011-06-01 15:34:25 +03003834 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003835 case 0x06: /* push es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003836 rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003837 break;
3838 case 0x07: /* pop es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003839 rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003840 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003841 case 0x0e: /* push cs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003842 rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003843 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003844 case 0x16: /* push ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003845 rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003846 break;
3847 case 0x17: /* pop ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003848 rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003849 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003850 case 0x1e: /* push ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003851 rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003852 break;
3853 case 0x1f: /* pop ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003854 rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003855 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003856 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003857 emulate_1op("inc", ctxt->dst, ctxt->eflags);
Avi Kivity33615aa2007-10-31 11:15:56 +02003858 break;
3859 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003860 emulate_1op("dec", ctxt->dst, ctxt->eflags);
Avi Kivity33615aa2007-10-31 11:15:56 +02003861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003863 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003865 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003867 case 0x6c: /* insb */
3868 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003869 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003870 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003871 case 0x6e: /* outsb */
3872 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003873 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003874 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003875 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003876 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003877 if (test_cc(ctxt->b, ctxt->eflags))
3878 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003879 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003880 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003881 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003882 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003884 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003885 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003886 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003887 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003888 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003889 rc = em_xchg(ctxt);
3890 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003891 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003892 switch (ctxt->op_bytes) {
3893 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
3894 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
3895 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003896 }
3897 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003898 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003899 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003900 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003901 case 0xc4: /* les */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003902 rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003903 break;
3904 case 0xc5: /* lds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003905 rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003906 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003907 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003908 rc = emulate_int(ctxt, 3);
3909 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003910 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003911 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003912 break;
3913 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003914 if (ctxt->eflags & EFLG_OF)
3915 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003916 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003917 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003918 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003919 break;
3920 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003921 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003922 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003923 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003924 case 0xe4: /* inb */
3925 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003926 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003927 case 0xe6: /* outb */
3928 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003929 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003930 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003931 long int rel = ctxt->src.val;
3932 ctxt->src.val = (unsigned long) ctxt->_eip;
3933 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003934 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003935 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003936 }
3937 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003938 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003939 jmp_rel(ctxt, ctxt->src.val);
3940 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003941 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003942 case 0xec: /* in al,dx */
3943 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003944 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003945 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3946 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003947 goto done; /* IO is needed */
3948 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003949 case 0xee: /* out dx,al */
3950 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003951 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003952 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3953 &ctxt->src.val, 1);
3954 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003955 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003956 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003957 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003958 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003959 case 0xf5: /* cmc */
3960 /* complement carry flag from eflags reg */
3961 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003962 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003963 case 0xf6 ... 0xf7: /* Grp3 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003964 rc = em_grp3(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003965 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003966 case 0xf8: /* clc */
3967 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003968 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003969 case 0xf9: /* stc */
3970 ctxt->eflags |= EFLG_CF;
3971 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003972 case 0xfc: /* cld */
3973 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003974 break;
3975 case 0xfd: /* std */
3976 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003977 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003978 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003979 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003980 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003981 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003982 rc = em_grp45(ctxt);
3983 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003984 default:
3985 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003987
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003988 if (rc != X86EMUL_CONTINUE)
3989 goto done;
3990
Avi Kivity018a98d2007-11-27 19:30:56 +02003991writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003992 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003993 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003994 goto done;
3995
Gleb Natapov5cd21912010-03-18 15:20:26 +02003996 /*
3997 * restore dst type in case the decoding will be reused
3998 * (happens for string instruction )
3999 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004000 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004001
Avi Kivity9dac77f2011-06-01 15:34:25 +03004002 if ((ctxt->d & SrcMask) == SrcSI)
4003 string_addr_inc(ctxt, seg_override(ctxt),
4004 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004005
Avi Kivity9dac77f2011-06-01 15:34:25 +03004006 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004007 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004008 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004009
Avi Kivity9dac77f2011-06-01 15:34:25 +03004010 if (ctxt->rep_prefix && (ctxt->d & String)) {
4011 struct read_cache *r = &ctxt->io_read;
4012 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004013
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004014 if (!string_insn_completed(ctxt)) {
4015 /*
4016 * Re-enter guest when pio read ahead buffer is empty
4017 * or, if it is not used, after each 1024 iteration.
4018 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004019 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004020 (r->end == 0 || r->end != r->pos)) {
4021 /*
4022 * Reset read cache. Usually happens before
4023 * decode, but since instruction is restarted
4024 * we have to do it here.
4025 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004026 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004027 return EMULATION_RESTART;
4028 }
4029 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004030 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004031 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004032
Avi Kivity9dac77f2011-06-01 15:34:25 +03004033 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004034
4035done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004036 if (rc == X86EMUL_PROPAGATE_FAULT)
4037 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004038 if (rc == X86EMUL_INTERCEPTED)
4039 return EMULATION_INTERCEPTED;
4040
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004041 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042
4043twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004044 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004045 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004046 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004047 break;
4048 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004049 case 0x0d: /* GrpP (prefetch) */
4050 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004051 break;
4052 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004053 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004054 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004056 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004058 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004059 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004060 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004061 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004062 goto done;
4063 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004064 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004065 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004067 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004068 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004069 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004070 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004071 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004072 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004073 goto done;
4074 }
4075
Avi Kivity9dac77f2011-06-01 15:34:25 +03004076 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004078 case 0x30:
4079 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004080 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4081 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4082 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004083 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004084 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004085 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004086 }
4087 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004088 break;
4089 case 0x32:
4090 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004091 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004092 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004093 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004094 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004095 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004096 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4097 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004098 }
4099 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004100 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4103 if (!test_cc(ctxt->b, ctxt->eflags))
4104 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004106 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004107 if (test_cc(ctxt->b, ctxt->eflags))
4108 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004109 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004110 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004111 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004112 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004113 case 0xa0: /* push fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004114 rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004115 break;
4116 case 0xa1: /* pop fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004117 rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004118 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004119 case 0xa3:
4120 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004121 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004122 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004123 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
4124 emulate_2op_SrcV_nobyte("bt", ctxt->src, ctxt->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004125 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004126 case 0xa4: /* shld imm8, r, r/m */
4127 case 0xa5: /* shld cl, r, r/m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004128 emulate_2op_cl("shld", ctxt->src2, ctxt->src, ctxt->dst, ctxt->eflags);
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004129 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004130 case 0xa8: /* push gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004131 rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004132 break;
4133 case 0xa9: /* pop gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004134 rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004135 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004136 case 0xab:
4137 bts: /* bts */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004138 emulate_2op_SrcV_nobyte("bts", ctxt->src, ctxt->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004139 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004140 case 0xac: /* shrd imm8, r, r/m */
4141 case 0xad: /* shrd cl, r, r/m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004142 emulate_2op_cl("shrd", ctxt->src2, ctxt->src, ctxt->dst, ctxt->eflags);
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004143 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004144 case 0xae: /* clflush */
4145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 case 0xb0 ... 0xb1: /* cmpxchg */
4147 /*
4148 * Save real source value, then compare EAX against
4149 * destination.
4150 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004151 ctxt->src.orig_val = ctxt->src.val;
4152 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
4153 emulate_2op_SrcV("cmp", ctxt->src, ctxt->dst, ctxt->eflags);
Laurent Vivier05f086f2007-09-24 11:10:55 +02004154 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004156 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 } else {
4158 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004159 ctxt->dst.type = OP_REG;
4160 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 }
4162 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004163 case 0xb2: /* lss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004164 rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004165 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 case 0xb3:
4167 btr: /* btr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004168 emulate_2op_SrcV_nobyte("btr", ctxt->src, ctxt->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004170 case 0xb4: /* lfs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004171 rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004172 break;
4173 case 0xb5: /* lgs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004174 rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004175 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004177 ctxt->dst.bytes = ctxt->op_bytes;
4178 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4179 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004182 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 case 0:
4184 goto bt;
4185 case 1:
4186 goto bts;
4187 case 2:
4188 goto btr;
4189 case 3:
4190 goto btc;
4191 }
4192 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004193 case 0xbb:
4194 btc: /* btc */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004195 emulate_2op_SrcV_nobyte("btc", ctxt->src, ctxt->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004196 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004197 case 0xbc: { /* bsf */
4198 u8 zf;
4199 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004200 : "=r"(ctxt->dst.val), "=q"(zf)
4201 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004202 ctxt->eflags &= ~X86_EFLAGS_ZF;
4203 if (zf) {
4204 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004205 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004206 }
4207 break;
4208 }
4209 case 0xbd: { /* bsr */
4210 u8 zf;
4211 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004212 : "=r"(ctxt->dst.val), "=q"(zf)
4213 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004214 ctxt->eflags &= ~X86_EFLAGS_ZF;
4215 if (zf) {
4216 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004217 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004218 }
4219 break;
4220 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004222 ctxt->dst.bytes = ctxt->op_bytes;
4223 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4224 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004225 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004226 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004227 emulate_2op_SrcV("add", ctxt->src, ctxt->dst, ctxt->eflags);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004228 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004229 ctxt->src.val = ctxt->dst.orig_val;
4230 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004231 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004232 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004233 ctxt->dst.bytes = ctxt->op_bytes;
4234 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4235 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004236 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004238 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004239 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004240 default:
4241 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004242 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004243
4244 if (rc != X86EMUL_CONTINUE)
4245 goto done;
4246
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247 goto writeback;
4248
4249cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004250 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251}