blob: b7da0e3e0cc0aa3aca5228133d00ce9991ce9eee [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 Kivity047a4812010-07-26 14:37:47 +030086#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020087#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020088#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030089#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010090/* Source 2 operand type */
91#define Src2None (0<<29)
92#define Src2CL (1<<29)
93#define Src2ImmByte (2<<29)
94#define Src2One (3<<29)
95#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
Avi Kivityd0e53322010-07-29 15:11:54 +030097#define X2(x...) x, x
98#define X3(x...) X2(x), x
99#define X4(x...) X2(x), X2(x)
100#define X5(x...) X4(x), x
101#define X6(x...) X4(x), X2(x)
102#define X7(x...) X4(x), X3(x)
103#define X8(x...) X4(x), X4(x)
104#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300105
Avi Kivityd65b1de2010-07-29 15:11:35 +0300106struct opcode {
107 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300108 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300109 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300110 struct opcode *group;
111 struct group_dual *gdual;
112 } u;
113};
114
115struct group_dual {
116 struct opcode mod012[8];
117 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300118};
119
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200121#define EFLG_ID (1<<21)
122#define EFLG_VIP (1<<20)
123#define EFLG_VIF (1<<19)
124#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200125#define EFLG_VM (1<<17)
126#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200127#define EFLG_IOPL (3<<12)
128#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129#define EFLG_OF (1<<11)
130#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200131#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200132#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133#define EFLG_SF (1<<7)
134#define EFLG_ZF (1<<6)
135#define EFLG_AF (1<<4)
136#define EFLG_PF (1<<2)
137#define EFLG_CF (1<<0)
138
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300139#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
140#define EFLG_RESERVED_ONE_MASK 2
141
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142/*
143 * Instruction emulation:
144 * Most instructions are emulated directly via a fragment of inline assembly
145 * code. This allows us to save/restore EFLAGS and thus very easily pick up
146 * any modified flags.
147 */
148
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800149#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150#define _LO32 "k" /* force 32-bit operand */
151#define _STK "%%rsp" /* stack pointer */
152#elif defined(__i386__)
153#define _LO32 "" /* force 32-bit operand */
154#define _STK "%%esp" /* stack pointer */
155#endif
156
157/*
158 * These EFLAGS bits are restored from saved value during emulation, and
159 * any changes are written back to the saved value after emulation.
160 */
161#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
162
163/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200164#define _PRE_EFLAGS(_sav, _msk, _tmp) \
165 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
166 "movl %"_sav",%"_LO32 _tmp"; " \
167 "push %"_tmp"; " \
168 "push %"_tmp"; " \
169 "movl %"_msk",%"_LO32 _tmp"; " \
170 "andl %"_LO32 _tmp",("_STK"); " \
171 "pushf; " \
172 "notl %"_LO32 _tmp"; " \
173 "andl %"_LO32 _tmp",("_STK"); " \
174 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
175 "pop %"_tmp"; " \
176 "orl %"_LO32 _tmp",("_STK"); " \
177 "popf; " \
178 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179
180/* After executing instruction: write-back necessary bits in EFLAGS. */
181#define _POST_EFLAGS(_sav, _msk, _tmp) \
182 /* _sav |= EFLAGS & _msk; */ \
183 "pushf; " \
184 "pop %"_tmp"; " \
185 "andl %"_msk",%"_LO32 _tmp"; " \
186 "orl %"_LO32 _tmp",%"_sav"; "
187
Avi Kivitydda96d82008-11-26 15:14:10 +0200188#ifdef CONFIG_X86_64
189#define ON64(x) x
190#else
191#define ON64(x)
192#endif
193
Avi Kivity6b7ad612008-11-26 15:30:45 +0200194#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
195 do { \
196 __asm__ __volatile__ ( \
197 _PRE_EFLAGS("0", "4", "2") \
198 _op _suffix " %"_x"3,%1; " \
199 _POST_EFLAGS("0", "4", "2") \
200 : "=m" (_eflags), "=m" ((_dst).val), \
201 "=&r" (_tmp) \
202 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200203 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200204
205
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206/* Raw emulation: instruction has two explicit operands. */
207#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200208 do { \
209 unsigned long _tmp; \
210 \
211 switch ((_dst).bytes) { \
212 case 2: \
213 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
214 break; \
215 case 4: \
216 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
217 break; \
218 case 8: \
219 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
220 break; \
221 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222 } while (0)
223
224#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
225 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200226 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400227 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200229 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 break; \
231 default: \
232 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
233 _wx, _wy, _lx, _ly, _qx, _qy); \
234 break; \
235 } \
236 } while (0)
237
238/* Source operand is byte-sized and may be restricted to just %cl. */
239#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
240 __emulate_2op(_op, _src, _dst, _eflags, \
241 "b", "c", "b", "c", "b", "c", "b", "c")
242
243/* Source operand is byte, word, long or quad sized. */
244#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
245 __emulate_2op(_op, _src, _dst, _eflags, \
246 "b", "q", "w", "r", _LO32, "r", "", "r")
247
248/* Source operand is word, long or quad sized. */
249#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
250 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
251 "w", "r", _LO32, "r", "", "r")
252
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100253/* Instruction has three operands and one operand is stored in ECX register */
254#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
255 do { \
256 unsigned long _tmp; \
257 _type _clv = (_cl).val; \
258 _type _srcv = (_src).val; \
259 _type _dstv = (_dst).val; \
260 \
261 __asm__ __volatile__ ( \
262 _PRE_EFLAGS("0", "5", "2") \
263 _op _suffix " %4,%1 \n" \
264 _POST_EFLAGS("0", "5", "2") \
265 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
266 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
267 ); \
268 \
269 (_cl).val = (unsigned long) _clv; \
270 (_src).val = (unsigned long) _srcv; \
271 (_dst).val = (unsigned long) _dstv; \
272 } while (0)
273
274#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
275 do { \
276 switch ((_dst).bytes) { \
277 case 2: \
278 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
279 "w", unsigned short); \
280 break; \
281 case 4: \
282 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
283 "l", unsigned int); \
284 break; \
285 case 8: \
286 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
287 "q", unsigned long)); \
288 break; \
289 } \
290 } while (0)
291
Avi Kivitydda96d82008-11-26 15:14:10 +0200292#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 do { \
294 unsigned long _tmp; \
295 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200296 __asm__ __volatile__ ( \
297 _PRE_EFLAGS("0", "3", "2") \
298 _op _suffix " %1; " \
299 _POST_EFLAGS("0", "3", "2") \
300 : "=m" (_eflags), "+m" ((_dst).val), \
301 "=&r" (_tmp) \
302 : "i" (EFLAGS_MASK)); \
303 } while (0)
304
305/* Instruction has only one explicit operand (no source operand). */
306#define emulate_1op(_op, _dst, _eflags) \
307 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400308 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200309 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
310 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
311 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
312 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313 } \
314 } while (0)
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316/* Fetch next part of the instruction being emulated. */
317#define insn_fetch(_type, _size, _eip) \
318({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200319 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200320 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321 goto done; \
322 (_eip) += (_size); \
323 (_type)_x; \
324})
325
Gleb Natapov414e6272010-04-28 19:15:26 +0300326#define insn_fetch_arr(_arr, _size, _eip) \
327({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
328 if (rc != X86EMUL_CONTINUE) \
329 goto done; \
330 (_eip) += (_size); \
331})
332
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800333static inline unsigned long ad_mask(struct decode_cache *c)
334{
335 return (1UL << (c->ad_bytes << 3)) - 1;
336}
337
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800339static inline unsigned long
340address_mask(struct decode_cache *c, unsigned long reg)
341{
342 if (c->ad_bytes == sizeof(unsigned long))
343 return reg;
344 else
345 return reg & ad_mask(c);
346}
347
348static inline unsigned long
349register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
350{
351 return base + address_mask(c, reg);
352}
353
Harvey Harrison7a9572752008-02-19 07:40:41 -0800354static inline void
355register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
356{
357 if (c->ad_bytes == sizeof(unsigned long))
358 *reg += inc;
359 else
360 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
361}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362
Harvey Harrison7a9572752008-02-19 07:40:41 -0800363static inline void jmp_rel(struct decode_cache *c, int rel)
364{
365 register_address_increment(c, &c->eip, rel);
366}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300367
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300368static void set_seg_override(struct decode_cache *c, int seg)
369{
370 c->has_seg_override = true;
371 c->seg_override = seg;
372}
373
Gleb Natapov79168fd2010-04-28 19:15:30 +0300374static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
375 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300376{
377 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
378 return 0;
379
Gleb Natapov79168fd2010-04-28 19:15:30 +0300380 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300381}
382
383static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300384 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300385 struct decode_cache *c)
386{
387 if (!c->has_seg_override)
388 return 0;
389
Gleb Natapov79168fd2010-04-28 19:15:30 +0300390 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300391}
392
Gleb Natapov79168fd2010-04-28 19:15:30 +0300393static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
394 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300395{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300396 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300397}
398
Gleb Natapov79168fd2010-04-28 19:15:30 +0300399static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
400 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300401{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300402 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300403}
404
Gleb Natapov54b84862010-04-28 19:15:44 +0300405static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
406 u32 error, bool valid)
407{
408 ctxt->exception = vec;
409 ctxt->error_code = error;
410 ctxt->error_code_valid = valid;
411 ctxt->restart = false;
412}
413
414static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
415{
416 emulate_exception(ctxt, GP_VECTOR, err, true);
417}
418
419static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
420 int err)
421{
422 ctxt->cr2 = addr;
423 emulate_exception(ctxt, PF_VECTOR, err, true);
424}
425
426static void emulate_ud(struct x86_emulate_ctxt *ctxt)
427{
428 emulate_exception(ctxt, UD_VECTOR, 0, false);
429}
430
431static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
432{
433 emulate_exception(ctxt, TS_VECTOR, err, true);
434}
435
Avi Kivity62266862007-11-20 13:15:52 +0200436static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
437 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300438 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200439{
440 struct fetch_cache *fc = &ctxt->decode.fetch;
441 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300442 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200443
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300444 if (eip == fc->end) {
445 cur_size = fc->end - fc->start;
446 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
447 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
448 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900449 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200450 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300451 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200452 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300453 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900454 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200455}
456
457static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
458 struct x86_emulate_ops *ops,
459 unsigned long eip, void *dest, unsigned size)
460{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900461 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200462
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200463 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200464 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200465 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200466 while (size--) {
467 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900468 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200469 return rc;
470 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900471 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200472}
473
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000474/*
475 * Given the 'reg' portion of a ModRM byte, and a register block, return a
476 * pointer into the block that addresses the relevant register.
477 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
478 */
479static void *decode_register(u8 modrm_reg, unsigned long *regs,
480 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481{
482 void *p;
483
484 p = &regs[modrm_reg];
485 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
486 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
487 return p;
488}
489
490static int read_descriptor(struct x86_emulate_ctxt *ctxt,
491 struct x86_emulate_ops *ops,
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300492 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493 u16 *size, unsigned long *address, int op_bytes)
494{
495 int rc;
496
497 if (op_bytes == 2)
498 op_bytes = 3;
499 *address = 0;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300500 rc = ops->read_std(addr, (unsigned long *)size, 2, ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900501 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502 return rc;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300503 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 return rc;
505}
506
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300507static int test_cc(unsigned int condition, unsigned int flags)
508{
509 int rc = 0;
510
511 switch ((condition & 15) >> 1) {
512 case 0: /* o */
513 rc |= (flags & EFLG_OF);
514 break;
515 case 1: /* b/c/nae */
516 rc |= (flags & EFLG_CF);
517 break;
518 case 2: /* z/e */
519 rc |= (flags & EFLG_ZF);
520 break;
521 case 3: /* be/na */
522 rc |= (flags & (EFLG_CF|EFLG_ZF));
523 break;
524 case 4: /* s */
525 rc |= (flags & EFLG_SF);
526 break;
527 case 5: /* p/pe */
528 rc |= (flags & EFLG_PF);
529 break;
530 case 7: /* le/ng */
531 rc |= (flags & EFLG_ZF);
532 /* fall through */
533 case 6: /* l/nge */
534 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
535 break;
536 }
537
538 /* Odd condition identifiers (lsb == 1) have inverted sense. */
539 return (!!rc ^ (condition & 1));
540}
541
Avi Kivity3c118e22007-10-31 10:27:04 +0200542static void decode_register_operand(struct operand *op,
543 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200544 int inhibit_bytereg)
545{
Avi Kivity33615aa2007-10-31 11:15:56 +0200546 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200547 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200548
549 if (!(c->d & ModRM))
550 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200551 op->type = OP_REG;
552 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300553 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
554 op->val = *(u8 *)op->addr.reg;
Avi Kivity3c118e22007-10-31 10:27:04 +0200555 op->bytes = 1;
556 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300557 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200558 op->bytes = c->op_bytes;
559 switch (op->bytes) {
560 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300561 op->val = *(u16 *)op->addr.reg;
Avi Kivity3c118e22007-10-31 10:27:04 +0200562 break;
563 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300564 op->val = *(u32 *)op->addr.reg;
Avi Kivity3c118e22007-10-31 10:27:04 +0200565 break;
566 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300567 op->val = *(u64 *) op->addr.reg;
Avi Kivity3c118e22007-10-31 10:27:04 +0200568 break;
569 }
570 }
571 op->orig_val = op->val;
572}
573
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200574static int decode_modrm(struct x86_emulate_ctxt *ctxt,
575 struct x86_emulate_ops *ops)
576{
577 struct decode_cache *c = &ctxt->decode;
578 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700579 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900580 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200581
582 if (c->rex_prefix) {
583 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
584 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
585 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
586 }
587
588 c->modrm = insn_fetch(u8, 1, c->eip);
589 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
590 c->modrm_reg |= (c->modrm & 0x38) >> 3;
591 c->modrm_rm |= (c->modrm & 0x07);
592 c->modrm_ea = 0;
593 c->use_modrm_ea = 1;
Avi Kivity09ee57c2010-08-01 12:07:29 +0300594 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200595
596 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300597 c->modrm_ptr = decode_register(c->modrm_rm,
598 c->regs, c->d & ByteOp);
599 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200600 return rc;
601 }
602
603 if (c->ad_bytes == 2) {
604 unsigned bx = c->regs[VCPU_REGS_RBX];
605 unsigned bp = c->regs[VCPU_REGS_RBP];
606 unsigned si = c->regs[VCPU_REGS_RSI];
607 unsigned di = c->regs[VCPU_REGS_RDI];
608
609 /* 16-bit ModR/M decode. */
610 switch (c->modrm_mod) {
611 case 0:
612 if (c->modrm_rm == 6)
613 c->modrm_ea += insn_fetch(u16, 2, c->eip);
614 break;
615 case 1:
616 c->modrm_ea += insn_fetch(s8, 1, c->eip);
617 break;
618 case 2:
619 c->modrm_ea += insn_fetch(u16, 2, c->eip);
620 break;
621 }
622 switch (c->modrm_rm) {
623 case 0:
624 c->modrm_ea += bx + si;
625 break;
626 case 1:
627 c->modrm_ea += bx + di;
628 break;
629 case 2:
630 c->modrm_ea += bp + si;
631 break;
632 case 3:
633 c->modrm_ea += bp + di;
634 break;
635 case 4:
636 c->modrm_ea += si;
637 break;
638 case 5:
639 c->modrm_ea += di;
640 break;
641 case 6:
642 if (c->modrm_mod != 0)
643 c->modrm_ea += bp;
644 break;
645 case 7:
646 c->modrm_ea += bx;
647 break;
648 }
649 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
650 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300651 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200652 c->modrm_ea = (u16)c->modrm_ea;
653 } else {
654 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700655 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200656 sib = insn_fetch(u8, 1, c->eip);
657 index_reg |= (sib >> 3) & 7;
658 base_reg |= sib & 7;
659 scale = sib >> 6;
660
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700661 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
662 c->modrm_ea += insn_fetch(s32, 4, c->eip);
663 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200664 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700665 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200666 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700667 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
668 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700669 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700670 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200671 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200672 switch (c->modrm_mod) {
673 case 0:
674 if (c->modrm_rm == 5)
675 c->modrm_ea += insn_fetch(s32, 4, c->eip);
676 break;
677 case 1:
678 c->modrm_ea += insn_fetch(s8, 1, c->eip);
679 break;
680 case 2:
681 c->modrm_ea += insn_fetch(s32, 4, c->eip);
682 break;
683 }
684 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200685done:
686 return rc;
687}
688
689static int decode_abs(struct x86_emulate_ctxt *ctxt,
690 struct x86_emulate_ops *ops)
691{
692 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900693 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200694
695 switch (c->ad_bytes) {
696 case 2:
697 c->modrm_ea = insn_fetch(u16, 2, c->eip);
698 break;
699 case 4:
700 c->modrm_ea = insn_fetch(u32, 4, c->eip);
701 break;
702 case 8:
703 c->modrm_ea = insn_fetch(u64, 8, c->eip);
704 break;
705 }
706done:
707 return rc;
708}
709
Gleb Natapov9de41572010-04-28 19:15:22 +0300710static int read_emulated(struct x86_emulate_ctxt *ctxt,
711 struct x86_emulate_ops *ops,
712 unsigned long addr, void *dest, unsigned size)
713{
714 int rc;
715 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300716 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300717
718 while (size) {
719 int n = min(size, 8u);
720 size -= n;
721 if (mc->pos < mc->end)
722 goto read_cached;
723
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300724 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
725 ctxt->vcpu);
726 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300727 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300728 if (rc != X86EMUL_CONTINUE)
729 return rc;
730 mc->end += n;
731
732 read_cached:
733 memcpy(dest, mc->data + mc->pos, n);
734 mc->pos += n;
735 dest += n;
736 addr += n;
737 }
738 return X86EMUL_CONTINUE;
739}
740
Gleb Natapov7b262e92010-03-18 15:20:27 +0200741static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
742 struct x86_emulate_ops *ops,
743 unsigned int size, unsigned short port,
744 void *dest)
745{
746 struct read_cache *rc = &ctxt->decode.io_read;
747
748 if (rc->pos == rc->end) { /* refill pio read ahead */
749 struct decode_cache *c = &ctxt->decode;
750 unsigned int in_page, n;
751 unsigned int count = c->rep_prefix ?
752 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
753 in_page = (ctxt->eflags & EFLG_DF) ?
754 offset_in_page(c->regs[VCPU_REGS_RDI]) :
755 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
756 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
757 count);
758 if (n == 0)
759 n = 1;
760 rc->pos = rc->end = 0;
761 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
762 return 0;
763 rc->end = n * size;
764 }
765
766 memcpy(dest, rc->data + rc->pos, size);
767 rc->pos += size;
768 return 1;
769}
770
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200771static u32 desc_limit_scaled(struct desc_struct *desc)
772{
773 u32 limit = get_desc_limit(desc);
774
775 return desc->g ? (limit << 12) | 0xfff : limit;
776}
777
778static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
779 struct x86_emulate_ops *ops,
780 u16 selector, struct desc_ptr *dt)
781{
782 if (selector & 1 << 2) {
783 struct desc_struct desc;
784 memset (dt, 0, sizeof *dt);
785 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
786 return;
787
788 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
789 dt->address = get_desc_base(&desc);
790 } else
791 ops->get_gdt(dt, ctxt->vcpu);
792}
793
794/* allowed just for 8 bytes segments */
795static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
796 struct x86_emulate_ops *ops,
797 u16 selector, struct desc_struct *desc)
798{
799 struct desc_ptr dt;
800 u16 index = selector >> 3;
801 int ret;
802 u32 err;
803 ulong addr;
804
805 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
806
807 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300808 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200809 return X86EMUL_PROPAGATE_FAULT;
810 }
811 addr = dt.address + index * 8;
812 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
813 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300814 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200815
816 return ret;
817}
818
819/* allowed just for 8 bytes segments */
820static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
821 struct x86_emulate_ops *ops,
822 u16 selector, struct desc_struct *desc)
823{
824 struct desc_ptr dt;
825 u16 index = selector >> 3;
826 u32 err;
827 ulong addr;
828 int ret;
829
830 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
831
832 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300833 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200834 return X86EMUL_PROPAGATE_FAULT;
835 }
836
837 addr = dt.address + index * 8;
838 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
839 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300840 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200841
842 return ret;
843}
844
845static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
846 struct x86_emulate_ops *ops,
847 u16 selector, int seg)
848{
849 struct desc_struct seg_desc;
850 u8 dpl, rpl, cpl;
851 unsigned err_vec = GP_VECTOR;
852 u32 err_code = 0;
853 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
854 int ret;
855
856 memset(&seg_desc, 0, sizeof seg_desc);
857
858 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
859 || ctxt->mode == X86EMUL_MODE_REAL) {
860 /* set real mode segment descriptor */
861 set_desc_base(&seg_desc, selector << 4);
862 set_desc_limit(&seg_desc, 0xffff);
863 seg_desc.type = 3;
864 seg_desc.p = 1;
865 seg_desc.s = 1;
866 goto load;
867 }
868
869 /* NULL selector is not valid for TR, CS and SS */
870 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
871 && null_selector)
872 goto exception;
873
874 /* TR should be in GDT only */
875 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
876 goto exception;
877
878 if (null_selector) /* for NULL selector skip all following checks */
879 goto load;
880
881 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
882 if (ret != X86EMUL_CONTINUE)
883 return ret;
884
885 err_code = selector & 0xfffc;
886 err_vec = GP_VECTOR;
887
888 /* can't load system descriptor into segment selecor */
889 if (seg <= VCPU_SREG_GS && !seg_desc.s)
890 goto exception;
891
892 if (!seg_desc.p) {
893 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
894 goto exception;
895 }
896
897 rpl = selector & 3;
898 dpl = seg_desc.dpl;
899 cpl = ops->cpl(ctxt->vcpu);
900
901 switch (seg) {
902 case VCPU_SREG_SS:
903 /*
904 * segment is not a writable data segment or segment
905 * selector's RPL != CPL or segment selector's RPL != CPL
906 */
907 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
908 goto exception;
909 break;
910 case VCPU_SREG_CS:
911 if (!(seg_desc.type & 8))
912 goto exception;
913
914 if (seg_desc.type & 4) {
915 /* conforming */
916 if (dpl > cpl)
917 goto exception;
918 } else {
919 /* nonconforming */
920 if (rpl > cpl || dpl != cpl)
921 goto exception;
922 }
923 /* CS(RPL) <- CPL */
924 selector = (selector & 0xfffc) | cpl;
925 break;
926 case VCPU_SREG_TR:
927 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
928 goto exception;
929 break;
930 case VCPU_SREG_LDTR:
931 if (seg_desc.s || seg_desc.type != 2)
932 goto exception;
933 break;
934 default: /* DS, ES, FS, or GS */
935 /*
936 * segment is not a data or readable code segment or
937 * ((segment is a data or nonconforming code segment)
938 * and (both RPL and CPL > DPL))
939 */
940 if ((seg_desc.type & 0xa) == 0x8 ||
941 (((seg_desc.type & 0xc) != 0xc) &&
942 (rpl > dpl && cpl > dpl)))
943 goto exception;
944 break;
945 }
946
947 if (seg_desc.s) {
948 /* mark segment as accessed */
949 seg_desc.type |= 1;
950 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
951 if (ret != X86EMUL_CONTINUE)
952 return ret;
953 }
954load:
955 ops->set_segment_selector(selector, seg, ctxt->vcpu);
956 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
957 return X86EMUL_CONTINUE;
958exception:
Gleb Natapov54b84862010-04-28 19:15:44 +0300959 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200960 return X86EMUL_PROPAGATE_FAULT;
961}
962
Wei Yongjunc37eda12010-06-15 09:03:33 +0800963static inline int writeback(struct x86_emulate_ctxt *ctxt,
964 struct x86_emulate_ops *ops)
965{
966 int rc;
967 struct decode_cache *c = &ctxt->decode;
968 u32 err;
969
970 switch (c->dst.type) {
971 case OP_REG:
972 /* The 4-byte case *is* correct:
973 * in 64-bit mode we zero-extend.
974 */
975 switch (c->dst.bytes) {
976 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300977 *(u8 *)c->dst.addr.reg = (u8)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800978 break;
979 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300980 *(u16 *)c->dst.addr.reg = (u16)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800981 break;
982 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300983 *c->dst.addr.reg = (u32)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800984 break; /* 64b: zero-ext */
985 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300986 *c->dst.addr.reg = c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800987 break;
988 }
989 break;
990 case OP_MEM:
991 if (c->lock_prefix)
992 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300993 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +0800994 &c->dst.orig_val,
995 &c->dst.val,
996 c->dst.bytes,
997 &err,
998 ctxt->vcpu);
999 else
1000 rc = ops->write_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001001 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001002 &c->dst.val,
1003 c->dst.bytes,
1004 &err,
1005 ctxt->vcpu);
1006 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001007 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001008 if (rc != X86EMUL_CONTINUE)
1009 return rc;
1010 break;
1011 case OP_NONE:
1012 /* no writeback */
1013 break;
1014 default:
1015 break;
1016 }
1017 return X86EMUL_CONTINUE;
1018}
1019
Gleb Natapov79168fd2010-04-28 19:15:30 +03001020static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1021 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001022{
1023 struct decode_cache *c = &ctxt->decode;
1024
1025 c->dst.type = OP_MEM;
1026 c->dst.bytes = c->op_bytes;
1027 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001028 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001029 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1030 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001031}
1032
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001033static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001034 struct x86_emulate_ops *ops,
1035 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001036{
1037 struct decode_cache *c = &ctxt->decode;
1038 int rc;
1039
Gleb Natapov79168fd2010-04-28 19:15:30 +03001040 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001041 c->regs[VCPU_REGS_RSP]),
1042 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001043 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001044 return rc;
1045
Avi Kivity350f69d2009-01-05 11:12:40 +02001046 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001047 return rc;
1048}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001049
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001050static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1051 struct x86_emulate_ops *ops,
1052 void *dest, int len)
1053{
1054 int rc;
1055 unsigned long val, change_mask;
1056 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001057 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001058
1059 rc = emulate_pop(ctxt, ops, &val, len);
1060 if (rc != X86EMUL_CONTINUE)
1061 return rc;
1062
1063 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1064 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1065
1066 switch(ctxt->mode) {
1067 case X86EMUL_MODE_PROT64:
1068 case X86EMUL_MODE_PROT32:
1069 case X86EMUL_MODE_PROT16:
1070 if (cpl == 0)
1071 change_mask |= EFLG_IOPL;
1072 if (cpl <= iopl)
1073 change_mask |= EFLG_IF;
1074 break;
1075 case X86EMUL_MODE_VM86:
1076 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001077 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001078 return X86EMUL_PROPAGATE_FAULT;
1079 }
1080 change_mask |= EFLG_IF;
1081 break;
1082 default: /* real mode */
1083 change_mask |= (EFLG_IOPL | EFLG_IF);
1084 break;
1085 }
1086
1087 *(unsigned long *)dest =
1088 (ctxt->eflags & ~change_mask) | (val & change_mask);
1089
1090 return rc;
1091}
1092
Gleb Natapov79168fd2010-04-28 19:15:30 +03001093static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1094 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001095{
1096 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001097
Gleb Natapov79168fd2010-04-28 19:15:30 +03001098 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001099
Gleb Natapov79168fd2010-04-28 19:15:30 +03001100 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001101}
1102
1103static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1104 struct x86_emulate_ops *ops, int seg)
1105{
1106 struct decode_cache *c = &ctxt->decode;
1107 unsigned long selector;
1108 int rc;
1109
1110 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001111 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001112 return rc;
1113
Gleb Natapov2e873022010-03-18 15:20:18 +02001114 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001115 return rc;
1116}
1117
Wei Yongjunc37eda12010-06-15 09:03:33 +08001118static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001119 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001120{
1121 struct decode_cache *c = &ctxt->decode;
1122 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001123 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001124 int reg = VCPU_REGS_RAX;
1125
1126 while (reg <= VCPU_REGS_RDI) {
1127 (reg == VCPU_REGS_RSP) ?
1128 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1129
Gleb Natapov79168fd2010-04-28 19:15:30 +03001130 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001131
1132 rc = writeback(ctxt, ops);
1133 if (rc != X86EMUL_CONTINUE)
1134 return rc;
1135
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001136 ++reg;
1137 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001138
1139 /* Disable writeback. */
1140 c->dst.type = OP_NONE;
1141
1142 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001143}
1144
1145static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1146 struct x86_emulate_ops *ops)
1147{
1148 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001149 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001150 int reg = VCPU_REGS_RDI;
1151
1152 while (reg >= VCPU_REGS_RAX) {
1153 if (reg == VCPU_REGS_RSP) {
1154 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1155 c->op_bytes);
1156 --reg;
1157 }
1158
1159 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001160 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001161 break;
1162 --reg;
1163 }
1164 return rc;
1165}
1166
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001167static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1168 struct x86_emulate_ops *ops)
1169{
1170 struct decode_cache *c = &ctxt->decode;
1171 int rc = X86EMUL_CONTINUE;
1172 unsigned long temp_eip = 0;
1173 unsigned long temp_eflags = 0;
1174 unsigned long cs = 0;
1175 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1176 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1177 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1178 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1179
1180 /* TODO: Add stack limit check */
1181
1182 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1183
1184 if (rc != X86EMUL_CONTINUE)
1185 return rc;
1186
1187 if (temp_eip & ~0xffff) {
1188 emulate_gp(ctxt, 0);
1189 return X86EMUL_PROPAGATE_FAULT;
1190 }
1191
1192 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1193
1194 if (rc != X86EMUL_CONTINUE)
1195 return rc;
1196
1197 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1198
1199 if (rc != X86EMUL_CONTINUE)
1200 return rc;
1201
1202 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1203
1204 if (rc != X86EMUL_CONTINUE)
1205 return rc;
1206
1207 c->eip = temp_eip;
1208
1209
1210 if (c->op_bytes == 4)
1211 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1212 else if (c->op_bytes == 2) {
1213 ctxt->eflags &= ~0xffff;
1214 ctxt->eflags |= temp_eflags;
1215 }
1216
1217 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1218 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1219
1220 return rc;
1221}
1222
1223static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1224 struct x86_emulate_ops* ops)
1225{
1226 switch(ctxt->mode) {
1227 case X86EMUL_MODE_REAL:
1228 return emulate_iret_real(ctxt, ops);
1229 case X86EMUL_MODE_VM86:
1230 case X86EMUL_MODE_PROT16:
1231 case X86EMUL_MODE_PROT32:
1232 case X86EMUL_MODE_PROT64:
1233 default:
1234 /* iret from protected mode unimplemented yet */
1235 return X86EMUL_UNHANDLEABLE;
1236 }
1237}
1238
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001239static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1240 struct x86_emulate_ops *ops)
1241{
1242 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001243
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001244 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001245}
1246
Laurent Vivier05f086f2007-09-24 11:10:55 +02001247static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001248{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001249 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001250 switch (c->modrm_reg) {
1251 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001252 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253 break;
1254 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001255 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001256 break;
1257 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001258 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001259 break;
1260 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001261 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001262 break;
1263 case 4: /* sal/shl */
1264 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001265 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001266 break;
1267 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001268 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001269 break;
1270 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001271 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001272 break;
1273 }
1274}
1275
1276static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001277 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001278{
1279 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001280
1281 switch (c->modrm_reg) {
1282 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001283 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001284 break;
1285 case 2: /* not */
1286 c->dst.val = ~c->dst.val;
1287 break;
1288 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001289 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001290 break;
1291 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001292 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001293 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001294 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001295}
1296
1297static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001298 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001299{
1300 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001301
1302 switch (c->modrm_reg) {
1303 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001304 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001305 break;
1306 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001307 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001308 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001309 case 2: /* call near abs */ {
1310 long int old_eip;
1311 old_eip = c->eip;
1312 c->eip = c->src.val;
1313 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001314 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001315 break;
1316 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001317 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001318 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001319 break;
1320 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001321 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001322 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001323 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001324 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001325}
1326
1327static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001328 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001329{
1330 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001331 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001332
1333 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1334 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001335 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1336 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001337 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001338 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001339 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1340 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001341
Laurent Vivier05f086f2007-09-24 11:10:55 +02001342 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001343 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001344 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001345}
1346
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001347static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1348 struct x86_emulate_ops *ops)
1349{
1350 struct decode_cache *c = &ctxt->decode;
1351 int rc;
1352 unsigned long cs;
1353
1354 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001355 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001356 return rc;
1357 if (c->op_bytes == 4)
1358 c->eip = (u32)c->eip;
1359 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001360 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001361 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001362 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001363 return rc;
1364}
1365
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001366static inline void
1367setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001368 struct x86_emulate_ops *ops, struct desc_struct *cs,
1369 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001370{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001371 memset(cs, 0, sizeof(struct desc_struct));
1372 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1373 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001374
1375 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001376 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001377 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001378 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001379 cs->type = 0x0b; /* Read, Execute, Accessed */
1380 cs->s = 1;
1381 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001382 cs->p = 1;
1383 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001384
Gleb Natapov79168fd2010-04-28 19:15:30 +03001385 set_desc_base(ss, 0); /* flat segment */
1386 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001387 ss->g = 1; /* 4kb granularity */
1388 ss->s = 1;
1389 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001390 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001391 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001392 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001393}
1394
1395static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001396emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001397{
1398 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001399 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001400 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001401 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001402
1403 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001404 if (ctxt->mode == X86EMUL_MODE_REAL ||
1405 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001406 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001407 return X86EMUL_PROPAGATE_FAULT;
1408 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001409
Gleb Natapov79168fd2010-04-28 19:15:30 +03001410 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001411
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001412 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001413 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001414 cs_sel = (u16)(msr_data & 0xfffc);
1415 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001416
1417 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001418 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001419 cs.l = 1;
1420 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001421 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1422 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1423 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1424 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001425
1426 c->regs[VCPU_REGS_RCX] = c->eip;
1427 if (is_long_mode(ctxt->vcpu)) {
1428#ifdef CONFIG_X86_64
1429 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1430
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001431 ops->get_msr(ctxt->vcpu,
1432 ctxt->mode == X86EMUL_MODE_PROT64 ?
1433 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001434 c->eip = msr_data;
1435
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001436 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001437 ctxt->eflags &= ~(msr_data | EFLG_RF);
1438#endif
1439 } else {
1440 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001441 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001442 c->eip = (u32)msr_data;
1443
1444 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1445 }
1446
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001447 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001448}
1449
Andre Przywara8c604352009-06-18 12:56:01 +02001450static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001451emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001452{
1453 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001454 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001455 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001456 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001457
Gleb Natapova0044752010-02-10 14:21:31 +02001458 /* inject #GP if in real mode */
1459 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001460 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001461 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001462 }
1463
1464 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1465 * Therefore, we inject an #UD.
1466 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001467 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001468 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001469 return X86EMUL_PROPAGATE_FAULT;
1470 }
Andre Przywara8c604352009-06-18 12:56:01 +02001471
Gleb Natapov79168fd2010-04-28 19:15:30 +03001472 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001473
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001474 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001475 switch (ctxt->mode) {
1476 case X86EMUL_MODE_PROT32:
1477 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001478 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001479 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001480 }
1481 break;
1482 case X86EMUL_MODE_PROT64:
1483 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001484 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001485 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001486 }
1487 break;
1488 }
1489
1490 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001491 cs_sel = (u16)msr_data;
1492 cs_sel &= ~SELECTOR_RPL_MASK;
1493 ss_sel = cs_sel + 8;
1494 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001495 if (ctxt->mode == X86EMUL_MODE_PROT64
1496 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001497 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001498 cs.l = 1;
1499 }
1500
Gleb Natapov79168fd2010-04-28 19:15:30 +03001501 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1502 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1503 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1504 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001505
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001506 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001507 c->eip = msr_data;
1508
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001509 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001510 c->regs[VCPU_REGS_RSP] = msr_data;
1511
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001512 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001513}
1514
Andre Przywara4668f052009-06-18 12:56:02 +02001515static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001516emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001517{
1518 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001519 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001520 u64 msr_data;
1521 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001522 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001523
Gleb Natapova0044752010-02-10 14:21:31 +02001524 /* inject #GP if in real mode or Virtual 8086 mode */
1525 if (ctxt->mode == X86EMUL_MODE_REAL ||
1526 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001527 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001528 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001529 }
1530
Gleb Natapov79168fd2010-04-28 19:15:30 +03001531 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001532
1533 if ((c->rex_prefix & 0x8) != 0x0)
1534 usermode = X86EMUL_MODE_PROT64;
1535 else
1536 usermode = X86EMUL_MODE_PROT32;
1537
1538 cs.dpl = 3;
1539 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001540 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001541 switch (usermode) {
1542 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001543 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001544 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001545 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001546 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001547 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001548 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001549 break;
1550 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001551 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001552 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001553 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001554 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001555 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001556 ss_sel = cs_sel + 8;
1557 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001558 cs.l = 1;
1559 break;
1560 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001561 cs_sel |= SELECTOR_RPL_MASK;
1562 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001563
Gleb Natapov79168fd2010-04-28 19:15:30 +03001564 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1565 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1566 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1567 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001568
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001569 c->eip = c->regs[VCPU_REGS_RDX];
1570 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001571
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001572 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001573}
1574
Gleb Natapov9c537242010-03-18 15:20:05 +02001575static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1576 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001577{
1578 int iopl;
1579 if (ctxt->mode == X86EMUL_MODE_REAL)
1580 return false;
1581 if (ctxt->mode == X86EMUL_MODE_VM86)
1582 return true;
1583 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001584 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001585}
1586
1587static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1588 struct x86_emulate_ops *ops,
1589 u16 port, u16 len)
1590{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001591 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001592 int r;
1593 u16 io_bitmap_ptr;
1594 u8 perm, bit_idx = port & 0x7;
1595 unsigned mask = (1 << len) - 1;
1596
Gleb Natapov79168fd2010-04-28 19:15:30 +03001597 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1598 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001599 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001600 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001601 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001602 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1603 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001604 if (r != X86EMUL_CONTINUE)
1605 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001606 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001607 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001608 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1609 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001610 if (r != X86EMUL_CONTINUE)
1611 return false;
1612 if ((perm >> bit_idx) & mask)
1613 return false;
1614 return true;
1615}
1616
1617static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1618 struct x86_emulate_ops *ops,
1619 u16 port, u16 len)
1620{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001621 if (ctxt->perm_ok)
1622 return true;
1623
Gleb Natapov9c537242010-03-18 15:20:05 +02001624 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001625 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1626 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001627
1628 ctxt->perm_ok = true;
1629
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001630 return true;
1631}
1632
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001633static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1634 struct x86_emulate_ops *ops,
1635 struct tss_segment_16 *tss)
1636{
1637 struct decode_cache *c = &ctxt->decode;
1638
1639 tss->ip = c->eip;
1640 tss->flag = ctxt->eflags;
1641 tss->ax = c->regs[VCPU_REGS_RAX];
1642 tss->cx = c->regs[VCPU_REGS_RCX];
1643 tss->dx = c->regs[VCPU_REGS_RDX];
1644 tss->bx = c->regs[VCPU_REGS_RBX];
1645 tss->sp = c->regs[VCPU_REGS_RSP];
1646 tss->bp = c->regs[VCPU_REGS_RBP];
1647 tss->si = c->regs[VCPU_REGS_RSI];
1648 tss->di = c->regs[VCPU_REGS_RDI];
1649
1650 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1651 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1652 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1653 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1654 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1655}
1656
1657static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1658 struct x86_emulate_ops *ops,
1659 struct tss_segment_16 *tss)
1660{
1661 struct decode_cache *c = &ctxt->decode;
1662 int ret;
1663
1664 c->eip = tss->ip;
1665 ctxt->eflags = tss->flag | 2;
1666 c->regs[VCPU_REGS_RAX] = tss->ax;
1667 c->regs[VCPU_REGS_RCX] = tss->cx;
1668 c->regs[VCPU_REGS_RDX] = tss->dx;
1669 c->regs[VCPU_REGS_RBX] = tss->bx;
1670 c->regs[VCPU_REGS_RSP] = tss->sp;
1671 c->regs[VCPU_REGS_RBP] = tss->bp;
1672 c->regs[VCPU_REGS_RSI] = tss->si;
1673 c->regs[VCPU_REGS_RDI] = tss->di;
1674
1675 /*
1676 * SDM says that segment selectors are loaded before segment
1677 * descriptors
1678 */
1679 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1680 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1681 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1682 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1683 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1684
1685 /*
1686 * Now load segment descriptors. If fault happenes at this stage
1687 * it is handled in a context of new task
1688 */
1689 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1690 if (ret != X86EMUL_CONTINUE)
1691 return ret;
1692 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1693 if (ret != X86EMUL_CONTINUE)
1694 return ret;
1695 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1696 if (ret != X86EMUL_CONTINUE)
1697 return ret;
1698 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1699 if (ret != X86EMUL_CONTINUE)
1700 return ret;
1701 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1702 if (ret != X86EMUL_CONTINUE)
1703 return ret;
1704
1705 return X86EMUL_CONTINUE;
1706}
1707
1708static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1709 struct x86_emulate_ops *ops,
1710 u16 tss_selector, u16 old_tss_sel,
1711 ulong old_tss_base, struct desc_struct *new_desc)
1712{
1713 struct tss_segment_16 tss_seg;
1714 int ret;
1715 u32 err, new_tss_base = get_desc_base(new_desc);
1716
1717 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1718 &err);
1719 if (ret == X86EMUL_PROPAGATE_FAULT) {
1720 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001721 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001722 return ret;
1723 }
1724
1725 save_state_to_tss16(ctxt, ops, &tss_seg);
1726
1727 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1728 &err);
1729 if (ret == X86EMUL_PROPAGATE_FAULT) {
1730 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001731 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001732 return ret;
1733 }
1734
1735 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1736 &err);
1737 if (ret == X86EMUL_PROPAGATE_FAULT) {
1738 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001739 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001740 return ret;
1741 }
1742
1743 if (old_tss_sel != 0xffff) {
1744 tss_seg.prev_task_link = old_tss_sel;
1745
1746 ret = ops->write_std(new_tss_base,
1747 &tss_seg.prev_task_link,
1748 sizeof tss_seg.prev_task_link,
1749 ctxt->vcpu, &err);
1750 if (ret == X86EMUL_PROPAGATE_FAULT) {
1751 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001752 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001753 return ret;
1754 }
1755 }
1756
1757 return load_state_from_tss16(ctxt, ops, &tss_seg);
1758}
1759
1760static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1761 struct x86_emulate_ops *ops,
1762 struct tss_segment_32 *tss)
1763{
1764 struct decode_cache *c = &ctxt->decode;
1765
1766 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1767 tss->eip = c->eip;
1768 tss->eflags = ctxt->eflags;
1769 tss->eax = c->regs[VCPU_REGS_RAX];
1770 tss->ecx = c->regs[VCPU_REGS_RCX];
1771 tss->edx = c->regs[VCPU_REGS_RDX];
1772 tss->ebx = c->regs[VCPU_REGS_RBX];
1773 tss->esp = c->regs[VCPU_REGS_RSP];
1774 tss->ebp = c->regs[VCPU_REGS_RBP];
1775 tss->esi = c->regs[VCPU_REGS_RSI];
1776 tss->edi = c->regs[VCPU_REGS_RDI];
1777
1778 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1779 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1780 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1781 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1782 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1783 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1784 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1785}
1786
1787static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1788 struct x86_emulate_ops *ops,
1789 struct tss_segment_32 *tss)
1790{
1791 struct decode_cache *c = &ctxt->decode;
1792 int ret;
1793
Gleb Natapov0f122442010-04-28 19:15:31 +03001794 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001795 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001796 return X86EMUL_PROPAGATE_FAULT;
1797 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001798 c->eip = tss->eip;
1799 ctxt->eflags = tss->eflags | 2;
1800 c->regs[VCPU_REGS_RAX] = tss->eax;
1801 c->regs[VCPU_REGS_RCX] = tss->ecx;
1802 c->regs[VCPU_REGS_RDX] = tss->edx;
1803 c->regs[VCPU_REGS_RBX] = tss->ebx;
1804 c->regs[VCPU_REGS_RSP] = tss->esp;
1805 c->regs[VCPU_REGS_RBP] = tss->ebp;
1806 c->regs[VCPU_REGS_RSI] = tss->esi;
1807 c->regs[VCPU_REGS_RDI] = tss->edi;
1808
1809 /*
1810 * SDM says that segment selectors are loaded before segment
1811 * descriptors
1812 */
1813 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1814 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1815 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1816 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1817 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1818 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1819 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1820
1821 /*
1822 * Now load segment descriptors. If fault happenes at this stage
1823 * it is handled in a context of new task
1824 */
1825 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1826 if (ret != X86EMUL_CONTINUE)
1827 return ret;
1828 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1829 if (ret != X86EMUL_CONTINUE)
1830 return ret;
1831 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1832 if (ret != X86EMUL_CONTINUE)
1833 return ret;
1834 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1835 if (ret != X86EMUL_CONTINUE)
1836 return ret;
1837 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1838 if (ret != X86EMUL_CONTINUE)
1839 return ret;
1840 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1841 if (ret != X86EMUL_CONTINUE)
1842 return ret;
1843 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1844 if (ret != X86EMUL_CONTINUE)
1845 return ret;
1846
1847 return X86EMUL_CONTINUE;
1848}
1849
1850static int task_switch_32(struct x86_emulate_ctxt *ctxt,
1851 struct x86_emulate_ops *ops,
1852 u16 tss_selector, u16 old_tss_sel,
1853 ulong old_tss_base, struct desc_struct *new_desc)
1854{
1855 struct tss_segment_32 tss_seg;
1856 int ret;
1857 u32 err, new_tss_base = get_desc_base(new_desc);
1858
1859 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1860 &err);
1861 if (ret == X86EMUL_PROPAGATE_FAULT) {
1862 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001863 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001864 return ret;
1865 }
1866
1867 save_state_to_tss32(ctxt, ops, &tss_seg);
1868
1869 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1870 &err);
1871 if (ret == X86EMUL_PROPAGATE_FAULT) {
1872 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001873 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001874 return ret;
1875 }
1876
1877 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1878 &err);
1879 if (ret == X86EMUL_PROPAGATE_FAULT) {
1880 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001881 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001882 return ret;
1883 }
1884
1885 if (old_tss_sel != 0xffff) {
1886 tss_seg.prev_task_link = old_tss_sel;
1887
1888 ret = ops->write_std(new_tss_base,
1889 &tss_seg.prev_task_link,
1890 sizeof tss_seg.prev_task_link,
1891 ctxt->vcpu, &err);
1892 if (ret == X86EMUL_PROPAGATE_FAULT) {
1893 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001894 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001895 return ret;
1896 }
1897 }
1898
1899 return load_state_from_tss32(ctxt, ops, &tss_seg);
1900}
1901
1902static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001903 struct x86_emulate_ops *ops,
1904 u16 tss_selector, int reason,
1905 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001906{
1907 struct desc_struct curr_tss_desc, next_tss_desc;
1908 int ret;
1909 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
1910 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03001911 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02001912 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001913
1914 /* FIXME: old_tss_base == ~0 ? */
1915
1916 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
1917 if (ret != X86EMUL_CONTINUE)
1918 return ret;
1919 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
1920 if (ret != X86EMUL_CONTINUE)
1921 return ret;
1922
1923 /* FIXME: check that next_tss_desc is tss */
1924
1925 if (reason != TASK_SWITCH_IRET) {
1926 if ((tss_selector & 3) > next_tss_desc.dpl ||
1927 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001928 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001929 return X86EMUL_PROPAGATE_FAULT;
1930 }
1931 }
1932
Gleb Natapovceffb452010-03-18 15:20:19 +02001933 desc_limit = desc_limit_scaled(&next_tss_desc);
1934 if (!next_tss_desc.p ||
1935 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
1936 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001937 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001938 return X86EMUL_PROPAGATE_FAULT;
1939 }
1940
1941 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
1942 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
1943 write_segment_descriptor(ctxt, ops, old_tss_sel,
1944 &curr_tss_desc);
1945 }
1946
1947 if (reason == TASK_SWITCH_IRET)
1948 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
1949
1950 /* set back link to prev task only if NT bit is set in eflags
1951 note that old_tss_sel is not used afetr this point */
1952 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
1953 old_tss_sel = 0xffff;
1954
1955 if (next_tss_desc.type & 8)
1956 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
1957 old_tss_base, &next_tss_desc);
1958 else
1959 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
1960 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02001961 if (ret != X86EMUL_CONTINUE)
1962 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001963
1964 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
1965 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
1966
1967 if (reason != TASK_SWITCH_IRET) {
1968 next_tss_desc.type |= (1 << 1); /* set busy flag */
1969 write_segment_descriptor(ctxt, ops, tss_selector,
1970 &next_tss_desc);
1971 }
1972
1973 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
1974 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
1975 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
1976
Jan Kiszkae269fb22010-04-14 15:51:09 +02001977 if (has_error_code) {
1978 struct decode_cache *c = &ctxt->decode;
1979
1980 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
1981 c->lock_prefix = 0;
1982 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001983 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02001984 }
1985
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001986 return ret;
1987}
1988
1989int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001990 u16 tss_selector, int reason,
1991 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001992{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03001993 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001994 struct decode_cache *c = &ctxt->decode;
1995 int rc;
1996
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001997 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02001998 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001999
Jan Kiszkae269fb22010-04-14 15:51:09 +02002000 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2001 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002002
2003 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002004 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002005 if (rc == X86EMUL_CONTINUE)
2006 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002007 }
2008
Gleb Natapov19d04432010-04-15 12:29:50 +03002009 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002010}
2011
Gleb Natapova682e352010-03-18 15:20:21 +02002012static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002013 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002014{
2015 struct decode_cache *c = &ctxt->decode;
2016 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2017
Gleb Natapovd9271122010-03-18 15:20:22 +02002018 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002019 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002020}
2021
Avi Kivity63540382010-07-29 15:11:55 +03002022static int em_push(struct x86_emulate_ctxt *ctxt)
2023{
2024 emulate_push(ctxt, ctxt->ops);
2025 return X86EMUL_CONTINUE;
2026}
2027
Avi Kivity73fba5f2010-07-29 15:11:53 +03002028#define D(_y) { .flags = (_y) }
2029#define N D(0)
2030#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2031#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2032#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2033
2034static struct opcode group1[] = {
2035 X7(D(Lock)), N
2036};
2037
2038static struct opcode group1A[] = {
2039 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2040};
2041
2042static struct opcode group3[] = {
2043 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2044 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2045 X4(D(Undefined)),
2046};
2047
2048static struct opcode group4[] = {
2049 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2050 N, N, N, N, N, N,
2051};
2052
2053static struct opcode group5[] = {
2054 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2055 D(SrcMem | ModRM | Stack), N,
2056 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2057 D(SrcMem | ModRM | Stack), N,
2058};
2059
2060static struct group_dual group7 = { {
2061 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2062 D(SrcNone | ModRM | DstMem | Mov), N,
2063 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
2064}, {
2065 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2066 D(SrcNone | ModRM | DstMem | Mov), N,
2067 D(SrcMem16 | ModRM | Mov | Priv), N,
2068} };
2069
2070static struct opcode group8[] = {
2071 N, N, N, N,
2072 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2073 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2074};
2075
2076static struct group_dual group9 = { {
2077 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2078}, {
2079 N, N, N, N, N, N, N, N,
2080} };
2081
2082static struct opcode opcode_table[256] = {
2083 /* 0x00 - 0x07 */
2084 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2085 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2086 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2087 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2088 /* 0x08 - 0x0F */
2089 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2090 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2091 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2092 D(ImplicitOps | Stack | No64), N,
2093 /* 0x10 - 0x17 */
2094 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2095 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2096 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2097 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2098 /* 0x18 - 0x1F */
2099 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2100 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2101 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2102 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2103 /* 0x20 - 0x27 */
2104 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2105 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2106 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2107 /* 0x28 - 0x2F */
2108 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2109 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2110 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2111 /* 0x30 - 0x37 */
2112 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2113 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2114 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2115 /* 0x38 - 0x3F */
2116 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2117 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2118 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2119 N, N,
2120 /* 0x40 - 0x4F */
2121 X16(D(DstReg)),
2122 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002123 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002124 /* 0x58 - 0x5F */
2125 X8(D(DstReg | Stack)),
2126 /* 0x60 - 0x67 */
2127 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2128 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2129 N, N, N, N,
2130 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002131 I(SrcImm | Mov | Stack, em_push), N,
2132 I(SrcImmByte | Mov | Stack, em_push), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002133 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2134 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2135 /* 0x70 - 0x7F */
2136 X16(D(SrcImmByte)),
2137 /* 0x80 - 0x87 */
2138 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2139 G(DstMem | SrcImm | ModRM | Group, group1),
2140 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2141 G(DstMem | SrcImmByte | ModRM | Group, group1),
2142 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2143 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2144 /* 0x88 - 0x8F */
2145 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2146 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
2147 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
2148 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2149 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002150 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002151 /* 0x98 - 0x9F */
2152 N, N, D(SrcImmFAddr | No64), N,
2153 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2154 /* 0xA0 - 0xA7 */
2155 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2156 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2157 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2158 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2159 /* 0xA8 - 0xAF */
2160 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
2161 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
2162 D(ByteOp | DstDI | String), D(DstDI | String),
2163 /* 0xB0 - 0xB7 */
2164 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2165 /* 0xB8 - 0xBF */
2166 X8(D(DstReg | SrcImm | Mov)),
2167 /* 0xC0 - 0xC7 */
2168 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
2169 N, D(ImplicitOps | Stack), N, N,
2170 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2171 /* 0xC8 - 0xCF */
2172 N, N, N, D(ImplicitOps | Stack),
2173 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2174 /* 0xD0 - 0xD7 */
2175 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2176 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2177 N, N, N, N,
2178 /* 0xD8 - 0xDF */
2179 N, N, N, N, N, N, N, N,
2180 /* 0xE0 - 0xE7 */
2181 N, N, N, N,
2182 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2183 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2184 /* 0xE8 - 0xEF */
2185 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2186 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2187 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2188 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2189 /* 0xF0 - 0xF7 */
2190 N, N, N, N,
2191 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2192 /* 0xF8 - 0xFF */
2193 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
2194 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2195};
2196
2197static struct opcode twobyte_table[256] = {
2198 /* 0x00 - 0x0F */
2199 N, GD(0, &group7), N, N,
2200 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2201 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2202 N, D(ImplicitOps | ModRM), N, N,
2203 /* 0x10 - 0x1F */
2204 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2205 /* 0x20 - 0x2F */
2206 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
2207 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
2208 N, N, N, N,
2209 N, N, N, N, N, N, N, N,
2210 /* 0x30 - 0x3F */
2211 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
2212 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2213 N, N, N, N, N, N, N, N,
2214 /* 0x40 - 0x4F */
2215 X16(D(DstReg | SrcMem | ModRM | Mov)),
2216 /* 0x50 - 0x5F */
2217 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2218 /* 0x60 - 0x6F */
2219 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2220 /* 0x70 - 0x7F */
2221 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2222 /* 0x80 - 0x8F */
2223 X16(D(SrcImm)),
2224 /* 0x90 - 0x9F */
2225 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2226 /* 0xA0 - 0xA7 */
2227 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2228 N, D(DstMem | SrcReg | ModRM | BitOp),
2229 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2230 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2231 /* 0xA8 - 0xAF */
2232 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2233 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2234 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2235 D(DstMem | SrcReg | Src2CL | ModRM),
2236 D(ModRM), N,
2237 /* 0xB0 - 0xB7 */
2238 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2239 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2240 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2241 D(DstReg | SrcMem16 | ModRM | Mov),
2242 /* 0xB8 - 0xBF */
2243 N, N,
2244 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2245 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2246 D(DstReg | SrcMem16 | ModRM | Mov),
2247 /* 0xC0 - 0xCF */
2248 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
2249 N, N, N, GD(0, &group9),
2250 N, N, N, N, N, N, N, N,
2251 /* 0xD0 - 0xDF */
2252 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2253 /* 0xE0 - 0xEF */
2254 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2255 /* 0xF0 - 0xFF */
2256 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2257};
2258
2259#undef D
2260#undef N
2261#undef G
2262#undef GD
2263#undef I
2264
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002265int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002266x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2267{
2268 struct x86_emulate_ops *ops = ctxt->ops;
2269 struct decode_cache *c = &ctxt->decode;
2270 int rc = X86EMUL_CONTINUE;
2271 int mode = ctxt->mode;
2272 int def_op_bytes, def_ad_bytes, dual, goffset;
2273 struct opcode opcode, *g_mod012, *g_mod3;
2274
2275 /* we cannot decode insn before we complete previous rep insn */
2276 WARN_ON(ctxt->restart);
2277
2278 c->eip = ctxt->eip;
2279 c->fetch.start = c->fetch.end = c->eip;
2280 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2281
2282 switch (mode) {
2283 case X86EMUL_MODE_REAL:
2284 case X86EMUL_MODE_VM86:
2285 case X86EMUL_MODE_PROT16:
2286 def_op_bytes = def_ad_bytes = 2;
2287 break;
2288 case X86EMUL_MODE_PROT32:
2289 def_op_bytes = def_ad_bytes = 4;
2290 break;
2291#ifdef CONFIG_X86_64
2292 case X86EMUL_MODE_PROT64:
2293 def_op_bytes = 4;
2294 def_ad_bytes = 8;
2295 break;
2296#endif
2297 default:
2298 return -1;
2299 }
2300
2301 c->op_bytes = def_op_bytes;
2302 c->ad_bytes = def_ad_bytes;
2303
2304 /* Legacy prefixes. */
2305 for (;;) {
2306 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2307 case 0x66: /* operand-size override */
2308 /* switch between 2/4 bytes */
2309 c->op_bytes = def_op_bytes ^ 6;
2310 break;
2311 case 0x67: /* address-size override */
2312 if (mode == X86EMUL_MODE_PROT64)
2313 /* switch between 4/8 bytes */
2314 c->ad_bytes = def_ad_bytes ^ 12;
2315 else
2316 /* switch between 2/4 bytes */
2317 c->ad_bytes = def_ad_bytes ^ 6;
2318 break;
2319 case 0x26: /* ES override */
2320 case 0x2e: /* CS override */
2321 case 0x36: /* SS override */
2322 case 0x3e: /* DS override */
2323 set_seg_override(c, (c->b >> 3) & 3);
2324 break;
2325 case 0x64: /* FS override */
2326 case 0x65: /* GS override */
2327 set_seg_override(c, c->b & 7);
2328 break;
2329 case 0x40 ... 0x4f: /* REX */
2330 if (mode != X86EMUL_MODE_PROT64)
2331 goto done_prefixes;
2332 c->rex_prefix = c->b;
2333 continue;
2334 case 0xf0: /* LOCK */
2335 c->lock_prefix = 1;
2336 break;
2337 case 0xf2: /* REPNE/REPNZ */
2338 c->rep_prefix = REPNE_PREFIX;
2339 break;
2340 case 0xf3: /* REP/REPE/REPZ */
2341 c->rep_prefix = REPE_PREFIX;
2342 break;
2343 default:
2344 goto done_prefixes;
2345 }
2346
2347 /* Any legacy prefix after a REX prefix nullifies its effect. */
2348
2349 c->rex_prefix = 0;
2350 }
2351
2352done_prefixes:
2353
2354 /* REX prefix. */
2355 if (c->rex_prefix)
2356 if (c->rex_prefix & 8)
2357 c->op_bytes = 8; /* REX.W */
2358
2359 /* Opcode byte(s). */
2360 opcode = opcode_table[c->b];
2361 if (opcode.flags == 0) {
2362 /* Two-byte opcode? */
2363 if (c->b == 0x0f) {
2364 c->twobyte = 1;
2365 c->b = insn_fetch(u8, 1, c->eip);
2366 opcode = twobyte_table[c->b];
2367 }
2368 }
2369 c->d = opcode.flags;
2370
2371 if (c->d & Group) {
2372 dual = c->d & GroupDual;
2373 c->modrm = insn_fetch(u8, 1, c->eip);
2374 --c->eip;
2375
2376 if (c->d & GroupDual) {
2377 g_mod012 = opcode.u.gdual->mod012;
2378 g_mod3 = opcode.u.gdual->mod3;
2379 } else
2380 g_mod012 = g_mod3 = opcode.u.group;
2381
2382 c->d &= ~(Group | GroupDual);
2383
2384 goffset = (c->modrm >> 3) & 7;
2385
2386 if ((c->modrm >> 6) == 3)
2387 opcode = g_mod3[goffset];
2388 else
2389 opcode = g_mod012[goffset];
2390 c->d |= opcode.flags;
2391 }
2392
2393 c->execute = opcode.u.execute;
2394
2395 /* Unrecognised? */
2396 if (c->d == 0 || (c->d & Undefined)) {
2397 DPRINTF("Cannot emulate %02x\n", c->b);
2398 return -1;
2399 }
2400
2401 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2402 c->op_bytes = 8;
2403
2404 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002405 if (c->d & ModRM) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002406 rc = decode_modrm(ctxt, ops);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002407 if (!c->has_seg_override)
2408 set_seg_override(c, c->modrm_seg);
2409 } else if (c->d & MemAbs)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002410 rc = decode_abs(ctxt, ops);
2411 if (rc != X86EMUL_CONTINUE)
2412 goto done;
2413
2414 if (!c->has_seg_override)
2415 set_seg_override(c, VCPU_SREG_DS);
2416
2417 if (!(!c->twobyte && c->b == 0x8d))
2418 c->modrm_ea += seg_override_base(ctxt, ops, c);
2419
2420 if (c->ad_bytes != 8)
2421 c->modrm_ea = (u32)c->modrm_ea;
2422
2423 if (c->rip_relative)
2424 c->modrm_ea += c->eip;
2425
2426 /*
2427 * Decode and fetch the source operand: register, memory
2428 * or immediate.
2429 */
2430 switch (c->d & SrcMask) {
2431 case SrcNone:
2432 break;
2433 case SrcReg:
2434 decode_register_operand(&c->src, c, 0);
2435 break;
2436 case SrcMem16:
2437 c->src.bytes = 2;
2438 goto srcmem_common;
2439 case SrcMem32:
2440 c->src.bytes = 4;
2441 goto srcmem_common;
2442 case SrcMem:
2443 c->src.bytes = (c->d & ByteOp) ? 1 :
2444 c->op_bytes;
2445 /* Don't fetch the address for invlpg: it could be unmapped. */
2446 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
2447 break;
2448 srcmem_common:
2449 /*
2450 * For instructions with a ModR/M byte, switch to register
2451 * access if Mod = 3.
2452 */
2453 if ((c->d & ModRM) && c->modrm_mod == 3) {
2454 c->src.type = OP_REG;
2455 c->src.val = c->modrm_val;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002456 c->src.addr.reg = c->modrm_ptr;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002457 break;
2458 }
2459 c->src.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002460 c->src.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002461 c->src.val = 0;
2462 break;
2463 case SrcImm:
2464 case SrcImmU:
2465 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002466 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002467 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2468 if (c->src.bytes == 8)
2469 c->src.bytes = 4;
2470 /* NB. Immediates are sign-extended as necessary. */
2471 switch (c->src.bytes) {
2472 case 1:
2473 c->src.val = insn_fetch(s8, 1, c->eip);
2474 break;
2475 case 2:
2476 c->src.val = insn_fetch(s16, 2, c->eip);
2477 break;
2478 case 4:
2479 c->src.val = insn_fetch(s32, 4, c->eip);
2480 break;
2481 }
2482 if ((c->d & SrcMask) == SrcImmU) {
2483 switch (c->src.bytes) {
2484 case 1:
2485 c->src.val &= 0xff;
2486 break;
2487 case 2:
2488 c->src.val &= 0xffff;
2489 break;
2490 case 4:
2491 c->src.val &= 0xffffffff;
2492 break;
2493 }
2494 }
2495 break;
2496 case SrcImmByte:
2497 case SrcImmUByte:
2498 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002499 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002500 c->src.bytes = 1;
2501 if ((c->d & SrcMask) == SrcImmByte)
2502 c->src.val = insn_fetch(s8, 1, c->eip);
2503 else
2504 c->src.val = insn_fetch(u8, 1, c->eip);
2505 break;
2506 case SrcAcc:
2507 c->src.type = OP_REG;
2508 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002509 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002510 switch (c->src.bytes) {
2511 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002512 c->src.val = *(u8 *)c->src.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002513 break;
2514 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002515 c->src.val = *(u16 *)c->src.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002516 break;
2517 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002518 c->src.val = *(u32 *)c->src.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002519 break;
2520 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002521 c->src.val = *(u64 *)c->src.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002522 break;
2523 }
2524 break;
2525 case SrcOne:
2526 c->src.bytes = 1;
2527 c->src.val = 1;
2528 break;
2529 case SrcSI:
2530 c->src.type = OP_MEM;
2531 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002532 c->src.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002533 register_address(c, seg_override_base(ctxt, ops, c),
2534 c->regs[VCPU_REGS_RSI]);
2535 c->src.val = 0;
2536 break;
2537 case SrcImmFAddr:
2538 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002539 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002540 c->src.bytes = c->op_bytes + 2;
2541 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2542 break;
2543 case SrcMemFAddr:
2544 c->src.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002545 c->src.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002546 c->src.bytes = c->op_bytes + 2;
2547 break;
2548 }
2549
2550 /*
2551 * Decode and fetch the second source operand: register, memory
2552 * or immediate.
2553 */
2554 switch (c->d & Src2Mask) {
2555 case Src2None:
2556 break;
2557 case Src2CL:
2558 c->src2.bytes = 1;
2559 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2560 break;
2561 case Src2ImmByte:
2562 c->src2.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002563 c->src2.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002564 c->src2.bytes = 1;
2565 c->src2.val = insn_fetch(u8, 1, c->eip);
2566 break;
2567 case Src2One:
2568 c->src2.bytes = 1;
2569 c->src2.val = 1;
2570 break;
2571 }
2572
2573 /* Decode and fetch the destination operand: register or memory. */
2574 switch (c->d & DstMask) {
2575 case ImplicitOps:
2576 /* Special instructions do their own operand decoding. */
2577 return 0;
2578 case DstReg:
2579 decode_register_operand(&c->dst, c,
2580 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2581 break;
2582 case DstMem:
2583 case DstMem64:
2584 if ((c->d & ModRM) && c->modrm_mod == 3) {
2585 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2586 c->dst.type = OP_REG;
2587 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002588 c->dst.addr.reg = c->modrm_ptr;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002589 break;
2590 }
2591 c->dst.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002592 c->dst.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002593 if ((c->d & DstMask) == DstMem64)
2594 c->dst.bytes = 8;
2595 else
2596 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2597 c->dst.val = 0;
2598 if (c->d & BitOp) {
2599 unsigned long mask = ~(c->dst.bytes * 8 - 1);
2600
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002601 c->dst.addr.mem = c->dst.addr.mem +
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002602 (c->src.val & mask) / 8;
2603 }
2604 break;
2605 case DstAcc:
2606 c->dst.type = OP_REG;
2607 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002608 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002609 switch (c->dst.bytes) {
2610 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002611 c->dst.val = *(u8 *)c->dst.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002612 break;
2613 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002614 c->dst.val = *(u16 *)c->dst.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002615 break;
2616 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002617 c->dst.val = *(u32 *)c->dst.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002618 break;
2619 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002620 c->dst.val = *(u64 *)c->dst.addr.reg;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002621 break;
2622 }
2623 c->dst.orig_val = c->dst.val;
2624 break;
2625 case DstDI:
2626 c->dst.type = OP_MEM;
2627 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002628 c->dst.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002629 register_address(c, es_base(ctxt, ops),
2630 c->regs[VCPU_REGS_RDI]);
2631 c->dst.val = 0;
2632 break;
2633 }
2634
2635done:
2636 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2637}
2638
2639int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002640x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002641{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002642 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002643 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002644 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002645 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002646 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002647
Gleb Natapov9de41572010-04-28 19:15:22 +03002648 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002649
Gleb Natapov11616242010-02-11 14:43:14 +02002650 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002651 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002652 goto done;
2653 }
2654
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002655 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002656 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002657 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002658 goto done;
2659 }
2660
Gleb Natapove92805a2010-02-10 14:21:35 +02002661 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002662 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002663 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002664 goto done;
2665 }
2666
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002667 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002668 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002669 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002670 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002671 string_done:
2672 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002673 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002674 goto done;
2675 }
2676 /* The second termination condition only applies for REPE
2677 * and REPNE. Test if the repeat string operation prefix is
2678 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2679 * corresponding termination condition according to:
2680 * - if REPE/REPZ and ZF = 0 then done
2681 * - if REPNE/REPNZ and ZF = 1 then done
2682 */
2683 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002684 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002685 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002686 ((ctxt->eflags & EFLG_ZF) == 0))
2687 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002688 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002689 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2690 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002691 }
Gleb Natapov063db062010-03-18 15:20:06 +02002692 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002693 }
2694
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002695 if (c->src.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002696 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002697 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002698 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002699 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002700 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002701 }
2702
Gleb Natapove35b7b92010-02-25 16:36:42 +02002703 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002704 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002705 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002706 if (rc != X86EMUL_CONTINUE)
2707 goto done;
2708 }
2709
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002710 if ((c->d & DstMask) == ImplicitOps)
2711 goto special_insn;
2712
2713
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002714 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2715 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002716 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002717 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002718 if (rc != X86EMUL_CONTINUE)
2719 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002720 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002721 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002722
Avi Kivity018a98d2007-11-27 19:30:56 +02002723special_insn:
2724
Avi Kivityef65c882010-07-29 15:11:51 +03002725 if (c->execute) {
2726 rc = c->execute(ctxt);
2727 if (rc != X86EMUL_CONTINUE)
2728 goto done;
2729 goto writeback;
2730 }
2731
Laurent Viviere4e03de2007-09-18 11:52:50 +02002732 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 goto twobyte_insn;
2734
Laurent Viviere4e03de2007-09-18 11:52:50 +02002735 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 case 0x00 ... 0x05:
2737 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002738 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002740 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002741 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002742 break;
2743 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002744 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002745 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002746 goto done;
2747 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 case 0x08 ... 0x0d:
2749 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002750 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002752 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002753 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002754 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 case 0x10 ... 0x15:
2756 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002757 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002759 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002760 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002761 break;
2762 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002763 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002764 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002765 goto done;
2766 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 case 0x18 ... 0x1d:
2768 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002769 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002771 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002772 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002773 break;
2774 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002775 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002776 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002777 goto done;
2778 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002779 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002781 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 break;
2783 case 0x28 ... 0x2d:
2784 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002785 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 break;
2787 case 0x30 ... 0x35:
2788 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002789 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 break;
2791 case 0x38 ... 0x3d:
2792 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002793 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002795 case 0x40 ... 0x47: /* inc r16/r32 */
2796 emulate_1op("inc", c->dst, ctxt->eflags);
2797 break;
2798 case 0x48 ... 0x4f: /* dec r16/r32 */
2799 emulate_1op("dec", c->dst, ctxt->eflags);
2800 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002801 case 0x58 ... 0x5f: /* pop reg */
2802 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002803 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002804 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002805 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002806 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002807 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002808 rc = emulate_pusha(ctxt, ops);
2809 if (rc != X86EMUL_CONTINUE)
2810 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002811 break;
2812 case 0x61: /* popa */
2813 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002814 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002815 goto done;
2816 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002818 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002820 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002822 case 0x6c: /* insb */
2823 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002824 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002825 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002826 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002827 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002828 goto done;
2829 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002830 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2831 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002832 goto done; /* IO is needed, skip writeback */
2833 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002834 case 0x6e: /* outsb */
2835 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002836 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002837 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002838 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002839 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002840 goto done;
2841 }
Gleb Natapov79729952010-03-18 15:20:24 +02002842 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2843 &c->src.val, 1, ctxt->vcpu);
2844
2845 c->dst.type = OP_NONE; /* nothing to writeback */
2846 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002847 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002848 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002849 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002850 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002852 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 case 0:
2854 goto add;
2855 case 1:
2856 goto or;
2857 case 2:
2858 goto adc;
2859 case 3:
2860 goto sbb;
2861 case 4:
2862 goto and;
2863 case 5:
2864 goto sub;
2865 case 6:
2866 goto xor;
2867 case 7:
2868 goto cmp;
2869 }
2870 break;
2871 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002872 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002873 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 break;
2875 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002876 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002878 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002880 *(u8 *) c->src.addr.reg = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 break;
2882 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002883 *(u16 *) c->src.addr.reg = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884 break;
2885 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002886 *c->src.addr.reg = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 break; /* 64b reg: zero-extend */
2888 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002889 *c->src.addr.reg = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890 break;
2891 }
2892 /*
2893 * Write back the memory destination with implicit LOCK
2894 * prefix.
2895 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002896 c->dst.val = c->src.val;
2897 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002900 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002901 case 0x8c: /* mov r/m, sreg */
2902 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002903 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002904 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002905 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002906 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002907 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002908 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002909 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002910 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002911 case 0x8e: { /* mov seg, r/m16 */
2912 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002913
2914 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002915
Gleb Natapovc6975182010-02-18 12:15:01 +02002916 if (c->modrm_reg == VCPU_SREG_CS ||
2917 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002918 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002919 goto done;
2920 }
2921
Glauber Costa310b5d32009-05-12 16:21:06 -04002922 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002923 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002924
Gleb Natapov2e873022010-03-18 15:20:18 +02002925 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002926
2927 c->dst.type = OP_NONE; /* Disable writeback. */
2928 break;
2929 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002931 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002932 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002935 case 0x90 ... 0x97: /* nop / xchg reg, rax */
2936 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
2937 goto done;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002938 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002939 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002940 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002941 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002942 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002943 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002944 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002945 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002946 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002947 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2948 if (rc != X86EMUL_CONTINUE)
2949 goto done;
2950 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002951 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002953 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002955 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002956 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02002957 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002958 case 0xa8 ... 0xa9: /* test ax, imm */
2959 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002961 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 break;
2963 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002964 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 case 0xae ... 0xaf: /* scas */
2966 DPRINTF("Urk! I don't handle SCAS.\n");
2967 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002968 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002969 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002970 case 0xc0 ... 0xc1:
2971 emulate_grp2(ctxt);
2972 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002973 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002974 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002975 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002976 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002977 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002978 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2979 mov:
2980 c->dst.val = c->src.val;
2981 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002982 case 0xcb: /* ret far */
2983 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002984 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002985 goto done;
2986 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002987 case 0xcf: /* iret */
2988 rc = emulate_iret(ctxt, ops);
2989
2990 if (rc != X86EMUL_CONTINUE)
2991 goto done;
2992 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002993 case 0xd0 ... 0xd1: /* Grp2 */
2994 c->src.val = 1;
2995 emulate_grp2(ctxt);
2996 break;
2997 case 0xd2 ... 0xd3: /* Grp2 */
2998 c->src.val = c->regs[VCPU_REGS_RCX];
2999 emulate_grp2(ctxt);
3000 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003001 case 0xe4: /* inb */
3002 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003003 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003004 case 0xe6: /* outb */
3005 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003006 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003007 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003008 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003009 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003010 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003011 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003012 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003013 }
3014 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003015 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003016 case 0xea: { /* jmp far */
3017 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003018 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003019 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3020
3021 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003022 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003023
Gleb Natapov414e6272010-04-28 19:15:26 +03003024 c->eip = 0;
3025 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003026 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003027 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003028 case 0xeb:
3029 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003030 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003031 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003032 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003033 case 0xec: /* in al,dx */
3034 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003035 c->src.val = c->regs[VCPU_REGS_RDX];
3036 do_io_in:
3037 c->dst.bytes = min(c->dst.bytes, 4u);
3038 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003039 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003040 goto done;
3041 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003042 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3043 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003044 goto done; /* IO is needed */
3045 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003046 case 0xee: /* out dx,al */
3047 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003048 c->src.val = c->regs[VCPU_REGS_RDX];
3049 do_io_out:
3050 c->dst.bytes = min(c->dst.bytes, 4u);
3051 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003052 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003053 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003054 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003055 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3056 ctxt->vcpu);
3057 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003058 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003059 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003060 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003061 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003062 case 0xf5: /* cmc */
3063 /* complement carry flag from eflags reg */
3064 ctxt->eflags ^= EFLG_CF;
3065 c->dst.type = OP_NONE; /* Disable writeback. */
3066 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003067 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003068 if (!emulate_grp3(ctxt, ops))
3069 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003070 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003071 case 0xf8: /* clc */
3072 ctxt->eflags &= ~EFLG_CF;
3073 c->dst.type = OP_NONE; /* Disable writeback. */
3074 break;
3075 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003076 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003077 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003078 goto done;
3079 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003080 ctxt->eflags &= ~X86_EFLAGS_IF;
3081 c->dst.type = OP_NONE; /* Disable writeback. */
3082 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003083 break;
3084 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003085 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003086 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003087 goto done;
3088 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003089 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003090 ctxt->eflags |= X86_EFLAGS_IF;
3091 c->dst.type = OP_NONE; /* Disable writeback. */
3092 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003093 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003094 case 0xfc: /* cld */
3095 ctxt->eflags &= ~EFLG_DF;
3096 c->dst.type = OP_NONE; /* Disable writeback. */
3097 break;
3098 case 0xfd: /* std */
3099 ctxt->eflags |= EFLG_DF;
3100 c->dst.type = OP_NONE; /* Disable writeback. */
3101 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003102 case 0xfe: /* Grp4 */
3103 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003104 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003105 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003106 goto done;
3107 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003108 case 0xff: /* Grp5 */
3109 if (c->modrm_reg == 5)
3110 goto jump_far;
3111 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003112 default:
3113 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003115
3116writeback:
3117 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003118 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003119 goto done;
3120
Gleb Natapov5cd21912010-03-18 15:20:26 +02003121 /*
3122 * restore dst type in case the decoding will be reused
3123 * (happens for string instruction )
3124 */
3125 c->dst.type = saved_dst_type;
3126
Gleb Natapova682e352010-03-18 15:20:21 +02003127 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003128 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3129 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003130
3131 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003132 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3133 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003134
Gleb Natapov5cd21912010-03-18 15:20:26 +02003135 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003136 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003137 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003138 /*
3139 * Re-enter guest when pio read ahead buffer is empty or,
3140 * if it is not used, after each 1024 iteration.
3141 */
3142 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3143 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003144 ctxt->restart = false;
3145 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003146 /*
3147 * reset read cache here in case string instruction is restared
3148 * without decoding
3149 */
3150 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003151 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003152
3153done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003154 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155
3156twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003157 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003159 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 u16 size;
3161 unsigned long address;
3162
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003163 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003164 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003165 goto cannot_emulate;
3166
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003167 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003168 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003169 goto done;
3170
Avi Kivity33e38852008-05-21 15:34:25 +03003171 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003172 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003173 /* Disable writeback. */
3174 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003175 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003177 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003178 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003179 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 goto done;
3181 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003182 /* Disable writeback. */
3183 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003185 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003186 if (c->modrm_mod == 3) {
3187 switch (c->modrm_rm) {
3188 case 1:
3189 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003190 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003191 goto done;
3192 break;
3193 default:
3194 goto cannot_emulate;
3195 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003196 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003197 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003198 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003199 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003200 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003201 goto done;
3202 realmode_lidt(ctxt->vcpu, size, address);
3203 }
Avi Kivity16286d02008-04-14 14:40:50 +03003204 /* Disable writeback. */
3205 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 break;
3207 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003208 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003209 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 break;
3211 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003212 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003213 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003214 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003216 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003217 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003218 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003220 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003221 /* Disable writeback. */
3222 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 break;
3224 default:
3225 goto cannot_emulate;
3226 }
3227 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003228 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003229 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003230 if (rc != X86EMUL_CONTINUE)
3231 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003232 else
3233 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003234 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003235 case 0x06:
3236 emulate_clts(ctxt->vcpu);
3237 c->dst.type = OP_NONE;
3238 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003239 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003240 kvm_emulate_wbinvd(ctxt->vcpu);
3241 c->dst.type = OP_NONE;
3242 break;
3243 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003244 case 0x0d: /* GrpP (prefetch) */
3245 case 0x18: /* Grp16 (prefetch/nop) */
3246 c->dst.type = OP_NONE;
3247 break;
3248 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003249 switch (c->modrm_reg) {
3250 case 1:
3251 case 5 ... 7:
3252 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003253 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003254 goto done;
3255 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003256 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003257 c->dst.type = OP_NONE; /* no writeback */
3258 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003260 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3261 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003262 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003263 goto done;
3264 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003265 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003266 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003268 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003269 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003270 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003271 goto done;
3272 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003273 c->dst.type = OP_NONE;
3274 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003276 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3277 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003278 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003279 goto done;
3280 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003281
Gleb Natapov338dbc92010-04-28 19:15:32 +03003282 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3283 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3284 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3285 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003286 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003287 goto done;
3288 }
3289
Laurent Viviera01af5e2007-09-24 11:10:56 +02003290 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003291 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003292 case 0x30:
3293 /* wrmsr */
3294 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3295 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003296 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003297 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003298 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003299 }
3300 rc = X86EMUL_CONTINUE;
3301 c->dst.type = OP_NONE;
3302 break;
3303 case 0x32:
3304 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003305 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003306 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003307 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003308 } else {
3309 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3310 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3311 }
3312 rc = X86EMUL_CONTINUE;
3313 c->dst.type = OP_NONE;
3314 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003315 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003316 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003317 if (rc != X86EMUL_CONTINUE)
3318 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003319 else
3320 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003321 break;
3322 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003323 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003324 if (rc != X86EMUL_CONTINUE)
3325 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003326 else
3327 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003328 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003330 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003331 if (!test_cc(c->b, ctxt->eflags))
3332 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003334 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003335 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003336 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003337 c->dst.type = OP_NONE;
3338 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003339 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003340 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003341 break;
3342 case 0xa1: /* pop fs */
3343 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003344 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003345 goto done;
3346 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003347 case 0xa3:
3348 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003349 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003350 /* only subword offset */
3351 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003352 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003353 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003354 case 0xa4: /* shld imm8, r, r/m */
3355 case 0xa5: /* shld cl, r, r/m */
3356 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3357 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003358 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003359 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003360 break;
3361 case 0xa9: /* pop gs */
3362 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003363 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003364 goto done;
3365 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003366 case 0xab:
3367 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003368 /* only subword offset */
3369 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003370 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003371 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003372 case 0xac: /* shrd imm8, r, r/m */
3373 case 0xad: /* shrd cl, r, r/m */
3374 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3375 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003376 case 0xae: /* clflush */
3377 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003378 case 0xb0 ... 0xb1: /* cmpxchg */
3379 /*
3380 * Save real source value, then compare EAX against
3381 * destination.
3382 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003383 c->src.orig_val = c->src.val;
3384 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003385 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3386 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003388 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 } else {
3390 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003391 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003392 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393 }
3394 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 case 0xb3:
3396 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003397 /* only subword offset */
3398 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003399 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003402 c->dst.bytes = c->op_bytes;
3403 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3404 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003406 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003407 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 case 0:
3409 goto bt;
3410 case 1:
3411 goto bts;
3412 case 2:
3413 goto btr;
3414 case 3:
3415 goto btc;
3416 }
3417 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003418 case 0xbb:
3419 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003420 /* only subword offset */
3421 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003422 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003423 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003425 c->dst.bytes = c->op_bytes;
3426 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3427 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003429 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003430 c->dst.bytes = c->op_bytes;
3431 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3432 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003433 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003435 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003436 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003437 goto done;
3438 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003439 default:
3440 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 }
3442 goto writeback;
3443
3444cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003445 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446 return -1;
3447}