blob: 904fc1c99b973dc17ae9b455d4c415a61231ad55 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b122010-07-29 15:11:49 +030049#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivityab85b122010-07-29 15:11:49 +030051#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
54#define DstAcc (4<<1) /* Destination Accumulator */
55#define DstDI (5<<1) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<1) /* 64bit memory operand */
57#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080058/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020059#define SrcNone (0<<4) /* No source operand. */
60#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
61#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf202009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Gleb Natapov341de7e2009-04-12 13:36:41 +030074#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030076#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030078#define Mov (1<<9)
79#define BitOp (1<<10)
80#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020081#define String (1<<12) /* String instruction (rep capable) */
82#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020083#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
84#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030085/* Misc flags */
Avi 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,
492 void *ptr,
493 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;
Laurent Viviercebff022007-07-30 13:35:24 +0300500 rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2,
Gleb Natapov1871c602010-02-10 14:21:32 +0200501 ctxt->vcpu, NULL);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900502 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503 return rc;
Laurent Viviercebff022007-07-30 13:35:24 +0300504 rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +0200505 ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 return rc;
507}
508
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300509static int test_cc(unsigned int condition, unsigned int flags)
510{
511 int rc = 0;
512
513 switch ((condition & 15) >> 1) {
514 case 0: /* o */
515 rc |= (flags & EFLG_OF);
516 break;
517 case 1: /* b/c/nae */
518 rc |= (flags & EFLG_CF);
519 break;
520 case 2: /* z/e */
521 rc |= (flags & EFLG_ZF);
522 break;
523 case 3: /* be/na */
524 rc |= (flags & (EFLG_CF|EFLG_ZF));
525 break;
526 case 4: /* s */
527 rc |= (flags & EFLG_SF);
528 break;
529 case 5: /* p/pe */
530 rc |= (flags & EFLG_PF);
531 break;
532 case 7: /* le/ng */
533 rc |= (flags & EFLG_ZF);
534 /* fall through */
535 case 6: /* l/nge */
536 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
537 break;
538 }
539
540 /* Odd condition identifiers (lsb == 1) have inverted sense. */
541 return (!!rc ^ (condition & 1));
542}
543
Avi Kivity3c118e22007-10-31 10:27:04 +0200544static void decode_register_operand(struct operand *op,
545 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200546 int inhibit_bytereg)
547{
Avi Kivity33615aa2007-10-31 11:15:56 +0200548 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200549 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200550
551 if (!(c->d & ModRM))
552 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200553 op->type = OP_REG;
554 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity33615aa2007-10-31 11:15:56 +0200555 op->ptr = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200556 op->val = *(u8 *)op->ptr;
557 op->bytes = 1;
558 } else {
Avi Kivity33615aa2007-10-31 11:15:56 +0200559 op->ptr = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200560 op->bytes = c->op_bytes;
561 switch (op->bytes) {
562 case 2:
563 op->val = *(u16 *)op->ptr;
564 break;
565 case 4:
566 op->val = *(u32 *)op->ptr;
567 break;
568 case 8:
569 op->val = *(u64 *) op->ptr;
570 break;
571 }
572 }
573 op->orig_val = op->val;
574}
575
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200576static int decode_modrm(struct x86_emulate_ctxt *ctxt,
577 struct x86_emulate_ops *ops)
578{
579 struct decode_cache *c = &ctxt->decode;
580 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700581 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900582 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200583
584 if (c->rex_prefix) {
585 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
586 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
587 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
588 }
589
590 c->modrm = insn_fetch(u8, 1, c->eip);
591 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
592 c->modrm_reg |= (c->modrm & 0x38) >> 3;
593 c->modrm_rm |= (c->modrm & 0x07);
594 c->modrm_ea = 0;
595 c->use_modrm_ea = 1;
596
597 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300598 c->modrm_ptr = decode_register(c->modrm_rm,
599 c->regs, c->d & ByteOp);
600 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200601 return rc;
602 }
603
604 if (c->ad_bytes == 2) {
605 unsigned bx = c->regs[VCPU_REGS_RBX];
606 unsigned bp = c->regs[VCPU_REGS_RBP];
607 unsigned si = c->regs[VCPU_REGS_RSI];
608 unsigned di = c->regs[VCPU_REGS_RDI];
609
610 /* 16-bit ModR/M decode. */
611 switch (c->modrm_mod) {
612 case 0:
613 if (c->modrm_rm == 6)
614 c->modrm_ea += insn_fetch(u16, 2, c->eip);
615 break;
616 case 1:
617 c->modrm_ea += insn_fetch(s8, 1, c->eip);
618 break;
619 case 2:
620 c->modrm_ea += insn_fetch(u16, 2, c->eip);
621 break;
622 }
623 switch (c->modrm_rm) {
624 case 0:
625 c->modrm_ea += bx + si;
626 break;
627 case 1:
628 c->modrm_ea += bx + di;
629 break;
630 case 2:
631 c->modrm_ea += bp + si;
632 break;
633 case 3:
634 c->modrm_ea += bp + di;
635 break;
636 case 4:
637 c->modrm_ea += si;
638 break;
639 case 5:
640 c->modrm_ea += di;
641 break;
642 case 6:
643 if (c->modrm_mod != 0)
644 c->modrm_ea += bp;
645 break;
646 case 7:
647 c->modrm_ea += bx;
648 break;
649 }
650 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
651 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300652 if (!c->has_seg_override)
653 set_seg_override(c, VCPU_SREG_SS);
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200654 c->modrm_ea = (u16)c->modrm_ea;
655 } else {
656 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700657 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200658 sib = insn_fetch(u8, 1, c->eip);
659 index_reg |= (sib >> 3) & 7;
660 base_reg |= sib & 7;
661 scale = sib >> 6;
662
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700663 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
664 c->modrm_ea += insn_fetch(s32, 4, c->eip);
665 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200666 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700667 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200668 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700669 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
670 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700671 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700672 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200673 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200674 switch (c->modrm_mod) {
675 case 0:
676 if (c->modrm_rm == 5)
677 c->modrm_ea += insn_fetch(s32, 4, c->eip);
678 break;
679 case 1:
680 c->modrm_ea += insn_fetch(s8, 1, c->eip);
681 break;
682 case 2:
683 c->modrm_ea += insn_fetch(s32, 4, c->eip);
684 break;
685 }
686 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200687done:
688 return rc;
689}
690
691static int decode_abs(struct x86_emulate_ctxt *ctxt,
692 struct x86_emulate_ops *ops)
693{
694 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900695 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200696
697 switch (c->ad_bytes) {
698 case 2:
699 c->modrm_ea = insn_fetch(u16, 2, c->eip);
700 break;
701 case 4:
702 c->modrm_ea = insn_fetch(u32, 4, c->eip);
703 break;
704 case 8:
705 c->modrm_ea = insn_fetch(u64, 8, c->eip);
706 break;
707 }
708done:
709 return rc;
710}
711
Gleb Natapov9de41572010-04-28 19:15:22 +0300712static int read_emulated(struct x86_emulate_ctxt *ctxt,
713 struct x86_emulate_ops *ops,
714 unsigned long addr, void *dest, unsigned size)
715{
716 int rc;
717 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300718 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300719
720 while (size) {
721 int n = min(size, 8u);
722 size -= n;
723 if (mc->pos < mc->end)
724 goto read_cached;
725
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300726 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
727 ctxt->vcpu);
728 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300729 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300730 if (rc != X86EMUL_CONTINUE)
731 return rc;
732 mc->end += n;
733
734 read_cached:
735 memcpy(dest, mc->data + mc->pos, n);
736 mc->pos += n;
737 dest += n;
738 addr += n;
739 }
740 return X86EMUL_CONTINUE;
741}
742
Gleb Natapov7b262e92010-03-18 15:20:27 +0200743static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
744 struct x86_emulate_ops *ops,
745 unsigned int size, unsigned short port,
746 void *dest)
747{
748 struct read_cache *rc = &ctxt->decode.io_read;
749
750 if (rc->pos == rc->end) { /* refill pio read ahead */
751 struct decode_cache *c = &ctxt->decode;
752 unsigned int in_page, n;
753 unsigned int count = c->rep_prefix ?
754 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
755 in_page = (ctxt->eflags & EFLG_DF) ?
756 offset_in_page(c->regs[VCPU_REGS_RDI]) :
757 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
758 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
759 count);
760 if (n == 0)
761 n = 1;
762 rc->pos = rc->end = 0;
763 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
764 return 0;
765 rc->end = n * size;
766 }
767
768 memcpy(dest, rc->data + rc->pos, size);
769 rc->pos += size;
770 return 1;
771}
772
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200773static u32 desc_limit_scaled(struct desc_struct *desc)
774{
775 u32 limit = get_desc_limit(desc);
776
777 return desc->g ? (limit << 12) | 0xfff : limit;
778}
779
780static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
781 struct x86_emulate_ops *ops,
782 u16 selector, struct desc_ptr *dt)
783{
784 if (selector & 1 << 2) {
785 struct desc_struct desc;
786 memset (dt, 0, sizeof *dt);
787 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
788 return;
789
790 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
791 dt->address = get_desc_base(&desc);
792 } else
793 ops->get_gdt(dt, ctxt->vcpu);
794}
795
796/* allowed just for 8 bytes segments */
797static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
798 struct x86_emulate_ops *ops,
799 u16 selector, struct desc_struct *desc)
800{
801 struct desc_ptr dt;
802 u16 index = selector >> 3;
803 int ret;
804 u32 err;
805 ulong addr;
806
807 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
808
809 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300810 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200811 return X86EMUL_PROPAGATE_FAULT;
812 }
813 addr = dt.address + index * 8;
814 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
815 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300816 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200817
818 return ret;
819}
820
821/* allowed just for 8 bytes segments */
822static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
823 struct x86_emulate_ops *ops,
824 u16 selector, struct desc_struct *desc)
825{
826 struct desc_ptr dt;
827 u16 index = selector >> 3;
828 u32 err;
829 ulong addr;
830 int ret;
831
832 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
833
834 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300835 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200836 return X86EMUL_PROPAGATE_FAULT;
837 }
838
839 addr = dt.address + index * 8;
840 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
841 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300842 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200843
844 return ret;
845}
846
847static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
848 struct x86_emulate_ops *ops,
849 u16 selector, int seg)
850{
851 struct desc_struct seg_desc;
852 u8 dpl, rpl, cpl;
853 unsigned err_vec = GP_VECTOR;
854 u32 err_code = 0;
855 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
856 int ret;
857
858 memset(&seg_desc, 0, sizeof seg_desc);
859
860 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
861 || ctxt->mode == X86EMUL_MODE_REAL) {
862 /* set real mode segment descriptor */
863 set_desc_base(&seg_desc, selector << 4);
864 set_desc_limit(&seg_desc, 0xffff);
865 seg_desc.type = 3;
866 seg_desc.p = 1;
867 seg_desc.s = 1;
868 goto load;
869 }
870
871 /* NULL selector is not valid for TR, CS and SS */
872 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
873 && null_selector)
874 goto exception;
875
876 /* TR should be in GDT only */
877 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
878 goto exception;
879
880 if (null_selector) /* for NULL selector skip all following checks */
881 goto load;
882
883 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
884 if (ret != X86EMUL_CONTINUE)
885 return ret;
886
887 err_code = selector & 0xfffc;
888 err_vec = GP_VECTOR;
889
890 /* can't load system descriptor into segment selecor */
891 if (seg <= VCPU_SREG_GS && !seg_desc.s)
892 goto exception;
893
894 if (!seg_desc.p) {
895 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
896 goto exception;
897 }
898
899 rpl = selector & 3;
900 dpl = seg_desc.dpl;
901 cpl = ops->cpl(ctxt->vcpu);
902
903 switch (seg) {
904 case VCPU_SREG_SS:
905 /*
906 * segment is not a writable data segment or segment
907 * selector's RPL != CPL or segment selector's RPL != CPL
908 */
909 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
910 goto exception;
911 break;
912 case VCPU_SREG_CS:
913 if (!(seg_desc.type & 8))
914 goto exception;
915
916 if (seg_desc.type & 4) {
917 /* conforming */
918 if (dpl > cpl)
919 goto exception;
920 } else {
921 /* nonconforming */
922 if (rpl > cpl || dpl != cpl)
923 goto exception;
924 }
925 /* CS(RPL) <- CPL */
926 selector = (selector & 0xfffc) | cpl;
927 break;
928 case VCPU_SREG_TR:
929 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
930 goto exception;
931 break;
932 case VCPU_SREG_LDTR:
933 if (seg_desc.s || seg_desc.type != 2)
934 goto exception;
935 break;
936 default: /* DS, ES, FS, or GS */
937 /*
938 * segment is not a data or readable code segment or
939 * ((segment is a data or nonconforming code segment)
940 * and (both RPL and CPL > DPL))
941 */
942 if ((seg_desc.type & 0xa) == 0x8 ||
943 (((seg_desc.type & 0xc) != 0xc) &&
944 (rpl > dpl && cpl > dpl)))
945 goto exception;
946 break;
947 }
948
949 if (seg_desc.s) {
950 /* mark segment as accessed */
951 seg_desc.type |= 1;
952 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
953 if (ret != X86EMUL_CONTINUE)
954 return ret;
955 }
956load:
957 ops->set_segment_selector(selector, seg, ctxt->vcpu);
958 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
959 return X86EMUL_CONTINUE;
960exception:
Gleb Natapov54b84862010-04-28 19:15:44 +0300961 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200962 return X86EMUL_PROPAGATE_FAULT;
963}
964
Wei Yongjunc37eda12010-06-15 09:03:33 +0800965static inline int writeback(struct x86_emulate_ctxt *ctxt,
966 struct x86_emulate_ops *ops)
967{
968 int rc;
969 struct decode_cache *c = &ctxt->decode;
970 u32 err;
971
972 switch (c->dst.type) {
973 case OP_REG:
974 /* The 4-byte case *is* correct:
975 * in 64-bit mode we zero-extend.
976 */
977 switch (c->dst.bytes) {
978 case 1:
979 *(u8 *)c->dst.ptr = (u8)c->dst.val;
980 break;
981 case 2:
982 *(u16 *)c->dst.ptr = (u16)c->dst.val;
983 break;
984 case 4:
985 *c->dst.ptr = (u32)c->dst.val;
986 break; /* 64b: zero-ext */
987 case 8:
988 *c->dst.ptr = c->dst.val;
989 break;
990 }
991 break;
992 case OP_MEM:
993 if (c->lock_prefix)
994 rc = ops->cmpxchg_emulated(
995 (unsigned long)c->dst.ptr,
996 &c->dst.orig_val,
997 &c->dst.val,
998 c->dst.bytes,
999 &err,
1000 ctxt->vcpu);
1001 else
1002 rc = ops->write_emulated(
1003 (unsigned long)c->dst.ptr,
1004 &c->dst.val,
1005 c->dst.bytes,
1006 &err,
1007 ctxt->vcpu);
1008 if (rc == X86EMUL_PROPAGATE_FAULT)
1009 emulate_pf(ctxt,
1010 (unsigned long)c->dst.ptr, err);
1011 if (rc != X86EMUL_CONTINUE)
1012 return rc;
1013 break;
1014 case OP_NONE:
1015 /* no writeback */
1016 break;
1017 default:
1018 break;
1019 }
1020 return X86EMUL_CONTINUE;
1021}
1022
Gleb Natapov79168fd2010-04-28 19:15:30 +03001023static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1024 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001025{
1026 struct decode_cache *c = &ctxt->decode;
1027
1028 c->dst.type = OP_MEM;
1029 c->dst.bytes = c->op_bytes;
1030 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001031 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001032 c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops),
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001033 c->regs[VCPU_REGS_RSP]);
1034}
1035
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001036static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001037 struct x86_emulate_ops *ops,
1038 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001039{
1040 struct decode_cache *c = &ctxt->decode;
1041 int rc;
1042
Gleb Natapov79168fd2010-04-28 19:15:30 +03001043 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001044 c->regs[VCPU_REGS_RSP]),
1045 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001046 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001047 return rc;
1048
Avi Kivity350f69d2009-01-05 11:12:40 +02001049 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001050 return rc;
1051}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001052
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001053static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1054 struct x86_emulate_ops *ops,
1055 void *dest, int len)
1056{
1057 int rc;
1058 unsigned long val, change_mask;
1059 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001060 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001061
1062 rc = emulate_pop(ctxt, ops, &val, len);
1063 if (rc != X86EMUL_CONTINUE)
1064 return rc;
1065
1066 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1067 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1068
1069 switch(ctxt->mode) {
1070 case X86EMUL_MODE_PROT64:
1071 case X86EMUL_MODE_PROT32:
1072 case X86EMUL_MODE_PROT16:
1073 if (cpl == 0)
1074 change_mask |= EFLG_IOPL;
1075 if (cpl <= iopl)
1076 change_mask |= EFLG_IF;
1077 break;
1078 case X86EMUL_MODE_VM86:
1079 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001080 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001081 return X86EMUL_PROPAGATE_FAULT;
1082 }
1083 change_mask |= EFLG_IF;
1084 break;
1085 default: /* real mode */
1086 change_mask |= (EFLG_IOPL | EFLG_IF);
1087 break;
1088 }
1089
1090 *(unsigned long *)dest =
1091 (ctxt->eflags & ~change_mask) | (val & change_mask);
1092
1093 return rc;
1094}
1095
Gleb Natapov79168fd2010-04-28 19:15:30 +03001096static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1097 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001098{
1099 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001100
Gleb Natapov79168fd2010-04-28 19:15:30 +03001101 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001102
Gleb Natapov79168fd2010-04-28 19:15:30 +03001103 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001104}
1105
1106static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1107 struct x86_emulate_ops *ops, int seg)
1108{
1109 struct decode_cache *c = &ctxt->decode;
1110 unsigned long selector;
1111 int rc;
1112
1113 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001114 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001115 return rc;
1116
Gleb Natapov2e873022010-03-18 15:20:18 +02001117 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001118 return rc;
1119}
1120
Wei Yongjunc37eda12010-06-15 09:03:33 +08001121static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001122 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001123{
1124 struct decode_cache *c = &ctxt->decode;
1125 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001126 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001127 int reg = VCPU_REGS_RAX;
1128
1129 while (reg <= VCPU_REGS_RDI) {
1130 (reg == VCPU_REGS_RSP) ?
1131 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1132
Gleb Natapov79168fd2010-04-28 19:15:30 +03001133 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001134
1135 rc = writeback(ctxt, ops);
1136 if (rc != X86EMUL_CONTINUE)
1137 return rc;
1138
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001139 ++reg;
1140 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001141
1142 /* Disable writeback. */
1143 c->dst.type = OP_NONE;
1144
1145 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001146}
1147
1148static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1149 struct x86_emulate_ops *ops)
1150{
1151 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001152 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001153 int reg = VCPU_REGS_RDI;
1154
1155 while (reg >= VCPU_REGS_RAX) {
1156 if (reg == VCPU_REGS_RSP) {
1157 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1158 c->op_bytes);
1159 --reg;
1160 }
1161
1162 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001163 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001164 break;
1165 --reg;
1166 }
1167 return rc;
1168}
1169
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001170static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1171 struct x86_emulate_ops *ops)
1172{
1173 struct decode_cache *c = &ctxt->decode;
1174 int rc = X86EMUL_CONTINUE;
1175 unsigned long temp_eip = 0;
1176 unsigned long temp_eflags = 0;
1177 unsigned long cs = 0;
1178 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1179 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1180 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1181 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1182
1183 /* TODO: Add stack limit check */
1184
1185 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1186
1187 if (rc != X86EMUL_CONTINUE)
1188 return rc;
1189
1190 if (temp_eip & ~0xffff) {
1191 emulate_gp(ctxt, 0);
1192 return X86EMUL_PROPAGATE_FAULT;
1193 }
1194
1195 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1196
1197 if (rc != X86EMUL_CONTINUE)
1198 return rc;
1199
1200 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1201
1202 if (rc != X86EMUL_CONTINUE)
1203 return rc;
1204
1205 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1206
1207 if (rc != X86EMUL_CONTINUE)
1208 return rc;
1209
1210 c->eip = temp_eip;
1211
1212
1213 if (c->op_bytes == 4)
1214 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1215 else if (c->op_bytes == 2) {
1216 ctxt->eflags &= ~0xffff;
1217 ctxt->eflags |= temp_eflags;
1218 }
1219
1220 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1221 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1222
1223 return rc;
1224}
1225
1226static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1227 struct x86_emulate_ops* ops)
1228{
1229 switch(ctxt->mode) {
1230 case X86EMUL_MODE_REAL:
1231 return emulate_iret_real(ctxt, ops);
1232 case X86EMUL_MODE_VM86:
1233 case X86EMUL_MODE_PROT16:
1234 case X86EMUL_MODE_PROT32:
1235 case X86EMUL_MODE_PROT64:
1236 default:
1237 /* iret from protected mode unimplemented yet */
1238 return X86EMUL_UNHANDLEABLE;
1239 }
1240}
1241
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001242static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1243 struct x86_emulate_ops *ops)
1244{
1245 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001246
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001247 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001248}
1249
Laurent Vivier05f086f2007-09-24 11:10:55 +02001250static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001251{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001252 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001253 switch (c->modrm_reg) {
1254 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001255 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001256 break;
1257 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001258 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001259 break;
1260 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001261 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001262 break;
1263 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001264 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001265 break;
1266 case 4: /* sal/shl */
1267 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001268 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001269 break;
1270 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001271 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001272 break;
1273 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001274 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001275 break;
1276 }
1277}
1278
1279static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001280 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001281{
1282 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001283
1284 switch (c->modrm_reg) {
1285 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001286 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001287 break;
1288 case 2: /* not */
1289 c->dst.val = ~c->dst.val;
1290 break;
1291 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001292 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001293 break;
1294 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001295 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001296 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001297 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001298}
1299
1300static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001301 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001302{
1303 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001304
1305 switch (c->modrm_reg) {
1306 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001307 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001308 break;
1309 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001310 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001311 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001312 case 2: /* call near abs */ {
1313 long int old_eip;
1314 old_eip = c->eip;
1315 c->eip = c->src.val;
1316 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001317 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001318 break;
1319 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001320 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001321 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001322 break;
1323 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001324 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001325 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001326 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001327 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001328}
1329
1330static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001331 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001332{
1333 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001334 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001335
1336 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1337 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001338 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1339 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001340 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001341 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001342 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1343 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001344
Laurent Vivier05f086f2007-09-24 11:10:55 +02001345 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001346 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001347 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001348}
1349
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001350static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1351 struct x86_emulate_ops *ops)
1352{
1353 struct decode_cache *c = &ctxt->decode;
1354 int rc;
1355 unsigned long cs;
1356
1357 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001358 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001359 return rc;
1360 if (c->op_bytes == 4)
1361 c->eip = (u32)c->eip;
1362 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001363 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001364 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001365 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001366 return rc;
1367}
1368
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001369static inline void
1370setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001371 struct x86_emulate_ops *ops, struct desc_struct *cs,
1372 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001373{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001374 memset(cs, 0, sizeof(struct desc_struct));
1375 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1376 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001377
1378 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001379 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001380 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001381 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001382 cs->type = 0x0b; /* Read, Execute, Accessed */
1383 cs->s = 1;
1384 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001385 cs->p = 1;
1386 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001387
Gleb Natapov79168fd2010-04-28 19:15:30 +03001388 set_desc_base(ss, 0); /* flat segment */
1389 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001390 ss->g = 1; /* 4kb granularity */
1391 ss->s = 1;
1392 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001393 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001394 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001395 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001396}
1397
1398static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001399emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001400{
1401 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001402 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001403 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001404 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001405
1406 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001407 if (ctxt->mode == X86EMUL_MODE_REAL ||
1408 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001409 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001410 return X86EMUL_PROPAGATE_FAULT;
1411 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001412
Gleb Natapov79168fd2010-04-28 19:15:30 +03001413 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001414
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001415 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001416 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001417 cs_sel = (u16)(msr_data & 0xfffc);
1418 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001419
1420 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001421 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001422 cs.l = 1;
1423 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001424 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1425 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1426 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1427 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001428
1429 c->regs[VCPU_REGS_RCX] = c->eip;
1430 if (is_long_mode(ctxt->vcpu)) {
1431#ifdef CONFIG_X86_64
1432 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1433
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001434 ops->get_msr(ctxt->vcpu,
1435 ctxt->mode == X86EMUL_MODE_PROT64 ?
1436 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001437 c->eip = msr_data;
1438
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001439 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001440 ctxt->eflags &= ~(msr_data | EFLG_RF);
1441#endif
1442 } else {
1443 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001444 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001445 c->eip = (u32)msr_data;
1446
1447 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1448 }
1449
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001450 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001451}
1452
Andre Przywara8c604352009-06-18 12:56:01 +02001453static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001454emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001455{
1456 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001457 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001458 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001459 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001460
Gleb Natapova0044752010-02-10 14:21:31 +02001461 /* inject #GP if in real mode */
1462 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001463 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001464 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001465 }
1466
1467 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1468 * Therefore, we inject an #UD.
1469 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001470 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001471 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001472 return X86EMUL_PROPAGATE_FAULT;
1473 }
Andre Przywara8c604352009-06-18 12:56:01 +02001474
Gleb Natapov79168fd2010-04-28 19:15:30 +03001475 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001476
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001477 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001478 switch (ctxt->mode) {
1479 case X86EMUL_MODE_PROT32:
1480 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001481 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001482 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001483 }
1484 break;
1485 case X86EMUL_MODE_PROT64:
1486 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001487 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001488 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001489 }
1490 break;
1491 }
1492
1493 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001494 cs_sel = (u16)msr_data;
1495 cs_sel &= ~SELECTOR_RPL_MASK;
1496 ss_sel = cs_sel + 8;
1497 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001498 if (ctxt->mode == X86EMUL_MODE_PROT64
1499 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001500 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001501 cs.l = 1;
1502 }
1503
Gleb Natapov79168fd2010-04-28 19:15:30 +03001504 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1505 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1506 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1507 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001508
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001509 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001510 c->eip = msr_data;
1511
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001512 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001513 c->regs[VCPU_REGS_RSP] = msr_data;
1514
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001515 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001516}
1517
Andre Przywara4668f052009-06-18 12:56:02 +02001518static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001519emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001520{
1521 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001522 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001523 u64 msr_data;
1524 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001525 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001526
Gleb Natapova0044752010-02-10 14:21:31 +02001527 /* inject #GP if in real mode or Virtual 8086 mode */
1528 if (ctxt->mode == X86EMUL_MODE_REAL ||
1529 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001530 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001531 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001532 }
1533
Gleb Natapov79168fd2010-04-28 19:15:30 +03001534 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001535
1536 if ((c->rex_prefix & 0x8) != 0x0)
1537 usermode = X86EMUL_MODE_PROT64;
1538 else
1539 usermode = X86EMUL_MODE_PROT32;
1540
1541 cs.dpl = 3;
1542 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001543 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001544 switch (usermode) {
1545 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001546 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001547 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001548 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001549 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001550 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001551 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001552 break;
1553 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001554 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001555 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001556 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001557 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001558 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001559 ss_sel = cs_sel + 8;
1560 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001561 cs.l = 1;
1562 break;
1563 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001564 cs_sel |= SELECTOR_RPL_MASK;
1565 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001566
Gleb Natapov79168fd2010-04-28 19:15:30 +03001567 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1568 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1569 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1570 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001571
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001572 c->eip = c->regs[VCPU_REGS_RDX];
1573 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001574
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001575 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001576}
1577
Gleb Natapov9c537242010-03-18 15:20:05 +02001578static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1579 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001580{
1581 int iopl;
1582 if (ctxt->mode == X86EMUL_MODE_REAL)
1583 return false;
1584 if (ctxt->mode == X86EMUL_MODE_VM86)
1585 return true;
1586 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001587 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001588}
1589
1590static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1591 struct x86_emulate_ops *ops,
1592 u16 port, u16 len)
1593{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001594 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001595 int r;
1596 u16 io_bitmap_ptr;
1597 u8 perm, bit_idx = port & 0x7;
1598 unsigned mask = (1 << len) - 1;
1599
Gleb Natapov79168fd2010-04-28 19:15:30 +03001600 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1601 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001602 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001603 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001604 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001605 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1606 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001607 if (r != X86EMUL_CONTINUE)
1608 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001609 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001610 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001611 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1612 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001613 if (r != X86EMUL_CONTINUE)
1614 return false;
1615 if ((perm >> bit_idx) & mask)
1616 return false;
1617 return true;
1618}
1619
1620static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1621 struct x86_emulate_ops *ops,
1622 u16 port, u16 len)
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;
1627 return true;
1628}
1629
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001630static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1631 struct x86_emulate_ops *ops,
1632 struct tss_segment_16 *tss)
1633{
1634 struct decode_cache *c = &ctxt->decode;
1635
1636 tss->ip = c->eip;
1637 tss->flag = ctxt->eflags;
1638 tss->ax = c->regs[VCPU_REGS_RAX];
1639 tss->cx = c->regs[VCPU_REGS_RCX];
1640 tss->dx = c->regs[VCPU_REGS_RDX];
1641 tss->bx = c->regs[VCPU_REGS_RBX];
1642 tss->sp = c->regs[VCPU_REGS_RSP];
1643 tss->bp = c->regs[VCPU_REGS_RBP];
1644 tss->si = c->regs[VCPU_REGS_RSI];
1645 tss->di = c->regs[VCPU_REGS_RDI];
1646
1647 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1648 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1649 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1650 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1651 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1652}
1653
1654static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1655 struct x86_emulate_ops *ops,
1656 struct tss_segment_16 *tss)
1657{
1658 struct decode_cache *c = &ctxt->decode;
1659 int ret;
1660
1661 c->eip = tss->ip;
1662 ctxt->eflags = tss->flag | 2;
1663 c->regs[VCPU_REGS_RAX] = tss->ax;
1664 c->regs[VCPU_REGS_RCX] = tss->cx;
1665 c->regs[VCPU_REGS_RDX] = tss->dx;
1666 c->regs[VCPU_REGS_RBX] = tss->bx;
1667 c->regs[VCPU_REGS_RSP] = tss->sp;
1668 c->regs[VCPU_REGS_RBP] = tss->bp;
1669 c->regs[VCPU_REGS_RSI] = tss->si;
1670 c->regs[VCPU_REGS_RDI] = tss->di;
1671
1672 /*
1673 * SDM says that segment selectors are loaded before segment
1674 * descriptors
1675 */
1676 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1677 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1678 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1679 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1680 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1681
1682 /*
1683 * Now load segment descriptors. If fault happenes at this stage
1684 * it is handled in a context of new task
1685 */
1686 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1687 if (ret != X86EMUL_CONTINUE)
1688 return ret;
1689 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1690 if (ret != X86EMUL_CONTINUE)
1691 return ret;
1692 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1693 if (ret != X86EMUL_CONTINUE)
1694 return ret;
1695 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1696 if (ret != X86EMUL_CONTINUE)
1697 return ret;
1698 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1699 if (ret != X86EMUL_CONTINUE)
1700 return ret;
1701
1702 return X86EMUL_CONTINUE;
1703}
1704
1705static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1706 struct x86_emulate_ops *ops,
1707 u16 tss_selector, u16 old_tss_sel,
1708 ulong old_tss_base, struct desc_struct *new_desc)
1709{
1710 struct tss_segment_16 tss_seg;
1711 int ret;
1712 u32 err, new_tss_base = get_desc_base(new_desc);
1713
1714 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1715 &err);
1716 if (ret == X86EMUL_PROPAGATE_FAULT) {
1717 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001718 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001719 return ret;
1720 }
1721
1722 save_state_to_tss16(ctxt, ops, &tss_seg);
1723
1724 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1725 &err);
1726 if (ret == X86EMUL_PROPAGATE_FAULT) {
1727 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001728 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001729 return ret;
1730 }
1731
1732 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1733 &err);
1734 if (ret == X86EMUL_PROPAGATE_FAULT) {
1735 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001736 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001737 return ret;
1738 }
1739
1740 if (old_tss_sel != 0xffff) {
1741 tss_seg.prev_task_link = old_tss_sel;
1742
1743 ret = ops->write_std(new_tss_base,
1744 &tss_seg.prev_task_link,
1745 sizeof tss_seg.prev_task_link,
1746 ctxt->vcpu, &err);
1747 if (ret == X86EMUL_PROPAGATE_FAULT) {
1748 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001749 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001750 return ret;
1751 }
1752 }
1753
1754 return load_state_from_tss16(ctxt, ops, &tss_seg);
1755}
1756
1757static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1758 struct x86_emulate_ops *ops,
1759 struct tss_segment_32 *tss)
1760{
1761 struct decode_cache *c = &ctxt->decode;
1762
1763 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1764 tss->eip = c->eip;
1765 tss->eflags = ctxt->eflags;
1766 tss->eax = c->regs[VCPU_REGS_RAX];
1767 tss->ecx = c->regs[VCPU_REGS_RCX];
1768 tss->edx = c->regs[VCPU_REGS_RDX];
1769 tss->ebx = c->regs[VCPU_REGS_RBX];
1770 tss->esp = c->regs[VCPU_REGS_RSP];
1771 tss->ebp = c->regs[VCPU_REGS_RBP];
1772 tss->esi = c->regs[VCPU_REGS_RSI];
1773 tss->edi = c->regs[VCPU_REGS_RDI];
1774
1775 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1776 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1777 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1778 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1779 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1780 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1781 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1782}
1783
1784static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1785 struct x86_emulate_ops *ops,
1786 struct tss_segment_32 *tss)
1787{
1788 struct decode_cache *c = &ctxt->decode;
1789 int ret;
1790
Gleb Natapov0f122442010-04-28 19:15:31 +03001791 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001792 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001793 return X86EMUL_PROPAGATE_FAULT;
1794 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001795 c->eip = tss->eip;
1796 ctxt->eflags = tss->eflags | 2;
1797 c->regs[VCPU_REGS_RAX] = tss->eax;
1798 c->regs[VCPU_REGS_RCX] = tss->ecx;
1799 c->regs[VCPU_REGS_RDX] = tss->edx;
1800 c->regs[VCPU_REGS_RBX] = tss->ebx;
1801 c->regs[VCPU_REGS_RSP] = tss->esp;
1802 c->regs[VCPU_REGS_RBP] = tss->ebp;
1803 c->regs[VCPU_REGS_RSI] = tss->esi;
1804 c->regs[VCPU_REGS_RDI] = tss->edi;
1805
1806 /*
1807 * SDM says that segment selectors are loaded before segment
1808 * descriptors
1809 */
1810 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1811 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1812 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1813 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1814 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1815 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1816 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1817
1818 /*
1819 * Now load segment descriptors. If fault happenes at this stage
1820 * it is handled in a context of new task
1821 */
1822 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1823 if (ret != X86EMUL_CONTINUE)
1824 return ret;
1825 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1826 if (ret != X86EMUL_CONTINUE)
1827 return ret;
1828 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1829 if (ret != X86EMUL_CONTINUE)
1830 return ret;
1831 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1832 if (ret != X86EMUL_CONTINUE)
1833 return ret;
1834 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1835 if (ret != X86EMUL_CONTINUE)
1836 return ret;
1837 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1838 if (ret != X86EMUL_CONTINUE)
1839 return ret;
1840 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1841 if (ret != X86EMUL_CONTINUE)
1842 return ret;
1843
1844 return X86EMUL_CONTINUE;
1845}
1846
1847static int task_switch_32(struct x86_emulate_ctxt *ctxt,
1848 struct x86_emulate_ops *ops,
1849 u16 tss_selector, u16 old_tss_sel,
1850 ulong old_tss_base, struct desc_struct *new_desc)
1851{
1852 struct tss_segment_32 tss_seg;
1853 int ret;
1854 u32 err, new_tss_base = get_desc_base(new_desc);
1855
1856 ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1857 &err);
1858 if (ret == X86EMUL_PROPAGATE_FAULT) {
1859 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001860 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001861 return ret;
1862 }
1863
1864 save_state_to_tss32(ctxt, ops, &tss_seg);
1865
1866 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1867 &err);
1868 if (ret == X86EMUL_PROPAGATE_FAULT) {
1869 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001870 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001871 return ret;
1872 }
1873
1874 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1875 &err);
1876 if (ret == X86EMUL_PROPAGATE_FAULT) {
1877 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001878 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001879 return ret;
1880 }
1881
1882 if (old_tss_sel != 0xffff) {
1883 tss_seg.prev_task_link = old_tss_sel;
1884
1885 ret = ops->write_std(new_tss_base,
1886 &tss_seg.prev_task_link,
1887 sizeof tss_seg.prev_task_link,
1888 ctxt->vcpu, &err);
1889 if (ret == X86EMUL_PROPAGATE_FAULT) {
1890 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001891 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001892 return ret;
1893 }
1894 }
1895
1896 return load_state_from_tss32(ctxt, ops, &tss_seg);
1897}
1898
1899static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001900 struct x86_emulate_ops *ops,
1901 u16 tss_selector, int reason,
1902 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001903{
1904 struct desc_struct curr_tss_desc, next_tss_desc;
1905 int ret;
1906 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
1907 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03001908 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02001909 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001910
1911 /* FIXME: old_tss_base == ~0 ? */
1912
1913 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
1914 if (ret != X86EMUL_CONTINUE)
1915 return ret;
1916 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
1917 if (ret != X86EMUL_CONTINUE)
1918 return ret;
1919
1920 /* FIXME: check that next_tss_desc is tss */
1921
1922 if (reason != TASK_SWITCH_IRET) {
1923 if ((tss_selector & 3) > next_tss_desc.dpl ||
1924 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001925 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001926 return X86EMUL_PROPAGATE_FAULT;
1927 }
1928 }
1929
Gleb Natapovceffb452010-03-18 15:20:19 +02001930 desc_limit = desc_limit_scaled(&next_tss_desc);
1931 if (!next_tss_desc.p ||
1932 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
1933 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001934 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001935 return X86EMUL_PROPAGATE_FAULT;
1936 }
1937
1938 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
1939 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
1940 write_segment_descriptor(ctxt, ops, old_tss_sel,
1941 &curr_tss_desc);
1942 }
1943
1944 if (reason == TASK_SWITCH_IRET)
1945 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
1946
1947 /* set back link to prev task only if NT bit is set in eflags
1948 note that old_tss_sel is not used afetr this point */
1949 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
1950 old_tss_sel = 0xffff;
1951
1952 if (next_tss_desc.type & 8)
1953 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
1954 old_tss_base, &next_tss_desc);
1955 else
1956 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
1957 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02001958 if (ret != X86EMUL_CONTINUE)
1959 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001960
1961 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
1962 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
1963
1964 if (reason != TASK_SWITCH_IRET) {
1965 next_tss_desc.type |= (1 << 1); /* set busy flag */
1966 write_segment_descriptor(ctxt, ops, tss_selector,
1967 &next_tss_desc);
1968 }
1969
1970 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
1971 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
1972 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
1973
Jan Kiszkae269fb22010-04-14 15:51:09 +02001974 if (has_error_code) {
1975 struct decode_cache *c = &ctxt->decode;
1976
1977 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
1978 c->lock_prefix = 0;
1979 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001980 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02001981 }
1982
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001983 return ret;
1984}
1985
1986int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001987 u16 tss_selector, int reason,
1988 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001989{
Avi Kivity9aabc882010-07-29 15:11:50 +03001990 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001991 struct decode_cache *c = &ctxt->decode;
1992 int rc;
1993
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001994 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02001995 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001996
Jan Kiszkae269fb22010-04-14 15:51:09 +02001997 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
1998 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001999
2000 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002001 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002002 if (rc == X86EMUL_CONTINUE)
2003 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002004 }
2005
Gleb Natapov19d04432010-04-15 12:29:50 +03002006 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002007}
2008
Gleb Natapova682e352010-03-18 15:20:21 +02002009static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002010 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002011{
2012 struct decode_cache *c = &ctxt->decode;
2013 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2014
Gleb Natapovd9271122010-03-18 15:20:22 +02002015 register_address_increment(c, &c->regs[reg], df * op->bytes);
2016 op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002017}
2018
Avi Kivity63540382010-07-29 15:11:55 +03002019static int em_push(struct x86_emulate_ctxt *ctxt)
2020{
2021 emulate_push(ctxt, ctxt->ops);
2022 return X86EMUL_CONTINUE;
2023}
2024
Avi Kivity73fba5f2010-07-29 15:11:53 +03002025#define D(_y) { .flags = (_y) }
2026#define N D(0)
2027#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2028#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2029#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2030
2031static struct opcode group1[] = {
2032 X7(D(Lock)), N
2033};
2034
2035static struct opcode group1A[] = {
2036 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2037};
2038
2039static struct opcode group3[] = {
2040 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2041 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2042 X4(D(Undefined)),
2043};
2044
2045static struct opcode group4[] = {
2046 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2047 N, N, N, N, N, N,
2048};
2049
2050static struct opcode group5[] = {
2051 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2052 D(SrcMem | ModRM | Stack), N,
2053 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2054 D(SrcMem | ModRM | Stack), N,
2055};
2056
2057static struct group_dual group7 = { {
2058 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2059 D(SrcNone | ModRM | DstMem | Mov), N,
2060 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
2061}, {
2062 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2063 D(SrcNone | ModRM | DstMem | Mov), N,
2064 D(SrcMem16 | ModRM | Mov | Priv), N,
2065} };
2066
2067static struct opcode group8[] = {
2068 N, N, N, N,
2069 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2070 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2071};
2072
2073static struct group_dual group9 = { {
2074 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2075}, {
2076 N, N, N, N, N, N, N, N,
2077} };
2078
2079static struct opcode opcode_table[256] = {
2080 /* 0x00 - 0x07 */
2081 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2082 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2083 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2084 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2085 /* 0x08 - 0x0F */
2086 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2087 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2088 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2089 D(ImplicitOps | Stack | No64), N,
2090 /* 0x10 - 0x17 */
2091 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2092 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2093 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2094 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2095 /* 0x18 - 0x1F */
2096 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2097 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2098 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2099 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2100 /* 0x20 - 0x27 */
2101 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2102 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2103 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2104 /* 0x28 - 0x2F */
2105 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2106 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2107 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2108 /* 0x30 - 0x37 */
2109 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2110 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2111 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2112 /* 0x38 - 0x3F */
2113 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2114 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2115 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2116 N, N,
2117 /* 0x40 - 0x4F */
2118 X16(D(DstReg)),
2119 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002120 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002121 /* 0x58 - 0x5F */
2122 X8(D(DstReg | Stack)),
2123 /* 0x60 - 0x67 */
2124 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2125 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2126 N, N, N, N,
2127 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002128 I(SrcImm | Mov | Stack, em_push), N,
2129 I(SrcImmByte | Mov | Stack, em_push), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002130 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2131 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2132 /* 0x70 - 0x7F */
2133 X16(D(SrcImmByte)),
2134 /* 0x80 - 0x87 */
2135 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2136 G(DstMem | SrcImm | ModRM | Group, group1),
2137 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2138 G(DstMem | SrcImmByte | ModRM | Group, group1),
2139 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2140 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2141 /* 0x88 - 0x8F */
2142 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2143 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
2144 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
2145 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2146 /* 0x90 - 0x97 */
2147 D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg),
2148 /* 0x98 - 0x9F */
2149 N, N, D(SrcImmFAddr | No64), N,
2150 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2151 /* 0xA0 - 0xA7 */
2152 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2153 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2154 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2155 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2156 /* 0xA8 - 0xAF */
2157 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
2158 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
2159 D(ByteOp | DstDI | String), D(DstDI | String),
2160 /* 0xB0 - 0xB7 */
2161 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2162 /* 0xB8 - 0xBF */
2163 X8(D(DstReg | SrcImm | Mov)),
2164 /* 0xC0 - 0xC7 */
2165 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
2166 N, D(ImplicitOps | Stack), N, N,
2167 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2168 /* 0xC8 - 0xCF */
2169 N, N, N, D(ImplicitOps | Stack),
2170 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2171 /* 0xD0 - 0xD7 */
2172 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2173 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2174 N, N, N, N,
2175 /* 0xD8 - 0xDF */
2176 N, N, N, N, N, N, N, N,
2177 /* 0xE0 - 0xE7 */
2178 N, N, N, N,
2179 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2180 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2181 /* 0xE8 - 0xEF */
2182 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2183 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2184 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2185 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2186 /* 0xF0 - 0xF7 */
2187 N, N, N, N,
2188 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2189 /* 0xF8 - 0xFF */
2190 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
2191 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2192};
2193
2194static struct opcode twobyte_table[256] = {
2195 /* 0x00 - 0x0F */
2196 N, GD(0, &group7), N, N,
2197 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2198 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2199 N, D(ImplicitOps | ModRM), N, N,
2200 /* 0x10 - 0x1F */
2201 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2202 /* 0x20 - 0x2F */
2203 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
2204 D(ModRM | ImplicitOps | Priv), D(ModRM | Priv),
2205 N, N, N, N,
2206 N, N, N, N, N, N, N, N,
2207 /* 0x30 - 0x3F */
2208 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
2209 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2210 N, N, N, N, N, N, N, N,
2211 /* 0x40 - 0x4F */
2212 X16(D(DstReg | SrcMem | ModRM | Mov)),
2213 /* 0x50 - 0x5F */
2214 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2215 /* 0x60 - 0x6F */
2216 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2217 /* 0x70 - 0x7F */
2218 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2219 /* 0x80 - 0x8F */
2220 X16(D(SrcImm)),
2221 /* 0x90 - 0x9F */
2222 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2223 /* 0xA0 - 0xA7 */
2224 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2225 N, D(DstMem | SrcReg | ModRM | BitOp),
2226 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2227 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2228 /* 0xA8 - 0xAF */
2229 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2230 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2231 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2232 D(DstMem | SrcReg | Src2CL | ModRM),
2233 D(ModRM), N,
2234 /* 0xB0 - 0xB7 */
2235 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2236 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2237 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2238 D(DstReg | SrcMem16 | ModRM | Mov),
2239 /* 0xB8 - 0xBF */
2240 N, N,
2241 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2242 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2243 D(DstReg | SrcMem16 | ModRM | Mov),
2244 /* 0xC0 - 0xCF */
2245 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
2246 N, N, N, GD(0, &group9),
2247 N, N, N, N, N, N, N, N,
2248 /* 0xD0 - 0xDF */
2249 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2250 /* 0xE0 - 0xEF */
2251 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2252 /* 0xF0 - 0xFF */
2253 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2254};
2255
2256#undef D
2257#undef N
2258#undef G
2259#undef GD
2260#undef I
2261
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002262int
Avi Kivitydde7e6d2010-07-29 15:11:52 +03002263x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2264{
2265 struct x86_emulate_ops *ops = ctxt->ops;
2266 struct decode_cache *c = &ctxt->decode;
2267 int rc = X86EMUL_CONTINUE;
2268 int mode = ctxt->mode;
2269 int def_op_bytes, def_ad_bytes, dual, goffset;
2270 struct opcode opcode, *g_mod012, *g_mod3;
2271
2272 /* we cannot decode insn before we complete previous rep insn */
2273 WARN_ON(ctxt->restart);
2274
2275 c->eip = ctxt->eip;
2276 c->fetch.start = c->fetch.end = c->eip;
2277 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2278
2279 switch (mode) {
2280 case X86EMUL_MODE_REAL:
2281 case X86EMUL_MODE_VM86:
2282 case X86EMUL_MODE_PROT16:
2283 def_op_bytes = def_ad_bytes = 2;
2284 break;
2285 case X86EMUL_MODE_PROT32:
2286 def_op_bytes = def_ad_bytes = 4;
2287 break;
2288#ifdef CONFIG_X86_64
2289 case X86EMUL_MODE_PROT64:
2290 def_op_bytes = 4;
2291 def_ad_bytes = 8;
2292 break;
2293#endif
2294 default:
2295 return -1;
2296 }
2297
2298 c->op_bytes = def_op_bytes;
2299 c->ad_bytes = def_ad_bytes;
2300
2301 /* Legacy prefixes. */
2302 for (;;) {
2303 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2304 case 0x66: /* operand-size override */
2305 /* switch between 2/4 bytes */
2306 c->op_bytes = def_op_bytes ^ 6;
2307 break;
2308 case 0x67: /* address-size override */
2309 if (mode == X86EMUL_MODE_PROT64)
2310 /* switch between 4/8 bytes */
2311 c->ad_bytes = def_ad_bytes ^ 12;
2312 else
2313 /* switch between 2/4 bytes */
2314 c->ad_bytes = def_ad_bytes ^ 6;
2315 break;
2316 case 0x26: /* ES override */
2317 case 0x2e: /* CS override */
2318 case 0x36: /* SS override */
2319 case 0x3e: /* DS override */
2320 set_seg_override(c, (c->b >> 3) & 3);
2321 break;
2322 case 0x64: /* FS override */
2323 case 0x65: /* GS override */
2324 set_seg_override(c, c->b & 7);
2325 break;
2326 case 0x40 ... 0x4f: /* REX */
2327 if (mode != X86EMUL_MODE_PROT64)
2328 goto done_prefixes;
2329 c->rex_prefix = c->b;
2330 continue;
2331 case 0xf0: /* LOCK */
2332 c->lock_prefix = 1;
2333 break;
2334 case 0xf2: /* REPNE/REPNZ */
2335 c->rep_prefix = REPNE_PREFIX;
2336 break;
2337 case 0xf3: /* REP/REPE/REPZ */
2338 c->rep_prefix = REPE_PREFIX;
2339 break;
2340 default:
2341 goto done_prefixes;
2342 }
2343
2344 /* Any legacy prefix after a REX prefix nullifies its effect. */
2345
2346 c->rex_prefix = 0;
2347 }
2348
2349done_prefixes:
2350
2351 /* REX prefix. */
2352 if (c->rex_prefix)
2353 if (c->rex_prefix & 8)
2354 c->op_bytes = 8; /* REX.W */
2355
2356 /* Opcode byte(s). */
2357 opcode = opcode_table[c->b];
2358 if (opcode.flags == 0) {
2359 /* Two-byte opcode? */
2360 if (c->b == 0x0f) {
2361 c->twobyte = 1;
2362 c->b = insn_fetch(u8, 1, c->eip);
2363 opcode = twobyte_table[c->b];
2364 }
2365 }
2366 c->d = opcode.flags;
2367
2368 if (c->d & Group) {
2369 dual = c->d & GroupDual;
2370 c->modrm = insn_fetch(u8, 1, c->eip);
2371 --c->eip;
2372
2373 if (c->d & GroupDual) {
2374 g_mod012 = opcode.u.gdual->mod012;
2375 g_mod3 = opcode.u.gdual->mod3;
2376 } else
2377 g_mod012 = g_mod3 = opcode.u.group;
2378
2379 c->d &= ~(Group | GroupDual);
2380
2381 goffset = (c->modrm >> 3) & 7;
2382
2383 if ((c->modrm >> 6) == 3)
2384 opcode = g_mod3[goffset];
2385 else
2386 opcode = g_mod012[goffset];
2387 c->d |= opcode.flags;
2388 }
2389
2390 c->execute = opcode.u.execute;
2391
2392 /* Unrecognised? */
2393 if (c->d == 0 || (c->d & Undefined)) {
2394 DPRINTF("Cannot emulate %02x\n", c->b);
2395 return -1;
2396 }
2397
2398 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2399 c->op_bytes = 8;
2400
2401 /* ModRM and SIB bytes. */
2402 if (c->d & ModRM)
2403 rc = decode_modrm(ctxt, ops);
2404 else if (c->d & MemAbs)
2405 rc = decode_abs(ctxt, ops);
2406 if (rc != X86EMUL_CONTINUE)
2407 goto done;
2408
2409 if (!c->has_seg_override)
2410 set_seg_override(c, VCPU_SREG_DS);
2411
2412 if (!(!c->twobyte && c->b == 0x8d))
2413 c->modrm_ea += seg_override_base(ctxt, ops, c);
2414
2415 if (c->ad_bytes != 8)
2416 c->modrm_ea = (u32)c->modrm_ea;
2417
2418 if (c->rip_relative)
2419 c->modrm_ea += c->eip;
2420
2421 /*
2422 * Decode and fetch the source operand: register, memory
2423 * or immediate.
2424 */
2425 switch (c->d & SrcMask) {
2426 case SrcNone:
2427 break;
2428 case SrcReg:
2429 decode_register_operand(&c->src, c, 0);
2430 break;
2431 case SrcMem16:
2432 c->src.bytes = 2;
2433 goto srcmem_common;
2434 case SrcMem32:
2435 c->src.bytes = 4;
2436 goto srcmem_common;
2437 case SrcMem:
2438 c->src.bytes = (c->d & ByteOp) ? 1 :
2439 c->op_bytes;
2440 /* Don't fetch the address for invlpg: it could be unmapped. */
2441 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
2442 break;
2443 srcmem_common:
2444 /*
2445 * For instructions with a ModR/M byte, switch to register
2446 * access if Mod = 3.
2447 */
2448 if ((c->d & ModRM) && c->modrm_mod == 3) {
2449 c->src.type = OP_REG;
2450 c->src.val = c->modrm_val;
2451 c->src.ptr = c->modrm_ptr;
2452 break;
2453 }
2454 c->src.type = OP_MEM;
2455 c->src.ptr = (unsigned long *)c->modrm_ea;
2456 c->src.val = 0;
2457 break;
2458 case SrcImm:
2459 case SrcImmU:
2460 c->src.type = OP_IMM;
2461 c->src.ptr = (unsigned long *)c->eip;
2462 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2463 if (c->src.bytes == 8)
2464 c->src.bytes = 4;
2465 /* NB. Immediates are sign-extended as necessary. */
2466 switch (c->src.bytes) {
2467 case 1:
2468 c->src.val = insn_fetch(s8, 1, c->eip);
2469 break;
2470 case 2:
2471 c->src.val = insn_fetch(s16, 2, c->eip);
2472 break;
2473 case 4:
2474 c->src.val = insn_fetch(s32, 4, c->eip);
2475 break;
2476 }
2477 if ((c->d & SrcMask) == SrcImmU) {
2478 switch (c->src.bytes) {
2479 case 1:
2480 c->src.val &= 0xff;
2481 break;
2482 case 2:
2483 c->src.val &= 0xffff;
2484 break;
2485 case 4:
2486 c->src.val &= 0xffffffff;
2487 break;
2488 }
2489 }
2490 break;
2491 case SrcImmByte:
2492 case SrcImmUByte:
2493 c->src.type = OP_IMM;
2494 c->src.ptr = (unsigned long *)c->eip;
2495 c->src.bytes = 1;
2496 if ((c->d & SrcMask) == SrcImmByte)
2497 c->src.val = insn_fetch(s8, 1, c->eip);
2498 else
2499 c->src.val = insn_fetch(u8, 1, c->eip);
2500 break;
2501 case SrcAcc:
2502 c->src.type = OP_REG;
2503 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2504 c->src.ptr = &c->regs[VCPU_REGS_RAX];
2505 switch (c->src.bytes) {
2506 case 1:
2507 c->src.val = *(u8 *)c->src.ptr;
2508 break;
2509 case 2:
2510 c->src.val = *(u16 *)c->src.ptr;
2511 break;
2512 case 4:
2513 c->src.val = *(u32 *)c->src.ptr;
2514 break;
2515 case 8:
2516 c->src.val = *(u64 *)c->src.ptr;
2517 break;
2518 }
2519 break;
2520 case SrcOne:
2521 c->src.bytes = 1;
2522 c->src.val = 1;
2523 break;
2524 case SrcSI:
2525 c->src.type = OP_MEM;
2526 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2527 c->src.ptr = (unsigned long *)
2528 register_address(c, seg_override_base(ctxt, ops, c),
2529 c->regs[VCPU_REGS_RSI]);
2530 c->src.val = 0;
2531 break;
2532 case SrcImmFAddr:
2533 c->src.type = OP_IMM;
2534 c->src.ptr = (unsigned long *)c->eip;
2535 c->src.bytes = c->op_bytes + 2;
2536 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2537 break;
2538 case SrcMemFAddr:
2539 c->src.type = OP_MEM;
2540 c->src.ptr = (unsigned long *)c->modrm_ea;
2541 c->src.bytes = c->op_bytes + 2;
2542 break;
2543 }
2544
2545 /*
2546 * Decode and fetch the second source operand: register, memory
2547 * or immediate.
2548 */
2549 switch (c->d & Src2Mask) {
2550 case Src2None:
2551 break;
2552 case Src2CL:
2553 c->src2.bytes = 1;
2554 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2555 break;
2556 case Src2ImmByte:
2557 c->src2.type = OP_IMM;
2558 c->src2.ptr = (unsigned long *)c->eip;
2559 c->src2.bytes = 1;
2560 c->src2.val = insn_fetch(u8, 1, c->eip);
2561 break;
2562 case Src2One:
2563 c->src2.bytes = 1;
2564 c->src2.val = 1;
2565 break;
2566 }
2567
2568 /* Decode and fetch the destination operand: register or memory. */
2569 switch (c->d & DstMask) {
2570 case ImplicitOps:
2571 /* Special instructions do their own operand decoding. */
2572 return 0;
2573 case DstReg:
2574 decode_register_operand(&c->dst, c,
2575 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2576 break;
2577 case DstMem:
2578 case DstMem64:
2579 if ((c->d & ModRM) && c->modrm_mod == 3) {
2580 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2581 c->dst.type = OP_REG;
2582 c->dst.val = c->dst.orig_val = c->modrm_val;
2583 c->dst.ptr = c->modrm_ptr;
2584 break;
2585 }
2586 c->dst.type = OP_MEM;
2587 c->dst.ptr = (unsigned long *)c->modrm_ea;
2588 if ((c->d & DstMask) == DstMem64)
2589 c->dst.bytes = 8;
2590 else
2591 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2592 c->dst.val = 0;
2593 if (c->d & BitOp) {
2594 unsigned long mask = ~(c->dst.bytes * 8 - 1);
2595
2596 c->dst.ptr = (void *)c->dst.ptr +
2597 (c->src.val & mask) / 8;
2598 }
2599 break;
2600 case DstAcc:
2601 c->dst.type = OP_REG;
2602 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2603 c->dst.ptr = &c->regs[VCPU_REGS_RAX];
2604 switch (c->dst.bytes) {
2605 case 1:
2606 c->dst.val = *(u8 *)c->dst.ptr;
2607 break;
2608 case 2:
2609 c->dst.val = *(u16 *)c->dst.ptr;
2610 break;
2611 case 4:
2612 c->dst.val = *(u32 *)c->dst.ptr;
2613 break;
2614 case 8:
2615 c->dst.val = *(u64 *)c->dst.ptr;
2616 break;
2617 }
2618 c->dst.orig_val = c->dst.val;
2619 break;
2620 case DstDI:
2621 c->dst.type = OP_MEM;
2622 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2623 c->dst.ptr = (unsigned long *)
2624 register_address(c, es_base(ctxt, ops),
2625 c->regs[VCPU_REGS_RDI]);
2626 c->dst.val = 0;
2627 break;
2628 }
2629
2630done:
2631 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2632}
2633
2634int
Avi Kivity9aabc882010-07-29 15:11:50 +03002635x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002636{
Avi Kivity9aabc882010-07-29 15:11:50 +03002637 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002638 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002639 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002640 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002641 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002642
Gleb Natapov9de41572010-04-28 19:15:22 +03002643 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002644
Gleb Natapov11616242010-02-11 14:43:14 +02002645 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002646 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002647 goto done;
2648 }
2649
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002650 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002651 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002652 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002653 goto done;
2654 }
2655
Gleb Natapove92805a2010-02-10 14:21:35 +02002656 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002657 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002658 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002659 goto done;
2660 }
2661
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002662 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002663 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002664 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002665 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002666 string_done:
2667 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002668 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002669 goto done;
2670 }
2671 /* The second termination condition only applies for REPE
2672 * and REPNE. Test if the repeat string operation prefix is
2673 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2674 * corresponding termination condition according to:
2675 * - if REPE/REPZ and ZF = 0 then done
2676 * - if REPNE/REPNZ and ZF = 1 then done
2677 */
2678 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002679 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002680 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002681 ((ctxt->eflags & EFLG_ZF) == 0))
2682 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002683 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002684 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2685 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002686 }
Gleb Natapov063db062010-03-18 15:20:06 +02002687 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002688 }
2689
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002690 if (c->src.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002691 rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr,
Gleb Natapov414e6272010-04-28 19:15:26 +03002692 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002693 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002694 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002695 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002696 }
2697
Gleb Natapove35b7b92010-02-25 16:36:42 +02002698 if (c->src2.type == OP_MEM) {
Gleb Natapov9de41572010-04-28 19:15:22 +03002699 rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr,
2700 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002701 if (rc != X86EMUL_CONTINUE)
2702 goto done;
2703 }
2704
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002705 if ((c->d & DstMask) == ImplicitOps)
2706 goto special_insn;
2707
2708
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002709 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2710 /* optimisation - avoid slow emulated read if Mov */
Gleb Natapov9de41572010-04-28 19:15:22 +03002711 rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr,
2712 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002713 if (rc != X86EMUL_CONTINUE)
2714 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002715 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002716 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002717
Avi Kivity018a98d2007-11-27 19:30:56 +02002718special_insn:
2719
Avi Kivityef65c882010-07-29 15:11:51 +03002720 if (c->execute) {
2721 rc = c->execute(ctxt);
2722 if (rc != X86EMUL_CONTINUE)
2723 goto done;
2724 goto writeback;
2725 }
2726
Laurent Viviere4e03de2007-09-18 11:52:50 +02002727 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 goto twobyte_insn;
2729
Laurent Viviere4e03de2007-09-18 11:52:50 +02002730 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 case 0x00 ... 0x05:
2732 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002733 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002735 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002736 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002737 break;
2738 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002739 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002740 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002741 goto done;
2742 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 case 0x08 ... 0x0d:
2744 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002745 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002747 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002748 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002749 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 case 0x10 ... 0x15:
2751 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002752 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002754 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002755 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002756 break;
2757 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002758 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002759 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002760 goto done;
2761 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 case 0x18 ... 0x1d:
2763 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002764 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002766 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002767 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002768 break;
2769 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002770 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002771 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002772 goto done;
2773 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002774 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002776 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 break;
2778 case 0x28 ... 0x2d:
2779 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002780 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 break;
2782 case 0x30 ... 0x35:
2783 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002784 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 break;
2786 case 0x38 ... 0x3d:
2787 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002788 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002790 case 0x40 ... 0x47: /* inc r16/r32 */
2791 emulate_1op("inc", c->dst, ctxt->eflags);
2792 break;
2793 case 0x48 ... 0x4f: /* dec r16/r32 */
2794 emulate_1op("dec", c->dst, ctxt->eflags);
2795 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002796 case 0x58 ... 0x5f: /* pop reg */
2797 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002798 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002799 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002800 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002801 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002802 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002803 rc = emulate_pusha(ctxt, ops);
2804 if (rc != X86EMUL_CONTINUE)
2805 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002806 break;
2807 case 0x61: /* popa */
2808 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002809 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002810 goto done;
2811 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002813 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002815 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002817 case 0x6c: /* insb */
2818 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002819 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002820 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002821 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002822 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002823 goto done;
2824 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002825 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2826 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002827 goto done; /* IO is needed, skip writeback */
2828 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002829 case 0x6e: /* outsb */
2830 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002831 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002832 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002833 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002834 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002835 goto done;
2836 }
Gleb Natapov79729952010-03-18 15:20:24 +02002837 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2838 &c->src.val, 1, ctxt->vcpu);
2839
2840 c->dst.type = OP_NONE; /* nothing to writeback */
2841 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002842 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002843 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002844 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002845 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002847 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 case 0:
2849 goto add;
2850 case 1:
2851 goto or;
2852 case 2:
2853 goto adc;
2854 case 3:
2855 goto sbb;
2856 case 4:
2857 goto and;
2858 case 5:
2859 goto sub;
2860 case 6:
2861 goto xor;
2862 case 7:
2863 goto cmp;
2864 }
2865 break;
2866 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002867 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002868 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 break;
2870 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002871 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002873 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 case 1:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002875 *(u8 *) c->src.ptr = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 break;
2877 case 2:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002878 *(u16 *) c->src.ptr = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 break;
2880 case 4:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002881 *c->src.ptr = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 break; /* 64b reg: zero-extend */
2883 case 8:
Laurent Viviere4e03de2007-09-18 11:52:50 +02002884 *c->src.ptr = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 break;
2886 }
2887 /*
2888 * Write back the memory destination with implicit LOCK
2889 * prefix.
2890 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002891 c->dst.val = c->src.val;
2892 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002895 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002896 case 0x8c: /* mov r/m, sreg */
2897 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002898 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002899 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002900 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002901 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002902 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002903 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002904 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002905 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002906 case 0x8e: { /* mov seg, r/m16 */
2907 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002908
2909 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002910
Gleb Natapovc6975182010-02-18 12:15:01 +02002911 if (c->modrm_reg == VCPU_SREG_CS ||
2912 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002913 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002914 goto done;
2915 }
2916
Glauber Costa310b5d32009-05-12 16:21:06 -04002917 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002918 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002919
Gleb Natapov2e873022010-03-18 15:20:18 +02002920 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002921
2922 c->dst.type = OP_NONE; /* Disable writeback. */
2923 break;
2924 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002926 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002927 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 break;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002930 case 0x90: /* nop / xchg r8,rax */
Gleb Natapovb8a98942010-04-28 19:15:25 +03002931 if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) {
2932 c->dst.type = OP_NONE; /* nop */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002933 break;
2934 }
2935 case 0x91 ... 0x97: /* xchg reg,rax */
Gleb Natapovf0c13ef2010-04-28 19:15:24 +03002936 c->src.type = OP_REG;
2937 c->src.bytes = c->op_bytes;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002938 c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX];
2939 c->src.val = *(c->src.ptr);
2940 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002941 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002942 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002943 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002944 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002945 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002946 c->dst.type = OP_REG;
Laurent Vivier05f086f2007-09-24 11:10:55 +02002947 c->dst.ptr = (unsigned long *) &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002948 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002949 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2950 if (rc != X86EMUL_CONTINUE)
2951 goto done;
2952 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002953 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002955 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002957 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002958 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr);
Gleb Natapova682e352010-03-18 15:20:21 +02002959 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002960 case 0xa8 ... 0xa9: /* test ax, imm */
2961 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002963 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 break;
2965 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002966 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 case 0xae ... 0xaf: /* scas */
2968 DPRINTF("Urk! I don't handle SCAS.\n");
2969 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002970 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002971 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002972 case 0xc0 ... 0xc1:
2973 emulate_grp2(ctxt);
2974 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002975 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002976 c->dst.type = OP_REG;
Avi Kivity111de5d2007-11-27 19:14:21 +02002977 c->dst.ptr = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002978 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002979 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002980 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2981 mov:
2982 c->dst.val = c->src.val;
2983 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002984 case 0xcb: /* ret far */
2985 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002986 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002987 goto done;
2988 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002989 case 0xcf: /* iret */
2990 rc = emulate_iret(ctxt, ops);
2991
2992 if (rc != X86EMUL_CONTINUE)
2993 goto done;
2994 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002995 case 0xd0 ... 0xd1: /* Grp2 */
2996 c->src.val = 1;
2997 emulate_grp2(ctxt);
2998 break;
2999 case 0xd2 ... 0xd3: /* Grp2 */
3000 c->src.val = c->regs[VCPU_REGS_RCX];
3001 emulate_grp2(ctxt);
3002 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003003 case 0xe4: /* inb */
3004 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003005 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003006 case 0xe6: /* outb */
3007 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003008 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003009 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03003010 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003011 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08003012 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03003013 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003014 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003015 }
3016 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003017 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003018 case 0xea: { /* jmp far */
3019 unsigned short sel;
Gleb Natapovea798492010-02-25 16:36:43 +02003020 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003021 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3022
3023 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003024 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003025
Gleb Natapov414e6272010-04-28 19:15:26 +03003026 c->eip = 0;
3027 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003028 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003029 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003030 case 0xeb:
3031 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003032 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003033 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003034 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003035 case 0xec: /* in al,dx */
3036 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003037 c->src.val = c->regs[VCPU_REGS_RDX];
3038 do_io_in:
3039 c->dst.bytes = min(c->dst.bytes, 4u);
3040 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003041 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003042 goto done;
3043 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003044 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3045 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003046 goto done; /* IO is needed */
3047 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003048 case 0xee: /* out dx,al */
3049 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003050 c->src.val = c->regs[VCPU_REGS_RDX];
3051 do_io_out:
3052 c->dst.bytes = min(c->dst.bytes, 4u);
3053 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003054 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003055 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003056 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003057 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3058 ctxt->vcpu);
3059 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003060 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003061 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003062 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003063 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003064 case 0xf5: /* cmc */
3065 /* complement carry flag from eflags reg */
3066 ctxt->eflags ^= EFLG_CF;
3067 c->dst.type = OP_NONE; /* Disable writeback. */
3068 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003069 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003070 if (!emulate_grp3(ctxt, ops))
3071 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003072 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003073 case 0xf8: /* clc */
3074 ctxt->eflags &= ~EFLG_CF;
3075 c->dst.type = OP_NONE; /* Disable writeback. */
3076 break;
3077 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003078 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003079 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003080 goto done;
3081 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003082 ctxt->eflags &= ~X86_EFLAGS_IF;
3083 c->dst.type = OP_NONE; /* Disable writeback. */
3084 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003085 break;
3086 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003087 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003088 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003089 goto done;
3090 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003091 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003092 ctxt->eflags |= X86_EFLAGS_IF;
3093 c->dst.type = OP_NONE; /* Disable writeback. */
3094 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003095 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003096 case 0xfc: /* cld */
3097 ctxt->eflags &= ~EFLG_DF;
3098 c->dst.type = OP_NONE; /* Disable writeback. */
3099 break;
3100 case 0xfd: /* std */
3101 ctxt->eflags |= EFLG_DF;
3102 c->dst.type = OP_NONE; /* Disable writeback. */
3103 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003104 case 0xfe: /* Grp4 */
3105 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003106 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003107 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003108 goto done;
3109 break;
Gleb Natapovea798492010-02-25 16:36:43 +02003110 case 0xff: /* Grp5 */
3111 if (c->modrm_reg == 5)
3112 goto jump_far;
3113 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003114 default:
3115 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003117
3118writeback:
3119 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003120 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003121 goto done;
3122
Gleb Natapov5cd21912010-03-18 15:20:26 +02003123 /*
3124 * restore dst type in case the decoding will be reused
3125 * (happens for string instruction )
3126 */
3127 c->dst.type = saved_dst_type;
3128
Gleb Natapova682e352010-03-18 15:20:21 +02003129 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003130 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3131 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003132
3133 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003134 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3135 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003136
Gleb Natapov5cd21912010-03-18 15:20:26 +02003137 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003138 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003139 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003140 /*
3141 * Re-enter guest when pio read ahead buffer is empty or,
3142 * if it is not used, after each 1024 iteration.
3143 */
3144 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3145 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003146 ctxt->restart = false;
3147 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003148 /*
3149 * reset read cache here in case string instruction is restared
3150 * without decoding
3151 */
3152 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003153 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003154
3155done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003156 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157
3158twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003159 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003161 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 u16 size;
3163 unsigned long address;
3164
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003165 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003166 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003167 goto cannot_emulate;
3168
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003169 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003170 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003171 goto done;
3172
Avi Kivity33e38852008-05-21 15:34:25 +03003173 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003174 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003175 /* Disable writeback. */
3176 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003177 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 case 2: /* lgdt */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003179 rc = read_descriptor(ctxt, ops, c->src.ptr,
3180 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003181 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 goto done;
3183 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003184 /* Disable writeback. */
3185 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003187 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003188 if (c->modrm_mod == 3) {
3189 switch (c->modrm_rm) {
3190 case 1:
3191 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003192 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003193 goto done;
3194 break;
3195 default:
3196 goto cannot_emulate;
3197 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003198 } else {
Laurent Viviere4e03de2007-09-18 11:52:50 +02003199 rc = read_descriptor(ctxt, ops, c->src.ptr,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003200 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003201 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003202 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003203 goto done;
3204 realmode_lidt(ctxt->vcpu, size, address);
3205 }
Avi Kivity16286d02008-04-14 14:40:50 +03003206 /* Disable writeback. */
3207 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 break;
3209 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003210 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003211 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 break;
3213 case 6: /* lmsw */
Gleb Natapov93a152b2010-03-18 15:20:04 +02003214 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) |
3215 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003216 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003218 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003219 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003220 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003222 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003223 /* Disable writeback. */
3224 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 break;
3226 default:
3227 goto cannot_emulate;
3228 }
3229 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003230 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003231 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003232 if (rc != X86EMUL_CONTINUE)
3233 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003234 else
3235 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003236 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003237 case 0x06:
3238 emulate_clts(ctxt->vcpu);
3239 c->dst.type = OP_NONE;
3240 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003241 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003242 kvm_emulate_wbinvd(ctxt->vcpu);
3243 c->dst.type = OP_NONE;
3244 break;
3245 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003246 case 0x0d: /* GrpP (prefetch) */
3247 case 0x18: /* Grp16 (prefetch/nop) */
3248 c->dst.type = OP_NONE;
3249 break;
3250 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003251 switch (c->modrm_reg) {
3252 case 1:
3253 case 5 ... 7:
3254 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003255 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003256 goto done;
3257 }
Gleb Natapov52a46612010-03-18 15:20:03 +02003258 c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003259 c->dst.type = OP_NONE; /* no writeback */
3260 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003262 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3263 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003264 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003265 goto done;
3266 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003267 ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003268 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003270 case 0x22: /* mov reg, cr */
Gleb Natapov0f122442010-04-28 19:15:31 +03003271 if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003272 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003273 goto done;
3274 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003275 c->dst.type = OP_NONE;
3276 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003278 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3279 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003280 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003281 goto done;
3282 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003283
Gleb Natapov338dbc92010-04-28 19:15:32 +03003284 if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] &
3285 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3286 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3287 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003288 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003289 goto done;
3290 }
3291
Laurent Viviera01af5e2007-09-24 11:10:56 +02003292 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003294 case 0x30:
3295 /* wrmsr */
3296 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3297 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003298 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003299 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003300 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003301 }
3302 rc = X86EMUL_CONTINUE;
3303 c->dst.type = OP_NONE;
3304 break;
3305 case 0x32:
3306 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003307 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003308 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003309 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003310 } else {
3311 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3312 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3313 }
3314 rc = X86EMUL_CONTINUE;
3315 c->dst.type = OP_NONE;
3316 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003317 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003318 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003319 if (rc != X86EMUL_CONTINUE)
3320 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003321 else
3322 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003323 break;
3324 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003325 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003326 if (rc != X86EMUL_CONTINUE)
3327 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003328 else
3329 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003330 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003332 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003333 if (!test_cc(c->b, ctxt->eflags))
3334 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003336 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003337 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003338 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003339 c->dst.type = OP_NONE;
3340 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003341 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003342 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003343 break;
3344 case 0xa1: /* pop fs */
3345 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003346 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003347 goto done;
3348 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003349 case 0xa3:
3350 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003351 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003352 /* only subword offset */
3353 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003354 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003355 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003356 case 0xa4: /* shld imm8, r, r/m */
3357 case 0xa5: /* shld cl, r, r/m */
3358 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3359 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003360 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003361 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003362 break;
3363 case 0xa9: /* pop gs */
3364 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003365 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003366 goto done;
3367 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003368 case 0xab:
3369 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003370 /* only subword offset */
3371 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003372 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003373 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003374 case 0xac: /* shrd imm8, r, r/m */
3375 case 0xad: /* shrd cl, r, r/m */
3376 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3377 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003378 case 0xae: /* clflush */
3379 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 case 0xb0 ... 0xb1: /* cmpxchg */
3381 /*
3382 * Save real source value, then compare EAX against
3383 * destination.
3384 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003385 c->src.orig_val = c->src.val;
3386 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003387 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3388 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003390 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391 } else {
3392 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003393 c->dst.type = OP_REG;
3394 c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395 }
3396 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 case 0xb3:
3398 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003399 /* only subword offset */
3400 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003401 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003404 c->dst.bytes = c->op_bytes;
3405 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3406 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003409 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410 case 0:
3411 goto bt;
3412 case 1:
3413 goto bts;
3414 case 2:
3415 goto btr;
3416 case 3:
3417 goto btc;
3418 }
3419 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003420 case 0xbb:
3421 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003422 /* only subword offset */
3423 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003424 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003425 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003427 c->dst.bytes = c->op_bytes;
3428 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3429 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003431 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003432 c->dst.bytes = c->op_bytes;
3433 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3434 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003435 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003437 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003438 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003439 goto done;
3440 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003441 default:
3442 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443 }
3444 goto writeback;
3445
3446cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003447 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448 return -1;
3449}