blob: 8617c344405d188f7d8774d1edab1fae66f84566 [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
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b122010-07-29 15:11:49 +030049#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivityab85b122010-07-29 15:11:49 +030051#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
54#define DstAcc (4<<1) /* Destination Accumulator */
55#define DstDI (5<<1) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<1) /* 64bit memory operand */
57#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080058/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020059#define SrcNone (0<<4) /* No source operand. */
60#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
61#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Gleb Natapov341de7e2009-04-12 13:36:41 +030074#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030076#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030078#define Mov (1<<9)
79#define BitOp (1<<10)
80#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020081#define String (1<<12) /* String instruction (rep capable) */
82#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020083#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
84#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030085/* Misc flags */
Avi Kivity5a506b12010-08-01 15:10:29 +030086#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030087#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030088#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020089#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020090#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030091#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010092/* Source 2 operand type */
93#define Src2None (0<<29)
94#define Src2CL (1<<29)
95#define Src2ImmByte (2<<29)
96#define Src2One (3<<29)
97#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098
Avi Kivityd0e53322010-07-29 15:11:54 +030099#define X2(x...) x, x
100#define X3(x...) X2(x), x
101#define X4(x...) X2(x), X2(x)
102#define X5(x...) X4(x), x
103#define X6(x...) X4(x), X2(x)
104#define X7(x...) X4(x), X3(x)
105#define X8(x...) X4(x), X4(x)
106#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300107
Avi Kivityd65b1de2010-07-29 15:11:35 +0300108struct opcode {
109 u32 flags;
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;
114 } u;
115};
116
117struct group_dual {
118 struct opcode mod012[8];
119 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300120};
121
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200123#define EFLG_ID (1<<21)
124#define EFLG_VIP (1<<20)
125#define EFLG_VIF (1<<19)
126#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200127#define EFLG_VM (1<<17)
128#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200129#define EFLG_IOPL (3<<12)
130#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131#define EFLG_OF (1<<11)
132#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200133#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200134#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135#define EFLG_SF (1<<7)
136#define EFLG_ZF (1<<6)
137#define EFLG_AF (1<<4)
138#define EFLG_PF (1<<2)
139#define EFLG_CF (1<<0)
140
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300141#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
142#define EFLG_RESERVED_ONE_MASK 2
143
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144/*
145 * Instruction emulation:
146 * Most instructions are emulated directly via a fragment of inline assembly
147 * code. This allows us to save/restore EFLAGS and thus very easily pick up
148 * any modified flags.
149 */
150
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800151#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152#define _LO32 "k" /* force 32-bit operand */
153#define _STK "%%rsp" /* stack pointer */
154#elif defined(__i386__)
155#define _LO32 "" /* force 32-bit operand */
156#define _STK "%%esp" /* stack pointer */
157#endif
158
159/*
160 * These EFLAGS bits are restored from saved value during emulation, and
161 * any changes are written back to the saved value after emulation.
162 */
163#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
164
165/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200166#define _PRE_EFLAGS(_sav, _msk, _tmp) \
167 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
168 "movl %"_sav",%"_LO32 _tmp"; " \
169 "push %"_tmp"; " \
170 "push %"_tmp"; " \
171 "movl %"_msk",%"_LO32 _tmp"; " \
172 "andl %"_LO32 _tmp",("_STK"); " \
173 "pushf; " \
174 "notl %"_LO32 _tmp"; " \
175 "andl %"_LO32 _tmp",("_STK"); " \
176 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
177 "pop %"_tmp"; " \
178 "orl %"_LO32 _tmp",("_STK"); " \
179 "popf; " \
180 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
182/* After executing instruction: write-back necessary bits in EFLAGS. */
183#define _POST_EFLAGS(_sav, _msk, _tmp) \
184 /* _sav |= EFLAGS & _msk; */ \
185 "pushf; " \
186 "pop %"_tmp"; " \
187 "andl %"_msk",%"_LO32 _tmp"; " \
188 "orl %"_LO32 _tmp",%"_sav"; "
189
Avi Kivitydda96d82008-11-26 15:14:10 +0200190#ifdef CONFIG_X86_64
191#define ON64(x) x
192#else
193#define ON64(x)
194#endif
195
Avi Kivity6b7ad612008-11-26 15:30:45 +0200196#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
197 do { \
198 __asm__ __volatile__ ( \
199 _PRE_EFLAGS("0", "4", "2") \
200 _op _suffix " %"_x"3,%1; " \
201 _POST_EFLAGS("0", "4", "2") \
202 : "=m" (_eflags), "=m" ((_dst).val), \
203 "=&r" (_tmp) \
204 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200205 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200206
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208/* Raw emulation: instruction has two explicit operands. */
209#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200210 do { \
211 unsigned long _tmp; \
212 \
213 switch ((_dst).bytes) { \
214 case 2: \
215 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
216 break; \
217 case 4: \
218 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
219 break; \
220 case 8: \
221 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
222 break; \
223 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 } while (0)
225
226#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
227 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200228 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400229 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200231 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 break; \
233 default: \
234 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
235 _wx, _wy, _lx, _ly, _qx, _qy); \
236 break; \
237 } \
238 } while (0)
239
240/* Source operand is byte-sized and may be restricted to just %cl. */
241#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
242 __emulate_2op(_op, _src, _dst, _eflags, \
243 "b", "c", "b", "c", "b", "c", "b", "c")
244
245/* Source operand is byte, word, long or quad sized. */
246#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
247 __emulate_2op(_op, _src, _dst, _eflags, \
248 "b", "q", "w", "r", _LO32, "r", "", "r")
249
250/* Source operand is word, long or quad sized. */
251#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
252 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
253 "w", "r", _LO32, "r", "", "r")
254
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100255/* Instruction has three operands and one operand is stored in ECX register */
256#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
257 do { \
258 unsigned long _tmp; \
259 _type _clv = (_cl).val; \
260 _type _srcv = (_src).val; \
261 _type _dstv = (_dst).val; \
262 \
263 __asm__ __volatile__ ( \
264 _PRE_EFLAGS("0", "5", "2") \
265 _op _suffix " %4,%1 \n" \
266 _POST_EFLAGS("0", "5", "2") \
267 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
268 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
269 ); \
270 \
271 (_cl).val = (unsigned long) _clv; \
272 (_src).val = (unsigned long) _srcv; \
273 (_dst).val = (unsigned long) _dstv; \
274 } while (0)
275
276#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
277 do { \
278 switch ((_dst).bytes) { \
279 case 2: \
280 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
281 "w", unsigned short); \
282 break; \
283 case 4: \
284 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
285 "l", unsigned int); \
286 break; \
287 case 8: \
288 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
289 "q", unsigned long)); \
290 break; \
291 } \
292 } while (0)
293
Avi Kivitydda96d82008-11-26 15:14:10 +0200294#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 do { \
296 unsigned long _tmp; \
297 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200298 __asm__ __volatile__ ( \
299 _PRE_EFLAGS("0", "3", "2") \
300 _op _suffix " %1; " \
301 _POST_EFLAGS("0", "3", "2") \
302 : "=m" (_eflags), "+m" ((_dst).val), \
303 "=&r" (_tmp) \
304 : "i" (EFLAGS_MASK)); \
305 } while (0)
306
307/* Instruction has only one explicit operand (no source operand). */
308#define emulate_1op(_op, _dst, _eflags) \
309 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400310 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200311 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
312 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
313 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
314 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 } \
316 } while (0)
317
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300318#define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
319 do { \
320 unsigned long _tmp; \
321 \
322 __asm__ __volatile__ ( \
323 _PRE_EFLAGS("0", "4", "1") \
324 _op _suffix " %5; " \
325 _POST_EFLAGS("0", "4", "1") \
326 : "=m" (_eflags), "=&r" (_tmp), \
327 "+a" (_rax), "+d" (_rdx) \
328 : "i" (EFLAGS_MASK), "m" ((_src).val), \
329 "a" (_rax), "d" (_rdx)); \
330 } while (0)
331
332/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
333#define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
334 do { \
335 switch((_src).bytes) { \
336 case 1: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "b"); break; \
337 case 2: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "w"); break; \
338 case 4: __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "l"); break; \
339 case 8: ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, "q")); break; \
340 } \
341 } while (0)
342
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343/* Fetch next part of the instruction being emulated. */
344#define insn_fetch(_type, _size, _eip) \
345({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200346 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200347 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348 goto done; \
349 (_eip) += (_size); \
350 (_type)_x; \
351})
352
Gleb Natapov414e6272010-04-28 19:15:26 +0300353#define insn_fetch_arr(_arr, _size, _eip) \
354({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
355 if (rc != X86EMUL_CONTINUE) \
356 goto done; \
357 (_eip) += (_size); \
358})
359
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800360static inline unsigned long ad_mask(struct decode_cache *c)
361{
362 return (1UL << (c->ad_bytes << 3)) - 1;
363}
364
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800366static inline unsigned long
367address_mask(struct decode_cache *c, unsigned long reg)
368{
369 if (c->ad_bytes == sizeof(unsigned long))
370 return reg;
371 else
372 return reg & ad_mask(c);
373}
374
375static inline unsigned long
376register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
377{
378 return base + address_mask(c, reg);
379}
380
Harvey Harrison7a9572752008-02-19 07:40:41 -0800381static inline void
382register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
383{
384 if (c->ad_bytes == sizeof(unsigned long))
385 *reg += inc;
386 else
387 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
388}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389
Harvey Harrison7a9572752008-02-19 07:40:41 -0800390static inline void jmp_rel(struct decode_cache *c, int rel)
391{
392 register_address_increment(c, &c->eip, rel);
393}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300394
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300395static void set_seg_override(struct decode_cache *c, int seg)
396{
397 c->has_seg_override = true;
398 c->seg_override = seg;
399}
400
Gleb Natapov79168fd2010-04-28 19:15:30 +0300401static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
402 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300403{
404 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
405 return 0;
406
Gleb Natapov79168fd2010-04-28 19:15:30 +0300407 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300408}
409
410static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300411 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300412 struct decode_cache *c)
413{
414 if (!c->has_seg_override)
415 return 0;
416
Gleb Natapov79168fd2010-04-28 19:15:30 +0300417 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300418}
419
Gleb Natapov79168fd2010-04-28 19:15:30 +0300420static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
421 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300422{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300423 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300424}
425
Gleb Natapov79168fd2010-04-28 19:15:30 +0300426static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
427 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300428{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300429 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300430}
431
Gleb Natapov54b84862010-04-28 19:15:44 +0300432static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
433 u32 error, bool valid)
434{
435 ctxt->exception = vec;
436 ctxt->error_code = error;
437 ctxt->error_code_valid = valid;
438 ctxt->restart = false;
439}
440
441static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
442{
443 emulate_exception(ctxt, GP_VECTOR, err, true);
444}
445
446static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
447 int err)
448{
449 ctxt->cr2 = addr;
450 emulate_exception(ctxt, PF_VECTOR, err, true);
451}
452
453static void emulate_ud(struct x86_emulate_ctxt *ctxt)
454{
455 emulate_exception(ctxt, UD_VECTOR, 0, false);
456}
457
458static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
459{
460 emulate_exception(ctxt, TS_VECTOR, err, true);
461}
462
Avi Kivity62266862007-11-20 13:15:52 +0200463static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
464 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300465 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200466{
467 struct fetch_cache *fc = &ctxt->decode.fetch;
468 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300469 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200470
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300471 if (eip == fc->end) {
472 cur_size = fc->end - fc->start;
473 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
474 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
475 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900476 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200477 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300478 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200479 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300480 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900481 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200482}
483
484static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
485 struct x86_emulate_ops *ops,
486 unsigned long eip, void *dest, unsigned size)
487{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900488 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200489
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200490 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200491 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200492 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200493 while (size--) {
494 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900495 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200496 return rc;
497 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900498 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200499}
500
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000501/*
502 * Given the 'reg' portion of a ModRM byte, and a register block, return a
503 * pointer into the block that addresses the relevant register.
504 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
505 */
506static void *decode_register(u8 modrm_reg, unsigned long *regs,
507 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508{
509 void *p;
510
511 p = &regs[modrm_reg];
512 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
513 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
514 return p;
515}
516
517static int read_descriptor(struct x86_emulate_ctxt *ctxt,
518 struct x86_emulate_ops *ops,
Avi Kivity1a6440a2010-08-01 12:35:10 +0300519 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 u16 *size, unsigned long *address, int op_bytes)
521{
522 int rc;
523
524 if (op_bytes == 2)
525 op_bytes = 3;
526 *address = 0;
Avi Kivity1a6440a2010-08-01 12:35:10 +0300527 rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900528 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529 return rc;
Avi Kivity1a6440a2010-08-01 12:35:10 +0300530 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531 return rc;
532}
533
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300534static int test_cc(unsigned int condition, unsigned int flags)
535{
536 int rc = 0;
537
538 switch ((condition & 15) >> 1) {
539 case 0: /* o */
540 rc |= (flags & EFLG_OF);
541 break;
542 case 1: /* b/c/nae */
543 rc |= (flags & EFLG_CF);
544 break;
545 case 2: /* z/e */
546 rc |= (flags & EFLG_ZF);
547 break;
548 case 3: /* be/na */
549 rc |= (flags & (EFLG_CF|EFLG_ZF));
550 break;
551 case 4: /* s */
552 rc |= (flags & EFLG_SF);
553 break;
554 case 5: /* p/pe */
555 rc |= (flags & EFLG_PF);
556 break;
557 case 7: /* le/ng */
558 rc |= (flags & EFLG_ZF);
559 /* fall through */
560 case 6: /* l/nge */
561 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
562 break;
563 }
564
565 /* Odd condition identifiers (lsb == 1) have inverted sense. */
566 return (!!rc ^ (condition & 1));
567}
568
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300569static void fetch_register_operand(struct operand *op)
570{
571 switch (op->bytes) {
572 case 1:
573 op->val = *(u8 *)op->addr.reg;
574 break;
575 case 2:
576 op->val = *(u16 *)op->addr.reg;
577 break;
578 case 4:
579 op->val = *(u32 *)op->addr.reg;
580 break;
581 case 8:
582 op->val = *(u64 *)op->addr.reg;
583 break;
584 }
585}
586
Avi Kivity3c118e22007-10-31 10:27:04 +0200587static void decode_register_operand(struct operand *op,
588 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200589 int inhibit_bytereg)
590{
Avi Kivity33615aa2007-10-31 11:15:56 +0200591 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200592 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200593
594 if (!(c->d & ModRM))
595 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200596 op->type = OP_REG;
597 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300598 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200599 op->bytes = 1;
600 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +0300601 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200602 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200603 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300604 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200605 op->orig_val = op->val;
606}
607
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200608static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300609 struct x86_emulate_ops *ops,
610 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200611{
612 struct decode_cache *c = &ctxt->decode;
613 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700614 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900615 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300616 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200617
618 if (c->rex_prefix) {
619 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
620 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
621 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
622 }
623
624 c->modrm = insn_fetch(u8, 1, c->eip);
625 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
626 c->modrm_reg |= (c->modrm & 0x38) >> 3;
627 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300628 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200629
630 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300631 op->type = OP_REG;
632 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
633 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300634 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300635 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200636 return rc;
637 }
638
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300639 op->type = OP_MEM;
640
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200641 if (c->ad_bytes == 2) {
642 unsigned bx = c->regs[VCPU_REGS_RBX];
643 unsigned bp = c->regs[VCPU_REGS_RBP];
644 unsigned si = c->regs[VCPU_REGS_RSI];
645 unsigned di = c->regs[VCPU_REGS_RDI];
646
647 /* 16-bit ModR/M decode. */
648 switch (c->modrm_mod) {
649 case 0:
650 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300651 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200652 break;
653 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300654 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200655 break;
656 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300657 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200658 break;
659 }
660 switch (c->modrm_rm) {
661 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300662 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200663 break;
664 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300665 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200666 break;
667 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300668 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200669 break;
670 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300671 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200672 break;
673 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300674 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200675 break;
676 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300677 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200678 break;
679 case 6:
680 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300681 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200682 break;
683 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300684 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200685 break;
686 }
687 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
688 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300689 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300690 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200691 } else {
692 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700693 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200694 sib = insn_fetch(u8, 1, c->eip);
695 index_reg |= (sib >> 3) & 7;
696 base_reg |= sib & 7;
697 scale = sib >> 6;
698
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700699 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300700 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700701 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300702 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700703 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300704 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700705 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
706 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700707 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700708 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300709 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200710 switch (c->modrm_mod) {
711 case 0:
712 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300713 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200714 break;
715 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300716 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200717 break;
718 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300719 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720 break;
721 }
722 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300723 op->addr.mem = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200724done:
725 return rc;
726}
727
728static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300729 struct x86_emulate_ops *ops,
730 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200731{
732 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900733 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200734
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300735 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200736 switch (c->ad_bytes) {
737 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300738 op->addr.mem = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200739 break;
740 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300741 op->addr.mem = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200742 break;
743 case 8:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300744 op->addr.mem = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200745 break;
746 }
747done:
748 return rc;
749}
750
Wei Yongjun35c843c2010-08-09 11:34:56 +0800751static void fetch_bit_operand(struct decode_cache *c)
752{
753 long sv, mask;
754
Wei Yongjun3885f182010-08-09 11:37:37 +0800755 if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
Wei Yongjun35c843c2010-08-09 11:34:56 +0800756 mask = ~(c->dst.bytes * 8 - 1);
757
758 if (c->src.bytes == 2)
759 sv = (s16)c->src.val & (s16)mask;
760 else if (c->src.bytes == 4)
761 sv = (s32)c->src.val & (s32)mask;
762
763 c->dst.addr.mem += (sv >> 3);
764 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +0800765
766 /* only subword offset */
767 c->src.val &= (c->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +0800768}
769
Gleb Natapov9de41572010-04-28 19:15:22 +0300770static int read_emulated(struct x86_emulate_ctxt *ctxt,
771 struct x86_emulate_ops *ops,
772 unsigned long addr, void *dest, unsigned size)
773{
774 int rc;
775 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300776 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300777
778 while (size) {
779 int n = min(size, 8u);
780 size -= n;
781 if (mc->pos < mc->end)
782 goto read_cached;
783
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300784 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
785 ctxt->vcpu);
786 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300787 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300788 if (rc != X86EMUL_CONTINUE)
789 return rc;
790 mc->end += n;
791
792 read_cached:
793 memcpy(dest, mc->data + mc->pos, n);
794 mc->pos += n;
795 dest += n;
796 addr += n;
797 }
798 return X86EMUL_CONTINUE;
799}
800
Gleb Natapov7b262e92010-03-18 15:20:27 +0200801static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
802 struct x86_emulate_ops *ops,
803 unsigned int size, unsigned short port,
804 void *dest)
805{
806 struct read_cache *rc = &ctxt->decode.io_read;
807
808 if (rc->pos == rc->end) { /* refill pio read ahead */
809 struct decode_cache *c = &ctxt->decode;
810 unsigned int in_page, n;
811 unsigned int count = c->rep_prefix ?
812 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
813 in_page = (ctxt->eflags & EFLG_DF) ?
814 offset_in_page(c->regs[VCPU_REGS_RDI]) :
815 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
816 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
817 count);
818 if (n == 0)
819 n = 1;
820 rc->pos = rc->end = 0;
821 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
822 return 0;
823 rc->end = n * size;
824 }
825
826 memcpy(dest, rc->data + rc->pos, size);
827 rc->pos += size;
828 return 1;
829}
830
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200831static u32 desc_limit_scaled(struct desc_struct *desc)
832{
833 u32 limit = get_desc_limit(desc);
834
835 return desc->g ? (limit << 12) | 0xfff : limit;
836}
837
838static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
839 struct x86_emulate_ops *ops,
840 u16 selector, struct desc_ptr *dt)
841{
842 if (selector & 1 << 2) {
843 struct desc_struct desc;
844 memset (dt, 0, sizeof *dt);
845 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
846 return;
847
848 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
849 dt->address = get_desc_base(&desc);
850 } else
851 ops->get_gdt(dt, ctxt->vcpu);
852}
853
854/* allowed just for 8 bytes segments */
855static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
856 struct x86_emulate_ops *ops,
857 u16 selector, struct desc_struct *desc)
858{
859 struct desc_ptr dt;
860 u16 index = selector >> 3;
861 int ret;
862 u32 err;
863 ulong addr;
864
865 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
866
867 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300868 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200869 return X86EMUL_PROPAGATE_FAULT;
870 }
871 addr = dt.address + index * 8;
872 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
873 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300874 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200875
876 return ret;
877}
878
879/* allowed just for 8 bytes segments */
880static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
881 struct x86_emulate_ops *ops,
882 u16 selector, struct desc_struct *desc)
883{
884 struct desc_ptr dt;
885 u16 index = selector >> 3;
886 u32 err;
887 ulong addr;
888 int ret;
889
890 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
891
892 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300893 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200894 return X86EMUL_PROPAGATE_FAULT;
895 }
896
897 addr = dt.address + index * 8;
898 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
899 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300900 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200901
902 return ret;
903}
904
905static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
906 struct x86_emulate_ops *ops,
907 u16 selector, int seg)
908{
909 struct desc_struct seg_desc;
910 u8 dpl, rpl, cpl;
911 unsigned err_vec = GP_VECTOR;
912 u32 err_code = 0;
913 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
914 int ret;
915
916 memset(&seg_desc, 0, sizeof seg_desc);
917
918 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
919 || ctxt->mode == X86EMUL_MODE_REAL) {
920 /* set real mode segment descriptor */
921 set_desc_base(&seg_desc, selector << 4);
922 set_desc_limit(&seg_desc, 0xffff);
923 seg_desc.type = 3;
924 seg_desc.p = 1;
925 seg_desc.s = 1;
926 goto load;
927 }
928
929 /* NULL selector is not valid for TR, CS and SS */
930 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
931 && null_selector)
932 goto exception;
933
934 /* TR should be in GDT only */
935 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
936 goto exception;
937
938 if (null_selector) /* for NULL selector skip all following checks */
939 goto load;
940
941 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
942 if (ret != X86EMUL_CONTINUE)
943 return ret;
944
945 err_code = selector & 0xfffc;
946 err_vec = GP_VECTOR;
947
948 /* can't load system descriptor into segment selecor */
949 if (seg <= VCPU_SREG_GS && !seg_desc.s)
950 goto exception;
951
952 if (!seg_desc.p) {
953 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
954 goto exception;
955 }
956
957 rpl = selector & 3;
958 dpl = seg_desc.dpl;
959 cpl = ops->cpl(ctxt->vcpu);
960
961 switch (seg) {
962 case VCPU_SREG_SS:
963 /*
964 * segment is not a writable data segment or segment
965 * selector's RPL != CPL or segment selector's RPL != CPL
966 */
967 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
968 goto exception;
969 break;
970 case VCPU_SREG_CS:
971 if (!(seg_desc.type & 8))
972 goto exception;
973
974 if (seg_desc.type & 4) {
975 /* conforming */
976 if (dpl > cpl)
977 goto exception;
978 } else {
979 /* nonconforming */
980 if (rpl > cpl || dpl != cpl)
981 goto exception;
982 }
983 /* CS(RPL) <- CPL */
984 selector = (selector & 0xfffc) | cpl;
985 break;
986 case VCPU_SREG_TR:
987 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
988 goto exception;
989 break;
990 case VCPU_SREG_LDTR:
991 if (seg_desc.s || seg_desc.type != 2)
992 goto exception;
993 break;
994 default: /* DS, ES, FS, or GS */
995 /*
996 * segment is not a data or readable code segment or
997 * ((segment is a data or nonconforming code segment)
998 * and (both RPL and CPL > DPL))
999 */
1000 if ((seg_desc.type & 0xa) == 0x8 ||
1001 (((seg_desc.type & 0xc) != 0xc) &&
1002 (rpl > dpl && cpl > dpl)))
1003 goto exception;
1004 break;
1005 }
1006
1007 if (seg_desc.s) {
1008 /* mark segment as accessed */
1009 seg_desc.type |= 1;
1010 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
1011 if (ret != X86EMUL_CONTINUE)
1012 return ret;
1013 }
1014load:
1015 ops->set_segment_selector(selector, seg, ctxt->vcpu);
1016 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
1017 return X86EMUL_CONTINUE;
1018exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001019 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001020 return X86EMUL_PROPAGATE_FAULT;
1021}
1022
Wei Yongjunc37eda12010-06-15 09:03:33 +08001023static inline int writeback(struct x86_emulate_ctxt *ctxt,
1024 struct x86_emulate_ops *ops)
1025{
1026 int rc;
1027 struct decode_cache *c = &ctxt->decode;
1028 u32 err;
1029
1030 switch (c->dst.type) {
1031 case OP_REG:
1032 /* The 4-byte case *is* correct:
1033 * in 64-bit mode we zero-extend.
1034 */
1035 switch (c->dst.bytes) {
1036 case 1:
Avi Kivity1a6440a2010-08-01 12:35:10 +03001037 *(u8 *)c->dst.addr.reg = (u8)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001038 break;
1039 case 2:
Avi Kivity1a6440a2010-08-01 12:35:10 +03001040 *(u16 *)c->dst.addr.reg = (u16)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001041 break;
1042 case 4:
Avi Kivity1a6440a2010-08-01 12:35:10 +03001043 *c->dst.addr.reg = (u32)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001044 break; /* 64b: zero-ext */
1045 case 8:
Avi Kivity1a6440a2010-08-01 12:35:10 +03001046 *c->dst.addr.reg = c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001047 break;
1048 }
1049 break;
1050 case OP_MEM:
1051 if (c->lock_prefix)
1052 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440a2010-08-01 12:35:10 +03001053 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001054 &c->dst.orig_val,
1055 &c->dst.val,
1056 c->dst.bytes,
1057 &err,
1058 ctxt->vcpu);
1059 else
1060 rc = ops->write_emulated(
Avi Kivity1a6440a2010-08-01 12:35:10 +03001061 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001062 &c->dst.val,
1063 c->dst.bytes,
1064 &err,
1065 ctxt->vcpu);
1066 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440a2010-08-01 12:35:10 +03001067 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001068 if (rc != X86EMUL_CONTINUE)
1069 return rc;
1070 break;
1071 case OP_NONE:
1072 /* no writeback */
1073 break;
1074 default:
1075 break;
1076 }
1077 return X86EMUL_CONTINUE;
1078}
1079
Gleb Natapov79168fd2010-04-28 19:15:30 +03001080static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1081 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001082{
1083 struct decode_cache *c = &ctxt->decode;
1084
1085 c->dst.type = OP_MEM;
1086 c->dst.bytes = c->op_bytes;
1087 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001088 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440a2010-08-01 12:35:10 +03001089 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1090 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001091}
1092
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001093static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001094 struct x86_emulate_ops *ops,
1095 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001096{
1097 struct decode_cache *c = &ctxt->decode;
1098 int rc;
1099
Gleb Natapov79168fd2010-04-28 19:15:30 +03001100 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001101 c->regs[VCPU_REGS_RSP]),
1102 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001103 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001104 return rc;
1105
Avi Kivity350f69d2009-01-05 11:12:40 +02001106 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001107 return rc;
1108}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001109
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001110static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1111 struct x86_emulate_ops *ops,
1112 void *dest, int len)
1113{
1114 int rc;
1115 unsigned long val, change_mask;
1116 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001117 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001118
1119 rc = emulate_pop(ctxt, ops, &val, len);
1120 if (rc != X86EMUL_CONTINUE)
1121 return rc;
1122
1123 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1124 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1125
1126 switch(ctxt->mode) {
1127 case X86EMUL_MODE_PROT64:
1128 case X86EMUL_MODE_PROT32:
1129 case X86EMUL_MODE_PROT16:
1130 if (cpl == 0)
1131 change_mask |= EFLG_IOPL;
1132 if (cpl <= iopl)
1133 change_mask |= EFLG_IF;
1134 break;
1135 case X86EMUL_MODE_VM86:
1136 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001137 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001138 return X86EMUL_PROPAGATE_FAULT;
1139 }
1140 change_mask |= EFLG_IF;
1141 break;
1142 default: /* real mode */
1143 change_mask |= (EFLG_IOPL | EFLG_IF);
1144 break;
1145 }
1146
1147 *(unsigned long *)dest =
1148 (ctxt->eflags & ~change_mask) | (val & change_mask);
1149
1150 return rc;
1151}
1152
Gleb Natapov79168fd2010-04-28 19:15:30 +03001153static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1154 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001155{
1156 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001157
Gleb Natapov79168fd2010-04-28 19:15:30 +03001158 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001159
Gleb Natapov79168fd2010-04-28 19:15:30 +03001160 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001161}
1162
1163static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1164 struct x86_emulate_ops *ops, int seg)
1165{
1166 struct decode_cache *c = &ctxt->decode;
1167 unsigned long selector;
1168 int rc;
1169
1170 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001171 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001172 return rc;
1173
Gleb Natapov2e873022010-03-18 15:20:18 +02001174 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001175 return rc;
1176}
1177
Wei Yongjunc37eda12010-06-15 09:03:33 +08001178static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001179 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001180{
1181 struct decode_cache *c = &ctxt->decode;
1182 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001183 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001184 int reg = VCPU_REGS_RAX;
1185
1186 while (reg <= VCPU_REGS_RDI) {
1187 (reg == VCPU_REGS_RSP) ?
1188 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1189
Gleb Natapov79168fd2010-04-28 19:15:30 +03001190 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001191
1192 rc = writeback(ctxt, ops);
1193 if (rc != X86EMUL_CONTINUE)
1194 return rc;
1195
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001196 ++reg;
1197 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001198
1199 /* Disable writeback. */
1200 c->dst.type = OP_NONE;
1201
1202 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001203}
1204
1205static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1206 struct x86_emulate_ops *ops)
1207{
1208 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001209 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001210 int reg = VCPU_REGS_RDI;
1211
1212 while (reg >= VCPU_REGS_RAX) {
1213 if (reg == VCPU_REGS_RSP) {
1214 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1215 c->op_bytes);
1216 --reg;
1217 }
1218
1219 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001220 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001221 break;
1222 --reg;
1223 }
1224 return rc;
1225}
1226
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001227int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1228 struct x86_emulate_ops *ops, int irq)
1229{
1230 struct decode_cache *c = &ctxt->decode;
1231 int rc = X86EMUL_CONTINUE;
1232 struct desc_ptr dt;
1233 gva_t cs_addr;
1234 gva_t eip_addr;
1235 u16 cs, eip;
1236 u32 err;
1237
1238 /* TODO: Add limit checks */
1239 c->src.val = ctxt->eflags;
1240 emulate_push(ctxt, ops);
1241
1242 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1243
1244 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1245 emulate_push(ctxt, ops);
1246
1247 c->src.val = c->eip;
1248 emulate_push(ctxt, ops);
1249
1250 ops->get_idt(&dt, ctxt->vcpu);
1251
1252 eip_addr = dt.address + (irq << 2);
1253 cs_addr = dt.address + (irq << 2) + 2;
1254
1255 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &err);
1256 if (rc != X86EMUL_CONTINUE)
1257 return rc;
1258
1259 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &err);
1260 if (rc != X86EMUL_CONTINUE)
1261 return rc;
1262
1263 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1264 if (rc != X86EMUL_CONTINUE)
1265 return rc;
1266
1267 c->eip = eip;
1268
1269 return rc;
1270}
1271
1272static int emulate_int(struct x86_emulate_ctxt *ctxt,
1273 struct x86_emulate_ops *ops, int irq)
1274{
1275 switch(ctxt->mode) {
1276 case X86EMUL_MODE_REAL:
1277 return emulate_int_real(ctxt, ops, irq);
1278 case X86EMUL_MODE_VM86:
1279 case X86EMUL_MODE_PROT16:
1280 case X86EMUL_MODE_PROT32:
1281 case X86EMUL_MODE_PROT64:
1282 default:
1283 /* Protected mode interrupts unimplemented yet */
1284 return X86EMUL_UNHANDLEABLE;
1285 }
1286}
1287
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001288static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1289 struct x86_emulate_ops *ops)
1290{
1291 struct decode_cache *c = &ctxt->decode;
1292 int rc = X86EMUL_CONTINUE;
1293 unsigned long temp_eip = 0;
1294 unsigned long temp_eflags = 0;
1295 unsigned long cs = 0;
1296 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1297 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1298 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1299 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1300
1301 /* TODO: Add stack limit check */
1302
1303 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1304
1305 if (rc != X86EMUL_CONTINUE)
1306 return rc;
1307
1308 if (temp_eip & ~0xffff) {
1309 emulate_gp(ctxt, 0);
1310 return X86EMUL_PROPAGATE_FAULT;
1311 }
1312
1313 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1314
1315 if (rc != X86EMUL_CONTINUE)
1316 return rc;
1317
1318 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1319
1320 if (rc != X86EMUL_CONTINUE)
1321 return rc;
1322
1323 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1324
1325 if (rc != X86EMUL_CONTINUE)
1326 return rc;
1327
1328 c->eip = temp_eip;
1329
1330
1331 if (c->op_bytes == 4)
1332 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1333 else if (c->op_bytes == 2) {
1334 ctxt->eflags &= ~0xffff;
1335 ctxt->eflags |= temp_eflags;
1336 }
1337
1338 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1339 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1340
1341 return rc;
1342}
1343
1344static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1345 struct x86_emulate_ops* ops)
1346{
1347 switch(ctxt->mode) {
1348 case X86EMUL_MODE_REAL:
1349 return emulate_iret_real(ctxt, ops);
1350 case X86EMUL_MODE_VM86:
1351 case X86EMUL_MODE_PROT16:
1352 case X86EMUL_MODE_PROT32:
1353 case X86EMUL_MODE_PROT64:
1354 default:
1355 /* iret from protected mode unimplemented yet */
1356 return X86EMUL_UNHANDLEABLE;
1357 }
1358}
1359
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001360static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1361 struct x86_emulate_ops *ops)
1362{
1363 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001364
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001365 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001366}
1367
Laurent Vivier05f086f2007-09-24 11:10:55 +02001368static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001369{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001370 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001371 switch (c->modrm_reg) {
1372 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001373 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001374 break;
1375 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001376 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001377 break;
1378 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001379 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001380 break;
1381 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001382 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001383 break;
1384 case 4: /* sal/shl */
1385 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001386 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001387 break;
1388 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001389 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001390 break;
1391 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001392 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001393 break;
1394 }
1395}
1396
1397static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001398 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001399{
1400 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001401 unsigned long *rax = &c->regs[VCPU_REGS_RAX];
1402 unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001403
1404 switch (c->modrm_reg) {
1405 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001406 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001407 break;
1408 case 2: /* not */
1409 c->dst.val = ~c->dst.val;
1410 break;
1411 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001412 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001413 break;
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03001414 case 4: /* mul */
1415 emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
1416 break;
1417 case 5: /* imul */
1418 emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
1419 break;
1420 case 6: /* div */
1421 emulate_1op_rax_rdx("div", c->src, *rax, *rdx, ctxt->eflags);
1422 break;
1423 case 7: /* idiv */
1424 emulate_1op_rax_rdx("idiv", c->src, *rax, *rdx, ctxt->eflags);
1425 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001426 default:
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001427 return X86EMUL_UNHANDLEABLE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001428 }
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03001429 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001430}
1431
1432static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001433 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001434{
1435 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001436
1437 switch (c->modrm_reg) {
1438 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001439 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001440 break;
1441 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001442 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001443 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001444 case 2: /* call near abs */ {
1445 long int old_eip;
1446 old_eip = c->eip;
1447 c->eip = c->src.val;
1448 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001449 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001450 break;
1451 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001452 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001453 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001454 break;
1455 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001456 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001457 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001458 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001459 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001460}
1461
1462static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001463 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001464{
1465 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001466 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001467
1468 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1469 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001470 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1471 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001472 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001473 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001474 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1475 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001476
Laurent Vivier05f086f2007-09-24 11:10:55 +02001477 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001478 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001479 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001480}
1481
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001482static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1483 struct x86_emulate_ops *ops)
1484{
1485 struct decode_cache *c = &ctxt->decode;
1486 int rc;
1487 unsigned long cs;
1488
1489 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001490 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001491 return rc;
1492 if (c->op_bytes == 4)
1493 c->eip = (u32)c->eip;
1494 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001495 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001496 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001497 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001498 return rc;
1499}
1500
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001501static inline void
1502setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001503 struct x86_emulate_ops *ops, struct desc_struct *cs,
1504 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001505{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001506 memset(cs, 0, sizeof(struct desc_struct));
1507 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1508 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001509
1510 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001511 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001512 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001513 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001514 cs->type = 0x0b; /* Read, Execute, Accessed */
1515 cs->s = 1;
1516 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001517 cs->p = 1;
1518 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001519
Gleb Natapov79168fd2010-04-28 19:15:30 +03001520 set_desc_base(ss, 0); /* flat segment */
1521 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001522 ss->g = 1; /* 4kb granularity */
1523 ss->s = 1;
1524 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001525 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001526 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001527 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001528}
1529
1530static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001531emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001532{
1533 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001534 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001535 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001536 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001537
1538 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001539 if (ctxt->mode == X86EMUL_MODE_REAL ||
1540 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001541 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001542 return X86EMUL_PROPAGATE_FAULT;
1543 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001544
Gleb Natapov79168fd2010-04-28 19:15:30 +03001545 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001546
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001547 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001548 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001549 cs_sel = (u16)(msr_data & 0xfffc);
1550 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001551
1552 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001553 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001554 cs.l = 1;
1555 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001556 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1557 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1558 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1559 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001560
1561 c->regs[VCPU_REGS_RCX] = c->eip;
1562 if (is_long_mode(ctxt->vcpu)) {
1563#ifdef CONFIG_X86_64
1564 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1565
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001566 ops->get_msr(ctxt->vcpu,
1567 ctxt->mode == X86EMUL_MODE_PROT64 ?
1568 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001569 c->eip = msr_data;
1570
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001571 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001572 ctxt->eflags &= ~(msr_data | EFLG_RF);
1573#endif
1574 } else {
1575 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001576 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001577 c->eip = (u32)msr_data;
1578
1579 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1580 }
1581
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001582 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001583}
1584
Andre Przywara8c604352009-06-18 12:56:01 +02001585static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001586emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001587{
1588 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001589 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001590 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001591 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001592
Gleb Natapova0044752010-02-10 14:21:31 +02001593 /* inject #GP if in real mode */
1594 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001595 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001596 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001597 }
1598
1599 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1600 * Therefore, we inject an #UD.
1601 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001602 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001603 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001604 return X86EMUL_PROPAGATE_FAULT;
1605 }
Andre Przywara8c604352009-06-18 12:56:01 +02001606
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001608
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001609 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001610 switch (ctxt->mode) {
1611 case X86EMUL_MODE_PROT32:
1612 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001613 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001614 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001615 }
1616 break;
1617 case X86EMUL_MODE_PROT64:
1618 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001619 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001620 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001621 }
1622 break;
1623 }
1624
1625 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001626 cs_sel = (u16)msr_data;
1627 cs_sel &= ~SELECTOR_RPL_MASK;
1628 ss_sel = cs_sel + 8;
1629 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001630 if (ctxt->mode == X86EMUL_MODE_PROT64
1631 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001632 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001633 cs.l = 1;
1634 }
1635
Gleb Natapov79168fd2010-04-28 19:15:30 +03001636 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1637 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1638 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1639 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001640
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001641 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001642 c->eip = msr_data;
1643
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001644 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001645 c->regs[VCPU_REGS_RSP] = msr_data;
1646
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001647 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001648}
1649
Andre Przywara4668f052009-06-18 12:56:02 +02001650static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001651emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001652{
1653 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001654 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001655 u64 msr_data;
1656 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001657 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001658
Gleb Natapova0044752010-02-10 14:21:31 +02001659 /* inject #GP if in real mode or Virtual 8086 mode */
1660 if (ctxt->mode == X86EMUL_MODE_REAL ||
1661 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001662 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001663 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001664 }
1665
Gleb Natapov79168fd2010-04-28 19:15:30 +03001666 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001667
1668 if ((c->rex_prefix & 0x8) != 0x0)
1669 usermode = X86EMUL_MODE_PROT64;
1670 else
1671 usermode = X86EMUL_MODE_PROT32;
1672
1673 cs.dpl = 3;
1674 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001675 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001676 switch (usermode) {
1677 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001678 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001679 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001680 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001681 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001682 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001683 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001684 break;
1685 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001686 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001687 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001688 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001689 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001690 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001691 ss_sel = cs_sel + 8;
1692 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001693 cs.l = 1;
1694 break;
1695 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001696 cs_sel |= SELECTOR_RPL_MASK;
1697 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001698
Gleb Natapov79168fd2010-04-28 19:15:30 +03001699 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1700 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1701 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1702 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001703
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001704 c->eip = c->regs[VCPU_REGS_RDX];
1705 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001706
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001707 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001708}
1709
Gleb Natapov9c537242010-03-18 15:20:05 +02001710static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1711 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001712{
1713 int iopl;
1714 if (ctxt->mode == X86EMUL_MODE_REAL)
1715 return false;
1716 if (ctxt->mode == X86EMUL_MODE_VM86)
1717 return true;
1718 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001719 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001720}
1721
1722static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1723 struct x86_emulate_ops *ops,
1724 u16 port, u16 len)
1725{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001726 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001727 int r;
1728 u16 io_bitmap_ptr;
1729 u8 perm, bit_idx = port & 0x7;
1730 unsigned mask = (1 << len) - 1;
1731
Gleb Natapov79168fd2010-04-28 19:15:30 +03001732 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1733 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001734 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001735 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001736 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001737 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1738 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001739 if (r != X86EMUL_CONTINUE)
1740 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001741 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001742 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001743 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1744 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001745 if (r != X86EMUL_CONTINUE)
1746 return false;
1747 if ((perm >> bit_idx) & mask)
1748 return false;
1749 return true;
1750}
1751
1752static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1753 struct x86_emulate_ops *ops,
1754 u16 port, u16 len)
1755{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001756 if (ctxt->perm_ok)
1757 return true;
1758
Gleb Natapov9c537242010-03-18 15:20:05 +02001759 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001760 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1761 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001762
1763 ctxt->perm_ok = true;
1764
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001765 return true;
1766}
1767
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001768static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1769 struct x86_emulate_ops *ops,
1770 struct tss_segment_16 *tss)
1771{
1772 struct decode_cache *c = &ctxt->decode;
1773
1774 tss->ip = c->eip;
1775 tss->flag = ctxt->eflags;
1776 tss->ax = c->regs[VCPU_REGS_RAX];
1777 tss->cx = c->regs[VCPU_REGS_RCX];
1778 tss->dx = c->regs[VCPU_REGS_RDX];
1779 tss->bx = c->regs[VCPU_REGS_RBX];
1780 tss->sp = c->regs[VCPU_REGS_RSP];
1781 tss->bp = c->regs[VCPU_REGS_RBP];
1782 tss->si = c->regs[VCPU_REGS_RSI];
1783 tss->di = c->regs[VCPU_REGS_RDI];
1784
1785 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1786 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1787 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1788 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1789 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1790}
1791
1792static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1793 struct x86_emulate_ops *ops,
1794 struct tss_segment_16 *tss)
1795{
1796 struct decode_cache *c = &ctxt->decode;
1797 int ret;
1798
1799 c->eip = tss->ip;
1800 ctxt->eflags = tss->flag | 2;
1801 c->regs[VCPU_REGS_RAX] = tss->ax;
1802 c->regs[VCPU_REGS_RCX] = tss->cx;
1803 c->regs[VCPU_REGS_RDX] = tss->dx;
1804 c->regs[VCPU_REGS_RBX] = tss->bx;
1805 c->regs[VCPU_REGS_RSP] = tss->sp;
1806 c->regs[VCPU_REGS_RBP] = tss->bp;
1807 c->regs[VCPU_REGS_RSI] = tss->si;
1808 c->regs[VCPU_REGS_RDI] = tss->di;
1809
1810 /*
1811 * SDM says that segment selectors are loaded before segment
1812 * descriptors
1813 */
1814 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1815 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1816 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1817 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1818 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1819
1820 /*
1821 * Now load segment descriptors. If fault happenes at this stage
1822 * it is handled in a context of new task
1823 */
1824 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1825 if (ret != X86EMUL_CONTINUE)
1826 return ret;
1827 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1828 if (ret != X86EMUL_CONTINUE)
1829 return ret;
1830 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1831 if (ret != X86EMUL_CONTINUE)
1832 return ret;
1833 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1834 if (ret != X86EMUL_CONTINUE)
1835 return ret;
1836 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1837 if (ret != X86EMUL_CONTINUE)
1838 return ret;
1839
1840 return X86EMUL_CONTINUE;
1841}
1842
1843static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1844 struct x86_emulate_ops *ops,
1845 u16 tss_selector, u16 old_tss_sel,
1846 ulong old_tss_base, struct desc_struct *new_desc)
1847{
1848 struct tss_segment_16 tss_seg;
1849 int ret;
1850 u32 err, new_tss_base = get_desc_base(new_desc);
1851
1852 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1853 &err);
1854 if (ret == X86EMUL_PROPAGATE_FAULT) {
1855 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001856 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001857 return ret;
1858 }
1859
1860 save_state_to_tss16(ctxt, ops, &tss_seg);
1861
1862 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1863 &err);
1864 if (ret == X86EMUL_PROPAGATE_FAULT) {
1865 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001866 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001867 return ret;
1868 }
1869
1870 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1871 &err);
1872 if (ret == X86EMUL_PROPAGATE_FAULT) {
1873 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001874 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001875 return ret;
1876 }
1877
1878 if (old_tss_sel != 0xffff) {
1879 tss_seg.prev_task_link = old_tss_sel;
1880
1881 ret = ops->write_std(new_tss_base,
1882 &tss_seg.prev_task_link,
1883 sizeof tss_seg.prev_task_link,
1884 ctxt->vcpu, &err);
1885 if (ret == X86EMUL_PROPAGATE_FAULT) {
1886 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001887 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001888 return ret;
1889 }
1890 }
1891
1892 return load_state_from_tss16(ctxt, ops, &tss_seg);
1893}
1894
1895static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1896 struct x86_emulate_ops *ops,
1897 struct tss_segment_32 *tss)
1898{
1899 struct decode_cache *c = &ctxt->decode;
1900
1901 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1902 tss->eip = c->eip;
1903 tss->eflags = ctxt->eflags;
1904 tss->eax = c->regs[VCPU_REGS_RAX];
1905 tss->ecx = c->regs[VCPU_REGS_RCX];
1906 tss->edx = c->regs[VCPU_REGS_RDX];
1907 tss->ebx = c->regs[VCPU_REGS_RBX];
1908 tss->esp = c->regs[VCPU_REGS_RSP];
1909 tss->ebp = c->regs[VCPU_REGS_RBP];
1910 tss->esi = c->regs[VCPU_REGS_RSI];
1911 tss->edi = c->regs[VCPU_REGS_RDI];
1912
1913 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1914 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1915 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1916 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1917 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1918 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1919 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1920}
1921
1922static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1923 struct x86_emulate_ops *ops,
1924 struct tss_segment_32 *tss)
1925{
1926 struct decode_cache *c = &ctxt->decode;
1927 int ret;
1928
Gleb Natapov0f122442010-04-28 19:15:31 +03001929 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001930 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001931 return X86EMUL_PROPAGATE_FAULT;
1932 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001933 c->eip = tss->eip;
1934 ctxt->eflags = tss->eflags | 2;
1935 c->regs[VCPU_REGS_RAX] = tss->eax;
1936 c->regs[VCPU_REGS_RCX] = tss->ecx;
1937 c->regs[VCPU_REGS_RDX] = tss->edx;
1938 c->regs[VCPU_REGS_RBX] = tss->ebx;
1939 c->regs[VCPU_REGS_RSP] = tss->esp;
1940 c->regs[VCPU_REGS_RBP] = tss->ebp;
1941 c->regs[VCPU_REGS_RSI] = tss->esi;
1942 c->regs[VCPU_REGS_RDI] = tss->edi;
1943
1944 /*
1945 * SDM says that segment selectors are loaded before segment
1946 * descriptors
1947 */
1948 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1949 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1950 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1951 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1952 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1953 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1954 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1955
1956 /*
1957 * Now load segment descriptors. If fault happenes at this stage
1958 * it is handled in a context of new task
1959 */
1960 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1961 if (ret != X86EMUL_CONTINUE)
1962 return ret;
1963 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1964 if (ret != X86EMUL_CONTINUE)
1965 return ret;
1966 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1967 if (ret != X86EMUL_CONTINUE)
1968 return ret;
1969 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1970 if (ret != X86EMUL_CONTINUE)
1971 return ret;
1972 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1973 if (ret != X86EMUL_CONTINUE)
1974 return ret;
1975 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1976 if (ret != X86EMUL_CONTINUE)
1977 return ret;
1978 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1979 if (ret != X86EMUL_CONTINUE)
1980 return ret;
1981
1982 return X86EMUL_CONTINUE;
1983}
1984
1985static int task_switch_32(struct x86_emulate_ctxt *ctxt,
1986 struct x86_emulate_ops *ops,
1987 u16 tss_selector, u16 old_tss_sel,
1988 ulong old_tss_base, struct desc_struct *new_desc)
1989{
1990 struct tss_segment_32 tss_seg;
1991 int ret;
1992 u32 err, new_tss_base = get_desc_base(new_desc);
1993
1994 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1995 &err);
1996 if (ret == X86EMUL_PROPAGATE_FAULT) {
1997 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001998 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001999 return ret;
2000 }
2001
2002 save_state_to_tss32(ctxt, ops, &tss_seg);
2003
2004 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2005 &err);
2006 if (ret == X86EMUL_PROPAGATE_FAULT) {
2007 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002008 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002009 return ret;
2010 }
2011
2012 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
2013 &err);
2014 if (ret == X86EMUL_PROPAGATE_FAULT) {
2015 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002016 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002017 return ret;
2018 }
2019
2020 if (old_tss_sel != 0xffff) {
2021 tss_seg.prev_task_link = old_tss_sel;
2022
2023 ret = ops->write_std(new_tss_base,
2024 &tss_seg.prev_task_link,
2025 sizeof tss_seg.prev_task_link,
2026 ctxt->vcpu, &err);
2027 if (ret == X86EMUL_PROPAGATE_FAULT) {
2028 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03002029 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002030 return ret;
2031 }
2032 }
2033
2034 return load_state_from_tss32(ctxt, ops, &tss_seg);
2035}
2036
2037static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002038 struct x86_emulate_ops *ops,
2039 u16 tss_selector, int reason,
2040 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002041{
2042 struct desc_struct curr_tss_desc, next_tss_desc;
2043 int ret;
2044 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2045 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002046 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002047 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002048
2049 /* FIXME: old_tss_base == ~0 ? */
2050
2051 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2052 if (ret != X86EMUL_CONTINUE)
2053 return ret;
2054 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2055 if (ret != X86EMUL_CONTINUE)
2056 return ret;
2057
2058 /* FIXME: check that next_tss_desc is tss */
2059
2060 if (reason != TASK_SWITCH_IRET) {
2061 if ((tss_selector & 3) > next_tss_desc.dpl ||
2062 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002063 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002064 return X86EMUL_PROPAGATE_FAULT;
2065 }
2066 }
2067
Gleb Natapovceffb452010-03-18 15:20:19 +02002068 desc_limit = desc_limit_scaled(&next_tss_desc);
2069 if (!next_tss_desc.p ||
2070 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2071 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002072 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002073 return X86EMUL_PROPAGATE_FAULT;
2074 }
2075
2076 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2077 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2078 write_segment_descriptor(ctxt, ops, old_tss_sel,
2079 &curr_tss_desc);
2080 }
2081
2082 if (reason == TASK_SWITCH_IRET)
2083 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2084
2085 /* set back link to prev task only if NT bit is set in eflags
2086 note that old_tss_sel is not used afetr this point */
2087 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2088 old_tss_sel = 0xffff;
2089
2090 if (next_tss_desc.type & 8)
2091 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2092 old_tss_base, &next_tss_desc);
2093 else
2094 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2095 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002096 if (ret != X86EMUL_CONTINUE)
2097 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002098
2099 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2100 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2101
2102 if (reason != TASK_SWITCH_IRET) {
2103 next_tss_desc.type |= (1 << 1); /* set busy flag */
2104 write_segment_descriptor(ctxt, ops, tss_selector,
2105 &next_tss_desc);
2106 }
2107
2108 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2109 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2110 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2111
Jan Kiszkae269fb22010-04-14 15:51:09 +02002112 if (has_error_code) {
2113 struct decode_cache *c = &ctxt->decode;
2114
2115 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2116 c->lock_prefix = 0;
2117 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002118 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002119 }
2120
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002121 return ret;
2122}
2123
2124int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002125 u16 tss_selector, int reason,
2126 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002127{
Avi Kivity9aabc882010-07-29 15:11:50 +03002128 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002129 struct decode_cache *c = &ctxt->decode;
2130 int rc;
2131
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002132 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002133 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002134
Jan Kiszkae269fb22010-04-14 15:51:09 +02002135 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2136 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002137
2138 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002139 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002140 if (rc == X86EMUL_CONTINUE)
2141 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002142 }
2143
Gleb Natapov19d04432010-04-15 12:29:50 +03002144 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002145}
2146
Gleb Natapova682e352010-03-18 15:20:21 +02002147static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002148 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002149{
2150 struct decode_cache *c = &ctxt->decode;
2151 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2152
Gleb Natapovd9271122010-03-18 15:20:22 +02002153 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440a2010-08-01 12:35:10 +03002154 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002155}
2156
Avi Kivity63540382010-07-29 15:11:55 +03002157static int em_push(struct x86_emulate_ctxt *ctxt)
2158{
2159 emulate_push(ctxt, ctxt->ops);
2160 return X86EMUL_CONTINUE;
2161}
2162
Avi Kivity73fba5f2010-07-29 15:11:53 +03002163#define D(_y) { .flags = (_y) }
2164#define N D(0)
2165#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2166#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2167#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2168
2169static struct opcode group1[] = {
2170 X7(D(Lock)), N
2171};
2172
2173static struct opcode group1A[] = {
2174 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2175};
2176
2177static struct opcode group3[] = {
2178 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2179 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +03002180 X4(D(SrcMem | ModRM)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002181};
2182
2183static struct opcode group4[] = {
2184 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2185 N, N, N, N, N, N,
2186};
2187
2188static struct opcode group5[] = {
2189 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2190 D(SrcMem | ModRM | Stack), N,
2191 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2192 D(SrcMem | ModRM | Stack), N,
2193};
2194
2195static struct group_dual group7 = { {
2196 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2197 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002198 D(SrcMem16 | ModRM | Mov | Priv),
2199 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002200}, {
2201 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2202 D(SrcNone | ModRM | DstMem | Mov), N,
2203 D(SrcMem16 | ModRM | Mov | Priv), N,
2204} };
2205
2206static struct opcode group8[] = {
2207 N, N, N, N,
2208 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2209 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2210};
2211
2212static struct group_dual group9 = { {
2213 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2214}, {
2215 N, N, N, N, N, N, N, N,
2216} };
2217
2218static struct opcode opcode_table[256] = {
2219 /* 0x00 - 0x07 */
2220 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2221 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2222 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2223 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2224 /* 0x08 - 0x0F */
2225 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2226 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2227 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2228 D(ImplicitOps | Stack | No64), N,
2229 /* 0x10 - 0x17 */
2230 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2231 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2232 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2233 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2234 /* 0x18 - 0x1F */
2235 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2236 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2237 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2238 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2239 /* 0x20 - 0x27 */
2240 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2241 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2242 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2243 /* 0x28 - 0x2F */
2244 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2245 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2246 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2247 /* 0x30 - 0x37 */
2248 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2249 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2250 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2251 /* 0x38 - 0x3F */
2252 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2253 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2254 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2255 N, N,
2256 /* 0x40 - 0x4F */
2257 X16(D(DstReg)),
2258 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002259 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002260 /* 0x58 - 0x5F */
2261 X8(D(DstReg | Stack)),
2262 /* 0x60 - 0x67 */
2263 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2264 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2265 N, N, N, N,
2266 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002267 I(SrcImm | Mov | Stack, em_push), N,
2268 I(SrcImmByte | Mov | Stack, em_push), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002269 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2270 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2271 /* 0x70 - 0x7F */
2272 X16(D(SrcImmByte)),
2273 /* 0x80 - 0x87 */
2274 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2275 G(DstMem | SrcImm | ModRM | Group, group1),
2276 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2277 G(DstMem | SrcImmByte | ModRM | Group, group1),
2278 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2279 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2280 /* 0x88 - 0x8F */
2281 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2282 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002283 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002284 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2285 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002286 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002287 /* 0x98 - 0x9F */
2288 N, N, D(SrcImmFAddr | No64), N,
2289 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2290 /* 0xA0 - 0xA7 */
2291 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2292 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2293 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2294 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2295 /* 0xA8 - 0xAF */
Wei Yongjun06cb7042010-08-04 15:36:53 +08002296 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm),
2297 D(ByteOp | SrcAcc | DstDI | Mov | String), D(SrcAcc | DstDI | Mov | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002298 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
2299 D(ByteOp | DstDI | String), D(DstDI | String),
2300 /* 0xB0 - 0xB7 */
2301 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2302 /* 0xB8 - 0xBF */
2303 X8(D(DstReg | SrcImm | Mov)),
2304 /* 0xC0 - 0xC7 */
2305 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
2306 N, D(ImplicitOps | Stack), N, N,
2307 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2308 /* 0xC8 - 0xCF */
2309 N, N, N, D(ImplicitOps | Stack),
2310 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2311 /* 0xD0 - 0xD7 */
Wei Yongjunc034da82010-08-04 15:38:59 +08002312 D(ByteOp | DstMem | SrcOne | ModRM), D(DstMem | SrcOne | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002313 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2314 N, N, N, N,
2315 /* 0xD8 - 0xDF */
2316 N, N, N, N, N, N, N, N,
2317 /* 0xE0 - 0xE7 */
2318 N, N, N, N,
2319 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2320 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2321 /* 0xE8 - 0xEF */
2322 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2323 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2324 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2325 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2326 /* 0xF0 - 0xF7 */
2327 N, N, N, N,
2328 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2329 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002330 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002331 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2332};
2333
2334static struct opcode twobyte_table[256] = {
2335 /* 0x00 - 0x0F */
2336 N, GD(0, &group7), N, N,
2337 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2338 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2339 N, D(ImplicitOps | ModRM), N, N,
2340 /* 0x10 - 0x1F */
2341 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2342 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002343 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2344 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002345 N, N, N, N,
2346 N, N, N, N, N, N, N, N,
2347 /* 0x30 - 0x3F */
2348 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
2349 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2350 N, N, N, N, N, N, N, N,
2351 /* 0x40 - 0x4F */
2352 X16(D(DstReg | SrcMem | ModRM | Mov)),
2353 /* 0x50 - 0x5F */
2354 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2355 /* 0x60 - 0x6F */
2356 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2357 /* 0x70 - 0x7F */
2358 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2359 /* 0x80 - 0x8F */
2360 X16(D(SrcImm)),
2361 /* 0x90 - 0x9F */
2362 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2363 /* 0xA0 - 0xA7 */
2364 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2365 N, D(DstMem | SrcReg | ModRM | BitOp),
2366 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2367 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2368 /* 0xA8 - 0xAF */
2369 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2370 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2371 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2372 D(DstMem | SrcReg | Src2CL | ModRM),
2373 D(ModRM), N,
2374 /* 0xB0 - 0xB7 */
2375 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2376 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2377 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2378 D(DstReg | SrcMem16 | ModRM | Mov),
2379 /* 0xB8 - 0xBF */
2380 N, N,
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08002381 G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002382 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2383 D(DstReg | SrcMem16 | ModRM | Mov),
2384 /* 0xC0 - 0xCF */
2385 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
2386 N, N, N, GD(0, &group9),
2387 N, N, N, N, N, N, N, N,
2388 /* 0xD0 - 0xDF */
2389 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2390 /* 0xE0 - 0xEF */
2391 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2392 /* 0xF0 - 0xFF */
2393 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2394};
2395
2396#undef D
2397#undef N
2398#undef G
2399#undef GD
2400#undef I
2401
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002402int
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002403x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2404{
2405 struct x86_emulate_ops *ops = ctxt->ops;
2406 struct decode_cache *c = &ctxt->decode;
2407 int rc = X86EMUL_CONTINUE;
2408 int mode = ctxt->mode;
2409 int def_op_bytes, def_ad_bytes, dual, goffset;
2410 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002411 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002412
2413 /* we cannot decode insn before we complete previous rep insn */
2414 WARN_ON(ctxt->restart);
2415
2416 c->eip = ctxt->eip;
2417 c->fetch.start = c->fetch.end = c->eip;
2418 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2419
2420 switch (mode) {
2421 case X86EMUL_MODE_REAL:
2422 case X86EMUL_MODE_VM86:
2423 case X86EMUL_MODE_PROT16:
2424 def_op_bytes = def_ad_bytes = 2;
2425 break;
2426 case X86EMUL_MODE_PROT32:
2427 def_op_bytes = def_ad_bytes = 4;
2428 break;
2429#ifdef CONFIG_X86_64
2430 case X86EMUL_MODE_PROT64:
2431 def_op_bytes = 4;
2432 def_ad_bytes = 8;
2433 break;
2434#endif
2435 default:
2436 return -1;
2437 }
2438
2439 c->op_bytes = def_op_bytes;
2440 c->ad_bytes = def_ad_bytes;
2441
2442 /* Legacy prefixes. */
2443 for (;;) {
2444 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2445 case 0x66: /* operand-size override */
2446 /* switch between 2/4 bytes */
2447 c->op_bytes = def_op_bytes ^ 6;
2448 break;
2449 case 0x67: /* address-size override */
2450 if (mode == X86EMUL_MODE_PROT64)
2451 /* switch between 4/8 bytes */
2452 c->ad_bytes = def_ad_bytes ^ 12;
2453 else
2454 /* switch between 2/4 bytes */
2455 c->ad_bytes = def_ad_bytes ^ 6;
2456 break;
2457 case 0x26: /* ES override */
2458 case 0x2e: /* CS override */
2459 case 0x36: /* SS override */
2460 case 0x3e: /* DS override */
2461 set_seg_override(c, (c->b >> 3) & 3);
2462 break;
2463 case 0x64: /* FS override */
2464 case 0x65: /* GS override */
2465 set_seg_override(c, c->b & 7);
2466 break;
2467 case 0x40 ... 0x4f: /* REX */
2468 if (mode != X86EMUL_MODE_PROT64)
2469 goto done_prefixes;
2470 c->rex_prefix = c->b;
2471 continue;
2472 case 0xf0: /* LOCK */
2473 c->lock_prefix = 1;
2474 break;
2475 case 0xf2: /* REPNE/REPNZ */
2476 c->rep_prefix = REPNE_PREFIX;
2477 break;
2478 case 0xf3: /* REP/REPE/REPZ */
2479 c->rep_prefix = REPE_PREFIX;
2480 break;
2481 default:
2482 goto done_prefixes;
2483 }
2484
2485 /* Any legacy prefix after a REX prefix nullifies its effect. */
2486
2487 c->rex_prefix = 0;
2488 }
2489
2490done_prefixes:
2491
2492 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002493 if (c->rex_prefix & 8)
2494 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002495
2496 /* Opcode byte(s). */
2497 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002498 /* Two-byte opcode? */
2499 if (c->b == 0x0f) {
2500 c->twobyte = 1;
2501 c->b = insn_fetch(u8, 1, c->eip);
2502 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002503 }
2504 c->d = opcode.flags;
2505
2506 if (c->d & Group) {
2507 dual = c->d & GroupDual;
2508 c->modrm = insn_fetch(u8, 1, c->eip);
2509 --c->eip;
2510
2511 if (c->d & GroupDual) {
2512 g_mod012 = opcode.u.gdual->mod012;
2513 g_mod3 = opcode.u.gdual->mod3;
2514 } else
2515 g_mod012 = g_mod3 = opcode.u.group;
2516
2517 c->d &= ~(Group | GroupDual);
2518
2519 goffset = (c->modrm >> 3) & 7;
2520
2521 if ((c->modrm >> 6) == 3)
2522 opcode = g_mod3[goffset];
2523 else
2524 opcode = g_mod012[goffset];
2525 c->d |= opcode.flags;
2526 }
2527
2528 c->execute = opcode.u.execute;
2529
2530 /* Unrecognised? */
2531 if (c->d == 0 || (c->d & Undefined)) {
2532 DPRINTF("Cannot emulate %02x\n", c->b);
2533 return -1;
2534 }
2535
2536 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2537 c->op_bytes = 8;
2538
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002539 if (c->d & Op3264) {
2540 if (mode == X86EMUL_MODE_PROT64)
2541 c->op_bytes = 8;
2542 else
2543 c->op_bytes = 4;
2544 }
2545
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002546 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002547 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002548 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002549 if (!c->has_seg_override)
2550 set_seg_override(c, c->modrm_seg);
2551 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002552 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002553 if (rc != X86EMUL_CONTINUE)
2554 goto done;
2555
2556 if (!c->has_seg_override)
2557 set_seg_override(c, VCPU_SREG_DS);
2558
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002559 if (memop.type == OP_MEM && !(!c->twobyte && c->b == 0x8d))
2560 memop.addr.mem += seg_override_base(ctxt, ops, c);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002561
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002562 if (memop.type == OP_MEM && c->ad_bytes != 8)
2563 memop.addr.mem = (u32)memop.addr.mem;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002564
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002565 if (memop.type == OP_MEM && c->rip_relative)
2566 memop.addr.mem += c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002567
2568 /*
2569 * Decode and fetch the source operand: register, memory
2570 * or immediate.
2571 */
2572 switch (c->d & SrcMask) {
2573 case SrcNone:
2574 break;
2575 case SrcReg:
2576 decode_register_operand(&c->src, c, 0);
2577 break;
2578 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002579 memop.bytes = 2;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002580 goto srcmem_common;
2581 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002582 memop.bytes = 4;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002583 goto srcmem_common;
2584 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002585 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002586 c->op_bytes;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002587 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002588 c->src = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002589 break;
2590 case SrcImm:
2591 case SrcImmU:
2592 c->src.type = OP_IMM;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002593 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002594 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2595 if (c->src.bytes == 8)
2596 c->src.bytes = 4;
2597 /* NB. Immediates are sign-extended as necessary. */
2598 switch (c->src.bytes) {
2599 case 1:
2600 c->src.val = insn_fetch(s8, 1, c->eip);
2601 break;
2602 case 2:
2603 c->src.val = insn_fetch(s16, 2, c->eip);
2604 break;
2605 case 4:
2606 c->src.val = insn_fetch(s32, 4, c->eip);
2607 break;
2608 }
2609 if ((c->d & SrcMask) == SrcImmU) {
2610 switch (c->src.bytes) {
2611 case 1:
2612 c->src.val &= 0xff;
2613 break;
2614 case 2:
2615 c->src.val &= 0xffff;
2616 break;
2617 case 4:
2618 c->src.val &= 0xffffffff;
2619 break;
2620 }
2621 }
2622 break;
2623 case SrcImmByte:
2624 case SrcImmUByte:
2625 c->src.type = OP_IMM;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002626 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002627 c->src.bytes = 1;
2628 if ((c->d & SrcMask) == SrcImmByte)
2629 c->src.val = insn_fetch(s8, 1, c->eip);
2630 else
2631 c->src.val = insn_fetch(u8, 1, c->eip);
2632 break;
2633 case SrcAcc:
2634 c->src.type = OP_REG;
2635 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002636 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002637 fetch_register_operand(&c->src);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002638 break;
2639 case SrcOne:
2640 c->src.bytes = 1;
2641 c->src.val = 1;
2642 break;
2643 case SrcSI:
2644 c->src.type = OP_MEM;
2645 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002646 c->src.addr.mem =
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002647 register_address(c, seg_override_base(ctxt, ops, c),
2648 c->regs[VCPU_REGS_RSI]);
2649 c->src.val = 0;
2650 break;
2651 case SrcImmFAddr:
2652 c->src.type = OP_IMM;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002653 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002654 c->src.bytes = c->op_bytes + 2;
2655 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2656 break;
2657 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002658 memop.bytes = c->op_bytes + 2;
2659 goto srcmem_common;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002660 break;
2661 }
2662
2663 /*
2664 * Decode and fetch the second source operand: register, memory
2665 * or immediate.
2666 */
2667 switch (c->d & Src2Mask) {
2668 case Src2None:
2669 break;
2670 case Src2CL:
2671 c->src2.bytes = 1;
2672 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2673 break;
2674 case Src2ImmByte:
2675 c->src2.type = OP_IMM;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002676 c->src2.addr.mem = c->eip;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002677 c->src2.bytes = 1;
2678 c->src2.val = insn_fetch(u8, 1, c->eip);
2679 break;
2680 case Src2One:
2681 c->src2.bytes = 1;
2682 c->src2.val = 1;
2683 break;
2684 }
2685
2686 /* Decode and fetch the destination operand: register or memory. */
2687 switch (c->d & DstMask) {
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002688 case DstReg:
2689 decode_register_operand(&c->dst, c,
2690 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2691 break;
2692 case DstMem:
2693 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002694 c->dst = memop;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002695 if ((c->d & DstMask) == DstMem64)
2696 c->dst.bytes = 8;
2697 else
2698 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002699 if (c->d & BitOp)
2700 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002701 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002702 break;
2703 case DstAcc:
2704 c->dst.type = OP_REG;
2705 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002706 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002707 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002708 c->dst.orig_val = c->dst.val;
2709 break;
2710 case DstDI:
2711 c->dst.type = OP_MEM;
2712 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002713 c->dst.addr.mem =
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002714 register_address(c, es_base(ctxt, ops),
2715 c->regs[VCPU_REGS_RDI]);
2716 c->dst.val = 0;
2717 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002718 case ImplicitOps:
2719 /* Special instructions do their own operand decoding. */
2720 default:
2721 c->dst.type = OP_NONE; /* Disable writeback. */
2722 return 0;
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002723 }
2724
2725done:
2726 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2727}
2728
2729int
Avi Kivity9aabc882010-07-29 15:11:50 +03002730x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002731{
Avi Kivity9aabc882010-07-29 15:11:50 +03002732 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002733 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002734 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002735 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002736 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002737 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002738
Gleb Natapov9de41572010-04-28 19:15:22 +03002739 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002740
Gleb Natapov11616242010-02-11 14:43:14 +02002741 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002742 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002743 goto done;
2744 }
2745
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002746 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002747 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002748 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002749 goto done;
2750 }
2751
Gleb Natapove92805a2010-02-10 14:21:35 +02002752 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002753 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002754 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002755 goto done;
2756 }
2757
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002758 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002759 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002760 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002761 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002762 string_done:
2763 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002764 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002765 goto done;
2766 }
2767 /* The second termination condition only applies for REPE
2768 * and REPNE. Test if the repeat string operation prefix is
2769 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2770 * corresponding termination condition according to:
2771 * - if REPE/REPZ and ZF = 0 then done
2772 * - if REPNE/REPNZ and ZF = 1 then done
2773 */
2774 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002775 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002776 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002777 ((ctxt->eflags & EFLG_ZF) == 0))
2778 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002779 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002780 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2781 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002782 }
Gleb Natapov063db062010-03-18 15:20:06 +02002783 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002784 }
2785
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002786 if (c->src.type == OP_MEM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002787 if (c->d & NoAccess)
2788 goto no_fetch;
Avi Kivity1a6440a2010-08-01 12:35:10 +03002789 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002790 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002791 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002792 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002793 c->src.orig_val64 = c->src.val64;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002794 no_fetch:
2795 ;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002796 }
2797
Gleb Natapove35b7b92010-02-25 16:36:42 +02002798 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440a2010-08-01 12:35:10 +03002799 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002800 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002801 if (rc != X86EMUL_CONTINUE)
2802 goto done;
2803 }
2804
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002805 if ((c->d & DstMask) == ImplicitOps)
2806 goto special_insn;
2807
2808
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002809 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2810 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440a2010-08-01 12:35:10 +03002811 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002812 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002813 if (rc != X86EMUL_CONTINUE)
2814 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002815 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002816 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002817
Avi Kivity018a98d2007-11-27 19:30:56 +02002818special_insn:
2819
Avi Kivityef65c882010-07-29 15:11:51 +03002820 if (c->execute) {
2821 rc = c->execute(ctxt);
2822 if (rc != X86EMUL_CONTINUE)
2823 goto done;
2824 goto writeback;
2825 }
2826
Laurent Viviere4e03de2007-09-18 11:52:50 +02002827 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 goto twobyte_insn;
2829
Laurent Viviere4e03de2007-09-18 11:52:50 +02002830 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 case 0x00 ... 0x05:
2832 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002833 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002835 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002836 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002837 break;
2838 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002839 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002840 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002841 goto done;
2842 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 case 0x08 ... 0x0d:
2844 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002845 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002847 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002848 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002849 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 case 0x10 ... 0x15:
2851 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002852 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002854 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002855 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002856 break;
2857 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002858 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002859 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002860 goto done;
2861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 case 0x18 ... 0x1d:
2863 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002864 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002866 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002867 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002868 break;
2869 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002870 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002871 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002872 goto done;
2873 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002874 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002876 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 break;
2878 case 0x28 ... 0x2d:
2879 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002880 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 break;
2882 case 0x30 ... 0x35:
2883 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002884 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 break;
2886 case 0x38 ... 0x3d:
2887 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002888 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002890 case 0x40 ... 0x47: /* inc r16/r32 */
2891 emulate_1op("inc", c->dst, ctxt->eflags);
2892 break;
2893 case 0x48 ... 0x4f: /* dec r16/r32 */
2894 emulate_1op("dec", c->dst, ctxt->eflags);
2895 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002896 case 0x58 ... 0x5f: /* pop reg */
2897 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002898 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002899 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002900 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002901 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002902 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002903 rc = emulate_pusha(ctxt, ops);
2904 if (rc != X86EMUL_CONTINUE)
2905 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002906 break;
2907 case 0x61: /* popa */
2908 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002909 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002910 goto done;
2911 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002913 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002915 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002917 case 0x6c: /* insb */
2918 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002919 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002920 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002921 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002922 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002923 goto done;
2924 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002925 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2926 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002927 goto done; /* IO is needed, skip writeback */
2928 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002929 case 0x6e: /* outsb */
2930 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002931 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002932 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002933 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002934 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002935 goto done;
2936 }
Gleb Natapov79729952010-03-18 15:20:24 +02002937 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2938 &c->src.val, 1, ctxt->vcpu);
2939
2940 c->dst.type = OP_NONE; /* nothing to writeback */
2941 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002942 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002943 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002944 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002945 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002947 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 case 0:
2949 goto add;
2950 case 1:
2951 goto or;
2952 case 2:
2953 goto adc;
2954 case 3:
2955 goto sbb;
2956 case 4:
2957 goto and;
2958 case 5:
2959 goto sub;
2960 case 6:
2961 goto xor;
2962 case 7:
2963 goto cmp;
2964 }
2965 break;
2966 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002967 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002968 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 break;
2970 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002971 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002973 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 case 1:
Avi Kivity1a6440a2010-08-01 12:35:10 +03002975 *(u8 *) c->src.addr.reg = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976 break;
2977 case 2:
Avi Kivity1a6440a2010-08-01 12:35:10 +03002978 *(u16 *) c->src.addr.reg = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979 break;
2980 case 4:
Avi Kivity1a6440a2010-08-01 12:35:10 +03002981 *c->src.addr.reg = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 break; /* 64b reg: zero-extend */
2983 case 8:
Avi Kivity1a6440a2010-08-01 12:35:10 +03002984 *c->src.addr.reg = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985 break;
2986 }
2987 /*
2988 * Write back the memory destination with implicit LOCK
2989 * prefix.
2990 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002991 c->dst.val = c->src.val;
2992 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002995 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002996 case 0x8c: /* mov r/m, sreg */
2997 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002998 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002999 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003000 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03003001 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02003002 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003003 case 0x8d: /* lea r16/r32, m */
Avi Kivity342fc632010-08-01 15:13:22 +03003004 c->dst.val = c->src.addr.mem;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03003005 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003006 case 0x8e: { /* mov seg, r/m16 */
3007 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003008
3009 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003010
Gleb Natapovc6975182010-02-18 12:15:01 +02003011 if (c->modrm_reg == VCPU_SREG_CS ||
3012 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003013 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02003014 goto done;
3015 }
3016
Glauber Costa310b5d32009-05-12 16:21:06 -04003017 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03003018 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04003019
Gleb Natapov2e873022010-03-18 15:20:18 +02003020 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02003021
3022 c->dst.type = OP_NONE; /* Disable writeback. */
3023 break;
3024 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003026 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003027 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03003030 case 0x90 ... 0x97: /* nop / xchg reg, rax */
3031 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03003032 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03003033 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07003034 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003035 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03003036 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003037 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03003038 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02003039 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003040 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02003041 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003042 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
3043 if (rc != X86EMUL_CONTINUE)
3044 goto done;
3045 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08003046 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02003048 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003050 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440a2010-08-01 12:35:10 +03003051 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02003052 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003053 case 0xa8 ... 0xa9: /* test ax, imm */
3054 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 case 0xaa ... 0xab: /* stos */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02003057 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058 case 0xae ... 0xaf: /* scas */
3059 DPRINTF("Urk! I don't handle SCAS.\n");
3060 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03003061 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02003062 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02003063 case 0xc0 ... 0xc1:
3064 emulate_grp2(ctxt);
3065 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003066 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003067 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003068 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003069 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003070 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02003071 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
3072 mov:
3073 c->dst.val = c->src.val;
3074 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003075 case 0xcb: /* ret far */
3076 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003077 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003078 goto done;
3079 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003080 case 0xcc: /* int3 */
3081 irq = 3;
3082 goto do_interrupt;
3083 case 0xcd: /* int n */
3084 irq = c->src.val;
3085 do_interrupt:
3086 rc = emulate_int(ctxt, ops, irq);
3087 if (rc != X86EMUL_CONTINUE)
3088 goto done;
3089 break;
3090 case 0xce: /* into */
3091 if (ctxt->eflags & EFLG_OF) {
3092 irq = 4;
3093 goto do_interrupt;
3094 }
3095 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003096 case 0xcf: /* iret */
3097 rc = emulate_iret(ctxt, ops);
3098
3099 if (rc != X86EMUL_CONTINUE)
3100 goto done;
3101 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003102 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003103 emulate_grp2(ctxt);
3104 break;
3105 case 0xd2 ... 0xd3: /* Grp2 */
3106 c->src.val = c->regs[VCPU_REGS_RCX];
3107 emulate_grp2(ctxt);
3108 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003109 case 0xe4: /* inb */
3110 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003111 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003112 case 0xe6: /* outb */
3113 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003114 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003115 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003116 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003117 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003118 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003119 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003120 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003121 }
3122 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003123 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003124 case 0xea: { /* jmp far */
3125 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02003126 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003127 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3128
3129 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003130 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003131
Gleb Natapov414e6272010-04-28 19:15:26 +03003132 c->eip = 0;
3133 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003134 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003135 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003136 case 0xeb:
3137 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003138 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003139 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003140 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003141 case 0xec: /* in al,dx */
3142 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003143 c->src.val = c->regs[VCPU_REGS_RDX];
3144 do_io_in:
3145 c->dst.bytes = min(c->dst.bytes, 4u);
3146 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003147 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003148 goto done;
3149 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003150 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3151 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003152 goto done; /* IO is needed */
3153 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003154 case 0xee: /* out dx,al */
3155 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003156 c->src.val = c->regs[VCPU_REGS_RDX];
3157 do_io_out:
3158 c->dst.bytes = min(c->dst.bytes, 4u);
3159 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003160 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003161 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003162 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003163 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3164 ctxt->vcpu);
3165 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003166 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003167 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003168 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003169 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003170 case 0xf5: /* cmc */
3171 /* complement carry flag from eflags reg */
3172 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003173 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003174 case 0xf6 ... 0xf7: /* Grp3 */
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03003175 if (emulate_grp3(ctxt, ops) != X86EMUL_CONTINUE)
Gleb Natapovaca06a82010-03-18 15:20:15 +02003176 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003177 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003178 case 0xf8: /* clc */
3179 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003180 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003181 case 0xf9: /* stc */
3182 ctxt->eflags |= EFLG_CF;
3183 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003184 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003185 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003186 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003187 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003188 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003189 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003190 break;
3191 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003192 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003193 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003194 goto done;
3195 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003196 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003197 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003198 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003199 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003200 case 0xfc: /* cld */
3201 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003202 break;
3203 case 0xfd: /* std */
3204 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003205 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003206 case 0xfe: /* Grp4 */
3207 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003208 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003209 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003210 goto done;
3211 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003212 case 0xff: /* Grp5 */
3213 if (c->modrm_reg == 5)
3214 goto jump_far;
3215 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003216 default:
3217 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003219
3220writeback:
3221 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003222 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003223 goto done;
3224
Gleb Natapov5cd21912010-03-18 15:20:26 +02003225 /*
3226 * restore dst type in case the decoding will be reused
3227 * (happens for string instruction )
3228 */
3229 c->dst.type = saved_dst_type;
3230
Gleb Natapova682e352010-03-18 15:20:21 +02003231 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003232 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3233 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003234
3235 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003236 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3237 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003238
Gleb Natapov5cd21912010-03-18 15:20:26 +02003239 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003240 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003241 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003242 /*
3243 * Re-enter guest when pio read ahead buffer is empty or,
3244 * if it is not used, after each 1024 iteration.
3245 */
3246 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3247 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003248 ctxt->restart = false;
3249 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003250 /*
3251 * reset read cache here in case string instruction is restared
3252 * without decoding
3253 */
3254 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003255 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003256
3257done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003258 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259
3260twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003261 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003263 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264 u16 size;
3265 unsigned long address;
3266
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003267 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003268 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003269 goto cannot_emulate;
3270
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003271 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003272 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003273 goto done;
3274
Avi Kivity33e38852008-05-21 15:34:25 +03003275 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003276 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003277 /* Disable writeback. */
3278 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003279 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280 case 2: /* lgdt */
Avi Kivity1a6440a2010-08-01 12:35:10 +03003281 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003282 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003283 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 goto done;
3285 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003286 /* Disable writeback. */
3287 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003289 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003290 if (c->modrm_mod == 3) {
3291 switch (c->modrm_rm) {
3292 case 1:
3293 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003294 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003295 goto done;
3296 break;
3297 default:
3298 goto cannot_emulate;
3299 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003300 } else {
Avi Kivity1a6440a2010-08-01 12:35:10 +03003301 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003302 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003303 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003304 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003305 goto done;
3306 realmode_lidt(ctxt->vcpu, size, address);
3307 }
Avi Kivity16286d02008-04-14 14:40:50 +03003308 /* Disable writeback. */
3309 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 break;
3311 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003312 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003313 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 break;
3315 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003316 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003317 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003318 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003320 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003321 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003322 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323 case 7: /* invlpg*/
Avi Kivity1f6f0582010-08-01 15:19:22 +03003324 emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
Avi Kivity16286d02008-04-14 14:40:50 +03003325 /* Disable writeback. */
3326 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003327 break;
3328 default:
3329 goto cannot_emulate;
3330 }
3331 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003332 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003333 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003334 if (rc != X86EMUL_CONTINUE)
3335 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003336 else
3337 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003338 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003339 case 0x06:
3340 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003341 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003342 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003343 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003344 break;
3345 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003346 case 0x0d: /* GrpP (prefetch) */
3347 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003348 break;
3349 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003350 switch (c->modrm_reg) {
3351 case 1:
3352 case 5 ... 7:
3353 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003354 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003355 goto done;
3356 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003357 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003358 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003360 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3361 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003362 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003363 goto done;
3364 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003365 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003367 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003368 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003369 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003370 goto done;
3371 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003372 c->dst.type = OP_NONE;
3373 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003375 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3376 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003377 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003378 goto done;
3379 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003380
Avi Kivityb27f3852010-08-01 14:25:22 +03003381 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003382 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3383 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3384 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003385 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003386 goto done;
3387 }
3388
Laurent Viviera01af5e2007-09-24 11:10:56 +02003389 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003391 case 0x30:
3392 /* wrmsr */
3393 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3394 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003395 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003396 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003397 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003398 }
3399 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003400 break;
3401 case 0x32:
3402 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003403 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003404 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003405 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003406 } else {
3407 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3408 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3409 }
3410 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003411 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003412 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003413 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003414 if (rc != X86EMUL_CONTINUE)
3415 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003416 else
3417 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003418 break;
3419 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003420 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003421 if (rc != X86EMUL_CONTINUE)
3422 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003423 else
3424 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003425 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003427 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003428 if (!test_cc(c->b, ctxt->eflags))
3429 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003431 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003432 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003433 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003434 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003435 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003436 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003437 break;
3438 case 0xa1: /* pop fs */
3439 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003440 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003441 goto done;
3442 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003443 case 0xa3:
3444 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003445 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003446 /* only subword offset */
3447 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003448 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003449 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003450 case 0xa4: /* shld imm8, r, r/m */
3451 case 0xa5: /* shld cl, r, r/m */
3452 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3453 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003454 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003455 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003456 break;
3457 case 0xa9: /* pop gs */
3458 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003459 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003460 goto done;
3461 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003462 case 0xab:
3463 bts: /* bts */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003464 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003465 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003466 case 0xac: /* shrd imm8, r, r/m */
3467 case 0xad: /* shrd cl, r, r/m */
3468 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3469 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003470 case 0xae: /* clflush */
3471 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003472 case 0xb0 ... 0xb1: /* cmpxchg */
3473 /*
3474 * Save real source value, then compare EAX against
3475 * destination.
3476 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003477 c->src.orig_val = c->src.val;
3478 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003479 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3480 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003482 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 } else {
3484 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003485 c->dst.type = OP_REG;
Avi Kivity1a6440a2010-08-01 12:35:10 +03003486 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003487 }
3488 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489 case 0xb3:
3490 btr: /* btr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003491 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003494 c->dst.bytes = c->op_bytes;
3495 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3496 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003499 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500 case 0:
3501 goto bt;
3502 case 1:
3503 goto bts;
3504 case 2:
3505 goto btr;
3506 case 3:
3507 goto btc;
3508 }
3509 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003510 case 0xbb:
3511 btc: /* btc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02003512 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003513 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003515 c->dst.bytes = c->op_bytes;
3516 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3517 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003519 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003520 c->dst.bytes = c->op_bytes;
3521 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3522 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003523 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003525 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003526 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003527 goto done;
3528 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003529 default:
3530 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531 }
3532 goto writeback;
3533
3534cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003535 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536 return -1;
3537}