blob: 5fc441c064ba126d14351ba49ed7d7ae45957703 [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 Kivityab85b12b2010-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 Kivityab85b12b2010-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 Kivityc9eaf20f2009-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
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318/* Fetch next part of the instruction being emulated. */
319#define insn_fetch(_type, _size, _eip) \
320({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200321 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200322 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323 goto done; \
324 (_eip) += (_size); \
325 (_type)_x; \
326})
327
Gleb Natapov414e6272010-04-28 19:15:26 +0300328#define insn_fetch_arr(_arr, _size, _eip) \
329({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
330 if (rc != X86EMUL_CONTINUE) \
331 goto done; \
332 (_eip) += (_size); \
333})
334
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800335static inline unsigned long ad_mask(struct decode_cache *c)
336{
337 return (1UL << (c->ad_bytes << 3)) - 1;
338}
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800341static inline unsigned long
342address_mask(struct decode_cache *c, unsigned long reg)
343{
344 if (c->ad_bytes == sizeof(unsigned long))
345 return reg;
346 else
347 return reg & ad_mask(c);
348}
349
350static inline unsigned long
351register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
352{
353 return base + address_mask(c, reg);
354}
355
Harvey Harrison7a9572752008-02-19 07:40:41 -0800356static inline void
357register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
358{
359 if (c->ad_bytes == sizeof(unsigned long))
360 *reg += inc;
361 else
362 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
363}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364
Harvey Harrison7a9572752008-02-19 07:40:41 -0800365static inline void jmp_rel(struct decode_cache *c, int rel)
366{
367 register_address_increment(c, &c->eip, rel);
368}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300369
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300370static void set_seg_override(struct decode_cache *c, int seg)
371{
372 c->has_seg_override = true;
373 c->seg_override = seg;
374}
375
Gleb Natapov79168fd2010-04-28 19:15:30 +0300376static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
377 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300378{
379 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
380 return 0;
381
Gleb Natapov79168fd2010-04-28 19:15:30 +0300382 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300383}
384
385static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300386 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300387 struct decode_cache *c)
388{
389 if (!c->has_seg_override)
390 return 0;
391
Gleb Natapov79168fd2010-04-28 19:15:30 +0300392 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300393}
394
Gleb Natapov79168fd2010-04-28 19:15:30 +0300395static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
396 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300397{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300398 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300399}
400
Gleb Natapov79168fd2010-04-28 19:15:30 +0300401static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
402 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300403{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300404 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300405}
406
Gleb Natapov54b84862010-04-28 19:15:44 +0300407static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
408 u32 error, bool valid)
409{
410 ctxt->exception = vec;
411 ctxt->error_code = error;
412 ctxt->error_code_valid = valid;
413 ctxt->restart = false;
414}
415
416static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
417{
418 emulate_exception(ctxt, GP_VECTOR, err, true);
419}
420
421static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
422 int err)
423{
424 ctxt->cr2 = addr;
425 emulate_exception(ctxt, PF_VECTOR, err, true);
426}
427
428static void emulate_ud(struct x86_emulate_ctxt *ctxt)
429{
430 emulate_exception(ctxt, UD_VECTOR, 0, false);
431}
432
433static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
434{
435 emulate_exception(ctxt, TS_VECTOR, err, true);
436}
437
Avi Kivity62266862007-11-20 13:15:52 +0200438static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
439 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300440 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200441{
442 struct fetch_cache *fc = &ctxt->decode.fetch;
443 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300444 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200445
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300446 if (eip == fc->end) {
447 cur_size = fc->end - fc->start;
448 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
449 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
450 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900451 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200452 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300453 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200454 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300455 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900456 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200457}
458
459static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
460 struct x86_emulate_ops *ops,
461 unsigned long eip, void *dest, unsigned size)
462{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900463 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200464
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200465 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200466 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200467 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200468 while (size--) {
469 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900470 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200471 return rc;
472 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900473 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200474}
475
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000476/*
477 * Given the 'reg' portion of a ModRM byte, and a register block, return a
478 * pointer into the block that addresses the relevant register.
479 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
480 */
481static void *decode_register(u8 modrm_reg, unsigned long *regs,
482 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483{
484 void *p;
485
486 p = &regs[modrm_reg];
487 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
488 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
489 return p;
490}
491
492static int read_descriptor(struct x86_emulate_ctxt *ctxt,
493 struct x86_emulate_ops *ops,
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300494 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495 u16 *size, unsigned long *address, int op_bytes)
496{
497 int rc;
498
499 if (op_bytes == 2)
500 op_bytes = 3;
501 *address = 0;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300502 rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900503 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 return rc;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300505 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 return rc;
507}
508
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300509static int test_cc(unsigned int condition, unsigned int flags)
510{
511 int rc = 0;
512
513 switch ((condition & 15) >> 1) {
514 case 0: /* o */
515 rc |= (flags & EFLG_OF);
516 break;
517 case 1: /* b/c/nae */
518 rc |= (flags & EFLG_CF);
519 break;
520 case 2: /* z/e */
521 rc |= (flags & EFLG_ZF);
522 break;
523 case 3: /* be/na */
524 rc |= (flags & (EFLG_CF|EFLG_ZF));
525 break;
526 case 4: /* s */
527 rc |= (flags & EFLG_SF);
528 break;
529 case 5: /* p/pe */
530 rc |= (flags & EFLG_PF);
531 break;
532 case 7: /* le/ng */
533 rc |= (flags & EFLG_ZF);
534 /* fall through */
535 case 6: /* l/nge */
536 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
537 break;
538 }
539
540 /* Odd condition identifiers (lsb == 1) have inverted sense. */
541 return (!!rc ^ (condition & 1));
542}
543
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300544static void fetch_register_operand(struct operand *op)
545{
546 switch (op->bytes) {
547 case 1:
548 op->val = *(u8 *)op->addr.reg;
549 break;
550 case 2:
551 op->val = *(u16 *)op->addr.reg;
552 break;
553 case 4:
554 op->val = *(u32 *)op->addr.reg;
555 break;
556 case 8:
557 op->val = *(u64 *)op->addr.reg;
558 break;
559 }
560}
561
Avi Kivity3c118e22007-10-31 10:27:04 +0200562static void decode_register_operand(struct operand *op,
563 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200564 int inhibit_bytereg)
565{
Avi Kivity33615aa2007-10-31 11:15:56 +0200566 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200567 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200568
569 if (!(c->d & ModRM))
570 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200571 op->type = OP_REG;
572 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300573 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200574 op->bytes = 1;
575 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300576 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200577 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200578 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300579 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200580 op->orig_val = op->val;
581}
582
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200583static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300584 struct x86_emulate_ops *ops,
585 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200586{
587 struct decode_cache *c = &ctxt->decode;
588 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700589 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900590 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300591 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200592
593 if (c->rex_prefix) {
594 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
595 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
596 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
597 }
598
599 c->modrm = insn_fetch(u8, 1, c->eip);
600 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
601 c->modrm_reg |= (c->modrm & 0x38) >> 3;
602 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300603 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200604
605 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300606 op->type = OP_REG;
607 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
608 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300609 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300610 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200611 return rc;
612 }
613
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300614 op->type = OP_MEM;
615
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200616 if (c->ad_bytes == 2) {
617 unsigned bx = c->regs[VCPU_REGS_RBX];
618 unsigned bp = c->regs[VCPU_REGS_RBP];
619 unsigned si = c->regs[VCPU_REGS_RSI];
620 unsigned di = c->regs[VCPU_REGS_RDI];
621
622 /* 16-bit ModR/M decode. */
623 switch (c->modrm_mod) {
624 case 0:
625 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300626 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200627 break;
628 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300629 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200630 break;
631 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300632 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200633 break;
634 }
635 switch (c->modrm_rm) {
636 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300637 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200638 break;
639 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300640 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200641 break;
642 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300643 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200644 break;
645 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300646 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200647 break;
648 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300649 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200650 break;
651 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300652 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200653 break;
654 case 6:
655 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300656 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200657 break;
658 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300659 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200660 break;
661 }
662 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
663 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300664 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300665 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200666 } else {
667 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700668 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200669 sib = insn_fetch(u8, 1, c->eip);
670 index_reg |= (sib >> 3) & 7;
671 base_reg |= sib & 7;
672 scale = sib >> 6;
673
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700674 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300675 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700676 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300677 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700678 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300679 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700680 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
681 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700682 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700683 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300684 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200685 switch (c->modrm_mod) {
686 case 0:
687 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300688 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200689 break;
690 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300691 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200692 break;
693 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300694 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200695 break;
696 }
697 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300698 op->addr.mem = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200699done:
700 return rc;
701}
702
703static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300704 struct x86_emulate_ops *ops,
705 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200706{
707 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900708 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200709
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300710 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200711 switch (c->ad_bytes) {
712 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300713 op->addr.mem = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200714 break;
715 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300716 op->addr.mem = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200717 break;
718 case 8:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300719 op->addr.mem = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720 break;
721 }
722done:
723 return rc;
724}
725
Wei Yongjun35c843c2010-08-09 11:34:56 +0800726static void fetch_bit_operand(struct decode_cache *c)
727{
728 long sv, mask;
729
730 if (c->dst.type == OP_MEM) {
731 mask = ~(c->dst.bytes * 8 - 1);
732
733 if (c->src.bytes == 2)
734 sv = (s16)c->src.val & (s16)mask;
735 else if (c->src.bytes == 4)
736 sv = (s32)c->src.val & (s32)mask;
737
738 c->dst.addr.mem += (sv >> 3);
739 }
740}
741
Gleb Natapov9de41572010-04-28 19:15:22 +0300742static int read_emulated(struct x86_emulate_ctxt *ctxt,
743 struct x86_emulate_ops *ops,
744 unsigned long addr, void *dest, unsigned size)
745{
746 int rc;
747 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300748 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300749
750 while (size) {
751 int n = min(size, 8u);
752 size -= n;
753 if (mc->pos < mc->end)
754 goto read_cached;
755
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300756 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
757 ctxt->vcpu);
758 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300759 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300760 if (rc != X86EMUL_CONTINUE)
761 return rc;
762 mc->end += n;
763
764 read_cached:
765 memcpy(dest, mc->data + mc->pos, n);
766 mc->pos += n;
767 dest += n;
768 addr += n;
769 }
770 return X86EMUL_CONTINUE;
771}
772
Gleb Natapov7b262e92010-03-18 15:20:27 +0200773static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
774 struct x86_emulate_ops *ops,
775 unsigned int size, unsigned short port,
776 void *dest)
777{
778 struct read_cache *rc = &ctxt->decode.io_read;
779
780 if (rc->pos == rc->end) { /* refill pio read ahead */
781 struct decode_cache *c = &ctxt->decode;
782 unsigned int in_page, n;
783 unsigned int count = c->rep_prefix ?
784 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
785 in_page = (ctxt->eflags & EFLG_DF) ?
786 offset_in_page(c->regs[VCPU_REGS_RDI]) :
787 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
788 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
789 count);
790 if (n == 0)
791 n = 1;
792 rc->pos = rc->end = 0;
793 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
794 return 0;
795 rc->end = n * size;
796 }
797
798 memcpy(dest, rc->data + rc->pos, size);
799 rc->pos += size;
800 return 1;
801}
802
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200803static u32 desc_limit_scaled(struct desc_struct *desc)
804{
805 u32 limit = get_desc_limit(desc);
806
807 return desc->g ? (limit << 12) | 0xfff : limit;
808}
809
810static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
811 struct x86_emulate_ops *ops,
812 u16 selector, struct desc_ptr *dt)
813{
814 if (selector & 1 << 2) {
815 struct desc_struct desc;
816 memset (dt, 0, sizeof *dt);
817 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
818 return;
819
820 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
821 dt->address = get_desc_base(&desc);
822 } else
823 ops->get_gdt(dt, ctxt->vcpu);
824}
825
826/* allowed just for 8 bytes segments */
827static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
828 struct x86_emulate_ops *ops,
829 u16 selector, struct desc_struct *desc)
830{
831 struct desc_ptr dt;
832 u16 index = selector >> 3;
833 int ret;
834 u32 err;
835 ulong addr;
836
837 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
838
839 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300840 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200841 return X86EMUL_PROPAGATE_FAULT;
842 }
843 addr = dt.address + index * 8;
844 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
845 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300846 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200847
848 return ret;
849}
850
851/* allowed just for 8 bytes segments */
852static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
853 struct x86_emulate_ops *ops,
854 u16 selector, struct desc_struct *desc)
855{
856 struct desc_ptr dt;
857 u16 index = selector >> 3;
858 u32 err;
859 ulong addr;
860 int ret;
861
862 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
863
864 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300865 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200866 return X86EMUL_PROPAGATE_FAULT;
867 }
868
869 addr = dt.address + index * 8;
870 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
871 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300872 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200873
874 return ret;
875}
876
877static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
878 struct x86_emulate_ops *ops,
879 u16 selector, int seg)
880{
881 struct desc_struct seg_desc;
882 u8 dpl, rpl, cpl;
883 unsigned err_vec = GP_VECTOR;
884 u32 err_code = 0;
885 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
886 int ret;
887
888 memset(&seg_desc, 0, sizeof seg_desc);
889
890 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
891 || ctxt->mode == X86EMUL_MODE_REAL) {
892 /* set real mode segment descriptor */
893 set_desc_base(&seg_desc, selector << 4);
894 set_desc_limit(&seg_desc, 0xffff);
895 seg_desc.type = 3;
896 seg_desc.p = 1;
897 seg_desc.s = 1;
898 goto load;
899 }
900
901 /* NULL selector is not valid for TR, CS and SS */
902 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
903 && null_selector)
904 goto exception;
905
906 /* TR should be in GDT only */
907 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
908 goto exception;
909
910 if (null_selector) /* for NULL selector skip all following checks */
911 goto load;
912
913 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
914 if (ret != X86EMUL_CONTINUE)
915 return ret;
916
917 err_code = selector & 0xfffc;
918 err_vec = GP_VECTOR;
919
920 /* can't load system descriptor into segment selecor */
921 if (seg <= VCPU_SREG_GS && !seg_desc.s)
922 goto exception;
923
924 if (!seg_desc.p) {
925 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
926 goto exception;
927 }
928
929 rpl = selector & 3;
930 dpl = seg_desc.dpl;
931 cpl = ops->cpl(ctxt->vcpu);
932
933 switch (seg) {
934 case VCPU_SREG_SS:
935 /*
936 * segment is not a writable data segment or segment
937 * selector's RPL != CPL or segment selector's RPL != CPL
938 */
939 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
940 goto exception;
941 break;
942 case VCPU_SREG_CS:
943 if (!(seg_desc.type & 8))
944 goto exception;
945
946 if (seg_desc.type & 4) {
947 /* conforming */
948 if (dpl > cpl)
949 goto exception;
950 } else {
951 /* nonconforming */
952 if (rpl > cpl || dpl != cpl)
953 goto exception;
954 }
955 /* CS(RPL) <- CPL */
956 selector = (selector & 0xfffc) | cpl;
957 break;
958 case VCPU_SREG_TR:
959 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
960 goto exception;
961 break;
962 case VCPU_SREG_LDTR:
963 if (seg_desc.s || seg_desc.type != 2)
964 goto exception;
965 break;
966 default: /* DS, ES, FS, or GS */
967 /*
968 * segment is not a data or readable code segment or
969 * ((segment is a data or nonconforming code segment)
970 * and (both RPL and CPL > DPL))
971 */
972 if ((seg_desc.type & 0xa) == 0x8 ||
973 (((seg_desc.type & 0xc) != 0xc) &&
974 (rpl > dpl && cpl > dpl)))
975 goto exception;
976 break;
977 }
978
979 if (seg_desc.s) {
980 /* mark segment as accessed */
981 seg_desc.type |= 1;
982 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
983 if (ret != X86EMUL_CONTINUE)
984 return ret;
985 }
986load:
987 ops->set_segment_selector(selector, seg, ctxt->vcpu);
988 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
989 return X86EMUL_CONTINUE;
990exception:
Gleb Natapov54b84862010-04-28 19:15:44 +0300991 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200992 return X86EMUL_PROPAGATE_FAULT;
993}
994
Wei Yongjunc37eda12010-06-15 09:03:33 +0800995static inline int writeback(struct x86_emulate_ctxt *ctxt,
996 struct x86_emulate_ops *ops)
997{
998 int rc;
999 struct decode_cache *c = &ctxt->decode;
1000 u32 err;
1001
1002 switch (c->dst.type) {
1003 case OP_REG:
1004 /* The 4-byte case *is* correct:
1005 * in 64-bit mode we zero-extend.
1006 */
1007 switch (c->dst.bytes) {
1008 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001009 *(u8 *)c->dst.addr.reg = (u8)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001010 break;
1011 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001012 *(u16 *)c->dst.addr.reg = (u16)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001013 break;
1014 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001015 *c->dst.addr.reg = (u32)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001016 break; /* 64b: zero-ext */
1017 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001018 *c->dst.addr.reg = c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001019 break;
1020 }
1021 break;
1022 case OP_MEM:
1023 if (c->lock_prefix)
1024 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001025 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001026 &c->dst.orig_val,
1027 &c->dst.val,
1028 c->dst.bytes,
1029 &err,
1030 ctxt->vcpu);
1031 else
1032 rc = ops->write_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001033 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001034 &c->dst.val,
1035 c->dst.bytes,
1036 &err,
1037 ctxt->vcpu);
1038 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001039 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001040 if (rc != X86EMUL_CONTINUE)
1041 return rc;
1042 break;
1043 case OP_NONE:
1044 /* no writeback */
1045 break;
1046 default:
1047 break;
1048 }
1049 return X86EMUL_CONTINUE;
1050}
1051
Gleb Natapov79168fd2010-04-28 19:15:30 +03001052static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1053 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001054{
1055 struct decode_cache *c = &ctxt->decode;
1056
1057 c->dst.type = OP_MEM;
1058 c->dst.bytes = c->op_bytes;
1059 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001060 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001061 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1062 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001063}
1064
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001065static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001066 struct x86_emulate_ops *ops,
1067 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001068{
1069 struct decode_cache *c = &ctxt->decode;
1070 int rc;
1071
Gleb Natapov79168fd2010-04-28 19:15:30 +03001072 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001073 c->regs[VCPU_REGS_RSP]),
1074 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001075 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001076 return rc;
1077
Avi Kivity350f69d2009-01-05 11:12:40 +02001078 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001079 return rc;
1080}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001081
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001082static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1083 struct x86_emulate_ops *ops,
1084 void *dest, int len)
1085{
1086 int rc;
1087 unsigned long val, change_mask;
1088 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001089 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001090
1091 rc = emulate_pop(ctxt, ops, &val, len);
1092 if (rc != X86EMUL_CONTINUE)
1093 return rc;
1094
1095 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1096 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1097
1098 switch(ctxt->mode) {
1099 case X86EMUL_MODE_PROT64:
1100 case X86EMUL_MODE_PROT32:
1101 case X86EMUL_MODE_PROT16:
1102 if (cpl == 0)
1103 change_mask |= EFLG_IOPL;
1104 if (cpl <= iopl)
1105 change_mask |= EFLG_IF;
1106 break;
1107 case X86EMUL_MODE_VM86:
1108 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001109 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001110 return X86EMUL_PROPAGATE_FAULT;
1111 }
1112 change_mask |= EFLG_IF;
1113 break;
1114 default: /* real mode */
1115 change_mask |= (EFLG_IOPL | EFLG_IF);
1116 break;
1117 }
1118
1119 *(unsigned long *)dest =
1120 (ctxt->eflags & ~change_mask) | (val & change_mask);
1121
1122 return rc;
1123}
1124
Gleb Natapov79168fd2010-04-28 19:15:30 +03001125static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1126 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001127{
1128 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001129
Gleb Natapov79168fd2010-04-28 19:15:30 +03001130 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001131
Gleb Natapov79168fd2010-04-28 19:15:30 +03001132 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001133}
1134
1135static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1136 struct x86_emulate_ops *ops, int seg)
1137{
1138 struct decode_cache *c = &ctxt->decode;
1139 unsigned long selector;
1140 int rc;
1141
1142 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001143 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001144 return rc;
1145
Gleb Natapov2e873022010-03-18 15:20:18 +02001146 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001147 return rc;
1148}
1149
Wei Yongjunc37eda12010-06-15 09:03:33 +08001150static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001151 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001152{
1153 struct decode_cache *c = &ctxt->decode;
1154 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001155 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001156 int reg = VCPU_REGS_RAX;
1157
1158 while (reg <= VCPU_REGS_RDI) {
1159 (reg == VCPU_REGS_RSP) ?
1160 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1161
Gleb Natapov79168fd2010-04-28 19:15:30 +03001162 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001163
1164 rc = writeback(ctxt, ops);
1165 if (rc != X86EMUL_CONTINUE)
1166 return rc;
1167
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001168 ++reg;
1169 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001170
1171 /* Disable writeback. */
1172 c->dst.type = OP_NONE;
1173
1174 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001175}
1176
1177static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1178 struct x86_emulate_ops *ops)
1179{
1180 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001181 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001182 int reg = VCPU_REGS_RDI;
1183
1184 while (reg >= VCPU_REGS_RAX) {
1185 if (reg == VCPU_REGS_RSP) {
1186 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1187 c->op_bytes);
1188 --reg;
1189 }
1190
1191 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001192 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001193 break;
1194 --reg;
1195 }
1196 return rc;
1197}
1198
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001199int emulate_int_real(struct x86_emulate_ctxt *ctxt,
1200 struct x86_emulate_ops *ops, int irq)
1201{
1202 struct decode_cache *c = &ctxt->decode;
1203 int rc = X86EMUL_CONTINUE;
1204 struct desc_ptr dt;
1205 gva_t cs_addr;
1206 gva_t eip_addr;
1207 u16 cs, eip;
1208 u32 err;
1209
1210 /* TODO: Add limit checks */
1211 c->src.val = ctxt->eflags;
1212 emulate_push(ctxt, ops);
1213
1214 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1215
1216 c->src.val = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1217 emulate_push(ctxt, ops);
1218
1219 c->src.val = c->eip;
1220 emulate_push(ctxt, ops);
1221
1222 ops->get_idt(&dt, ctxt->vcpu);
1223
1224 eip_addr = dt.address + (irq << 2);
1225 cs_addr = dt.address + (irq << 2) + 2;
1226
1227 rc = ops->read_std(cs_addr, &cs, 2, ctxt->vcpu, &err);
1228 if (rc != X86EMUL_CONTINUE)
1229 return rc;
1230
1231 rc = ops->read_std(eip_addr, &eip, 2, ctxt->vcpu, &err);
1232 if (rc != X86EMUL_CONTINUE)
1233 return rc;
1234
1235 rc = load_segment_descriptor(ctxt, ops, cs, VCPU_SREG_CS);
1236 if (rc != X86EMUL_CONTINUE)
1237 return rc;
1238
1239 c->eip = eip;
1240
1241 return rc;
1242}
1243
1244static int emulate_int(struct x86_emulate_ctxt *ctxt,
1245 struct x86_emulate_ops *ops, int irq)
1246{
1247 switch(ctxt->mode) {
1248 case X86EMUL_MODE_REAL:
1249 return emulate_int_real(ctxt, ops, irq);
1250 case X86EMUL_MODE_VM86:
1251 case X86EMUL_MODE_PROT16:
1252 case X86EMUL_MODE_PROT32:
1253 case X86EMUL_MODE_PROT64:
1254 default:
1255 /* Protected mode interrupts unimplemented yet */
1256 return X86EMUL_UNHANDLEABLE;
1257 }
1258}
1259
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001260static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1261 struct x86_emulate_ops *ops)
1262{
1263 struct decode_cache *c = &ctxt->decode;
1264 int rc = X86EMUL_CONTINUE;
1265 unsigned long temp_eip = 0;
1266 unsigned long temp_eflags = 0;
1267 unsigned long cs = 0;
1268 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1269 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1270 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1271 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1272
1273 /* TODO: Add stack limit check */
1274
1275 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1276
1277 if (rc != X86EMUL_CONTINUE)
1278 return rc;
1279
1280 if (temp_eip & ~0xffff) {
1281 emulate_gp(ctxt, 0);
1282 return X86EMUL_PROPAGATE_FAULT;
1283 }
1284
1285 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1286
1287 if (rc != X86EMUL_CONTINUE)
1288 return rc;
1289
1290 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1291
1292 if (rc != X86EMUL_CONTINUE)
1293 return rc;
1294
1295 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1296
1297 if (rc != X86EMUL_CONTINUE)
1298 return rc;
1299
1300 c->eip = temp_eip;
1301
1302
1303 if (c->op_bytes == 4)
1304 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1305 else if (c->op_bytes == 2) {
1306 ctxt->eflags &= ~0xffff;
1307 ctxt->eflags |= temp_eflags;
1308 }
1309
1310 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1311 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1312
1313 return rc;
1314}
1315
1316static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1317 struct x86_emulate_ops* ops)
1318{
1319 switch(ctxt->mode) {
1320 case X86EMUL_MODE_REAL:
1321 return emulate_iret_real(ctxt, ops);
1322 case X86EMUL_MODE_VM86:
1323 case X86EMUL_MODE_PROT16:
1324 case X86EMUL_MODE_PROT32:
1325 case X86EMUL_MODE_PROT64:
1326 default:
1327 /* iret from protected mode unimplemented yet */
1328 return X86EMUL_UNHANDLEABLE;
1329 }
1330}
1331
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001332static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1333 struct x86_emulate_ops *ops)
1334{
1335 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001336
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001337 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001338}
1339
Laurent Vivier05f086f2007-09-24 11:10:55 +02001340static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001341{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001342 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001343 switch (c->modrm_reg) {
1344 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001345 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346 break;
1347 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001348 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001349 break;
1350 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001351 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001352 break;
1353 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001354 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001355 break;
1356 case 4: /* sal/shl */
1357 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001358 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001359 break;
1360 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001361 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001362 break;
1363 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001364 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001365 break;
1366 }
1367}
1368
1369static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001370 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001371{
1372 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001373
1374 switch (c->modrm_reg) {
1375 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001376 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001377 break;
1378 case 2: /* not */
1379 c->dst.val = ~c->dst.val;
1380 break;
1381 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001382 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001383 break;
1384 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001385 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001386 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001387 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001388}
1389
1390static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001391 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001392{
1393 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001394
1395 switch (c->modrm_reg) {
1396 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001397 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001398 break;
1399 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001400 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001401 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001402 case 2: /* call near abs */ {
1403 long int old_eip;
1404 old_eip = c->eip;
1405 c->eip = c->src.val;
1406 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001407 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001408 break;
1409 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001410 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001411 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001412 break;
1413 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001414 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001415 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001416 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001417 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001418}
1419
1420static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001421 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001422{
1423 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001424 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001425
1426 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1427 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001428 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1429 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001430 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001431 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001432 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1433 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001434
Laurent Vivier05f086f2007-09-24 11:10:55 +02001435 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001436 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001437 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001438}
1439
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001440static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1441 struct x86_emulate_ops *ops)
1442{
1443 struct decode_cache *c = &ctxt->decode;
1444 int rc;
1445 unsigned long cs;
1446
1447 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001448 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001449 return rc;
1450 if (c->op_bytes == 4)
1451 c->eip = (u32)c->eip;
1452 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001453 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001454 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001455 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001456 return rc;
1457}
1458
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001459static inline void
1460setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001461 struct x86_emulate_ops *ops, struct desc_struct *cs,
1462 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001463{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001464 memset(cs, 0, sizeof(struct desc_struct));
1465 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1466 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001467
1468 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001469 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001470 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001471 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001472 cs->type = 0x0b; /* Read, Execute, Accessed */
1473 cs->s = 1;
1474 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001475 cs->p = 1;
1476 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001477
Gleb Natapov79168fd2010-04-28 19:15:30 +03001478 set_desc_base(ss, 0); /* flat segment */
1479 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001480 ss->g = 1; /* 4kb granularity */
1481 ss->s = 1;
1482 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001483 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001484 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001485 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001486}
1487
1488static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001489emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001490{
1491 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001492 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001493 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001494 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001495
1496 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001497 if (ctxt->mode == X86EMUL_MODE_REAL ||
1498 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001499 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001500 return X86EMUL_PROPAGATE_FAULT;
1501 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001502
Gleb Natapov79168fd2010-04-28 19:15:30 +03001503 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001504
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001505 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001506 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001507 cs_sel = (u16)(msr_data & 0xfffc);
1508 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001509
1510 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001511 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001512 cs.l = 1;
1513 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001514 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1515 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1516 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1517 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001518
1519 c->regs[VCPU_REGS_RCX] = c->eip;
1520 if (is_long_mode(ctxt->vcpu)) {
1521#ifdef CONFIG_X86_64
1522 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1523
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001524 ops->get_msr(ctxt->vcpu,
1525 ctxt->mode == X86EMUL_MODE_PROT64 ?
1526 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001527 c->eip = msr_data;
1528
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001529 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001530 ctxt->eflags &= ~(msr_data | EFLG_RF);
1531#endif
1532 } else {
1533 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001534 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001535 c->eip = (u32)msr_data;
1536
1537 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1538 }
1539
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001540 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001541}
1542
Andre Przywara8c604352009-06-18 12:56:01 +02001543static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001544emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001545{
1546 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001547 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001548 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001549 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001550
Gleb Natapova0044752010-02-10 14:21:31 +02001551 /* inject #GP if in real mode */
1552 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001553 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001554 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001555 }
1556
1557 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1558 * Therefore, we inject an #UD.
1559 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001560 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001561 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001562 return X86EMUL_PROPAGATE_FAULT;
1563 }
Andre Przywara8c604352009-06-18 12:56:01 +02001564
Gleb Natapov79168fd2010-04-28 19:15:30 +03001565 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001566
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001567 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001568 switch (ctxt->mode) {
1569 case X86EMUL_MODE_PROT32:
1570 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001571 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001572 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001573 }
1574 break;
1575 case X86EMUL_MODE_PROT64:
1576 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001577 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001578 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001579 }
1580 break;
1581 }
1582
1583 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001584 cs_sel = (u16)msr_data;
1585 cs_sel &= ~SELECTOR_RPL_MASK;
1586 ss_sel = cs_sel + 8;
1587 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001588 if (ctxt->mode == X86EMUL_MODE_PROT64
1589 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001590 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001591 cs.l = 1;
1592 }
1593
Gleb Natapov79168fd2010-04-28 19:15:30 +03001594 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1595 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1596 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1597 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001598
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001599 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001600 c->eip = msr_data;
1601
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001602 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001603 c->regs[VCPU_REGS_RSP] = msr_data;
1604
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001605 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001606}
1607
Andre Przywara4668f052009-06-18 12:56:02 +02001608static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001609emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001610{
1611 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001612 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001613 u64 msr_data;
1614 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001615 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001616
Gleb Natapova0044752010-02-10 14:21:31 +02001617 /* inject #GP if in real mode or Virtual 8086 mode */
1618 if (ctxt->mode == X86EMUL_MODE_REAL ||
1619 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001620 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001621 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001622 }
1623
Gleb Natapov79168fd2010-04-28 19:15:30 +03001624 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001625
1626 if ((c->rex_prefix & 0x8) != 0x0)
1627 usermode = X86EMUL_MODE_PROT64;
1628 else
1629 usermode = X86EMUL_MODE_PROT32;
1630
1631 cs.dpl = 3;
1632 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001633 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001634 switch (usermode) {
1635 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001636 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001637 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001638 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001639 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001640 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001641 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001642 break;
1643 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001644 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001645 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001646 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001647 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001648 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001649 ss_sel = cs_sel + 8;
1650 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001651 cs.l = 1;
1652 break;
1653 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001654 cs_sel |= SELECTOR_RPL_MASK;
1655 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001656
Gleb Natapov79168fd2010-04-28 19:15:30 +03001657 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1658 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1659 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1660 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001661
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001662 c->eip = c->regs[VCPU_REGS_RDX];
1663 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001664
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001665 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001666}
1667
Gleb Natapov9c537242010-03-18 15:20:05 +02001668static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1669 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001670{
1671 int iopl;
1672 if (ctxt->mode == X86EMUL_MODE_REAL)
1673 return false;
1674 if (ctxt->mode == X86EMUL_MODE_VM86)
1675 return true;
1676 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001677 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001678}
1679
1680static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1681 struct x86_emulate_ops *ops,
1682 u16 port, u16 len)
1683{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001684 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001685 int r;
1686 u16 io_bitmap_ptr;
1687 u8 perm, bit_idx = port & 0x7;
1688 unsigned mask = (1 << len) - 1;
1689
Gleb Natapov79168fd2010-04-28 19:15:30 +03001690 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1691 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001692 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001693 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001694 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001695 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1696 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001697 if (r != X86EMUL_CONTINUE)
1698 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001699 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001700 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001701 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1702 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001703 if (r != X86EMUL_CONTINUE)
1704 return false;
1705 if ((perm >> bit_idx) & mask)
1706 return false;
1707 return true;
1708}
1709
1710static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1711 struct x86_emulate_ops *ops,
1712 u16 port, u16 len)
1713{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001714 if (ctxt->perm_ok)
1715 return true;
1716
Gleb Natapov9c537242010-03-18 15:20:05 +02001717 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001718 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1719 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001720
1721 ctxt->perm_ok = true;
1722
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001723 return true;
1724}
1725
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001726static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1727 struct x86_emulate_ops *ops,
1728 struct tss_segment_16 *tss)
1729{
1730 struct decode_cache *c = &ctxt->decode;
1731
1732 tss->ip = c->eip;
1733 tss->flag = ctxt->eflags;
1734 tss->ax = c->regs[VCPU_REGS_RAX];
1735 tss->cx = c->regs[VCPU_REGS_RCX];
1736 tss->dx = c->regs[VCPU_REGS_RDX];
1737 tss->bx = c->regs[VCPU_REGS_RBX];
1738 tss->sp = c->regs[VCPU_REGS_RSP];
1739 tss->bp = c->regs[VCPU_REGS_RBP];
1740 tss->si = c->regs[VCPU_REGS_RSI];
1741 tss->di = c->regs[VCPU_REGS_RDI];
1742
1743 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1744 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1745 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1746 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1747 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1748}
1749
1750static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1751 struct x86_emulate_ops *ops,
1752 struct tss_segment_16 *tss)
1753{
1754 struct decode_cache *c = &ctxt->decode;
1755 int ret;
1756
1757 c->eip = tss->ip;
1758 ctxt->eflags = tss->flag | 2;
1759 c->regs[VCPU_REGS_RAX] = tss->ax;
1760 c->regs[VCPU_REGS_RCX] = tss->cx;
1761 c->regs[VCPU_REGS_RDX] = tss->dx;
1762 c->regs[VCPU_REGS_RBX] = tss->bx;
1763 c->regs[VCPU_REGS_RSP] = tss->sp;
1764 c->regs[VCPU_REGS_RBP] = tss->bp;
1765 c->regs[VCPU_REGS_RSI] = tss->si;
1766 c->regs[VCPU_REGS_RDI] = tss->di;
1767
1768 /*
1769 * SDM says that segment selectors are loaded before segment
1770 * descriptors
1771 */
1772 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1773 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1774 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1775 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1776 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1777
1778 /*
1779 * Now load segment descriptors. If fault happenes at this stage
1780 * it is handled in a context of new task
1781 */
1782 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1783 if (ret != X86EMUL_CONTINUE)
1784 return ret;
1785 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1786 if (ret != X86EMUL_CONTINUE)
1787 return ret;
1788 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1789 if (ret != X86EMUL_CONTINUE)
1790 return ret;
1791 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1792 if (ret != X86EMUL_CONTINUE)
1793 return ret;
1794 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1795 if (ret != X86EMUL_CONTINUE)
1796 return ret;
1797
1798 return X86EMUL_CONTINUE;
1799}
1800
1801static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1802 struct x86_emulate_ops *ops,
1803 u16 tss_selector, u16 old_tss_sel,
1804 ulong old_tss_base, struct desc_struct *new_desc)
1805{
1806 struct tss_segment_16 tss_seg;
1807 int ret;
1808 u32 err, new_tss_base = get_desc_base(new_desc);
1809
1810 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1811 &err);
1812 if (ret == X86EMUL_PROPAGATE_FAULT) {
1813 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001814 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001815 return ret;
1816 }
1817
1818 save_state_to_tss16(ctxt, ops, &tss_seg);
1819
1820 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1821 &err);
1822 if (ret == X86EMUL_PROPAGATE_FAULT) {
1823 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001824 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001825 return ret;
1826 }
1827
1828 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1829 &err);
1830 if (ret == X86EMUL_PROPAGATE_FAULT) {
1831 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001832 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001833 return ret;
1834 }
1835
1836 if (old_tss_sel != 0xffff) {
1837 tss_seg.prev_task_link = old_tss_sel;
1838
1839 ret = ops->write_std(new_tss_base,
1840 &tss_seg.prev_task_link,
1841 sizeof tss_seg.prev_task_link,
1842 ctxt->vcpu, &err);
1843 if (ret == X86EMUL_PROPAGATE_FAULT) {
1844 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001845 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001846 return ret;
1847 }
1848 }
1849
1850 return load_state_from_tss16(ctxt, ops, &tss_seg);
1851}
1852
1853static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1854 struct x86_emulate_ops *ops,
1855 struct tss_segment_32 *tss)
1856{
1857 struct decode_cache *c = &ctxt->decode;
1858
1859 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1860 tss->eip = c->eip;
1861 tss->eflags = ctxt->eflags;
1862 tss->eax = c->regs[VCPU_REGS_RAX];
1863 tss->ecx = c->regs[VCPU_REGS_RCX];
1864 tss->edx = c->regs[VCPU_REGS_RDX];
1865 tss->ebx = c->regs[VCPU_REGS_RBX];
1866 tss->esp = c->regs[VCPU_REGS_RSP];
1867 tss->ebp = c->regs[VCPU_REGS_RBP];
1868 tss->esi = c->regs[VCPU_REGS_RSI];
1869 tss->edi = c->regs[VCPU_REGS_RDI];
1870
1871 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1872 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1873 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1874 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1875 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1876 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1877 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1878}
1879
1880static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1881 struct x86_emulate_ops *ops,
1882 struct tss_segment_32 *tss)
1883{
1884 struct decode_cache *c = &ctxt->decode;
1885 int ret;
1886
Gleb Natapov0f122442010-04-28 19:15:31 +03001887 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001888 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001889 return X86EMUL_PROPAGATE_FAULT;
1890 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001891 c->eip = tss->eip;
1892 ctxt->eflags = tss->eflags | 2;
1893 c->regs[VCPU_REGS_RAX] = tss->eax;
1894 c->regs[VCPU_REGS_RCX] = tss->ecx;
1895 c->regs[VCPU_REGS_RDX] = tss->edx;
1896 c->regs[VCPU_REGS_RBX] = tss->ebx;
1897 c->regs[VCPU_REGS_RSP] = tss->esp;
1898 c->regs[VCPU_REGS_RBP] = tss->ebp;
1899 c->regs[VCPU_REGS_RSI] = tss->esi;
1900 c->regs[VCPU_REGS_RDI] = tss->edi;
1901
1902 /*
1903 * SDM says that segment selectors are loaded before segment
1904 * descriptors
1905 */
1906 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1907 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1908 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1909 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1910 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1911 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1912 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1913
1914 /*
1915 * Now load segment descriptors. If fault happenes at this stage
1916 * it is handled in a context of new task
1917 */
1918 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1919 if (ret != X86EMUL_CONTINUE)
1920 return ret;
1921 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1922 if (ret != X86EMUL_CONTINUE)
1923 return ret;
1924 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1925 if (ret != X86EMUL_CONTINUE)
1926 return ret;
1927 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1928 if (ret != X86EMUL_CONTINUE)
1929 return ret;
1930 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1931 if (ret != X86EMUL_CONTINUE)
1932 return ret;
1933 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1934 if (ret != X86EMUL_CONTINUE)
1935 return ret;
1936 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1937 if (ret != X86EMUL_CONTINUE)
1938 return ret;
1939
1940 return X86EMUL_CONTINUE;
1941}
1942
1943static int task_switch_32(struct x86_emulate_ctxt *ctxt,
1944 struct x86_emulate_ops *ops,
1945 u16 tss_selector, u16 old_tss_sel,
1946 ulong old_tss_base, struct desc_struct *new_desc)
1947{
1948 struct tss_segment_32 tss_seg;
1949 int ret;
1950 u32 err, new_tss_base = get_desc_base(new_desc);
1951
1952 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1953 &err);
1954 if (ret == X86EMUL_PROPAGATE_FAULT) {
1955 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001956 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001957 return ret;
1958 }
1959
1960 save_state_to_tss32(ctxt, ops, &tss_seg);
1961
1962 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1963 &err);
1964 if (ret == X86EMUL_PROPAGATE_FAULT) {
1965 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001966 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001967 return ret;
1968 }
1969
1970 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1971 &err);
1972 if (ret == X86EMUL_PROPAGATE_FAULT) {
1973 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001974 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001975 return ret;
1976 }
1977
1978 if (old_tss_sel != 0xffff) {
1979 tss_seg.prev_task_link = old_tss_sel;
1980
1981 ret = ops->write_std(new_tss_base,
1982 &tss_seg.prev_task_link,
1983 sizeof tss_seg.prev_task_link,
1984 ctxt->vcpu, &err);
1985 if (ret == X86EMUL_PROPAGATE_FAULT) {
1986 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001987 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001988 return ret;
1989 }
1990 }
1991
1992 return load_state_from_tss32(ctxt, ops, &tss_seg);
1993}
1994
1995static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001996 struct x86_emulate_ops *ops,
1997 u16 tss_selector, int reason,
1998 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001999{
2000 struct desc_struct curr_tss_desc, next_tss_desc;
2001 int ret;
2002 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
2003 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03002004 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02002005 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002006
2007 /* FIXME: old_tss_base == ~0 ? */
2008
2009 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
2010 if (ret != X86EMUL_CONTINUE)
2011 return ret;
2012 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
2013 if (ret != X86EMUL_CONTINUE)
2014 return ret;
2015
2016 /* FIXME: check that next_tss_desc is tss */
2017
2018 if (reason != TASK_SWITCH_IRET) {
2019 if ((tss_selector & 3) > next_tss_desc.dpl ||
2020 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002021 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002022 return X86EMUL_PROPAGATE_FAULT;
2023 }
2024 }
2025
Gleb Natapovceffb452010-03-18 15:20:19 +02002026 desc_limit = desc_limit_scaled(&next_tss_desc);
2027 if (!next_tss_desc.p ||
2028 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2029 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002030 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002031 return X86EMUL_PROPAGATE_FAULT;
2032 }
2033
2034 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2035 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
2036 write_segment_descriptor(ctxt, ops, old_tss_sel,
2037 &curr_tss_desc);
2038 }
2039
2040 if (reason == TASK_SWITCH_IRET)
2041 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2042
2043 /* set back link to prev task only if NT bit is set in eflags
2044 note that old_tss_sel is not used afetr this point */
2045 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2046 old_tss_sel = 0xffff;
2047
2048 if (next_tss_desc.type & 8)
2049 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
2050 old_tss_base, &next_tss_desc);
2051 else
2052 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
2053 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002054 if (ret != X86EMUL_CONTINUE)
2055 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002056
2057 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2058 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2059
2060 if (reason != TASK_SWITCH_IRET) {
2061 next_tss_desc.type |= (1 << 1); /* set busy flag */
2062 write_segment_descriptor(ctxt, ops, tss_selector,
2063 &next_tss_desc);
2064 }
2065
2066 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
2067 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
2068 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
2069
Jan Kiszkae269fb22010-04-14 15:51:09 +02002070 if (has_error_code) {
2071 struct decode_cache *c = &ctxt->decode;
2072
2073 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2074 c->lock_prefix = 0;
2075 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002076 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002077 }
2078
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002079 return ret;
2080}
2081
2082int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002083 u16 tss_selector, int reason,
2084 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002085{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002086 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002087 struct decode_cache *c = &ctxt->decode;
2088 int rc;
2089
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002090 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002091 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002092
Jan Kiszkae269fb22010-04-14 15:51:09 +02002093 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2094 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002095
2096 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002097 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002098 if (rc == X86EMUL_CONTINUE)
2099 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002100 }
2101
Gleb Natapov19d04432010-04-15 12:29:50 +03002102 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002103}
2104
Gleb Natapova682e352010-03-18 15:20:21 +02002105static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002106 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002107{
2108 struct decode_cache *c = &ctxt->decode;
2109 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2110
Gleb Natapovd9271122010-03-18 15:20:22 +02002111 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002112 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002113}
2114
Avi Kivity63540382010-07-29 15:11:55 +03002115static int em_push(struct x86_emulate_ctxt *ctxt)
2116{
2117 emulate_push(ctxt, ctxt->ops);
2118 return X86EMUL_CONTINUE;
2119}
2120
Avi Kivity73fba5f2010-07-29 15:11:53 +03002121#define D(_y) { .flags = (_y) }
2122#define N D(0)
2123#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2124#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2125#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2126
2127static struct opcode group1[] = {
2128 X7(D(Lock)), N
2129};
2130
2131static struct opcode group1A[] = {
2132 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2133};
2134
2135static struct opcode group3[] = {
2136 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2137 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2138 X4(D(Undefined)),
2139};
2140
2141static struct opcode group4[] = {
2142 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2143 N, N, N, N, N, N,
2144};
2145
2146static struct opcode group5[] = {
2147 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2148 D(SrcMem | ModRM | Stack), N,
2149 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2150 D(SrcMem | ModRM | Stack), N,
2151};
2152
2153static struct group_dual group7 = { {
2154 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2155 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002156 D(SrcMem16 | ModRM | Mov | Priv),
2157 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002158}, {
2159 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2160 D(SrcNone | ModRM | DstMem | Mov), N,
2161 D(SrcMem16 | ModRM | Mov | Priv), N,
2162} };
2163
2164static struct opcode group8[] = {
2165 N, N, N, N,
2166 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2167 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2168};
2169
2170static struct group_dual group9 = { {
2171 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2172}, {
2173 N, N, N, N, N, N, N, N,
2174} };
2175
2176static struct opcode opcode_table[256] = {
2177 /* 0x00 - 0x07 */
2178 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2179 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2180 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2181 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2182 /* 0x08 - 0x0F */
2183 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2184 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2185 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2186 D(ImplicitOps | Stack | No64), N,
2187 /* 0x10 - 0x17 */
2188 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2189 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2190 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2191 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2192 /* 0x18 - 0x1F */
2193 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2194 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2195 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2196 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2197 /* 0x20 - 0x27 */
2198 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2199 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2200 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2201 /* 0x28 - 0x2F */
2202 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2203 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2204 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2205 /* 0x30 - 0x37 */
2206 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2207 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2208 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2209 /* 0x38 - 0x3F */
2210 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2211 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2212 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2213 N, N,
2214 /* 0x40 - 0x4F */
2215 X16(D(DstReg)),
2216 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002217 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002218 /* 0x58 - 0x5F */
2219 X8(D(DstReg | Stack)),
2220 /* 0x60 - 0x67 */
2221 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2222 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2223 N, N, N, N,
2224 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002225 I(SrcImm | Mov | Stack, em_push), N,
2226 I(SrcImmByte | Mov | Stack, em_push), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002227 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2228 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2229 /* 0x70 - 0x7F */
2230 X16(D(SrcImmByte)),
2231 /* 0x80 - 0x87 */
2232 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2233 G(DstMem | SrcImm | ModRM | Group, group1),
2234 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2235 G(DstMem | SrcImmByte | ModRM | Group, group1),
2236 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2237 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2238 /* 0x88 - 0x8F */
2239 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2240 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002241 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002242 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2243 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002244 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002245 /* 0x98 - 0x9F */
2246 N, N, D(SrcImmFAddr | No64), N,
2247 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2248 /* 0xA0 - 0xA7 */
2249 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2250 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2251 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2252 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2253 /* 0xA8 - 0xAF */
Wei Yongjun06cb7042010-08-04 15:36:53 +08002254 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm),
2255 D(ByteOp | SrcAcc | DstDI | Mov | String), D(SrcAcc | DstDI | Mov | String),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002256 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
2257 D(ByteOp | DstDI | String), D(DstDI | String),
2258 /* 0xB0 - 0xB7 */
2259 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2260 /* 0xB8 - 0xBF */
2261 X8(D(DstReg | SrcImm | Mov)),
2262 /* 0xC0 - 0xC7 */
2263 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
2264 N, D(ImplicitOps | Stack), N, N,
2265 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2266 /* 0xC8 - 0xCF */
2267 N, N, N, D(ImplicitOps | Stack),
2268 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2269 /* 0xD0 - 0xD7 */
Wei Yongjunc034da82010-08-04 15:38:59 +08002270 D(ByteOp | DstMem | SrcOne | ModRM), D(DstMem | SrcOne | ModRM),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002271 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2272 N, N, N, N,
2273 /* 0xD8 - 0xDF */
2274 N, N, N, N, N, N, N, N,
2275 /* 0xE0 - 0xE7 */
2276 N, N, N, N,
2277 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2278 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2279 /* 0xE8 - 0xEF */
2280 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2281 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2282 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2283 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2284 /* 0xF0 - 0xF7 */
2285 N, N, N, N,
2286 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2287 /* 0xF8 - 0xFF */
Mohammed Gamal8744aa92010-08-05 15:42:49 +03002288 D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002289 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2290};
2291
2292static struct opcode twobyte_table[256] = {
2293 /* 0x00 - 0x0F */
2294 N, GD(0, &group7), N, N,
2295 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2296 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2297 N, D(ImplicitOps | ModRM), N, N,
2298 /* 0x10 - 0x1F */
2299 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2300 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002301 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2302 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002303 N, N, N, N,
2304 N, N, N, N, N, N, N, N,
2305 /* 0x30 - 0x3F */
2306 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
2307 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2308 N, N, N, N, N, N, N, N,
2309 /* 0x40 - 0x4F */
2310 X16(D(DstReg | SrcMem | ModRM | Mov)),
2311 /* 0x50 - 0x5F */
2312 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2313 /* 0x60 - 0x6F */
2314 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2315 /* 0x70 - 0x7F */
2316 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2317 /* 0x80 - 0x8F */
2318 X16(D(SrcImm)),
2319 /* 0x90 - 0x9F */
2320 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2321 /* 0xA0 - 0xA7 */
2322 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2323 N, D(DstMem | SrcReg | ModRM | BitOp),
2324 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2325 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2326 /* 0xA8 - 0xAF */
2327 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2328 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2329 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2330 D(DstMem | SrcReg | Src2CL | ModRM),
2331 D(ModRM), N,
2332 /* 0xB0 - 0xB7 */
2333 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2334 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2335 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2336 D(DstReg | SrcMem16 | ModRM | Mov),
2337 /* 0xB8 - 0xBF */
2338 N, N,
2339 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2340 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2341 D(DstReg | SrcMem16 | ModRM | Mov),
2342 /* 0xC0 - 0xCF */
2343 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
2344 N, N, N, GD(0, &group9),
2345 N, N, N, N, N, N, N, N,
2346 /* 0xD0 - 0xDF */
2347 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2348 /* 0xE0 - 0xEF */
2349 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2350 /* 0xF0 - 0xFF */
2351 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2352};
2353
2354#undef D
2355#undef N
2356#undef G
2357#undef GD
2358#undef I
2359
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002360int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002361x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2362{
2363 struct x86_emulate_ops *ops = ctxt->ops;
2364 struct decode_cache *c = &ctxt->decode;
2365 int rc = X86EMUL_CONTINUE;
2366 int mode = ctxt->mode;
2367 int def_op_bytes, def_ad_bytes, dual, goffset;
2368 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002369 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002370
2371 /* we cannot decode insn before we complete previous rep insn */
2372 WARN_ON(ctxt->restart);
2373
2374 c->eip = ctxt->eip;
2375 c->fetch.start = c->fetch.end = c->eip;
2376 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2377
2378 switch (mode) {
2379 case X86EMUL_MODE_REAL:
2380 case X86EMUL_MODE_VM86:
2381 case X86EMUL_MODE_PROT16:
2382 def_op_bytes = def_ad_bytes = 2;
2383 break;
2384 case X86EMUL_MODE_PROT32:
2385 def_op_bytes = def_ad_bytes = 4;
2386 break;
2387#ifdef CONFIG_X86_64
2388 case X86EMUL_MODE_PROT64:
2389 def_op_bytes = 4;
2390 def_ad_bytes = 8;
2391 break;
2392#endif
2393 default:
2394 return -1;
2395 }
2396
2397 c->op_bytes = def_op_bytes;
2398 c->ad_bytes = def_ad_bytes;
2399
2400 /* Legacy prefixes. */
2401 for (;;) {
2402 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2403 case 0x66: /* operand-size override */
2404 /* switch between 2/4 bytes */
2405 c->op_bytes = def_op_bytes ^ 6;
2406 break;
2407 case 0x67: /* address-size override */
2408 if (mode == X86EMUL_MODE_PROT64)
2409 /* switch between 4/8 bytes */
2410 c->ad_bytes = def_ad_bytes ^ 12;
2411 else
2412 /* switch between 2/4 bytes */
2413 c->ad_bytes = def_ad_bytes ^ 6;
2414 break;
2415 case 0x26: /* ES override */
2416 case 0x2e: /* CS override */
2417 case 0x36: /* SS override */
2418 case 0x3e: /* DS override */
2419 set_seg_override(c, (c->b >> 3) & 3);
2420 break;
2421 case 0x64: /* FS override */
2422 case 0x65: /* GS override */
2423 set_seg_override(c, c->b & 7);
2424 break;
2425 case 0x40 ... 0x4f: /* REX */
2426 if (mode != X86EMUL_MODE_PROT64)
2427 goto done_prefixes;
2428 c->rex_prefix = c->b;
2429 continue;
2430 case 0xf0: /* LOCK */
2431 c->lock_prefix = 1;
2432 break;
2433 case 0xf2: /* REPNE/REPNZ */
2434 c->rep_prefix = REPNE_PREFIX;
2435 break;
2436 case 0xf3: /* REP/REPE/REPZ */
2437 c->rep_prefix = REPE_PREFIX;
2438 break;
2439 default:
2440 goto done_prefixes;
2441 }
2442
2443 /* Any legacy prefix after a REX prefix nullifies its effect. */
2444
2445 c->rex_prefix = 0;
2446 }
2447
2448done_prefixes:
2449
2450 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002451 if (c->rex_prefix & 8)
2452 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002453
2454 /* Opcode byte(s). */
2455 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002456 /* Two-byte opcode? */
2457 if (c->b == 0x0f) {
2458 c->twobyte = 1;
2459 c->b = insn_fetch(u8, 1, c->eip);
2460 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002461 }
2462 c->d = opcode.flags;
2463
2464 if (c->d & Group) {
2465 dual = c->d & GroupDual;
2466 c->modrm = insn_fetch(u8, 1, c->eip);
2467 --c->eip;
2468
2469 if (c->d & GroupDual) {
2470 g_mod012 = opcode.u.gdual->mod012;
2471 g_mod3 = opcode.u.gdual->mod3;
2472 } else
2473 g_mod012 = g_mod3 = opcode.u.group;
2474
2475 c->d &= ~(Group | GroupDual);
2476
2477 goffset = (c->modrm >> 3) & 7;
2478
2479 if ((c->modrm >> 6) == 3)
2480 opcode = g_mod3[goffset];
2481 else
2482 opcode = g_mod012[goffset];
2483 c->d |= opcode.flags;
2484 }
2485
2486 c->execute = opcode.u.execute;
2487
2488 /* Unrecognised? */
2489 if (c->d == 0 || (c->d & Undefined)) {
2490 DPRINTF("Cannot emulate %02x\n", c->b);
2491 return -1;
2492 }
2493
2494 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2495 c->op_bytes = 8;
2496
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002497 if (c->d & Op3264) {
2498 if (mode == X86EMUL_MODE_PROT64)
2499 c->op_bytes = 8;
2500 else
2501 c->op_bytes = 4;
2502 }
2503
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002504 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002505 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002506 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002507 if (!c->has_seg_override)
2508 set_seg_override(c, c->modrm_seg);
2509 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002510 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002511 if (rc != X86EMUL_CONTINUE)
2512 goto done;
2513
2514 if (!c->has_seg_override)
2515 set_seg_override(c, VCPU_SREG_DS);
2516
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002517 if (memop.type == OP_MEM && !(!c->twobyte && c->b == 0x8d))
2518 memop.addr.mem += seg_override_base(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002519
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002520 if (memop.type == OP_MEM && c->ad_bytes != 8)
2521 memop.addr.mem = (u32)memop.addr.mem;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002522
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002523 if (memop.type == OP_MEM && c->rip_relative)
2524 memop.addr.mem += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002525
2526 /*
2527 * Decode and fetch the source operand: register, memory
2528 * or immediate.
2529 */
2530 switch (c->d & SrcMask) {
2531 case SrcNone:
2532 break;
2533 case SrcReg:
2534 decode_register_operand(&c->src, c, 0);
2535 break;
2536 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002537 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002538 goto srcmem_common;
2539 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002540 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002541 goto srcmem_common;
2542 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002543 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002544 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002545 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002546 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002547 break;
2548 case SrcImm:
2549 case SrcImmU:
2550 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002551 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002552 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2553 if (c->src.bytes == 8)
2554 c->src.bytes = 4;
2555 /* NB. Immediates are sign-extended as necessary. */
2556 switch (c->src.bytes) {
2557 case 1:
2558 c->src.val = insn_fetch(s8, 1, c->eip);
2559 break;
2560 case 2:
2561 c->src.val = insn_fetch(s16, 2, c->eip);
2562 break;
2563 case 4:
2564 c->src.val = insn_fetch(s32, 4, c->eip);
2565 break;
2566 }
2567 if ((c->d & SrcMask) == SrcImmU) {
2568 switch (c->src.bytes) {
2569 case 1:
2570 c->src.val &= 0xff;
2571 break;
2572 case 2:
2573 c->src.val &= 0xffff;
2574 break;
2575 case 4:
2576 c->src.val &= 0xffffffff;
2577 break;
2578 }
2579 }
2580 break;
2581 case SrcImmByte:
2582 case SrcImmUByte:
2583 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002584 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002585 c->src.bytes = 1;
2586 if ((c->d & SrcMask) == SrcImmByte)
2587 c->src.val = insn_fetch(s8, 1, c->eip);
2588 else
2589 c->src.val = insn_fetch(u8, 1, c->eip);
2590 break;
2591 case SrcAcc:
2592 c->src.type = OP_REG;
2593 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002594 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002595 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002596 break;
2597 case SrcOne:
2598 c->src.bytes = 1;
2599 c->src.val = 1;
2600 break;
2601 case SrcSI:
2602 c->src.type = OP_MEM;
2603 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002604 c->src.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002605 register_address(c, seg_override_base(ctxt, ops, c),
2606 c->regs[VCPU_REGS_RSI]);
2607 c->src.val = 0;
2608 break;
2609 case SrcImmFAddr:
2610 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002611 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002612 c->src.bytes = c->op_bytes + 2;
2613 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2614 break;
2615 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002616 memop.bytes = c->op_bytes + 2;
2617 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002618 break;
2619 }
2620
2621 /*
2622 * Decode and fetch the second source operand: register, memory
2623 * or immediate.
2624 */
2625 switch (c->d & Src2Mask) {
2626 case Src2None:
2627 break;
2628 case Src2CL:
2629 c->src2.bytes = 1;
2630 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2631 break;
2632 case Src2ImmByte:
2633 c->src2.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002634 c->src2.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002635 c->src2.bytes = 1;
2636 c->src2.val = insn_fetch(u8, 1, c->eip);
2637 break;
2638 case Src2One:
2639 c->src2.bytes = 1;
2640 c->src2.val = 1;
2641 break;
2642 }
2643
2644 /* Decode and fetch the destination operand: register or memory. */
2645 switch (c->d & DstMask) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002646 case DstReg:
2647 decode_register_operand(&c->dst, c,
2648 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2649 break;
2650 case DstMem:
2651 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002652 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002653 if ((c->d & DstMask) == DstMem64)
2654 c->dst.bytes = 8;
2655 else
2656 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Wei Yongjun35c843c2010-08-09 11:34:56 +08002657 if (c->d & BitOp)
2658 fetch_bit_operand(c);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002659 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002660 break;
2661 case DstAcc:
2662 c->dst.type = OP_REG;
2663 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002664 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002665 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002666 c->dst.orig_val = c->dst.val;
2667 break;
2668 case DstDI:
2669 c->dst.type = OP_MEM;
2670 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002671 c->dst.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002672 register_address(c, es_base(ctxt, ops),
2673 c->regs[VCPU_REGS_RDI]);
2674 c->dst.val = 0;
2675 break;
Wei Yongjun36089fe2010-08-04 15:38:18 +08002676 case ImplicitOps:
2677 /* Special instructions do their own operand decoding. */
2678 default:
2679 c->dst.type = OP_NONE; /* Disable writeback. */
2680 return 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002681 }
2682
2683done:
2684 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2685}
2686
2687int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002688x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002689{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002690 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002691 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002692 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002693 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002694 int saved_dst_type = c->dst.type;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002695 int irq; /* Used for int 3, int, and into */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002696
Gleb Natapov9de41572010-04-28 19:15:22 +03002697 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002698
Gleb Natapov11616242010-02-11 14:43:14 +02002699 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002700 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002701 goto done;
2702 }
2703
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002704 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002705 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002706 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002707 goto done;
2708 }
2709
Gleb Natapove92805a2010-02-10 14:21:35 +02002710 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002711 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002712 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002713 goto done;
2714 }
2715
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002716 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002717 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002718 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002719 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002720 string_done:
2721 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002722 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002723 goto done;
2724 }
2725 /* The second termination condition only applies for REPE
2726 * and REPNE. Test if the repeat string operation prefix is
2727 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2728 * corresponding termination condition according to:
2729 * - if REPE/REPZ and ZF = 0 then done
2730 * - if REPNE/REPNZ and ZF = 1 then done
2731 */
2732 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002733 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002734 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002735 ((ctxt->eflags & EFLG_ZF) == 0))
2736 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002737 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002738 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2739 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002740 }
Gleb Natapov063db062010-03-18 15:20:06 +02002741 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002742 }
2743
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002744 if (c->src.type == OP_MEM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002745 if (c->d & NoAccess)
2746 goto no_fetch;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002747 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002748 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002749 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002750 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002751 c->src.orig_val64 = c->src.val64;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002752 no_fetch:
2753 ;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002754 }
2755
Gleb Natapove35b7b92010-02-25 16:36:42 +02002756 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002757 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002758 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002759 if (rc != X86EMUL_CONTINUE)
2760 goto done;
2761 }
2762
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002763 if ((c->d & DstMask) == ImplicitOps)
2764 goto special_insn;
2765
2766
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002767 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2768 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002769 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002770 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002771 if (rc != X86EMUL_CONTINUE)
2772 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002773 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002774 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002775
Avi Kivity018a98d2007-11-27 19:30:56 +02002776special_insn:
2777
Avi Kivityef65c882010-07-29 15:11:51 +03002778 if (c->execute) {
2779 rc = c->execute(ctxt);
2780 if (rc != X86EMUL_CONTINUE)
2781 goto done;
2782 goto writeback;
2783 }
2784
Laurent Viviere4e03de2007-09-18 11:52:50 +02002785 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 goto twobyte_insn;
2787
Laurent Viviere4e03de2007-09-18 11:52:50 +02002788 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 case 0x00 ... 0x05:
2790 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002791 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002793 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002794 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002795 break;
2796 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002797 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002798 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002799 goto done;
2800 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 case 0x08 ... 0x0d:
2802 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002803 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002805 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002806 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002807 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 case 0x10 ... 0x15:
2809 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002810 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002812 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002813 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002814 break;
2815 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002816 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002817 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002818 goto done;
2819 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 case 0x18 ... 0x1d:
2821 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002822 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002824 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002825 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002826 break;
2827 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002828 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002829 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002830 goto done;
2831 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002832 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002834 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 break;
2836 case 0x28 ... 0x2d:
2837 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002838 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 break;
2840 case 0x30 ... 0x35:
2841 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002842 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 break;
2844 case 0x38 ... 0x3d:
2845 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002846 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002848 case 0x40 ... 0x47: /* inc r16/r32 */
2849 emulate_1op("inc", c->dst, ctxt->eflags);
2850 break;
2851 case 0x48 ... 0x4f: /* dec r16/r32 */
2852 emulate_1op("dec", c->dst, ctxt->eflags);
2853 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002854 case 0x58 ... 0x5f: /* pop reg */
2855 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002856 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002857 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002858 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002859 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002860 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002861 rc = emulate_pusha(ctxt, ops);
2862 if (rc != X86EMUL_CONTINUE)
2863 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002864 break;
2865 case 0x61: /* popa */
2866 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002867 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002868 goto done;
2869 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002871 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002873 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002875 case 0x6c: /* insb */
2876 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002877 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002878 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002879 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002880 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002881 goto done;
2882 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002883 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2884 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002885 goto done; /* IO is needed, skip writeback */
2886 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002887 case 0x6e: /* outsb */
2888 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002889 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002890 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002891 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002892 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002893 goto done;
2894 }
Gleb Natapov79729952010-03-18 15:20:24 +02002895 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2896 &c->src.val, 1, ctxt->vcpu);
2897
2898 c->dst.type = OP_NONE; /* nothing to writeback */
2899 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002900 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002901 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002902 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002903 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002905 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 case 0:
2907 goto add;
2908 case 1:
2909 goto or;
2910 case 2:
2911 goto adc;
2912 case 3:
2913 goto sbb;
2914 case 4:
2915 goto and;
2916 case 5:
2917 goto sub;
2918 case 6:
2919 goto xor;
2920 case 7:
2921 goto cmp;
2922 }
2923 break;
2924 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002925 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002926 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 break;
2928 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002929 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002931 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002933 *(u8 *) c->src.addr.reg = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 break;
2935 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002936 *(u16 *) c->src.addr.reg = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937 break;
2938 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002939 *c->src.addr.reg = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 break; /* 64b reg: zero-extend */
2941 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002942 *c->src.addr.reg = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 break;
2944 }
2945 /*
2946 * Write back the memory destination with implicit LOCK
2947 * prefix.
2948 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002949 c->dst.val = c->src.val;
2950 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002953 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002954 case 0x8c: /* mov r/m, sreg */
2955 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002956 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002957 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002958 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002959 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002960 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002961 case 0x8d: /* lea r16/r32, m */
Avi Kivity342fc632010-08-01 15:13:22 +03002962 c->dst.val = c->src.addr.mem;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002963 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002964 case 0x8e: { /* mov seg, r/m16 */
2965 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002966
2967 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002968
Gleb Natapovc6975182010-02-18 12:15:01 +02002969 if (c->modrm_reg == VCPU_SREG_CS ||
2970 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002971 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002972 goto done;
2973 }
2974
Glauber Costa310b5d32009-05-12 16:21:06 -04002975 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002976 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002977
Gleb Natapov2e873022010-03-18 15:20:18 +02002978 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002979
2980 c->dst.type = OP_NONE; /* Disable writeback. */
2981 break;
2982 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002984 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002985 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002988 case 0x90 ... 0x97: /* nop / xchg reg, rax */
2989 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03002990 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002991 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002992 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002993 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002994 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002995 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002996 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002997 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002998 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002999 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02003000 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
3001 if (rc != X86EMUL_CONTINUE)
3002 goto done;
3003 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08003004 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02003006 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01003008 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003009 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02003010 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03003011 case 0xa8 ... 0xa9: /* test ax, imm */
3012 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013 case 0xaa ... 0xab: /* stos */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02003015 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016 case 0xae ... 0xaf: /* scas */
3017 DPRINTF("Urk! I don't handle SCAS.\n");
3018 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03003019 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02003020 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02003021 case 0xc0 ... 0xc1:
3022 emulate_grp2(ctxt);
3023 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003024 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003025 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003026 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02003027 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02003028 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02003029 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
3030 mov:
3031 c->dst.val = c->src.val;
3032 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003033 case 0xcb: /* ret far */
3034 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003035 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02003036 goto done;
3037 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03003038 case 0xcc: /* int3 */
3039 irq = 3;
3040 goto do_interrupt;
3041 case 0xcd: /* int n */
3042 irq = c->src.val;
3043 do_interrupt:
3044 rc = emulate_int(ctxt, ops, irq);
3045 if (rc != X86EMUL_CONTINUE)
3046 goto done;
3047 break;
3048 case 0xce: /* into */
3049 if (ctxt->eflags & EFLG_OF) {
3050 irq = 4;
3051 goto do_interrupt;
3052 }
3053 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03003054 case 0xcf: /* iret */
3055 rc = emulate_iret(ctxt, ops);
3056
3057 if (rc != X86EMUL_CONTINUE)
3058 goto done;
3059 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003060 case 0xd0 ... 0xd1: /* Grp2 */
Avi Kivity018a98d2007-11-27 19:30:56 +02003061 emulate_grp2(ctxt);
3062 break;
3063 case 0xd2 ... 0xd3: /* Grp2 */
3064 c->src.val = c->regs[VCPU_REGS_RCX];
3065 emulate_grp2(ctxt);
3066 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003067 case 0xe4: /* inb */
3068 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003069 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003070 case 0xe6: /* outb */
3071 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003072 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003073 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003074 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003075 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003076 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003077 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003078 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003079 }
3080 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003081 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003082 case 0xea: { /* jmp far */
3083 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003084 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003085 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3086
3087 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003088 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003089
Gleb Natapov414e6272010-04-28 19:15:26 +03003090 c->eip = 0;
3091 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003092 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003093 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003094 case 0xeb:
3095 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003096 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003097 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003098 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003099 case 0xec: /* in al,dx */
3100 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003101 c->src.val = c->regs[VCPU_REGS_RDX];
3102 do_io_in:
3103 c->dst.bytes = min(c->dst.bytes, 4u);
3104 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003105 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003106 goto done;
3107 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003108 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3109 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003110 goto done; /* IO is needed */
3111 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003112 case 0xee: /* out dx,al */
3113 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003114 c->src.val = c->regs[VCPU_REGS_RDX];
3115 do_io_out:
3116 c->dst.bytes = min(c->dst.bytes, 4u);
3117 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003118 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003119 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003120 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003121 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3122 ctxt->vcpu);
3123 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003124 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003125 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003126 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003127 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003128 case 0xf5: /* cmc */
3129 /* complement carry flag from eflags reg */
3130 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003131 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003132 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003133 if (!emulate_grp3(ctxt, ops))
3134 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003135 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003136 case 0xf8: /* clc */
3137 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003138 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03003139 case 0xf9: /* stc */
3140 ctxt->eflags |= EFLG_CF;
3141 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003142 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003143 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003144 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003145 goto done;
Wei Yongjun36089fe2010-08-04 15:38:18 +08003146 } else
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003147 ctxt->eflags &= ~X86_EFLAGS_IF;
Avi Kivity111de5d2007-11-27 19:14:21 +02003148 break;
3149 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003150 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003151 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003152 goto done;
3153 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003154 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003155 ctxt->eflags |= X86_EFLAGS_IF;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003156 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003157 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003158 case 0xfc: /* cld */
3159 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003160 break;
3161 case 0xfd: /* std */
3162 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003163 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003164 case 0xfe: /* Grp4 */
3165 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003166 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003167 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003168 goto done;
3169 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003170 case 0xff: /* Grp5 */
3171 if (c->modrm_reg == 5)
3172 goto jump_far;
3173 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003174 default:
3175 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003177
3178writeback:
3179 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003180 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003181 goto done;
3182
Gleb Natapov5cd21912010-03-18 15:20:26 +02003183 /*
3184 * restore dst type in case the decoding will be reused
3185 * (happens for string instruction )
3186 */
3187 c->dst.type = saved_dst_type;
3188
Gleb Natapova682e352010-03-18 15:20:21 +02003189 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003190 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3191 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003192
3193 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003194 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3195 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003196
Gleb Natapov5cd21912010-03-18 15:20:26 +02003197 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003198 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003199 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003200 /*
3201 * Re-enter guest when pio read ahead buffer is empty or,
3202 * if it is not used, after each 1024 iteration.
3203 */
3204 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3205 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003206 ctxt->restart = false;
3207 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003208 /*
3209 * reset read cache here in case string instruction is restared
3210 * without decoding
3211 */
3212 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003213 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003214
3215done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003216 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217
3218twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003219 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003221 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 u16 size;
3223 unsigned long address;
3224
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003225 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003226 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003227 goto cannot_emulate;
3228
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003229 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003230 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003231 goto done;
3232
Avi Kivity33e38852008-05-21 15:34:25 +03003233 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003234 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003235 /* Disable writeback. */
3236 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003237 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003239 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003240 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003241 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 goto done;
3243 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003244 /* Disable writeback. */
3245 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003247 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003248 if (c->modrm_mod == 3) {
3249 switch (c->modrm_rm) {
3250 case 1:
3251 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003252 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003253 goto done;
3254 break;
3255 default:
3256 goto cannot_emulate;
3257 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003258 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003259 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003260 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003261 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003262 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003263 goto done;
3264 realmode_lidt(ctxt->vcpu, size, address);
3265 }
Avi Kivity16286d02008-04-14 14:40:50 +03003266 /* Disable writeback. */
3267 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 break;
3269 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003270 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003271 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 break;
3273 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003274 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003275 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003276 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003278 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003279 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003280 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 case 7: /* invlpg*/
Avi Kivity1f6f0582010-08-01 15:19:22 +03003282 emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
Avi Kivity16286d02008-04-14 14:40:50 +03003283 /* Disable writeback. */
3284 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285 break;
3286 default:
3287 goto cannot_emulate;
3288 }
3289 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003290 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003291 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003292 if (rc != X86EMUL_CONTINUE)
3293 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003294 else
3295 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003296 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003297 case 0x06:
3298 emulate_clts(ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003299 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003300 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003301 kvm_emulate_wbinvd(ctxt->vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003302 break;
3303 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003304 case 0x0d: /* GrpP (prefetch) */
3305 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02003306 break;
3307 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003308 switch (c->modrm_reg) {
3309 case 1:
3310 case 5 ... 7:
3311 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003312 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003313 goto done;
3314 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003315 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003316 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003318 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3319 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003320 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003321 goto done;
3322 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003323 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003325 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003326 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003327 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003328 goto done;
3329 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003330 c->dst.type = OP_NONE;
3331 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003333 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3334 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003335 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003336 goto done;
3337 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003338
Avi Kivityb27f3852010-08-01 14:25:22 +03003339 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003340 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3341 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3342 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003343 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003344 goto done;
3345 }
3346
Laurent Viviera01af5e2007-09-24 11:10:56 +02003347 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003349 case 0x30:
3350 /* wrmsr */
3351 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3352 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003353 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003354 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003355 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003356 }
3357 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003358 break;
3359 case 0x32:
3360 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003361 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003362 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003363 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003364 } else {
3365 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3366 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3367 }
3368 rc = X86EMUL_CONTINUE;
Avi Kivity018a98d2007-11-27 19:30:56 +02003369 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003370 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003371 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003372 if (rc != X86EMUL_CONTINUE)
3373 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003374 else
3375 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003376 break;
3377 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003378 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003379 if (rc != X86EMUL_CONTINUE)
3380 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003381 else
3382 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003383 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003385 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003386 if (!test_cc(c->b, ctxt->eflags))
3387 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003389 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003390 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003391 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003392 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003393 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003394 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003395 break;
3396 case 0xa1: /* pop fs */
3397 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003398 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003399 goto done;
3400 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003401 case 0xa3:
3402 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003403 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003404 /* only subword offset */
3405 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003406 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003407 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003408 case 0xa4: /* shld imm8, r, r/m */
3409 case 0xa5: /* shld cl, r, r/m */
3410 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3411 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003412 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003413 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003414 break;
3415 case 0xa9: /* pop gs */
3416 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003417 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003418 goto done;
3419 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003420 case 0xab:
3421 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003422 /* only subword offset */
3423 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003424 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003425 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003426 case 0xac: /* shrd imm8, r, r/m */
3427 case 0xad: /* shrd cl, r, r/m */
3428 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3429 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003430 case 0xae: /* clflush */
3431 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 case 0xb0 ... 0xb1: /* cmpxchg */
3433 /*
3434 * Save real source value, then compare EAX against
3435 * destination.
3436 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003437 c->src.orig_val = c->src.val;
3438 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003439 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3440 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003442 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443 } else {
3444 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003445 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003446 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 }
3448 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449 case 0xb3:
3450 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003451 /* only subword offset */
3452 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003453 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003456 c->dst.bytes = c->op_bytes;
3457 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3458 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003461 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 case 0:
3463 goto bt;
3464 case 1:
3465 goto bts;
3466 case 2:
3467 goto btr;
3468 case 3:
3469 goto btc;
3470 }
3471 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003472 case 0xbb:
3473 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003474 /* only subword offset */
3475 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003476 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003477 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003479 c->dst.bytes = c->op_bytes;
3480 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3481 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003483 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003484 c->dst.bytes = c->op_bytes;
3485 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3486 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003487 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003489 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003490 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003491 goto done;
3492 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003493 default:
3494 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495 }
3496 goto writeback;
3497
3498cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003499 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500 return -1;
3501}