blob: a9a4a0b78a7d1a4a066b5cf979d2e294f0dd00c4 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030049#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030051#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
54#define DstAcc (4<<1) /* Destination Accumulator */
55#define DstDI (5<<1) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<1) /* 64bit memory operand */
57#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080058/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020059#define SrcNone (0<<4) /* No source operand. */
60#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
61#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Gleb Natapov341de7e2009-04-12 13:36:41 +030074#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030076#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030078#define Mov (1<<9)
79#define BitOp (1<<10)
80#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020081#define String (1<<12) /* String instruction (rep capable) */
82#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020083#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
84#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030085/* Misc flags */
Avi Kivity5a506b12010-08-01 15:10:29 +030086#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030087#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030088#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020089#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020090#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030091#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010092/* Source 2 operand type */
93#define Src2None (0<<29)
94#define Src2CL (1<<29)
95#define Src2ImmByte (2<<29)
96#define Src2One (3<<29)
97#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098
Avi Kivityd0e53322010-07-29 15:11:54 +030099#define X2(x...) x, x
100#define X3(x...) X2(x), x
101#define X4(x...) X2(x), X2(x)
102#define X5(x...) X4(x), x
103#define X6(x...) X4(x), X2(x)
104#define X7(x...) X4(x), X3(x)
105#define X8(x...) X4(x), X4(x)
106#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300107
Avi Kivityd65b1de2010-07-29 15:11:35 +0300108struct opcode {
109 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300110 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300111 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300112 struct opcode *group;
113 struct group_dual *gdual;
114 } u;
115};
116
117struct group_dual {
118 struct opcode mod012[8];
119 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300120};
121
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200123#define EFLG_ID (1<<21)
124#define EFLG_VIP (1<<20)
125#define EFLG_VIF (1<<19)
126#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200127#define EFLG_VM (1<<17)
128#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200129#define EFLG_IOPL (3<<12)
130#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131#define EFLG_OF (1<<11)
132#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200133#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200134#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135#define EFLG_SF (1<<7)
136#define EFLG_ZF (1<<6)
137#define EFLG_AF (1<<4)
138#define EFLG_PF (1<<2)
139#define EFLG_CF (1<<0)
140
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300141#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
142#define EFLG_RESERVED_ONE_MASK 2
143
Avi Kivity6aa8b732006-12-10 02:21:36 -0800144/*
145 * Instruction emulation:
146 * Most instructions are emulated directly via a fragment of inline assembly
147 * code. This allows us to save/restore EFLAGS and thus very easily pick up
148 * any modified flags.
149 */
150
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800151#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152#define _LO32 "k" /* force 32-bit operand */
153#define _STK "%%rsp" /* stack pointer */
154#elif defined(__i386__)
155#define _LO32 "" /* force 32-bit operand */
156#define _STK "%%esp" /* stack pointer */
157#endif
158
159/*
160 * These EFLAGS bits are restored from saved value during emulation, and
161 * any changes are written back to the saved value after emulation.
162 */
163#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
164
165/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200166#define _PRE_EFLAGS(_sav, _msk, _tmp) \
167 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
168 "movl %"_sav",%"_LO32 _tmp"; " \
169 "push %"_tmp"; " \
170 "push %"_tmp"; " \
171 "movl %"_msk",%"_LO32 _tmp"; " \
172 "andl %"_LO32 _tmp",("_STK"); " \
173 "pushf; " \
174 "notl %"_LO32 _tmp"; " \
175 "andl %"_LO32 _tmp",("_STK"); " \
176 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
177 "pop %"_tmp"; " \
178 "orl %"_LO32 _tmp",("_STK"); " \
179 "popf; " \
180 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
182/* After executing instruction: write-back necessary bits in EFLAGS. */
183#define _POST_EFLAGS(_sav, _msk, _tmp) \
184 /* _sav |= EFLAGS & _msk; */ \
185 "pushf; " \
186 "pop %"_tmp"; " \
187 "andl %"_msk",%"_LO32 _tmp"; " \
188 "orl %"_LO32 _tmp",%"_sav"; "
189
Avi Kivitydda96d82008-11-26 15:14:10 +0200190#ifdef CONFIG_X86_64
191#define ON64(x) x
192#else
193#define ON64(x)
194#endif
195
Avi Kivity6b7ad612008-11-26 15:30:45 +0200196#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
197 do { \
198 __asm__ __volatile__ ( \
199 _PRE_EFLAGS("0", "4", "2") \
200 _op _suffix " %"_x"3,%1; " \
201 _POST_EFLAGS("0", "4", "2") \
202 : "=m" (_eflags), "=m" ((_dst).val), \
203 "=&r" (_tmp) \
204 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200205 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200206
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208/* Raw emulation: instruction has two explicit operands. */
209#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200210 do { \
211 unsigned long _tmp; \
212 \
213 switch ((_dst).bytes) { \
214 case 2: \
215 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
216 break; \
217 case 4: \
218 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
219 break; \
220 case 8: \
221 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
222 break; \
223 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 } while (0)
225
226#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
227 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200228 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400229 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200231 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 break; \
233 default: \
234 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
235 _wx, _wy, _lx, _ly, _qx, _qy); \
236 break; \
237 } \
238 } while (0)
239
240/* Source operand is byte-sized and may be restricted to just %cl. */
241#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
242 __emulate_2op(_op, _src, _dst, _eflags, \
243 "b", "c", "b", "c", "b", "c", "b", "c")
244
245/* Source operand is byte, word, long or quad sized. */
246#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
247 __emulate_2op(_op, _src, _dst, _eflags, \
248 "b", "q", "w", "r", _LO32, "r", "", "r")
249
250/* Source operand is word, long or quad sized. */
251#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
252 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
253 "w", "r", _LO32, "r", "", "r")
254
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100255/* Instruction has three operands and one operand is stored in ECX register */
256#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
257 do { \
258 unsigned long _tmp; \
259 _type _clv = (_cl).val; \
260 _type _srcv = (_src).val; \
261 _type _dstv = (_dst).val; \
262 \
263 __asm__ __volatile__ ( \
264 _PRE_EFLAGS("0", "5", "2") \
265 _op _suffix " %4,%1 \n" \
266 _POST_EFLAGS("0", "5", "2") \
267 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
268 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
269 ); \
270 \
271 (_cl).val = (unsigned long) _clv; \
272 (_src).val = (unsigned long) _srcv; \
273 (_dst).val = (unsigned long) _dstv; \
274 } while (0)
275
276#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
277 do { \
278 switch ((_dst).bytes) { \
279 case 2: \
280 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
281 "w", unsigned short); \
282 break; \
283 case 4: \
284 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
285 "l", unsigned int); \
286 break; \
287 case 8: \
288 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
289 "q", unsigned long)); \
290 break; \
291 } \
292 } while (0)
293
Avi Kivitydda96d82008-11-26 15:14:10 +0200294#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 do { \
296 unsigned long _tmp; \
297 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200298 __asm__ __volatile__ ( \
299 _PRE_EFLAGS("0", "3", "2") \
300 _op _suffix " %1; " \
301 _POST_EFLAGS("0", "3", "2") \
302 : "=m" (_eflags), "+m" ((_dst).val), \
303 "=&r" (_tmp) \
304 : "i" (EFLAGS_MASK)); \
305 } while (0)
306
307/* Instruction has only one explicit operand (no source operand). */
308#define emulate_1op(_op, _dst, _eflags) \
309 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400310 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200311 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
312 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
313 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
314 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315 } \
316 } while (0)
317
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318/* Fetch next part of the instruction being emulated. */
319#define insn_fetch(_type, _size, _eip) \
320({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200321 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200322 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323 goto done; \
324 (_eip) += (_size); \
325 (_type)_x; \
326})
327
Gleb Natapov414e6272010-04-28 19:15:26 +0300328#define insn_fetch_arr(_arr, _size, _eip) \
329({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
330 if (rc != X86EMUL_CONTINUE) \
331 goto done; \
332 (_eip) += (_size); \
333})
334
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800335static inline unsigned long ad_mask(struct decode_cache *c)
336{
337 return (1UL << (c->ad_bytes << 3)) - 1;
338}
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800341static inline unsigned long
342address_mask(struct decode_cache *c, unsigned long reg)
343{
344 if (c->ad_bytes == sizeof(unsigned long))
345 return reg;
346 else
347 return reg & ad_mask(c);
348}
349
350static inline unsigned long
351register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
352{
353 return base + address_mask(c, reg);
354}
355
Harvey Harrison7a9572752008-02-19 07:40:41 -0800356static inline void
357register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
358{
359 if (c->ad_bytes == sizeof(unsigned long))
360 *reg += inc;
361 else
362 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
363}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364
Harvey Harrison7a9572752008-02-19 07:40:41 -0800365static inline void jmp_rel(struct decode_cache *c, int rel)
366{
367 register_address_increment(c, &c->eip, rel);
368}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300369
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300370static void set_seg_override(struct decode_cache *c, int seg)
371{
372 c->has_seg_override = true;
373 c->seg_override = seg;
374}
375
Gleb Natapov79168fd2010-04-28 19:15:30 +0300376static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
377 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300378{
379 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
380 return 0;
381
Gleb Natapov79168fd2010-04-28 19:15:30 +0300382 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300383}
384
385static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300386 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300387 struct decode_cache *c)
388{
389 if (!c->has_seg_override)
390 return 0;
391
Gleb Natapov79168fd2010-04-28 19:15:30 +0300392 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300393}
394
Gleb Natapov79168fd2010-04-28 19:15:30 +0300395static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
396 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300397{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300398 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300399}
400
Gleb Natapov79168fd2010-04-28 19:15:30 +0300401static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
402 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300403{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300404 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300405}
406
Gleb Natapov54b84862010-04-28 19:15:44 +0300407static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
408 u32 error, bool valid)
409{
410 ctxt->exception = vec;
411 ctxt->error_code = error;
412 ctxt->error_code_valid = valid;
413 ctxt->restart = false;
414}
415
416static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
417{
418 emulate_exception(ctxt, GP_VECTOR, err, true);
419}
420
421static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
422 int err)
423{
424 ctxt->cr2 = addr;
425 emulate_exception(ctxt, PF_VECTOR, err, true);
426}
427
428static void emulate_ud(struct x86_emulate_ctxt *ctxt)
429{
430 emulate_exception(ctxt, UD_VECTOR, 0, false);
431}
432
433static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
434{
435 emulate_exception(ctxt, TS_VECTOR, err, true);
436}
437
Avi Kivity62266862007-11-20 13:15:52 +0200438static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
439 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300440 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200441{
442 struct fetch_cache *fc = &ctxt->decode.fetch;
443 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300444 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200445
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300446 if (eip == fc->end) {
447 cur_size = fc->end - fc->start;
448 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
449 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
450 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900451 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200452 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300453 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200454 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300455 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900456 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200457}
458
459static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
460 struct x86_emulate_ops *ops,
461 unsigned long eip, void *dest, unsigned size)
462{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900463 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200464
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200465 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200466 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200467 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200468 while (size--) {
469 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900470 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200471 return rc;
472 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900473 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200474}
475
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000476/*
477 * Given the 'reg' portion of a ModRM byte, and a register block, return a
478 * pointer into the block that addresses the relevant register.
479 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
480 */
481static void *decode_register(u8 modrm_reg, unsigned long *regs,
482 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483{
484 void *p;
485
486 p = &regs[modrm_reg];
487 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
488 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
489 return p;
490}
491
492static int read_descriptor(struct x86_emulate_ctxt *ctxt,
493 struct x86_emulate_ops *ops,
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300494 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495 u16 *size, unsigned long *address, int op_bytes)
496{
497 int rc;
498
499 if (op_bytes == 2)
500 op_bytes = 3;
501 *address = 0;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300502 rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900503 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 return rc;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300505 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 return rc;
507}
508
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300509static int test_cc(unsigned int condition, unsigned int flags)
510{
511 int rc = 0;
512
513 switch ((condition & 15) >> 1) {
514 case 0: /* o */
515 rc |= (flags & EFLG_OF);
516 break;
517 case 1: /* b/c/nae */
518 rc |= (flags & EFLG_CF);
519 break;
520 case 2: /* z/e */
521 rc |= (flags & EFLG_ZF);
522 break;
523 case 3: /* be/na */
524 rc |= (flags & (EFLG_CF|EFLG_ZF));
525 break;
526 case 4: /* s */
527 rc |= (flags & EFLG_SF);
528 break;
529 case 5: /* p/pe */
530 rc |= (flags & EFLG_PF);
531 break;
532 case 7: /* le/ng */
533 rc |= (flags & EFLG_ZF);
534 /* fall through */
535 case 6: /* l/nge */
536 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
537 break;
538 }
539
540 /* Odd condition identifiers (lsb == 1) have inverted sense. */
541 return (!!rc ^ (condition & 1));
542}
543
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300544static void fetch_register_operand(struct operand *op)
545{
546 switch (op->bytes) {
547 case 1:
548 op->val = *(u8 *)op->addr.reg;
549 break;
550 case 2:
551 op->val = *(u16 *)op->addr.reg;
552 break;
553 case 4:
554 op->val = *(u32 *)op->addr.reg;
555 break;
556 case 8:
557 op->val = *(u64 *)op->addr.reg;
558 break;
559 }
560}
561
Avi Kivity3c118e22007-10-31 10:27:04 +0200562static void decode_register_operand(struct operand *op,
563 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200564 int inhibit_bytereg)
565{
Avi Kivity33615aa2007-10-31 11:15:56 +0200566 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200567 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200568
569 if (!(c->d & ModRM))
570 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200571 op->type = OP_REG;
572 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300573 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200574 op->bytes = 1;
575 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300576 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200577 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200578 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300579 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200580 op->orig_val = op->val;
581}
582
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200583static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300584 struct x86_emulate_ops *ops,
585 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200586{
587 struct decode_cache *c = &ctxt->decode;
588 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700589 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900590 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300591 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200592
593 if (c->rex_prefix) {
594 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
595 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
596 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
597 }
598
599 c->modrm = insn_fetch(u8, 1, c->eip);
600 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
601 c->modrm_reg |= (c->modrm & 0x38) >> 3;
602 c->modrm_rm |= (c->modrm & 0x07);
Avi Kivity09ee57c2010-08-01 12:07:29 +0300603 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200604
605 if (c->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300606 op->type = OP_REG;
607 op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
608 op->addr.reg = decode_register(c->modrm_rm,
Avi Kivity107d6d22008-05-05 14:58:26 +0300609 c->regs, c->d & ByteOp);
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300610 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200611 return rc;
612 }
613
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300614 op->type = OP_MEM;
615
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200616 if (c->ad_bytes == 2) {
617 unsigned bx = c->regs[VCPU_REGS_RBX];
618 unsigned bp = c->regs[VCPU_REGS_RBP];
619 unsigned si = c->regs[VCPU_REGS_RSI];
620 unsigned di = c->regs[VCPU_REGS_RDI];
621
622 /* 16-bit ModR/M decode. */
623 switch (c->modrm_mod) {
624 case 0:
625 if (c->modrm_rm == 6)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300626 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200627 break;
628 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300629 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200630 break;
631 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300632 modrm_ea += insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200633 break;
634 }
635 switch (c->modrm_rm) {
636 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300637 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200638 break;
639 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300640 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200641 break;
642 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300643 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200644 break;
645 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300646 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200647 break;
648 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300649 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200650 break;
651 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300652 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200653 break;
654 case 6:
655 if (c->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300656 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200657 break;
658 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300659 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200660 break;
661 }
662 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
663 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300664 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300665 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200666 } else {
667 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700668 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200669 sib = insn_fetch(u8, 1, c->eip);
670 index_reg |= (sib >> 3) & 7;
671 base_reg |= sib & 7;
672 scale = sib >> 6;
673
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700674 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300675 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700676 else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300677 modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700678 if (index_reg != 4)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300679 modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700680 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
681 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700682 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700683 } else
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300684 modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200685 switch (c->modrm_mod) {
686 case 0:
687 if (c->modrm_rm == 5)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300688 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200689 break;
690 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300691 modrm_ea += insn_fetch(s8, 1, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200692 break;
693 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300694 modrm_ea += insn_fetch(s32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200695 break;
696 }
697 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300698 op->addr.mem = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200699done:
700 return rc;
701}
702
703static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300704 struct x86_emulate_ops *ops,
705 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200706{
707 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900708 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200709
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300710 op->type = OP_MEM;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200711 switch (c->ad_bytes) {
712 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300713 op->addr.mem = insn_fetch(u16, 2, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200714 break;
715 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300716 op->addr.mem = insn_fetch(u32, 4, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200717 break;
718 case 8:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +0300719 op->addr.mem = insn_fetch(u64, 8, c->eip);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200720 break;
721 }
722done:
723 return rc;
724}
725
Gleb Natapov9de41572010-04-28 19:15:22 +0300726static int read_emulated(struct x86_emulate_ctxt *ctxt,
727 struct x86_emulate_ops *ops,
728 unsigned long addr, void *dest, unsigned size)
729{
730 int rc;
731 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300732 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300733
734 while (size) {
735 int n = min(size, 8u);
736 size -= n;
737 if (mc->pos < mc->end)
738 goto read_cached;
739
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300740 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
741 ctxt->vcpu);
742 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300743 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300744 if (rc != X86EMUL_CONTINUE)
745 return rc;
746 mc->end += n;
747
748 read_cached:
749 memcpy(dest, mc->data + mc->pos, n);
750 mc->pos += n;
751 dest += n;
752 addr += n;
753 }
754 return X86EMUL_CONTINUE;
755}
756
Gleb Natapov7b262e92010-03-18 15:20:27 +0200757static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
758 struct x86_emulate_ops *ops,
759 unsigned int size, unsigned short port,
760 void *dest)
761{
762 struct read_cache *rc = &ctxt->decode.io_read;
763
764 if (rc->pos == rc->end) { /* refill pio read ahead */
765 struct decode_cache *c = &ctxt->decode;
766 unsigned int in_page, n;
767 unsigned int count = c->rep_prefix ?
768 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
769 in_page = (ctxt->eflags & EFLG_DF) ?
770 offset_in_page(c->regs[VCPU_REGS_RDI]) :
771 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
772 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
773 count);
774 if (n == 0)
775 n = 1;
776 rc->pos = rc->end = 0;
777 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
778 return 0;
779 rc->end = n * size;
780 }
781
782 memcpy(dest, rc->data + rc->pos, size);
783 rc->pos += size;
784 return 1;
785}
786
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200787static u32 desc_limit_scaled(struct desc_struct *desc)
788{
789 u32 limit = get_desc_limit(desc);
790
791 return desc->g ? (limit << 12) | 0xfff : limit;
792}
793
794static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
795 struct x86_emulate_ops *ops,
796 u16 selector, struct desc_ptr *dt)
797{
798 if (selector & 1 << 2) {
799 struct desc_struct desc;
800 memset (dt, 0, sizeof *dt);
801 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
802 return;
803
804 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
805 dt->address = get_desc_base(&desc);
806 } else
807 ops->get_gdt(dt, ctxt->vcpu);
808}
809
810/* allowed just for 8 bytes segments */
811static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
812 struct x86_emulate_ops *ops,
813 u16 selector, struct desc_struct *desc)
814{
815 struct desc_ptr dt;
816 u16 index = selector >> 3;
817 int ret;
818 u32 err;
819 ulong addr;
820
821 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
822
823 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300824 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200825 return X86EMUL_PROPAGATE_FAULT;
826 }
827 addr = dt.address + index * 8;
828 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
829 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300830 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200831
832 return ret;
833}
834
835/* allowed just for 8 bytes segments */
836static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
837 struct x86_emulate_ops *ops,
838 u16 selector, struct desc_struct *desc)
839{
840 struct desc_ptr dt;
841 u16 index = selector >> 3;
842 u32 err;
843 ulong addr;
844 int ret;
845
846 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
847
848 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300849 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200850 return X86EMUL_PROPAGATE_FAULT;
851 }
852
853 addr = dt.address + index * 8;
854 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
855 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300856 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200857
858 return ret;
859}
860
861static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
862 struct x86_emulate_ops *ops,
863 u16 selector, int seg)
864{
865 struct desc_struct seg_desc;
866 u8 dpl, rpl, cpl;
867 unsigned err_vec = GP_VECTOR;
868 u32 err_code = 0;
869 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
870 int ret;
871
872 memset(&seg_desc, 0, sizeof seg_desc);
873
874 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
875 || ctxt->mode == X86EMUL_MODE_REAL) {
876 /* set real mode segment descriptor */
877 set_desc_base(&seg_desc, selector << 4);
878 set_desc_limit(&seg_desc, 0xffff);
879 seg_desc.type = 3;
880 seg_desc.p = 1;
881 seg_desc.s = 1;
882 goto load;
883 }
884
885 /* NULL selector is not valid for TR, CS and SS */
886 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
887 && null_selector)
888 goto exception;
889
890 /* TR should be in GDT only */
891 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
892 goto exception;
893
894 if (null_selector) /* for NULL selector skip all following checks */
895 goto load;
896
897 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
898 if (ret != X86EMUL_CONTINUE)
899 return ret;
900
901 err_code = selector & 0xfffc;
902 err_vec = GP_VECTOR;
903
904 /* can't load system descriptor into segment selecor */
905 if (seg <= VCPU_SREG_GS && !seg_desc.s)
906 goto exception;
907
908 if (!seg_desc.p) {
909 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
910 goto exception;
911 }
912
913 rpl = selector & 3;
914 dpl = seg_desc.dpl;
915 cpl = ops->cpl(ctxt->vcpu);
916
917 switch (seg) {
918 case VCPU_SREG_SS:
919 /*
920 * segment is not a writable data segment or segment
921 * selector's RPL != CPL or segment selector's RPL != CPL
922 */
923 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
924 goto exception;
925 break;
926 case VCPU_SREG_CS:
927 if (!(seg_desc.type & 8))
928 goto exception;
929
930 if (seg_desc.type & 4) {
931 /* conforming */
932 if (dpl > cpl)
933 goto exception;
934 } else {
935 /* nonconforming */
936 if (rpl > cpl || dpl != cpl)
937 goto exception;
938 }
939 /* CS(RPL) <- CPL */
940 selector = (selector & 0xfffc) | cpl;
941 break;
942 case VCPU_SREG_TR:
943 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
944 goto exception;
945 break;
946 case VCPU_SREG_LDTR:
947 if (seg_desc.s || seg_desc.type != 2)
948 goto exception;
949 break;
950 default: /* DS, ES, FS, or GS */
951 /*
952 * segment is not a data or readable code segment or
953 * ((segment is a data or nonconforming code segment)
954 * and (both RPL and CPL > DPL))
955 */
956 if ((seg_desc.type & 0xa) == 0x8 ||
957 (((seg_desc.type & 0xc) != 0xc) &&
958 (rpl > dpl && cpl > dpl)))
959 goto exception;
960 break;
961 }
962
963 if (seg_desc.s) {
964 /* mark segment as accessed */
965 seg_desc.type |= 1;
966 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
967 if (ret != X86EMUL_CONTINUE)
968 return ret;
969 }
970load:
971 ops->set_segment_selector(selector, seg, ctxt->vcpu);
972 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
973 return X86EMUL_CONTINUE;
974exception:
Gleb Natapov54b84862010-04-28 19:15:44 +0300975 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200976 return X86EMUL_PROPAGATE_FAULT;
977}
978
Wei Yongjunc37eda12010-06-15 09:03:33 +0800979static inline int writeback(struct x86_emulate_ctxt *ctxt,
980 struct x86_emulate_ops *ops)
981{
982 int rc;
983 struct decode_cache *c = &ctxt->decode;
984 u32 err;
985
986 switch (c->dst.type) {
987 case OP_REG:
988 /* The 4-byte case *is* correct:
989 * in 64-bit mode we zero-extend.
990 */
991 switch (c->dst.bytes) {
992 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300993 *(u8 *)c->dst.addr.reg = (u8)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800994 break;
995 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300996 *(u16 *)c->dst.addr.reg = (u16)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800997 break;
998 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300999 *c->dst.addr.reg = (u32)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001000 break; /* 64b: zero-ext */
1001 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001002 *c->dst.addr.reg = c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001003 break;
1004 }
1005 break;
1006 case OP_MEM:
1007 if (c->lock_prefix)
1008 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001009 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001010 &c->dst.orig_val,
1011 &c->dst.val,
1012 c->dst.bytes,
1013 &err,
1014 ctxt->vcpu);
1015 else
1016 rc = ops->write_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001017 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001018 &c->dst.val,
1019 c->dst.bytes,
1020 &err,
1021 ctxt->vcpu);
1022 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001023 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001024 if (rc != X86EMUL_CONTINUE)
1025 return rc;
1026 break;
1027 case OP_NONE:
1028 /* no writeback */
1029 break;
1030 default:
1031 break;
1032 }
1033 return X86EMUL_CONTINUE;
1034}
1035
Gleb Natapov79168fd2010-04-28 19:15:30 +03001036static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1037 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001038{
1039 struct decode_cache *c = &ctxt->decode;
1040
1041 c->dst.type = OP_MEM;
1042 c->dst.bytes = c->op_bytes;
1043 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001044 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001045 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1046 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001047}
1048
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001049static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001050 struct x86_emulate_ops *ops,
1051 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001052{
1053 struct decode_cache *c = &ctxt->decode;
1054 int rc;
1055
Gleb Natapov79168fd2010-04-28 19:15:30 +03001056 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001057 c->regs[VCPU_REGS_RSP]),
1058 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001059 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001060 return rc;
1061
Avi Kivity350f69d2009-01-05 11:12:40 +02001062 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001063 return rc;
1064}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001065
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001066static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1067 struct x86_emulate_ops *ops,
1068 void *dest, int len)
1069{
1070 int rc;
1071 unsigned long val, change_mask;
1072 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001073 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001074
1075 rc = emulate_pop(ctxt, ops, &val, len);
1076 if (rc != X86EMUL_CONTINUE)
1077 return rc;
1078
1079 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1080 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1081
1082 switch(ctxt->mode) {
1083 case X86EMUL_MODE_PROT64:
1084 case X86EMUL_MODE_PROT32:
1085 case X86EMUL_MODE_PROT16:
1086 if (cpl == 0)
1087 change_mask |= EFLG_IOPL;
1088 if (cpl <= iopl)
1089 change_mask |= EFLG_IF;
1090 break;
1091 case X86EMUL_MODE_VM86:
1092 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001093 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001094 return X86EMUL_PROPAGATE_FAULT;
1095 }
1096 change_mask |= EFLG_IF;
1097 break;
1098 default: /* real mode */
1099 change_mask |= (EFLG_IOPL | EFLG_IF);
1100 break;
1101 }
1102
1103 *(unsigned long *)dest =
1104 (ctxt->eflags & ~change_mask) | (val & change_mask);
1105
1106 return rc;
1107}
1108
Gleb Natapov79168fd2010-04-28 19:15:30 +03001109static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1110 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001111{
1112 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001113
Gleb Natapov79168fd2010-04-28 19:15:30 +03001114 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001115
Gleb Natapov79168fd2010-04-28 19:15:30 +03001116 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001117}
1118
1119static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1120 struct x86_emulate_ops *ops, int seg)
1121{
1122 struct decode_cache *c = &ctxt->decode;
1123 unsigned long selector;
1124 int rc;
1125
1126 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001127 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001128 return rc;
1129
Gleb Natapov2e873022010-03-18 15:20:18 +02001130 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001131 return rc;
1132}
1133
Wei Yongjunc37eda12010-06-15 09:03:33 +08001134static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001135 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001136{
1137 struct decode_cache *c = &ctxt->decode;
1138 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001139 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001140 int reg = VCPU_REGS_RAX;
1141
1142 while (reg <= VCPU_REGS_RDI) {
1143 (reg == VCPU_REGS_RSP) ?
1144 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1145
Gleb Natapov79168fd2010-04-28 19:15:30 +03001146 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001147
1148 rc = writeback(ctxt, ops);
1149 if (rc != X86EMUL_CONTINUE)
1150 return rc;
1151
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001152 ++reg;
1153 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001154
1155 /* Disable writeback. */
1156 c->dst.type = OP_NONE;
1157
1158 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001159}
1160
1161static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1162 struct x86_emulate_ops *ops)
1163{
1164 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001165 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001166 int reg = VCPU_REGS_RDI;
1167
1168 while (reg >= VCPU_REGS_RAX) {
1169 if (reg == VCPU_REGS_RSP) {
1170 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1171 c->op_bytes);
1172 --reg;
1173 }
1174
1175 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001176 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001177 break;
1178 --reg;
1179 }
1180 return rc;
1181}
1182
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001183static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1184 struct x86_emulate_ops *ops)
1185{
1186 struct decode_cache *c = &ctxt->decode;
1187 int rc = X86EMUL_CONTINUE;
1188 unsigned long temp_eip = 0;
1189 unsigned long temp_eflags = 0;
1190 unsigned long cs = 0;
1191 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1192 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1193 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1194 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1195
1196 /* TODO: Add stack limit check */
1197
1198 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1199
1200 if (rc != X86EMUL_CONTINUE)
1201 return rc;
1202
1203 if (temp_eip & ~0xffff) {
1204 emulate_gp(ctxt, 0);
1205 return X86EMUL_PROPAGATE_FAULT;
1206 }
1207
1208 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1209
1210 if (rc != X86EMUL_CONTINUE)
1211 return rc;
1212
1213 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1214
1215 if (rc != X86EMUL_CONTINUE)
1216 return rc;
1217
1218 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1219
1220 if (rc != X86EMUL_CONTINUE)
1221 return rc;
1222
1223 c->eip = temp_eip;
1224
1225
1226 if (c->op_bytes == 4)
1227 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1228 else if (c->op_bytes == 2) {
1229 ctxt->eflags &= ~0xffff;
1230 ctxt->eflags |= temp_eflags;
1231 }
1232
1233 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1234 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1235
1236 return rc;
1237}
1238
1239static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1240 struct x86_emulate_ops* ops)
1241{
1242 switch(ctxt->mode) {
1243 case X86EMUL_MODE_REAL:
1244 return emulate_iret_real(ctxt, ops);
1245 case X86EMUL_MODE_VM86:
1246 case X86EMUL_MODE_PROT16:
1247 case X86EMUL_MODE_PROT32:
1248 case X86EMUL_MODE_PROT64:
1249 default:
1250 /* iret from protected mode unimplemented yet */
1251 return X86EMUL_UNHANDLEABLE;
1252 }
1253}
1254
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001255static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1256 struct x86_emulate_ops *ops)
1257{
1258 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001259
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001260 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001261}
1262
Laurent Vivier05f086f2007-09-24 11:10:55 +02001263static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001264{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001265 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001266 switch (c->modrm_reg) {
1267 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001268 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001269 break;
1270 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001271 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001272 break;
1273 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001274 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001275 break;
1276 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001277 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001278 break;
1279 case 4: /* sal/shl */
1280 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001281 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001282 break;
1283 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001284 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001285 break;
1286 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001287 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001288 break;
1289 }
1290}
1291
1292static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001293 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001294{
1295 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001296
1297 switch (c->modrm_reg) {
1298 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001299 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001300 break;
1301 case 2: /* not */
1302 c->dst.val = ~c->dst.val;
1303 break;
1304 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001305 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001306 break;
1307 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001308 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001309 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001310 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001311}
1312
1313static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001314 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001315{
1316 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001317
1318 switch (c->modrm_reg) {
1319 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001320 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001321 break;
1322 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001323 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001324 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001325 case 2: /* call near abs */ {
1326 long int old_eip;
1327 old_eip = c->eip;
1328 c->eip = c->src.val;
1329 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001330 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001331 break;
1332 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001333 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001334 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001335 break;
1336 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001337 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001338 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001339 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001340 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001341}
1342
1343static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001344 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001345{
1346 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001347 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001348
1349 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1350 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001351 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1352 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001353 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001354 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001355 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1356 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001357
Laurent Vivier05f086f2007-09-24 11:10:55 +02001358 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001359 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001360 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001361}
1362
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001363static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1364 struct x86_emulate_ops *ops)
1365{
1366 struct decode_cache *c = &ctxt->decode;
1367 int rc;
1368 unsigned long cs;
1369
1370 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001371 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001372 return rc;
1373 if (c->op_bytes == 4)
1374 c->eip = (u32)c->eip;
1375 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001376 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001377 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001378 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001379 return rc;
1380}
1381
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001382static inline void
1383setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001384 struct x86_emulate_ops *ops, struct desc_struct *cs,
1385 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001386{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001387 memset(cs, 0, sizeof(struct desc_struct));
1388 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1389 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001390
1391 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001392 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001393 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001394 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001395 cs->type = 0x0b; /* Read, Execute, Accessed */
1396 cs->s = 1;
1397 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001398 cs->p = 1;
1399 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001400
Gleb Natapov79168fd2010-04-28 19:15:30 +03001401 set_desc_base(ss, 0); /* flat segment */
1402 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001403 ss->g = 1; /* 4kb granularity */
1404 ss->s = 1;
1405 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001406 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001407 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001408 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001409}
1410
1411static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001412emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001413{
1414 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001415 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001416 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001417 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001418
1419 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001420 if (ctxt->mode == X86EMUL_MODE_REAL ||
1421 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001422 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001423 return X86EMUL_PROPAGATE_FAULT;
1424 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001425
Gleb Natapov79168fd2010-04-28 19:15:30 +03001426 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001427
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001428 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001429 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001430 cs_sel = (u16)(msr_data & 0xfffc);
1431 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001432
1433 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001434 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001435 cs.l = 1;
1436 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001437 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1438 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1439 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1440 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001441
1442 c->regs[VCPU_REGS_RCX] = c->eip;
1443 if (is_long_mode(ctxt->vcpu)) {
1444#ifdef CONFIG_X86_64
1445 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1446
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001447 ops->get_msr(ctxt->vcpu,
1448 ctxt->mode == X86EMUL_MODE_PROT64 ?
1449 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001450 c->eip = msr_data;
1451
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001452 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001453 ctxt->eflags &= ~(msr_data | EFLG_RF);
1454#endif
1455 } else {
1456 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001457 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001458 c->eip = (u32)msr_data;
1459
1460 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1461 }
1462
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001463 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001464}
1465
Andre Przywara8c604352009-06-18 12:56:01 +02001466static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001467emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001468{
1469 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001470 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001471 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001472 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001473
Gleb Natapova0044752010-02-10 14:21:31 +02001474 /* inject #GP if in real mode */
1475 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001476 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001477 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001478 }
1479
1480 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1481 * Therefore, we inject an #UD.
1482 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001483 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001484 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001485 return X86EMUL_PROPAGATE_FAULT;
1486 }
Andre Przywara8c604352009-06-18 12:56:01 +02001487
Gleb Natapov79168fd2010-04-28 19:15:30 +03001488 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001489
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001490 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001491 switch (ctxt->mode) {
1492 case X86EMUL_MODE_PROT32:
1493 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001494 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001495 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001496 }
1497 break;
1498 case X86EMUL_MODE_PROT64:
1499 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001500 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001501 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001502 }
1503 break;
1504 }
1505
1506 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001507 cs_sel = (u16)msr_data;
1508 cs_sel &= ~SELECTOR_RPL_MASK;
1509 ss_sel = cs_sel + 8;
1510 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001511 if (ctxt->mode == X86EMUL_MODE_PROT64
1512 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001513 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001514 cs.l = 1;
1515 }
1516
Gleb Natapov79168fd2010-04-28 19:15:30 +03001517 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1518 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1519 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1520 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001521
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001522 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001523 c->eip = msr_data;
1524
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001525 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001526 c->regs[VCPU_REGS_RSP] = msr_data;
1527
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001528 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001529}
1530
Andre Przywara4668f052009-06-18 12:56:02 +02001531static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001532emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001533{
1534 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001535 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001536 u64 msr_data;
1537 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001538 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001539
Gleb Natapova0044752010-02-10 14:21:31 +02001540 /* inject #GP if in real mode or Virtual 8086 mode */
1541 if (ctxt->mode == X86EMUL_MODE_REAL ||
1542 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001543 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001544 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001545 }
1546
Gleb Natapov79168fd2010-04-28 19:15:30 +03001547 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001548
1549 if ((c->rex_prefix & 0x8) != 0x0)
1550 usermode = X86EMUL_MODE_PROT64;
1551 else
1552 usermode = X86EMUL_MODE_PROT32;
1553
1554 cs.dpl = 3;
1555 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001556 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001557 switch (usermode) {
1558 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001559 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001560 if ((msr_data & 0xfffc) == 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 = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001565 break;
1566 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001567 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001568 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001569 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001570 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001571 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001572 ss_sel = cs_sel + 8;
1573 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001574 cs.l = 1;
1575 break;
1576 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001577 cs_sel |= SELECTOR_RPL_MASK;
1578 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001579
Gleb Natapov79168fd2010-04-28 19:15:30 +03001580 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1581 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1582 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1583 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001584
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001585 c->eip = c->regs[VCPU_REGS_RDX];
1586 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001587
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001588 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001589}
1590
Gleb Natapov9c537242010-03-18 15:20:05 +02001591static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1592 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001593{
1594 int iopl;
1595 if (ctxt->mode == X86EMUL_MODE_REAL)
1596 return false;
1597 if (ctxt->mode == X86EMUL_MODE_VM86)
1598 return true;
1599 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001600 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001601}
1602
1603static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1604 struct x86_emulate_ops *ops,
1605 u16 port, u16 len)
1606{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001608 int r;
1609 u16 io_bitmap_ptr;
1610 u8 perm, bit_idx = port & 0x7;
1611 unsigned mask = (1 << len) - 1;
1612
Gleb Natapov79168fd2010-04-28 19:15:30 +03001613 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1614 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001615 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001616 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001617 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001618 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1619 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001620 if (r != X86EMUL_CONTINUE)
1621 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001622 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001623 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001624 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1625 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001626 if (r != X86EMUL_CONTINUE)
1627 return false;
1628 if ((perm >> bit_idx) & mask)
1629 return false;
1630 return true;
1631}
1632
1633static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1634 struct x86_emulate_ops *ops,
1635 u16 port, u16 len)
1636{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001637 if (ctxt->perm_ok)
1638 return true;
1639
Gleb Natapov9c537242010-03-18 15:20:05 +02001640 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001641 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1642 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001643
1644 ctxt->perm_ok = true;
1645
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001646 return true;
1647}
1648
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001649static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1650 struct x86_emulate_ops *ops,
1651 struct tss_segment_16 *tss)
1652{
1653 struct decode_cache *c = &ctxt->decode;
1654
1655 tss->ip = c->eip;
1656 tss->flag = ctxt->eflags;
1657 tss->ax = c->regs[VCPU_REGS_RAX];
1658 tss->cx = c->regs[VCPU_REGS_RCX];
1659 tss->dx = c->regs[VCPU_REGS_RDX];
1660 tss->bx = c->regs[VCPU_REGS_RBX];
1661 tss->sp = c->regs[VCPU_REGS_RSP];
1662 tss->bp = c->regs[VCPU_REGS_RBP];
1663 tss->si = c->regs[VCPU_REGS_RSI];
1664 tss->di = c->regs[VCPU_REGS_RDI];
1665
1666 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1667 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1668 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1669 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1670 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1671}
1672
1673static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1674 struct x86_emulate_ops *ops,
1675 struct tss_segment_16 *tss)
1676{
1677 struct decode_cache *c = &ctxt->decode;
1678 int ret;
1679
1680 c->eip = tss->ip;
1681 ctxt->eflags = tss->flag | 2;
1682 c->regs[VCPU_REGS_RAX] = tss->ax;
1683 c->regs[VCPU_REGS_RCX] = tss->cx;
1684 c->regs[VCPU_REGS_RDX] = tss->dx;
1685 c->regs[VCPU_REGS_RBX] = tss->bx;
1686 c->regs[VCPU_REGS_RSP] = tss->sp;
1687 c->regs[VCPU_REGS_RBP] = tss->bp;
1688 c->regs[VCPU_REGS_RSI] = tss->si;
1689 c->regs[VCPU_REGS_RDI] = tss->di;
1690
1691 /*
1692 * SDM says that segment selectors are loaded before segment
1693 * descriptors
1694 */
1695 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1696 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1697 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1698 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1699 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1700
1701 /*
1702 * Now load segment descriptors. If fault happenes at this stage
1703 * it is handled in a context of new task
1704 */
1705 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1706 if (ret != X86EMUL_CONTINUE)
1707 return ret;
1708 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1709 if (ret != X86EMUL_CONTINUE)
1710 return ret;
1711 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1712 if (ret != X86EMUL_CONTINUE)
1713 return ret;
1714 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1715 if (ret != X86EMUL_CONTINUE)
1716 return ret;
1717 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1718 if (ret != X86EMUL_CONTINUE)
1719 return ret;
1720
1721 return X86EMUL_CONTINUE;
1722}
1723
1724static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1725 struct x86_emulate_ops *ops,
1726 u16 tss_selector, u16 old_tss_sel,
1727 ulong old_tss_base, struct desc_struct *new_desc)
1728{
1729 struct tss_segment_16 tss_seg;
1730 int ret;
1731 u32 err, new_tss_base = get_desc_base(new_desc);
1732
1733 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1734 &err);
1735 if (ret == X86EMUL_PROPAGATE_FAULT) {
1736 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001737 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001738 return ret;
1739 }
1740
1741 save_state_to_tss16(ctxt, ops, &tss_seg);
1742
1743 ret = ops->write_std(old_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, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001748 return ret;
1749 }
1750
1751 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1752 &err);
1753 if (ret == X86EMUL_PROPAGATE_FAULT) {
1754 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001755 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001756 return ret;
1757 }
1758
1759 if (old_tss_sel != 0xffff) {
1760 tss_seg.prev_task_link = old_tss_sel;
1761
1762 ret = ops->write_std(new_tss_base,
1763 &tss_seg.prev_task_link,
1764 sizeof tss_seg.prev_task_link,
1765 ctxt->vcpu, &err);
1766 if (ret == X86EMUL_PROPAGATE_FAULT) {
1767 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001768 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001769 return ret;
1770 }
1771 }
1772
1773 return load_state_from_tss16(ctxt, ops, &tss_seg);
1774}
1775
1776static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1777 struct x86_emulate_ops *ops,
1778 struct tss_segment_32 *tss)
1779{
1780 struct decode_cache *c = &ctxt->decode;
1781
1782 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1783 tss->eip = c->eip;
1784 tss->eflags = ctxt->eflags;
1785 tss->eax = c->regs[VCPU_REGS_RAX];
1786 tss->ecx = c->regs[VCPU_REGS_RCX];
1787 tss->edx = c->regs[VCPU_REGS_RDX];
1788 tss->ebx = c->regs[VCPU_REGS_RBX];
1789 tss->esp = c->regs[VCPU_REGS_RSP];
1790 tss->ebp = c->regs[VCPU_REGS_RBP];
1791 tss->esi = c->regs[VCPU_REGS_RSI];
1792 tss->edi = c->regs[VCPU_REGS_RDI];
1793
1794 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1795 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1796 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1797 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1798 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1799 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1800 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1801}
1802
1803static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1804 struct x86_emulate_ops *ops,
1805 struct tss_segment_32 *tss)
1806{
1807 struct decode_cache *c = &ctxt->decode;
1808 int ret;
1809
Gleb Natapov0f122442010-04-28 19:15:31 +03001810 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001811 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001812 return X86EMUL_PROPAGATE_FAULT;
1813 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001814 c->eip = tss->eip;
1815 ctxt->eflags = tss->eflags | 2;
1816 c->regs[VCPU_REGS_RAX] = tss->eax;
1817 c->regs[VCPU_REGS_RCX] = tss->ecx;
1818 c->regs[VCPU_REGS_RDX] = tss->edx;
1819 c->regs[VCPU_REGS_RBX] = tss->ebx;
1820 c->regs[VCPU_REGS_RSP] = tss->esp;
1821 c->regs[VCPU_REGS_RBP] = tss->ebp;
1822 c->regs[VCPU_REGS_RSI] = tss->esi;
1823 c->regs[VCPU_REGS_RDI] = tss->edi;
1824
1825 /*
1826 * SDM says that segment selectors are loaded before segment
1827 * descriptors
1828 */
1829 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1830 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1831 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1832 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1833 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1834 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1835 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1836
1837 /*
1838 * Now load segment descriptors. If fault happenes at this stage
1839 * it is handled in a context of new task
1840 */
1841 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1842 if (ret != X86EMUL_CONTINUE)
1843 return ret;
1844 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1845 if (ret != X86EMUL_CONTINUE)
1846 return ret;
1847 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1848 if (ret != X86EMUL_CONTINUE)
1849 return ret;
1850 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1851 if (ret != X86EMUL_CONTINUE)
1852 return ret;
1853 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1854 if (ret != X86EMUL_CONTINUE)
1855 return ret;
1856 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1857 if (ret != X86EMUL_CONTINUE)
1858 return ret;
1859 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1860 if (ret != X86EMUL_CONTINUE)
1861 return ret;
1862
1863 return X86EMUL_CONTINUE;
1864}
1865
1866static int task_switch_32(struct x86_emulate_ctxt *ctxt,
1867 struct x86_emulate_ops *ops,
1868 u16 tss_selector, u16 old_tss_sel,
1869 ulong old_tss_base, struct desc_struct *new_desc)
1870{
1871 struct tss_segment_32 tss_seg;
1872 int ret;
1873 u32 err, new_tss_base = get_desc_base(new_desc);
1874
1875 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1876 &err);
1877 if (ret == X86EMUL_PROPAGATE_FAULT) {
1878 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001879 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001880 return ret;
1881 }
1882
1883 save_state_to_tss32(ctxt, ops, &tss_seg);
1884
1885 ret = ops->write_std(old_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, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001890 return ret;
1891 }
1892
1893 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1894 &err);
1895 if (ret == X86EMUL_PROPAGATE_FAULT) {
1896 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001897 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001898 return ret;
1899 }
1900
1901 if (old_tss_sel != 0xffff) {
1902 tss_seg.prev_task_link = old_tss_sel;
1903
1904 ret = ops->write_std(new_tss_base,
1905 &tss_seg.prev_task_link,
1906 sizeof tss_seg.prev_task_link,
1907 ctxt->vcpu, &err);
1908 if (ret == X86EMUL_PROPAGATE_FAULT) {
1909 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001910 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001911 return ret;
1912 }
1913 }
1914
1915 return load_state_from_tss32(ctxt, ops, &tss_seg);
1916}
1917
1918static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001919 struct x86_emulate_ops *ops,
1920 u16 tss_selector, int reason,
1921 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001922{
1923 struct desc_struct curr_tss_desc, next_tss_desc;
1924 int ret;
1925 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
1926 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03001927 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02001928 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001929
1930 /* FIXME: old_tss_base == ~0 ? */
1931
1932 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
1933 if (ret != X86EMUL_CONTINUE)
1934 return ret;
1935 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
1936 if (ret != X86EMUL_CONTINUE)
1937 return ret;
1938
1939 /* FIXME: check that next_tss_desc is tss */
1940
1941 if (reason != TASK_SWITCH_IRET) {
1942 if ((tss_selector & 3) > next_tss_desc.dpl ||
1943 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001944 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001945 return X86EMUL_PROPAGATE_FAULT;
1946 }
1947 }
1948
Gleb Natapovceffb452010-03-18 15:20:19 +02001949 desc_limit = desc_limit_scaled(&next_tss_desc);
1950 if (!next_tss_desc.p ||
1951 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
1952 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001953 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001954 return X86EMUL_PROPAGATE_FAULT;
1955 }
1956
1957 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
1958 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
1959 write_segment_descriptor(ctxt, ops, old_tss_sel,
1960 &curr_tss_desc);
1961 }
1962
1963 if (reason == TASK_SWITCH_IRET)
1964 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
1965
1966 /* set back link to prev task only if NT bit is set in eflags
1967 note that old_tss_sel is not used afetr this point */
1968 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
1969 old_tss_sel = 0xffff;
1970
1971 if (next_tss_desc.type & 8)
1972 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
1973 old_tss_base, &next_tss_desc);
1974 else
1975 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
1976 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02001977 if (ret != X86EMUL_CONTINUE)
1978 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001979
1980 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
1981 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
1982
1983 if (reason != TASK_SWITCH_IRET) {
1984 next_tss_desc.type |= (1 << 1); /* set busy flag */
1985 write_segment_descriptor(ctxt, ops, tss_selector,
1986 &next_tss_desc);
1987 }
1988
1989 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
1990 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
1991 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
1992
Jan Kiszkae269fb22010-04-14 15:51:09 +02001993 if (has_error_code) {
1994 struct decode_cache *c = &ctxt->decode;
1995
1996 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
1997 c->lock_prefix = 0;
1998 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001999 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002000 }
2001
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002002 return ret;
2003}
2004
2005int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002006 u16 tss_selector, int reason,
2007 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002008{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002009 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002010 struct decode_cache *c = &ctxt->decode;
2011 int rc;
2012
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002013 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002014 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002015
Jan Kiszkae269fb22010-04-14 15:51:09 +02002016 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2017 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002018
2019 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002020 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002021 if (rc == X86EMUL_CONTINUE)
2022 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002023 }
2024
Gleb Natapov19d04432010-04-15 12:29:50 +03002025 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002026}
2027
Gleb Natapova682e352010-03-18 15:20:21 +02002028static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002029 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002030{
2031 struct decode_cache *c = &ctxt->decode;
2032 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2033
Gleb Natapovd9271122010-03-18 15:20:22 +02002034 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002035 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002036}
2037
Avi Kivity63540382010-07-29 15:11:55 +03002038static int em_push(struct x86_emulate_ctxt *ctxt)
2039{
2040 emulate_push(ctxt, ctxt->ops);
2041 return X86EMUL_CONTINUE;
2042}
2043
Avi Kivity73fba5f2010-07-29 15:11:53 +03002044#define D(_y) { .flags = (_y) }
2045#define N D(0)
2046#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2047#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2048#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2049
2050static struct opcode group1[] = {
2051 X7(D(Lock)), N
2052};
2053
2054static struct opcode group1A[] = {
2055 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2056};
2057
2058static struct opcode group3[] = {
2059 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2060 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2061 X4(D(Undefined)),
2062};
2063
2064static struct opcode group4[] = {
2065 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2066 N, N, N, N, N, N,
2067};
2068
2069static struct opcode group5[] = {
2070 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2071 D(SrcMem | ModRM | Stack), N,
2072 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2073 D(SrcMem | ModRM | Stack), N,
2074};
2075
2076static struct group_dual group7 = { {
2077 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2078 D(SrcNone | ModRM | DstMem | Mov), N,
Avi Kivity5a506b12010-08-01 15:10:29 +03002079 D(SrcMem16 | ModRM | Mov | Priv),
2080 D(SrcMem | ModRM | ByteOp | Priv | NoAccess),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002081}, {
2082 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2083 D(SrcNone | ModRM | DstMem | Mov), N,
2084 D(SrcMem16 | ModRM | Mov | Priv), N,
2085} };
2086
2087static struct opcode group8[] = {
2088 N, N, N, N,
2089 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2090 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2091};
2092
2093static struct group_dual group9 = { {
2094 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2095}, {
2096 N, N, N, N, N, N, N, N,
2097} };
2098
2099static struct opcode opcode_table[256] = {
2100 /* 0x00 - 0x07 */
2101 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2102 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2103 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2104 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2105 /* 0x08 - 0x0F */
2106 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2107 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2108 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2109 D(ImplicitOps | Stack | No64), N,
2110 /* 0x10 - 0x17 */
2111 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2112 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2113 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2114 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2115 /* 0x18 - 0x1F */
2116 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2117 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2118 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2119 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2120 /* 0x20 - 0x27 */
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 /* 0x28 - 0x2F */
2125 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2126 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2127 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2128 /* 0x30 - 0x37 */
2129 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2130 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2131 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2132 /* 0x38 - 0x3F */
2133 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2134 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2135 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2136 N, N,
2137 /* 0x40 - 0x4F */
2138 X16(D(DstReg)),
2139 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002140 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002141 /* 0x58 - 0x5F */
2142 X8(D(DstReg | Stack)),
2143 /* 0x60 - 0x67 */
2144 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2145 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2146 N, N, N, N,
2147 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002148 I(SrcImm | Mov | Stack, em_push), N,
2149 I(SrcImmByte | Mov | Stack, em_push), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002150 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2151 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2152 /* 0x70 - 0x7F */
2153 X16(D(SrcImmByte)),
2154 /* 0x80 - 0x87 */
2155 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2156 G(DstMem | SrcImm | ModRM | Group, group1),
2157 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2158 G(DstMem | SrcImmByte | ModRM | Group, group1),
2159 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2160 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2161 /* 0x88 - 0x8F */
2162 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2163 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
Avi Kivity342fc632010-08-01 15:13:22 +03002164 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | SrcMem | NoAccess | DstReg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002165 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2166 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002167 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002168 /* 0x98 - 0x9F */
2169 N, N, D(SrcImmFAddr | No64), N,
2170 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2171 /* 0xA0 - 0xA7 */
2172 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2173 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2174 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2175 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2176 /* 0xA8 - 0xAF */
2177 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
2178 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
2179 D(ByteOp | DstDI | String), D(DstDI | String),
2180 /* 0xB0 - 0xB7 */
2181 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2182 /* 0xB8 - 0xBF */
2183 X8(D(DstReg | SrcImm | Mov)),
2184 /* 0xC0 - 0xC7 */
2185 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
2186 N, D(ImplicitOps | Stack), N, N,
2187 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2188 /* 0xC8 - 0xCF */
2189 N, N, N, D(ImplicitOps | Stack),
2190 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2191 /* 0xD0 - 0xD7 */
2192 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2193 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2194 N, N, N, N,
2195 /* 0xD8 - 0xDF */
2196 N, N, N, N, N, N, N, N,
2197 /* 0xE0 - 0xE7 */
2198 N, N, N, N,
2199 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2200 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2201 /* 0xE8 - 0xEF */
2202 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2203 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2204 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2205 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2206 /* 0xF0 - 0xF7 */
2207 N, N, N, N,
2208 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2209 /* 0xF8 - 0xFF */
2210 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
2211 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2212};
2213
2214static struct opcode twobyte_table[256] = {
2215 /* 0x00 - 0x0F */
2216 N, GD(0, &group7), N, N,
2217 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2218 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2219 N, D(ImplicitOps | ModRM), N, N,
2220 /* 0x10 - 0x1F */
2221 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2222 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002223 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2224 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002225 N, N, N, N,
2226 N, N, N, N, N, N, N, N,
2227 /* 0x30 - 0x3F */
2228 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
2229 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2230 N, N, N, N, N, N, N, N,
2231 /* 0x40 - 0x4F */
2232 X16(D(DstReg | SrcMem | ModRM | Mov)),
2233 /* 0x50 - 0x5F */
2234 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2235 /* 0x60 - 0x6F */
2236 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2237 /* 0x70 - 0x7F */
2238 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2239 /* 0x80 - 0x8F */
2240 X16(D(SrcImm)),
2241 /* 0x90 - 0x9F */
2242 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2243 /* 0xA0 - 0xA7 */
2244 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2245 N, D(DstMem | SrcReg | ModRM | BitOp),
2246 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2247 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2248 /* 0xA8 - 0xAF */
2249 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2250 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2251 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2252 D(DstMem | SrcReg | Src2CL | ModRM),
2253 D(ModRM), N,
2254 /* 0xB0 - 0xB7 */
2255 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2256 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2257 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2258 D(DstReg | SrcMem16 | ModRM | Mov),
2259 /* 0xB8 - 0xBF */
2260 N, N,
2261 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2262 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2263 D(DstReg | SrcMem16 | ModRM | Mov),
2264 /* 0xC0 - 0xCF */
2265 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
2266 N, N, N, GD(0, &group9),
2267 N, N, N, N, N, N, N, N,
2268 /* 0xD0 - 0xDF */
2269 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2270 /* 0xE0 - 0xEF */
2271 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2272 /* 0xF0 - 0xFF */
2273 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2274};
2275
2276#undef D
2277#undef N
2278#undef G
2279#undef GD
2280#undef I
2281
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002282int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002283x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2284{
2285 struct x86_emulate_ops *ops = ctxt->ops;
2286 struct decode_cache *c = &ctxt->decode;
2287 int rc = X86EMUL_CONTINUE;
2288 int mode = ctxt->mode;
2289 int def_op_bytes, def_ad_bytes, dual, goffset;
2290 struct opcode opcode, *g_mod012, *g_mod3;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002291 struct operand memop = { .type = OP_NONE };
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002292
2293 /* we cannot decode insn before we complete previous rep insn */
2294 WARN_ON(ctxt->restart);
2295
2296 c->eip = ctxt->eip;
2297 c->fetch.start = c->fetch.end = c->eip;
2298 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2299
2300 switch (mode) {
2301 case X86EMUL_MODE_REAL:
2302 case X86EMUL_MODE_VM86:
2303 case X86EMUL_MODE_PROT16:
2304 def_op_bytes = def_ad_bytes = 2;
2305 break;
2306 case X86EMUL_MODE_PROT32:
2307 def_op_bytes = def_ad_bytes = 4;
2308 break;
2309#ifdef CONFIG_X86_64
2310 case X86EMUL_MODE_PROT64:
2311 def_op_bytes = 4;
2312 def_ad_bytes = 8;
2313 break;
2314#endif
2315 default:
2316 return -1;
2317 }
2318
2319 c->op_bytes = def_op_bytes;
2320 c->ad_bytes = def_ad_bytes;
2321
2322 /* Legacy prefixes. */
2323 for (;;) {
2324 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2325 case 0x66: /* operand-size override */
2326 /* switch between 2/4 bytes */
2327 c->op_bytes = def_op_bytes ^ 6;
2328 break;
2329 case 0x67: /* address-size override */
2330 if (mode == X86EMUL_MODE_PROT64)
2331 /* switch between 4/8 bytes */
2332 c->ad_bytes = def_ad_bytes ^ 12;
2333 else
2334 /* switch between 2/4 bytes */
2335 c->ad_bytes = def_ad_bytes ^ 6;
2336 break;
2337 case 0x26: /* ES override */
2338 case 0x2e: /* CS override */
2339 case 0x36: /* SS override */
2340 case 0x3e: /* DS override */
2341 set_seg_override(c, (c->b >> 3) & 3);
2342 break;
2343 case 0x64: /* FS override */
2344 case 0x65: /* GS override */
2345 set_seg_override(c, c->b & 7);
2346 break;
2347 case 0x40 ... 0x4f: /* REX */
2348 if (mode != X86EMUL_MODE_PROT64)
2349 goto done_prefixes;
2350 c->rex_prefix = c->b;
2351 continue;
2352 case 0xf0: /* LOCK */
2353 c->lock_prefix = 1;
2354 break;
2355 case 0xf2: /* REPNE/REPNZ */
2356 c->rep_prefix = REPNE_PREFIX;
2357 break;
2358 case 0xf3: /* REP/REPE/REPZ */
2359 c->rep_prefix = REPE_PREFIX;
2360 break;
2361 default:
2362 goto done_prefixes;
2363 }
2364
2365 /* Any legacy prefix after a REX prefix nullifies its effect. */
2366
2367 c->rex_prefix = 0;
2368 }
2369
2370done_prefixes:
2371
2372 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002373 if (c->rex_prefix & 8)
2374 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002375
2376 /* Opcode byte(s). */
2377 opcode = opcode_table[c->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08002378 /* Two-byte opcode? */
2379 if (c->b == 0x0f) {
2380 c->twobyte = 1;
2381 c->b = insn_fetch(u8, 1, c->eip);
2382 opcode = twobyte_table[c->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002383 }
2384 c->d = opcode.flags;
2385
2386 if (c->d & Group) {
2387 dual = c->d & GroupDual;
2388 c->modrm = insn_fetch(u8, 1, c->eip);
2389 --c->eip;
2390
2391 if (c->d & GroupDual) {
2392 g_mod012 = opcode.u.gdual->mod012;
2393 g_mod3 = opcode.u.gdual->mod3;
2394 } else
2395 g_mod012 = g_mod3 = opcode.u.group;
2396
2397 c->d &= ~(Group | GroupDual);
2398
2399 goffset = (c->modrm >> 3) & 7;
2400
2401 if ((c->modrm >> 6) == 3)
2402 opcode = g_mod3[goffset];
2403 else
2404 opcode = g_mod012[goffset];
2405 c->d |= opcode.flags;
2406 }
2407
2408 c->execute = opcode.u.execute;
2409
2410 /* Unrecognised? */
2411 if (c->d == 0 || (c->d & Undefined)) {
2412 DPRINTF("Cannot emulate %02x\n", c->b);
2413 return -1;
2414 }
2415
2416 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2417 c->op_bytes = 8;
2418
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002419 if (c->d & Op3264) {
2420 if (mode == X86EMUL_MODE_PROT64)
2421 c->op_bytes = 8;
2422 else
2423 c->op_bytes = 4;
2424 }
2425
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002426 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002427 if (c->d & ModRM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002428 rc = decode_modrm(ctxt, ops, &memop);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002429 if (!c->has_seg_override)
2430 set_seg_override(c, c->modrm_seg);
2431 } else if (c->d & MemAbs)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002432 rc = decode_abs(ctxt, ops, &memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002433 if (rc != X86EMUL_CONTINUE)
2434 goto done;
2435
2436 if (!c->has_seg_override)
2437 set_seg_override(c, VCPU_SREG_DS);
2438
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002439 if (memop.type == OP_MEM && !(!c->twobyte && c->b == 0x8d))
2440 memop.addr.mem += seg_override_base(ctxt, ops, c);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002441
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002442 if (memop.type == OP_MEM && c->ad_bytes != 8)
2443 memop.addr.mem = (u32)memop.addr.mem;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002444
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002445 if (memop.type == OP_MEM && c->rip_relative)
2446 memop.addr.mem += c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002447
2448 /*
2449 * Decode and fetch the source operand: register, memory
2450 * or immediate.
2451 */
2452 switch (c->d & SrcMask) {
2453 case SrcNone:
2454 break;
2455 case SrcReg:
2456 decode_register_operand(&c->src, c, 0);
2457 break;
2458 case SrcMem16:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002459 memop.bytes = 2;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002460 goto srcmem_common;
2461 case SrcMem32:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002462 memop.bytes = 4;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002463 goto srcmem_common;
2464 case SrcMem:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002465 memop.bytes = (c->d & ByteOp) ? 1 :
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002466 c->op_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002467 srcmem_common:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002468 c->src = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002469 break;
2470 case SrcImm:
2471 case SrcImmU:
2472 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002473 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002474 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2475 if (c->src.bytes == 8)
2476 c->src.bytes = 4;
2477 /* NB. Immediates are sign-extended as necessary. */
2478 switch (c->src.bytes) {
2479 case 1:
2480 c->src.val = insn_fetch(s8, 1, c->eip);
2481 break;
2482 case 2:
2483 c->src.val = insn_fetch(s16, 2, c->eip);
2484 break;
2485 case 4:
2486 c->src.val = insn_fetch(s32, 4, c->eip);
2487 break;
2488 }
2489 if ((c->d & SrcMask) == SrcImmU) {
2490 switch (c->src.bytes) {
2491 case 1:
2492 c->src.val &= 0xff;
2493 break;
2494 case 2:
2495 c->src.val &= 0xffff;
2496 break;
2497 case 4:
2498 c->src.val &= 0xffffffff;
2499 break;
2500 }
2501 }
2502 break;
2503 case SrcImmByte:
2504 case SrcImmUByte:
2505 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002506 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002507 c->src.bytes = 1;
2508 if ((c->d & SrcMask) == SrcImmByte)
2509 c->src.val = insn_fetch(s8, 1, c->eip);
2510 else
2511 c->src.val = insn_fetch(u8, 1, c->eip);
2512 break;
2513 case SrcAcc:
2514 c->src.type = OP_REG;
2515 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002516 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002517 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002518 break;
2519 case SrcOne:
2520 c->src.bytes = 1;
2521 c->src.val = 1;
2522 break;
2523 case SrcSI:
2524 c->src.type = OP_MEM;
2525 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002526 c->src.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002527 register_address(c, seg_override_base(ctxt, ops, c),
2528 c->regs[VCPU_REGS_RSI]);
2529 c->src.val = 0;
2530 break;
2531 case SrcImmFAddr:
2532 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002533 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002534 c->src.bytes = c->op_bytes + 2;
2535 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2536 break;
2537 case SrcMemFAddr:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002538 memop.bytes = c->op_bytes + 2;
2539 goto srcmem_common;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002540 break;
2541 }
2542
2543 /*
2544 * Decode and fetch the second source operand: register, memory
2545 * or immediate.
2546 */
2547 switch (c->d & Src2Mask) {
2548 case Src2None:
2549 break;
2550 case Src2CL:
2551 c->src2.bytes = 1;
2552 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2553 break;
2554 case Src2ImmByte:
2555 c->src2.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002556 c->src2.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002557 c->src2.bytes = 1;
2558 c->src2.val = insn_fetch(u8, 1, c->eip);
2559 break;
2560 case Src2One:
2561 c->src2.bytes = 1;
2562 c->src2.val = 1;
2563 break;
2564 }
2565
2566 /* Decode and fetch the destination operand: register or memory. */
2567 switch (c->d & DstMask) {
2568 case ImplicitOps:
2569 /* Special instructions do their own operand decoding. */
2570 return 0;
2571 case DstReg:
2572 decode_register_operand(&c->dst, c,
2573 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2574 break;
2575 case DstMem:
2576 case DstMem64:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002577 c->dst = memop;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002578 if ((c->d & DstMask) == DstMem64)
2579 c->dst.bytes = 8;
2580 else
2581 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002582 if (c->dst.type == OP_MEM && (c->d & BitOp)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002583 unsigned long mask = ~(c->dst.bytes * 8 - 1);
2584
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002585 c->dst.addr.mem = c->dst.addr.mem +
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002586 (c->src.val & mask) / 8;
2587 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002588 c->dst.orig_val = c->dst.val;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002589 break;
2590 case DstAcc:
2591 c->dst.type = OP_REG;
2592 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002593 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002594 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002595 c->dst.orig_val = c->dst.val;
2596 break;
2597 case DstDI:
2598 c->dst.type = OP_MEM;
2599 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002600 c->dst.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002601 register_address(c, es_base(ctxt, ops),
2602 c->regs[VCPU_REGS_RDI]);
2603 c->dst.val = 0;
2604 break;
2605 }
2606
2607done:
2608 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2609}
2610
2611int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002612x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002613{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002614 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002615 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002616 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002617 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002618 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002619
Gleb Natapov9de41572010-04-28 19:15:22 +03002620 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002621
Gleb Natapov11616242010-02-11 14:43:14 +02002622 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002623 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002624 goto done;
2625 }
2626
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002627 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002628 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002629 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002630 goto done;
2631 }
2632
Gleb Natapove92805a2010-02-10 14:21:35 +02002633 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002634 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002635 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002636 goto done;
2637 }
2638
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002639 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002640 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002641 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002642 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002643 string_done:
2644 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002645 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002646 goto done;
2647 }
2648 /* The second termination condition only applies for REPE
2649 * and REPNE. Test if the repeat string operation prefix is
2650 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2651 * corresponding termination condition according to:
2652 * - if REPE/REPZ and ZF = 0 then done
2653 * - if REPNE/REPNZ and ZF = 1 then done
2654 */
2655 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002656 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002657 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002658 ((ctxt->eflags & EFLG_ZF) == 0))
2659 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002660 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002661 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2662 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002663 }
Gleb Natapov063db062010-03-18 15:20:06 +02002664 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002665 }
2666
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002667 if (c->src.type == OP_MEM) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002668 if (c->d & NoAccess)
2669 goto no_fetch;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002670 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002671 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002672 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002673 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002674 c->src.orig_val64 = c->src.val64;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03002675 no_fetch:
2676 ;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002677 }
2678
Gleb Natapove35b7b92010-02-25 16:36:42 +02002679 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002680 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002681 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002682 if (rc != X86EMUL_CONTINUE)
2683 goto done;
2684 }
2685
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002686 if ((c->d & DstMask) == ImplicitOps)
2687 goto special_insn;
2688
2689
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002690 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2691 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002692 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002693 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002694 if (rc != X86EMUL_CONTINUE)
2695 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002696 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002697 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002698
Avi Kivity018a98d2007-11-27 19:30:56 +02002699special_insn:
2700
Avi Kivityef65c882010-07-29 15:11:51 +03002701 if (c->execute) {
2702 rc = c->execute(ctxt);
2703 if (rc != X86EMUL_CONTINUE)
2704 goto done;
2705 goto writeback;
2706 }
2707
Laurent Viviere4e03de2007-09-18 11:52:50 +02002708 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 goto twobyte_insn;
2710
Laurent Viviere4e03de2007-09-18 11:52:50 +02002711 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 case 0x00 ... 0x05:
2713 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002714 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002716 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002717 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002718 break;
2719 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002720 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002721 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002722 goto done;
2723 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 case 0x08 ... 0x0d:
2725 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002726 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002728 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002729 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002730 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 case 0x10 ... 0x15:
2732 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002733 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002735 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002736 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002737 break;
2738 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002739 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002740 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002741 goto done;
2742 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 case 0x18 ... 0x1d:
2744 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002745 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002747 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002748 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002749 break;
2750 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002751 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002752 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002753 goto done;
2754 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002755 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002757 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 break;
2759 case 0x28 ... 0x2d:
2760 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002761 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 break;
2763 case 0x30 ... 0x35:
2764 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002765 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 break;
2767 case 0x38 ... 0x3d:
2768 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002769 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002771 case 0x40 ... 0x47: /* inc r16/r32 */
2772 emulate_1op("inc", c->dst, ctxt->eflags);
2773 break;
2774 case 0x48 ... 0x4f: /* dec r16/r32 */
2775 emulate_1op("dec", c->dst, ctxt->eflags);
2776 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002777 case 0x58 ... 0x5f: /* pop reg */
2778 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002779 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002780 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002781 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002782 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002783 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002784 rc = emulate_pusha(ctxt, ops);
2785 if (rc != X86EMUL_CONTINUE)
2786 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002787 break;
2788 case 0x61: /* popa */
2789 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002790 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002791 goto done;
2792 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002794 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002796 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002798 case 0x6c: /* insb */
2799 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002800 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002801 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002802 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002803 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002804 goto done;
2805 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002806 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2807 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002808 goto done; /* IO is needed, skip writeback */
2809 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002810 case 0x6e: /* outsb */
2811 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002812 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002813 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002814 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002815 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002816 goto done;
2817 }
Gleb Natapov79729952010-03-18 15:20:24 +02002818 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2819 &c->src.val, 1, ctxt->vcpu);
2820
2821 c->dst.type = OP_NONE; /* nothing to writeback */
2822 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002823 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002824 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002825 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002826 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002828 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 case 0:
2830 goto add;
2831 case 1:
2832 goto or;
2833 case 2:
2834 goto adc;
2835 case 3:
2836 goto sbb;
2837 case 4:
2838 goto and;
2839 case 5:
2840 goto sub;
2841 case 6:
2842 goto xor;
2843 case 7:
2844 goto cmp;
2845 }
2846 break;
2847 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002848 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002849 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 break;
2851 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002852 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002854 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002856 *(u8 *) c->src.addr.reg = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 break;
2858 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002859 *(u16 *) c->src.addr.reg = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 break;
2861 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002862 *c->src.addr.reg = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 break; /* 64b reg: zero-extend */
2864 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002865 *c->src.addr.reg = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 break;
2867 }
2868 /*
2869 * Write back the memory destination with implicit LOCK
2870 * prefix.
2871 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002872 c->dst.val = c->src.val;
2873 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002876 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002877 case 0x8c: /* mov r/m, sreg */
2878 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002879 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002880 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002881 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002882 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002883 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002884 case 0x8d: /* lea r16/r32, m */
Avi Kivity342fc632010-08-01 15:13:22 +03002885 c->dst.val = c->src.addr.mem;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002886 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002887 case 0x8e: { /* mov seg, r/m16 */
2888 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002889
2890 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002891
Gleb Natapovc6975182010-02-18 12:15:01 +02002892 if (c->modrm_reg == VCPU_SREG_CS ||
2893 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002894 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002895 goto done;
2896 }
2897
Glauber Costa310b5d32009-05-12 16:21:06 -04002898 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002899 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002900
Gleb Natapov2e873022010-03-18 15:20:18 +02002901 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002902
2903 c->dst.type = OP_NONE; /* Disable writeback. */
2904 break;
2905 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002907 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002908 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002911 case 0x90 ... 0x97: /* nop / xchg reg, rax */
2912 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
Mohammed Gamal34698d82010-08-04 14:41:04 +03002913 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002914 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002915 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002916 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002917 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002918 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002919 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002920 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002921 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002922 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002923 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2924 if (rc != X86EMUL_CONTINUE)
2925 goto done;
2926 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002927 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002929 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002931 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002932 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02002933 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002934 case 0xa8 ... 0xa9: /* test ax, imm */
2935 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002936 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002937 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938 break;
2939 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002940 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 case 0xae ... 0xaf: /* scas */
2942 DPRINTF("Urk! I don't handle SCAS.\n");
2943 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002944 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002945 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002946 case 0xc0 ... 0xc1:
2947 emulate_grp2(ctxt);
2948 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002949 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002950 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002951 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002952 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002953 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002954 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2955 mov:
2956 c->dst.val = c->src.val;
2957 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002958 case 0xcb: /* ret far */
2959 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002960 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002961 goto done;
2962 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002963 case 0xcf: /* iret */
2964 rc = emulate_iret(ctxt, ops);
2965
2966 if (rc != X86EMUL_CONTINUE)
2967 goto done;
2968 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002969 case 0xd0 ... 0xd1: /* Grp2 */
2970 c->src.val = 1;
2971 emulate_grp2(ctxt);
2972 break;
2973 case 0xd2 ... 0xd3: /* Grp2 */
2974 c->src.val = c->regs[VCPU_REGS_RCX];
2975 emulate_grp2(ctxt);
2976 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002977 case 0xe4: /* inb */
2978 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002979 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002980 case 0xe6: /* outb */
2981 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002982 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002983 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002984 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002985 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002986 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002987 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002988 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002989 }
2990 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002991 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03002992 case 0xea: { /* jmp far */
2993 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02002994 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03002995 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
2996
2997 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02002998 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02002999
Gleb Natapov414e6272010-04-28 19:15:26 +03003000 c->eip = 0;
3001 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003002 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003003 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003004 case 0xeb:
3005 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003006 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003007 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003008 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003009 case 0xec: /* in al,dx */
3010 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003011 c->src.val = c->regs[VCPU_REGS_RDX];
3012 do_io_in:
3013 c->dst.bytes = min(c->dst.bytes, 4u);
3014 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003015 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003016 goto done;
3017 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003018 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3019 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003020 goto done; /* IO is needed */
3021 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003022 case 0xee: /* out dx,al */
3023 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003024 c->src.val = c->regs[VCPU_REGS_RDX];
3025 do_io_out:
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 Natapovcf8f70b2010-03-18 15:20:23 +02003029 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003030 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003031 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3032 ctxt->vcpu);
3033 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003034 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003035 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003036 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003037 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003038 case 0xf5: /* cmc */
3039 /* complement carry flag from eflags reg */
3040 ctxt->eflags ^= EFLG_CF;
3041 c->dst.type = OP_NONE; /* Disable writeback. */
3042 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003043 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003044 if (!emulate_grp3(ctxt, ops))
3045 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003046 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003047 case 0xf8: /* clc */
3048 ctxt->eflags &= ~EFLG_CF;
3049 c->dst.type = OP_NONE; /* Disable writeback. */
3050 break;
3051 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003052 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003053 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003054 goto done;
3055 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003056 ctxt->eflags &= ~X86_EFLAGS_IF;
3057 c->dst.type = OP_NONE; /* Disable writeback. */
3058 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003059 break;
3060 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003061 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003062 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003063 goto done;
3064 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003065 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003066 ctxt->eflags |= X86_EFLAGS_IF;
3067 c->dst.type = OP_NONE; /* Disable writeback. */
3068 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003069 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003070 case 0xfc: /* cld */
3071 ctxt->eflags &= ~EFLG_DF;
3072 c->dst.type = OP_NONE; /* Disable writeback. */
3073 break;
3074 case 0xfd: /* std */
3075 ctxt->eflags |= EFLG_DF;
3076 c->dst.type = OP_NONE; /* Disable writeback. */
3077 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003078 case 0xfe: /* Grp4 */
3079 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003080 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003081 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003082 goto done;
3083 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003084 case 0xff: /* Grp5 */
3085 if (c->modrm_reg == 5)
3086 goto jump_far;
3087 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003088 default:
3089 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003091
3092writeback:
3093 rc = writeback(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
Gleb Natapov5cd21912010-03-18 15:20:26 +02003097 /*
3098 * restore dst type in case the decoding will be reused
3099 * (happens for string instruction )
3100 */
3101 c->dst.type = saved_dst_type;
3102
Gleb Natapova682e352010-03-18 15:20:21 +02003103 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003104 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3105 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003106
3107 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003108 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3109 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003110
Gleb Natapov5cd21912010-03-18 15:20:26 +02003111 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003112 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003113 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003114 /*
3115 * Re-enter guest when pio read ahead buffer is empty or,
3116 * if it is not used, after each 1024 iteration.
3117 */
3118 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3119 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003120 ctxt->restart = false;
3121 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003122 /*
3123 * reset read cache here in case string instruction is restared
3124 * without decoding
3125 */
3126 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003127 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003128
3129done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003130 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131
3132twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003133 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003135 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 u16 size;
3137 unsigned long address;
3138
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003139 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003140 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003141 goto cannot_emulate;
3142
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003143 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003144 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003145 goto done;
3146
Avi Kivity33e38852008-05-21 15:34:25 +03003147 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003148 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003149 /* Disable writeback. */
3150 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003151 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003153 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003154 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003155 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 goto done;
3157 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003158 /* Disable writeback. */
3159 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003161 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003162 if (c->modrm_mod == 3) {
3163 switch (c->modrm_rm) {
3164 case 1:
3165 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003166 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003167 goto done;
3168 break;
3169 default:
3170 goto cannot_emulate;
3171 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003172 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003173 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003174 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003175 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003176 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003177 goto done;
3178 realmode_lidt(ctxt->vcpu, size, address);
3179 }
Avi Kivity16286d02008-04-14 14:40:50 +03003180 /* Disable writeback. */
3181 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 break;
3183 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003184 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003185 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 break;
3187 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003188 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003189 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003190 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003192 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003193 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003194 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 case 7: /* invlpg*/
Avi Kivity1f6f0582010-08-01 15:19:22 +03003196 emulate_invlpg(ctxt->vcpu, c->src.addr.mem);
Avi Kivity16286d02008-04-14 14:40:50 +03003197 /* Disable writeback. */
3198 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 break;
3200 default:
3201 goto cannot_emulate;
3202 }
3203 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003204 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003205 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003206 if (rc != X86EMUL_CONTINUE)
3207 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003208 else
3209 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003210 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003211 case 0x06:
3212 emulate_clts(ctxt->vcpu);
3213 c->dst.type = OP_NONE;
3214 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003215 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003216 kvm_emulate_wbinvd(ctxt->vcpu);
3217 c->dst.type = OP_NONE;
3218 break;
3219 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003220 case 0x0d: /* GrpP (prefetch) */
3221 case 0x18: /* Grp16 (prefetch/nop) */
3222 c->dst.type = OP_NONE;
3223 break;
3224 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003225 switch (c->modrm_reg) {
3226 case 1:
3227 case 5 ... 7:
3228 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003229 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003230 goto done;
3231 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003232 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003233 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003235 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3236 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003237 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003238 goto done;
3239 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003240 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003242 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003243 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003244 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003245 goto done;
3246 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003247 c->dst.type = OP_NONE;
3248 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003250 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3251 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003252 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003253 goto done;
3254 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003255
Avi Kivityb27f3852010-08-01 14:25:22 +03003256 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003257 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3258 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3259 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003260 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003261 goto done;
3262 }
3263
Laurent Viviera01af5e2007-09-24 11:10:56 +02003264 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003266 case 0x30:
3267 /* wrmsr */
3268 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3269 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003270 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003271 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003272 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003273 }
3274 rc = X86EMUL_CONTINUE;
3275 c->dst.type = OP_NONE;
3276 break;
3277 case 0x32:
3278 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003279 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003280 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003281 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003282 } else {
3283 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3284 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3285 }
3286 rc = X86EMUL_CONTINUE;
3287 c->dst.type = OP_NONE;
3288 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003289 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003290 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003291 if (rc != X86EMUL_CONTINUE)
3292 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003293 else
3294 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003295 break;
3296 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003297 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003298 if (rc != X86EMUL_CONTINUE)
3299 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003300 else
3301 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003302 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003304 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003305 if (!test_cc(c->b, ctxt->eflags))
3306 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003307 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003308 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003309 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003310 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003311 c->dst.type = OP_NONE;
3312 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003313 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003314 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003315 break;
3316 case 0xa1: /* pop fs */
3317 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003318 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003319 goto done;
3320 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003321 case 0xa3:
3322 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003323 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003324 /* only subword offset */
3325 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003326 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003327 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003328 case 0xa4: /* shld imm8, r, r/m */
3329 case 0xa5: /* shld cl, r, r/m */
3330 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3331 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003332 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003333 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003334 break;
3335 case 0xa9: /* pop gs */
3336 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003337 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003338 goto done;
3339 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003340 case 0xab:
3341 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003342 /* only subword offset */
3343 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003344 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003345 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003346 case 0xac: /* shrd imm8, r, r/m */
3347 case 0xad: /* shrd cl, r, r/m */
3348 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3349 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003350 case 0xae: /* clflush */
3351 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003352 case 0xb0 ... 0xb1: /* cmpxchg */
3353 /*
3354 * Save real source value, then compare EAX against
3355 * destination.
3356 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003357 c->src.orig_val = c->src.val;
3358 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003359 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3360 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003362 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 } else {
3364 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003365 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003366 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367 }
3368 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 case 0xb3:
3370 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003371 /* only subword offset */
3372 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003373 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003376 c->dst.bytes = c->op_bytes;
3377 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3378 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003381 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003382 case 0:
3383 goto bt;
3384 case 1:
3385 goto bts;
3386 case 2:
3387 goto btr;
3388 case 3:
3389 goto btc;
3390 }
3391 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003392 case 0xbb:
3393 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003394 /* only subword offset */
3395 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003396 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003397 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003399 c->dst.bytes = c->op_bytes;
3400 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3401 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003403 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003404 c->dst.bytes = c->op_bytes;
3405 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3406 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003407 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003409 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003410 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003411 goto done;
3412 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003413 default:
3414 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415 }
3416 goto writeback;
3417
3418cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003419 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420 return -1;
3421}