blob: 458914d0f4b03b0122a4e35ba2235d64e7e794c6 [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 Kivitya31b9ce2011-09-07 16:41:35 +0300208#define ____emulate_2op(ctxt, _op, _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 Kivitya31b9ce2011-09-07 16:41:35 +0300214 : "=m" ((ctxt)->eflags), \
215 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200216 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300217 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200218 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200219
220
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300222#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200223 do { \
224 unsigned long _tmp; \
225 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300226 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200227 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300228 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200229 break; \
230 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300231 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200232 break; \
233 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300234 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200235 break; \
236 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 } while (0)
238
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300239#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200241 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300242 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300244 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 break; \
246 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300247 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 _wx, _wy, _lx, _ly, _qx, _qy); \
249 break; \
250 } \
251 } while (0)
252
253/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300254#define emulate_2op_SrcB(ctxt, _op) \
255 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256
257/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300258#define emulate_2op_SrcV(ctxt, _op) \
259 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800260
261/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300262#define emulate_2op_SrcV_nobyte(ctxt, _op) \
263 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100265/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity72952612011-04-20 13:12:27 +0300266#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
267 do { \
268 unsigned long _tmp; \
269 _type _clv = (_cl).val; \
270 _type _srcv = (_src).val; \
271 _type _dstv = (_dst).val; \
272 \
273 __asm__ __volatile__ ( \
274 _PRE_EFLAGS("0", "5", "2") \
275 _op _suffix " %4,%1 \n" \
276 _POST_EFLAGS("0", "5", "2") \
277 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
278 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
279 ); \
280 \
281 (_cl).val = (unsigned long) _clv; \
282 (_src).val = (unsigned long) _srcv; \
283 (_dst).val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100284 } while (0)
285
Avi Kivity72952612011-04-20 13:12:27 +0300286#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
287 do { \
288 switch ((_dst).bytes) { \
289 case 2: \
290 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
291 "w", unsigned short); \
292 break; \
293 case 4: \
294 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
295 "l", unsigned int); \
296 break; \
297 case 8: \
298 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
299 "q", unsigned long)); \
300 break; \
301 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100302 } while (0)
303
Avi Kivitydda96d82008-11-26 15:14:10 +0200304#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 do { \
306 unsigned long _tmp; \
307 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200308 __asm__ __volatile__ ( \
309 _PRE_EFLAGS("0", "3", "2") \
310 _op _suffix " %1; " \
311 _POST_EFLAGS("0", "3", "2") \
312 : "=m" (_eflags), "+m" ((_dst).val), \
313 "=&r" (_tmp) \
314 : "i" (EFLAGS_MASK)); \
315 } while (0)
316
317/* Instruction has only one explicit operand (no source operand). */
318#define emulate_1op(_op, _dst, _eflags) \
319 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400320 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200321 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
322 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
323 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
324 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325 } \
326 } while (0)
327
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300328#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
329 do { \
330 unsigned long _tmp; \
331 \
332 __asm__ __volatile__ ( \
333 _PRE_EFLAGS("0", "4", "1") \
334 _op _suffix " %5; " \
335 _POST_EFLAGS("0", "4", "1") \
336 : "=m" (_eflags), "=&r" (_tmp), \
337 "+a" (_rax), "+d" (_rdx) \
338 : "i" (EFLAGS_MASK), "m" ((_src).val), \
339 "a" (_rax), "d" (_rdx)); \
340 } while (0)
341
Avi Kivityf6b35972010-08-26 11:59:00 +0300342#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
343 do { \
344 unsigned long _tmp; \
345 \
346 __asm__ __volatile__ ( \
347 _PRE_EFLAGS("0", "5", "1") \
348 "1: \n\t" \
349 _op _suffix " %6; " \
350 "2: \n\t" \
351 _POST_EFLAGS("0", "5", "1") \
352 ".pushsection .fixup,\"ax\" \n\t" \
353 "3: movb $1, %4 \n\t" \
354 "jmp 2b \n\t" \
355 ".popsection \n\t" \
356 _ASM_EXTABLE(1b, 3b) \
357 : "=m" (_eflags), "=&r" (_tmp), \
358 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
359 : "i" (EFLAGS_MASK), "m" ((_src).val), \
360 "a" (_rax), "d" (_rdx)); \
361 } while (0)
362
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300363/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivity72952612011-04-20 13:12:27 +0300364#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
365 do { \
366 switch((_src).bytes) { \
367 case 1: \
368 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
369 _eflags, "b"); \
370 break; \
371 case 2: \
372 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
373 _eflags, "w"); \
374 break; \
375 case 4: \
376 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
377 _eflags, "l"); \
378 break; \
379 case 8: \
380 ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
381 _eflags, "q")); \
382 break; \
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300383 } \
384 } while (0)
385
Avi Kivityf6b35972010-08-26 11:59:00 +0300386#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
387 do { \
388 switch((_src).bytes) { \
389 case 1: \
390 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
391 _eflags, "b", _ex); \
392 break; \
393 case 2: \
394 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
395 _eflags, "w", _ex); \
396 break; \
397 case 4: \
398 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
399 _eflags, "l", _ex); \
400 break; \
401 case 8: ON64( \
402 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
403 _eflags, "q", _ex)); \
404 break; \
405 } \
406 } while (0)
407
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200408static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
409 enum x86_intercept intercept,
410 enum x86_intercept_stage stage)
411{
412 struct x86_instruction_info info = {
413 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300414 .rep_prefix = ctxt->rep_prefix,
415 .modrm_mod = ctxt->modrm_mod,
416 .modrm_reg = ctxt->modrm_reg,
417 .modrm_rm = ctxt->modrm_rm,
418 .src_val = ctxt->src.val64,
419 .src_bytes = ctxt->src.bytes,
420 .dst_bytes = ctxt->dst.bytes,
421 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200422 .next_rip = ctxt->eip,
423 };
424
Avi Kivity29535382011-04-20 13:37:53 +0300425 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200426}
427
Avi Kivity9dac77f2011-06-01 15:34:25 +0300428static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800429{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300430 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800431}
432
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800434static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300435address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800436{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300437 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800438 return reg;
439 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300440 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800441}
442
443static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300444register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800445{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300446 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800447}
448
Harvey Harrison7a9572752008-02-19 07:40:41 -0800449static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300450register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800451{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300452 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrison7a9572752008-02-19 07:40:41 -0800453 *reg += inc;
454 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300455 *reg = (*reg & ~ad_mask(ctxt)) | ((*reg + inc) & ad_mask(ctxt));
Harvey Harrison7a9572752008-02-19 07:40:41 -0800456}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Avi Kivity9dac77f2011-06-01 15:34:25 +0300458static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800459{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300460 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800461}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300462
Avi Kivity56697682011-04-03 14:08:51 +0300463static u32 desc_limit_scaled(struct desc_struct *desc)
464{
465 u32 limit = get_desc_limit(desc);
466
467 return desc->g ? (limit << 12) | 0xfff : limit;
468}
469
Avi Kivity9dac77f2011-06-01 15:34:25 +0300470static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300471{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300472 ctxt->has_seg_override = true;
473 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300474}
475
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900476static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300477{
478 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
479 return 0;
480
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900481 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300482}
483
Avi Kivity9dac77f2011-06-01 15:34:25 +0300484static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300485{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300486 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300487 return 0;
488
Avi Kivity9dac77f2011-06-01 15:34:25 +0300489 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300490}
491
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200492static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
493 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300494{
Avi Kivityda9cb572010-11-22 17:53:21 +0200495 ctxt->exception.vector = vec;
496 ctxt->exception.error_code = error;
497 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200498 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300499}
500
Joerg Roedel3b88e412011-04-04 12:39:29 +0200501static int emulate_db(struct x86_emulate_ctxt *ctxt)
502{
503 return emulate_exception(ctxt, DB_VECTOR, 0, false);
504}
505
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200506static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300507{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200508 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300509}
510
Avi Kivity618ff152011-04-03 12:32:09 +0300511static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
512{
513 return emulate_exception(ctxt, SS_VECTOR, err, true);
514}
515
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200516static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300517{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200518 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300519}
520
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200521static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300522{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200523 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300524}
525
Avi Kivity34d1f492010-08-26 11:59:01 +0300526static int emulate_de(struct x86_emulate_ctxt *ctxt)
527{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200528 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300529}
530
Avi Kivity12537912011-03-29 11:41:27 +0200531static int emulate_nm(struct x86_emulate_ctxt *ctxt)
532{
533 return emulate_exception(ctxt, NM_VECTOR, 0, false);
534}
535
Avi Kivity1aa36612011-04-27 13:20:30 +0300536static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
537{
538 u16 selector;
539 struct desc_struct desc;
540
541 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
542 return selector;
543}
544
545static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
546 unsigned seg)
547{
548 u16 dummy;
549 u32 base3;
550 struct desc_struct desc;
551
552 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
553 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
554}
555
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400556static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300557 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400558 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300559 ulong *linear)
560{
Avi Kivity618ff152011-04-03 12:32:09 +0300561 struct desc_struct desc;
562 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300563 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300564 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300565 u16 sel;
Avi Kivity618ff152011-04-03 12:32:09 +0300566 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300567
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900568 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300569 switch (ctxt->mode) {
570 case X86EMUL_MODE_REAL:
571 break;
572 case X86EMUL_MODE_PROT64:
573 if (((signed long)la << 16) >> 16 != la)
574 return emulate_gp(ctxt, 0);
575 break;
576 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300577 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
578 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300579 if (!usable)
580 goto bad;
581 /* code segment or read-only data segment */
582 if (((desc.type & 8) || !(desc.type & 2)) && write)
583 goto bad;
584 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400585 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300586 goto bad;
587 lim = desc_limit_scaled(&desc);
588 if ((desc.type & 8) || !(desc.type & 4)) {
589 /* expand-up segment */
590 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
591 goto bad;
592 } else {
593 /* exapand-down segment */
594 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
595 goto bad;
596 lim = desc.d ? 0xffffffff : 0xffff;
597 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
598 goto bad;
599 }
Avi Kivity717746e2011-04-20 13:37:53 +0300600 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity1aa36612011-04-27 13:20:30 +0300601 rpl = sel & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300602 cpl = max(cpl, rpl);
603 if (!(desc.type & 8)) {
604 /* data segment */
605 if (cpl > desc.dpl)
606 goto bad;
607 } else if ((desc.type & 8) && !(desc.type & 4)) {
608 /* nonconforming code segment */
609 if (cpl != desc.dpl)
610 goto bad;
611 } else if ((desc.type & 8) && (desc.type & 4)) {
612 /* conforming code segment */
613 if (cpl < desc.dpl)
614 goto bad;
615 }
616 break;
617 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300618 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300619 la &= (u32)-1;
620 *linear = la;
621 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300622bad:
623 if (addr.seg == VCPU_SREG_SS)
624 return emulate_ss(ctxt, addr.seg);
625 else
626 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300627}
628
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400629static int linearize(struct x86_emulate_ctxt *ctxt,
630 struct segmented_address addr,
631 unsigned size, bool write,
632 ulong *linear)
633{
634 return __linearize(ctxt, addr, size, write, false, linear);
635}
636
637
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200638static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
639 struct segmented_address addr,
640 void *data,
641 unsigned size)
642{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200643 int rc;
644 ulong linear;
645
Avi Kivity83b87952011-04-03 11:31:19 +0300646 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200647 if (rc != X86EMUL_CONTINUE)
648 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300649 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200650}
651
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900652/*
653 * Fetch the next byte of the instruction being emulated which is pointed to
654 * by ctxt->_eip, then increment ctxt->_eip.
655 *
656 * Also prefetch the remaining bytes of the instruction without crossing page
657 * boundary if they are not in fetch_cache yet.
658 */
659static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200660{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300661 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200662 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300663 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200664
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900665 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400666 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900667 struct segmented_address addr = { .seg = VCPU_SREG_CS,
668 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300669 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900670 size = min(15UL - cur_size,
671 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400672 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900673 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400674 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900675 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
676 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900677 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200678 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300679 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200680 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900681 *dest = fc->data[ctxt->_eip - fc->start];
682 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900683 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200684}
685
686static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900687 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200688{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900689 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200690
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200691 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900692 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200693 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200694 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900695 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900696 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200697 return rc;
698 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900699 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200700}
701
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900702/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900703#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900704({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900705 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900706 if (rc != X86EMUL_CONTINUE) \
707 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900708 (_type)_x; \
709})
710
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900711#define insn_fetch_arr(_arr, _size, _ctxt) \
712({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900713 if (rc != X86EMUL_CONTINUE) \
714 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900715})
716
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000717/*
718 * Given the 'reg' portion of a ModRM byte, and a register block, return a
719 * pointer into the block that addresses the relevant register.
720 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
721 */
722static void *decode_register(u8 modrm_reg, unsigned long *regs,
723 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724{
725 void *p;
726
727 p = &regs[modrm_reg];
728 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
729 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
730 return p;
731}
732
733static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200734 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 u16 *size, unsigned long *address, int op_bytes)
736{
737 int rc;
738
739 if (op_bytes == 2)
740 op_bytes = 3;
741 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200742 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900743 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200745 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200746 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747 return rc;
748}
749
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300750static int test_cc(unsigned int condition, unsigned int flags)
751{
752 int rc = 0;
753
754 switch ((condition & 15) >> 1) {
755 case 0: /* o */
756 rc |= (flags & EFLG_OF);
757 break;
758 case 1: /* b/c/nae */
759 rc |= (flags & EFLG_CF);
760 break;
761 case 2: /* z/e */
762 rc |= (flags & EFLG_ZF);
763 break;
764 case 3: /* be/na */
765 rc |= (flags & (EFLG_CF|EFLG_ZF));
766 break;
767 case 4: /* s */
768 rc |= (flags & EFLG_SF);
769 break;
770 case 5: /* p/pe */
771 rc |= (flags & EFLG_PF);
772 break;
773 case 7: /* le/ng */
774 rc |= (flags & EFLG_ZF);
775 /* fall through */
776 case 6: /* l/nge */
777 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
778 break;
779 }
780
781 /* Odd condition identifiers (lsb == 1) have inverted sense. */
782 return (!!rc ^ (condition & 1));
783}
784
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300785static void fetch_register_operand(struct operand *op)
786{
787 switch (op->bytes) {
788 case 1:
789 op->val = *(u8 *)op->addr.reg;
790 break;
791 case 2:
792 op->val = *(u16 *)op->addr.reg;
793 break;
794 case 4:
795 op->val = *(u32 *)op->addr.reg;
796 break;
797 case 8:
798 op->val = *(u64 *)op->addr.reg;
799 break;
800 }
801}
802
Avi Kivity12537912011-03-29 11:41:27 +0200803static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
804{
805 ctxt->ops->get_fpu(ctxt);
806 switch (reg) {
807 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
808 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
809 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
810 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
811 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
812 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
813 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
814 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
815#ifdef CONFIG_X86_64
816 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
817 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
818 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
819 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
820 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
821 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
822 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
823 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
824#endif
825 default: BUG();
826 }
827 ctxt->ops->put_fpu(ctxt);
828}
829
830static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
831 int reg)
832{
833 ctxt->ops->get_fpu(ctxt);
834 switch (reg) {
835 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
836 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
837 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
838 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
839 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
840 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
841 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
842 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
843#ifdef CONFIG_X86_64
844 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
845 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
846 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
847 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
848 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
849 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
850 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
851 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
852#endif
853 default: BUG();
854 }
855 ctxt->ops->put_fpu(ctxt);
856}
857
858static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
859 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200860 int inhibit_bytereg)
861{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300862 unsigned reg = ctxt->modrm_reg;
863 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200864
Avi Kivity9dac77f2011-06-01 15:34:25 +0300865 if (!(ctxt->d & ModRM))
866 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200867
Avi Kivity9dac77f2011-06-01 15:34:25 +0300868 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200869 op->type = OP_XMM;
870 op->bytes = 16;
871 op->addr.xmm = reg;
872 read_sse_reg(ctxt, &op->vec_val, reg);
873 return;
874 }
875
Avi Kivity3c118e22007-10-31 10:27:04 +0200876 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300877 if ((ctxt->d & ByteOp) && !inhibit_bytereg) {
878 op->addr.reg = decode_register(reg, ctxt->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200879 op->bytes = 1;
880 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +0300881 op->addr.reg = decode_register(reg, ctxt->regs, 0);
882 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200883 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300884 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200885 op->orig_val = op->val;
886}
887
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300889 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200890{
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200891 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700892 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900893 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300894 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200895
Avi Kivity9dac77f2011-06-01 15:34:25 +0300896 if (ctxt->rex_prefix) {
897 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
898 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
899 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200900 }
901
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900902 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +0300903 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
904 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
905 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
906 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200907
Avi Kivity9dac77f2011-06-01 15:34:25 +0300908 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300909 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300910 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
911 op->addr.reg = decode_register(ctxt->modrm_rm,
912 ctxt->regs, ctxt->d & ByteOp);
913 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +0200914 op->type = OP_XMM;
915 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +0300916 op->addr.xmm = ctxt->modrm_rm;
917 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +0200918 return rc;
919 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300920 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200921 return rc;
922 }
923
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300924 op->type = OP_MEM;
925
Avi Kivity9dac77f2011-06-01 15:34:25 +0300926 if (ctxt->ad_bytes == 2) {
927 unsigned bx = ctxt->regs[VCPU_REGS_RBX];
928 unsigned bp = ctxt->regs[VCPU_REGS_RBP];
929 unsigned si = ctxt->regs[VCPU_REGS_RSI];
930 unsigned di = ctxt->regs[VCPU_REGS_RDI];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200931
932 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300933 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200934 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300935 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900936 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200937 break;
938 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900939 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200940 break;
941 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900942 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200943 break;
944 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300945 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200946 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300947 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200948 break;
949 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300950 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200951 break;
952 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300953 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200954 break;
955 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300956 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200957 break;
958 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300959 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200960 break;
961 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300962 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200963 break;
964 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300965 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300966 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200967 break;
968 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300969 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200970 break;
971 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300972 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
973 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
974 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300975 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200976 } else {
977 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +0300978 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900979 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200980 index_reg |= (sib >> 3) & 7;
981 base_reg |= sib & 7;
982 scale = sib >> 6;
983
Avi Kivity9dac77f2011-06-01 15:34:25 +0300984 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900985 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700986 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300987 modrm_ea += ctxt->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700988 if (index_reg != 4)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300989 modrm_ea += ctxt->regs[index_reg] << scale;
990 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -0700991 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +0300992 ctxt->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700993 } else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300994 modrm_ea += ctxt->regs[ctxt->modrm_rm];
995 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200996 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +0300997 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900998 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200999 break;
1000 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001001 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001002 break;
1003 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001004 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001005 break;
1006 }
1007 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001008 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001009done:
1010 return rc;
1011}
1012
1013static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001014 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001015{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001016 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001017
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001018 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001019 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001020 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001021 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001022 break;
1023 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001024 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001025 break;
1026 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001027 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001028 break;
1029 }
1030done:
1031 return rc;
1032}
1033
Avi Kivity9dac77f2011-06-01 15:34:25 +03001034static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001035{
Sheng Yang7129eec2010-09-28 16:33:32 +08001036 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001037
Avi Kivity9dac77f2011-06-01 15:34:25 +03001038 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1039 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001040
Avi Kivity9dac77f2011-06-01 15:34:25 +03001041 if (ctxt->src.bytes == 2)
1042 sv = (s16)ctxt->src.val & (s16)mask;
1043 else if (ctxt->src.bytes == 4)
1044 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001045
Avi Kivity9dac77f2011-06-01 15:34:25 +03001046 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001047 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001048
1049 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001050 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001051}
1052
Gleb Natapov9de41572010-04-28 19:15:22 +03001053static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001054 unsigned long addr, void *dest, unsigned size)
1055{
1056 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001057 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001058
1059 while (size) {
1060 int n = min(size, 8u);
1061 size -= n;
1062 if (mc->pos < mc->end)
1063 goto read_cached;
1064
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001065 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1066 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001067 if (rc != X86EMUL_CONTINUE)
1068 return rc;
1069 mc->end += n;
1070
1071 read_cached:
1072 memcpy(dest, mc->data + mc->pos, n);
1073 mc->pos += n;
1074 dest += n;
1075 addr += n;
1076 }
1077 return X86EMUL_CONTINUE;
1078}
1079
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001080static int segmented_read(struct x86_emulate_ctxt *ctxt,
1081 struct segmented_address addr,
1082 void *data,
1083 unsigned size)
1084{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001085 int rc;
1086 ulong linear;
1087
Avi Kivity83b87952011-04-03 11:31:19 +03001088 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001089 if (rc != X86EMUL_CONTINUE)
1090 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001091 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001092}
1093
1094static int segmented_write(struct x86_emulate_ctxt *ctxt,
1095 struct segmented_address addr,
1096 const void *data,
1097 unsigned size)
1098{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001099 int rc;
1100 ulong linear;
1101
Avi Kivity83b87952011-04-03 11:31:19 +03001102 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001103 if (rc != X86EMUL_CONTINUE)
1104 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001105 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1106 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001107}
1108
1109static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1110 struct segmented_address addr,
1111 const void *orig_data, const void *data,
1112 unsigned size)
1113{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001114 int rc;
1115 ulong linear;
1116
Avi Kivity83b87952011-04-03 11:31:19 +03001117 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001118 if (rc != X86EMUL_CONTINUE)
1119 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001120 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1121 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001122}
1123
Gleb Natapov7b262e92010-03-18 15:20:27 +02001124static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001125 unsigned int size, unsigned short port,
1126 void *dest)
1127{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001128 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001129
1130 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001131 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 unsigned int count = ctxt->rep_prefix ?
1133 address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001134 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 offset_in_page(ctxt->regs[VCPU_REGS_RDI]) :
1136 PAGE_SIZE - offset_in_page(ctxt->regs[VCPU_REGS_RDI]);
Gleb Natapov7b262e92010-03-18 15:20:27 +02001137 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1138 count);
1139 if (n == 0)
1140 n = 1;
1141 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001142 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001143 return 0;
1144 rc->end = n * size;
1145 }
1146
1147 memcpy(dest, rc->data + rc->pos, size);
1148 rc->pos += size;
1149 return 1;
1150}
1151
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001152static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001153 u16 selector, struct desc_ptr *dt)
1154{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001155 struct x86_emulate_ops *ops = ctxt->ops;
1156
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001157 if (selector & 1 << 2) {
1158 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001159 u16 sel;
1160
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001161 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001162 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001163 return;
1164
1165 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1166 dt->address = get_desc_base(&desc);
1167 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001168 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001169}
1170
1171/* allowed just for 8 bytes segments */
1172static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001173 u16 selector, struct desc_struct *desc)
1174{
1175 struct desc_ptr dt;
1176 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001177 ulong addr;
1178
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001179 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001180
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001181 if (dt.size < index * 8 + 7)
1182 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001183
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001184 addr = dt.address + index * 8;
1185 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1186 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001187}
1188
1189/* allowed just for 8 bytes segments */
1190static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001191 u16 selector, struct desc_struct *desc)
1192{
1193 struct desc_ptr dt;
1194 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001195 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001196
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001197 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001198
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001199 if (dt.size < index * 8 + 7)
1200 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001201
1202 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001203 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1204 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001205}
1206
Gleb Natapov5601d052011-03-07 14:55:06 +02001207/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001208static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001209 u16 selector, int seg)
1210{
1211 struct desc_struct seg_desc;
1212 u8 dpl, rpl, cpl;
1213 unsigned err_vec = GP_VECTOR;
1214 u32 err_code = 0;
1215 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1216 int ret;
1217
1218 memset(&seg_desc, 0, sizeof seg_desc);
1219
1220 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1221 || ctxt->mode == X86EMUL_MODE_REAL) {
1222 /* set real mode segment descriptor */
1223 set_desc_base(&seg_desc, selector << 4);
1224 set_desc_limit(&seg_desc, 0xffff);
1225 seg_desc.type = 3;
1226 seg_desc.p = 1;
1227 seg_desc.s = 1;
1228 goto load;
1229 }
1230
1231 /* NULL selector is not valid for TR, CS and SS */
1232 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1233 && null_selector)
1234 goto exception;
1235
1236 /* TR should be in GDT only */
1237 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1238 goto exception;
1239
1240 if (null_selector) /* for NULL selector skip all following checks */
1241 goto load;
1242
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001243 ret = read_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001244 if (ret != X86EMUL_CONTINUE)
1245 return ret;
1246
1247 err_code = selector & 0xfffc;
1248 err_vec = GP_VECTOR;
1249
1250 /* can't load system descriptor into segment selecor */
1251 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1252 goto exception;
1253
1254 if (!seg_desc.p) {
1255 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1256 goto exception;
1257 }
1258
1259 rpl = selector & 3;
1260 dpl = seg_desc.dpl;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001261 cpl = ctxt->ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001262
1263 switch (seg) {
1264 case VCPU_SREG_SS:
1265 /*
1266 * segment is not a writable data segment or segment
1267 * selector's RPL != CPL or segment selector's RPL != CPL
1268 */
1269 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1270 goto exception;
1271 break;
1272 case VCPU_SREG_CS:
1273 if (!(seg_desc.type & 8))
1274 goto exception;
1275
1276 if (seg_desc.type & 4) {
1277 /* conforming */
1278 if (dpl > cpl)
1279 goto exception;
1280 } else {
1281 /* nonconforming */
1282 if (rpl > cpl || dpl != cpl)
1283 goto exception;
1284 }
1285 /* CS(RPL) <- CPL */
1286 selector = (selector & 0xfffc) | cpl;
1287 break;
1288 case VCPU_SREG_TR:
1289 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1290 goto exception;
1291 break;
1292 case VCPU_SREG_LDTR:
1293 if (seg_desc.s || seg_desc.type != 2)
1294 goto exception;
1295 break;
1296 default: /* DS, ES, FS, or GS */
1297 /*
1298 * segment is not a data or readable code segment or
1299 * ((segment is a data or nonconforming code segment)
1300 * and (both RPL and CPL > DPL))
1301 */
1302 if ((seg_desc.type & 0xa) == 0x8 ||
1303 (((seg_desc.type & 0xc) != 0xc) &&
1304 (rpl > dpl && cpl > dpl)))
1305 goto exception;
1306 break;
1307 }
1308
1309 if (seg_desc.s) {
1310 /* mark segment as accessed */
1311 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001312 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001313 if (ret != X86EMUL_CONTINUE)
1314 return ret;
1315 }
1316load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001317 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001318 return X86EMUL_CONTINUE;
1319exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001320 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001321 return X86EMUL_PROPAGATE_FAULT;
1322}
1323
Wei Yongjun31be40b2010-08-17 09:17:30 +08001324static void write_register_operand(struct operand *op)
1325{
1326 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1327 switch (op->bytes) {
1328 case 1:
1329 *(u8 *)op->addr.reg = (u8)op->val;
1330 break;
1331 case 2:
1332 *(u16 *)op->addr.reg = (u16)op->val;
1333 break;
1334 case 4:
1335 *op->addr.reg = (u32)op->val;
1336 break; /* 64b: zero-extend */
1337 case 8:
1338 *op->addr.reg = op->val;
1339 break;
1340 }
1341}
1342
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001343static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001344{
1345 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001346
Avi Kivity9dac77f2011-06-01 15:34:25 +03001347 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001348 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001349 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001350 break;
1351 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001352 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001353 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001354 ctxt->dst.addr.mem,
1355 &ctxt->dst.orig_val,
1356 &ctxt->dst.val,
1357 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001358 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001359 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001360 ctxt->dst.addr.mem,
1361 &ctxt->dst.val,
1362 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001363 if (rc != X86EMUL_CONTINUE)
1364 return rc;
1365 break;
Avi Kivity12537912011-03-29 11:41:27 +02001366 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001367 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001368 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001369 case OP_NONE:
1370 /* no writeback */
1371 break;
1372 default:
1373 break;
1374 }
1375 return X86EMUL_CONTINUE;
1376}
1377
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001378static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001379{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001380 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001381
Avi Kivity9dac77f2011-06-01 15:34:25 +03001382 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], -ctxt->op_bytes);
1383 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001384 addr.seg = VCPU_SREG_SS;
1385
1386 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001387 ctxt->dst.type = OP_NONE;
1388 return segmented_write(ctxt, addr, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001389}
1390
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001391static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001392 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001394 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001395 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001396
Avi Kivity9dac77f2011-06-01 15:34:25 +03001397 addr.ea = register_address(ctxt, ctxt->regs[VCPU_REGS_RSP]);
Avi Kivity90de84f2010-11-17 15:28:21 +02001398 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001399 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001400 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001401 return rc;
1402
Avi Kivity9dac77f2011-06-01 15:34:25 +03001403 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001404 return rc;
1405}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001406
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001407static int em_pop(struct x86_emulate_ctxt *ctxt)
1408{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001409 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001410}
1411
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001412static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001413 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001414{
1415 int rc;
1416 unsigned long val, change_mask;
1417 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001418 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001419
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001420 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001421 if (rc != X86EMUL_CONTINUE)
1422 return rc;
1423
1424 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1425 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1426
1427 switch(ctxt->mode) {
1428 case X86EMUL_MODE_PROT64:
1429 case X86EMUL_MODE_PROT32:
1430 case X86EMUL_MODE_PROT16:
1431 if (cpl == 0)
1432 change_mask |= EFLG_IOPL;
1433 if (cpl <= iopl)
1434 change_mask |= EFLG_IF;
1435 break;
1436 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001437 if (iopl < 3)
1438 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001439 change_mask |= EFLG_IF;
1440 break;
1441 default: /* real mode */
1442 change_mask |= (EFLG_IOPL | EFLG_IF);
1443 break;
1444 }
1445
1446 *(unsigned long *)dest =
1447 (ctxt->eflags & ~change_mask) | (val & change_mask);
1448
1449 return rc;
1450}
1451
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001452static int em_popf(struct x86_emulate_ctxt *ctxt)
1453{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001454 ctxt->dst.type = OP_REG;
1455 ctxt->dst.addr.reg = &ctxt->eflags;
1456 ctxt->dst.bytes = ctxt->op_bytes;
1457 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001458}
1459
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001460static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001461{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001462 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001463
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001464 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001465}
1466
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001467static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001468{
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001469 unsigned long selector;
1470 int rc;
1471
Avi Kivity9dac77f2011-06-01 15:34:25 +03001472 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001473 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001474 return rc;
1475
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001476 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001477 return rc;
1478}
1479
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001480static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001481{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001482 unsigned long old_esp = ctxt->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001483 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001484 int reg = VCPU_REGS_RAX;
1485
1486 while (reg <= VCPU_REGS_RDI) {
1487 (reg == VCPU_REGS_RSP) ?
Avi Kivity9dac77f2011-06-01 15:34:25 +03001488 (ctxt->src.val = old_esp) : (ctxt->src.val = ctxt->regs[reg]);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001489
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001490 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001491 if (rc != X86EMUL_CONTINUE)
1492 return rc;
1493
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001494 ++reg;
1495 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001496
Wei Yongjunc37eda12010-06-15 09:03:33 +08001497 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001498}
1499
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001500static int em_pushf(struct x86_emulate_ctxt *ctxt)
1501{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001502 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001503 return em_push(ctxt);
1504}
1505
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001506static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001507{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001508 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001509 int reg = VCPU_REGS_RDI;
1510
1511 while (reg >= VCPU_REGS_RAX) {
1512 if (reg == VCPU_REGS_RSP) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001513 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP],
1514 ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001515 --reg;
1516 }
1517
Avi Kivity9dac77f2011-06-01 15:34:25 +03001518 rc = emulate_pop(ctxt, &ctxt->regs[reg], ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001519 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001520 break;
1521 --reg;
1522 }
1523 return rc;
1524}
1525
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001526int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001527{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001528 struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001529 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001530 struct desc_ptr dt;
1531 gva_t cs_addr;
1532 gva_t eip_addr;
1533 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001534
1535 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001536 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001537 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001538 if (rc != X86EMUL_CONTINUE)
1539 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001540
1541 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1542
Avi Kivity9dac77f2011-06-01 15:34:25 +03001543 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001544 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001545 if (rc != X86EMUL_CONTINUE)
1546 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001547
Avi Kivity9dac77f2011-06-01 15:34:25 +03001548 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001549 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001550 if (rc != X86EMUL_CONTINUE)
1551 return rc;
1552
Avi Kivity4bff1e862011-04-20 13:37:53 +03001553 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001554
1555 eip_addr = dt.address + (irq << 2);
1556 cs_addr = dt.address + (irq << 2) + 2;
1557
Avi Kivity0f65dd72011-04-20 13:37:53 +03001558 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001559 if (rc != X86EMUL_CONTINUE)
1560 return rc;
1561
Avi Kivity0f65dd72011-04-20 13:37:53 +03001562 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001563 if (rc != X86EMUL_CONTINUE)
1564 return rc;
1565
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001566 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001567 if (rc != X86EMUL_CONTINUE)
1568 return rc;
1569
Avi Kivity9dac77f2011-06-01 15:34:25 +03001570 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001571
1572 return rc;
1573}
1574
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001575static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001576{
1577 switch(ctxt->mode) {
1578 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001579 return emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001580 case X86EMUL_MODE_VM86:
1581 case X86EMUL_MODE_PROT16:
1582 case X86EMUL_MODE_PROT32:
1583 case X86EMUL_MODE_PROT64:
1584 default:
1585 /* Protected mode interrupts unimplemented yet */
1586 return X86EMUL_UNHANDLEABLE;
1587 }
1588}
1589
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001590static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001591{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001592 int rc = X86EMUL_CONTINUE;
1593 unsigned long temp_eip = 0;
1594 unsigned long temp_eflags = 0;
1595 unsigned long cs = 0;
1596 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1597 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1598 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1599 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1600
1601 /* TODO: Add stack limit check */
1602
Avi Kivity9dac77f2011-06-01 15:34:25 +03001603 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001604
1605 if (rc != X86EMUL_CONTINUE)
1606 return rc;
1607
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001608 if (temp_eip & ~0xffff)
1609 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001610
Avi Kivity9dac77f2011-06-01 15:34:25 +03001611 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001612
1613 if (rc != X86EMUL_CONTINUE)
1614 return rc;
1615
Avi Kivity9dac77f2011-06-01 15:34:25 +03001616 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001617
1618 if (rc != X86EMUL_CONTINUE)
1619 return rc;
1620
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001621 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001622
1623 if (rc != X86EMUL_CONTINUE)
1624 return rc;
1625
Avi Kivity9dac77f2011-06-01 15:34:25 +03001626 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001627
1628
Avi Kivity9dac77f2011-06-01 15:34:25 +03001629 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001630 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001631 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001632 ctxt->eflags &= ~0xffff;
1633 ctxt->eflags |= temp_eflags;
1634 }
1635
1636 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1637 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1638
1639 return rc;
1640}
1641
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001642static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001643{
1644 switch(ctxt->mode) {
1645 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001646 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001647 case X86EMUL_MODE_VM86:
1648 case X86EMUL_MODE_PROT16:
1649 case X86EMUL_MODE_PROT32:
1650 case X86EMUL_MODE_PROT64:
1651 default:
1652 /* iret from protected mode unimplemented yet */
1653 return X86EMUL_UNHANDLEABLE;
1654 }
1655}
1656
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001657static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1658{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001659 int rc;
1660 unsigned short sel;
1661
Avi Kivity9dac77f2011-06-01 15:34:25 +03001662 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001663
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001664 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001665 if (rc != X86EMUL_CONTINUE)
1666 return rc;
1667
Avi Kivity9dac77f2011-06-01 15:34:25 +03001668 ctxt->_eip = 0;
1669 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001670 return X86EMUL_CONTINUE;
1671}
1672
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001673static int em_grp1a(struct x86_emulate_ctxt *ctxt)
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001674{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001675 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001676}
1677
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001678static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001679{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001680 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001682 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683 break;
1684 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001685 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001686 break;
1687 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001688 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001689 break;
1690 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001691 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001692 break;
1693 case 4: /* sal/shl */
1694 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001695 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696 break;
1697 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001698 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699 break;
1700 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001701 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001702 break;
1703 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001704 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001705}
1706
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001707static int em_grp3(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001708{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001709 unsigned long *rax = &ctxt->regs[VCPU_REGS_RAX];
1710 unsigned long *rdx = &ctxt->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001711 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001712
Avi Kivity9dac77f2011-06-01 15:34:25 +03001713 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001714 case 0 ... 1: /* test */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001715 emulate_2op_SrcV(ctxt, "test");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001716 break;
1717 case 2: /* not */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001718 ctxt->dst.val = ~ctxt->dst.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001719 break;
1720 case 3: /* neg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001721 emulate_1op("neg", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001722 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001723 case 4: /* mul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001724 emulate_1op_rax_rdx("mul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001725 break;
1726 case 5: /* imul */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001727 emulate_1op_rax_rdx("imul", ctxt->src, *rax, *rdx, ctxt->eflags);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001728 break;
1729 case 6: /* div */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001730 emulate_1op_rax_rdx_ex("div", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001731 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001732 break;
1733 case 7: /* idiv */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001734 emulate_1op_rax_rdx_ex("idiv", ctxt->src, *rax, *rdx,
Avi Kivity34d1f492010-08-26 11:59:01 +03001735 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001736 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001738 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001740 if (de)
1741 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001742 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743}
1744
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001745static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001746{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001747 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001748
Avi Kivity9dac77f2011-06-01 15:34:25 +03001749 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001750 case 0: /* inc */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001751 emulate_1op("inc", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001752 break;
1753 case 1: /* dec */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001754 emulate_1op("dec", ctxt->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001755 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001756 case 2: /* call near abs */ {
1757 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001758 old_eip = ctxt->_eip;
1759 ctxt->_eip = ctxt->src.val;
1760 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001761 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001762 break;
1763 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001764 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001765 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001766 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001767 case 5: /* jmp far */
1768 rc = em_jmp_far(ctxt);
1769 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001770 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001771 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001772 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001773 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001774 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001775}
1776
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001777static int em_grp9(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001778{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001779 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001780
Avi Kivity9dac77f2011-06-01 15:34:25 +03001781 if (((u32) (old >> 0) != (u32) ctxt->regs[VCPU_REGS_RAX]) ||
1782 ((u32) (old >> 32) != (u32) ctxt->regs[VCPU_REGS_RDX])) {
1783 ctxt->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1784 ctxt->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001785 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001786 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03001787 ctxt->dst.val64 = ((u64)ctxt->regs[VCPU_REGS_RCX] << 32) |
1788 (u32) ctxt->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789
Laurent Vivier05f086f2007-09-24 11:10:55 +02001790 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001791 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001792 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793}
1794
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001795static int em_ret(struct x86_emulate_ctxt *ctxt)
1796{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001797 ctxt->dst.type = OP_REG;
1798 ctxt->dst.addr.reg = &ctxt->_eip;
1799 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09001800 return em_pop(ctxt);
1801}
1802
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001803static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001804{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001805 int rc;
1806 unsigned long cs;
1807
Avi Kivity9dac77f2011-06-01 15:34:25 +03001808 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001809 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001810 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001811 if (ctxt->op_bytes == 4)
1812 ctxt->_eip = (u32)ctxt->_eip;
1813 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001814 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001815 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001816 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001817 return rc;
1818}
1819
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001820static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001821{
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001822 unsigned short sel;
1823 int rc;
1824
Avi Kivity9dac77f2011-06-01 15:34:25 +03001825 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001826
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001827 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001828 if (rc != X86EMUL_CONTINUE)
1829 return rc;
1830
Avi Kivity9dac77f2011-06-01 15:34:25 +03001831 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001832 return rc;
1833}
1834
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001835static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001836setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001837 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001838{
Avi Kivity1aa36612011-04-27 13:20:30 +03001839 u16 selector;
1840
Gleb Natapov79168fd2010-04-28 19:15:30 +03001841 memset(cs, 0, sizeof(struct desc_struct));
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001842 ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001843 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001844
1845 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001846 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001847 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001848 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001849 cs->type = 0x0b; /* Read, Execute, Accessed */
1850 cs->s = 1;
1851 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001852 cs->p = 1;
1853 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001854
Gleb Natapov79168fd2010-04-28 19:15:30 +03001855 set_desc_base(ss, 0); /* flat segment */
1856 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001857 ss->g = 1; /* 4kb granularity */
1858 ss->s = 1;
1859 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001860 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001861 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001862 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001863}
1864
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001865static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001866{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001867 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001868 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001869 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001870 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001871 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001872
1873 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001874 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001875 ctxt->mode == X86EMUL_MODE_VM86)
1876 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001878 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001879 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001880
Avi Kivity717746e2011-04-20 13:37:53 +03001881 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001882 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001883 cs_sel = (u16)(msr_data & 0xfffc);
1884 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001886 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001887 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001888 cs.l = 1;
1889 }
Avi Kivity1aa36612011-04-27 13:20:30 +03001890 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1891 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001892
Avi Kivity9dac77f2011-06-01 15:34:25 +03001893 ctxt->regs[VCPU_REGS_RCX] = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001894 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001895#ifdef CONFIG_X86_64
Avi Kivity9dac77f2011-06-01 15:34:25 +03001896 ctxt->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001897
Avi Kivity717746e2011-04-20 13:37:53 +03001898 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001899 ctxt->mode == X86EMUL_MODE_PROT64 ?
1900 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001901 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001902
Avi Kivity717746e2011-04-20 13:37:53 +03001903 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001904 ctxt->eflags &= ~(msr_data | EFLG_RF);
1905#endif
1906 } else {
1907 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001908 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001909 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001910
1911 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1912 }
1913
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001914 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001915}
1916
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001917static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02001918{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001919 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001920 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001921 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001922 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001923 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001924
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001925 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02001926 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001927 if (ctxt->mode == X86EMUL_MODE_REAL)
1928 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001929
1930 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1931 * Therefore, we inject an #UD.
1932 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001933 if (ctxt->mode == X86EMUL_MODE_PROT64)
1934 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001935
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001936 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001937
Avi Kivity717746e2011-04-20 13:37:53 +03001938 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001939 switch (ctxt->mode) {
1940 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001941 if ((msr_data & 0xfffc) == 0x0)
1942 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001943 break;
1944 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001945 if (msr_data == 0x0)
1946 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001947 break;
1948 }
1949
1950 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001951 cs_sel = (u16)msr_data;
1952 cs_sel &= ~SELECTOR_RPL_MASK;
1953 ss_sel = cs_sel + 8;
1954 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03001955 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001956 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001957 cs.l = 1;
1958 }
1959
Avi Kivity1aa36612011-04-27 13:20:30 +03001960 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
1961 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001962
Avi Kivity717746e2011-04-20 13:37:53 +03001963 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001964 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001965
Avi Kivity717746e2011-04-20 13:37:53 +03001966 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 ctxt->regs[VCPU_REGS_RSP] = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02001968
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001969 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001970}
1971
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001972static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02001973{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001974 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001975 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001976 u64 msr_data;
1977 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08001978 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001979
Gleb Natapova0044752010-02-10 14:21:31 +02001980 /* inject #GP if in real mode or Virtual 8086 mode */
1981 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001982 ctxt->mode == X86EMUL_MODE_VM86)
1983 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001984
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001985 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001986
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02001988 usermode = X86EMUL_MODE_PROT64;
1989 else
1990 usermode = X86EMUL_MODE_PROT32;
1991
1992 cs.dpl = 3;
1993 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03001994 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001995 switch (usermode) {
1996 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001997 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001998 if ((msr_data & 0xfffc) == 0x0)
1999 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002000 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002001 break;
2002 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002003 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002004 if (msr_data == 0x0)
2005 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002006 ss_sel = cs_sel + 8;
2007 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002008 cs.l = 1;
2009 break;
2010 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002011 cs_sel |= SELECTOR_RPL_MASK;
2012 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002013
Avi Kivity1aa36612011-04-27 13:20:30 +03002014 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2015 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002016
Avi Kivity9dac77f2011-06-01 15:34:25 +03002017 ctxt->_eip = ctxt->regs[VCPU_REGS_RDX];
2018 ctxt->regs[VCPU_REGS_RSP] = ctxt->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002019
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002020 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002021}
2022
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002023static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002024{
2025 int iopl;
2026 if (ctxt->mode == X86EMUL_MODE_REAL)
2027 return false;
2028 if (ctxt->mode == X86EMUL_MODE_VM86)
2029 return true;
2030 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002031 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002032}
2033
2034static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002035 u16 port, u16 len)
2036{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002037 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002038 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002039 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002040 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002041 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002042 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002043 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002044
Avi Kivity1aa36612011-04-27 13:20:30 +03002045 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002046 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002047 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002048 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002049 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002050 base = get_desc_base(&tr_seg);
2051#ifdef CONFIG_X86_64
2052 base |= ((u64)base3) << 32;
2053#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002054 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002055 if (r != X86EMUL_CONTINUE)
2056 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002057 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002058 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002059 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002060 if (r != X86EMUL_CONTINUE)
2061 return false;
2062 if ((perm >> bit_idx) & mask)
2063 return false;
2064 return true;
2065}
2066
2067static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002068 u16 port, u16 len)
2069{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002070 if (ctxt->perm_ok)
2071 return true;
2072
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002073 if (emulator_bad_iopl(ctxt))
2074 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002075 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002076
2077 ctxt->perm_ok = true;
2078
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002079 return true;
2080}
2081
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002082static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002083 struct tss_segment_16 *tss)
2084{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002085 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002086 tss->flag = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002087 tss->ax = ctxt->regs[VCPU_REGS_RAX];
2088 tss->cx = ctxt->regs[VCPU_REGS_RCX];
2089 tss->dx = ctxt->regs[VCPU_REGS_RDX];
2090 tss->bx = ctxt->regs[VCPU_REGS_RBX];
2091 tss->sp = ctxt->regs[VCPU_REGS_RSP];
2092 tss->bp = ctxt->regs[VCPU_REGS_RBP];
2093 tss->si = ctxt->regs[VCPU_REGS_RSI];
2094 tss->di = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002095
Avi Kivity1aa36612011-04-27 13:20:30 +03002096 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2097 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2098 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2099 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2100 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002101}
2102
2103static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002104 struct tss_segment_16 *tss)
2105{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002106 int ret;
2107
Avi Kivity9dac77f2011-06-01 15:34:25 +03002108 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002109 ctxt->eflags = tss->flag | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 ctxt->regs[VCPU_REGS_RAX] = tss->ax;
2111 ctxt->regs[VCPU_REGS_RCX] = tss->cx;
2112 ctxt->regs[VCPU_REGS_RDX] = tss->dx;
2113 ctxt->regs[VCPU_REGS_RBX] = tss->bx;
2114 ctxt->regs[VCPU_REGS_RSP] = tss->sp;
2115 ctxt->regs[VCPU_REGS_RBP] = tss->bp;
2116 ctxt->regs[VCPU_REGS_RSI] = tss->si;
2117 ctxt->regs[VCPU_REGS_RDI] = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002118
2119 /*
2120 * SDM says that segment selectors are loaded before segment
2121 * descriptors
2122 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002123 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2124 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2125 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2126 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2127 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002128
2129 /*
2130 * Now load segment descriptors. If fault happenes at this stage
2131 * it is handled in a context of new task
2132 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002133 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002134 if (ret != X86EMUL_CONTINUE)
2135 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002136 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002137 if (ret != X86EMUL_CONTINUE)
2138 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002139 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002140 if (ret != X86EMUL_CONTINUE)
2141 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002142 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002143 if (ret != X86EMUL_CONTINUE)
2144 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002145 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002146 if (ret != X86EMUL_CONTINUE)
2147 return ret;
2148
2149 return X86EMUL_CONTINUE;
2150}
2151
2152static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153 u16 tss_selector, u16 old_tss_sel,
2154 ulong old_tss_base, struct desc_struct *new_desc)
2155{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002156 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002157 struct tss_segment_16 tss_seg;
2158 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002159 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002160
Avi Kivity0f65dd72011-04-20 13:37:53 +03002161 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002162 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002163 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002164 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002165 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002166
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002167 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168
Avi Kivity0f65dd72011-04-20 13:37:53 +03002169 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002170 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002171 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002172 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002173 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002174
Avi Kivity0f65dd72011-04-20 13:37:53 +03002175 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002176 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002177 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002179 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002180
2181 if (old_tss_sel != 0xffff) {
2182 tss_seg.prev_task_link = old_tss_sel;
2183
Avi Kivity0f65dd72011-04-20 13:37:53 +03002184 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002185 &tss_seg.prev_task_link,
2186 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002187 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002188 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002189 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002190 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002191 }
2192
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002193 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002194}
2195
2196static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002197 struct tss_segment_32 *tss)
2198{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002199 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002200 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002201 tss->eflags = ctxt->eflags;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002202 tss->eax = ctxt->regs[VCPU_REGS_RAX];
2203 tss->ecx = ctxt->regs[VCPU_REGS_RCX];
2204 tss->edx = ctxt->regs[VCPU_REGS_RDX];
2205 tss->ebx = ctxt->regs[VCPU_REGS_RBX];
2206 tss->esp = ctxt->regs[VCPU_REGS_RSP];
2207 tss->ebp = ctxt->regs[VCPU_REGS_RBP];
2208 tss->esi = ctxt->regs[VCPU_REGS_RSI];
2209 tss->edi = ctxt->regs[VCPU_REGS_RDI];
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002210
Avi Kivity1aa36612011-04-27 13:20:30 +03002211 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2212 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2213 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2214 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2215 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2216 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2217 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002218}
2219
2220static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002221 struct tss_segment_32 *tss)
2222{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002223 int ret;
2224
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002225 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002226 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002227 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002228 ctxt->eflags = tss->eflags | 2;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002229 ctxt->regs[VCPU_REGS_RAX] = tss->eax;
2230 ctxt->regs[VCPU_REGS_RCX] = tss->ecx;
2231 ctxt->regs[VCPU_REGS_RDX] = tss->edx;
2232 ctxt->regs[VCPU_REGS_RBX] = tss->ebx;
2233 ctxt->regs[VCPU_REGS_RSP] = tss->esp;
2234 ctxt->regs[VCPU_REGS_RBP] = tss->ebp;
2235 ctxt->regs[VCPU_REGS_RSI] = tss->esi;
2236 ctxt->regs[VCPU_REGS_RDI] = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002237
2238 /*
2239 * SDM says that segment selectors are loaded before segment
2240 * descriptors
2241 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002242 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2243 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2244 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2245 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2246 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2247 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2248 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002249
2250 /*
2251 * Now load segment descriptors. If fault happenes at this stage
2252 * it is handled in a context of new task
2253 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002254 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002255 if (ret != X86EMUL_CONTINUE)
2256 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002257 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002258 if (ret != X86EMUL_CONTINUE)
2259 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002260 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002261 if (ret != X86EMUL_CONTINUE)
2262 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002263 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002264 if (ret != X86EMUL_CONTINUE)
2265 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002266 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002267 if (ret != X86EMUL_CONTINUE)
2268 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002269 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002270 if (ret != X86EMUL_CONTINUE)
2271 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002272 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002273 if (ret != X86EMUL_CONTINUE)
2274 return ret;
2275
2276 return X86EMUL_CONTINUE;
2277}
2278
2279static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002280 u16 tss_selector, u16 old_tss_sel,
2281 ulong old_tss_base, struct desc_struct *new_desc)
2282{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002283 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002284 struct tss_segment_32 tss_seg;
2285 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002286 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002287
Avi Kivity0f65dd72011-04-20 13:37:53 +03002288 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002289 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002290 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002291 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002293
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002294 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002295
Avi Kivity0f65dd72011-04-20 13:37:53 +03002296 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002297 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002298 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002299 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002300 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002301
Avi Kivity0f65dd72011-04-20 13:37:53 +03002302 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002303 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002304 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002305 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002306 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002307
2308 if (old_tss_sel != 0xffff) {
2309 tss_seg.prev_task_link = old_tss_sel;
2310
Avi Kivity0f65dd72011-04-20 13:37:53 +03002311 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002312 &tss_seg.prev_task_link,
2313 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002314 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002315 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002316 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002317 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002318 }
2319
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002320 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002321}
2322
2323static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002324 u16 tss_selector, int reason,
2325 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002326{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002327 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002328 struct desc_struct curr_tss_desc, next_tss_desc;
2329 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002330 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002331 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002332 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002333 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002334
2335 /* FIXME: old_tss_base == ~0 ? */
2336
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002337 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002338 if (ret != X86EMUL_CONTINUE)
2339 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002340 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002341 if (ret != X86EMUL_CONTINUE)
2342 return ret;
2343
2344 /* FIXME: check that next_tss_desc is tss */
2345
2346 if (reason != TASK_SWITCH_IRET) {
2347 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002348 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002349 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350 }
2351
Gleb Natapovceffb452010-03-18 15:20:19 +02002352 desc_limit = desc_limit_scaled(&next_tss_desc);
2353 if (!next_tss_desc.p ||
2354 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2355 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002356 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002357 return X86EMUL_PROPAGATE_FAULT;
2358 }
2359
2360 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2361 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002362 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002363 }
2364
2365 if (reason == TASK_SWITCH_IRET)
2366 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2367
2368 /* set back link to prev task only if NT bit is set in eflags
2369 note that old_tss_sel is not used afetr this point */
2370 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2371 old_tss_sel = 0xffff;
2372
2373 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002374 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375 old_tss_base, &next_tss_desc);
2376 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002377 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002378 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002379 if (ret != X86EMUL_CONTINUE)
2380 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002381
2382 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2383 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2384
2385 if (reason != TASK_SWITCH_IRET) {
2386 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002387 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002388 }
2389
Avi Kivity717746e2011-04-20 13:37:53 +03002390 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002391 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002392
Jan Kiszkae269fb22010-04-14 15:51:09 +02002393 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002394 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2395 ctxt->lock_prefix = 0;
2396 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002397 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002398 }
2399
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002400 return ret;
2401}
2402
2403int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002404 u16 tss_selector, int reason,
2405 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002407 int rc;
2408
Avi Kivity9dac77f2011-06-01 15:34:25 +03002409 ctxt->_eip = ctxt->eip;
2410 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002412 rc = emulator_do_task_switch(ctxt, tss_selector, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002413 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002414
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002415 if (rc == X86EMUL_CONTINUE)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002416 ctxt->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002417
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002418 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002419}
2420
Avi Kivity90de84f2010-11-17 15:28:21 +02002421static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
Gleb Natapovd9271122010-03-18 15:20:22 +02002422 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002423{
Gleb Natapova682e352010-03-18 15:20:21 +02002424 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2425
Avi Kivity9dac77f2011-06-01 15:34:25 +03002426 register_address_increment(ctxt, &ctxt->regs[reg], df * op->bytes);
2427 op->addr.mem.ea = register_address(ctxt, ctxt->regs[reg]);
Avi Kivity90de84f2010-11-17 15:28:21 +02002428 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002429}
2430
Avi Kivity7af04fc2010-08-18 14:16:35 +03002431static int em_das(struct x86_emulate_ctxt *ctxt)
2432{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002433 u8 al, old_al;
2434 bool af, cf, old_cf;
2435
2436 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002437 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002438
2439 old_al = al;
2440 old_cf = cf;
2441 cf = false;
2442 af = ctxt->eflags & X86_EFLAGS_AF;
2443 if ((al & 0x0f) > 9 || af) {
2444 al -= 6;
2445 cf = old_cf | (al >= 250);
2446 af = true;
2447 } else {
2448 af = false;
2449 }
2450 if (old_al > 0x99 || old_cf) {
2451 al -= 0x60;
2452 cf = true;
2453 }
2454
Avi Kivity9dac77f2011-06-01 15:34:25 +03002455 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002456 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002457 ctxt->src.type = OP_IMM;
2458 ctxt->src.val = 0;
2459 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002460 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002461 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2462 if (cf)
2463 ctxt->eflags |= X86_EFLAGS_CF;
2464 if (af)
2465 ctxt->eflags |= X86_EFLAGS_AF;
2466 return X86EMUL_CONTINUE;
2467}
2468
Avi Kivity0ef753b2010-08-18 14:51:45 +03002469static int em_call_far(struct x86_emulate_ctxt *ctxt)
2470{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002471 u16 sel, old_cs;
2472 ulong old_eip;
2473 int rc;
2474
Avi Kivity1aa36612011-04-27 13:20:30 +03002475 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002476 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002477
Avi Kivity9dac77f2011-06-01 15:34:25 +03002478 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002479 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002480 return X86EMUL_CONTINUE;
2481
Avi Kivity9dac77f2011-06-01 15:34:25 +03002482 ctxt->_eip = 0;
2483 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002484
Avi Kivity9dac77f2011-06-01 15:34:25 +03002485 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002486 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002487 if (rc != X86EMUL_CONTINUE)
2488 return rc;
2489
Avi Kivity9dac77f2011-06-01 15:34:25 +03002490 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002491 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002492}
2493
Avi Kivity40ece7c2010-08-18 15:12:09 +03002494static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2495{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002496 int rc;
2497
Avi Kivity9dac77f2011-06-01 15:34:25 +03002498 ctxt->dst.type = OP_REG;
2499 ctxt->dst.addr.reg = &ctxt->_eip;
2500 ctxt->dst.bytes = ctxt->op_bytes;
2501 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002502 if (rc != X86EMUL_CONTINUE)
2503 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002504 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RSP], ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002505 return X86EMUL_CONTINUE;
2506}
2507
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002508static int em_add(struct x86_emulate_ctxt *ctxt)
2509{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002510 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002511 return X86EMUL_CONTINUE;
2512}
2513
2514static int em_or(struct x86_emulate_ctxt *ctxt)
2515{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002516 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002517 return X86EMUL_CONTINUE;
2518}
2519
2520static int em_adc(struct x86_emulate_ctxt *ctxt)
2521{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002522 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002523 return X86EMUL_CONTINUE;
2524}
2525
2526static int em_sbb(struct x86_emulate_ctxt *ctxt)
2527{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002528 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002529 return X86EMUL_CONTINUE;
2530}
2531
2532static int em_and(struct x86_emulate_ctxt *ctxt)
2533{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002534 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002535 return X86EMUL_CONTINUE;
2536}
2537
2538static int em_sub(struct x86_emulate_ctxt *ctxt)
2539{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002540 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002541 return X86EMUL_CONTINUE;
2542}
2543
2544static int em_xor(struct x86_emulate_ctxt *ctxt)
2545{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002546 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002547 return X86EMUL_CONTINUE;
2548}
2549
2550static int em_cmp(struct x86_emulate_ctxt *ctxt)
2551{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002552 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002553 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002554 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002555 return X86EMUL_CONTINUE;
2556}
2557
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002558static int em_test(struct x86_emulate_ctxt *ctxt)
2559{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002560 emulate_2op_SrcV(ctxt, "test");
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09002561 return X86EMUL_CONTINUE;
2562}
2563
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002564static int em_xchg(struct x86_emulate_ctxt *ctxt)
2565{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002566 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002567 ctxt->src.val = ctxt->dst.val;
2568 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002569
2570 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002571 ctxt->dst.val = ctxt->src.orig_val;
2572 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09002573 return X86EMUL_CONTINUE;
2574}
2575
Avi Kivity5c82aa22010-08-18 18:31:43 +03002576static int em_imul(struct x86_emulate_ctxt *ctxt)
2577{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002578 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03002579 return X86EMUL_CONTINUE;
2580}
2581
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002582static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2583{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002584 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002585 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002586}
2587
Avi Kivity61429142010-08-19 15:13:00 +03002588static int em_cwd(struct x86_emulate_ctxt *ctxt)
2589{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002590 ctxt->dst.type = OP_REG;
2591 ctxt->dst.bytes = ctxt->src.bytes;
2592 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
2593 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03002594
2595 return X86EMUL_CONTINUE;
2596}
2597
Avi Kivity48bb5d32010-08-18 18:54:34 +03002598static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2599{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002600 u64 tsc = 0;
2601
Avi Kivity717746e2011-04-20 13:37:53 +03002602 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002603 ctxt->regs[VCPU_REGS_RAX] = (u32)tsc;
2604 ctxt->regs[VCPU_REGS_RDX] = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03002605 return X86EMUL_CONTINUE;
2606}
2607
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002608static int em_mov(struct x86_emulate_ctxt *ctxt)
2609{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002610 ctxt->dst.val = ctxt->src.val;
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002611 return X86EMUL_CONTINUE;
2612}
2613
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002614static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
2615{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002616 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002617 return emulate_ud(ctxt);
2618
Avi Kivity9dac77f2011-06-01 15:34:25 +03002619 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002620 return X86EMUL_CONTINUE;
2621}
2622
2623static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
2624{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002625 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002626
Avi Kivity9dac77f2011-06-01 15:34:25 +03002627 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002628 return emulate_ud(ctxt);
2629
Avi Kivity9dac77f2011-06-01 15:34:25 +03002630 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002631 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
2632
2633 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002634 ctxt->dst.type = OP_NONE;
2635 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09002636}
2637
Avi Kivityaa97bb42010-01-20 18:09:23 +02002638static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2639{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002640 memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
Avi Kivityaa97bb42010-01-20 18:09:23 +02002641 return X86EMUL_CONTINUE;
2642}
2643
Avi Kivity38503912011-03-31 18:48:09 +02002644static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2645{
Avi Kivity9fa088f2011-03-31 18:54:30 +02002646 int rc;
2647 ulong linear;
2648
Avi Kivity9dac77f2011-06-01 15:34:25 +03002649 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002650 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03002651 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002652 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002653 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02002654 return X86EMUL_CONTINUE;
2655}
2656
Avi Kivity2d04a052011-04-20 15:32:49 +03002657static int em_clts(struct x86_emulate_ctxt *ctxt)
2658{
2659 ulong cr0;
2660
2661 cr0 = ctxt->ops->get_cr(ctxt, 0);
2662 cr0 &= ~X86_CR0_TS;
2663 ctxt->ops->set_cr(ctxt, 0, cr0);
2664 return X86EMUL_CONTINUE;
2665}
2666
Avi Kivity26d05cc2011-04-21 12:07:59 +03002667static int em_vmcall(struct x86_emulate_ctxt *ctxt)
2668{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002669 int rc;
2670
Avi Kivity9dac77f2011-06-01 15:34:25 +03002671 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002672 return X86EMUL_UNHANDLEABLE;
2673
2674 rc = ctxt->ops->fix_hypercall(ctxt);
2675 if (rc != X86EMUL_CONTINUE)
2676 return rc;
2677
2678 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002679 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002680 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002681 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002682 return X86EMUL_CONTINUE;
2683}
2684
2685static int em_lgdt(struct x86_emulate_ctxt *ctxt)
2686{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002687 struct desc_ptr desc_ptr;
2688 int rc;
2689
Avi Kivity9dac77f2011-06-01 15:34:25 +03002690 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03002691 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002692 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002693 if (rc != X86EMUL_CONTINUE)
2694 return rc;
2695 ctxt->ops->set_gdt(ctxt, &desc_ptr);
2696 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002697 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002698 return X86EMUL_CONTINUE;
2699}
2700
Avi Kivity5ef39c72011-04-21 12:21:50 +03002701static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03002702{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002703 int rc;
2704
Avi Kivity5ef39c72011-04-21 12:21:50 +03002705 rc = ctxt->ops->fix_hypercall(ctxt);
2706
Avi Kivity26d05cc2011-04-21 12:07:59 +03002707 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002708 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002709 return rc;
2710}
2711
2712static int em_lidt(struct x86_emulate_ctxt *ctxt)
2713{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002714 struct desc_ptr desc_ptr;
2715 int rc;
2716
Avi Kivity9dac77f2011-06-01 15:34:25 +03002717 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09002718 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03002719 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002720 if (rc != X86EMUL_CONTINUE)
2721 return rc;
2722 ctxt->ops->set_idt(ctxt, &desc_ptr);
2723 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002724 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002725 return X86EMUL_CONTINUE;
2726}
2727
2728static int em_smsw(struct x86_emulate_ctxt *ctxt)
2729{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002730 ctxt->dst.bytes = 2;
2731 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03002732 return X86EMUL_CONTINUE;
2733}
2734
2735static int em_lmsw(struct x86_emulate_ctxt *ctxt)
2736{
Avi Kivity26d05cc2011-04-21 12:07:59 +03002737 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03002738 | (ctxt->src.val & 0x0f));
2739 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03002740 return X86EMUL_CONTINUE;
2741}
2742
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002743static int em_loop(struct x86_emulate_ctxt *ctxt)
2744{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002745 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
2746 if ((address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) != 0) &&
2747 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
2748 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002749
2750 return X86EMUL_CONTINUE;
2751}
2752
2753static int em_jcxz(struct x86_emulate_ctxt *ctxt)
2754{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002755 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0)
2756 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09002757
2758 return X86EMUL_CONTINUE;
2759}
2760
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09002761static int em_cli(struct x86_emulate_ctxt *ctxt)
2762{
2763 if (emulator_bad_iopl(ctxt))
2764 return emulate_gp(ctxt, 0);
2765
2766 ctxt->eflags &= ~X86_EFLAGS_IF;
2767 return X86EMUL_CONTINUE;
2768}
2769
2770static int em_sti(struct x86_emulate_ctxt *ctxt)
2771{
2772 if (emulator_bad_iopl(ctxt))
2773 return emulate_gp(ctxt, 0);
2774
2775 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
2776 ctxt->eflags |= X86_EFLAGS_IF;
2777 return X86EMUL_CONTINUE;
2778}
2779
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002780static bool valid_cr(int nr)
2781{
2782 switch (nr) {
2783 case 0:
2784 case 2 ... 4:
2785 case 8:
2786 return true;
2787 default:
2788 return false;
2789 }
2790}
2791
2792static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2793{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002794 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002795 return emulate_ud(ctxt);
2796
2797 return X86EMUL_CONTINUE;
2798}
2799
2800static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2801{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002802 u64 new_val = ctxt->src.val64;
2803 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002804 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002805
2806 static u64 cr_reserved_bits[] = {
2807 0xffffffff00000000ULL,
2808 0, 0, 0, /* CR3 checked later */
2809 CR4_RESERVED_BITS,
2810 0, 0, 0,
2811 CR8_RESERVED_BITS,
2812 };
2813
2814 if (!valid_cr(cr))
2815 return emulate_ud(ctxt);
2816
2817 if (new_val & cr_reserved_bits[cr])
2818 return emulate_gp(ctxt, 0);
2819
2820 switch (cr) {
2821 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002822 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002823 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2824 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2825 return emulate_gp(ctxt, 0);
2826
Avi Kivity717746e2011-04-20 13:37:53 +03002827 cr4 = ctxt->ops->get_cr(ctxt, 4);
2828 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002829
2830 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2831 !(cr4 & X86_CR4_PAE))
2832 return emulate_gp(ctxt, 0);
2833
2834 break;
2835 }
2836 case 3: {
2837 u64 rsvd = 0;
2838
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002839 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
2840 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002841 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002842 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002843 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03002844 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002845 rsvd = CR3_NONPAE_RESERVED_BITS;
2846
2847 if (new_val & rsvd)
2848 return emulate_gp(ctxt, 0);
2849
2850 break;
2851 }
2852 case 4: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002853 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002854
Avi Kivity717746e2011-04-20 13:37:53 +03002855 cr4 = ctxt->ops->get_cr(ctxt, 4);
2856 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002857
2858 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2859 return emulate_gp(ctxt, 0);
2860
2861 break;
2862 }
2863 }
2864
2865 return X86EMUL_CONTINUE;
2866}
2867
Joerg Roedel3b88e412011-04-04 12:39:29 +02002868static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2869{
2870 unsigned long dr7;
2871
Avi Kivity717746e2011-04-20 13:37:53 +03002872 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002873
2874 /* Check if DR7.Global_Enable is set */
2875 return dr7 & (1 << 13);
2876}
2877
2878static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2879{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002880 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002881 u64 cr4;
2882
2883 if (dr > 7)
2884 return emulate_ud(ctxt);
2885
Avi Kivity717746e2011-04-20 13:37:53 +03002886 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002887 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2888 return emulate_ud(ctxt);
2889
2890 if (check_dr7_gd(ctxt))
2891 return emulate_db(ctxt);
2892
2893 return X86EMUL_CONTINUE;
2894}
2895
2896static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2897{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002898 u64 new_val = ctxt->src.val64;
2899 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02002900
2901 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2902 return emulate_gp(ctxt, 0);
2903
2904 return check_dr_read(ctxt);
2905}
2906
Joerg Roedel01de8b02011-04-04 12:39:31 +02002907static int check_svme(struct x86_emulate_ctxt *ctxt)
2908{
2909 u64 efer;
2910
Avi Kivity717746e2011-04-20 13:37:53 +03002911 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002912
2913 if (!(efer & EFER_SVME))
2914 return emulate_ud(ctxt);
2915
2916 return X86EMUL_CONTINUE;
2917}
2918
2919static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2920{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002921 u64 rax = ctxt->regs[VCPU_REGS_RAX];
Joerg Roedel01de8b02011-04-04 12:39:31 +02002922
2923 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07002924 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002925 return emulate_gp(ctxt, 0);
2926
2927 return check_svme(ctxt);
2928}
2929
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002930static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2931{
Avi Kivity717746e2011-04-20 13:37:53 +03002932 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002933
Avi Kivity717746e2011-04-20 13:37:53 +03002934 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002935 return emulate_ud(ctxt);
2936
2937 return X86EMUL_CONTINUE;
2938}
2939
Joerg Roedel80612522011-04-04 12:39:33 +02002940static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2941{
Avi Kivity717746e2011-04-20 13:37:53 +03002942 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002943 u64 rcx = ctxt->regs[VCPU_REGS_RCX];
Joerg Roedel80612522011-04-04 12:39:33 +02002944
Avi Kivity717746e2011-04-20 13:37:53 +03002945 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002946 (rcx > 3))
2947 return emulate_gp(ctxt, 0);
2948
2949 return X86EMUL_CONTINUE;
2950}
2951
Joerg Roedelf6511932011-04-04 12:39:35 +02002952static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2953{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002954 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
2955 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002956 return emulate_gp(ctxt, 0);
2957
2958 return X86EMUL_CONTINUE;
2959}
2960
2961static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2962{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002963 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
2964 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02002965 return emulate_gp(ctxt, 0);
2966
2967 return X86EMUL_CONTINUE;
2968}
2969
Avi Kivity73fba5f2010-07-29 15:11:53 +03002970#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002971#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002972#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2973 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002974#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002975#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002976#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
Avi Kivity46561642011-04-24 14:09:59 +03002977#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002978#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002979#define II(_f, _e, _i) \
2980 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002981#define IIP(_f, _e, _i, _p) \
2982 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2983 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002984#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002985
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002986#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002987#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002988#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2989
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002990#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
2991 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
2992 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03002993
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002994static struct opcode group7_rm1[] = {
2995 DI(SrcNone | ModRM | Priv, monitor),
2996 DI(SrcNone | ModRM | Priv, mwait),
2997 N, N, N, N, N, N,
2998};
2999
Joerg Roedel01de8b02011-04-04 12:39:31 +02003000static struct opcode group7_rm3[] = {
3001 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003002 II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003003 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
3004 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
3005 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
3006 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
3007 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
3008 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
3009};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003010
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003011static struct opcode group7_rm7[] = {
3012 N,
3013 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
3014 N, N, N, N, N, N,
3015};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003016
Avi Kivity73fba5f2010-07-29 15:11:53 +03003017static struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003018 I(Lock, em_add),
3019 I(Lock, em_or),
3020 I(Lock, em_adc),
3021 I(Lock, em_sbb),
3022 I(Lock, em_and),
3023 I(Lock, em_sub),
3024 I(Lock, em_xor),
3025 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003026};
3027
3028static struct opcode group1A[] = {
3029 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
3030};
3031
3032static struct opcode group3[] = {
3033 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
3034 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03003035 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003036};
3037
3038static struct opcode group4[] = {
3039 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
3040 N, N, N, N, N, N,
3041};
3042
3043static struct opcode group5[] = {
3044 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03003045 D(SrcMem | ModRM | Stack),
3046 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003047 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
3048 D(SrcMem | ModRM | Stack), N,
3049};
3050
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003051static struct opcode group6[] = {
3052 DI(ModRM | Prot, sldt),
3053 DI(ModRM | Prot, str),
3054 DI(ModRM | Prot | Priv, lldt),
3055 DI(ModRM | Prot | Priv, ltr),
3056 N, N, N, N,
3057};
3058
Avi Kivity73fba5f2010-07-29 15:11:53 +03003059static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003060 DI(ModRM | Mov | DstMem | Priv, sgdt),
3061 DI(ModRM | Mov | DstMem | Priv, sidt),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003062 II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
3063 II(ModRM | SrcMem | Priv, em_lidt, lidt),
3064 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3065 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
3066 II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003067}, {
Avi Kivity5ef39c72011-04-21 12:21:50 +03003068 I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
3069 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003070 N, EXT(0, group7_rm3),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003071 II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
3072 II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003073} };
3074
3075static struct opcode group8[] = {
3076 N, N, N, N,
3077 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
3078 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
3079};
3080
3081static struct group_dual group9 = { {
3082 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
3083}, {
3084 N, N, N, N, N, N, N, N,
3085} };
3086
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003087static struct opcode group11[] = {
3088 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
3089};
3090
Avi Kivityaa97bb42010-01-20 18:09:23 +02003091static struct gprefix pfx_0f_6f_0f_7f = {
3092 N, N, N, I(Sse, em_movdqu),
3093};
3094
Avi Kivity73fba5f2010-07-29 15:11:53 +03003095static struct opcode opcode_table[256] = {
3096 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003097 I6ALU(Lock, em_add),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003098 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3099 /* 0x08 - 0x0F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003100 I6ALU(Lock, em_or),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003101 D(ImplicitOps | Stack | No64), N,
3102 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003103 I6ALU(Lock, em_adc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003104 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3105 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003106 I6ALU(Lock, em_sbb),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003107 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
3108 /* 0x20 - 0x27 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003109 I6ALU(Lock, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003110 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003111 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003112 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003113 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003114 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003115 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003116 /* 0x40 - 0x4F */
3117 X16(D(DstReg)),
3118 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003119 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003120 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003121 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003122 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003123 I(ImplicitOps | Stack | No64, em_pusha),
3124 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003125 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3126 N, N, N, N,
3127 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003128 I(SrcImm | Mov | Stack, em_push),
3129 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003130 I(SrcImmByte | Mov | Stack, em_push),
3131 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003132 D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
3133 D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003134 /* 0x70 - 0x7F */
3135 X16(D(SrcImmByte)),
3136 /* 0x80 - 0x87 */
3137 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
3138 G(DstMem | SrcImm | ModRM | Group, group1),
3139 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
3140 G(DstMem | SrcImmByte | ModRM | Group, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003141 I2bv(DstMem | SrcReg | ModRM, em_test),
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003142 I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003143 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003144 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
3145 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003146 I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
3147 D(ModRM | SrcMem | NoAccess | DstReg),
3148 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3149 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003150 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003151 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003152 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003153 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003154 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003155 II(ImplicitOps | Stack, em_pushf, pushf),
3156 II(ImplicitOps | Stack, em_popf, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003157 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003158 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
3159 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
3160 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003161 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003162 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003163 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003164 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3165 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003166 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003167 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003168 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003169 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003170 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003171 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003172 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003173 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003174 I(ImplicitOps | Stack, em_ret),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003175 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003176 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003177 /* 0xC8 - 0xCF */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003178 N, N, N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003179 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003180 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003181 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003182 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003183 N, N, N, N,
3184 /* 0xD8 - 0xDF */
3185 N, N, N, N, N, N, N, N,
3186 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003187 X3(I(SrcImmByte, em_loop)),
3188 I(SrcImmByte, em_jcxz),
Joerg Roedelf6511932011-04-04 12:39:35 +02003189 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
3190 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003191 /* 0xE8 - 0xEF */
3192 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003193 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003194 D2bvIP(SrcDX | DstAcc, in, check_perm_in),
3195 D2bvIP(SrcAcc | DstDX, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003196 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003197 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003198 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3199 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003200 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003201 D(ImplicitOps), D(ImplicitOps),
3202 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003203 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3204};
3205
3206static struct opcode twobyte_table[256] = {
3207 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003208 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003209 N, I(ImplicitOps | VendorSpecific, em_syscall),
3210 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003211 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003212 N, D(ImplicitOps | ModRM), N, N,
3213 /* 0x10 - 0x1F */
3214 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3215 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003216 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003217 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003218 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003219 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003220 N, N, N, N,
3221 N, N, N, N, N, N, N, N,
3222 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003223 DI(ImplicitOps | Priv, wrmsr),
3224 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3225 DI(ImplicitOps | Priv, rdmsr),
3226 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003227 I(ImplicitOps | VendorSpecific, em_sysenter),
3228 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003229 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003230 N, N, N, N, N, N, N, N,
3231 /* 0x40 - 0x4F */
3232 X16(D(DstReg | SrcMem | ModRM | Mov)),
3233 /* 0x50 - 0x5F */
3234 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3235 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003236 N, N, N, N,
3237 N, N, N, N,
3238 N, N, N, N,
3239 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003240 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003241 N, N, N, N,
3242 N, N, N, N,
3243 N, N, N, N,
3244 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003245 /* 0x80 - 0x8F */
3246 X16(D(SrcImm)),
3247 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003248 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003249 /* 0xA0 - 0xA7 */
3250 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003251 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003252 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3253 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3254 /* 0xA8 - 0xAF */
3255 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003256 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003257 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3258 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003259 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003260 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003261 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003262 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3263 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3264 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003265 /* 0xB8 - 0xBF */
3266 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003267 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003268 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3269 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003270 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003271 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003272 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003273 N, N, N, GD(0, &group9),
3274 N, N, N, N, N, N, N, N,
3275 /* 0xD0 - 0xDF */
3276 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3277 /* 0xE0 - 0xEF */
3278 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3279 /* 0xF0 - 0xFF */
3280 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3281};
3282
3283#undef D
3284#undef N
3285#undef G
3286#undef GD
3287#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003288#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003289#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003290
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003291#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003292#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003293#undef I2bv
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003294#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003295
Avi Kivity9dac77f2011-06-01 15:34:25 +03003296static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03003297{
3298 unsigned size;
3299
Avi Kivity9dac77f2011-06-01 15:34:25 +03003300 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003301 if (size == 8)
3302 size = 4;
3303 return size;
3304}
3305
3306static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3307 unsigned size, bool sign_extension)
3308{
Avi Kivity39f21ee2010-08-18 19:20:21 +03003309 int rc = X86EMUL_CONTINUE;
3310
3311 op->type = OP_IMM;
3312 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003313 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003314 /* NB. Immediates are sign-extended as necessary. */
3315 switch (op->bytes) {
3316 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003317 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003318 break;
3319 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003320 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003321 break;
3322 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003323 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003324 break;
3325 }
3326 if (!sign_extension) {
3327 switch (op->bytes) {
3328 case 1:
3329 op->val &= 0xff;
3330 break;
3331 case 2:
3332 op->val &= 0xffff;
3333 break;
3334 case 4:
3335 op->val &= 0xffffffff;
3336 break;
3337 }
3338 }
3339done:
3340 return rc;
3341}
3342
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003343int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003344{
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003345 int rc = X86EMUL_CONTINUE;
3346 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03003347 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003348 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03003349 struct opcode opcode;
Avi Kivitycb16c342011-06-19 19:21:11 +03003350 struct operand memop = { .type = OP_NONE }, *memopp = NULL;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003351
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->_eip = ctxt->eip;
3353 ctxt->fetch.start = ctxt->_eip;
3354 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01003355 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003356 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003357
3358 switch (mode) {
3359 case X86EMUL_MODE_REAL:
3360 case X86EMUL_MODE_VM86:
3361 case X86EMUL_MODE_PROT16:
3362 def_op_bytes = def_ad_bytes = 2;
3363 break;
3364 case X86EMUL_MODE_PROT32:
3365 def_op_bytes = def_ad_bytes = 4;
3366 break;
3367#ifdef CONFIG_X86_64
3368 case X86EMUL_MODE_PROT64:
3369 def_op_bytes = 4;
3370 def_ad_bytes = 8;
3371 break;
3372#endif
3373 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003374 return EMULATION_FAILED;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003375 }
3376
Avi Kivity9dac77f2011-06-01 15:34:25 +03003377 ctxt->op_bytes = def_op_bytes;
3378 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003379
3380 /* Legacy prefixes. */
3381 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003382 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003383 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003384 op_prefix = true;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003385 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003386 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003387 break;
3388 case 0x67: /* address-size override */
3389 if (mode == X86EMUL_MODE_PROT64)
3390 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003391 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003392 else
3393 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003394 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003395 break;
3396 case 0x26: /* ES override */
3397 case 0x2e: /* CS override */
3398 case 0x36: /* SS override */
3399 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003400 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003401 break;
3402 case 0x64: /* FS override */
3403 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003404 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003405 break;
3406 case 0x40 ... 0x4f: /* REX */
3407 if (mode != X86EMUL_MODE_PROT64)
3408 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003409 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003410 continue;
3411 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003412 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003413 break;
3414 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003415 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003416 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003417 break;
3418 default:
3419 goto done_prefixes;
3420 }
3421
3422 /* Any legacy prefix after a REX prefix nullifies its effect. */
3423
Avi Kivity9dac77f2011-06-01 15:34:25 +03003424 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003425 }
3426
3427done_prefixes:
3428
3429 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003430 if (ctxt->rex_prefix & 8)
3431 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003432
3433 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003434 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003435 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003436 if (ctxt->b == 0x0f) {
3437 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003438 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003439 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003440 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003441 ctxt->d = opcode.flags;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003442
Avi Kivity9dac77f2011-06-01 15:34:25 +03003443 while (ctxt->d & GroupMask) {
3444 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03003445 case Group:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003446 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003447 --ctxt->_eip;
3448 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03003449 opcode = opcode.u.group[goffset];
3450 break;
3451 case GroupDual:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003452 ctxt->modrm = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003453 --ctxt->_eip;
3454 goffset = (ctxt->modrm >> 3) & 7;
3455 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03003456 opcode = opcode.u.gdual->mod3[goffset];
3457 else
3458 opcode = opcode.u.gdual->mod012[goffset];
3459 break;
3460 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003461 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02003462 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03003463 break;
3464 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003465 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003466 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003467 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03003468 switch (simd_prefix) {
3469 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3470 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3471 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3472 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3473 }
3474 break;
3475 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003476 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003477 }
Avi Kivity46561642011-04-24 14:09:59 +03003478
Avi Kivity9dac77f2011-06-01 15:34:25 +03003479 ctxt->d &= ~GroupMask;
3480 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003481 }
3482
Avi Kivity9dac77f2011-06-01 15:34:25 +03003483 ctxt->execute = opcode.u.execute;
3484 ctxt->check_perm = opcode.check_perm;
3485 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003486
3487 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003488 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003489 return EMULATION_FAILED;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003490
Avi Kivity9dac77f2011-06-01 15:34:25 +03003491 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003492 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02003493
Avi Kivity9dac77f2011-06-01 15:34:25 +03003494 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
3495 ctxt->op_bytes = 8;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003496
Avi Kivity9dac77f2011-06-01 15:34:25 +03003497 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003498 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003499 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003500 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003501 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003502 }
3503
Avi Kivity9dac77f2011-06-01 15:34:25 +03003504 if (ctxt->d & Sse)
3505 ctxt->op_bytes = 16;
Avi Kivity12537912011-03-29 11:41:27 +02003506
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003507 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003508 if (ctxt->d & ModRM) {
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003509 rc = decode_modrm(ctxt, &memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003510 if (!ctxt->has_seg_override)
3511 set_seg_override(ctxt, ctxt->modrm_seg);
3512 } else if (ctxt->d & MemAbs)
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09003513 rc = decode_abs(ctxt, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003514 if (rc != X86EMUL_CONTINUE)
3515 goto done;
3516
Avi Kivity9dac77f2011-06-01 15:34:25 +03003517 if (!ctxt->has_seg_override)
3518 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003519
Avi Kivity9dac77f2011-06-01 15:34:25 +03003520 memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003521
Avi Kivity9dac77f2011-06-01 15:34:25 +03003522 if (memop.type == OP_MEM && ctxt->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003523 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003524
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003525 /*
3526 * Decode and fetch the source operand: register, memory
3527 * or immediate.
3528 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003529 switch (ctxt->d & SrcMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003530 case SrcNone:
3531 break;
3532 case SrcReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003533 decode_register_operand(ctxt, &ctxt->src, 0);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003534 break;
3535 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003536 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003537 goto srcmem_common;
3538 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003539 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003540 goto srcmem_common;
3541 case SrcMem:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003542 memop.bytes = (ctxt->d & ByteOp) ? 1 :
3543 ctxt->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003544 srcmem_common:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003545 ctxt->src = memop;
3546 memopp = &ctxt->src;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003547 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003548 case SrcImmU16:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003549 rc = decode_imm(ctxt, &ctxt->src, 2, false);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003550 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003551 case SrcImm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003552 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003553 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003554 case SrcImmU:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003555 rc = decode_imm(ctxt, &ctxt->src, imm_size(ctxt), false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003556 break;
3557 case SrcImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003558 rc = decode_imm(ctxt, &ctxt->src, 1, true);
Avi Kivity39f21ee2010-08-18 19:20:21 +03003559 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003560 case SrcImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003561 rc = decode_imm(ctxt, &ctxt->src, 1, false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003562 break;
3563 case SrcAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003564 ctxt->src.type = OP_REG;
3565 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3566 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3567 fetch_register_operand(&ctxt->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003568 break;
3569 case SrcOne:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003570 ctxt->src.bytes = 1;
3571 ctxt->src.val = 1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003572 break;
3573 case SrcSI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003574 ctxt->src.type = OP_MEM;
3575 ctxt->src.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3576 ctxt->src.addr.mem.ea =
3577 register_address(ctxt, ctxt->regs[VCPU_REGS_RSI]);
3578 ctxt->src.addr.mem.seg = seg_override(ctxt);
3579 ctxt->src.val = 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003580 break;
3581 case SrcImmFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003582 ctxt->src.type = OP_IMM;
3583 ctxt->src.addr.mem.ea = ctxt->_eip;
3584 ctxt->src.bytes = ctxt->op_bytes + 2;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +09003585 insn_fetch_arr(ctxt->src.valptr, ctxt->src.bytes, ctxt);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003586 break;
3587 case SrcMemFAddr:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003588 memop.bytes = ctxt->op_bytes + 2;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003589 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003590 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003591 case SrcDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003592 ctxt->src.type = OP_REG;
3593 ctxt->src.bytes = 2;
3594 ctxt->src.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3595 fetch_register_operand(&ctxt->src);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003596 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003597 }
3598
Avi Kivity39f21ee2010-08-18 19:20:21 +03003599 if (rc != X86EMUL_CONTINUE)
3600 goto done;
3601
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003602 /*
3603 * Decode and fetch the second source operand: register, memory
3604 * or immediate.
3605 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003606 switch (ctxt->d & Src2Mask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003607 case Src2None:
3608 break;
3609 case Src2CL:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003610 ctxt->src2.bytes = 1;
Avi Kivity9be3be12011-09-13 10:45:38 +03003611 ctxt->src2.val = ctxt->regs[VCPU_REGS_RCX] & 0xff;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003612 break;
3613 case Src2ImmByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003614 rc = decode_imm(ctxt, &ctxt->src2, 1, true);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003615 break;
3616 case Src2One:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003617 ctxt->src2.bytes = 1;
3618 ctxt->src2.val = 1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003619 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003620 case Src2Imm:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003621 rc = decode_imm(ctxt, &ctxt->src2, imm_size(ctxt), true);
Avi Kivity7db41eb2010-08-18 19:25:28 +03003622 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003623 }
3624
Avi Kivity39f21ee2010-08-18 19:20:21 +03003625 if (rc != X86EMUL_CONTINUE)
3626 goto done;
3627
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003628 /* Decode and fetch the destination operand: register or memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003629 switch (ctxt->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003630 case DstReg:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003631 decode_register_operand(ctxt, &ctxt->dst,
3632 ctxt->twobyte && (ctxt->b == 0xb6 || ctxt->b == 0xb7));
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003633 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003634 case DstImmUByte:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003635 ctxt->dst.type = OP_IMM;
3636 ctxt->dst.addr.mem.ea = ctxt->_eip;
3637 ctxt->dst.bytes = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09003638 ctxt->dst.val = insn_fetch(u8, ctxt);
Wei Yongjun943858e2010-08-06 11:36:51 +08003639 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003640 case DstMem:
3641 case DstMem64:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003642 ctxt->dst = memop;
3643 memopp = &ctxt->dst;
3644 if ((ctxt->d & DstMask) == DstMem64)
3645 ctxt->dst.bytes = 8;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003646 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03003647 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3648 if (ctxt->d & BitOp)
3649 fetch_bit_operand(ctxt);
3650 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003651 break;
3652 case DstAcc:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003653 ctxt->dst.type = OP_REG;
3654 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3655 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RAX];
3656 fetch_register_operand(&ctxt->dst);
3657 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003658 break;
3659 case DstDI:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003660 ctxt->dst.type = OP_MEM;
3661 ctxt->dst.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
3662 ctxt->dst.addr.mem.ea =
3663 register_address(ctxt, ctxt->regs[VCPU_REGS_RDI]);
3664 ctxt->dst.addr.mem.seg = VCPU_SREG_ES;
3665 ctxt->dst.val = 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003666 break;
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003667 case DstDX:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003668 ctxt->dst.type = OP_REG;
3669 ctxt->dst.bytes = 2;
3670 ctxt->dst.addr.reg = &ctxt->regs[VCPU_REGS_RDX];
3671 fetch_register_operand(&ctxt->dst);
Marcelo Tosatti221192b2011-05-30 15:23:14 -03003672 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003673 case ImplicitOps:
3674 /* Special instructions do their own operand decoding. */
3675 default:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003676 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivitycb16c342011-06-19 19:21:11 +03003677 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003678 }
3679
3680done:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003681 if (memopp && memopp->type == OP_MEM && ctxt->rip_relative)
3682 memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03003683
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09003684 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003685}
3686
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003687static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3688{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003689 /* The second termination condition only applies for REPE
3690 * and REPNE. Test if the repeat string operation prefix is
3691 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3692 * corresponding termination condition according to:
3693 * - if REPE/REPZ and ZF = 0 then done
3694 * - if REPNE/REPNZ and ZF = 1 then done
3695 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003696 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
3697 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
3698 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003699 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03003700 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003701 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3702 return true;
3703
3704 return false;
3705}
3706
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003707int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003708{
Avi Kivity9aabc882010-07-29 15:11:50 +03003709 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003710 u64 msr_data;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003711 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003712 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003713
Avi Kivity9dac77f2011-06-01 15:34:25 +03003714 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003715
Avi Kivity9dac77f2011-06-01 15:34:25 +03003716 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003717 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003718 goto done;
3719 }
3720
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003721 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003722 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003723 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003724 goto done;
3725 }
3726
Avi Kivity9dac77f2011-06-01 15:34:25 +03003727 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003728 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003729 goto done;
3730 }
3731
Avi Kivity9dac77f2011-06-01 15:34:25 +03003732 if ((ctxt->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003733 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3734 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003735 rc = emulate_ud(ctxt);
3736 goto done;
3737 }
3738
Avi Kivity9dac77f2011-06-01 15:34:25 +03003739 if ((ctxt->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003740 rc = emulate_nm(ctxt);
3741 goto done;
3742 }
3743
Avi Kivity9dac77f2011-06-01 15:34:25 +03003744 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3745 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003746 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003747 if (rc != X86EMUL_CONTINUE)
3748 goto done;
3749 }
3750
Gleb Natapove92805a2010-02-10 14:21:35 +02003751 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003752 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003753 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003754 goto done;
3755 }
3756
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003757 /* Instruction can only be executed in protected mode */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003758 if ((ctxt->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003759 rc = emulate_ud(ctxt);
3760 goto done;
3761 }
3762
Joerg Roedeld09beab2011-04-04 12:39:25 +02003763 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003764 if (ctxt->check_perm) {
3765 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02003766 if (rc != X86EMUL_CONTINUE)
3767 goto done;
3768 }
3769
Avi Kivity9dac77f2011-06-01 15:34:25 +03003770 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3771 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003772 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003773 if (rc != X86EMUL_CONTINUE)
3774 goto done;
3775 }
3776
Avi Kivity9dac77f2011-06-01 15:34:25 +03003777 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003778 /* All REP prefixes have the same first termination condition */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003779 if (address_mask(ctxt, ctxt->regs[VCPU_REGS_RCX]) == 0) {
3780 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003781 goto done;
3782 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003783 }
3784
Avi Kivity9dac77f2011-06-01 15:34:25 +03003785 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
3786 rc = segmented_read(ctxt, ctxt->src.addr.mem,
3787 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003788 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003789 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003790 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003791 }
3792
Avi Kivity9dac77f2011-06-01 15:34:25 +03003793 if (ctxt->src2.type == OP_MEM) {
3794 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
3795 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003796 if (rc != X86EMUL_CONTINUE)
3797 goto done;
3798 }
3799
Avi Kivity9dac77f2011-06-01 15:34:25 +03003800 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003801 goto special_insn;
3802
3803
Avi Kivity9dac77f2011-06-01 15:34:25 +03003804 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003805 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003806 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
3807 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003808 if (rc != X86EMUL_CONTINUE)
3809 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003810 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03003811 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003812
Avi Kivity018a98d2007-11-27 19:30:56 +02003813special_insn:
3814
Avi Kivity9dac77f2011-06-01 15:34:25 +03003815 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
3816 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003817 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003818 if (rc != X86EMUL_CONTINUE)
3819 goto done;
3820 }
3821
Avi Kivity9dac77f2011-06-01 15:34:25 +03003822 if (ctxt->execute) {
3823 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03003824 if (rc != X86EMUL_CONTINUE)
3825 goto done;
3826 goto writeback;
3827 }
3828
Avi Kivity9dac77f2011-06-01 15:34:25 +03003829 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830 goto twobyte_insn;
3831
Avi Kivity9dac77f2011-06-01 15:34:25 +03003832 switch (ctxt->b) {
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003833 case 0x06: /* push es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003834 rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003835 break;
3836 case 0x07: /* pop es */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003837 rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003838 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003839 case 0x0e: /* push cs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003840 rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003841 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003842 case 0x16: /* push ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003843 rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003844 break;
3845 case 0x17: /* pop ss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003846 rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003847 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003848 case 0x1e: /* push ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003849 rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003850 break;
3851 case 0x1f: /* pop ds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003852 rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003853 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003854 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003855 emulate_1op("inc", ctxt->dst, ctxt->eflags);
Avi Kivity33615aa2007-10-31 11:15:56 +02003856 break;
3857 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003858 emulate_1op("dec", ctxt->dst, ctxt->eflags);
Avi Kivity33615aa2007-10-31 11:15:56 +02003859 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003861 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003863 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003865 case 0x6c: /* insb */
3866 case 0x6d: /* insw/insd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003867 ctxt->src.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003868 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003869 case 0x6e: /* outsb */
3870 case 0x6f: /* outsw/outsd */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003871 ctxt->dst.val = ctxt->regs[VCPU_REGS_RDX];
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003872 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003873 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003874 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003875 if (test_cc(ctxt->b, ctxt->eflags))
3876 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003877 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003878 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003879 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003880 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 case 0x8f: /* pop (sole member of Grp1a) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003882 rc = em_grp1a(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003884 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003885 if (ctxt->dst.addr.reg == &ctxt->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003886 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003887 rc = em_xchg(ctxt);
3888 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003889 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003890 switch (ctxt->op_bytes) {
3891 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
3892 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
3893 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003894 }
3895 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003896 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003897 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003898 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003899 case 0xc4: /* les */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003900 rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003901 break;
3902 case 0xc5: /* lds */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003903 rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003904 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003905 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003906 rc = emulate_int(ctxt, 3);
3907 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003908 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003909 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003910 break;
3911 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09003912 if (ctxt->eflags & EFLG_OF)
3913 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003914 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003915 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003916 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003917 break;
3918 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003919 ctxt->src.val = ctxt->regs[VCPU_REGS_RCX];
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003920 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003921 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003922 case 0xe4: /* inb */
3923 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003924 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003925 case 0xe6: /* outb */
3926 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003927 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003928 case 0xe8: /* call (near) */ {
Avi Kivity9dac77f2011-06-01 15:34:25 +03003929 long int rel = ctxt->src.val;
3930 ctxt->src.val = (unsigned long) ctxt->_eip;
3931 jmp_rel(ctxt, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003932 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003933 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003934 }
3935 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003936 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003937 jmp_rel(ctxt, ctxt->src.val);
3938 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003939 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003940 case 0xec: /* in al,dx */
3941 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003942 do_io_in:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003943 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3944 &ctxt->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003945 goto done; /* IO is needed */
3946 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003947 case 0xee: /* out dx,al */
3948 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003949 do_io_out:
Avi Kivity9dac77f2011-06-01 15:34:25 +03003950 ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3951 &ctxt->src.val, 1);
3952 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003953 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003954 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03003955 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003956 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003957 case 0xf5: /* cmc */
3958 /* complement carry flag from eflags reg */
3959 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003960 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003961 case 0xf6 ... 0xf7: /* Grp3 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003962 rc = em_grp3(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003963 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003964 case 0xf8: /* clc */
3965 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003966 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003967 case 0xf9: /* stc */
3968 ctxt->eflags |= EFLG_CF;
3969 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003970 case 0xfc: /* cld */
3971 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003972 break;
3973 case 0xfd: /* std */
3974 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003975 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003976 case 0xfe: /* Grp4 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003977 rc = em_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003978 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003979 case 0xff: /* Grp5 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09003980 rc = em_grp45(ctxt);
3981 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003982 default:
3983 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003985
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003986 if (rc != X86EMUL_CONTINUE)
3987 goto done;
3988
Avi Kivity018a98d2007-11-27 19:30:56 +02003989writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09003990 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003991 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003992 goto done;
3993
Gleb Natapov5cd21912010-03-18 15:20:26 +02003994 /*
3995 * restore dst type in case the decoding will be reused
3996 * (happens for string instruction )
3997 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003998 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003999
Avi Kivity9dac77f2011-06-01 15:34:25 +03004000 if ((ctxt->d & SrcMask) == SrcSI)
4001 string_addr_inc(ctxt, seg_override(ctxt),
4002 VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004003
Avi Kivity9dac77f2011-06-01 15:34:25 +03004004 if ((ctxt->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02004005 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Avi Kivity9dac77f2011-06-01 15:34:25 +03004006 &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004007
Avi Kivity9dac77f2011-06-01 15:34:25 +03004008 if (ctxt->rep_prefix && (ctxt->d & String)) {
4009 struct read_cache *r = &ctxt->io_read;
4010 register_address_increment(ctxt, &ctxt->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004011
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004012 if (!string_insn_completed(ctxt)) {
4013 /*
4014 * Re-enter guest when pio read ahead buffer is empty
4015 * or, if it is not used, after each 1024 iteration.
4016 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004017 if ((r->end != 0 || ctxt->regs[VCPU_REGS_RCX] & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004018 (r->end == 0 || r->end != r->pos)) {
4019 /*
4020 * Reset read cache. Usually happens before
4021 * decode, but since instruction is restarted
4022 * we have to do it here.
4023 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004024 ctxt->mem_read.end = 0;
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004025 return EMULATION_RESTART;
4026 }
4027 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004028 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004029 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004030
Avi Kivity9dac77f2011-06-01 15:34:25 +03004031 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004032
4033done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004034 if (rc == X86EMUL_PROPAGATE_FAULT)
4035 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004036 if (rc == X86EMUL_INTERCEPTED)
4037 return EMULATION_INTERCEPTED;
4038
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004039 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040
4041twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004042 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004043 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004044 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004045 break;
4046 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004047 case 0x0d: /* GrpP (prefetch) */
4048 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004049 break;
4050 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004051 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004052 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004054 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004056 case 0x22: /* mov reg, cr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004057 if (ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004058 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004059 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004060 goto done;
4061 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004062 ctxt->dst.type = OP_NONE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004063 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 case 0x23: /* mov from reg to dr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004065 if (ops->set_dr(ctxt, ctxt->modrm_reg, ctxt->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004066 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004067 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004068 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004069 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004070 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004071 goto done;
4072 }
4073
Avi Kivity9dac77f2011-06-01 15:34:25 +03004074 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004076 case 0x30:
4077 /* wrmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004078 msr_data = (u32)ctxt->regs[VCPU_REGS_RAX]
4079 | ((u64)ctxt->regs[VCPU_REGS_RDX] << 32);
4080 if (ops->set_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004081 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004082 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004083 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004084 }
4085 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004086 break;
4087 case 0x32:
4088 /* rdmsr */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004089 if (ops->get_msr(ctxt, ctxt->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004090 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004091 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004092 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004093 } else {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004094 ctxt->regs[VCPU_REGS_RAX] = (u32)msr_data;
4095 ctxt->regs[VCPU_REGS_RDX] = msr_data >> 32;
Avi Kivity018a98d2007-11-27 19:30:56 +02004096 }
4097 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004098 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004099 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004100 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4101 if (!test_cc(ctxt->b, ctxt->eflags))
4102 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004104 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004105 if (test_cc(ctxt->b, ctxt->eflags))
4106 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004107 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004108 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004109 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004110 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004111 case 0xa0: /* push fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004112 rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004113 break;
4114 case 0xa1: /* pop fs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004115 rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004116 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004117 case 0xa3:
4118 bt: /* bt */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004119 ctxt->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004120 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004121 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004122 emulate_2op_SrcV_nobyte(ctxt, "bt");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004123 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004124 case 0xa4: /* shld imm8, r, r/m */
4125 case 0xa5: /* shld cl, r, r/m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004126 emulate_2op_cl("shld", ctxt->src2, ctxt->src, ctxt->dst, ctxt->eflags);
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004127 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004128 case 0xa8: /* push gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004129 rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004130 break;
4131 case 0xa9: /* pop gs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004132 rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004133 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004134 case 0xab:
4135 bts: /* bts */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004136 emulate_2op_SrcV_nobyte(ctxt, "bts");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004137 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004138 case 0xac: /* shrd imm8, r, r/m */
4139 case 0xad: /* shrd cl, r, r/m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004140 emulate_2op_cl("shrd", ctxt->src2, ctxt->src, ctxt->dst, ctxt->eflags);
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004141 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004142 case 0xae: /* clflush */
4143 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 case 0xb0 ... 0xb1: /* cmpxchg */
4145 /*
4146 * Save real source value, then compare EAX against
4147 * destination.
4148 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004149 ctxt->src.orig_val = ctxt->src.val;
4150 ctxt->src.val = ctxt->regs[VCPU_REGS_RAX];
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004151 emulate_2op_SrcV(ctxt, "cmp");
Laurent Vivier05f086f2007-09-24 11:10:55 +02004152 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153 /* Success: write back to memory. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004154 ctxt->dst.val = ctxt->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 } else {
4156 /* Failure: write the value we saw to EAX. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004157 ctxt->dst.type = OP_REG;
4158 ctxt->dst.addr.reg = (unsigned long *)&ctxt->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 }
4160 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004161 case 0xb2: /* lss */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004162 rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004163 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 case 0xb3:
4165 btr: /* btr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004166 emulate_2op_SrcV_nobyte(ctxt, "btr");
Avi Kivity6aa8b732006-12-10 02:21:36 -08004167 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004168 case 0xb4: /* lfs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004169 rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004170 break;
4171 case 0xb5: /* lgs */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004172 rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004175 ctxt->dst.bytes = ctxt->op_bytes;
4176 ctxt->dst.val = (ctxt->d & ByteOp) ? (u8) ctxt->src.val
4177 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179 case 0xba: /* Grp8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004180 switch (ctxt->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 case 0:
4182 goto bt;
4183 case 1:
4184 goto bts;
4185 case 2:
4186 goto btr;
4187 case 3:
4188 goto btc;
4189 }
4190 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004191 case 0xbb:
4192 btc: /* btc */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004193 emulate_2op_SrcV_nobyte(ctxt, "btc");
Nitin A Kamble7de75242007-09-15 10:13:07 +03004194 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004195 case 0xbc: { /* bsf */
4196 u8 zf;
4197 __asm__ ("bsf %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004198 : "=r"(ctxt->dst.val), "=q"(zf)
4199 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004200 ctxt->eflags &= ~X86_EFLAGS_ZF;
4201 if (zf) {
4202 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004203 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004204 }
4205 break;
4206 }
4207 case 0xbd: { /* bsr */
4208 u8 zf;
4209 __asm__ ("bsr %2, %0; setz %1"
Avi Kivity9dac77f2011-06-01 15:34:25 +03004210 : "=r"(ctxt->dst.val), "=q"(zf)
4211 : "r"(ctxt->src.val));
Wei Yongjund9574a22010-08-10 13:48:22 +08004212 ctxt->eflags &= ~X86_EFLAGS_ZF;
4213 if (zf) {
4214 ctxt->eflags |= X86_EFLAGS_ZF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004215 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Wei Yongjund9574a22010-08-10 13:48:22 +08004216 }
4217 break;
4218 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004220 ctxt->dst.bytes = ctxt->op_bytes;
4221 ctxt->dst.val = (ctxt->d & ByteOp) ? (s8) ctxt->src.val :
4222 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004223 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004224 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004225 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004226 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004227 ctxt->src.val = ctxt->dst.orig_val;
4228 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004229 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004230 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004231 ctxt->dst.bytes = ctxt->op_bytes;
4232 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4233 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004234 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235 case 0xc7: /* Grp9 (cmpxchg8b) */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004236 rc = em_grp9(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004237 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004238 default:
4239 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004241
4242 if (rc != X86EMUL_CONTINUE)
4243 goto done;
4244
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245 goto writeback;
4246
4247cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004248 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249}