blob: 33ad16b7db2b743b73f253aa9717fc7b14a3d469 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
32 * Opcode effective-address decode tables.
33 * Note that we only emulate instructions that have at least one memory
34 * operand (excluding implicit stack references). We assume that stack
35 * references and instruction fetches will never occur in special memory
36 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
37 * not be handled.
38 */
39
40/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b122010-07-29 15:11:49 +030041#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080042/* Destination operand type. */
Avi Kivityab85b122010-07-29 15:11:49 +030043#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
44#define DstReg (2<<1) /* Register operand. */
45#define DstMem (3<<1) /* Memory operand. */
46#define DstAcc (4<<1) /* Destination Accumulator */
47#define DstDI (5<<1) /* Destination is in ES:(E)DI */
48#define DstMem64 (6<<1) /* 64bit memory operand */
Wei Yongjun943858e2010-08-06 11:36:51 +080049#define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
Avi Kivityab85b122010-07-29 15:11:49 +030050#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080051/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020052#define SrcNone (0<<4) /* No source operand. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020053#define SrcReg (1<<4) /* Register operand. */
54#define SrcMem (2<<4) /* Memory operand. */
55#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
56#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
57#define SrcImm (5<<4) /* Immediate operand. */
58#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010059#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030060#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030061#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020062#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030063#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
64#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080065#define SrcAcc (0xd<<4) /* Source Accumulator */
Avi Kivityb250e602010-08-18 15:11:24 +030066#define SrcImmU16 (0xe<<4) /* Immediate operand, unsigned, 16 bits */
Gleb Natapov341de7e2009-04-12 13:36:41 +030067#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080068/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030069#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080070/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030071#define Mov (1<<9)
72#define BitOp (1<<10)
73#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020074#define String (1<<12) /* String instruction (rep capable) */
75#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020076#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
77#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Avi Kivity0d7cdee2011-03-29 11:34:38 +020078#define Prefix (1<<16) /* Instruction varies with 66/f2/f3 prefix */
Avi Kivity12537912011-03-29 11:41:27 +020079#define Sse (1<<17) /* SSE Vector instruction */
Joerg Roedel01de8b02011-04-04 12:39:31 +020080#define RMExt (1<<18) /* Opcode extension in ModRM r/m if mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030081/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +020082#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +020083#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +030084#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030085#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030086#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020087#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020088#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030089#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010090/* Source 2 operand type */
91#define Src2None (0<<29)
92#define Src2CL (1<<29)
93#define Src2ImmByte (2<<29)
94#define Src2One (3<<29)
Avi Kivity7db41eb2010-08-18 19:25:28 +030095#define Src2Imm (4<<29)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010096#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
Avi Kivityd0e53322010-07-29 15:11:54 +030098#define X2(x...) x, x
99#define X3(x...) X2(x), x
100#define X4(x...) X2(x), X2(x)
101#define X5(x...) X4(x), x
102#define X6(x...) X4(x), X2(x)
103#define X7(x...) X4(x), X3(x)
104#define X8(x...) X4(x), X4(x)
105#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300106
Avi Kivityd65b1de2010-07-29 15:11:35 +0300107struct opcode {
108 u32 flags;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200109 u8 intercept;
Avi Kivity120df892010-07-29 15:11:39 +0300110 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300111 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300112 struct opcode *group;
113 struct group_dual *gdual;
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200114 struct gprefix *gprefix;
Avi Kivity120df892010-07-29 15:11:39 +0300115 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200116 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300117};
118
119struct group_dual {
120 struct opcode mod012[8];
121 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300122};
123
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200124struct gprefix {
125 struct opcode pfx_no;
126 struct opcode pfx_66;
127 struct opcode pfx_f2;
128 struct opcode pfx_f3;
129};
130
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200132#define EFLG_ID (1<<21)
133#define EFLG_VIP (1<<20)
134#define EFLG_VIF (1<<19)
135#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200136#define EFLG_VM (1<<17)
137#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200138#define EFLG_IOPL (3<<12)
139#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140#define EFLG_OF (1<<11)
141#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200142#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200143#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144#define EFLG_SF (1<<7)
145#define EFLG_ZF (1<<6)
146#define EFLG_AF (1<<4)
147#define EFLG_PF (1<<2)
148#define EFLG_CF (1<<0)
149
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300150#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
151#define EFLG_RESERVED_ONE_MASK 2
152
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153/*
154 * Instruction emulation:
155 * Most instructions are emulated directly via a fragment of inline assembly
156 * code. This allows us to save/restore EFLAGS and thus very easily pick up
157 * any modified flags.
158 */
159
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800160#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161#define _LO32 "k" /* force 32-bit operand */
162#define _STK "%%rsp" /* stack pointer */
163#elif defined(__i386__)
164#define _LO32 "" /* force 32-bit operand */
165#define _STK "%%esp" /* stack pointer */
166#endif
167
168/*
169 * These EFLAGS bits are restored from saved value during emulation, and
170 * any changes are written back to the saved value after emulation.
171 */
172#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
173
174/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200175#define _PRE_EFLAGS(_sav, _msk, _tmp) \
176 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
177 "movl %"_sav",%"_LO32 _tmp"; " \
178 "push %"_tmp"; " \
179 "push %"_tmp"; " \
180 "movl %"_msk",%"_LO32 _tmp"; " \
181 "andl %"_LO32 _tmp",("_STK"); " \
182 "pushf; " \
183 "notl %"_LO32 _tmp"; " \
184 "andl %"_LO32 _tmp",("_STK"); " \
185 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
186 "pop %"_tmp"; " \
187 "orl %"_LO32 _tmp",("_STK"); " \
188 "popf; " \
189 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190
191/* After executing instruction: write-back necessary bits in EFLAGS. */
192#define _POST_EFLAGS(_sav, _msk, _tmp) \
193 /* _sav |= EFLAGS & _msk; */ \
194 "pushf; " \
195 "pop %"_tmp"; " \
196 "andl %"_msk",%"_LO32 _tmp"; " \
197 "orl %"_LO32 _tmp",%"_sav"; "
198
Avi Kivitydda96d82008-11-26 15:14:10 +0200199#ifdef CONFIG_X86_64
200#define ON64(x) x
201#else
202#define ON64(x)
203#endif
204
Avi Kivityb3b3d252010-08-16 17:49:52 +0300205#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200206 do { \
207 __asm__ __volatile__ ( \
208 _PRE_EFLAGS("0", "4", "2") \
209 _op _suffix " %"_x"3,%1; " \
210 _POST_EFLAGS("0", "4", "2") \
Avi Kivityfb2c2642010-08-16 17:50:56 +0300211 : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200212 "=&r" (_tmp) \
213 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200214 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200215
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217/* Raw emulation: instruction has two explicit operands. */
218#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200219 do { \
220 unsigned long _tmp; \
221 \
222 switch ((_dst).bytes) { \
223 case 2: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300224 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200225 break; \
226 case 4: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300227 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
Avi Kivity6b7ad612008-11-26 15:30:45 +0200228 break; \
229 case 8: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300230 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200231 break; \
232 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 } while (0)
234
235#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
236 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200237 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400238 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 case 1: \
Avi Kivityb3b3d252010-08-16 17:49:52 +0300240 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241 break; \
242 default: \
243 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
244 _wx, _wy, _lx, _ly, _qx, _qy); \
245 break; \
246 } \
247 } while (0)
248
249/* Source operand is byte-sized and may be restricted to just %cl. */
250#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
251 __emulate_2op(_op, _src, _dst, _eflags, \
252 "b", "c", "b", "c", "b", "c", "b", "c")
253
254/* Source operand is byte, word, long or quad sized. */
255#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
256 __emulate_2op(_op, _src, _dst, _eflags, \
257 "b", "q", "w", "r", _LO32, "r", "", "r")
258
259/* Source operand is word, long or quad sized. */
260#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
261 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
262 "w", "r", _LO32, "r", "", "r")
263
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100264/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity72952612011-04-20 13:12:27 +0300265#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
266 do { \
267 unsigned long _tmp; \
268 _type _clv = (_cl).val; \
269 _type _srcv = (_src).val; \
270 _type _dstv = (_dst).val; \
271 \
272 __asm__ __volatile__ ( \
273 _PRE_EFLAGS("0", "5", "2") \
274 _op _suffix " %4,%1 \n" \
275 _POST_EFLAGS("0", "5", "2") \
276 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
277 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
278 ); \
279 \
280 (_cl).val = (unsigned long) _clv; \
281 (_src).val = (unsigned long) _srcv; \
282 (_dst).val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100283 } while (0)
284
Avi Kivity72952612011-04-20 13:12:27 +0300285#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
286 do { \
287 switch ((_dst).bytes) { \
288 case 2: \
289 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
290 "w", unsigned short); \
291 break; \
292 case 4: \
293 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
294 "l", unsigned int); \
295 break; \
296 case 8: \
297 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
298 "q", unsigned long)); \
299 break; \
300 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100301 } while (0)
302
Avi Kivitydda96d82008-11-26 15:14:10 +0200303#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304 do { \
305 unsigned long _tmp; \
306 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200307 __asm__ __volatile__ ( \
308 _PRE_EFLAGS("0", "3", "2") \
309 _op _suffix " %1; " \
310 _POST_EFLAGS("0", "3", "2") \
311 : "=m" (_eflags), "+m" ((_dst).val), \
312 "=&r" (_tmp) \
313 : "i" (EFLAGS_MASK)); \
314 } while (0)
315
316/* Instruction has only one explicit operand (no source operand). */
317#define emulate_1op(_op, _dst, _eflags) \
318 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400319 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200320 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
321 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
322 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
323 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324 } \
325 } while (0)
326
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300327#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
328 do { \
329 unsigned long _tmp; \
330 \
331 __asm__ __volatile__ ( \
332 _PRE_EFLAGS("0", "4", "1") \
333 _op _suffix " %5; " \
334 _POST_EFLAGS("0", "4", "1") \
335 : "=m" (_eflags), "=&r" (_tmp), \
336 "+a" (_rax), "+d" (_rdx) \
337 : "i" (EFLAGS_MASK), "m" ((_src).val), \
338 "a" (_rax), "d" (_rdx)); \
339 } while (0)
340
Avi Kivityf6b35972010-08-26 11:59:00 +0300341#define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
342 do { \
343 unsigned long _tmp; \
344 \
345 __asm__ __volatile__ ( \
346 _PRE_EFLAGS("0", "5", "1") \
347 "1: \n\t" \
348 _op _suffix " %6; " \
349 "2: \n\t" \
350 _POST_EFLAGS("0", "5", "1") \
351 ".pushsection .fixup,\"ax\" \n\t" \
352 "3: movb $1, %4 \n\t" \
353 "jmp 2b \n\t" \
354 ".popsection \n\t" \
355 _ASM_EXTABLE(1b, 3b) \
356 : "=m" (_eflags), "=&r" (_tmp), \
357 "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
358 : "i" (EFLAGS_MASK), "m" ((_src).val), \
359 "a" (_rax), "d" (_rdx)); \
360 } while (0)
361
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300362/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivity72952612011-04-20 13:12:27 +0300363#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
364 do { \
365 switch((_src).bytes) { \
366 case 1: \
367 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
368 _eflags, "b"); \
369 break; \
370 case 2: \
371 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
372 _eflags, "w"); \
373 break; \
374 case 4: \
375 __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
376 _eflags, "l"); \
377 break; \
378 case 8: \
379 ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
380 _eflags, "q")); \
381 break; \
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300382 } \
383 } while (0)
384
Avi Kivityf6b35972010-08-26 11:59:00 +0300385#define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
386 do { \
387 switch((_src).bytes) { \
388 case 1: \
389 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
390 _eflags, "b", _ex); \
391 break; \
392 case 2: \
393 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
394 _eflags, "w", _ex); \
395 break; \
396 case 4: \
397 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
398 _eflags, "l", _ex); \
399 break; \
400 case 8: ON64( \
401 __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
402 _eflags, "q", _ex)); \
403 break; \
404 } \
405 } while (0)
406
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407/* Fetch next part of the instruction being emulated. */
408#define insn_fetch(_type, _size, _eip) \
409({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200410 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200411 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412 goto done; \
413 (_eip) += (_size); \
414 (_type)_x; \
415})
416
Avi Kivity72952612011-04-20 13:12:27 +0300417#define insn_fetch_arr(_arr, _size, _eip) \
Gleb Natapov414e6272010-04-28 19:15:26 +0300418({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
419 if (rc != X86EMUL_CONTINUE) \
420 goto done; \
421 (_eip) += (_size); \
422})
423
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200424static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
425 enum x86_intercept intercept,
426 enum x86_intercept_stage stage)
427{
428 struct x86_instruction_info info = {
429 .intercept = intercept,
430 .rep_prefix = ctxt->decode.rep_prefix,
431 .modrm_mod = ctxt->decode.modrm_mod,
432 .modrm_reg = ctxt->decode.modrm_reg,
433 .modrm_rm = ctxt->decode.modrm_rm,
434 .src_val = ctxt->decode.src.val64,
435 .src_bytes = ctxt->decode.src.bytes,
436 .dst_bytes = ctxt->decode.dst.bytes,
437 .ad_bytes = ctxt->decode.ad_bytes,
438 .next_rip = ctxt->eip,
439 };
440
441 return ctxt->ops->intercept(ctxt->vcpu, &info, stage);
442}
443
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800444static inline unsigned long ad_mask(struct decode_cache *c)
445{
446 return (1UL << (c->ad_bytes << 3)) - 1;
447}
448
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800450static inline unsigned long
451address_mask(struct decode_cache *c, unsigned long reg)
452{
453 if (c->ad_bytes == sizeof(unsigned long))
454 return reg;
455 else
456 return reg & ad_mask(c);
457}
458
459static inline unsigned long
Avi Kivity90de84f2010-11-17 15:28:21 +0200460register_address(struct decode_cache *c, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800461{
Avi Kivity90de84f2010-11-17 15:28:21 +0200462 return address_mask(c, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800463}
464
Harvey Harrison7a9572752008-02-19 07:40:41 -0800465static inline void
466register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
467{
468 if (c->ad_bytes == sizeof(unsigned long))
469 *reg += inc;
470 else
471 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
472}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473
Harvey Harrison7a9572752008-02-19 07:40:41 -0800474static inline void jmp_rel(struct decode_cache *c, int rel)
475{
476 register_address_increment(c, &c->eip, rel);
477}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300478
Avi Kivity56697682011-04-03 14:08:51 +0300479static u32 desc_limit_scaled(struct desc_struct *desc)
480{
481 u32 limit = get_desc_limit(desc);
482
483 return desc->g ? (limit << 12) | 0xfff : limit;
484}
485
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300486static void set_seg_override(struct decode_cache *c, int seg)
487{
488 c->has_seg_override = true;
489 c->seg_override = seg;
490}
491
Gleb Natapov79168fd2010-04-28 19:15:30 +0300492static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
493 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300494{
495 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
496 return 0;
497
Avi Kivity4bff1e862011-04-20 13:37:53 +0300498 return ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300499}
500
Avi Kivity90de84f2010-11-17 15:28:21 +0200501static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
502 struct x86_emulate_ops *ops,
503 struct decode_cache *c)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300504{
505 if (!c->has_seg_override)
506 return 0;
507
Avi Kivity90de84f2010-11-17 15:28:21 +0200508 return c->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300509}
510
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200511static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
512 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300513{
Avi Kivityda9cb572010-11-22 17:53:21 +0200514 ctxt->exception.vector = vec;
515 ctxt->exception.error_code = error;
516 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200517 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300518}
519
Joerg Roedel3b88e412011-04-04 12:39:29 +0200520static int emulate_db(struct x86_emulate_ctxt *ctxt)
521{
522 return emulate_exception(ctxt, DB_VECTOR, 0, false);
523}
524
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200525static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300526{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200527 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300528}
529
Avi Kivity618ff152011-04-03 12:32:09 +0300530static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
531{
532 return emulate_exception(ctxt, SS_VECTOR, err, true);
533}
534
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200535static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300536{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200537 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300538}
539
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200540static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300541{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200542 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300543}
544
Avi Kivity34d1f492010-08-26 11:59:01 +0300545static int emulate_de(struct x86_emulate_ctxt *ctxt)
546{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200547 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300548}
549
Avi Kivity12537912011-03-29 11:41:27 +0200550static int emulate_nm(struct x86_emulate_ctxt *ctxt)
551{
552 return emulate_exception(ctxt, NM_VECTOR, 0, false);
553}
554
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400555static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300556 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400557 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300558 ulong *linear)
559{
560 struct decode_cache *c = &ctxt->decode;
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;
565 unsigned cpl, rpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300566
567 la = seg_base(ctxt, ctxt->ops, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300568 switch (ctxt->mode) {
569 case X86EMUL_MODE_REAL:
570 break;
571 case X86EMUL_MODE_PROT64:
572 if (((signed long)la << 16) >> 16 != la)
573 return emulate_gp(ctxt, 0);
574 break;
575 default:
Avi Kivity4bff1e862011-04-20 13:37:53 +0300576 usable = ctxt->ops->get_cached_descriptor(ctxt, &desc, NULL,
577 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300578 if (!usable)
579 goto bad;
580 /* code segment or read-only data segment */
581 if (((desc.type & 8) || !(desc.type & 2)) && write)
582 goto bad;
583 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400584 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300585 goto bad;
586 lim = desc_limit_scaled(&desc);
587 if ((desc.type & 8) || !(desc.type & 4)) {
588 /* expand-up segment */
589 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
590 goto bad;
591 } else {
592 /* exapand-down segment */
593 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
594 goto bad;
595 lim = desc.d ? 0xffffffff : 0xffff;
596 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
597 goto bad;
598 }
Avi Kivity717746e2011-04-20 13:37:53 +0300599 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity4bff1e862011-04-20 13:37:53 +0300600 rpl = ctxt->ops->get_segment_selector(ctxt, addr.seg) & 3;
Avi Kivity618ff152011-04-03 12:32:09 +0300601 cpl = max(cpl, rpl);
602 if (!(desc.type & 8)) {
603 /* data segment */
604 if (cpl > desc.dpl)
605 goto bad;
606 } else if ((desc.type & 8) && !(desc.type & 4)) {
607 /* nonconforming code segment */
608 if (cpl != desc.dpl)
609 goto bad;
610 } else if ((desc.type & 8) && (desc.type & 4)) {
611 /* conforming code segment */
612 if (cpl < desc.dpl)
613 goto bad;
614 }
615 break;
616 }
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400617 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : c->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300618 la &= (u32)-1;
619 *linear = la;
620 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300621bad:
622 if (addr.seg == VCPU_SREG_SS)
623 return emulate_ss(ctxt, addr.seg);
624 else
625 return emulate_gp(ctxt, addr.seg);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300626}
627
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400628static int linearize(struct x86_emulate_ctxt *ctxt,
629 struct segmented_address addr,
630 unsigned size, bool write,
631 ulong *linear)
632{
633 return __linearize(ctxt, addr, size, write, false, linear);
634}
635
636
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200637static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
638 struct segmented_address addr,
639 void *data,
640 unsigned size)
641{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200642 int rc;
643 ulong linear;
644
Avi Kivity83b87952011-04-03 11:31:19 +0300645 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200646 if (rc != X86EMUL_CONTINUE)
647 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300648 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200649}
650
Avi Kivity62266862007-11-20 13:15:52 +0200651static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
652 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300653 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200654{
655 struct fetch_cache *fc = &ctxt->decode.fetch;
656 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300657 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200658
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300659 if (eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400660 unsigned long linear;
661 struct segmented_address addr = { .seg=VCPU_SREG_CS, .ea=eip};
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300662 cur_size = fc->end - fc->start;
663 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400664 rc = __linearize(ctxt, addr, size, false, true, &linear);
665 if (rc != X86EMUL_CONTINUE)
666 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300667 rc = ops->fetch(ctxt, linear, fc->data + cur_size,
668 size, &ctxt->exception);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900669 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200670 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300671 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200672 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300673 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900674 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200675}
676
677static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
678 struct x86_emulate_ops *ops,
679 unsigned long eip, void *dest, unsigned size)
680{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900681 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200682
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200683 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200684 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200685 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200686 while (size--) {
687 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900688 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200689 return rc;
690 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900691 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200692}
693
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000694/*
695 * Given the 'reg' portion of a ModRM byte, and a register block, return a
696 * pointer into the block that addresses the relevant register.
697 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
698 */
699static void *decode_register(u8 modrm_reg, unsigned long *regs,
700 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701{
702 void *p;
703
704 p = &regs[modrm_reg];
705 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
706 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
707 return p;
708}
709
710static int read_descriptor(struct x86_emulate_ctxt *ctxt,
711 struct x86_emulate_ops *ops,
Avi Kivity90de84f2010-11-17 15:28:21 +0200712 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 u16 *size, unsigned long *address, int op_bytes)
714{
715 int rc;
716
717 if (op_bytes == 2)
718 op_bytes = 3;
719 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200720 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900721 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200723 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200724 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725 return rc;
726}
727
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300728static int test_cc(unsigned int condition, unsigned int flags)
729{
730 int rc = 0;
731
732 switch ((condition & 15) >> 1) {
733 case 0: /* o */
734 rc |= (flags & EFLG_OF);
735 break;
736 case 1: /* b/c/nae */
737 rc |= (flags & EFLG_CF);
738 break;
739 case 2: /* z/e */
740 rc |= (flags & EFLG_ZF);
741 break;
742 case 3: /* be/na */
743 rc |= (flags & (EFLG_CF|EFLG_ZF));
744 break;
745 case 4: /* s */
746 rc |= (flags & EFLG_SF);
747 break;
748 case 5: /* p/pe */
749 rc |= (flags & EFLG_PF);
750 break;
751 case 7: /* le/ng */
752 rc |= (flags & EFLG_ZF);
753 /* fall through */
754 case 6: /* l/nge */
755 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
756 break;
757 }
758
759 /* Odd condition identifiers (lsb == 1) have inverted sense. */
760 return (!!rc ^ (condition & 1));
761}
762
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300763static void fetch_register_operand(struct operand *op)
764{
765 switch (op->bytes) {
766 case 1:
767 op->val = *(u8 *)op->addr.reg;
768 break;
769 case 2:
770 op->val = *(u16 *)op->addr.reg;
771 break;
772 case 4:
773 op->val = *(u32 *)op->addr.reg;
774 break;
775 case 8:
776 op->val = *(u64 *)op->addr.reg;
777 break;
778 }
779}
780
Avi Kivity12537912011-03-29 11:41:27 +0200781static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
782{
783 ctxt->ops->get_fpu(ctxt);
784 switch (reg) {
785 case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
786 case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
787 case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
788 case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
789 case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
790 case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
791 case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
792 case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
793#ifdef CONFIG_X86_64
794 case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
795 case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
796 case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
797 case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
798 case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
799 case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
800 case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
801 case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
802#endif
803 default: BUG();
804 }
805 ctxt->ops->put_fpu(ctxt);
806}
807
808static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
809 int reg)
810{
811 ctxt->ops->get_fpu(ctxt);
812 switch (reg) {
813 case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
814 case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
815 case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
816 case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
817 case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
818 case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
819 case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
820 case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
821#ifdef CONFIG_X86_64
822 case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
823 case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
824 case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
825 case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
826 case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
827 case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
828 case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
829 case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
830#endif
831 default: BUG();
832 }
833 ctxt->ops->put_fpu(ctxt);
834}
835
836static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
837 struct operand *op,
Avi Kivity3c118e22007-10-31 10:27:04 +0200838 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200839 int inhibit_bytereg)
840{
Avi Kivity33615aa2007-10-31 11:15:56 +0200841 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200842 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200843
844 if (!(c->d & ModRM))
845 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +0200846
847 if (c->d & Sse) {
848 op->type = OP_XMM;
849 op->bytes = 16;
850 op->addr.xmm = reg;
851 read_sse_reg(ctxt, &op->vec_val, reg);
852 return;
853 }
854
Avi Kivity3c118e22007-10-31 10:27:04 +0200855 op->type = OP_REG;
856 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300857 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200858 op->bytes = 1;
859 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300860 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200861 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200862 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300863 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200864 op->orig_val = op->val;
865}
866
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200867static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300868 struct x86_emulate_ops *ops,
869 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200870{
871 struct decode_cache *c = &ctxt->decode;
872 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700873 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900874 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300875 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200876
877 if (c->rex_prefix) {
878 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
879 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
880 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
881 }
882
883 c->modrm = insn_fetch(u8, 1, c->eip);
884 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
885 c->modrm_reg |= (c->modrm & 0x38) >> 3;
886 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300887 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200888
889 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300890 op->type = OP_REG;
891 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
892 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300893 c->regs, c->d & ByteOp);
Avi Kivity12537912011-03-29 11:41:27 +0200894 if (c->d & Sse) {
895 op->type = OP_XMM;
896 op->bytes = 16;
897 op->addr.xmm = c->modrm_rm;
898 read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
899 return rc;
900 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300901 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200902 return rc;
903 }
904
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300905 op->type = OP_MEM;
906
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200907 if (c->ad_bytes == 2) {
908 unsigned bx = c->regs[VCPU_REGS_RBX];
909 unsigned bp = c->regs[VCPU_REGS_RBP];
910 unsigned si = c->regs[VCPU_REGS_RSI];
911 unsigned di = c->regs[VCPU_REGS_RDI];
912
913 /* 16-bit ModR/M decode. */
914 switch (c->modrm_mod) {
915 case 0:
916 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300917 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200918 break;
919 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300920 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200921 break;
922 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300923 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200924 break;
925 }
926 switch (c->modrm_rm) {
927 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300928 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200929 break;
930 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300931 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200932 break;
933 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300934 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200935 break;
936 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300937 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200938 break;
939 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300940 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200941 break;
942 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300943 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200944 break;
945 case 6:
946 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300947 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200948 break;
949 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300950 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200951 break;
952 }
953 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
954 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300955 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300956 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200957 } else {
958 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700959 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200960 sib = insn_fetch(u8, 1, c->eip);
961 index_reg |= (sib >> 3) & 7;
962 base_reg |= sib & 7;
963 scale = sib >> 6;
964
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700965 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300966 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700967 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300968 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700969 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300970 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700971 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
972 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700973 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700974 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300975 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200976 switch (c->modrm_mod) {
977 case 0:
978 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300979 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200980 break;
981 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300982 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200983 break;
984 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300985 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200986 break;
987 }
988 }
Avi Kivity90de84f2010-11-17 15:28:21 +0200989 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200990done:
991 return rc;
992}
993
994static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300995 struct x86_emulate_ops *ops,
996 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200997{
998 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900999 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001000
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001001 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001002 switch (c->ad_bytes) {
1003 case 2:
Avi Kivity90de84f2010-11-17 15:28:21 +02001004 op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001005 break;
1006 case 4:
Avi Kivity90de84f2010-11-17 15:28:21 +02001007 op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001008 break;
1009 case 8:
Avi Kivity90de84f2010-11-17 15:28:21 +02001010 op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001011 break;
1012 }
1013done:
1014 return rc;
1015}
1016
Wei Yongjun35c843c2010-08-09 11:34:56 +08001017static void fetch_bit_operand(struct decode_cache *c)
1018{
Sheng Yang7129eec2010-09-28 16:33:32 +08001019 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001020
Wei Yongjun3885f182010-08-09 11:37:37 +08001021 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +08001022 mask = ~(c->dst.bytes * 8 - 1);
1023
1024 if (c->src.bytes == 2)
1025 sv = (s16)c->src.val & (s16)mask;
1026 else if (c->src.bytes == 4)
1027 sv = (s32)c->src.val & (s32)mask;
1028
Avi Kivity90de84f2010-11-17 15:28:21 +02001029 c->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001030 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001031
1032 /* only subword offset */
1033 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001034}
1035
Gleb Natapov9de41572010-04-28 19:15:22 +03001036static int read_emulated(struct x86_emulate_ctxt *ctxt,
1037 struct x86_emulate_ops *ops,
1038 unsigned long addr, void *dest, unsigned size)
1039{
1040 int rc;
1041 struct read_cache *mc = &ctxt->decode.mem_read;
1042
1043 while (size) {
1044 int n = min(size, 8u);
1045 size -= n;
1046 if (mc->pos < mc->end)
1047 goto read_cached;
1048
Avi Kivity0f65dd72011-04-20 13:37:53 +03001049 rc = ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
1050 &ctxt->exception);
Gleb Natapov9de41572010-04-28 19:15:22 +03001051 if (rc != X86EMUL_CONTINUE)
1052 return rc;
1053 mc->end += n;
1054
1055 read_cached:
1056 memcpy(dest, mc->data + mc->pos, n);
1057 mc->pos += n;
1058 dest += n;
1059 addr += n;
1060 }
1061 return X86EMUL_CONTINUE;
1062}
1063
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001064static int segmented_read(struct x86_emulate_ctxt *ctxt,
1065 struct segmented_address addr,
1066 void *data,
1067 unsigned size)
1068{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001069 int rc;
1070 ulong linear;
1071
Avi Kivity83b87952011-04-03 11:31:19 +03001072 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001073 if (rc != X86EMUL_CONTINUE)
1074 return rc;
1075 return read_emulated(ctxt, ctxt->ops, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001076}
1077
1078static int segmented_write(struct x86_emulate_ctxt *ctxt,
1079 struct segmented_address addr,
1080 const void *data,
1081 unsigned size)
1082{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001083 int rc;
1084 ulong linear;
1085
Avi Kivity83b87952011-04-03 11:31:19 +03001086 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001087 if (rc != X86EMUL_CONTINUE)
1088 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001089 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1090 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001091}
1092
1093static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1094 struct segmented_address addr,
1095 const void *orig_data, const void *data,
1096 unsigned size)
1097{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001098 int rc;
1099 ulong linear;
1100
Avi Kivity83b87952011-04-03 11:31:19 +03001101 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001102 if (rc != X86EMUL_CONTINUE)
1103 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001104 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1105 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001106}
1107
Gleb Natapov7b262e92010-03-18 15:20:27 +02001108static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
1109 struct x86_emulate_ops *ops,
1110 unsigned int size, unsigned short port,
1111 void *dest)
1112{
1113 struct read_cache *rc = &ctxt->decode.io_read;
1114
1115 if (rc->pos == rc->end) { /* refill pio read ahead */
1116 struct decode_cache *c = &ctxt->decode;
1117 unsigned int in_page, n;
1118 unsigned int count = c->rep_prefix ?
1119 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
1120 in_page = (ctxt->eflags & EFLG_DF) ?
1121 offset_in_page(c->regs[VCPU_REGS_RDI]) :
1122 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
1123 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1124 count);
1125 if (n == 0)
1126 n = 1;
1127 rc->pos = rc->end = 0;
Avi Kivityca1d4a92011-04-20 13:37:53 +03001128 if (!ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001129 return 0;
1130 rc->end = n * size;
1131 }
1132
1133 memcpy(dest, rc->data + rc->pos, size);
1134 rc->pos += size;
1135 return 1;
1136}
1137
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001138static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
1139 struct x86_emulate_ops *ops,
1140 u16 selector, struct desc_ptr *dt)
1141{
1142 if (selector & 1 << 2) {
1143 struct desc_struct desc;
1144 memset (dt, 0, sizeof *dt);
Avi Kivity4bff1e862011-04-20 13:37:53 +03001145 if (!ops->get_cached_descriptor(ctxt, &desc, NULL,
1146 VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001147 return;
1148
1149 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1150 dt->address = get_desc_base(&desc);
1151 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001152 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001153}
1154
1155/* allowed just for 8 bytes segments */
1156static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1157 struct x86_emulate_ops *ops,
1158 u16 selector, struct desc_struct *desc)
1159{
1160 struct desc_ptr dt;
1161 u16 index = selector >> 3;
1162 int ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001163 ulong addr;
1164
1165 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1166
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001167 if (dt.size < index * 8 + 7)
1168 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001169 addr = dt.address + index * 8;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001170 ret = ops->read_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001171
1172 return ret;
1173}
1174
1175/* allowed just for 8 bytes segments */
1176static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1177 struct x86_emulate_ops *ops,
1178 u16 selector, struct desc_struct *desc)
1179{
1180 struct desc_ptr dt;
1181 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001182 ulong addr;
1183 int ret;
1184
1185 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
1186
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001187 if (dt.size < index * 8 + 7)
1188 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001189
1190 addr = dt.address + index * 8;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001191 ret = ops->write_std(ctxt, addr, desc, sizeof *desc, &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001192
1193 return ret;
1194}
1195
Gleb Natapov5601d052011-03-07 14:55:06 +02001196/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001197static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
1198 struct x86_emulate_ops *ops,
1199 u16 selector, int seg)
1200{
1201 struct desc_struct seg_desc;
1202 u8 dpl, rpl, cpl;
1203 unsigned err_vec = GP_VECTOR;
1204 u32 err_code = 0;
1205 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
1206 int ret;
1207
1208 memset(&seg_desc, 0, sizeof seg_desc);
1209
1210 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1211 || ctxt->mode == X86EMUL_MODE_REAL) {
1212 /* set real mode segment descriptor */
1213 set_desc_base(&seg_desc, selector << 4);
1214 set_desc_limit(&seg_desc, 0xffff);
1215 seg_desc.type = 3;
1216 seg_desc.p = 1;
1217 seg_desc.s = 1;
1218 goto load;
1219 }
1220
1221 /* NULL selector is not valid for TR, CS and SS */
1222 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
1223 && null_selector)
1224 goto exception;
1225
1226 /* TR should be in GDT only */
1227 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1228 goto exception;
1229
1230 if (null_selector) /* for NULL selector skip all following checks */
1231 goto load;
1232
1233 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
1234 if (ret != X86EMUL_CONTINUE)
1235 return ret;
1236
1237 err_code = selector & 0xfffc;
1238 err_vec = GP_VECTOR;
1239
1240 /* can't load system descriptor into segment selecor */
1241 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1242 goto exception;
1243
1244 if (!seg_desc.p) {
1245 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1246 goto exception;
1247 }
1248
1249 rpl = selector & 3;
1250 dpl = seg_desc.dpl;
Avi Kivity717746e2011-04-20 13:37:53 +03001251 cpl = ops->cpl(ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001252
1253 switch (seg) {
1254 case VCPU_SREG_SS:
1255 /*
1256 * segment is not a writable data segment or segment
1257 * selector's RPL != CPL or segment selector's RPL != CPL
1258 */
1259 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1260 goto exception;
1261 break;
1262 case VCPU_SREG_CS:
1263 if (!(seg_desc.type & 8))
1264 goto exception;
1265
1266 if (seg_desc.type & 4) {
1267 /* conforming */
1268 if (dpl > cpl)
1269 goto exception;
1270 } else {
1271 /* nonconforming */
1272 if (rpl > cpl || dpl != cpl)
1273 goto exception;
1274 }
1275 /* CS(RPL) <- CPL */
1276 selector = (selector & 0xfffc) | cpl;
1277 break;
1278 case VCPU_SREG_TR:
1279 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1280 goto exception;
1281 break;
1282 case VCPU_SREG_LDTR:
1283 if (seg_desc.s || seg_desc.type != 2)
1284 goto exception;
1285 break;
1286 default: /* DS, ES, FS, or GS */
1287 /*
1288 * segment is not a data or readable code segment or
1289 * ((segment is a data or nonconforming code segment)
1290 * and (both RPL and CPL > DPL))
1291 */
1292 if ((seg_desc.type & 0xa) == 0x8 ||
1293 (((seg_desc.type & 0xc) != 0xc) &&
1294 (rpl > dpl && cpl > dpl)))
1295 goto exception;
1296 break;
1297 }
1298
1299 if (seg_desc.s) {
1300 /* mark segment as accessed */
1301 seg_desc.type |= 1;
1302 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1303 if (ret != X86EMUL_CONTINUE)
1304 return ret;
1305 }
1306load:
Avi Kivity4bff1e862011-04-20 13:37:53 +03001307 ops->set_segment_selector(ctxt, selector, seg);
1308 ops->set_cached_descriptor(ctxt, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001309 return X86EMUL_CONTINUE;
1310exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001311 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001312 return X86EMUL_PROPAGATE_FAULT;
1313}
1314
Wei Yongjun31be40b2010-08-17 09:17:30 +08001315static void write_register_operand(struct operand *op)
1316{
1317 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1318 switch (op->bytes) {
1319 case 1:
1320 *(u8 *)op->addr.reg = (u8)op->val;
1321 break;
1322 case 2:
1323 *(u16 *)op->addr.reg = (u16)op->val;
1324 break;
1325 case 4:
1326 *op->addr.reg = (u32)op->val;
1327 break; /* 64b: zero-extend */
1328 case 8:
1329 *op->addr.reg = op->val;
1330 break;
1331 }
1332}
1333
Wei Yongjunc37eda12010-06-15 09:03:33 +08001334static inline int writeback(struct x86_emulate_ctxt *ctxt,
1335 struct x86_emulate_ops *ops)
1336{
1337 int rc;
1338 struct decode_cache *c = &ctxt->decode;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001339
1340 switch (c->dst.type) {
1341 case OP_REG:
Wei Yongjun31be40b2010-08-17 09:17:30 +08001342 write_register_operand(&c->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001343 break;
1344 case OP_MEM:
1345 if (c->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001346 rc = segmented_cmpxchg(ctxt,
1347 c->dst.addr.mem,
1348 &c->dst.orig_val,
1349 &c->dst.val,
1350 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001351 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001352 rc = segmented_write(ctxt,
1353 c->dst.addr.mem,
1354 &c->dst.val,
1355 c->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001356 if (rc != X86EMUL_CONTINUE)
1357 return rc;
1358 break;
Avi Kivity12537912011-03-29 11:41:27 +02001359 case OP_XMM:
1360 write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
1361 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001362 case OP_NONE:
1363 /* no writeback */
1364 break;
1365 default:
1366 break;
1367 }
1368 return X86EMUL_CONTINUE;
1369}
1370
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001371static int em_push(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001372{
1373 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001374 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001375
Harvey Harrison7a9572752008-02-19 07:40:41 -08001376 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001377 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1378 addr.seg = VCPU_SREG_SS;
1379
1380 /* Disable writeback. */
1381 c->dst.type = OP_NONE;
1382 return segmented_write(ctxt, addr, &c->src.val, c->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001383}
1384
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001385static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001386 struct x86_emulate_ops *ops,
1387 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001388{
1389 struct decode_cache *c = &ctxt->decode;
1390 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001391 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392
Avi Kivity90de84f2010-11-17 15:28:21 +02001393 addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
1394 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001395 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001396 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001397 return rc;
1398
Avi Kivity350f69d2009-01-05 11:12:40 +02001399 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001400 return rc;
1401}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001402
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001403static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1404 struct x86_emulate_ops *ops,
1405 void *dest, int len)
1406{
1407 int rc;
1408 unsigned long val, change_mask;
1409 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity717746e2011-04-20 13:37:53 +03001410 int cpl = ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001411
1412 rc = emulate_pop(ctxt, ops, &val, len);
1413 if (rc != X86EMUL_CONTINUE)
1414 return rc;
1415
1416 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1417 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1418
1419 switch(ctxt->mode) {
1420 case X86EMUL_MODE_PROT64:
1421 case X86EMUL_MODE_PROT32:
1422 case X86EMUL_MODE_PROT16:
1423 if (cpl == 0)
1424 change_mask |= EFLG_IOPL;
1425 if (cpl <= iopl)
1426 change_mask |= EFLG_IF;
1427 break;
1428 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001429 if (iopl < 3)
1430 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001431 change_mask |= EFLG_IF;
1432 break;
1433 default: /* real mode */
1434 change_mask |= (EFLG_IOPL | EFLG_IF);
1435 break;
1436 }
1437
1438 *(unsigned long *)dest =
1439 (ctxt->eflags & ~change_mask) | (val & change_mask);
1440
1441 return rc;
1442}
1443
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001444static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1445 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001446{
1447 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001448
Avi Kivity4bff1e862011-04-20 13:37:53 +03001449 c->src.val = ops->get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001450
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001451 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001452}
1453
1454static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1455 struct x86_emulate_ops *ops, int seg)
1456{
1457 struct decode_cache *c = &ctxt->decode;
1458 unsigned long selector;
1459 int rc;
1460
1461 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001462 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001463 return rc;
1464
Gleb Natapov2e873022010-03-18 15:20:18 +02001465 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001466 return rc;
1467}
1468
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001469static int emulate_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001470{
1471 struct decode_cache *c = &ctxt->decode;
1472 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001473 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001474 int reg = VCPU_REGS_RAX;
1475
1476 while (reg <= VCPU_REGS_RDI) {
1477 (reg == VCPU_REGS_RSP) ?
1478 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1479
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001480 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001481 if (rc != X86EMUL_CONTINUE)
1482 return rc;
1483
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001484 ++reg;
1485 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001486
Wei Yongjunc37eda12010-06-15 09:03:33 +08001487 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001488}
1489
1490static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1491 struct x86_emulate_ops *ops)
1492{
1493 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001494 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001495 int reg = VCPU_REGS_RDI;
1496
1497 while (reg >= VCPU_REGS_RAX) {
1498 if (reg == VCPU_REGS_RSP) {
1499 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1500 c->op_bytes);
1501 --reg;
1502 }
1503
1504 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001505 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001506 break;
1507 --reg;
1508 }
1509 return rc;
1510}
1511
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001512int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1513 struct x86_emulate_ops *ops, int irq)
1514{
1515 struct decode_cache *c = &ctxt->decode;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001516 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001517 struct desc_ptr dt;
1518 gva_t cs_addr;
1519 gva_t eip_addr;
1520 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001521
1522 /* TODO: Add limit checks */
1523 c->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001524 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001525 if (rc != X86EMUL_CONTINUE)
1526 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001527
1528 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1529
Avi Kivity4bff1e862011-04-20 13:37:53 +03001530 c->src.val = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001531 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001532 if (rc != X86EMUL_CONTINUE)
1533 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001534
1535 c->src.val = c->eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001536 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001537 if (rc != X86EMUL_CONTINUE)
1538 return rc;
1539
Avi Kivity4bff1e862011-04-20 13:37:53 +03001540 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001541
1542 eip_addr = dt.address + (irq << 2);
1543 cs_addr = dt.address + (irq << 2) + 2;
1544
Avi Kivity0f65dd72011-04-20 13:37:53 +03001545 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001546 if (rc != X86EMUL_CONTINUE)
1547 return rc;
1548
Avi Kivity0f65dd72011-04-20 13:37:53 +03001549 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001550 if (rc != X86EMUL_CONTINUE)
1551 return rc;
1552
1553 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1554 if (rc != X86EMUL_CONTINUE)
1555 return rc;
1556
1557 c->eip = eip;
1558
1559 return rc;
1560}
1561
1562static int emulate_int(struct x86_emulate_ctxt *ctxt,
1563 struct x86_emulate_ops *ops, int irq)
1564{
1565 switch(ctxt->mode) {
1566 case X86EMUL_MODE_REAL:
1567 return emulate_int_real(ctxt, ops, irq);
1568 case X86EMUL_MODE_VM86:
1569 case X86EMUL_MODE_PROT16:
1570 case X86EMUL_MODE_PROT32:
1571 case X86EMUL_MODE_PROT64:
1572 default:
1573 /* Protected mode interrupts unimplemented yet */
1574 return X86EMUL_UNHANDLEABLE;
1575 }
1576}
1577
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001578static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1579 struct x86_emulate_ops *ops)
1580{
1581 struct decode_cache *c = &ctxt->decode;
1582 int rc = X86EMUL_CONTINUE;
1583 unsigned long temp_eip = 0;
1584 unsigned long temp_eflags = 0;
1585 unsigned long cs = 0;
1586 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1587 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1588 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1589 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1590
1591 /* TODO: Add stack limit check */
1592
1593 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1594
1595 if (rc != X86EMUL_CONTINUE)
1596 return rc;
1597
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001598 if (temp_eip & ~0xffff)
1599 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001600
1601 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1602
1603 if (rc != X86EMUL_CONTINUE)
1604 return rc;
1605
1606 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1607
1608 if (rc != X86EMUL_CONTINUE)
1609 return rc;
1610
1611 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1612
1613 if (rc != X86EMUL_CONTINUE)
1614 return rc;
1615
1616 c->eip = temp_eip;
1617
1618
1619 if (c->op_bytes == 4)
1620 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1621 else if (c->op_bytes == 2) {
1622 ctxt->eflags &= ~0xffff;
1623 ctxt->eflags |= temp_eflags;
1624 }
1625
1626 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1627 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1628
1629 return rc;
1630}
1631
1632static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1633 struct x86_emulate_ops* ops)
1634{
1635 switch(ctxt->mode) {
1636 case X86EMUL_MODE_REAL:
1637 return emulate_iret_real(ctxt, ops);
1638 case X86EMUL_MODE_VM86:
1639 case X86EMUL_MODE_PROT16:
1640 case X86EMUL_MODE_PROT32:
1641 case X86EMUL_MODE_PROT64:
1642 default:
1643 /* iret from protected mode unimplemented yet */
1644 return X86EMUL_UNHANDLEABLE;
1645 }
1646}
1647
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001648static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1649 struct x86_emulate_ops *ops)
1650{
1651 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001652
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001653 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001654}
1655
Laurent Vivier05f086f2007-09-24 11:10:55 +02001656static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001657{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001658 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001659 switch (c->modrm_reg) {
1660 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001661 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001662 break;
1663 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001664 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001665 break;
1666 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001667 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001668 break;
1669 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001670 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001671 break;
1672 case 4: /* sal/shl */
1673 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001674 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001675 break;
1676 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001677 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001678 break;
1679 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001680 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681 break;
1682 }
1683}
1684
1685static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001686 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001687{
1688 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001689 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1690 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Avi Kivity34d1f492010-08-26 11:59:01 +03001691 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001692
1693 switch (c->modrm_reg) {
1694 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001695 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001696 break;
1697 case 2: /* not */
1698 c->dst.val = ~c->dst.val;
1699 break;
1700 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001701 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001702 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001703 case 4: /* mul */
1704 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1705 break;
1706 case 5: /* imul */
1707 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1708 break;
1709 case 6: /* div */
Avi Kivity34d1f492010-08-26 11:59:01 +03001710 emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
1711 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001712 break;
1713 case 7: /* idiv */
Avi Kivity34d1f492010-08-26 11:59:01 +03001714 emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
1715 ctxt->eflags, de);
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001716 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001718 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001719 }
Avi Kivity34d1f492010-08-26 11:59:01 +03001720 if (de)
1721 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001722 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001723}
1724
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001725static int emulate_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726{
1727 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001728 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001729
1730 switch (c->modrm_reg) {
1731 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001732 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001733 break;
1734 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001735 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001736 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001737 case 2: /* call near abs */ {
1738 long int old_eip;
1739 old_eip = c->eip;
1740 c->eip = c->src.val;
1741 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001742 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001743 break;
1744 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001745 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001746 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747 break;
1748 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001749 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001750 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001751 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001752 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001753}
1754
1755static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001756 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001757{
1758 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001759 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001760
1761 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1762 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001763 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1764 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001765 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001766 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001767 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1768 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001769
Laurent Vivier05f086f2007-09-24 11:10:55 +02001770 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001771 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001772 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001773}
1774
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001775static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1776 struct x86_emulate_ops *ops)
1777{
1778 struct decode_cache *c = &ctxt->decode;
1779 int rc;
1780 unsigned long cs;
1781
1782 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001783 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001784 return rc;
1785 if (c->op_bytes == 4)
1786 c->eip = (u32)c->eip;
1787 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001788 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001789 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001790 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001791 return rc;
1792}
1793
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08001794static int emulate_load_segment(struct x86_emulate_ctxt *ctxt,
1795 struct x86_emulate_ops *ops, int seg)
1796{
1797 struct decode_cache *c = &ctxt->decode;
1798 unsigned short sel;
1799 int rc;
1800
1801 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
1802
1803 rc = load_segment_descriptor(ctxt, ops, sel, seg);
1804 if (rc != X86EMUL_CONTINUE)
1805 return rc;
1806
1807 c->dst.val = c->src.val;
1808 return rc;
1809}
1810
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001811static inline void
1812setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001813 struct x86_emulate_ops *ops, struct desc_struct *cs,
1814 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001815{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001816 memset(cs, 0, sizeof(struct desc_struct));
Avi Kivity4bff1e862011-04-20 13:37:53 +03001817 ops->get_cached_descriptor(ctxt, cs, NULL, VCPU_SREG_CS);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001818 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001819
1820 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001821 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001822 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001823 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001824 cs->type = 0x0b; /* Read, Execute, Accessed */
1825 cs->s = 1;
1826 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001827 cs->p = 1;
1828 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001829
Gleb Natapov79168fd2010-04-28 19:15:30 +03001830 set_desc_base(ss, 0); /* flat segment */
1831 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001832 ss->g = 1; /* 4kb granularity */
1833 ss->s = 1;
1834 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001835 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001836 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001837 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001838}
1839
1840static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001841emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001842{
1843 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001844 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001845 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001846 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001847
1848 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001849 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001850 ctxt->mode == X86EMUL_MODE_VM86)
1851 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001852
Gleb Natapov79168fd2010-04-28 19:15:30 +03001853 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001854
Avi Kivity717746e2011-04-20 13:37:53 +03001855 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001856 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001857 cs_sel = (u16)(msr_data & 0xfffc);
1858 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001859
1860 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001861 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001862 cs.l = 1;
1863 }
Avi Kivity4bff1e862011-04-20 13:37:53 +03001864 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1865 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1866 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1867 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001868
1869 c->regs[VCPU_REGS_RCX] = c->eip;
1870 if (is_long_mode(ctxt->vcpu)) {
1871#ifdef CONFIG_X86_64
1872 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1873
Avi Kivity717746e2011-04-20 13:37:53 +03001874 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001875 ctxt->mode == X86EMUL_MODE_PROT64 ?
1876 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001877 c->eip = msr_data;
1878
Avi Kivity717746e2011-04-20 13:37:53 +03001879 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001880 ctxt->eflags &= ~(msr_data | EFLG_RF);
1881#endif
1882 } else {
1883 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03001884 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001885 c->eip = (u32)msr_data;
1886
1887 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1888 }
1889
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001890 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001891}
1892
Andre Przywara8c604352009-06-18 12:56:01 +02001893static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001894emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001895{
1896 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001897 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001898 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001899 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001900
Gleb Natapova0044752010-02-10 14:21:31 +02001901 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001902 if (ctxt->mode == X86EMUL_MODE_REAL)
1903 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001904
1905 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1906 * Therefore, we inject an #UD.
1907 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001908 if (ctxt->mode == X86EMUL_MODE_PROT64)
1909 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02001910
Gleb Natapov79168fd2010-04-28 19:15:30 +03001911 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001912
Avi Kivity717746e2011-04-20 13:37:53 +03001913 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001914 switch (ctxt->mode) {
1915 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001916 if ((msr_data & 0xfffc) == 0x0)
1917 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001918 break;
1919 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001920 if (msr_data == 0x0)
1921 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02001922 break;
1923 }
1924
1925 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001926 cs_sel = (u16)msr_data;
1927 cs_sel &= ~SELECTOR_RPL_MASK;
1928 ss_sel = cs_sel + 8;
1929 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001930 if (ctxt->mode == X86EMUL_MODE_PROT64
1931 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001932 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001933 cs.l = 1;
1934 }
1935
Avi Kivity4bff1e862011-04-20 13:37:53 +03001936 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1937 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1938 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1939 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02001940
Avi Kivity717746e2011-04-20 13:37:53 +03001941 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001942 c->eip = msr_data;
1943
Avi Kivity717746e2011-04-20 13:37:53 +03001944 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001945 c->regs[VCPU_REGS_RSP] = msr_data;
1946
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001947 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001948}
1949
Andre Przywara4668f052009-06-18 12:56:02 +02001950static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001951emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001952{
1953 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001954 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001955 u64 msr_data;
1956 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001957 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001958
Gleb Natapova0044752010-02-10 14:21:31 +02001959 /* inject #GP if in real mode or Virtual 8086 mode */
1960 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001961 ctxt->mode == X86EMUL_MODE_VM86)
1962 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02001963
Gleb Natapov79168fd2010-04-28 19:15:30 +03001964 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001965
1966 if ((c->rex_prefix & 0x8) != 0x0)
1967 usermode = X86EMUL_MODE_PROT64;
1968 else
1969 usermode = X86EMUL_MODE_PROT32;
1970
1971 cs.dpl = 3;
1972 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03001973 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001974 switch (usermode) {
1975 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001976 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001977 if ((msr_data & 0xfffc) == 0x0)
1978 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001979 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001980 break;
1981 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001982 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001983 if (msr_data == 0x0)
1984 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001985 ss_sel = cs_sel + 8;
1986 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001987 cs.l = 1;
1988 break;
1989 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001990 cs_sel |= SELECTOR_RPL_MASK;
1991 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001992
Avi Kivity4bff1e862011-04-20 13:37:53 +03001993 ops->set_cached_descriptor(ctxt, &cs, 0, VCPU_SREG_CS);
1994 ops->set_segment_selector(ctxt, cs_sel, VCPU_SREG_CS);
1995 ops->set_cached_descriptor(ctxt, &ss, 0, VCPU_SREG_SS);
1996 ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02001997
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001998 c->eip = c->regs[VCPU_REGS_RDX];
1999 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02002000
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002001 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002002}
2003
Gleb Natapov9c537242010-03-18 15:20:05 +02002004static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
2005 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002006{
2007 int iopl;
2008 if (ctxt->mode == X86EMUL_MODE_REAL)
2009 return false;
2010 if (ctxt->mode == X86EMUL_MODE_VM86)
2011 return true;
2012 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity717746e2011-04-20 13:37:53 +03002013 return ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002014}
2015
2016static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
2017 struct x86_emulate_ops *ops,
2018 u16 port, u16 len)
2019{
Gleb Natapov79168fd2010-04-28 19:15:30 +03002020 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002021 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002022 int r;
Gleb Natapov399a40c2011-03-07 14:55:07 +02002023 u16 io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002024 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002025 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002026
Avi Kivity4bff1e862011-04-20 13:37:53 +03002027 ops->get_cached_descriptor(ctxt, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002028 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002029 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002030 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002031 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002032 base = get_desc_base(&tr_seg);
2033#ifdef CONFIG_X86_64
2034 base |= ((u64)base3) << 32;
2035#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002036 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002037 if (r != X86EMUL_CONTINUE)
2038 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002039 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002040 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002041 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002042 if (r != X86EMUL_CONTINUE)
2043 return false;
2044 if ((perm >> bit_idx) & mask)
2045 return false;
2046 return true;
2047}
2048
2049static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
2050 struct x86_emulate_ops *ops,
2051 u16 port, u16 len)
2052{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002053 if (ctxt->perm_ok)
2054 return true;
2055
Gleb Natapov9c537242010-03-18 15:20:05 +02002056 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002057 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
2058 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002059
2060 ctxt->perm_ok = true;
2061
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002062 return true;
2063}
2064
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002065static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
2066 struct x86_emulate_ops *ops,
2067 struct tss_segment_16 *tss)
2068{
2069 struct decode_cache *c = &ctxt->decode;
2070
2071 tss->ip = c->eip;
2072 tss->flag = ctxt->eflags;
2073 tss->ax = c->regs[VCPU_REGS_RAX];
2074 tss->cx = c->regs[VCPU_REGS_RCX];
2075 tss->dx = c->regs[VCPU_REGS_RDX];
2076 tss->bx = c->regs[VCPU_REGS_RBX];
2077 tss->sp = c->regs[VCPU_REGS_RSP];
2078 tss->bp = c->regs[VCPU_REGS_RBP];
2079 tss->si = c->regs[VCPU_REGS_RSI];
2080 tss->di = c->regs[VCPU_REGS_RDI];
2081
Avi Kivity4bff1e862011-04-20 13:37:53 +03002082 tss->es = ops->get_segment_selector(ctxt, VCPU_SREG_ES);
2083 tss->cs = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
2084 tss->ss = ops->get_segment_selector(ctxt, VCPU_SREG_SS);
2085 tss->ds = ops->get_segment_selector(ctxt, VCPU_SREG_DS);
2086 tss->ldt = ops->get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002087}
2088
2089static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
2090 struct x86_emulate_ops *ops,
2091 struct tss_segment_16 *tss)
2092{
2093 struct decode_cache *c = &ctxt->decode;
2094 int ret;
2095
2096 c->eip = tss->ip;
2097 ctxt->eflags = tss->flag | 2;
2098 c->regs[VCPU_REGS_RAX] = tss->ax;
2099 c->regs[VCPU_REGS_RCX] = tss->cx;
2100 c->regs[VCPU_REGS_RDX] = tss->dx;
2101 c->regs[VCPU_REGS_RBX] = tss->bx;
2102 c->regs[VCPU_REGS_RSP] = tss->sp;
2103 c->regs[VCPU_REGS_RBP] = tss->bp;
2104 c->regs[VCPU_REGS_RSI] = tss->si;
2105 c->regs[VCPU_REGS_RDI] = tss->di;
2106
2107 /*
2108 * SDM says that segment selectors are loaded before segment
2109 * descriptors
2110 */
Avi Kivity4bff1e862011-04-20 13:37:53 +03002111 ops->set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2112 ops->set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2113 ops->set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2114 ops->set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2115 ops->set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002116
2117 /*
2118 * Now load segment descriptors. If fault happenes at this stage
2119 * it is handled in a context of new task
2120 */
2121 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
2122 if (ret != X86EMUL_CONTINUE)
2123 return ret;
2124 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2125 if (ret != X86EMUL_CONTINUE)
2126 return ret;
2127 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2128 if (ret != X86EMUL_CONTINUE)
2129 return ret;
2130 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2131 if (ret != X86EMUL_CONTINUE)
2132 return ret;
2133 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2134 if (ret != X86EMUL_CONTINUE)
2135 return ret;
2136
2137 return X86EMUL_CONTINUE;
2138}
2139
2140static int task_switch_16(struct x86_emulate_ctxt *ctxt,
2141 struct x86_emulate_ops *ops,
2142 u16 tss_selector, u16 old_tss_sel,
2143 ulong old_tss_base, struct desc_struct *new_desc)
2144{
2145 struct tss_segment_16 tss_seg;
2146 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002147 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002148
Avi Kivity0f65dd72011-04-20 13:37:53 +03002149 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002150 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002151 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002152 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002153 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002154
2155 save_state_to_tss16(ctxt, ops, &tss_seg);
2156
Avi Kivity0f65dd72011-04-20 13:37:53 +03002157 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002158 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002159 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002160 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002161 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002162
Avi Kivity0f65dd72011-04-20 13:37:53 +03002163 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002164 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002165 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002166 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002167 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002168
2169 if (old_tss_sel != 0xffff) {
2170 tss_seg.prev_task_link = old_tss_sel;
2171
Avi Kivity0f65dd72011-04-20 13:37:53 +03002172 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002173 &tss_seg.prev_task_link,
2174 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002175 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002176 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002177 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002178 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002179 }
2180
2181 return load_state_from_tss16(ctxt, ops, &tss_seg);
2182}
2183
2184static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
2185 struct x86_emulate_ops *ops,
2186 struct tss_segment_32 *tss)
2187{
2188 struct decode_cache *c = &ctxt->decode;
2189
Avi Kivity717746e2011-04-20 13:37:53 +03002190 tss->cr3 = ops->get_cr(ctxt, 3);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002191 tss->eip = c->eip;
2192 tss->eflags = ctxt->eflags;
2193 tss->eax = c->regs[VCPU_REGS_RAX];
2194 tss->ecx = c->regs[VCPU_REGS_RCX];
2195 tss->edx = c->regs[VCPU_REGS_RDX];
2196 tss->ebx = c->regs[VCPU_REGS_RBX];
2197 tss->esp = c->regs[VCPU_REGS_RSP];
2198 tss->ebp = c->regs[VCPU_REGS_RBP];
2199 tss->esi = c->regs[VCPU_REGS_RSI];
2200 tss->edi = c->regs[VCPU_REGS_RDI];
2201
Avi Kivity4bff1e862011-04-20 13:37:53 +03002202 tss->es = ops->get_segment_selector(ctxt, VCPU_SREG_ES);
2203 tss->cs = ops->get_segment_selector(ctxt, VCPU_SREG_CS);
2204 tss->ss = ops->get_segment_selector(ctxt, VCPU_SREG_SS);
2205 tss->ds = ops->get_segment_selector(ctxt, VCPU_SREG_DS);
2206 tss->fs = ops->get_segment_selector(ctxt, VCPU_SREG_FS);
2207 tss->gs = ops->get_segment_selector(ctxt, VCPU_SREG_GS);
2208 tss->ldt_selector = ops->get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002209}
2210
2211static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
2212 struct x86_emulate_ops *ops,
2213 struct tss_segment_32 *tss)
2214{
2215 struct decode_cache *c = &ctxt->decode;
2216 int ret;
2217
Avi Kivity717746e2011-04-20 13:37:53 +03002218 if (ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002219 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002220 c->eip = tss->eip;
2221 ctxt->eflags = tss->eflags | 2;
2222 c->regs[VCPU_REGS_RAX] = tss->eax;
2223 c->regs[VCPU_REGS_RCX] = tss->ecx;
2224 c->regs[VCPU_REGS_RDX] = tss->edx;
2225 c->regs[VCPU_REGS_RBX] = tss->ebx;
2226 c->regs[VCPU_REGS_RSP] = tss->esp;
2227 c->regs[VCPU_REGS_RBP] = tss->ebp;
2228 c->regs[VCPU_REGS_RSI] = tss->esi;
2229 c->regs[VCPU_REGS_RDI] = tss->edi;
2230
2231 /*
2232 * SDM says that segment selectors are loaded before segment
2233 * descriptors
2234 */
Avi Kivity4bff1e862011-04-20 13:37:53 +03002235 ops->set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2236 ops->set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2237 ops->set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2238 ops->set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2239 ops->set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2240 ops->set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2241 ops->set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002242
2243 /*
2244 * Now load segment descriptors. If fault happenes at this stage
2245 * it is handled in a context of new task
2246 */
2247 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
2248 if (ret != X86EMUL_CONTINUE)
2249 return ret;
2250 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
2251 if (ret != X86EMUL_CONTINUE)
2252 return ret;
2253 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
2254 if (ret != X86EMUL_CONTINUE)
2255 return ret;
2256 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
2257 if (ret != X86EMUL_CONTINUE)
2258 return ret;
2259 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
2260 if (ret != X86EMUL_CONTINUE)
2261 return ret;
2262 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
2263 if (ret != X86EMUL_CONTINUE)
2264 return ret;
2265 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
2266 if (ret != X86EMUL_CONTINUE)
2267 return ret;
2268
2269 return X86EMUL_CONTINUE;
2270}
2271
2272static int task_switch_32(struct x86_emulate_ctxt *ctxt,
2273 struct x86_emulate_ops *ops,
2274 u16 tss_selector, u16 old_tss_sel,
2275 ulong old_tss_base, struct desc_struct *new_desc)
2276{
2277 struct tss_segment_32 tss_seg;
2278 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002279 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002280
Avi Kivity0f65dd72011-04-20 13:37:53 +03002281 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002282 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002283 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002284 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002285 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002286
2287 save_state_to_tss32(ctxt, ops, &tss_seg);
2288
Avi Kivity0f65dd72011-04-20 13:37:53 +03002289 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002290 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002291 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002292 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002293 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002294
Avi Kivity0f65dd72011-04-20 13:37:53 +03002295 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002296 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002297 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002298 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002299 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002300
2301 if (old_tss_sel != 0xffff) {
2302 tss_seg.prev_task_link = old_tss_sel;
2303
Avi Kivity0f65dd72011-04-20 13:37:53 +03002304 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002305 &tss_seg.prev_task_link,
2306 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002307 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002308 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002309 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002310 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002311 }
2312
2313 return load_state_from_tss32(ctxt, ops, &tss_seg);
2314}
2315
2316static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002317 struct x86_emulate_ops *ops,
2318 u16 tss_selector, int reason,
2319 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002320{
2321 struct desc_struct curr_tss_desc, next_tss_desc;
2322 int ret;
Avi Kivity4bff1e862011-04-20 13:37:53 +03002323 u16 old_tss_sel = ops->get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002324 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002325 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002326 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002327
2328 /* FIXME: old_tss_base == ~0 ? */
2329
2330 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2331 if (ret != X86EMUL_CONTINUE)
2332 return ret;
2333 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2334 if (ret != X86EMUL_CONTINUE)
2335 return ret;
2336
2337 /* FIXME: check that next_tss_desc is tss */
2338
2339 if (reason != TASK_SWITCH_IRET) {
2340 if ((tss_selector & 3) > next_tss_desc.dpl ||
Avi Kivity717746e2011-04-20 13:37:53 +03002341 ops->cpl(ctxt) > next_tss_desc.dpl)
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002342 return emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002343 }
2344
Gleb Natapovceffb452010-03-18 15:20:19 +02002345 desc_limit = desc_limit_scaled(&next_tss_desc);
2346 if (!next_tss_desc.p ||
2347 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2348 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002349 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002350 return X86EMUL_PROPAGATE_FAULT;
2351 }
2352
2353 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2354 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2355 write_segment_descriptor(ctxt, ops, old_tss_sel,
2356 &curr_tss_desc);
2357 }
2358
2359 if (reason == TASK_SWITCH_IRET)
2360 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2361
2362 /* set back link to prev task only if NT bit is set in eflags
2363 note that old_tss_sel is not used afetr this point */
2364 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2365 old_tss_sel = 0xffff;
2366
2367 if (next_tss_desc.type & 8)
2368 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2369 old_tss_base, &next_tss_desc);
2370 else
2371 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2372 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002373 if (ret != X86EMUL_CONTINUE)
2374 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002375
2376 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2377 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2378
2379 if (reason != TASK_SWITCH_IRET) {
2380 next_tss_desc.type |= (1 << 1); /* set busy flag */
2381 write_segment_descriptor(ctxt, ops, tss_selector,
2382 &next_tss_desc);
2383 }
2384
Avi Kivity717746e2011-04-20 13:37:53 +03002385 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity4bff1e862011-04-20 13:37:53 +03002386 ops->set_cached_descriptor(ctxt, &next_tss_desc, 0, VCPU_SREG_TR);
2387 ops->set_segment_selector(ctxt, tss_selector, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002388
Jan Kiszkae269fb22010-04-14 15:51:09 +02002389 if (has_error_code) {
2390 struct decode_cache *c = &ctxt->decode;
2391
2392 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2393 c->lock_prefix = 0;
2394 c->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002395 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002396 }
2397
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002398 return ret;
2399}
2400
2401int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002402 u16 tss_selector, int reason,
2403 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002404{
Avi Kivity9aabc882010-07-29 15:11:50 +03002405 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002406 struct decode_cache *c = &ctxt->decode;
2407 int rc;
2408
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002409 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002410 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002411
Jan Kiszkae269fb22010-04-14 15:51:09 +02002412 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2413 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)
2416 ctxt->eip = c->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{
2424 struct decode_cache *c = &ctxt->decode;
2425 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2426
Gleb Natapovd9271122010-03-18 15:20:22 +02002427 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity90de84f2010-11-17 15:28:21 +02002428 op->addr.mem.ea = register_address(c, c->regs[reg]);
2429 op->addr.mem.seg = seg;
Gleb Natapova682e352010-03-18 15:20:21 +02002430}
2431
Avi Kivity7af04fc2010-08-18 14:16:35 +03002432static int em_das(struct x86_emulate_ctxt *ctxt)
2433{
2434 struct decode_cache *c = &ctxt->decode;
2435 u8 al, old_al;
2436 bool af, cf, old_cf;
2437
2438 cf = ctxt->eflags & X86_EFLAGS_CF;
2439 al = c->dst.val;
2440
2441 old_al = al;
2442 old_cf = cf;
2443 cf = false;
2444 af = ctxt->eflags & X86_EFLAGS_AF;
2445 if ((al & 0x0f) > 9 || af) {
2446 al -= 6;
2447 cf = old_cf | (al >= 250);
2448 af = true;
2449 } else {
2450 af = false;
2451 }
2452 if (old_al > 0x99 || old_cf) {
2453 al -= 0x60;
2454 cf = true;
2455 }
2456
2457 c->dst.val = al;
2458 /* Set PF, ZF, SF */
2459 c->src.type = OP_IMM;
2460 c->src.val = 0;
2461 c->src.bytes = 1;
2462 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
2463 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2464 if (cf)
2465 ctxt->eflags |= X86_EFLAGS_CF;
2466 if (af)
2467 ctxt->eflags |= X86_EFLAGS_AF;
2468 return X86EMUL_CONTINUE;
2469}
2470
Avi Kivity0ef753b2010-08-18 14:51:45 +03002471static int em_call_far(struct x86_emulate_ctxt *ctxt)
2472{
2473 struct decode_cache *c = &ctxt->decode;
2474 u16 sel, old_cs;
2475 ulong old_eip;
2476 int rc;
2477
Avi Kivity4bff1e862011-04-20 13:37:53 +03002478 old_cs = ctxt->ops->get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002479 old_eip = c->eip;
2480
2481 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2482 if (load_segment_descriptor(ctxt, ctxt->ops, sel, VCPU_SREG_CS))
2483 return X86EMUL_CONTINUE;
2484
2485 c->eip = 0;
2486 memcpy(&c->eip, c->src.valptr, c->op_bytes);
2487
2488 c->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002489 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002490 if (rc != X86EMUL_CONTINUE)
2491 return rc;
2492
2493 c->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002494 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002495}
2496
Avi Kivity40ece7c2010-08-18 15:12:09 +03002497static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2498{
2499 struct decode_cache *c = &ctxt->decode;
2500 int rc;
2501
2502 c->dst.type = OP_REG;
2503 c->dst.addr.reg = &c->eip;
2504 c->dst.bytes = c->op_bytes;
2505 rc = emulate_pop(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
2506 if (rc != X86EMUL_CONTINUE)
2507 return rc;
2508 register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
2509 return X86EMUL_CONTINUE;
2510}
2511
Avi Kivity5c82aa22010-08-18 18:31:43 +03002512static int em_imul(struct x86_emulate_ctxt *ctxt)
2513{
2514 struct decode_cache *c = &ctxt->decode;
2515
2516 emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
2517 return X86EMUL_CONTINUE;
2518}
2519
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002520static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
2521{
2522 struct decode_cache *c = &ctxt->decode;
2523
2524 c->dst.val = c->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03002525 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002526}
2527
Avi Kivity61429142010-08-19 15:13:00 +03002528static int em_cwd(struct x86_emulate_ctxt *ctxt)
2529{
2530 struct decode_cache *c = &ctxt->decode;
2531
2532 c->dst.type = OP_REG;
2533 c->dst.bytes = c->src.bytes;
2534 c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
2535 c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
2536
2537 return X86EMUL_CONTINUE;
2538}
2539
Avi Kivity48bb5d32010-08-18 18:54:34 +03002540static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
2541{
Avi Kivity48bb5d32010-08-18 18:54:34 +03002542 struct decode_cache *c = &ctxt->decode;
2543 u64 tsc = 0;
2544
Avi Kivity717746e2011-04-20 13:37:53 +03002545 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivity48bb5d32010-08-18 18:54:34 +03002546 c->regs[VCPU_REGS_RAX] = (u32)tsc;
2547 c->regs[VCPU_REGS_RDX] = tsc >> 32;
2548 return X86EMUL_CONTINUE;
2549}
2550
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002551static int em_mov(struct x86_emulate_ctxt *ctxt)
2552{
2553 struct decode_cache *c = &ctxt->decode;
2554 c->dst.val = c->src.val;
2555 return X86EMUL_CONTINUE;
2556}
2557
Avi Kivityaa97bb42010-01-20 18:09:23 +02002558static int em_movdqu(struct x86_emulate_ctxt *ctxt)
2559{
2560 struct decode_cache *c = &ctxt->decode;
2561 memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
2562 return X86EMUL_CONTINUE;
2563}
2564
Avi Kivity38503912011-03-31 18:48:09 +02002565static int em_invlpg(struct x86_emulate_ctxt *ctxt)
2566{
2567 struct decode_cache *c = &ctxt->decode;
Avi Kivity9fa088f2011-03-31 18:54:30 +02002568 int rc;
2569 ulong linear;
2570
Avi Kivity83b87952011-04-03 11:31:19 +03002571 rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02002572 if (rc == X86EMUL_CONTINUE)
2573 emulate_invlpg(ctxt->vcpu, linear);
Avi Kivity38503912011-03-31 18:48:09 +02002574 /* Disable writeback. */
2575 c->dst.type = OP_NONE;
2576 return X86EMUL_CONTINUE;
2577}
2578
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002579static bool valid_cr(int nr)
2580{
2581 switch (nr) {
2582 case 0:
2583 case 2 ... 4:
2584 case 8:
2585 return true;
2586 default:
2587 return false;
2588 }
2589}
2590
2591static int check_cr_read(struct x86_emulate_ctxt *ctxt)
2592{
2593 struct decode_cache *c = &ctxt->decode;
2594
2595 if (!valid_cr(c->modrm_reg))
2596 return emulate_ud(ctxt);
2597
2598 return X86EMUL_CONTINUE;
2599}
2600
2601static int check_cr_write(struct x86_emulate_ctxt *ctxt)
2602{
2603 struct decode_cache *c = &ctxt->decode;
2604 u64 new_val = c->src.val64;
2605 int cr = c->modrm_reg;
2606
2607 static u64 cr_reserved_bits[] = {
2608 0xffffffff00000000ULL,
2609 0, 0, 0, /* CR3 checked later */
2610 CR4_RESERVED_BITS,
2611 0, 0, 0,
2612 CR8_RESERVED_BITS,
2613 };
2614
2615 if (!valid_cr(cr))
2616 return emulate_ud(ctxt);
2617
2618 if (new_val & cr_reserved_bits[cr])
2619 return emulate_gp(ctxt, 0);
2620
2621 switch (cr) {
2622 case 0: {
2623 u64 cr4, efer;
2624 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
2625 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
2626 return emulate_gp(ctxt, 0);
2627
Avi Kivity717746e2011-04-20 13:37:53 +03002628 cr4 = ctxt->ops->get_cr(ctxt, 4);
2629 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002630
2631 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
2632 !(cr4 & X86_CR4_PAE))
2633 return emulate_gp(ctxt, 0);
2634
2635 break;
2636 }
2637 case 3: {
2638 u64 rsvd = 0;
2639
2640 if (is_long_mode(ctxt->vcpu))
2641 rsvd = CR3_L_MODE_RESERVED_BITS;
2642 else if (is_pae(ctxt->vcpu))
2643 rsvd = CR3_PAE_RESERVED_BITS;
2644 else if (is_paging(ctxt->vcpu))
2645 rsvd = CR3_NONPAE_RESERVED_BITS;
2646
2647 if (new_val & rsvd)
2648 return emulate_gp(ctxt, 0);
2649
2650 break;
2651 }
2652 case 4: {
2653 u64 cr4, efer;
2654
Avi Kivity717746e2011-04-20 13:37:53 +03002655 cr4 = ctxt->ops->get_cr(ctxt, 4);
2656 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002657
2658 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
2659 return emulate_gp(ctxt, 0);
2660
2661 break;
2662 }
2663 }
2664
2665 return X86EMUL_CONTINUE;
2666}
2667
Joerg Roedel3b88e412011-04-04 12:39:29 +02002668static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
2669{
2670 unsigned long dr7;
2671
Avi Kivity717746e2011-04-20 13:37:53 +03002672 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002673
2674 /* Check if DR7.Global_Enable is set */
2675 return dr7 & (1 << 13);
2676}
2677
2678static int check_dr_read(struct x86_emulate_ctxt *ctxt)
2679{
2680 struct decode_cache *c = &ctxt->decode;
2681 int dr = c->modrm_reg;
2682 u64 cr4;
2683
2684 if (dr > 7)
2685 return emulate_ud(ctxt);
2686
Avi Kivity717746e2011-04-20 13:37:53 +03002687 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02002688 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
2689 return emulate_ud(ctxt);
2690
2691 if (check_dr7_gd(ctxt))
2692 return emulate_db(ctxt);
2693
2694 return X86EMUL_CONTINUE;
2695}
2696
2697static int check_dr_write(struct x86_emulate_ctxt *ctxt)
2698{
2699 struct decode_cache *c = &ctxt->decode;
2700 u64 new_val = c->src.val64;
2701 int dr = c->modrm_reg;
2702
2703 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
2704 return emulate_gp(ctxt, 0);
2705
2706 return check_dr_read(ctxt);
2707}
2708
Joerg Roedel01de8b02011-04-04 12:39:31 +02002709static int check_svme(struct x86_emulate_ctxt *ctxt)
2710{
2711 u64 efer;
2712
Avi Kivity717746e2011-04-20 13:37:53 +03002713 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02002714
2715 if (!(efer & EFER_SVME))
2716 return emulate_ud(ctxt);
2717
2718 return X86EMUL_CONTINUE;
2719}
2720
2721static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
2722{
2723 u64 rax = kvm_register_read(ctxt->vcpu, VCPU_REGS_RAX);
2724
2725 /* Valid physical address? */
2726 if (rax & 0xffff000000000000)
2727 return emulate_gp(ctxt, 0);
2728
2729 return check_svme(ctxt);
2730}
2731
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002732static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
2733{
Avi Kivity717746e2011-04-20 13:37:53 +03002734 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002735
Avi Kivity717746e2011-04-20 13:37:53 +03002736 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002737 return emulate_ud(ctxt);
2738
2739 return X86EMUL_CONTINUE;
2740}
2741
Joerg Roedel80612522011-04-04 12:39:33 +02002742static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
2743{
Avi Kivity717746e2011-04-20 13:37:53 +03002744 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel80612522011-04-04 12:39:33 +02002745 u64 rcx = kvm_register_read(ctxt->vcpu, VCPU_REGS_RCX);
2746
Avi Kivity717746e2011-04-20 13:37:53 +03002747 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02002748 (rcx > 3))
2749 return emulate_gp(ctxt, 0);
2750
2751 return X86EMUL_CONTINUE;
2752}
2753
Joerg Roedelf6511932011-04-04 12:39:35 +02002754static int check_perm_in(struct x86_emulate_ctxt *ctxt)
2755{
2756 struct decode_cache *c = &ctxt->decode;
2757
2758 c->dst.bytes = min(c->dst.bytes, 4u);
2759 if (!emulator_io_permited(ctxt, ctxt->ops, c->src.val, c->dst.bytes))
2760 return emulate_gp(ctxt, 0);
2761
2762 return X86EMUL_CONTINUE;
2763}
2764
2765static int check_perm_out(struct x86_emulate_ctxt *ctxt)
2766{
2767 struct decode_cache *c = &ctxt->decode;
2768
2769 c->src.bytes = min(c->src.bytes, 4u);
2770 if (!emulator_io_permited(ctxt, ctxt->ops, c->dst.val, c->src.bytes))
2771 return emulate_gp(ctxt, 0);
2772
2773 return X86EMUL_CONTINUE;
2774}
2775
Avi Kivity73fba5f2010-07-29 15:11:53 +03002776#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002777#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002778#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
2779 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002780#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02002781#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002782#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2783#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2784#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02002785#define II(_f, _e, _i) \
2786 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02002787#define IIP(_f, _e, _i, _p) \
2788 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
2789 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02002790#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03002791
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002792#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02002793#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03002794#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
2795
Avi Kivity6230f7f2010-08-26 18:34:55 +03002796#define D6ALU(_f) D2bv((_f) | DstMem | SrcReg | ModRM), \
2797 D2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock), \
2798 D2bv(((_f) & ~Lock) | DstAcc | SrcImm)
2799
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002800static struct opcode group7_rm1[] = {
2801 DI(SrcNone | ModRM | Priv, monitor),
2802 DI(SrcNone | ModRM | Priv, mwait),
2803 N, N, N, N, N, N,
2804};
2805
Joerg Roedel01de8b02011-04-04 12:39:31 +02002806static struct opcode group7_rm3[] = {
2807 DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
Avi Kivitybfeed292011-04-05 16:25:20 +03002808 DI(SrcNone | ModRM | Prot | VendorSpecific, vmmcall),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002809 DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
2810 DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
2811 DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
2812 DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
2813 DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
2814 DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
2815};
Avi Kivity6230f7f2010-08-26 18:34:55 +03002816
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002817static struct opcode group7_rm7[] = {
2818 N,
2819 DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
2820 N, N, N, N, N, N,
2821};
Avi Kivity73fba5f2010-07-29 15:11:53 +03002822static struct opcode group1[] = {
2823 X7(D(Lock)), N
2824};
2825
2826static struct opcode group1A[] = {
2827 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2828};
2829
2830static struct opcode group3[] = {
2831 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2832 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002833 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002834};
2835
2836static struct opcode group4[] = {
2837 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2838 N, N, N, N, N, N,
2839};
2840
2841static struct opcode group5[] = {
2842 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Avi Kivity0ef753b2010-08-18 14:51:45 +03002843 D(SrcMem | ModRM | Stack),
2844 I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002845 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2846 D(SrcMem | ModRM | Stack), N,
2847};
2848
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002849static struct opcode group6[] = {
2850 DI(ModRM | Prot, sldt),
2851 DI(ModRM | Prot, str),
2852 DI(ModRM | Prot | Priv, lldt),
2853 DI(ModRM | Prot | Priv, ltr),
2854 N, N, N, N,
2855};
2856
Avi Kivity73fba5f2010-07-29 15:11:53 +03002857static struct group_dual group7 = { {
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002858 DI(ModRM | Mov | DstMem | Priv, sgdt),
2859 DI(ModRM | Mov | DstMem | Priv, sidt),
2860 DI(ModRM | SrcMem | Priv, lgdt), DI(ModRM | SrcMem | Priv, lidt),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002861 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
2862 DI(SrcMem16 | ModRM | Mov | Priv, lmsw),
2863 DI(SrcMem | ModRM | ByteOp | Priv | NoAccess, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002864}, {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002865 D(SrcNone | ModRM | Priv | VendorSpecific), EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02002866 N, EXT(0, group7_rm3),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002867 DI(SrcNone | ModRM | DstMem | Mov, smsw), N,
Joerg Roedeld7eb8202011-04-04 12:39:32 +02002868 DI(SrcMem16 | ModRM | Mov | Priv, lmsw), EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002869} };
2870
2871static struct opcode group8[] = {
2872 N, N, N, N,
2873 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2874 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2875};
2876
2877static struct group_dual group9 = { {
2878 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2879}, {
2880 N, N, N, N, N, N, N, N,
2881} };
2882
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002883static struct opcode group11[] = {
2884 I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
2885};
2886
Avi Kivityaa97bb42010-01-20 18:09:23 +02002887static struct gprefix pfx_0f_6f_0f_7f = {
2888 N, N, N, I(Sse, em_movdqu),
2889};
2890
Avi Kivity73fba5f2010-07-29 15:11:53 +03002891static struct opcode opcode_table[256] = {
2892 /* 0x00 - 0x07 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002893 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002894 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2895 /* 0x08 - 0x0F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002896 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002897 D(ImplicitOps | Stack | No64), N,
2898 /* 0x10 - 0x17 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002899 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002900 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2901 /* 0x18 - 0x1F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002902 D6ALU(Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002903 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2904 /* 0x20 - 0x27 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002905 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002906 /* 0x28 - 0x2F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002907 D6ALU(Lock), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002908 /* 0x30 - 0x37 */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002909 D6ALU(Lock), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002910 /* 0x38 - 0x3F */
Avi Kivity6230f7f2010-08-26 18:34:55 +03002911 D6ALU(0), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002912 /* 0x40 - 0x4F */
2913 X16(D(DstReg)),
2914 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002915 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002916 /* 0x58 - 0x5F */
2917 X8(D(DstReg | Stack)),
2918 /* 0x60 - 0x67 */
2919 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2920 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2921 N, N, N, N,
2922 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03002923 I(SrcImm | Mov | Stack, em_push),
2924 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03002925 I(SrcImmByte | Mov | Stack, em_push),
2926 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Joerg Roedelf6511932011-04-04 12:39:35 +02002927 D2bvIP(DstDI | Mov | String, ins, check_perm_in), /* insb, insw/insd */
2928 D2bvIP(SrcSI | ImplicitOps | String, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03002929 /* 0x70 - 0x7F */
2930 X16(D(SrcImmByte)),
2931 /* 0x80 - 0x87 */
2932 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2933 G(DstMem | SrcImm | ModRM | Group, group1),
2934 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2935 G(DstMem | SrcImmByte | ModRM | Group, group1),
Avi Kivity76e8e682010-08-26 11:56:09 +03002936 D2bv(DstMem | SrcReg | ModRM), D2bv(DstMem | SrcReg | ModRM | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002937 /* 0x88 - 0x8F */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002938 I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
2939 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002940 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002941 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2942 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02002943 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002944 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03002945 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08002946 I(SrcImmFAddr | No64, em_call_far), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002947 DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002948 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002949 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
2950 I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
2951 I2bv(SrcSI | DstDI | Mov | String, em_mov),
2952 D2bv(SrcSI | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002953 /* 0xA8 - 0xAF */
Avi Kivity50748612010-08-26 11:56:10 +03002954 D2bv(DstAcc | SrcImm),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002955 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
2956 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivity48fe67b2010-08-26 11:56:08 +03002957 D2bv(SrcAcc | DstDI | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002958 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002959 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002960 /* 0xB8 - 0xBF */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03002961 X8(I(DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002962 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002963 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03002964 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
2965 D(ImplicitOps | Stack),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002966 D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03002967 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002968 /* 0xC8 - 0xCF */
2969 N, N, N, D(ImplicitOps | Stack),
Avi Kivity3c6e2762011-04-04 12:39:23 +02002970 D(ImplicitOps), DI(SrcImmByte, intn),
2971 D(ImplicitOps | No64), DI(ImplicitOps, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002972 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03002973 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002974 N, N, N, N,
2975 /* 0xD8 - 0xDF */
2976 N, N, N, N, N, N, N, N,
2977 /* 0xE0 - 0xE7 */
Wei Yongjune4abac62010-08-19 14:25:48 +08002978 X4(D(SrcImmByte)),
Joerg Roedelf6511932011-04-04 12:39:35 +02002979 D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
2980 D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002981 /* 0xE8 - 0xEF */
2982 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2983 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
Joerg Roedelf6511932011-04-04 12:39:35 +02002984 D2bvIP(SrcNone | DstAcc, in, check_perm_in),
2985 D2bvIP(SrcAcc | ImplicitOps, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002986 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02002987 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002988 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
2989 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002990 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002991 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002992 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2993};
2994
2995static struct opcode twobyte_table[256] = {
2996 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02002997 G(0, group6), GD(0, &group7), N, N,
Joerg Roedelcfec82c2011-04-04 12:39:28 +02002998 N, D(ImplicitOps | VendorSpecific), DI(ImplicitOps | Priv, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02002999 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003000 N, D(ImplicitOps | ModRM), N, N,
3001 /* 0x10 - 0x1F */
3002 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3003 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003004 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003005 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003006 DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003007 DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003008 N, N, N, N,
3009 N, N, N, N, N, N, N, N,
3010 /* 0x30 - 0x3F */
Joerg Roedel80612522011-04-04 12:39:33 +02003011 DI(ImplicitOps | Priv, wrmsr),
3012 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
3013 DI(ImplicitOps | Priv, rdmsr),
3014 DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
Avi Kivityd8671622011-02-01 16:32:03 +02003015 D(ImplicitOps | VendorSpecific), D(ImplicitOps | Priv | VendorSpecific),
3016 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003017 N, N, N, N, N, N, N, N,
3018 /* 0x40 - 0x4F */
3019 X16(D(DstReg | SrcMem | ModRM | Mov)),
3020 /* 0x50 - 0x5F */
3021 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3022 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003023 N, N, N, N,
3024 N, N, N, N,
3025 N, N, N, N,
3026 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003027 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003028 N, N, N, N,
3029 N, N, N, N,
3030 N, N, N, N,
3031 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003032 /* 0x80 - 0x8F */
3033 X16(D(SrcImm)),
3034 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08003035 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003036 /* 0xA0 - 0xA7 */
3037 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003038 DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003039 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3040 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
3041 /* 0xA8 - 0xAF */
3042 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
Joerg Roedel80612522011-04-04 12:39:33 +02003043 DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003044 D(DstMem | SrcReg | Src2ImmByte | ModRM),
3045 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03003046 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003047 /* 0xB0 - 0xB7 */
Avi Kivity739ae402010-08-26 11:56:13 +03003048 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003049 D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
3050 D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
3051 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003052 /* 0xB8 - 0xBF */
3053 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08003054 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Wei Yongjund9574a22010-08-10 13:48:22 +08003055 D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
3056 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003057 /* 0xC0 - 0xCF */
Avi Kivity739ae402010-08-26 11:56:13 +03003058 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08003059 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003060 N, N, N, GD(0, &group9),
3061 N, N, N, N, N, N, N, N,
3062 /* 0xD0 - 0xDF */
3063 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3064 /* 0xE0 - 0xEF */
3065 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3066 /* 0xF0 - 0xFF */
3067 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
3068};
3069
3070#undef D
3071#undef N
3072#undef G
3073#undef GD
3074#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02003075#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02003076#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03003077
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003078#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02003079#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003080#undef I2bv
Avi Kivity6230f7f2010-08-26 18:34:55 +03003081#undef D6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003082
Avi Kivity39f21ee2010-08-18 19:20:21 +03003083static unsigned imm_size(struct decode_cache *c)
3084{
3085 unsigned size;
3086
3087 size = (c->d & ByteOp) ? 1 : c->op_bytes;
3088 if (size == 8)
3089 size = 4;
3090 return size;
3091}
3092
3093static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
3094 unsigned size, bool sign_extension)
3095{
3096 struct decode_cache *c = &ctxt->decode;
3097 struct x86_emulate_ops *ops = ctxt->ops;
3098 int rc = X86EMUL_CONTINUE;
3099
3100 op->type = OP_IMM;
3101 op->bytes = size;
Avi Kivity90de84f2010-11-17 15:28:21 +02003102 op->addr.mem.ea = c->eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03003103 /* NB. Immediates are sign-extended as necessary. */
3104 switch (op->bytes) {
3105 case 1:
3106 op->val = insn_fetch(s8, 1, c->eip);
3107 break;
3108 case 2:
3109 op->val = insn_fetch(s16, 2, c->eip);
3110 break;
3111 case 4:
3112 op->val = insn_fetch(s32, 4, c->eip);
3113 break;
3114 }
3115 if (!sign_extension) {
3116 switch (op->bytes) {
3117 case 1:
3118 op->val &= 0xff;
3119 break;
3120 case 2:
3121 op->val &= 0xffff;
3122 break;
3123 case 4:
3124 op->val &= 0xffffffff;
3125 break;
3126 }
3127 }
3128done:
3129 return rc;
3130}
3131
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003132int
Andre Przywaradc25e892010-12-21 11:12:07 +01003133x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003134{
3135 struct x86_emulate_ops *ops = ctxt->ops;
3136 struct decode_cache *c = &ctxt->decode;
3137 int rc = X86EMUL_CONTINUE;
3138 int mode = ctxt->mode;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003139 int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
3140 bool op_prefix = false;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003141 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003142 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003143
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003144 c->eip = ctxt->eip;
Andre Przywaradc25e892010-12-21 11:12:07 +01003145 c->fetch.start = c->eip;
3146 c->fetch.end = c->fetch.start + insn_len;
3147 if (insn_len > 0)
3148 memcpy(c->fetch.data, insn, insn_len);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003149
3150 switch (mode) {
3151 case X86EMUL_MODE_REAL:
3152 case X86EMUL_MODE_VM86:
3153 case X86EMUL_MODE_PROT16:
3154 def_op_bytes = def_ad_bytes = 2;
3155 break;
3156 case X86EMUL_MODE_PROT32:
3157 def_op_bytes = def_ad_bytes = 4;
3158 break;
3159#ifdef CONFIG_X86_64
3160 case X86EMUL_MODE_PROT64:
3161 def_op_bytes = 4;
3162 def_ad_bytes = 8;
3163 break;
3164#endif
3165 default:
3166 return -1;
3167 }
3168
3169 c->op_bytes = def_op_bytes;
3170 c->ad_bytes = def_ad_bytes;
3171
3172 /* Legacy prefixes. */
3173 for (;;) {
3174 switch (c->b = insn_fetch(u8, 1, c->eip)) {
3175 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003176 op_prefix = true;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003177 /* switch between 2/4 bytes */
3178 c->op_bytes = def_op_bytes ^ 6;
3179 break;
3180 case 0x67: /* address-size override */
3181 if (mode == X86EMUL_MODE_PROT64)
3182 /* switch between 4/8 bytes */
3183 c->ad_bytes = def_ad_bytes ^ 12;
3184 else
3185 /* switch between 2/4 bytes */
3186 c->ad_bytes = def_ad_bytes ^ 6;
3187 break;
3188 case 0x26: /* ES override */
3189 case 0x2e: /* CS override */
3190 case 0x36: /* SS override */
3191 case 0x3e: /* DS override */
3192 set_seg_override(c, (c->b >> 3) & 3);
3193 break;
3194 case 0x64: /* FS override */
3195 case 0x65: /* GS override */
3196 set_seg_override(c, c->b & 7);
3197 break;
3198 case 0x40 ... 0x4f: /* REX */
3199 if (mode != X86EMUL_MODE_PROT64)
3200 goto done_prefixes;
3201 c->rex_prefix = c->b;
3202 continue;
3203 case 0xf0: /* LOCK */
3204 c->lock_prefix = 1;
3205 break;
3206 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003207 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity1d6b1142010-01-20 16:00:35 +02003208 c->rep_prefix = c->b;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003209 break;
3210 default:
3211 goto done_prefixes;
3212 }
3213
3214 /* Any legacy prefix after a REX prefix nullifies its effect. */
3215
3216 c->rex_prefix = 0;
3217 }
3218
3219done_prefixes:
3220
3221 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03003222 if (c->rex_prefix & 8)
3223 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003224
3225 /* Opcode byte(s). */
3226 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08003227 /* Two-byte opcode? */
3228 if (c->b == 0x0f) {
3229 c->twobyte = 1;
3230 c->b = insn_fetch(u8, 1, c->eip);
3231 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003232 }
3233 c->d = opcode.flags;
3234
3235 if (c->d & Group) {
3236 dual = c->d & GroupDual;
3237 c->modrm = insn_fetch(u8, 1, c->eip);
3238 --c->eip;
3239
3240 if (c->d & GroupDual) {
3241 g_mod012 = opcode.u.gdual->mod012;
3242 g_mod3 = opcode.u.gdual->mod3;
3243 } else
3244 g_mod012 = g_mod3 = opcode.u.group;
3245
3246 c->d &= ~(Group | GroupDual);
3247
3248 goffset = (c->modrm >> 3) & 7;
3249
3250 if ((c->modrm >> 6) == 3)
3251 opcode = g_mod3[goffset];
3252 else
3253 opcode = g_mod012[goffset];
Joerg Roedel01de8b02011-04-04 12:39:31 +02003254
3255 if (opcode.flags & RMExt) {
3256 goffset = c->modrm & 7;
3257 opcode = opcode.u.group[goffset];
3258 }
3259
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003260 c->d |= opcode.flags;
3261 }
3262
Avi Kivity0d7cdee2011-03-29 11:34:38 +02003263 if (c->d & Prefix) {
3264 if (c->rep_prefix && op_prefix)
3265 return X86EMUL_UNHANDLEABLE;
3266 simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
3267 switch (simd_prefix) {
3268 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
3269 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
3270 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
3271 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
3272 }
3273 c->d |= opcode.flags;
3274 }
3275
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003276 c->execute = opcode.u.execute;
Joerg Roedeld09beab2011-04-04 12:39:25 +02003277 c->check_perm = opcode.check_perm;
Avi Kivityc4f035c2011-04-04 12:39:22 +02003278 c->intercept = opcode.intercept;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003279
3280 /* Unrecognised? */
Avi Kivityd53db5e2010-11-17 13:40:51 +02003281 if (c->d == 0 || (c->d & Undefined))
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003282 return -1;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003283
Avi Kivityd8671622011-02-01 16:32:03 +02003284 if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
3285 return -1;
3286
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003287 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
3288 c->op_bytes = 8;
3289
Avi Kivity7f9b4b72010-08-01 14:46:54 +03003290 if (c->d & Op3264) {
3291 if (mode == X86EMUL_MODE_PROT64)
3292 c->op_bytes = 8;
3293 else
3294 c->op_bytes = 4;
3295 }
3296
Avi Kivity12537912011-03-29 11:41:27 +02003297 if (c->d & Sse)
3298 c->op_bytes = 16;
3299
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003300 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03003301 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003302 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03003303 if (!c->has_seg_override)
3304 set_seg_override(c, c->modrm_seg);
3305 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003306 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003307 if (rc != X86EMUL_CONTINUE)
3308 goto done;
3309
3310 if (!c->has_seg_override)
3311 set_seg_override(c, VCPU_SREG_DS);
3312
Avi Kivity90de84f2010-11-17 15:28:21 +02003313 memop.addr.mem.seg = seg_override(ctxt, ops, c);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003314
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003315 if (memop.type == OP_MEM && c->ad_bytes != 8)
Avi Kivity90de84f2010-11-17 15:28:21 +02003316 memop.addr.mem.ea = (u32)memop.addr.mem.ea;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003317
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003318 if (memop.type == OP_MEM && c->rip_relative)
Avi Kivity90de84f2010-11-17 15:28:21 +02003319 memop.addr.mem.ea += c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003320
3321 /*
3322 * Decode and fetch the source operand: register, memory
3323 * or immediate.
3324 */
3325 switch (c->d & SrcMask) {
3326 case SrcNone:
3327 break;
3328 case SrcReg:
Avi Kivity12537912011-03-29 11:41:27 +02003329 decode_register_operand(ctxt, &c->src, c, 0);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003330 break;
3331 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003332 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003333 goto srcmem_common;
3334 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003335 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003336 goto srcmem_common;
3337 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003338 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003339 c->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003340 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003341 c->src = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003342 break;
Avi Kivityb250e602010-08-18 15:11:24 +03003343 case SrcImmU16:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003344 rc = decode_imm(ctxt, &c->src, 2, false);
3345 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003346 case SrcImm:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003347 rc = decode_imm(ctxt, &c->src, imm_size(c), true);
3348 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003349 case SrcImmU:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003350 rc = decode_imm(ctxt, &c->src, imm_size(c), false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003351 break;
3352 case SrcImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003353 rc = decode_imm(ctxt, &c->src, 1, true);
3354 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003355 case SrcImmUByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003356 rc = decode_imm(ctxt, &c->src, 1, false);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003357 break;
3358 case SrcAcc:
3359 c->src.type = OP_REG;
3360 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003361 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003362 fetch_register_operand(&c->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003363 break;
3364 case SrcOne:
3365 c->src.bytes = 1;
3366 c->src.val = 1;
3367 break;
3368 case SrcSI:
3369 c->src.type = OP_MEM;
3370 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003371 c->src.addr.mem.ea =
3372 register_address(c, c->regs[VCPU_REGS_RSI]);
3373 c->src.addr.mem.seg = seg_override(ctxt, ops, c),
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003374 c->src.val = 0;
3375 break;
3376 case SrcImmFAddr:
3377 c->src.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003378 c->src.addr.mem.ea = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003379 c->src.bytes = c->op_bytes + 2;
3380 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
3381 break;
3382 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003383 memop.bytes = c->op_bytes + 2;
3384 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003385 break;
3386 }
3387
Avi Kivity39f21ee2010-08-18 19:20:21 +03003388 if (rc != X86EMUL_CONTINUE)
3389 goto done;
3390
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003391 /*
3392 * Decode and fetch the second source operand: register, memory
3393 * or immediate.
3394 */
3395 switch (c->d & Src2Mask) {
3396 case Src2None:
3397 break;
3398 case Src2CL:
3399 c->src2.bytes = 1;
3400 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
3401 break;
3402 case Src2ImmByte:
Avi Kivity39f21ee2010-08-18 19:20:21 +03003403 rc = decode_imm(ctxt, &c->src2, 1, true);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003404 break;
3405 case Src2One:
3406 c->src2.bytes = 1;
3407 c->src2.val = 1;
3408 break;
Avi Kivity7db41eb2010-08-18 19:25:28 +03003409 case Src2Imm:
3410 rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
3411 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003412 }
3413
Avi Kivity39f21ee2010-08-18 19:20:21 +03003414 if (rc != X86EMUL_CONTINUE)
3415 goto done;
3416
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003417 /* Decode and fetch the destination operand: register or memory. */
3418 switch (c->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003419 case DstReg:
Avi Kivity12537912011-03-29 11:41:27 +02003420 decode_register_operand(ctxt, &c->dst, c,
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003421 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
3422 break;
Wei Yongjun943858e2010-08-06 11:36:51 +08003423 case DstImmUByte:
3424 c->dst.type = OP_IMM;
Avi Kivity90de84f2010-11-17 15:28:21 +02003425 c->dst.addr.mem.ea = c->eip;
Wei Yongjun943858e2010-08-06 11:36:51 +08003426 c->dst.bytes = 1;
3427 c->dst.val = insn_fetch(u8, 1, c->eip);
3428 break;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003429 case DstMem:
3430 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003431 c->dst = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003432 if ((c->d & DstMask) == DstMem64)
3433 c->dst.bytes = 8;
3434 else
3435 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08003436 if (c->d & BitOp)
3437 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03003438 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003439 break;
3440 case DstAcc:
3441 c->dst.type = OP_REG;
3442 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003443 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03003444 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003445 c->dst.orig_val = c->dst.val;
3446 break;
3447 case DstDI:
3448 c->dst.type = OP_MEM;
3449 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity90de84f2010-11-17 15:28:21 +02003450 c->dst.addr.mem.ea =
3451 register_address(c, c->regs[VCPU_REGS_RDI]);
3452 c->dst.addr.mem.seg = VCPU_SREG_ES;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003453 c->dst.val = 0;
3454 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003455 case ImplicitOps:
3456 /* Special instructions do their own operand decoding. */
3457 default:
3458 c->dst.type = OP_NONE; /* Disable writeback. */
3459 return 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003460 }
3461
3462done:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02003463 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003464}
3465
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003466static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
3467{
3468 struct decode_cache *c = &ctxt->decode;
3469
3470 /* The second termination condition only applies for REPE
3471 * and REPNE. Test if the repeat string operation prefix is
3472 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
3473 * corresponding termination condition according to:
3474 * - if REPE/REPZ and ZF = 0 then done
3475 * - if REPNE/REPNZ and ZF = 1 then done
3476 */
3477 if (((c->b == 0xa6) || (c->b == 0xa7) ||
3478 (c->b == 0xae) || (c->b == 0xaf))
3479 && (((c->rep_prefix == REPE_PREFIX) &&
3480 ((ctxt->eflags & EFLG_ZF) == 0))
3481 || ((c->rep_prefix == REPNE_PREFIX) &&
3482 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
3483 return true;
3484
3485 return false;
3486}
3487
Avi Kivitydde7e6d2010-07-29 15:11:52 +03003488int
Avi Kivity9aabc882010-07-29 15:11:50 +03003489x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003490{
Avi Kivity9aabc882010-07-29 15:11:50 +03003491 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003492 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003493 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003494 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02003495 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003496 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003497
Gleb Natapov9de41572010-04-28 19:15:22 +03003498 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04003499
Gleb Natapov11616242010-02-11 14:43:14 +02003500 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003501 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02003502 goto done;
3503 }
3504
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003505 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02003506 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003507 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02003508 goto done;
3509 }
3510
Avi Kivity081bca02010-08-26 11:06:15 +03003511 if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003512 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03003513 goto done;
3514 }
3515
Avi Kivity12537912011-03-29 11:41:27 +02003516 if ((c->d & Sse)
Avi Kivity717746e2011-04-20 13:37:53 +03003517 && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
3518 || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02003519 rc = emulate_ud(ctxt);
3520 goto done;
3521 }
3522
Avi Kivity717746e2011-04-20 13:37:53 +03003523 if ((c->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02003524 rc = emulate_nm(ctxt);
3525 goto done;
3526 }
3527
Avi Kivityc4f035c2011-04-04 12:39:22 +02003528 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003529 rc = emulator_check_intercept(ctxt, c->intercept,
3530 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003531 if (rc != X86EMUL_CONTINUE)
3532 goto done;
3533 }
3534
Gleb Natapove92805a2010-02-10 14:21:35 +02003535 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity717746e2011-04-20 13:37:53 +03003536 if ((c->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003537 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02003538 goto done;
3539 }
3540
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02003541 /* Instruction can only be executed in protected mode */
3542 if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
3543 rc = emulate_ud(ctxt);
3544 goto done;
3545 }
3546
Joerg Roedeld09beab2011-04-04 12:39:25 +02003547 /* Do instruction specific permission checks */
3548 if (c->check_perm) {
3549 rc = c->check_perm(ctxt);
3550 if (rc != X86EMUL_CONTINUE)
3551 goto done;
3552 }
3553
Avi Kivityc4f035c2011-04-04 12:39:22 +02003554 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003555 rc = emulator_check_intercept(ctxt, c->intercept,
3556 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003557 if (rc != X86EMUL_CONTINUE)
3558 goto done;
3559 }
3560
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003561 if (c->rep_prefix && (c->d & String)) {
3562 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02003563 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov95c55882010-04-28 19:15:39 +03003564 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003565 goto done;
3566 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02003567 }
3568
Wei Yongjunc483c022010-08-06 15:36:36 +08003569 if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003570 rc = segmented_read(ctxt, c->src.addr.mem,
3571 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09003572 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003573 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03003574 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003575 }
3576
Gleb Natapove35b7b92010-02-25 16:36:42 +02003577 if (c->src2.type == OP_MEM) {
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003578 rc = segmented_read(ctxt, c->src2.addr.mem,
3579 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02003580 if (rc != X86EMUL_CONTINUE)
3581 goto done;
3582 }
3583
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003584 if ((c->d & DstMask) == ImplicitOps)
3585 goto special_insn;
3586
3587
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003588 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
3589 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity3ca3ac42011-03-31 16:52:26 +02003590 rc = segmented_read(ctxt, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03003591 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003592 if (rc != X86EMUL_CONTINUE)
3593 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08003594 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02003595 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08003596
Avi Kivity018a98d2007-11-27 19:30:56 +02003597special_insn:
3598
Avi Kivityc4f035c2011-04-04 12:39:22 +02003599 if (unlikely(ctxt->guest_mode) && c->intercept) {
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003600 rc = emulator_check_intercept(ctxt, c->intercept,
3601 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02003602 if (rc != X86EMUL_CONTINUE)
3603 goto done;
3604 }
3605
Avi Kivityef65c882010-07-29 15:11:51 +03003606 if (c->execute) {
3607 rc = c->execute(ctxt);
3608 if (rc != X86EMUL_CONTINUE)
3609 goto done;
3610 goto writeback;
3611 }
3612
Laurent Viviere4e03de2007-09-18 11:52:50 +02003613 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614 goto twobyte_insn;
3615
Laurent Viviere4e03de2007-09-18 11:52:50 +02003616 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617 case 0x00 ... 0x05:
3618 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003619 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003621 case 0x06: /* push es */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003622 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003623 break;
3624 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003625 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003626 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003627 case 0x08 ... 0x0d:
3628 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003629 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003631 case 0x0e: /* push cs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003632 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003633 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634 case 0x10 ... 0x15:
3635 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003636 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003637 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003638 case 0x16: /* push ss */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003639 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003640 break;
3641 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003642 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003643 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003644 case 0x18 ... 0x1d:
3645 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003646 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003647 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003648 case 0x1e: /* push ds */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09003649 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003650 break;
3651 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003652 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003653 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02003654 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003655 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003656 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657 break;
3658 case 0x28 ... 0x2d:
3659 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003660 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003661 break;
3662 case 0x30 ... 0x35:
3663 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003664 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003665 break;
3666 case 0x38 ... 0x3d:
3667 cmp: /* cmp */
Takuya Yoshikawa575e7c12011-04-13 00:24:55 +09003668 c->dst.type = OP_NONE; /* Disable writeback. */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003669 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003671 case 0x40 ... 0x47: /* inc r16/r32 */
3672 emulate_1op("inc", c->dst, ctxt->eflags);
3673 break;
3674 case 0x48 ... 0x4f: /* dec r16/r32 */
3675 emulate_1op("dec", c->dst, ctxt->eflags);
3676 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02003677 case 0x58 ... 0x5f: /* pop reg */
3678 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02003679 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Avi Kivity33615aa2007-10-31 11:15:56 +02003680 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003681 case 0x60: /* pusha */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003682 rc = emulate_pusha(ctxt);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003683 break;
3684 case 0x61: /* popa */
3685 rc = emulate_popa(ctxt, ops);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02003686 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02003688 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003689 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003690 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003691 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003692 case 0x6c: /* insb */
3693 case 0x6d: /* insw/insd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003694 c->src.val = c->regs[VCPU_REGS_RDX];
3695 goto do_io_in;
Avi Kivity018a98d2007-11-27 19:30:56 +02003696 case 0x6e: /* outsb */
3697 case 0x6f: /* outsw/outsd */
Wei Yongjuna13a63f2010-08-06 11:46:12 +08003698 c->dst.val = c->regs[VCPU_REGS_RDX];
3699 goto do_io_out;
Gleb Natapov79729952010-03-18 15:20:24 +02003700 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003701 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003702 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003703 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003704 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003706 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707 case 0:
3708 goto add;
3709 case 1:
3710 goto or;
3711 case 2:
3712 goto adc;
3713 case 3:
3714 goto sbb;
3715 case 4:
3716 goto and;
3717 case 5:
3718 goto sub;
3719 case 6:
3720 goto xor;
3721 case 7:
3722 goto cmp;
3723 }
3724 break;
3725 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003726 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02003727 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728 break;
3729 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003730 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 /* Write back the register source. */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003732 c->src.val = c->dst.val;
3733 write_register_operand(&c->src);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 /*
3735 * Write back the memory destination with implicit LOCK
3736 * prefix.
3737 */
Wei Yongjun31be40b2010-08-17 09:17:30 +08003738 c->dst.val = c->src.orig_val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003739 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003740 break;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003741 case 0x8c: /* mov r/m, sreg */
3742 if (c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003743 rc = emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02003744 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003745 }
Avi Kivity4bff1e862011-04-20 13:37:53 +03003746 c->dst.val = ops->get_segment_selector(ctxt, c->modrm_reg);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003747 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003748 case 0x8d: /* lea r16/r32, m */
Avi Kivity90de84f2010-11-17 15:28:21 +02003749 c->dst.val = c->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003750 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003751 case 0x8e: { /* mov seg, r/m16 */
3752 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003753
3754 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003755
Gleb Natapovc6975182010-02-18 12:15:01 +02003756 if (c->modrm_reg == VCPU_SREG_CS ||
3757 c->modrm_reg > VCPU_SREG_GS) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003758 rc = emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003759 goto done;
3760 }
3761
Glauber Costa310b5d32009-05-12 16:21:06 -04003762 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003763 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003764
Gleb Natapov2e873022010-03-18 15:20:18 +02003765 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003766
3767 c->dst.type = OP_NONE; /* Disable writeback. */
3768 break;
3769 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003770 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003771 rc = emulate_grp1a(ctxt, ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003773 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3774 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003775 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003776 goto xchg;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08003777 case 0x98: /* cbw/cwde/cdqe */
3778 switch (c->op_bytes) {
3779 case 2: c->dst.val = (s8)c->dst.val; break;
3780 case 4: c->dst.val = (s16)c->dst.val; break;
3781 case 8: c->dst.val = (s32)c->dst.val; break;
3782 }
3783 break;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003784 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003785 c->src.val = (unsigned long) ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003786 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003787 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003788 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003789 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003790 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003791 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003792 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003793 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794 case 0xa6 ... 0xa7: /* cmps */
Gleb Natapova682e352010-03-18 15:20:21 +02003795 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003796 case 0xa8 ... 0xa9: /* test ax, imm */
3797 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798 case 0xae ... 0xaf: /* scas */
Avi Kivityf6b33fc2010-08-17 11:20:37 +03003799 goto cmp;
Avi Kivity018a98d2007-11-27 19:30:56 +02003800 case 0xc0 ... 0xc1:
3801 emulate_grp2(ctxt);
3802 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003803 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003804 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003805 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003806 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003807 goto pop_instruction;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003808 case 0xc4: /* les */
3809 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_ES);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003810 break;
3811 case 0xc5: /* lds */
3812 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_DS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08003813 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003814 case 0xcb: /* ret far */
3815 rc = emulate_ret_far(ctxt, ops);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003816 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003817 case 0xcc: /* int3 */
3818 irq = 3;
3819 goto do_interrupt;
3820 case 0xcd: /* int n */
3821 irq = c->src.val;
3822 do_interrupt:
3823 rc = emulate_int(ctxt, ops, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003824 break;
3825 case 0xce: /* into */
3826 if (ctxt->eflags & EFLG_OF) {
3827 irq = 4;
3828 goto do_interrupt;
3829 }
3830 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003831 case 0xcf: /* iret */
3832 rc = emulate_iret(ctxt, ops);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003833 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003834 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003835 emulate_grp2(ctxt);
3836 break;
3837 case 0xd2 ... 0xd3: /* Grp2 */
3838 c->src.val = c->regs[VCPU_REGS_RCX];
3839 emulate_grp2(ctxt);
3840 break;
Wei Yongjunf2f31842010-08-18 16:38:21 +08003841 case 0xe0 ... 0xe2: /* loop/loopz/loopnz */
3842 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
3843 if (address_mask(c, c->regs[VCPU_REGS_RCX]) != 0 &&
3844 (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
3845 jmp_rel(c, c->src.val);
3846 break;
Wei Yongjune4abac62010-08-19 14:25:48 +08003847 case 0xe3: /* jcxz/jecxz/jrcxz */
3848 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
3849 jmp_rel(c, c->src.val);
3850 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003851 case 0xe4: /* inb */
3852 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003853 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003854 case 0xe6: /* outb */
3855 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003856 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003857 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003858 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003859 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003860 jmp_rel(c, rel);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003861 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003862 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003863 }
3864 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003865 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003866 case 0xea: { /* jmp far */
3867 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02003868 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003869 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3870
3871 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003872 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003873
Gleb Natapov414e6272010-04-28 19:15:26 +03003874 c->eip = 0;
3875 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003876 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003877 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003878 case 0xeb:
3879 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003880 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003881 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003882 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003883 case 0xec: /* in al,dx */
3884 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003885 c->src.val = c->regs[VCPU_REGS_RDX];
3886 do_io_in:
Gleb Natapov7b262e92010-03-18 15:20:27 +02003887 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3888 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003889 goto done; /* IO is needed */
3890 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003891 case 0xee: /* out dx,al */
3892 case 0xef: /* out dx,(e/r)ax */
Wei Yongjun41167be2010-08-06 11:45:12 +08003893 c->dst.val = c->regs[VCPU_REGS_RDX];
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003894 do_io_out:
Avi Kivityca1d4a92011-04-20 13:37:53 +03003895 ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
3896 &c->src.val, 1);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003897 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003898 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003899 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003900 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003901 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003902 case 0xf5: /* cmc */
3903 /* complement carry flag from eflags reg */
3904 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003905 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003906 case 0xf6 ... 0xf7: /* Grp3 */
Avi Kivity34d1f492010-08-26 11:59:01 +03003907 rc = emulate_grp3(ctxt, ops);
Avi Kivity018a98d2007-11-27 19:30:56 +02003908 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003909 case 0xf8: /* clc */
3910 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003911 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003912 case 0xf9: /* stc */
3913 ctxt->eflags |= EFLG_CF;
3914 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003915 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003916 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003917 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003918 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003919 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003920 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003921 break;
3922 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003923 if (emulator_bad_iopl(ctxt, ops)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02003924 rc = emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003925 goto done;
3926 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003927 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003928 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003929 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003930 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003931 case 0xfc: /* cld */
3932 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003933 break;
3934 case 0xfd: /* std */
3935 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003936 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003937 case 0xfe: /* Grp4 */
3938 grp45:
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003939 rc = emulate_grp45(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02003940 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003941 case 0xff: /* Grp5 */
3942 if (c->modrm_reg == 5)
3943 goto jump_far;
3944 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003945 default:
3946 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003948
Avi Kivity7d9ddae2010-08-30 17:12:28 +03003949 if (rc != X86EMUL_CONTINUE)
3950 goto done;
3951
Avi Kivity018a98d2007-11-27 19:30:56 +02003952writeback:
3953 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003954 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003955 goto done;
3956
Gleb Natapov5cd21912010-03-18 15:20:26 +02003957 /*
3958 * restore dst type in case the decoding will be reused
3959 * (happens for string instruction )
3960 */
3961 c->dst.type = saved_dst_type;
3962
Gleb Natapova682e352010-03-18 15:20:21 +02003963 if ((c->d & SrcMask) == SrcSI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003964 string_addr_inc(ctxt, seg_override(ctxt, ops, c),
Gleb Natapov79168fd2010-04-28 19:15:30 +03003965 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003966
3967 if ((c->d & DstMask) == DstDI)
Avi Kivity90de84f2010-11-17 15:28:21 +02003968 string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
Gleb Natapov79168fd2010-04-28 19:15:30 +03003969 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003970
Gleb Natapov5cd21912010-03-18 15:20:26 +02003971 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov6e2fb2c2010-08-25 12:47:41 +03003972 struct read_cache *r = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003973 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03003974
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003975 if (!string_insn_completed(ctxt)) {
3976 /*
3977 * Re-enter guest when pio read ahead buffer is empty
3978 * or, if it is not used, after each 1024 iteration.
3979 */
3980 if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
3981 (r->end == 0 || r->end != r->pos)) {
3982 /*
3983 * Reset read cache. Usually happens before
3984 * decode, but since instruction is restarted
3985 * we have to do it here.
3986 */
3987 ctxt->decode.mem_read.end = 0;
3988 return EMULATION_RESTART;
3989 }
3990 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03003991 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02003992 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03003993
3994 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003995
3996done:
Avi Kivityda9cb572010-11-22 17:53:21 +02003997 if (rc == X86EMUL_PROPAGATE_FAULT)
3998 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02003999 if (rc == X86EMUL_INTERCEPTED)
4000 return EMULATION_INTERCEPTED;
4001
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004002 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004003
4004twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02004005 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004007 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008 u16 size;
4009 unsigned long address;
4010
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004011 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004012 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004013 goto cannot_emulate;
4014
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004015 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004016 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004017 goto done;
4018
Avi Kivity33e38852008-05-21 15:34:25 +03004019 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02004020 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03004021 /* Disable writeback. */
4022 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004023 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024 case 2: /* lgdt */
Avi Kivity1a6440a2010-08-01 12:35:10 +03004025 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02004026 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004027 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004028 goto done;
4029 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03004030 /* Disable writeback. */
4031 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004032 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004033 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02004034 if (c->modrm_mod == 3) {
4035 switch (c->modrm_rm) {
4036 case 1:
4037 rc = kvm_fix_hypercall(ctxt->vcpu);
Avi Kivity2b3d2a22008-12-23 19:46:01 +02004038 break;
4039 default:
4040 goto cannot_emulate;
4041 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004042 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +03004043 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004044 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02004045 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004046 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05004047 goto done;
4048 realmode_lidt(ctxt->vcpu, size, address);
4049 }
Avi Kivity16286d02008-04-14 14:40:50 +03004050 /* Disable writeback. */
4051 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052 break;
4053 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03004054 c->dst.bytes = 2;
Avi Kivity717746e2011-04-20 13:37:53 +03004055 c->dst.val = ops->get_cr(ctxt, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056 break;
4057 case 6: /* lmsw */
Avi Kivity717746e2011-04-20 13:37:53 +03004058 ops->set_cr(ctxt, 0, (ops->get_cr(ctxt, 0) & ~0x0eul) |
4059 (c->src.val & 0x0f));
Avi Kivitydc7457e2008-04-30 16:13:36 +03004060 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02004062 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03004063 emulate_ud(ctxt);
Avi Kivityda9cb572010-11-22 17:53:21 +02004064 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02004065 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066 case 7: /* invlpg*/
Avi Kivity38503912011-03-31 18:48:09 +02004067 rc = em_invlpg(ctxt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 break;
4069 default:
4070 goto cannot_emulate;
4071 }
4072 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02004073 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004074 rc = emulate_syscall(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004075 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004076 case 0x06:
4077 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02004078 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004079 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004080 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004081 break;
4082 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004083 case 0x0d: /* GrpP (prefetch) */
4084 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004085 break;
4086 case 0x20: /* mov cr, reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004087 c->dst.val = ops->get_cr(ctxt, c->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004088 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089 case 0x21: /* mov from dr to reg */
Avi Kivity717746e2011-04-20 13:37:53 +03004090 ops->get_dr(ctxt, c->modrm_reg, &c->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004092 case 0x22: /* mov reg, cr */
Avi Kivity717746e2011-04-20 13:37:53 +03004093 if (ops->set_cr(ctxt, c->modrm_reg, c->src.val)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004094 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004095 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov0f122442010-04-28 19:15:31 +03004096 goto done;
4097 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004098 c->dst.type = OP_NONE;
4099 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100 case 0x23: /* mov from reg to dr */
Avi Kivity717746e2011-04-20 13:37:53 +03004101 if (ops->set_dr(ctxt, c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03004102 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
Avi Kivity717746e2011-04-20 13:37:53 +03004103 ~0ULL : ~0U)) < 0) {
Gleb Natapov338dbc92010-04-28 19:15:32 +03004104 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03004105 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004106 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapov338dbc92010-04-28 19:15:32 +03004107 goto done;
4108 }
4109
Laurent Viviera01af5e2007-09-24 11:10:56 +02004110 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004112 case 0x30:
4113 /* wrmsr */
4114 msr_data = (u32)c->regs[VCPU_REGS_RAX]
4115 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Avi Kivity717746e2011-04-20 13:37:53 +03004116 if (ops->set_msr(ctxt, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004117 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004118 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004119 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004120 }
4121 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004122 break;
4123 case 0x32:
4124 /* rdmsr */
Avi Kivity717746e2011-04-20 13:37:53 +03004125 if (ops->get_msr(ctxt, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03004126 emulate_gp(ctxt, 0);
Avi Kivityda9cb572010-11-22 17:53:21 +02004127 rc = X86EMUL_PROPAGATE_FAULT;
Gleb Natapovfd525362010-03-18 15:20:13 +02004128 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02004129 } else {
4130 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
4131 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
4132 }
4133 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02004134 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02004135 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004136 rc = emulate_sysenter(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004137 break;
4138 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03004139 rc = emulate_sysexit(ctxt, ops);
Andre Przywarae99f0502009-06-17 15:50:33 +02004140 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004142 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02004143 if (!test_cc(c->b, ctxt->eflags))
4144 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004146 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02004147 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03004148 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004149 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004150 case 0x90 ... 0x9f: /* setcc r/m8 */
4151 c->dst.val = test_cc(c->b, ctxt->eflags);
4152 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004153 case 0xa0: /* push fs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09004154 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004155 break;
4156 case 0xa1: /* pop fs */
4157 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004158 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004159 case 0xa3:
4160 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08004161 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02004162 /* only subword offset */
4163 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02004164 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004165 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004166 case 0xa4: /* shld imm8, r, r/m */
4167 case 0xa5: /* shld cl, r, r/m */
4168 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
4169 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004170 case 0xa8: /* push gs */
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09004171 rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004172 break;
4173 case 0xa9: /* pop gs */
4174 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03004175 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004176 case 0xab:
4177 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004178 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004179 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004180 case 0xac: /* shrd imm8, r, r/m */
4181 case 0xad: /* shrd cl, r, r/m */
4182 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
4183 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004184 case 0xae: /* clflush */
4185 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 case 0xb0 ... 0xb1: /* cmpxchg */
4187 /*
4188 * Save real source value, then compare EAX against
4189 * destination.
4190 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004191 c->src.orig_val = c->src.val;
4192 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02004193 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
4194 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004196 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197 } else {
4198 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004199 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03004200 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201 }
4202 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004203 case 0xb2: /* lss */
4204 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_SS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004205 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004206 case 0xb3:
4207 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004208 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209 break;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004210 case 0xb4: /* lfs */
4211 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_FS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004212 break;
4213 case 0xb5: /* lgs */
4214 rc = emulate_load_segment(ctxt, ops, VCPU_SREG_GS);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08004215 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004217 c->dst.bytes = c->op_bytes;
4218 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
4219 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004222 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004223 case 0:
4224 goto bt;
4225 case 1:
4226 goto bts;
4227 case 2:
4228 goto btr;
4229 case 3:
4230 goto btc;
4231 }
4232 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03004233 case 0xbb:
4234 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02004235 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03004236 break;
Wei Yongjund9574a22010-08-10 13:48:22 +08004237 case 0xbc: { /* bsf */
4238 u8 zf;
4239 __asm__ ("bsf %2, %0; setz %1"
4240 : "=r"(c->dst.val), "=q"(zf)
4241 : "r"(c->src.val));
4242 ctxt->eflags &= ~X86_EFLAGS_ZF;
4243 if (zf) {
4244 ctxt->eflags |= X86_EFLAGS_ZF;
4245 c->dst.type = OP_NONE; /* Disable writeback. */
4246 }
4247 break;
4248 }
4249 case 0xbd: { /* bsr */
4250 u8 zf;
4251 __asm__ ("bsr %2, %0; setz %1"
4252 : "=r"(c->dst.val), "=q"(zf)
4253 : "r"(c->src.val));
4254 ctxt->eflags &= ~X86_EFLAGS_ZF;
4255 if (zf) {
4256 ctxt->eflags |= X86_EFLAGS_ZF;
4257 c->dst.type = OP_NONE; /* Disable writeback. */
4258 }
4259 break;
4260 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004262 c->dst.bytes = c->op_bytes;
4263 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
4264 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004265 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004266 case 0xc0 ... 0xc1: /* xadd */
4267 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
4268 /* Write back the register source. */
4269 c->src.val = c->dst.orig_val;
4270 write_register_operand(&c->src);
4271 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004272 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02004273 c->dst.bytes = c->op_bytes;
4274 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
4275 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004276 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004278 rc = emulate_grp9(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02004279 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004280 default:
4281 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004282 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004283
4284 if (rc != X86EMUL_CONTINUE)
4285 goto done;
4286
Avi Kivity6aa8b732006-12-10 02:21:36 -08004287 goto writeback;
4288
4289cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004290 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004291}