blob: eda69411d050c501695fcdb36773ca200e0ebabd [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,
584 struct x86_emulate_ops *ops)
585{
586 struct decode_cache *c = &ctxt->decode;
587 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700588 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900589 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200590
591 if (c->rex_prefix) {
592 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
593 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
594 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
595 }
596
597 c->modrm = insn_fetch(u8, 1, c->eip);
598 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
599 c->modrm_reg |= (c->modrm & 0x38) >> 3;
600 c->modrm_rm |= (c->modrm & 0x07);
601 c->modrm_ea = 0;
Avi Kivity09ee57c2010-08-01 12:07:29 +0300602 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200603
604 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300605 c->modrm_ptr = decode_register(c->modrm_rm,
606 c->regs, c->d & ByteOp);
607 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200608 return rc;
609 }
610
611 if (c->ad_bytes == 2) {
612 unsigned bx = c->regs[VCPU_REGS_RBX];
613 unsigned bp = c->regs[VCPU_REGS_RBP];
614 unsigned si = c->regs[VCPU_REGS_RSI];
615 unsigned di = c->regs[VCPU_REGS_RDI];
616
617 /* 16-bit ModR/M decode. */
618 switch (c->modrm_mod) {
619 case 0:
620 if (c->modrm_rm == 6)
621 c->modrm_ea += insn_fetch(u16, 2, c->eip);
622 break;
623 case 1:
624 c->modrm_ea += insn_fetch(s8, 1, c->eip);
625 break;
626 case 2:
627 c->modrm_ea += insn_fetch(u16, 2, c->eip);
628 break;
629 }
630 switch (c->modrm_rm) {
631 case 0:
632 c->modrm_ea += bx + si;
633 break;
634 case 1:
635 c->modrm_ea += bx + di;
636 break;
637 case 2:
638 c->modrm_ea += bp + si;
639 break;
640 case 3:
641 c->modrm_ea += bp + di;
642 break;
643 case 4:
644 c->modrm_ea += si;
645 break;
646 case 5:
647 c->modrm_ea += di;
648 break;
649 case 6:
650 if (c->modrm_mod != 0)
651 c->modrm_ea += bp;
652 break;
653 case 7:
654 c->modrm_ea += bx;
655 break;
656 }
657 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
658 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300659 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200660 c->modrm_ea = (u16)c->modrm_ea;
661 } else {
662 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700663 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200664 sib = insn_fetch(u8, 1, c->eip);
665 index_reg |= (sib >> 3) & 7;
666 base_reg |= sib & 7;
667 scale = sib >> 6;
668
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700669 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
670 c->modrm_ea += insn_fetch(s32, 4, c->eip);
671 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200672 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700673 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200674 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700675 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
676 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700677 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700678 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200679 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200680 switch (c->modrm_mod) {
681 case 0:
682 if (c->modrm_rm == 5)
683 c->modrm_ea += insn_fetch(s32, 4, c->eip);
684 break;
685 case 1:
686 c->modrm_ea += insn_fetch(s8, 1, c->eip);
687 break;
688 case 2:
689 c->modrm_ea += insn_fetch(s32, 4, c->eip);
690 break;
691 }
692 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200693done:
694 return rc;
695}
696
697static int decode_abs(struct x86_emulate_ctxt *ctxt,
698 struct x86_emulate_ops *ops)
699{
700 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900701 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200702
703 switch (c->ad_bytes) {
704 case 2:
705 c->modrm_ea = insn_fetch(u16, 2, c->eip);
706 break;
707 case 4:
708 c->modrm_ea = insn_fetch(u32, 4, c->eip);
709 break;
710 case 8:
711 c->modrm_ea = insn_fetch(u64, 8, c->eip);
712 break;
713 }
714done:
715 return rc;
716}
717
Gleb Natapov9de41572010-04-28 19:15:22 +0300718static int read_emulated(struct x86_emulate_ctxt *ctxt,
719 struct x86_emulate_ops *ops,
720 unsigned long addr, void *dest, unsigned size)
721{
722 int rc;
723 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300724 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300725
726 while (size) {
727 int n = min(size, 8u);
728 size -= n;
729 if (mc->pos < mc->end)
730 goto read_cached;
731
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300732 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
733 ctxt->vcpu);
734 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300735 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300736 if (rc != X86EMUL_CONTINUE)
737 return rc;
738 mc->end += n;
739
740 read_cached:
741 memcpy(dest, mc->data + mc->pos, n);
742 mc->pos += n;
743 dest += n;
744 addr += n;
745 }
746 return X86EMUL_CONTINUE;
747}
748
Gleb Natapov7b262e92010-03-18 15:20:27 +0200749static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
750 struct x86_emulate_ops *ops,
751 unsigned int size, unsigned short port,
752 void *dest)
753{
754 struct read_cache *rc = &ctxt->decode.io_read;
755
756 if (rc->pos == rc->end) { /* refill pio read ahead */
757 struct decode_cache *c = &ctxt->decode;
758 unsigned int in_page, n;
759 unsigned int count = c->rep_prefix ?
760 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
761 in_page = (ctxt->eflags & EFLG_DF) ?
762 offset_in_page(c->regs[VCPU_REGS_RDI]) :
763 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
764 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
765 count);
766 if (n == 0)
767 n = 1;
768 rc->pos = rc->end = 0;
769 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
770 return 0;
771 rc->end = n * size;
772 }
773
774 memcpy(dest, rc->data + rc->pos, size);
775 rc->pos += size;
776 return 1;
777}
778
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200779static u32 desc_limit_scaled(struct desc_struct *desc)
780{
781 u32 limit = get_desc_limit(desc);
782
783 return desc->g ? (limit << 12) | 0xfff : limit;
784}
785
786static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
787 struct x86_emulate_ops *ops,
788 u16 selector, struct desc_ptr *dt)
789{
790 if (selector & 1 << 2) {
791 struct desc_struct desc;
792 memset (dt, 0, sizeof *dt);
793 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
794 return;
795
796 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
797 dt->address = get_desc_base(&desc);
798 } else
799 ops->get_gdt(dt, ctxt->vcpu);
800}
801
802/* allowed just for 8 bytes segments */
803static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
804 struct x86_emulate_ops *ops,
805 u16 selector, struct desc_struct *desc)
806{
807 struct desc_ptr dt;
808 u16 index = selector >> 3;
809 int ret;
810 u32 err;
811 ulong addr;
812
813 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
814
815 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300816 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200817 return X86EMUL_PROPAGATE_FAULT;
818 }
819 addr = dt.address + index * 8;
820 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
821 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300822 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200823
824 return ret;
825}
826
827/* allowed just for 8 bytes segments */
828static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
829 struct x86_emulate_ops *ops,
830 u16 selector, struct desc_struct *desc)
831{
832 struct desc_ptr dt;
833 u16 index = selector >> 3;
834 u32 err;
835 ulong addr;
836 int ret;
837
838 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
839
840 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300841 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200842 return X86EMUL_PROPAGATE_FAULT;
843 }
844
845 addr = dt.address + index * 8;
846 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
847 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300848 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200849
850 return ret;
851}
852
853static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
854 struct x86_emulate_ops *ops,
855 u16 selector, int seg)
856{
857 struct desc_struct seg_desc;
858 u8 dpl, rpl, cpl;
859 unsigned err_vec = GP_VECTOR;
860 u32 err_code = 0;
861 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
862 int ret;
863
864 memset(&seg_desc, 0, sizeof seg_desc);
865
866 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
867 || ctxt->mode == X86EMUL_MODE_REAL) {
868 /* set real mode segment descriptor */
869 set_desc_base(&seg_desc, selector << 4);
870 set_desc_limit(&seg_desc, 0xffff);
871 seg_desc.type = 3;
872 seg_desc.p = 1;
873 seg_desc.s = 1;
874 goto load;
875 }
876
877 /* NULL selector is not valid for TR, CS and SS */
878 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
879 && null_selector)
880 goto exception;
881
882 /* TR should be in GDT only */
883 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
884 goto exception;
885
886 if (null_selector) /* for NULL selector skip all following checks */
887 goto load;
888
889 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
890 if (ret != X86EMUL_CONTINUE)
891 return ret;
892
893 err_code = selector & 0xfffc;
894 err_vec = GP_VECTOR;
895
896 /* can't load system descriptor into segment selecor */
897 if (seg <= VCPU_SREG_GS && !seg_desc.s)
898 goto exception;
899
900 if (!seg_desc.p) {
901 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
902 goto exception;
903 }
904
905 rpl = selector & 3;
906 dpl = seg_desc.dpl;
907 cpl = ops->cpl(ctxt->vcpu);
908
909 switch (seg) {
910 case VCPU_SREG_SS:
911 /*
912 * segment is not a writable data segment or segment
913 * selector's RPL != CPL or segment selector's RPL != CPL
914 */
915 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
916 goto exception;
917 break;
918 case VCPU_SREG_CS:
919 if (!(seg_desc.type & 8))
920 goto exception;
921
922 if (seg_desc.type & 4) {
923 /* conforming */
924 if (dpl > cpl)
925 goto exception;
926 } else {
927 /* nonconforming */
928 if (rpl > cpl || dpl != cpl)
929 goto exception;
930 }
931 /* CS(RPL) <- CPL */
932 selector = (selector & 0xfffc) | cpl;
933 break;
934 case VCPU_SREG_TR:
935 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
936 goto exception;
937 break;
938 case VCPU_SREG_LDTR:
939 if (seg_desc.s || seg_desc.type != 2)
940 goto exception;
941 break;
942 default: /* DS, ES, FS, or GS */
943 /*
944 * segment is not a data or readable code segment or
945 * ((segment is a data or nonconforming code segment)
946 * and (both RPL and CPL > DPL))
947 */
948 if ((seg_desc.type & 0xa) == 0x8 ||
949 (((seg_desc.type & 0xc) != 0xc) &&
950 (rpl > dpl && cpl > dpl)))
951 goto exception;
952 break;
953 }
954
955 if (seg_desc.s) {
956 /* mark segment as accessed */
957 seg_desc.type |= 1;
958 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
959 if (ret != X86EMUL_CONTINUE)
960 return ret;
961 }
962load:
963 ops->set_segment_selector(selector, seg, ctxt->vcpu);
964 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
965 return X86EMUL_CONTINUE;
966exception:
Gleb Natapov54b84862010-04-28 19:15:44 +0300967 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200968 return X86EMUL_PROPAGATE_FAULT;
969}
970
Wei Yongjunc37eda12010-06-15 09:03:33 +0800971static inline int writeback(struct x86_emulate_ctxt *ctxt,
972 struct x86_emulate_ops *ops)
973{
974 int rc;
975 struct decode_cache *c = &ctxt->decode;
976 u32 err;
977
978 switch (c->dst.type) {
979 case OP_REG:
980 /* The 4-byte case *is* correct:
981 * in 64-bit mode we zero-extend.
982 */
983 switch (c->dst.bytes) {
984 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300985 *(u8 *)c->dst.addr.reg = (u8)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800986 break;
987 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300988 *(u16 *)c->dst.addr.reg = (u16)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800989 break;
990 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300991 *c->dst.addr.reg = (u32)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800992 break; /* 64b: zero-ext */
993 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300994 *c->dst.addr.reg = c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800995 break;
996 }
997 break;
998 case OP_MEM:
999 if (c->lock_prefix)
1000 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001001 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001002 &c->dst.orig_val,
1003 &c->dst.val,
1004 c->dst.bytes,
1005 &err,
1006 ctxt->vcpu);
1007 else
1008 rc = ops->write_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001009 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001010 &c->dst.val,
1011 c->dst.bytes,
1012 &err,
1013 ctxt->vcpu);
1014 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001015 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001016 if (rc != X86EMUL_CONTINUE)
1017 return rc;
1018 break;
1019 case OP_NONE:
1020 /* no writeback */
1021 break;
1022 default:
1023 break;
1024 }
1025 return X86EMUL_CONTINUE;
1026}
1027
Gleb Natapov79168fd2010-04-28 19:15:30 +03001028static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1029 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001030{
1031 struct decode_cache *c = &ctxt->decode;
1032
1033 c->dst.type = OP_MEM;
1034 c->dst.bytes = c->op_bytes;
1035 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001036 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001037 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1038 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001039}
1040
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001041static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001042 struct x86_emulate_ops *ops,
1043 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001044{
1045 struct decode_cache *c = &ctxt->decode;
1046 int rc;
1047
Gleb Natapov79168fd2010-04-28 19:15:30 +03001048 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001049 c->regs[VCPU_REGS_RSP]),
1050 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001051 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001052 return rc;
1053
Avi Kivity350f69d2009-01-05 11:12:40 +02001054 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001055 return rc;
1056}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001057
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001058static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1059 struct x86_emulate_ops *ops,
1060 void *dest, int len)
1061{
1062 int rc;
1063 unsigned long val, change_mask;
1064 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001065 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001066
1067 rc = emulate_pop(ctxt, ops, &val, len);
1068 if (rc != X86EMUL_CONTINUE)
1069 return rc;
1070
1071 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1072 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1073
1074 switch(ctxt->mode) {
1075 case X86EMUL_MODE_PROT64:
1076 case X86EMUL_MODE_PROT32:
1077 case X86EMUL_MODE_PROT16:
1078 if (cpl == 0)
1079 change_mask |= EFLG_IOPL;
1080 if (cpl <= iopl)
1081 change_mask |= EFLG_IF;
1082 break;
1083 case X86EMUL_MODE_VM86:
1084 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001085 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001086 return X86EMUL_PROPAGATE_FAULT;
1087 }
1088 change_mask |= EFLG_IF;
1089 break;
1090 default: /* real mode */
1091 change_mask |= (EFLG_IOPL | EFLG_IF);
1092 break;
1093 }
1094
1095 *(unsigned long *)dest =
1096 (ctxt->eflags & ~change_mask) | (val & change_mask);
1097
1098 return rc;
1099}
1100
Gleb Natapov79168fd2010-04-28 19:15:30 +03001101static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1102 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001103{
1104 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001105
Gleb Natapov79168fd2010-04-28 19:15:30 +03001106 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001107
Gleb Natapov79168fd2010-04-28 19:15:30 +03001108 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001109}
1110
1111static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1112 struct x86_emulate_ops *ops, int seg)
1113{
1114 struct decode_cache *c = &ctxt->decode;
1115 unsigned long selector;
1116 int rc;
1117
1118 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001119 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001120 return rc;
1121
Gleb Natapov2e873022010-03-18 15:20:18 +02001122 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001123 return rc;
1124}
1125
Wei Yongjunc37eda12010-06-15 09:03:33 +08001126static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001127 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001128{
1129 struct decode_cache *c = &ctxt->decode;
1130 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001131 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001132 int reg = VCPU_REGS_RAX;
1133
1134 while (reg <= VCPU_REGS_RDI) {
1135 (reg == VCPU_REGS_RSP) ?
1136 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1137
Gleb Natapov79168fd2010-04-28 19:15:30 +03001138 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001139
1140 rc = writeback(ctxt, ops);
1141 if (rc != X86EMUL_CONTINUE)
1142 return rc;
1143
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001144 ++reg;
1145 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001146
1147 /* Disable writeback. */
1148 c->dst.type = OP_NONE;
1149
1150 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001151}
1152
1153static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1154 struct x86_emulate_ops *ops)
1155{
1156 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001157 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001158 int reg = VCPU_REGS_RDI;
1159
1160 while (reg >= VCPU_REGS_RAX) {
1161 if (reg == VCPU_REGS_RSP) {
1162 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1163 c->op_bytes);
1164 --reg;
1165 }
1166
1167 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001168 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001169 break;
1170 --reg;
1171 }
1172 return rc;
1173}
1174
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001175static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1176 struct x86_emulate_ops *ops)
1177{
1178 struct decode_cache *c = &ctxt->decode;
1179 int rc = X86EMUL_CONTINUE;
1180 unsigned long temp_eip = 0;
1181 unsigned long temp_eflags = 0;
1182 unsigned long cs = 0;
1183 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1184 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1185 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1186 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1187
1188 /* TODO: Add stack limit check */
1189
1190 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1191
1192 if (rc != X86EMUL_CONTINUE)
1193 return rc;
1194
1195 if (temp_eip & ~0xffff) {
1196 emulate_gp(ctxt, 0);
1197 return X86EMUL_PROPAGATE_FAULT;
1198 }
1199
1200 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1201
1202 if (rc != X86EMUL_CONTINUE)
1203 return rc;
1204
1205 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1206
1207 if (rc != X86EMUL_CONTINUE)
1208 return rc;
1209
1210 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1211
1212 if (rc != X86EMUL_CONTINUE)
1213 return rc;
1214
1215 c->eip = temp_eip;
1216
1217
1218 if (c->op_bytes == 4)
1219 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1220 else if (c->op_bytes == 2) {
1221 ctxt->eflags &= ~0xffff;
1222 ctxt->eflags |= temp_eflags;
1223 }
1224
1225 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1226 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1227
1228 return rc;
1229}
1230
1231static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1232 struct x86_emulate_ops* ops)
1233{
1234 switch(ctxt->mode) {
1235 case X86EMUL_MODE_REAL:
1236 return emulate_iret_real(ctxt, ops);
1237 case X86EMUL_MODE_VM86:
1238 case X86EMUL_MODE_PROT16:
1239 case X86EMUL_MODE_PROT32:
1240 case X86EMUL_MODE_PROT64:
1241 default:
1242 /* iret from protected mode unimplemented yet */
1243 return X86EMUL_UNHANDLEABLE;
1244 }
1245}
1246
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001247static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1248 struct x86_emulate_ops *ops)
1249{
1250 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001251
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001252 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253}
1254
Laurent Vivier05f086f2007-09-24 11:10:55 +02001255static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001256{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001257 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001258 switch (c->modrm_reg) {
1259 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001260 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001261 break;
1262 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001263 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001264 break;
1265 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001266 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001267 break;
1268 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001269 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001270 break;
1271 case 4: /* sal/shl */
1272 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001273 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001274 break;
1275 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001276 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001277 break;
1278 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001279 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001280 break;
1281 }
1282}
1283
1284static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001285 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001286{
1287 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001288
1289 switch (c->modrm_reg) {
1290 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001291 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001292 break;
1293 case 2: /* not */
1294 c->dst.val = ~c->dst.val;
1295 break;
1296 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001297 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001298 break;
1299 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001300 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001301 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001302 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001303}
1304
1305static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001306 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001307{
1308 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001309
1310 switch (c->modrm_reg) {
1311 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001312 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001313 break;
1314 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001315 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001316 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001317 case 2: /* call near abs */ {
1318 long int old_eip;
1319 old_eip = c->eip;
1320 c->eip = c->src.val;
1321 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001322 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001323 break;
1324 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001325 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001326 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001327 break;
1328 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001329 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001330 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001331 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001332 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001333}
1334
1335static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001336 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001337{
1338 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001339 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001340
1341 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1342 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001343 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1344 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001345 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001347 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1348 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001349
Laurent Vivier05f086f2007-09-24 11:10:55 +02001350 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001351 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001352 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001353}
1354
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001355static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1356 struct x86_emulate_ops *ops)
1357{
1358 struct decode_cache *c = &ctxt->decode;
1359 int rc;
1360 unsigned long cs;
1361
1362 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001363 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001364 return rc;
1365 if (c->op_bytes == 4)
1366 c->eip = (u32)c->eip;
1367 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001368 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001369 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001370 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001371 return rc;
1372}
1373
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001374static inline void
1375setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001376 struct x86_emulate_ops *ops, struct desc_struct *cs,
1377 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001378{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001379 memset(cs, 0, sizeof(struct desc_struct));
1380 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1381 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001382
1383 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001384 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001385 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001386 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001387 cs->type = 0x0b; /* Read, Execute, Accessed */
1388 cs->s = 1;
1389 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001390 cs->p = 1;
1391 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001392
Gleb Natapov79168fd2010-04-28 19:15:30 +03001393 set_desc_base(ss, 0); /* flat segment */
1394 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001395 ss->g = 1; /* 4kb granularity */
1396 ss->s = 1;
1397 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001398 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001399 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001400 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001401}
1402
1403static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001404emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001405{
1406 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001407 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001408 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001409 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001410
1411 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001412 if (ctxt->mode == X86EMUL_MODE_REAL ||
1413 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001414 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001415 return X86EMUL_PROPAGATE_FAULT;
1416 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001417
Gleb Natapov79168fd2010-04-28 19:15:30 +03001418 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001419
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001420 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001421 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001422 cs_sel = (u16)(msr_data & 0xfffc);
1423 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001424
1425 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001426 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001427 cs.l = 1;
1428 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001429 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1430 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1431 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1432 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001433
1434 c->regs[VCPU_REGS_RCX] = c->eip;
1435 if (is_long_mode(ctxt->vcpu)) {
1436#ifdef CONFIG_X86_64
1437 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1438
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001439 ops->get_msr(ctxt->vcpu,
1440 ctxt->mode == X86EMUL_MODE_PROT64 ?
1441 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001442 c->eip = msr_data;
1443
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001444 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001445 ctxt->eflags &= ~(msr_data | EFLG_RF);
1446#endif
1447 } else {
1448 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001449 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001450 c->eip = (u32)msr_data;
1451
1452 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1453 }
1454
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001455 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001456}
1457
Andre Przywara8c604352009-06-18 12:56:01 +02001458static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001459emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001460{
1461 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001462 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001463 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001464 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001465
Gleb Natapova0044752010-02-10 14:21:31 +02001466 /* inject #GP if in real mode */
1467 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001468 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001469 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001470 }
1471
1472 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1473 * Therefore, we inject an #UD.
1474 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001475 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001476 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001477 return X86EMUL_PROPAGATE_FAULT;
1478 }
Andre Przywara8c604352009-06-18 12:56:01 +02001479
Gleb Natapov79168fd2010-04-28 19:15:30 +03001480 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001481
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001482 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001483 switch (ctxt->mode) {
1484 case X86EMUL_MODE_PROT32:
1485 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001486 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001487 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001488 }
1489 break;
1490 case X86EMUL_MODE_PROT64:
1491 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001492 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001493 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001494 }
1495 break;
1496 }
1497
1498 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001499 cs_sel = (u16)msr_data;
1500 cs_sel &= ~SELECTOR_RPL_MASK;
1501 ss_sel = cs_sel + 8;
1502 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001503 if (ctxt->mode == X86EMUL_MODE_PROT64
1504 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001505 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001506 cs.l = 1;
1507 }
1508
Gleb Natapov79168fd2010-04-28 19:15:30 +03001509 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1510 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1511 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1512 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001513
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001514 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001515 c->eip = msr_data;
1516
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001517 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001518 c->regs[VCPU_REGS_RSP] = msr_data;
1519
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001520 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001521}
1522
Andre Przywara4668f052009-06-18 12:56:02 +02001523static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001524emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001525{
1526 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001527 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001528 u64 msr_data;
1529 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001530 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001531
Gleb Natapova0044752010-02-10 14:21:31 +02001532 /* inject #GP if in real mode or Virtual 8086 mode */
1533 if (ctxt->mode == X86EMUL_MODE_REAL ||
1534 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001535 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001536 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001537 }
1538
Gleb Natapov79168fd2010-04-28 19:15:30 +03001539 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001540
1541 if ((c->rex_prefix & 0x8) != 0x0)
1542 usermode = X86EMUL_MODE_PROT64;
1543 else
1544 usermode = X86EMUL_MODE_PROT32;
1545
1546 cs.dpl = 3;
1547 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001548 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001549 switch (usermode) {
1550 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001551 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001552 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001553 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001554 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001555 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001556 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001557 break;
1558 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001559 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001560 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001561 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001562 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001563 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001564 ss_sel = cs_sel + 8;
1565 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001566 cs.l = 1;
1567 break;
1568 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001569 cs_sel |= SELECTOR_RPL_MASK;
1570 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001571
Gleb Natapov79168fd2010-04-28 19:15:30 +03001572 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1573 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1574 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1575 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001576
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001577 c->eip = c->regs[VCPU_REGS_RDX];
1578 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001579
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001580 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001581}
1582
Gleb Natapov9c537242010-03-18 15:20:05 +02001583static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1584 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001585{
1586 int iopl;
1587 if (ctxt->mode == X86EMUL_MODE_REAL)
1588 return false;
1589 if (ctxt->mode == X86EMUL_MODE_VM86)
1590 return true;
1591 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001592 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001593}
1594
1595static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1596 struct x86_emulate_ops *ops,
1597 u16 port, u16 len)
1598{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001599 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001600 int r;
1601 u16 io_bitmap_ptr;
1602 u8 perm, bit_idx = port & 0x7;
1603 unsigned mask = (1 << len) - 1;
1604
Gleb Natapov79168fd2010-04-28 19:15:30 +03001605 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1606 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001607 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001608 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001609 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001610 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1611 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001612 if (r != X86EMUL_CONTINUE)
1613 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001614 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001615 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001616 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1617 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001618 if (r != X86EMUL_CONTINUE)
1619 return false;
1620 if ((perm >> bit_idx) & mask)
1621 return false;
1622 return true;
1623}
1624
1625static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1626 struct x86_emulate_ops *ops,
1627 u16 port, u16 len)
1628{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001629 if (ctxt->perm_ok)
1630 return true;
1631
Gleb Natapov9c537242010-03-18 15:20:05 +02001632 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001633 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1634 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001635
1636 ctxt->perm_ok = true;
1637
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001638 return true;
1639}
1640
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001641static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1642 struct x86_emulate_ops *ops,
1643 struct tss_segment_16 *tss)
1644{
1645 struct decode_cache *c = &ctxt->decode;
1646
1647 tss->ip = c->eip;
1648 tss->flag = ctxt->eflags;
1649 tss->ax = c->regs[VCPU_REGS_RAX];
1650 tss->cx = c->regs[VCPU_REGS_RCX];
1651 tss->dx = c->regs[VCPU_REGS_RDX];
1652 tss->bx = c->regs[VCPU_REGS_RBX];
1653 tss->sp = c->regs[VCPU_REGS_RSP];
1654 tss->bp = c->regs[VCPU_REGS_RBP];
1655 tss->si = c->regs[VCPU_REGS_RSI];
1656 tss->di = c->regs[VCPU_REGS_RDI];
1657
1658 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1659 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1660 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1661 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1662 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1663}
1664
1665static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1666 struct x86_emulate_ops *ops,
1667 struct tss_segment_16 *tss)
1668{
1669 struct decode_cache *c = &ctxt->decode;
1670 int ret;
1671
1672 c->eip = tss->ip;
1673 ctxt->eflags = tss->flag | 2;
1674 c->regs[VCPU_REGS_RAX] = tss->ax;
1675 c->regs[VCPU_REGS_RCX] = tss->cx;
1676 c->regs[VCPU_REGS_RDX] = tss->dx;
1677 c->regs[VCPU_REGS_RBX] = tss->bx;
1678 c->regs[VCPU_REGS_RSP] = tss->sp;
1679 c->regs[VCPU_REGS_RBP] = tss->bp;
1680 c->regs[VCPU_REGS_RSI] = tss->si;
1681 c->regs[VCPU_REGS_RDI] = tss->di;
1682
1683 /*
1684 * SDM says that segment selectors are loaded before segment
1685 * descriptors
1686 */
1687 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1688 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1689 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1690 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1691 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1692
1693 /*
1694 * Now load segment descriptors. If fault happenes at this stage
1695 * it is handled in a context of new task
1696 */
1697 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1698 if (ret != X86EMUL_CONTINUE)
1699 return ret;
1700 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1701 if (ret != X86EMUL_CONTINUE)
1702 return ret;
1703 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1704 if (ret != X86EMUL_CONTINUE)
1705 return ret;
1706 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1707 if (ret != X86EMUL_CONTINUE)
1708 return ret;
1709 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1710 if (ret != X86EMUL_CONTINUE)
1711 return ret;
1712
1713 return X86EMUL_CONTINUE;
1714}
1715
1716static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1717 struct x86_emulate_ops *ops,
1718 u16 tss_selector, u16 old_tss_sel,
1719 ulong old_tss_base, struct desc_struct *new_desc)
1720{
1721 struct tss_segment_16 tss_seg;
1722 int ret;
1723 u32 err, new_tss_base = get_desc_base(new_desc);
1724
1725 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1726 &err);
1727 if (ret == X86EMUL_PROPAGATE_FAULT) {
1728 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001729 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001730 return ret;
1731 }
1732
1733 save_state_to_tss16(ctxt, ops, &tss_seg);
1734
1735 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1736 &err);
1737 if (ret == X86EMUL_PROPAGATE_FAULT) {
1738 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001739 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001740 return ret;
1741 }
1742
1743 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1744 &err);
1745 if (ret == X86EMUL_PROPAGATE_FAULT) {
1746 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001747 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001748 return ret;
1749 }
1750
1751 if (old_tss_sel != 0xffff) {
1752 tss_seg.prev_task_link = old_tss_sel;
1753
1754 ret = ops->write_std(new_tss_base,
1755 &tss_seg.prev_task_link,
1756 sizeof tss_seg.prev_task_link,
1757 ctxt->vcpu, &err);
1758 if (ret == X86EMUL_PROPAGATE_FAULT) {
1759 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001760 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001761 return ret;
1762 }
1763 }
1764
1765 return load_state_from_tss16(ctxt, ops, &tss_seg);
1766}
1767
1768static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1769 struct x86_emulate_ops *ops,
1770 struct tss_segment_32 *tss)
1771{
1772 struct decode_cache *c = &ctxt->decode;
1773
1774 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1775 tss->eip = c->eip;
1776 tss->eflags = ctxt->eflags;
1777 tss->eax = c->regs[VCPU_REGS_RAX];
1778 tss->ecx = c->regs[VCPU_REGS_RCX];
1779 tss->edx = c->regs[VCPU_REGS_RDX];
1780 tss->ebx = c->regs[VCPU_REGS_RBX];
1781 tss->esp = c->regs[VCPU_REGS_RSP];
1782 tss->ebp = c->regs[VCPU_REGS_RBP];
1783 tss->esi = c->regs[VCPU_REGS_RSI];
1784 tss->edi = c->regs[VCPU_REGS_RDI];
1785
1786 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1787 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1788 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1789 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1790 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1791 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1792 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1793}
1794
1795static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1796 struct x86_emulate_ops *ops,
1797 struct tss_segment_32 *tss)
1798{
1799 struct decode_cache *c = &ctxt->decode;
1800 int ret;
1801
Gleb Natapov0f122442010-04-28 19:15:31 +03001802 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001803 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001804 return X86EMUL_PROPAGATE_FAULT;
1805 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001806 c->eip = tss->eip;
1807 ctxt->eflags = tss->eflags | 2;
1808 c->regs[VCPU_REGS_RAX] = tss->eax;
1809 c->regs[VCPU_REGS_RCX] = tss->ecx;
1810 c->regs[VCPU_REGS_RDX] = tss->edx;
1811 c->regs[VCPU_REGS_RBX] = tss->ebx;
1812 c->regs[VCPU_REGS_RSP] = tss->esp;
1813 c->regs[VCPU_REGS_RBP] = tss->ebp;
1814 c->regs[VCPU_REGS_RSI] = tss->esi;
1815 c->regs[VCPU_REGS_RDI] = tss->edi;
1816
1817 /*
1818 * SDM says that segment selectors are loaded before segment
1819 * descriptors
1820 */
1821 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1822 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1823 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1824 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1825 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1826 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1827 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1828
1829 /*
1830 * Now load segment descriptors. If fault happenes at this stage
1831 * it is handled in a context of new task
1832 */
1833 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1834 if (ret != X86EMUL_CONTINUE)
1835 return ret;
1836 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1837 if (ret != X86EMUL_CONTINUE)
1838 return ret;
1839 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1840 if (ret != X86EMUL_CONTINUE)
1841 return ret;
1842 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1843 if (ret != X86EMUL_CONTINUE)
1844 return ret;
1845 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1846 if (ret != X86EMUL_CONTINUE)
1847 return ret;
1848 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1849 if (ret != X86EMUL_CONTINUE)
1850 return ret;
1851 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1852 if (ret != X86EMUL_CONTINUE)
1853 return ret;
1854
1855 return X86EMUL_CONTINUE;
1856}
1857
1858static int task_switch_32(struct x86_emulate_ctxt *ctxt,
1859 struct x86_emulate_ops *ops,
1860 u16 tss_selector, u16 old_tss_sel,
1861 ulong old_tss_base, struct desc_struct *new_desc)
1862{
1863 struct tss_segment_32 tss_seg;
1864 int ret;
1865 u32 err, new_tss_base = get_desc_base(new_desc);
1866
1867 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1868 &err);
1869 if (ret == X86EMUL_PROPAGATE_FAULT) {
1870 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001871 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001872 return ret;
1873 }
1874
1875 save_state_to_tss32(ctxt, ops, &tss_seg);
1876
1877 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1878 &err);
1879 if (ret == X86EMUL_PROPAGATE_FAULT) {
1880 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001881 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001882 return ret;
1883 }
1884
1885 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1886 &err);
1887 if (ret == X86EMUL_PROPAGATE_FAULT) {
1888 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001889 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001890 return ret;
1891 }
1892
1893 if (old_tss_sel != 0xffff) {
1894 tss_seg.prev_task_link = old_tss_sel;
1895
1896 ret = ops->write_std(new_tss_base,
1897 &tss_seg.prev_task_link,
1898 sizeof tss_seg.prev_task_link,
1899 ctxt->vcpu, &err);
1900 if (ret == X86EMUL_PROPAGATE_FAULT) {
1901 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001902 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001903 return ret;
1904 }
1905 }
1906
1907 return load_state_from_tss32(ctxt, ops, &tss_seg);
1908}
1909
1910static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001911 struct x86_emulate_ops *ops,
1912 u16 tss_selector, int reason,
1913 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001914{
1915 struct desc_struct curr_tss_desc, next_tss_desc;
1916 int ret;
1917 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
1918 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03001919 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02001920 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001921
1922 /* FIXME: old_tss_base == ~0 ? */
1923
1924 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
1925 if (ret != X86EMUL_CONTINUE)
1926 return ret;
1927 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
1928 if (ret != X86EMUL_CONTINUE)
1929 return ret;
1930
1931 /* FIXME: check that next_tss_desc is tss */
1932
1933 if (reason != TASK_SWITCH_IRET) {
1934 if ((tss_selector & 3) > next_tss_desc.dpl ||
1935 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001936 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001937 return X86EMUL_PROPAGATE_FAULT;
1938 }
1939 }
1940
Gleb Natapovceffb452010-03-18 15:20:19 +02001941 desc_limit = desc_limit_scaled(&next_tss_desc);
1942 if (!next_tss_desc.p ||
1943 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
1944 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001945 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001946 return X86EMUL_PROPAGATE_FAULT;
1947 }
1948
1949 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
1950 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
1951 write_segment_descriptor(ctxt, ops, old_tss_sel,
1952 &curr_tss_desc);
1953 }
1954
1955 if (reason == TASK_SWITCH_IRET)
1956 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
1957
1958 /* set back link to prev task only if NT bit is set in eflags
1959 note that old_tss_sel is not used afetr this point */
1960 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
1961 old_tss_sel = 0xffff;
1962
1963 if (next_tss_desc.type & 8)
1964 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
1965 old_tss_base, &next_tss_desc);
1966 else
1967 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
1968 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02001969 if (ret != X86EMUL_CONTINUE)
1970 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001971
1972 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
1973 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
1974
1975 if (reason != TASK_SWITCH_IRET) {
1976 next_tss_desc.type |= (1 << 1); /* set busy flag */
1977 write_segment_descriptor(ctxt, ops, tss_selector,
1978 &next_tss_desc);
1979 }
1980
1981 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
1982 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
1983 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
1984
Jan Kiszkae269fb22010-04-14 15:51:09 +02001985 if (has_error_code) {
1986 struct decode_cache *c = &ctxt->decode;
1987
1988 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
1989 c->lock_prefix = 0;
1990 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001991 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02001992 }
1993
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001994 return ret;
1995}
1996
1997int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001998 u16 tss_selector, int reason,
1999 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002000{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002001 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002002 struct decode_cache *c = &ctxt->decode;
2003 int rc;
2004
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002005 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002006 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002007
Jan Kiszkae269fb22010-04-14 15:51:09 +02002008 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2009 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002010
2011 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002012 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002013 if (rc == X86EMUL_CONTINUE)
2014 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002015 }
2016
Gleb Natapov19d04432010-04-15 12:29:50 +03002017 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002018}
2019
Gleb Natapova682e352010-03-18 15:20:21 +02002020static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002021 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002022{
2023 struct decode_cache *c = &ctxt->decode;
2024 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2025
Gleb Natapovd9271122010-03-18 15:20:22 +02002026 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002027 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002028}
2029
Avi Kivity63540382010-07-29 15:11:55 +03002030static int em_push(struct x86_emulate_ctxt *ctxt)
2031{
2032 emulate_push(ctxt, ctxt->ops);
2033 return X86EMUL_CONTINUE;
2034}
2035
Avi Kivity73fba5f2010-07-29 15:11:53 +03002036#define D(_y) { .flags = (_y) }
2037#define N D(0)
2038#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2039#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2040#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2041
2042static struct opcode group1[] = {
2043 X7(D(Lock)), N
2044};
2045
2046static struct opcode group1A[] = {
2047 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2048};
2049
2050static struct opcode group3[] = {
2051 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2052 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2053 X4(D(Undefined)),
2054};
2055
2056static struct opcode group4[] = {
2057 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2058 N, N, N, N, N, N,
2059};
2060
2061static struct opcode group5[] = {
2062 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2063 D(SrcMem | ModRM | Stack), N,
2064 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2065 D(SrcMem | ModRM | Stack), N,
2066};
2067
2068static struct group_dual group7 = { {
2069 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2070 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002071 D(SrcMem16 | ModRM | Mov | Priv),
2072 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002073}, {
2074 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2075 D(SrcNone | ModRM | DstMem | Mov), N,
2076 D(SrcMem16 | ModRM | Mov | Priv), N,
2077} };
2078
2079static struct opcode group8[] = {
2080 N, N, N, N,
2081 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2082 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2083};
2084
2085static struct group_dual group9 = { {
2086 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2087}, {
2088 N, N, N, N, N, N, N, N,
2089} };
2090
2091static struct opcode opcode_table[256] = {
2092 /* 0x00 - 0x07 */
2093 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2094 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2095 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2096 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2097 /* 0x08 - 0x0F */
2098 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2099 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2100 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2101 D(ImplicitOps | Stack | No64), N,
2102 /* 0x10 - 0x17 */
2103 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2104 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2105 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2106 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2107 /* 0x18 - 0x1F */
2108 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2109 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2110 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2111 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2112 /* 0x20 - 0x27 */
2113 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2114 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2115 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2116 /* 0x28 - 0x2F */
2117 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2118 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2119 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2120 /* 0x30 - 0x37 */
2121 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2122 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2123 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2124 /* 0x38 - 0x3F */
2125 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2126 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2127 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2128 N, N,
2129 /* 0x40 - 0x4F */
2130 X16(D(DstReg)),
2131 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002132 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002133 /* 0x58 - 0x5F */
2134 X8(D(DstReg | Stack)),
2135 /* 0x60 - 0x67 */
2136 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2137 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2138 N, N, N, N,
2139 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002140 I(SrcImm | Mov | Stack, em_push), N,
2141 I(SrcImmByte | Mov | Stack, em_push), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002142 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2143 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2144 /* 0x70 - 0x7F */
2145 X16(D(SrcImmByte)),
2146 /* 0x80 - 0x87 */
2147 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2148 G(DstMem | SrcImm | ModRM | Group, group1),
2149 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2150 G(DstMem | SrcImmByte | ModRM | Group, group1),
2151 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2152 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2153 /* 0x88 - 0x8F */
2154 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2155 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002156 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002157 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2158 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002159 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002160 /* 0x98 - 0x9F */
2161 N, N, D(SrcImmFAddr | No64), N,
2162 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2163 /* 0xA0 - 0xA7 */
2164 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2165 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2166 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2167 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2168 /* 0xA8 - 0xAF */
2169 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
2170 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
2171 D(ByteOp | DstDI | String), D(DstDI | String),
2172 /* 0xB0 - 0xB7 */
2173 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2174 /* 0xB8 - 0xBF */
2175 X8(D(DstReg | SrcImm | Mov)),
2176 /* 0xC0 - 0xC7 */
2177 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
2178 N, D(ImplicitOps | Stack), N, N,
2179 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2180 /* 0xC8 - 0xCF */
2181 N, N, N, D(ImplicitOps | Stack),
2182 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2183 /* 0xD0 - 0xD7 */
2184 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2185 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2186 N, N, N, N,
2187 /* 0xD8 - 0xDF */
2188 N, N, N, N, N, N, N, N,
2189 /* 0xE0 - 0xE7 */
2190 N, N, N, N,
2191 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2192 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2193 /* 0xE8 - 0xEF */
2194 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2195 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2196 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2197 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2198 /* 0xF0 - 0xF7 */
2199 N, N, N, N,
2200 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2201 /* 0xF8 - 0xFF */
2202 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
2203 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2204};
2205
2206static struct opcode twobyte_table[256] = {
2207 /* 0x00 - 0x0F */
2208 N, GD(0, &group7), N, N,
2209 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2210 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2211 N, D(ImplicitOps | ModRM), N, N,
2212 /* 0x10 - 0x1F */
2213 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2214 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002215 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2216 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002217 N, N, N, N,
2218 N, N, N, N, N, N, N, N,
2219 /* 0x30 - 0x3F */
2220 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
2221 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2222 N, N, N, N, N, N, N, N,
2223 /* 0x40 - 0x4F */
2224 X16(D(DstReg | SrcMem | ModRM | Mov)),
2225 /* 0x50 - 0x5F */
2226 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2227 /* 0x60 - 0x6F */
2228 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2229 /* 0x70 - 0x7F */
2230 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2231 /* 0x80 - 0x8F */
2232 X16(D(SrcImm)),
2233 /* 0x90 - 0x9F */
2234 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2235 /* 0xA0 - 0xA7 */
2236 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2237 N, D(DstMem | SrcReg | ModRM | BitOp),
2238 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2239 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2240 /* 0xA8 - 0xAF */
2241 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2242 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2243 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2244 D(DstMem | SrcReg | Src2CL | ModRM),
2245 D(ModRM), N,
2246 /* 0xB0 - 0xB7 */
2247 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2248 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2249 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2250 D(DstReg | SrcMem16 | ModRM | Mov),
2251 /* 0xB8 - 0xBF */
2252 N, N,
2253 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2254 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2255 D(DstReg | SrcMem16 | ModRM | Mov),
2256 /* 0xC0 - 0xCF */
2257 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
2258 N, N, N, GD(0, &group9),
2259 N, N, N, N, N, N, N, N,
2260 /* 0xD0 - 0xDF */
2261 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2262 /* 0xE0 - 0xEF */
2263 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2264 /* 0xF0 - 0xFF */
2265 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2266};
2267
2268#undef D
2269#undef N
2270#undef G
2271#undef GD
2272#undef I
2273
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002274int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002275x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2276{
2277 struct x86_emulate_ops *ops = ctxt->ops;
2278 struct decode_cache *c = &ctxt->decode;
2279 int rc = X86EMUL_CONTINUE;
2280 int mode = ctxt->mode;
2281 int def_op_bytes, def_ad_bytes, dual, goffset;
2282 struct opcode opcode, *g_mod012, *g_mod3;
2283
2284 /* we cannot decode insn before we complete previous rep insn */
2285 WARN_ON(ctxt->restart);
2286
2287 c->eip = ctxt->eip;
2288 c->fetch.start = c->fetch.end = c->eip;
2289 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2290
2291 switch (mode) {
2292 case X86EMUL_MODE_REAL:
2293 case X86EMUL_MODE_VM86:
2294 case X86EMUL_MODE_PROT16:
2295 def_op_bytes = def_ad_bytes = 2;
2296 break;
2297 case X86EMUL_MODE_PROT32:
2298 def_op_bytes = def_ad_bytes = 4;
2299 break;
2300#ifdef CONFIG_X86_64
2301 case X86EMUL_MODE_PROT64:
2302 def_op_bytes = 4;
2303 def_ad_bytes = 8;
2304 break;
2305#endif
2306 default:
2307 return -1;
2308 }
2309
2310 c->op_bytes = def_op_bytes;
2311 c->ad_bytes = def_ad_bytes;
2312
2313 /* Legacy prefixes. */
2314 for (;;) {
2315 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2316 case 0x66: /* operand-size override */
2317 /* switch between 2/4 bytes */
2318 c->op_bytes = def_op_bytes ^ 6;
2319 break;
2320 case 0x67: /* address-size override */
2321 if (mode == X86EMUL_MODE_PROT64)
2322 /* switch between 4/8 bytes */
2323 c->ad_bytes = def_ad_bytes ^ 12;
2324 else
2325 /* switch between 2/4 bytes */
2326 c->ad_bytes = def_ad_bytes ^ 6;
2327 break;
2328 case 0x26: /* ES override */
2329 case 0x2e: /* CS override */
2330 case 0x36: /* SS override */
2331 case 0x3e: /* DS override */
2332 set_seg_override(c, (c->b >> 3) & 3);
2333 break;
2334 case 0x64: /* FS override */
2335 case 0x65: /* GS override */
2336 set_seg_override(c, c->b & 7);
2337 break;
2338 case 0x40 ... 0x4f: /* REX */
2339 if (mode != X86EMUL_MODE_PROT64)
2340 goto done_prefixes;
2341 c->rex_prefix = c->b;
2342 continue;
2343 case 0xf0: /* LOCK */
2344 c->lock_prefix = 1;
2345 break;
2346 case 0xf2: /* REPNE/REPNZ */
2347 c->rep_prefix = REPNE_PREFIX;
2348 break;
2349 case 0xf3: /* REP/REPE/REPZ */
2350 c->rep_prefix = REPE_PREFIX;
2351 break;
2352 default:
2353 goto done_prefixes;
2354 }
2355
2356 /* Any legacy prefix after a REX prefix nullifies its effect. */
2357
2358 c->rex_prefix = 0;
2359 }
2360
2361done_prefixes:
2362
2363 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002364 if (c->rex_prefix & 8)
2365 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002366
2367 /* Opcode byte(s). */
2368 opcode = opcode_table[c->b];
2369 if (opcode.flags == 0) {
2370 /* Two-byte opcode? */
2371 if (c->b == 0x0f) {
2372 c->twobyte = 1;
2373 c->b = insn_fetch(u8, 1, c->eip);
2374 opcode = twobyte_table[c->b];
2375 }
2376 }
2377 c->d = opcode.flags;
2378
2379 if (c->d & Group) {
2380 dual = c->d & GroupDual;
2381 c->modrm = insn_fetch(u8, 1, c->eip);
2382 --c->eip;
2383
2384 if (c->d & GroupDual) {
2385 g_mod012 = opcode.u.gdual->mod012;
2386 g_mod3 = opcode.u.gdual->mod3;
2387 } else
2388 g_mod012 = g_mod3 = opcode.u.group;
2389
2390 c->d &= ~(Group | GroupDual);
2391
2392 goffset = (c->modrm >> 3) & 7;
2393
2394 if ((c->modrm >> 6) == 3)
2395 opcode = g_mod3[goffset];
2396 else
2397 opcode = g_mod012[goffset];
2398 c->d |= opcode.flags;
2399 }
2400
2401 c->execute = opcode.u.execute;
2402
2403 /* Unrecognised? */
2404 if (c->d == 0 || (c->d & Undefined)) {
2405 DPRINTF("Cannot emulate %02x\n", c->b);
2406 return -1;
2407 }
2408
2409 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2410 c->op_bytes = 8;
2411
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002412 if (c->d & Op3264) {
2413 if (mode == X86EMUL_MODE_PROT64)
2414 c->op_bytes = 8;
2415 else
2416 c->op_bytes = 4;
2417 }
2418
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002419 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002420 if (c->d & ModRM) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002421 rc = decode_modrm(ctxt, ops);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002422 if (!c->has_seg_override)
2423 set_seg_override(c, c->modrm_seg);
2424 } else if (c->d & MemAbs)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002425 rc = decode_abs(ctxt, ops);
2426 if (rc != X86EMUL_CONTINUE)
2427 goto done;
2428
2429 if (!c->has_seg_override)
2430 set_seg_override(c, VCPU_SREG_DS);
2431
2432 if (!(!c->twobyte && c->b == 0x8d))
2433 c->modrm_ea += seg_override_base(ctxt, ops, c);
2434
2435 if (c->ad_bytes != 8)
2436 c->modrm_ea = (u32)c->modrm_ea;
2437
2438 if (c->rip_relative)
2439 c->modrm_ea += c->eip;
2440
2441 /*
2442 * Decode and fetch the source operand: register, memory
2443 * or immediate.
2444 */
2445 switch (c->d & SrcMask) {
2446 case SrcNone:
2447 break;
2448 case SrcReg:
2449 decode_register_operand(&c->src, c, 0);
2450 break;
2451 case SrcMem16:
2452 c->src.bytes = 2;
2453 goto srcmem_common;
2454 case SrcMem32:
2455 c->src.bytes = 4;
2456 goto srcmem_common;
2457 case SrcMem:
2458 c->src.bytes = (c->d & ByteOp) ? 1 :
2459 c->op_bytes;
2460 /* Don't fetch the address for invlpg: it could be unmapped. */
Avi Kivity5a506b12010-08-01 15:10:29 +03002461 if (c->d & NoAccess)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002462 break;
2463 srcmem_common:
2464 /*
2465 * For instructions with a ModR/M byte, switch to register
2466 * access if Mod = 3.
2467 */
2468 if ((c->d & ModRM) && c->modrm_mod == 3) {
2469 c->src.type = OP_REG;
2470 c->src.val = c->modrm_val;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002471 c->src.addr.reg = c->modrm_ptr;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002472 break;
2473 }
2474 c->src.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002475 c->src.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002476 c->src.val = 0;
2477 break;
2478 case SrcImm:
2479 case SrcImmU:
2480 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002481 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002482 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2483 if (c->src.bytes == 8)
2484 c->src.bytes = 4;
2485 /* NB. Immediates are sign-extended as necessary. */
2486 switch (c->src.bytes) {
2487 case 1:
2488 c->src.val = insn_fetch(s8, 1, c->eip);
2489 break;
2490 case 2:
2491 c->src.val = insn_fetch(s16, 2, c->eip);
2492 break;
2493 case 4:
2494 c->src.val = insn_fetch(s32, 4, c->eip);
2495 break;
2496 }
2497 if ((c->d & SrcMask) == SrcImmU) {
2498 switch (c->src.bytes) {
2499 case 1:
2500 c->src.val &= 0xff;
2501 break;
2502 case 2:
2503 c->src.val &= 0xffff;
2504 break;
2505 case 4:
2506 c->src.val &= 0xffffffff;
2507 break;
2508 }
2509 }
2510 break;
2511 case SrcImmByte:
2512 case SrcImmUByte:
2513 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002514 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002515 c->src.bytes = 1;
2516 if ((c->d & SrcMask) == SrcImmByte)
2517 c->src.val = insn_fetch(s8, 1, c->eip);
2518 else
2519 c->src.val = insn_fetch(u8, 1, c->eip);
2520 break;
2521 case SrcAcc:
2522 c->src.type = OP_REG;
2523 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002524 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002525 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002526 break;
2527 case SrcOne:
2528 c->src.bytes = 1;
2529 c->src.val = 1;
2530 break;
2531 case SrcSI:
2532 c->src.type = OP_MEM;
2533 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002534 c->src.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002535 register_address(c, seg_override_base(ctxt, ops, c),
2536 c->regs[VCPU_REGS_RSI]);
2537 c->src.val = 0;
2538 break;
2539 case SrcImmFAddr:
2540 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002541 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002542 c->src.bytes = c->op_bytes + 2;
2543 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2544 break;
2545 case SrcMemFAddr:
2546 c->src.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002547 c->src.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002548 c->src.bytes = c->op_bytes + 2;
2549 break;
2550 }
2551
2552 /*
2553 * Decode and fetch the second source operand: register, memory
2554 * or immediate.
2555 */
2556 switch (c->d & Src2Mask) {
2557 case Src2None:
2558 break;
2559 case Src2CL:
2560 c->src2.bytes = 1;
2561 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2562 break;
2563 case Src2ImmByte:
2564 c->src2.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002565 c->src2.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002566 c->src2.bytes = 1;
2567 c->src2.val = insn_fetch(u8, 1, c->eip);
2568 break;
2569 case Src2One:
2570 c->src2.bytes = 1;
2571 c->src2.val = 1;
2572 break;
2573 }
2574
2575 /* Decode and fetch the destination operand: register or memory. */
2576 switch (c->d & DstMask) {
2577 case ImplicitOps:
2578 /* Special instructions do their own operand decoding. */
2579 return 0;
2580 case DstReg:
2581 decode_register_operand(&c->dst, c,
2582 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2583 break;
2584 case DstMem:
2585 case DstMem64:
2586 if ((c->d & ModRM) && c->modrm_mod == 3) {
2587 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2588 c->dst.type = OP_REG;
2589 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002590 c->dst.addr.reg = c->modrm_ptr;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002591 break;
2592 }
2593 c->dst.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002594 c->dst.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002595 if ((c->d & DstMask) == DstMem64)
2596 c->dst.bytes = 8;
2597 else
2598 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2599 c->dst.val = 0;
2600 if (c->d & BitOp) {
2601 unsigned long mask = ~(c->dst.bytes * 8 - 1);
2602
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002603 c->dst.addr.mem = c->dst.addr.mem +
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002604 (c->src.val & mask) / 8;
2605 }
2606 break;
2607 case DstAcc:
2608 c->dst.type = OP_REG;
2609 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002610 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002611 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002612 c->dst.orig_val = c->dst.val;
2613 break;
2614 case DstDI:
2615 c->dst.type = OP_MEM;
2616 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002617 c->dst.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002618 register_address(c, es_base(ctxt, ops),
2619 c->regs[VCPU_REGS_RDI]);
2620 c->dst.val = 0;
2621 break;
2622 }
2623
2624done:
2625 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2626}
2627
2628int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002629x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002630{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002631 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002632 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002633 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002634 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002635 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002636
Gleb Natapov9de41572010-04-28 19:15:22 +03002637 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002638
Gleb Natapov11616242010-02-11 14:43:14 +02002639 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002640 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002641 goto done;
2642 }
2643
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002644 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002645 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002646 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002647 goto done;
2648 }
2649
Gleb Natapove92805a2010-02-10 14:21:35 +02002650 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002651 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002652 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002653 goto done;
2654 }
2655
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002656 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002657 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002658 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002659 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002660 string_done:
2661 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002662 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002663 goto done;
2664 }
2665 /* The second termination condition only applies for REPE
2666 * and REPNE. Test if the repeat string operation prefix is
2667 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2668 * corresponding termination condition according to:
2669 * - if REPE/REPZ and ZF = 0 then done
2670 * - if REPNE/REPNZ and ZF = 1 then done
2671 */
2672 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002673 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002674 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002675 ((ctxt->eflags & EFLG_ZF) == 0))
2676 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002677 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002678 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2679 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002680 }
Gleb Natapov063db062010-03-18 15:20:06 +02002681 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002682 }
2683
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002684 if (c->src.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002685 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002686 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002687 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002688 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002689 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002690 }
2691
Gleb Natapove35b7b92010-02-25 16:36:42 +02002692 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002693 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002694 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002695 if (rc != X86EMUL_CONTINUE)
2696 goto done;
2697 }
2698
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002699 if ((c->d & DstMask) == ImplicitOps)
2700 goto special_insn;
2701
2702
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002703 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2704 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002705 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002706 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002707 if (rc != X86EMUL_CONTINUE)
2708 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002709 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002710 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002711
Avi Kivity018a98d2007-11-27 19:30:56 +02002712special_insn:
2713
Avi Kivityef65c882010-07-29 15:11:51 +03002714 if (c->execute) {
2715 rc = c->execute(ctxt);
2716 if (rc != X86EMUL_CONTINUE)
2717 goto done;
2718 goto writeback;
2719 }
2720
Laurent Viviere4e03de2007-09-18 11:52:50 +02002721 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 goto twobyte_insn;
2723
Laurent Viviere4e03de2007-09-18 11:52:50 +02002724 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 case 0x00 ... 0x05:
2726 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002727 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002729 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002730 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002731 break;
2732 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002733 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002734 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002735 goto done;
2736 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 case 0x08 ... 0x0d:
2738 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002739 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002741 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002742 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002743 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 case 0x10 ... 0x15:
2745 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002746 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002748 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002749 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002750 break;
2751 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002752 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002753 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002754 goto done;
2755 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 case 0x18 ... 0x1d:
2757 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002758 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002760 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002761 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002762 break;
2763 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002764 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002765 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002766 goto done;
2767 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002768 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002770 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 break;
2772 case 0x28 ... 0x2d:
2773 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002774 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 break;
2776 case 0x30 ... 0x35:
2777 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002778 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 break;
2780 case 0x38 ... 0x3d:
2781 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002782 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002784 case 0x40 ... 0x47: /* inc r16/r32 */
2785 emulate_1op("inc", c->dst, ctxt->eflags);
2786 break;
2787 case 0x48 ... 0x4f: /* dec r16/r32 */
2788 emulate_1op("dec", c->dst, ctxt->eflags);
2789 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002790 case 0x58 ... 0x5f: /* pop reg */
2791 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002792 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002793 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002794 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002795 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002796 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002797 rc = emulate_pusha(ctxt, ops);
2798 if (rc != X86EMUL_CONTINUE)
2799 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002800 break;
2801 case 0x61: /* popa */
2802 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002803 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002804 goto done;
2805 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002807 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002809 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002811 case 0x6c: /* insb */
2812 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002813 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002814 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002815 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002816 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002817 goto done;
2818 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002819 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2820 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002821 goto done; /* IO is needed, skip writeback */
2822 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002823 case 0x6e: /* outsb */
2824 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002825 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002826 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002827 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002828 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002829 goto done;
2830 }
Gleb Natapov79729952010-03-18 15:20:24 +02002831 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2832 &c->src.val, 1, ctxt->vcpu);
2833
2834 c->dst.type = OP_NONE; /* nothing to writeback */
2835 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002836 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002837 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002838 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002839 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002841 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 case 0:
2843 goto add;
2844 case 1:
2845 goto or;
2846 case 2:
2847 goto adc;
2848 case 3:
2849 goto sbb;
2850 case 4:
2851 goto and;
2852 case 5:
2853 goto sub;
2854 case 6:
2855 goto xor;
2856 case 7:
2857 goto cmp;
2858 }
2859 break;
2860 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002861 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002862 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 break;
2864 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002865 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002867 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002869 *(u8 *) c->src.addr.reg = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 break;
2871 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002872 *(u16 *) c->src.addr.reg = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 break;
2874 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002875 *c->src.addr.reg = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 break; /* 64b reg: zero-extend */
2877 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002878 *c->src.addr.reg = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 break;
2880 }
2881 /*
2882 * Write back the memory destination with implicit LOCK
2883 * prefix.
2884 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002885 c->dst.val = c->src.val;
2886 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002889 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002890 case 0x8c: /* mov r/m, sreg */
2891 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002892 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002893 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002894 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002895 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002896 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002897 case 0x8d: /* lea r16/r32, m */
Avi Kivity342fc632010-08-01 15:13:22 +03002898 c->dst.val = c->src.addr.mem;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002899 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002900 case 0x8e: { /* mov seg, r/m16 */
2901 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002902
2903 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002904
Gleb Natapovc6975182010-02-18 12:15:01 +02002905 if (c->modrm_reg == VCPU_SREG_CS ||
2906 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002907 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002908 goto done;
2909 }
2910
Glauber Costa310b5d32009-05-12 16:21:06 -04002911 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002912 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002913
Gleb Natapov2e873022010-03-18 15:20:18 +02002914 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002915
2916 c->dst.type = OP_NONE; /* Disable writeback. */
2917 break;
2918 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002920 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002921 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002924 case 0x90 ... 0x97: /* nop / xchg reg, rax */
2925 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
2926 goto done;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002927 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002928 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002929 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002930 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002931 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002932 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002933 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002934 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002935 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002936 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2937 if (rc != X86EMUL_CONTINUE)
2938 goto done;
2939 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002940 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002942 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002944 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002945 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02002946 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002947 case 0xa8 ... 0xa9: /* test ax, imm */
2948 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002950 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951 break;
2952 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002953 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 case 0xae ... 0xaf: /* scas */
2955 DPRINTF("Urk! I don't handle SCAS.\n");
2956 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002957 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002958 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002959 case 0xc0 ... 0xc1:
2960 emulate_grp2(ctxt);
2961 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002962 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002963 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002964 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002965 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002966 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002967 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2968 mov:
2969 c->dst.val = c->src.val;
2970 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002971 case 0xcb: /* ret far */
2972 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002973 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002974 goto done;
2975 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002976 case 0xcf: /* iret */
2977 rc = emulate_iret(ctxt, ops);
2978
2979 if (rc != X86EMUL_CONTINUE)
2980 goto done;
2981 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002982 case 0xd0 ... 0xd1: /* Grp2 */
2983 c->src.val = 1;
2984 emulate_grp2(ctxt);
2985 break;
2986 case 0xd2 ... 0xd3: /* Grp2 */
2987 c->src.val = c->regs[VCPU_REGS_RCX];
2988 emulate_grp2(ctxt);
2989 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002990 case 0xe4: /* inb */
2991 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002992 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002993 case 0xe6: /* outb */
2994 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002995 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002996 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002997 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002998 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002999 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003000 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003001 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003002 }
3003 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003004 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003005 case 0xea: { /* jmp far */
3006 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003007 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003008 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3009
3010 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003011 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003012
Gleb Natapov414e6272010-04-28 19:15:26 +03003013 c->eip = 0;
3014 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003015 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003016 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003017 case 0xeb:
3018 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003019 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003020 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003021 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003022 case 0xec: /* in al,dx */
3023 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003024 c->src.val = c->regs[VCPU_REGS_RDX];
3025 do_io_in:
3026 c->dst.bytes = min(c->dst.bytes, 4u);
3027 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003028 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003029 goto done;
3030 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003031 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3032 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003033 goto done; /* IO is needed */
3034 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003035 case 0xee: /* out dx,al */
3036 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003037 c->src.val = c->regs[VCPU_REGS_RDX];
3038 do_io_out:
3039 c->dst.bytes = min(c->dst.bytes, 4u);
3040 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003041 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003042 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003043 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003044 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3045 ctxt->vcpu);
3046 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003047 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003048 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003049 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003050 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003051 case 0xf5: /* cmc */
3052 /* complement carry flag from eflags reg */
3053 ctxt->eflags ^= EFLG_CF;
3054 c->dst.type = OP_NONE; /* Disable writeback. */
3055 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003056 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003057 if (!emulate_grp3(ctxt, ops))
3058 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003059 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003060 case 0xf8: /* clc */
3061 ctxt->eflags &= ~EFLG_CF;
3062 c->dst.type = OP_NONE; /* Disable writeback. */
3063 break;
3064 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003065 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003066 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003067 goto done;
3068 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003069 ctxt->eflags &= ~X86_EFLAGS_IF;
3070 c->dst.type = OP_NONE; /* Disable writeback. */
3071 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003072 break;
3073 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003074 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003075 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003076 goto done;
3077 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003078 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003079 ctxt->eflags |= X86_EFLAGS_IF;
3080 c->dst.type = OP_NONE; /* Disable writeback. */
3081 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003082 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003083 case 0xfc: /* cld */
3084 ctxt->eflags &= ~EFLG_DF;
3085 c->dst.type = OP_NONE; /* Disable writeback. */
3086 break;
3087 case 0xfd: /* std */
3088 ctxt->eflags |= EFLG_DF;
3089 c->dst.type = OP_NONE; /* Disable writeback. */
3090 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003091 case 0xfe: /* Grp4 */
3092 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003093 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003094 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003095 goto done;
3096 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003097 case 0xff: /* Grp5 */
3098 if (c->modrm_reg == 5)
3099 goto jump_far;
3100 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003101 default:
3102 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003104
3105writeback:
3106 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003107 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003108 goto done;
3109
Gleb Natapov5cd21912010-03-18 15:20:26 +02003110 /*
3111 * restore dst type in case the decoding will be reused
3112 * (happens for string instruction )
3113 */
3114 c->dst.type = saved_dst_type;
3115
Gleb Natapova682e352010-03-18 15:20:21 +02003116 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003117 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3118 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003119
3120 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003121 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3122 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003123
Gleb Natapov5cd21912010-03-18 15:20:26 +02003124 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003125 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003126 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003127 /*
3128 * Re-enter guest when pio read ahead buffer is empty or,
3129 * if it is not used, after each 1024 iteration.
3130 */
3131 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3132 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003133 ctxt->restart = false;
3134 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003135 /*
3136 * reset read cache here in case string instruction is restared
3137 * without decoding
3138 */
3139 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003140 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003141
3142done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003143 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144
3145twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003146 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003148 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 u16 size;
3150 unsigned long address;
3151
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003152 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003153 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003154 goto cannot_emulate;
3155
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003156 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003157 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003158 goto done;
3159
Avi Kivity33e38852008-05-21 15:34:25 +03003160 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003161 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003162 /* Disable writeback. */
3163 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003164 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003166 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003167 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003168 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 goto done;
3170 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003171 /* Disable writeback. */
3172 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003174 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003175 if (c->modrm_mod == 3) {
3176 switch (c->modrm_rm) {
3177 case 1:
3178 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003179 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003180 goto done;
3181 break;
3182 default:
3183 goto cannot_emulate;
3184 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003185 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003186 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003187 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003188 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003189 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003190 goto done;
3191 realmode_lidt(ctxt->vcpu, size, address);
3192 }
Avi Kivity16286d02008-04-14 14:40:50 +03003193 /* Disable writeback. */
3194 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 break;
3196 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003197 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003198 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 break;
3200 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003201 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003202 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003203 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003205 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003206 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003207 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 case 7: /* invlpg*/
Avi Kivity1f6f0582010-08-01 15:19:22 +03003209 emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
Avi Kivity16286d02008-04-14 14:40:50 +03003210 /* Disable writeback. */
3211 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 break;
3213 default:
3214 goto cannot_emulate;
3215 }
3216 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003217 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003218 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003219 if (rc != X86EMUL_CONTINUE)
3220 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003221 else
3222 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003223 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003224 case 0x06:
3225 emulate_clts(ctxt->vcpu);
3226 c->dst.type = OP_NONE;
3227 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003228 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003229 kvm_emulate_wbinvd(ctxt->vcpu);
3230 c->dst.type = OP_NONE;
3231 break;
3232 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003233 case 0x0d: /* GrpP (prefetch) */
3234 case 0x18: /* Grp16 (prefetch/nop) */
3235 c->dst.type = OP_NONE;
3236 break;
3237 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003238 switch (c->modrm_reg) {
3239 case 1:
3240 case 5 ... 7:
3241 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003242 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003243 goto done;
3244 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003245 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003246 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003248 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3249 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003250 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003251 goto done;
3252 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003253 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003255 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003256 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003257 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003258 goto done;
3259 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003260 c->dst.type = OP_NONE;
3261 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003263 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3264 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003265 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003266 goto done;
3267 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003268
Avi Kivityb27f3852010-08-01 14:25:22 +03003269 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003270 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3271 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3272 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003273 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003274 goto done;
3275 }
3276
Laurent Viviera01af5e2007-09-24 11:10:56 +02003277 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003279 case 0x30:
3280 /* wrmsr */
3281 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3282 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003283 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003284 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003285 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003286 }
3287 rc = X86EMUL_CONTINUE;
3288 c->dst.type = OP_NONE;
3289 break;
3290 case 0x32:
3291 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003292 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003293 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003294 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003295 } else {
3296 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3297 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3298 }
3299 rc = X86EMUL_CONTINUE;
3300 c->dst.type = OP_NONE;
3301 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003302 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003303 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003304 if (rc != X86EMUL_CONTINUE)
3305 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003306 else
3307 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003308 break;
3309 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003310 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003311 if (rc != X86EMUL_CONTINUE)
3312 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003313 else
3314 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003315 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003317 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003318 if (!test_cc(c->b, ctxt->eflags))
3319 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003321 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003322 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003323 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003324 c->dst.type = OP_NONE;
3325 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003326 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003327 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003328 break;
3329 case 0xa1: /* pop fs */
3330 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003331 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003332 goto done;
3333 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003334 case 0xa3:
3335 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003336 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003337 /* only subword offset */
3338 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003339 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003340 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003341 case 0xa4: /* shld imm8, r, r/m */
3342 case 0xa5: /* shld cl, r, r/m */
3343 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3344 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003345 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003346 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003347 break;
3348 case 0xa9: /* pop gs */
3349 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003350 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003351 goto done;
3352 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003353 case 0xab:
3354 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003355 /* only subword offset */
3356 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003357 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003358 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003359 case 0xac: /* shrd imm8, r, r/m */
3360 case 0xad: /* shrd cl, r, r/m */
3361 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3362 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003363 case 0xae: /* clflush */
3364 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365 case 0xb0 ... 0xb1: /* cmpxchg */
3366 /*
3367 * Save real source value, then compare EAX against
3368 * destination.
3369 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003370 c->src.orig_val = c->src.val;
3371 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003372 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3373 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003375 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003376 } else {
3377 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003378 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003379 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 }
3381 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003382 case 0xb3:
3383 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003384 /* only subword offset */
3385 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003386 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003389 c->dst.bytes = c->op_bytes;
3390 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3391 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003394 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 case 0:
3396 goto bt;
3397 case 1:
3398 goto bts;
3399 case 2:
3400 goto btr;
3401 case 3:
3402 goto btc;
3403 }
3404 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003405 case 0xbb:
3406 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003407 /* only subword offset */
3408 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003409 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003410 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003412 c->dst.bytes = c->op_bytes;
3413 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3414 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003416 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003417 c->dst.bytes = c->op_bytes;
3418 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3419 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003420 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003422 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003423 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003424 goto done;
3425 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003426 default:
3427 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428 }
3429 goto writeback;
3430
3431cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003432 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 return -1;
3434}