blob: 628fb5de6a42d1781cc2683631b784b6e457e455 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Avi Kivity221d0592010-05-23 18:37:00 +030012 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
23#ifndef __KERNEL__
24#include <stdio.h>
25#include <stdint.h>
26#include <public/xen.h>
Mike Dayd77c26f2007-10-08 09:02:08 -040027#define DPRINTF(_f, _a ...) printf(_f , ## _a)
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#else
Avi Kivityedf88412007-12-16 11:02:48 +020029#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#define DPRINTF(x...) do {} while (0)
32#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030034#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity3eeb3282010-01-21 15:31:48 +020036#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020037#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020038
Avi Kivity6aa8b732006-12-10 02:21:36 -080039/*
40 * Opcode effective-address decode tables.
41 * Note that we only emulate instructions that have at least one memory
42 * operand (excluding implicit stack references). We assume that stack
43 * references and instruction fetches will never occur in special memory
44 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
45 * not be handled.
46 */
47
48/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030049#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080050/* Destination operand type. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030051#define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
52#define DstReg (2<<1) /* Register operand. */
53#define DstMem (3<<1) /* Memory operand. */
54#define DstAcc (4<<1) /* Destination Accumulator */
55#define DstDI (5<<1) /* Destination is in ES:(E)DI */
56#define DstMem64 (6<<1) /* 64bit memory operand */
57#define DstMask (7<<1)
Avi Kivity6aa8b732006-12-10 02:21:36 -080058/* Source operand type. */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020059#define SrcNone (0<<4) /* No source operand. */
60#define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */
61#define SrcReg (1<<4) /* Register operand. */
62#define SrcMem (2<<4) /* Memory operand. */
63#define SrcMem16 (3<<4) /* Memory operand (16-bit). */
64#define SrcMem32 (4<<4) /* Memory operand (32-bit). */
65#define SrcImm (5<<4) /* Immediate operand. */
66#define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */
Guillaume Thouveninbfcadf82008-12-04 14:27:38 +010067#define SrcOne (7<<4) /* Implied '1' */
Gleb Natapov341de7e2009-04-12 13:36:41 +030068#define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */
Avi Kivityc9eaf20f2009-05-18 16:13:45 +030069#define SrcImmU (9<<4) /* Immediate operand, unsigned */
Gleb Natapova682e352010-03-18 15:20:21 +020070#define SrcSI (0xa<<4) /* Source is in the DS:RSI */
Gleb Natapov414e6272010-04-28 19:15:26 +030071#define SrcImmFAddr (0xb<<4) /* Source is immediate far address */
72#define SrcMemFAddr (0xc<<4) /* Source is far address in memory */
Wei Yongjun5d55f292010-07-07 17:43:35 +080073#define SrcAcc (0xd<<4) /* Source Accumulator */
Gleb Natapov341de7e2009-04-12 13:36:41 +030074#define SrcMask (0xf<<4)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075/* Generic ModRM decode. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030076#define ModRM (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -080077/* Destination is only written; never read. */
Gleb Natapov341de7e2009-04-12 13:36:41 +030078#define Mov (1<<9)
79#define BitOp (1<<10)
80#define MemAbs (1<<11) /* Memory operand is absolute displacement */
Guillaume Thouvenin9c9fddd2008-09-12 13:50:25 +020081#define String (1<<12) /* String instruction (rep capable) */
82#define Stack (1<<13) /* Stack instruction (push/pop) */
Avi Kivitye09d0822008-01-18 12:38:59 +020083#define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */
84#define GroupDual (1<<15) /* Alternate decoding of mod == 3 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030085/* Misc flags */
Avi Kivity7f9b4b72010-08-01 14:46:54 +030086#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +030087#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +020088#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +020089#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +030090#define No64 (1<<28)
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +010091/* Source 2 operand type */
92#define Src2None (0<<29)
93#define Src2CL (1<<29)
94#define Src2ImmByte (2<<29)
95#define Src2One (3<<29)
96#define Src2Mask (7<<29)
Avi Kivity6aa8b732006-12-10 02:21:36 -080097
Avi Kivityd0e53322010-07-29 15:11:54 +030098#define X2(x...) x, x
99#define X3(x...) X2(x), x
100#define X4(x...) X2(x), X2(x)
101#define X5(x...) X4(x), x
102#define X6(x...) X4(x), X2(x)
103#define X7(x...) X4(x), X3(x)
104#define X8(x...) X4(x), X4(x)
105#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300106
Avi Kivityd65b1de2010-07-29 15:11:35 +0300107struct opcode {
108 u32 flags;
Avi Kivity120df892010-07-29 15:11:39 +0300109 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300110 int (*execute)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300111 struct opcode *group;
112 struct group_dual *gdual;
113 } u;
114};
115
116struct group_dual {
117 struct opcode mod012[8];
118 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300119};
120
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200122#define EFLG_ID (1<<21)
123#define EFLG_VIP (1<<20)
124#define EFLG_VIF (1<<19)
125#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200126#define EFLG_VM (1<<17)
127#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200128#define EFLG_IOPL (3<<12)
129#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130#define EFLG_OF (1<<11)
131#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200132#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200133#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134#define EFLG_SF (1<<7)
135#define EFLG_ZF (1<<6)
136#define EFLG_AF (1<<4)
137#define EFLG_PF (1<<2)
138#define EFLG_CF (1<<0)
139
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300140#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
141#define EFLG_RESERVED_ONE_MASK 2
142
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143/*
144 * Instruction emulation:
145 * Most instructions are emulated directly via a fragment of inline assembly
146 * code. This allows us to save/restore EFLAGS and thus very easily pick up
147 * any modified flags.
148 */
149
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800150#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151#define _LO32 "k" /* force 32-bit operand */
152#define _STK "%%rsp" /* stack pointer */
153#elif defined(__i386__)
154#define _LO32 "" /* force 32-bit operand */
155#define _STK "%%esp" /* stack pointer */
156#endif
157
158/*
159 * These EFLAGS bits are restored from saved value during emulation, and
160 * any changes are written back to the saved value after emulation.
161 */
162#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
163
164/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200165#define _PRE_EFLAGS(_sav, _msk, _tmp) \
166 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
167 "movl %"_sav",%"_LO32 _tmp"; " \
168 "push %"_tmp"; " \
169 "push %"_tmp"; " \
170 "movl %"_msk",%"_LO32 _tmp"; " \
171 "andl %"_LO32 _tmp",("_STK"); " \
172 "pushf; " \
173 "notl %"_LO32 _tmp"; " \
174 "andl %"_LO32 _tmp",("_STK"); " \
175 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
176 "pop %"_tmp"; " \
177 "orl %"_LO32 _tmp",("_STK"); " \
178 "popf; " \
179 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180
181/* After executing instruction: write-back necessary bits in EFLAGS. */
182#define _POST_EFLAGS(_sav, _msk, _tmp) \
183 /* _sav |= EFLAGS & _msk; */ \
184 "pushf; " \
185 "pop %"_tmp"; " \
186 "andl %"_msk",%"_LO32 _tmp"; " \
187 "orl %"_LO32 _tmp",%"_sav"; "
188
Avi Kivitydda96d82008-11-26 15:14:10 +0200189#ifdef CONFIG_X86_64
190#define ON64(x) x
191#else
192#define ON64(x)
193#endif
194
Avi Kivity6b7ad612008-11-26 15:30:45 +0200195#define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \
196 do { \
197 __asm__ __volatile__ ( \
198 _PRE_EFLAGS("0", "4", "2") \
199 _op _suffix " %"_x"3,%1; " \
200 _POST_EFLAGS("0", "4", "2") \
201 : "=m" (_eflags), "=m" ((_dst).val), \
202 "=&r" (_tmp) \
203 : _y ((_src).val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200204 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200205
206
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207/* Raw emulation: instruction has two explicit operands. */
208#define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200209 do { \
210 unsigned long _tmp; \
211 \
212 switch ((_dst).bytes) { \
213 case 2: \
214 ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \
215 break; \
216 case 4: \
217 ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \
218 break; \
219 case 8: \
220 ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \
221 break; \
222 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800223 } while (0)
224
225#define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
226 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200227 unsigned long _tmp; \
Mike Dayd77c26f2007-10-08 09:02:08 -0400228 switch ((_dst).bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229 case 1: \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200230 ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 break; \
232 default: \
233 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
234 _wx, _wy, _lx, _ly, _qx, _qy); \
235 break; \
236 } \
237 } while (0)
238
239/* Source operand is byte-sized and may be restricted to just %cl. */
240#define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
241 __emulate_2op(_op, _src, _dst, _eflags, \
242 "b", "c", "b", "c", "b", "c", "b", "c")
243
244/* Source operand is byte, word, long or quad sized. */
245#define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
246 __emulate_2op(_op, _src, _dst, _eflags, \
247 "b", "q", "w", "r", _LO32, "r", "", "r")
248
249/* Source operand is word, long or quad sized. */
250#define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
251 __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
252 "w", "r", _LO32, "r", "", "r")
253
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100254/* Instruction has three operands and one operand is stored in ECX register */
255#define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
256 do { \
257 unsigned long _tmp; \
258 _type _clv = (_cl).val; \
259 _type _srcv = (_src).val; \
260 _type _dstv = (_dst).val; \
261 \
262 __asm__ __volatile__ ( \
263 _PRE_EFLAGS("0", "5", "2") \
264 _op _suffix " %4,%1 \n" \
265 _POST_EFLAGS("0", "5", "2") \
266 : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
267 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
268 ); \
269 \
270 (_cl).val = (unsigned long) _clv; \
271 (_src).val = (unsigned long) _srcv; \
272 (_dst).val = (unsigned long) _dstv; \
273 } while (0)
274
275#define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
276 do { \
277 switch ((_dst).bytes) { \
278 case 2: \
279 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
280 "w", unsigned short); \
281 break; \
282 case 4: \
283 __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
284 "l", unsigned int); \
285 break; \
286 case 8: \
287 ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
288 "q", unsigned long)); \
289 break; \
290 } \
291 } while (0)
292
Avi Kivitydda96d82008-11-26 15:14:10 +0200293#define __emulate_1op(_op, _dst, _eflags, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294 do { \
295 unsigned long _tmp; \
296 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200297 __asm__ __volatile__ ( \
298 _PRE_EFLAGS("0", "3", "2") \
299 _op _suffix " %1; " \
300 _POST_EFLAGS("0", "3", "2") \
301 : "=m" (_eflags), "+m" ((_dst).val), \
302 "=&r" (_tmp) \
303 : "i" (EFLAGS_MASK)); \
304 } while (0)
305
306/* Instruction has only one explicit operand (no source operand). */
307#define emulate_1op(_op, _dst, _eflags) \
308 do { \
Mike Dayd77c26f2007-10-08 09:02:08 -0400309 switch ((_dst).bytes) { \
Avi Kivitydda96d82008-11-26 15:14:10 +0200310 case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
311 case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
312 case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
313 case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314 } \
315 } while (0)
316
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317/* Fetch next part of the instruction being emulated. */
318#define insn_fetch(_type, _size, _eip) \
319({ unsigned long _x; \
Avi Kivity62266862007-11-20 13:15:52 +0200320 rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \
Gleb Natapovaf5b4f72010-03-15 16:38:30 +0200321 if (rc != X86EMUL_CONTINUE) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 goto done; \
323 (_eip) += (_size); \
324 (_type)_x; \
325})
326
Gleb Natapov414e6272010-04-28 19:15:26 +0300327#define insn_fetch_arr(_arr, _size, _eip) \
328({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \
329 if (rc != X86EMUL_CONTINUE) \
330 goto done; \
331 (_eip) += (_size); \
332})
333
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800334static inline unsigned long ad_mask(struct decode_cache *c)
335{
336 return (1UL << (c->ad_bytes << 3)) - 1;
337}
338
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800340static inline unsigned long
341address_mask(struct decode_cache *c, unsigned long reg)
342{
343 if (c->ad_bytes == sizeof(unsigned long))
344 return reg;
345 else
346 return reg & ad_mask(c);
347}
348
349static inline unsigned long
350register_address(struct decode_cache *c, unsigned long base, unsigned long reg)
351{
352 return base + address_mask(c, reg);
353}
354
Harvey Harrison7a9572752008-02-19 07:40:41 -0800355static inline void
356register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
357{
358 if (c->ad_bytes == sizeof(unsigned long))
359 *reg += inc;
360 else
361 *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
362}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363
Harvey Harrison7a9572752008-02-19 07:40:41 -0800364static inline void jmp_rel(struct decode_cache *c, int rel)
365{
366 register_address_increment(c, &c->eip, rel);
367}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300368
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300369static void set_seg_override(struct decode_cache *c, int seg)
370{
371 c->has_seg_override = true;
372 c->seg_override = seg;
373}
374
Gleb Natapov79168fd2010-04-28 19:15:30 +0300375static unsigned long seg_base(struct x86_emulate_ctxt *ctxt,
376 struct x86_emulate_ops *ops, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300377{
378 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
379 return 0;
380
Gleb Natapov79168fd2010-04-28 19:15:30 +0300381 return ops->get_cached_segment_base(seg, ctxt->vcpu);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300382}
383
384static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +0300385 struct x86_emulate_ops *ops,
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300386 struct decode_cache *c)
387{
388 if (!c->has_seg_override)
389 return 0;
390
Gleb Natapov79168fd2010-04-28 19:15:30 +0300391 return seg_base(ctxt, ops, c->seg_override);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300392}
393
Gleb Natapov79168fd2010-04-28 19:15:30 +0300394static unsigned long es_base(struct x86_emulate_ctxt *ctxt,
395 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300396{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300397 return seg_base(ctxt, ops, VCPU_SREG_ES);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300398}
399
Gleb Natapov79168fd2010-04-28 19:15:30 +0300400static unsigned long ss_base(struct x86_emulate_ctxt *ctxt,
401 struct x86_emulate_ops *ops)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300402{
Gleb Natapov79168fd2010-04-28 19:15:30 +0300403 return seg_base(ctxt, ops, VCPU_SREG_SS);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300404}
405
Gleb Natapov54b84862010-04-28 19:15:44 +0300406static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
407 u32 error, bool valid)
408{
409 ctxt->exception = vec;
410 ctxt->error_code = error;
411 ctxt->error_code_valid = valid;
412 ctxt->restart = false;
413}
414
415static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
416{
417 emulate_exception(ctxt, GP_VECTOR, err, true);
418}
419
420static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
421 int err)
422{
423 ctxt->cr2 = addr;
424 emulate_exception(ctxt, PF_VECTOR, err, true);
425}
426
427static void emulate_ud(struct x86_emulate_ctxt *ctxt)
428{
429 emulate_exception(ctxt, UD_VECTOR, 0, false);
430}
431
432static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
433{
434 emulate_exception(ctxt, TS_VECTOR, err, true);
435}
436
Avi Kivity62266862007-11-20 13:15:52 +0200437static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
438 struct x86_emulate_ops *ops,
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300439 unsigned long eip, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200440{
441 struct fetch_cache *fc = &ctxt->decode.fetch;
442 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300443 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200444
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300445 if (eip == fc->end) {
446 cur_size = fc->end - fc->start;
447 size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
448 rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size,
449 size, ctxt->vcpu, NULL);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900450 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200451 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300452 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200453 }
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300454 *dest = fc->data[eip - fc->start];
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900455 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200456}
457
458static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
459 struct x86_emulate_ops *ops,
460 unsigned long eip, void *dest, unsigned size)
461{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900462 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200463
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200464 /* x86 instructions are limited to 15 bytes. */
Gleb Natapov063db062010-03-18 15:20:06 +0200465 if (eip + size - ctxt->eip > 15)
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200466 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200467 while (size--) {
468 rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900469 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200470 return rc;
471 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900472 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200473}
474
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000475/*
476 * Given the 'reg' portion of a ModRM byte, and a register block, return a
477 * pointer into the block that addresses the relevant register.
478 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
479 */
480static void *decode_register(u8 modrm_reg, unsigned long *regs,
481 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482{
483 void *p;
484
485 p = &regs[modrm_reg];
486 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
487 p = (unsigned char *)&regs[modrm_reg & 3] + 1;
488 return p;
489}
490
491static int read_descriptor(struct x86_emulate_ctxt *ctxt,
492 struct x86_emulate_ops *ops,
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300493 ulong addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494 u16 *size, unsigned long *address, int op_bytes)
495{
496 int rc;
497
498 if (op_bytes == 2)
499 op_bytes = 3;
500 *address = 0;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300501 rc = ops->read_std(addr, (unsigned long *)size, 2, 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;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300504 rc = ops->read_std(addr + 2, address, op_bytes, ctxt->vcpu, NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 return rc;
506}
507
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300508static int test_cc(unsigned int condition, unsigned int flags)
509{
510 int rc = 0;
511
512 switch ((condition & 15) >> 1) {
513 case 0: /* o */
514 rc |= (flags & EFLG_OF);
515 break;
516 case 1: /* b/c/nae */
517 rc |= (flags & EFLG_CF);
518 break;
519 case 2: /* z/e */
520 rc |= (flags & EFLG_ZF);
521 break;
522 case 3: /* be/na */
523 rc |= (flags & (EFLG_CF|EFLG_ZF));
524 break;
525 case 4: /* s */
526 rc |= (flags & EFLG_SF);
527 break;
528 case 5: /* p/pe */
529 rc |= (flags & EFLG_PF);
530 break;
531 case 7: /* le/ng */
532 rc |= (flags & EFLG_ZF);
533 /* fall through */
534 case 6: /* l/nge */
535 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
536 break;
537 }
538
539 /* Odd condition identifiers (lsb == 1) have inverted sense. */
540 return (!!rc ^ (condition & 1));
541}
542
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300543static void fetch_register_operand(struct operand *op)
544{
545 switch (op->bytes) {
546 case 1:
547 op->val = *(u8 *)op->addr.reg;
548 break;
549 case 2:
550 op->val = *(u16 *)op->addr.reg;
551 break;
552 case 4:
553 op->val = *(u32 *)op->addr.reg;
554 break;
555 case 8:
556 op->val = *(u64 *)op->addr.reg;
557 break;
558 }
559}
560
Avi Kivity3c118e22007-10-31 10:27:04 +0200561static void decode_register_operand(struct operand *op,
562 struct decode_cache *c,
Avi Kivity3c118e22007-10-31 10:27:04 +0200563 int inhibit_bytereg)
564{
Avi Kivity33615aa2007-10-31 11:15:56 +0200565 unsigned reg = c->modrm_reg;
Avi Kivity9f1ef3f2007-10-31 11:21:06 +0200566 int highbyte_regs = c->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +0200567
568 if (!(c->d & ModRM))
569 reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
Avi Kivity3c118e22007-10-31 10:27:04 +0200570 op->type = OP_REG;
571 if ((c->d & ByteOp) && !inhibit_bytereg) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300572 op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +0200573 op->bytes = 1;
574 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300575 op->addr.reg = decode_register(reg, c->regs, 0);
Avi Kivity3c118e22007-10-31 10:27:04 +0200576 op->bytes = c->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +0200577 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300578 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +0200579 op->orig_val = op->val;
580}
581
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200582static int decode_modrm(struct x86_emulate_ctxt *ctxt,
583 struct x86_emulate_ops *ops)
584{
585 struct decode_cache *c = &ctxt->decode;
586 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700587 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900588 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200589
590 if (c->rex_prefix) {
591 c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
592 index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
593 c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
594 }
595
596 c->modrm = insn_fetch(u8, 1, c->eip);
597 c->modrm_mod |= (c->modrm & 0xc0) >> 6;
598 c->modrm_reg |= (c->modrm & 0x38) >> 3;
599 c->modrm_rm |= (c->modrm & 0x07);
600 c->modrm_ea = 0;
Avi Kivity09ee57c2010-08-01 12:07:29 +0300601 c->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200602
603 if (c->modrm_mod == 3) {
Avi Kivity107d6d22008-05-05 14:58:26 +0300604 c->modrm_ptr = decode_register(c->modrm_rm,
605 c->regs, c->d & ByteOp);
606 c->modrm_val = *(unsigned long *)c->modrm_ptr;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200607 return rc;
608 }
609
610 if (c->ad_bytes == 2) {
611 unsigned bx = c->regs[VCPU_REGS_RBX];
612 unsigned bp = c->regs[VCPU_REGS_RBP];
613 unsigned si = c->regs[VCPU_REGS_RSI];
614 unsigned di = c->regs[VCPU_REGS_RDI];
615
616 /* 16-bit ModR/M decode. */
617 switch (c->modrm_mod) {
618 case 0:
619 if (c->modrm_rm == 6)
620 c->modrm_ea += insn_fetch(u16, 2, c->eip);
621 break;
622 case 1:
623 c->modrm_ea += insn_fetch(s8, 1, c->eip);
624 break;
625 case 2:
626 c->modrm_ea += insn_fetch(u16, 2, c->eip);
627 break;
628 }
629 switch (c->modrm_rm) {
630 case 0:
631 c->modrm_ea += bx + si;
632 break;
633 case 1:
634 c->modrm_ea += bx + di;
635 break;
636 case 2:
637 c->modrm_ea += bp + si;
638 break;
639 case 3:
640 c->modrm_ea += bp + di;
641 break;
642 case 4:
643 c->modrm_ea += si;
644 break;
645 case 5:
646 c->modrm_ea += di;
647 break;
648 case 6:
649 if (c->modrm_mod != 0)
650 c->modrm_ea += bp;
651 break;
652 case 7:
653 c->modrm_ea += bx;
654 break;
655 }
656 if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
657 (c->modrm_rm == 6 && c->modrm_mod != 0))
Avi Kivity09ee57c2010-08-01 12:07:29 +0300658 c->modrm_seg = VCPU_SREG_SS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200659 c->modrm_ea = (u16)c->modrm_ea;
660 } else {
661 /* 32/64-bit ModR/M decode. */
Avi Kivity84411d82008-06-15 21:53:26 -0700662 if ((c->modrm_rm & 7) == 4) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200663 sib = insn_fetch(u8, 1, c->eip);
664 index_reg |= (sib >> 3) & 7;
665 base_reg |= sib & 7;
666 scale = sib >> 6;
667
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700668 if ((base_reg & 7) == 5 && c->modrm_mod == 0)
669 c->modrm_ea += insn_fetch(s32, 4, c->eip);
670 else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200671 c->modrm_ea += c->regs[base_reg];
Avi Kivitydc71d0f2008-06-15 21:23:17 -0700672 if (index_reg != 4)
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200673 c->modrm_ea += c->regs[index_reg] << scale;
Avi Kivity84411d82008-06-15 21:53:26 -0700674 } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
675 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700676 c->rip_relative = 1;
Avi Kivity84411d82008-06-15 21:53:26 -0700677 } else
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200678 c->modrm_ea += c->regs[c->modrm_rm];
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200679 switch (c->modrm_mod) {
680 case 0:
681 if (c->modrm_rm == 5)
682 c->modrm_ea += insn_fetch(s32, 4, c->eip);
683 break;
684 case 1:
685 c->modrm_ea += insn_fetch(s8, 1, c->eip);
686 break;
687 case 2:
688 c->modrm_ea += insn_fetch(s32, 4, c->eip);
689 break;
690 }
691 }
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200692done:
693 return rc;
694}
695
696static int decode_abs(struct x86_emulate_ctxt *ctxt,
697 struct x86_emulate_ops *ops)
698{
699 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900700 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +0200701
702 switch (c->ad_bytes) {
703 case 2:
704 c->modrm_ea = insn_fetch(u16, 2, c->eip);
705 break;
706 case 4:
707 c->modrm_ea = insn_fetch(u32, 4, c->eip);
708 break;
709 case 8:
710 c->modrm_ea = insn_fetch(u64, 8, c->eip);
711 break;
712 }
713done:
714 return rc;
715}
716
Gleb Natapov9de41572010-04-28 19:15:22 +0300717static int read_emulated(struct x86_emulate_ctxt *ctxt,
718 struct x86_emulate_ops *ops,
719 unsigned long addr, void *dest, unsigned size)
720{
721 int rc;
722 struct read_cache *mc = &ctxt->decode.mem_read;
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300723 u32 err;
Gleb Natapov9de41572010-04-28 19:15:22 +0300724
725 while (size) {
726 int n = min(size, 8u);
727 size -= n;
728 if (mc->pos < mc->end)
729 goto read_cached;
730
Gleb Natapov8fe681e2010-04-28 19:15:37 +0300731 rc = ops->read_emulated(addr, mc->data + mc->end, n, &err,
732 ctxt->vcpu);
733 if (rc == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300734 emulate_pf(ctxt, addr, err);
Gleb Natapov9de41572010-04-28 19:15:22 +0300735 if (rc != X86EMUL_CONTINUE)
736 return rc;
737 mc->end += n;
738
739 read_cached:
740 memcpy(dest, mc->data + mc->pos, n);
741 mc->pos += n;
742 dest += n;
743 addr += n;
744 }
745 return X86EMUL_CONTINUE;
746}
747
Gleb Natapov7b262e92010-03-18 15:20:27 +0200748static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
749 struct x86_emulate_ops *ops,
750 unsigned int size, unsigned short port,
751 void *dest)
752{
753 struct read_cache *rc = &ctxt->decode.io_read;
754
755 if (rc->pos == rc->end) { /* refill pio read ahead */
756 struct decode_cache *c = &ctxt->decode;
757 unsigned int in_page, n;
758 unsigned int count = c->rep_prefix ?
759 address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
760 in_page = (ctxt->eflags & EFLG_DF) ?
761 offset_in_page(c->regs[VCPU_REGS_RDI]) :
762 PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
763 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
764 count);
765 if (n == 0)
766 n = 1;
767 rc->pos = rc->end = 0;
768 if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
769 return 0;
770 rc->end = n * size;
771 }
772
773 memcpy(dest, rc->data + rc->pos, size);
774 rc->pos += size;
775 return 1;
776}
777
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200778static u32 desc_limit_scaled(struct desc_struct *desc)
779{
780 u32 limit = get_desc_limit(desc);
781
782 return desc->g ? (limit << 12) | 0xfff : limit;
783}
784
785static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
786 struct x86_emulate_ops *ops,
787 u16 selector, struct desc_ptr *dt)
788{
789 if (selector & 1 << 2) {
790 struct desc_struct desc;
791 memset (dt, 0, sizeof *dt);
792 if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu))
793 return;
794
795 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
796 dt->address = get_desc_base(&desc);
797 } else
798 ops->get_gdt(dt, ctxt->vcpu);
799}
800
801/* allowed just for 8 bytes segments */
802static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
803 struct x86_emulate_ops *ops,
804 u16 selector, struct desc_struct *desc)
805{
806 struct desc_ptr dt;
807 u16 index = selector >> 3;
808 int ret;
809 u32 err;
810 ulong addr;
811
812 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
813
814 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300815 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200816 return X86EMUL_PROPAGATE_FAULT;
817 }
818 addr = dt.address + index * 8;
819 ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
820 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300821 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200822
823 return ret;
824}
825
826/* allowed just for 8 bytes segments */
827static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
828 struct x86_emulate_ops *ops,
829 u16 selector, struct desc_struct *desc)
830{
831 struct desc_ptr dt;
832 u16 index = selector >> 3;
833 u32 err;
834 ulong addr;
835 int ret;
836
837 get_descriptor_table_ptr(ctxt, ops, selector, &dt);
838
839 if (dt.size < index * 8 + 7) {
Gleb Natapov54b84862010-04-28 19:15:44 +0300840 emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200841 return X86EMUL_PROPAGATE_FAULT;
842 }
843
844 addr = dt.address + index * 8;
845 ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err);
846 if (ret == X86EMUL_PROPAGATE_FAULT)
Gleb Natapov54b84862010-04-28 19:15:44 +0300847 emulate_pf(ctxt, addr, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200848
849 return ret;
850}
851
852static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
853 struct x86_emulate_ops *ops,
854 u16 selector, int seg)
855{
856 struct desc_struct seg_desc;
857 u8 dpl, rpl, cpl;
858 unsigned err_vec = GP_VECTOR;
859 u32 err_code = 0;
860 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
861 int ret;
862
863 memset(&seg_desc, 0, sizeof seg_desc);
864
865 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
866 || ctxt->mode == X86EMUL_MODE_REAL) {
867 /* set real mode segment descriptor */
868 set_desc_base(&seg_desc, selector << 4);
869 set_desc_limit(&seg_desc, 0xffff);
870 seg_desc.type = 3;
871 seg_desc.p = 1;
872 seg_desc.s = 1;
873 goto load;
874 }
875
876 /* NULL selector is not valid for TR, CS and SS */
877 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
878 && null_selector)
879 goto exception;
880
881 /* TR should be in GDT only */
882 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
883 goto exception;
884
885 if (null_selector) /* for NULL selector skip all following checks */
886 goto load;
887
888 ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc);
889 if (ret != X86EMUL_CONTINUE)
890 return ret;
891
892 err_code = selector & 0xfffc;
893 err_vec = GP_VECTOR;
894
895 /* can't load system descriptor into segment selecor */
896 if (seg <= VCPU_SREG_GS && !seg_desc.s)
897 goto exception;
898
899 if (!seg_desc.p) {
900 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
901 goto exception;
902 }
903
904 rpl = selector & 3;
905 dpl = seg_desc.dpl;
906 cpl = ops->cpl(ctxt->vcpu);
907
908 switch (seg) {
909 case VCPU_SREG_SS:
910 /*
911 * segment is not a writable data segment or segment
912 * selector's RPL != CPL or segment selector's RPL != CPL
913 */
914 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
915 goto exception;
916 break;
917 case VCPU_SREG_CS:
918 if (!(seg_desc.type & 8))
919 goto exception;
920
921 if (seg_desc.type & 4) {
922 /* conforming */
923 if (dpl > cpl)
924 goto exception;
925 } else {
926 /* nonconforming */
927 if (rpl > cpl || dpl != cpl)
928 goto exception;
929 }
930 /* CS(RPL) <- CPL */
931 selector = (selector & 0xfffc) | cpl;
932 break;
933 case VCPU_SREG_TR:
934 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
935 goto exception;
936 break;
937 case VCPU_SREG_LDTR:
938 if (seg_desc.s || seg_desc.type != 2)
939 goto exception;
940 break;
941 default: /* DS, ES, FS, or GS */
942 /*
943 * segment is not a data or readable code segment or
944 * ((segment is a data or nonconforming code segment)
945 * and (both RPL and CPL > DPL))
946 */
947 if ((seg_desc.type & 0xa) == 0x8 ||
948 (((seg_desc.type & 0xc) != 0xc) &&
949 (rpl > dpl && cpl > dpl)))
950 goto exception;
951 break;
952 }
953
954 if (seg_desc.s) {
955 /* mark segment as accessed */
956 seg_desc.type |= 1;
957 ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc);
958 if (ret != X86EMUL_CONTINUE)
959 return ret;
960 }
961load:
962 ops->set_segment_selector(selector, seg, ctxt->vcpu);
963 ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu);
964 return X86EMUL_CONTINUE;
965exception:
Gleb Natapov54b84862010-04-28 19:15:44 +0300966 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200967 return X86EMUL_PROPAGATE_FAULT;
968}
969
Wei Yongjunc37eda12010-06-15 09:03:33 +0800970static inline int writeback(struct x86_emulate_ctxt *ctxt,
971 struct x86_emulate_ops *ops)
972{
973 int rc;
974 struct decode_cache *c = &ctxt->decode;
975 u32 err;
976
977 switch (c->dst.type) {
978 case OP_REG:
979 /* The 4-byte case *is* correct:
980 * in 64-bit mode we zero-extend.
981 */
982 switch (c->dst.bytes) {
983 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300984 *(u8 *)c->dst.addr.reg = (u8)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800985 break;
986 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300987 *(u16 *)c->dst.addr.reg = (u16)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800988 break;
989 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300990 *c->dst.addr.reg = (u32)c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800991 break; /* 64b: zero-ext */
992 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300993 *c->dst.addr.reg = c->dst.val;
Wei Yongjunc37eda12010-06-15 09:03:33 +0800994 break;
995 }
996 break;
997 case OP_MEM:
998 if (c->lock_prefix)
999 rc = ops->cmpxchg_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001000 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001001 &c->dst.orig_val,
1002 &c->dst.val,
1003 c->dst.bytes,
1004 &err,
1005 ctxt->vcpu);
1006 else
1007 rc = ops->write_emulated(
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001008 c->dst.addr.mem,
Wei Yongjunc37eda12010-06-15 09:03:33 +08001009 &c->dst.val,
1010 c->dst.bytes,
1011 &err,
1012 ctxt->vcpu);
1013 if (rc == X86EMUL_PROPAGATE_FAULT)
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001014 emulate_pf(ctxt, c->dst.addr.mem, err);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001015 if (rc != X86EMUL_CONTINUE)
1016 return rc;
1017 break;
1018 case OP_NONE:
1019 /* no writeback */
1020 break;
1021 default:
1022 break;
1023 }
1024 return X86EMUL_CONTINUE;
1025}
1026
Gleb Natapov79168fd2010-04-28 19:15:30 +03001027static inline void emulate_push(struct x86_emulate_ctxt *ctxt,
1028 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001029{
1030 struct decode_cache *c = &ctxt->decode;
1031
1032 c->dst.type = OP_MEM;
1033 c->dst.bytes = c->op_bytes;
1034 c->dst.val = c->src.val;
Harvey Harrison7a9572752008-02-19 07:40:41 -08001035 register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03001036 c->dst.addr.mem = register_address(c, ss_base(ctxt, ops),
1037 c->regs[VCPU_REGS_RSP]);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001038}
1039
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001040static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001041 struct x86_emulate_ops *ops,
1042 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001043{
1044 struct decode_cache *c = &ctxt->decode;
1045 int rc;
1046
Gleb Natapov79168fd2010-04-28 19:15:30 +03001047 rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops),
Gleb Natapov9de41572010-04-28 19:15:22 +03001048 c->regs[VCPU_REGS_RSP]),
1049 dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001050 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001051 return rc;
1052
Avi Kivity350f69d2009-01-05 11:12:40 +02001053 register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001054 return rc;
1055}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001056
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001057static int emulate_popf(struct x86_emulate_ctxt *ctxt,
1058 struct x86_emulate_ops *ops,
1059 void *dest, int len)
1060{
1061 int rc;
1062 unsigned long val, change_mask;
1063 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001064 int cpl = ops->cpl(ctxt->vcpu);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001065
1066 rc = emulate_pop(ctxt, ops, &val, len);
1067 if (rc != X86EMUL_CONTINUE)
1068 return rc;
1069
1070 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1071 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1072
1073 switch(ctxt->mode) {
1074 case X86EMUL_MODE_PROT64:
1075 case X86EMUL_MODE_PROT32:
1076 case X86EMUL_MODE_PROT16:
1077 if (cpl == 0)
1078 change_mask |= EFLG_IOPL;
1079 if (cpl <= iopl)
1080 change_mask |= EFLG_IF;
1081 break;
1082 case X86EMUL_MODE_VM86:
1083 if (iopl < 3) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001084 emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001085 return X86EMUL_PROPAGATE_FAULT;
1086 }
1087 change_mask |= EFLG_IF;
1088 break;
1089 default: /* real mode */
1090 change_mask |= (EFLG_IOPL | EFLG_IF);
1091 break;
1092 }
1093
1094 *(unsigned long *)dest =
1095 (ctxt->eflags & ~change_mask) | (val & change_mask);
1096
1097 return rc;
1098}
1099
Gleb Natapov79168fd2010-04-28 19:15:30 +03001100static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
1101 struct x86_emulate_ops *ops, int seg)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001102{
1103 struct decode_cache *c = &ctxt->decode;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001104
Gleb Natapov79168fd2010-04-28 19:15:30 +03001105 c->src.val = ops->get_segment_selector(seg, ctxt->vcpu);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001106
Gleb Natapov79168fd2010-04-28 19:15:30 +03001107 emulate_push(ctxt, ops);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001108}
1109
1110static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
1111 struct x86_emulate_ops *ops, int seg)
1112{
1113 struct decode_cache *c = &ctxt->decode;
1114 unsigned long selector;
1115 int rc;
1116
1117 rc = emulate_pop(ctxt, ops, &selector, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001118 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001119 return rc;
1120
Gleb Natapov2e873022010-03-18 15:20:18 +02001121 rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001122 return rc;
1123}
1124
Wei Yongjunc37eda12010-06-15 09:03:33 +08001125static int emulate_pusha(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001126 struct x86_emulate_ops *ops)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001127{
1128 struct decode_cache *c = &ctxt->decode;
1129 unsigned long old_esp = c->regs[VCPU_REGS_RSP];
Wei Yongjunc37eda12010-06-15 09:03:33 +08001130 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001131 int reg = VCPU_REGS_RAX;
1132
1133 while (reg <= VCPU_REGS_RDI) {
1134 (reg == VCPU_REGS_RSP) ?
1135 (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
1136
Gleb Natapov79168fd2010-04-28 19:15:30 +03001137 emulate_push(ctxt, ops);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001138
1139 rc = writeback(ctxt, ops);
1140 if (rc != X86EMUL_CONTINUE)
1141 return rc;
1142
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001143 ++reg;
1144 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001145
1146 /* Disable writeback. */
1147 c->dst.type = OP_NONE;
1148
1149 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001150}
1151
1152static int emulate_popa(struct x86_emulate_ctxt *ctxt,
1153 struct x86_emulate_ops *ops)
1154{
1155 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001156 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001157 int reg = VCPU_REGS_RDI;
1158
1159 while (reg >= VCPU_REGS_RAX) {
1160 if (reg == VCPU_REGS_RSP) {
1161 register_address_increment(c, &c->regs[VCPU_REGS_RSP],
1162 c->op_bytes);
1163 --reg;
1164 }
1165
1166 rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001167 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001168 break;
1169 --reg;
1170 }
1171 return rc;
1172}
1173
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001174static int emulate_iret_real(struct x86_emulate_ctxt *ctxt,
1175 struct x86_emulate_ops *ops)
1176{
1177 struct decode_cache *c = &ctxt->decode;
1178 int rc = X86EMUL_CONTINUE;
1179 unsigned long temp_eip = 0;
1180 unsigned long temp_eflags = 0;
1181 unsigned long cs = 0;
1182 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1183 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1184 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1185 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1186
1187 /* TODO: Add stack limit check */
1188
1189 rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes);
1190
1191 if (rc != X86EMUL_CONTINUE)
1192 return rc;
1193
1194 if (temp_eip & ~0xffff) {
1195 emulate_gp(ctxt, 0);
1196 return X86EMUL_PROPAGATE_FAULT;
1197 }
1198
1199 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
1200
1201 if (rc != X86EMUL_CONTINUE)
1202 return rc;
1203
1204 rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes);
1205
1206 if (rc != X86EMUL_CONTINUE)
1207 return rc;
1208
1209 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
1210
1211 if (rc != X86EMUL_CONTINUE)
1212 return rc;
1213
1214 c->eip = temp_eip;
1215
1216
1217 if (c->op_bytes == 4)
1218 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
1219 else if (c->op_bytes == 2) {
1220 ctxt->eflags &= ~0xffff;
1221 ctxt->eflags |= temp_eflags;
1222 }
1223
1224 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1225 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1226
1227 return rc;
1228}
1229
1230static inline int emulate_iret(struct x86_emulate_ctxt *ctxt,
1231 struct x86_emulate_ops* ops)
1232{
1233 switch(ctxt->mode) {
1234 case X86EMUL_MODE_REAL:
1235 return emulate_iret_real(ctxt, ops);
1236 case X86EMUL_MODE_VM86:
1237 case X86EMUL_MODE_PROT16:
1238 case X86EMUL_MODE_PROT32:
1239 case X86EMUL_MODE_PROT64:
1240 default:
1241 /* iret from protected mode unimplemented yet */
1242 return X86EMUL_UNHANDLEABLE;
1243 }
1244}
1245
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001246static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
1247 struct x86_emulate_ops *ops)
1248{
1249 struct decode_cache *c = &ctxt->decode;
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001250
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001251 return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001252}
1253
Laurent Vivier05f086f2007-09-24 11:10:55 +02001254static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001255{
Laurent Vivier05f086f2007-09-24 11:10:55 +02001256 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001257 switch (c->modrm_reg) {
1258 case 0: /* rol */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001259 emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001260 break;
1261 case 1: /* ror */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001262 emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001263 break;
1264 case 2: /* rcl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001265 emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001266 break;
1267 case 3: /* rcr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001268 emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001269 break;
1270 case 4: /* sal/shl */
1271 case 6: /* sal/shl */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001272 emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001273 break;
1274 case 5: /* shr */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001275 emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001276 break;
1277 case 7: /* sar */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001278 emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001279 break;
1280 }
1281}
1282
1283static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt,
Laurent Vivier05f086f2007-09-24 11:10:55 +02001284 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001285{
1286 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001287
1288 switch (c->modrm_reg) {
1289 case 0 ... 1: /* test */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001290 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001291 break;
1292 case 2: /* not */
1293 c->dst.val = ~c->dst.val;
1294 break;
1295 case 3: /* neg */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001296 emulate_1op("neg", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001297 break;
1298 default:
Gleb Natapovaca06a82010-03-18 15:20:15 +02001299 return 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001300 }
Gleb Natapovaca06a82010-03-18 15:20:15 +02001301 return 1;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001302}
1303
1304static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt,
Laurent Viviera01af5e2007-09-24 11:10:56 +02001305 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001306{
1307 struct decode_cache *c = &ctxt->decode;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001308
1309 switch (c->modrm_reg) {
1310 case 0: /* inc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001311 emulate_1op("inc", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001312 break;
1313 case 1: /* dec */
Laurent Vivier05f086f2007-09-24 11:10:55 +02001314 emulate_1op("dec", c->dst, ctxt->eflags);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001315 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03001316 case 2: /* call near abs */ {
1317 long int old_eip;
1318 old_eip = c->eip;
1319 c->eip = c->src.val;
1320 c->src.val = old_eip;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001321 emulate_push(ctxt, ops);
Mohammed Gamald19292e2008-09-08 21:47:19 +03001322 break;
1323 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001324 case 4: /* jmp abs */
Avi Kivityfd607542008-01-18 13:12:26 +02001325 c->eip = c->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001326 break;
1327 case 6: /* push */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001328 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001329 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001330 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001331 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001332}
1333
1334static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt,
Gleb Natapov69f55cb2010-03-18 15:20:20 +02001335 struct x86_emulate_ops *ops)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001336{
1337 struct decode_cache *c = &ctxt->decode;
Avi Kivity16518d52010-08-26 14:31:30 +03001338 u64 old = c->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001339
1340 if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
1341 ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001342 c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
1343 c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02001344 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001345 } else {
Avi Kivity16518d52010-08-26 14:31:30 +03001346 c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
1347 (u32) c->regs[VCPU_REGS_RBX];
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001348
Laurent Vivier05f086f2007-09-24 11:10:55 +02001349 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001350 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001351 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001352}
1353
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001354static int emulate_ret_far(struct x86_emulate_ctxt *ctxt,
1355 struct x86_emulate_ops *ops)
1356{
1357 struct decode_cache *c = &ctxt->decode;
1358 int rc;
1359 unsigned long cs;
1360
1361 rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001362 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001363 return rc;
1364 if (c->op_bytes == 4)
1365 c->eip = (u32)c->eip;
1366 rc = emulate_pop(ctxt, ops, &cs, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001367 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001368 return rc;
Gleb Natapov2e873022010-03-18 15:20:18 +02001369 rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02001370 return rc;
1371}
1372
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001373static inline void
1374setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Gleb Natapov79168fd2010-04-28 19:15:30 +03001375 struct x86_emulate_ops *ops, struct desc_struct *cs,
1376 struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001377{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001378 memset(cs, 0, sizeof(struct desc_struct));
1379 ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu);
1380 memset(ss, 0, sizeof(struct desc_struct));
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001381
1382 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001383 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001384 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001385 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001386 cs->type = 0x0b; /* Read, Execute, Accessed */
1387 cs->s = 1;
1388 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001389 cs->p = 1;
1390 cs->d = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001391
Gleb Natapov79168fd2010-04-28 19:15:30 +03001392 set_desc_base(ss, 0); /* flat segment */
1393 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001394 ss->g = 1; /* 4kb granularity */
1395 ss->s = 1;
1396 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03001397 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001398 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001399 ss->p = 1;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001400}
1401
1402static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001403emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001404{
1405 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001406 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001407 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001408 u16 cs_sel, ss_sel;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001409
1410 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001411 if (ctxt->mode == X86EMUL_MODE_REAL ||
1412 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001413 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001414 return X86EMUL_PROPAGATE_FAULT;
1415 }
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001416
Gleb Natapov79168fd2010-04-28 19:15:30 +03001417 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001418
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001419 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001420 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001421 cs_sel = (u16)(msr_data & 0xfffc);
1422 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001423
1424 if (is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001425 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001426 cs.l = 1;
1427 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001428 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1429 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1430 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1431 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001432
1433 c->regs[VCPU_REGS_RCX] = c->eip;
1434 if (is_long_mode(ctxt->vcpu)) {
1435#ifdef CONFIG_X86_64
1436 c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
1437
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001438 ops->get_msr(ctxt->vcpu,
1439 ctxt->mode == X86EMUL_MODE_PROT64 ?
1440 MSR_LSTAR : MSR_CSTAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001441 c->eip = msr_data;
1442
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001443 ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001444 ctxt->eflags &= ~(msr_data | EFLG_RF);
1445#endif
1446 } else {
1447 /* legacy mode */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001448 ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001449 c->eip = (u32)msr_data;
1450
1451 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
1452 }
1453
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001454 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02001455}
1456
Andre Przywara8c604352009-06-18 12:56:01 +02001457static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001458emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara8c604352009-06-18 12:56:01 +02001459{
1460 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001461 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02001462 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001463 u16 cs_sel, ss_sel;
Andre Przywara8c604352009-06-18 12:56:01 +02001464
Gleb Natapova0044752010-02-10 14:21:31 +02001465 /* inject #GP if in real mode */
1466 if (ctxt->mode == X86EMUL_MODE_REAL) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001467 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001468 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001469 }
1470
1471 /* XXX sysenter/sysexit have not been tested in 64bit mode.
1472 * Therefore, we inject an #UD.
1473 */
Gleb Natapov2e901c42010-03-18 15:20:12 +02001474 if (ctxt->mode == X86EMUL_MODE_PROT64) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001475 emulate_ud(ctxt);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001476 return X86EMUL_PROPAGATE_FAULT;
1477 }
Andre Przywara8c604352009-06-18 12:56:01 +02001478
Gleb Natapov79168fd2010-04-28 19:15:30 +03001479 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02001480
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001481 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001482 switch (ctxt->mode) {
1483 case X86EMUL_MODE_PROT32:
1484 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001485 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001486 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001487 }
1488 break;
1489 case X86EMUL_MODE_PROT64:
1490 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001491 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001492 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara8c604352009-06-18 12:56:01 +02001493 }
1494 break;
1495 }
1496
1497 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03001498 cs_sel = (u16)msr_data;
1499 cs_sel &= ~SELECTOR_RPL_MASK;
1500 ss_sel = cs_sel + 8;
1501 ss_sel &= ~SELECTOR_RPL_MASK;
Andre Przywara8c604352009-06-18 12:56:01 +02001502 if (ctxt->mode == X86EMUL_MODE_PROT64
1503 || is_long_mode(ctxt->vcpu)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03001504 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02001505 cs.l = 1;
1506 }
1507
Gleb Natapov79168fd2010-04-28 19:15:30 +03001508 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1509 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1510 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1511 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara8c604352009-06-18 12:56:01 +02001512
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001513 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001514 c->eip = msr_data;
1515
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001516 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02001517 c->regs[VCPU_REGS_RSP] = msr_data;
1518
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001519 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02001520}
1521
Andre Przywara4668f052009-06-18 12:56:02 +02001522static int
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001523emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
Andre Przywara4668f052009-06-18 12:56:02 +02001524{
1525 struct decode_cache *c = &ctxt->decode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001526 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02001527 u64 msr_data;
1528 int usermode;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001529 u16 cs_sel, ss_sel;
Andre Przywara4668f052009-06-18 12:56:02 +02001530
Gleb Natapova0044752010-02-10 14:21:31 +02001531 /* inject #GP if in real mode or Virtual 8086 mode */
1532 if (ctxt->mode == X86EMUL_MODE_REAL ||
1533 ctxt->mode == X86EMUL_MODE_VM86) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001534 emulate_gp(ctxt, 0);
Gleb Natapov2e901c42010-03-18 15:20:12 +02001535 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001536 }
1537
Gleb Natapov79168fd2010-04-28 19:15:30 +03001538 setup_syscalls_segments(ctxt, ops, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02001539
1540 if ((c->rex_prefix & 0x8) != 0x0)
1541 usermode = X86EMUL_MODE_PROT64;
1542 else
1543 usermode = X86EMUL_MODE_PROT32;
1544
1545 cs.dpl = 3;
1546 ss.dpl = 3;
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03001547 ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02001548 switch (usermode) {
1549 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001550 cs_sel = (u16)(msr_data + 16);
Andre Przywara4668f052009-06-18 12:56:02 +02001551 if ((msr_data & 0xfffc) == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001552 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001553 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001554 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001555 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02001556 break;
1557 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03001558 cs_sel = (u16)(msr_data + 32);
Andre Przywara4668f052009-06-18 12:56:02 +02001559 if (msr_data == 0x0) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001560 emulate_gp(ctxt, 0);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001561 return X86EMUL_PROPAGATE_FAULT;
Andre Przywara4668f052009-06-18 12:56:02 +02001562 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001563 ss_sel = cs_sel + 8;
1564 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02001565 cs.l = 1;
1566 break;
1567 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03001568 cs_sel |= SELECTOR_RPL_MASK;
1569 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02001570
Gleb Natapov79168fd2010-04-28 19:15:30 +03001571 ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu);
1572 ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu);
1573 ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu);
1574 ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu);
Andre Przywara4668f052009-06-18 12:56:02 +02001575
Gleb Natapovbdb475a2010-04-28 19:15:41 +03001576 c->eip = c->regs[VCPU_REGS_RDX];
1577 c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
Andre Przywara4668f052009-06-18 12:56:02 +02001578
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02001579 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02001580}
1581
Gleb Natapov9c537242010-03-18 15:20:05 +02001582static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt,
1583 struct x86_emulate_ops *ops)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001584{
1585 int iopl;
1586 if (ctxt->mode == X86EMUL_MODE_REAL)
1587 return false;
1588 if (ctxt->mode == X86EMUL_MODE_VM86)
1589 return true;
1590 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Gleb Natapov9c537242010-03-18 15:20:05 +02001591 return ops->cpl(ctxt->vcpu) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001592}
1593
1594static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
1595 struct x86_emulate_ops *ops,
1596 u16 port, u16 len)
1597{
Gleb Natapov79168fd2010-04-28 19:15:30 +03001598 struct desc_struct tr_seg;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001599 int r;
1600 u16 io_bitmap_ptr;
1601 u8 perm, bit_idx = port & 0x7;
1602 unsigned mask = (1 << len) - 1;
1603
Gleb Natapov79168fd2010-04-28 19:15:30 +03001604 ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu);
1605 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001606 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001607 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001608 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001609 r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2,
1610 ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001611 if (r != X86EMUL_CONTINUE)
1612 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001613 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001614 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001615 r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8,
1616 &perm, 1, ctxt->vcpu, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001617 if (r != X86EMUL_CONTINUE)
1618 return false;
1619 if ((perm >> bit_idx) & mask)
1620 return false;
1621 return true;
1622}
1623
1624static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
1625 struct x86_emulate_ops *ops,
1626 u16 port, u16 len)
1627{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001628 if (ctxt->perm_ok)
1629 return true;
1630
Gleb Natapov9c537242010-03-18 15:20:05 +02001631 if (emulator_bad_iopl(ctxt, ops))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001632 if (!emulator_io_port_access_allowed(ctxt, ops, port, len))
1633 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03001634
1635 ctxt->perm_ok = true;
1636
Gleb Natapovf850e2e2010-02-10 14:21:33 +02001637 return true;
1638}
1639
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001640static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
1641 struct x86_emulate_ops *ops,
1642 struct tss_segment_16 *tss)
1643{
1644 struct decode_cache *c = &ctxt->decode;
1645
1646 tss->ip = c->eip;
1647 tss->flag = ctxt->eflags;
1648 tss->ax = c->regs[VCPU_REGS_RAX];
1649 tss->cx = c->regs[VCPU_REGS_RCX];
1650 tss->dx = c->regs[VCPU_REGS_RDX];
1651 tss->bx = c->regs[VCPU_REGS_RBX];
1652 tss->sp = c->regs[VCPU_REGS_RSP];
1653 tss->bp = c->regs[VCPU_REGS_RBP];
1654 tss->si = c->regs[VCPU_REGS_RSI];
1655 tss->di = c->regs[VCPU_REGS_RDI];
1656
1657 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1658 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1659 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1660 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1661 tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1662}
1663
1664static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
1665 struct x86_emulate_ops *ops,
1666 struct tss_segment_16 *tss)
1667{
1668 struct decode_cache *c = &ctxt->decode;
1669 int ret;
1670
1671 c->eip = tss->ip;
1672 ctxt->eflags = tss->flag | 2;
1673 c->regs[VCPU_REGS_RAX] = tss->ax;
1674 c->regs[VCPU_REGS_RCX] = tss->cx;
1675 c->regs[VCPU_REGS_RDX] = tss->dx;
1676 c->regs[VCPU_REGS_RBX] = tss->bx;
1677 c->regs[VCPU_REGS_RSP] = tss->sp;
1678 c->regs[VCPU_REGS_RBP] = tss->bp;
1679 c->regs[VCPU_REGS_RSI] = tss->si;
1680 c->regs[VCPU_REGS_RDI] = tss->di;
1681
1682 /*
1683 * SDM says that segment selectors are loaded before segment
1684 * descriptors
1685 */
1686 ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu);
1687 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1688 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1689 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1690 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1691
1692 /*
1693 * Now load segment descriptors. If fault happenes at this stage
1694 * it is handled in a context of new task
1695 */
1696 ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR);
1697 if (ret != X86EMUL_CONTINUE)
1698 return ret;
1699 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1700 if (ret != X86EMUL_CONTINUE)
1701 return ret;
1702 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1703 if (ret != X86EMUL_CONTINUE)
1704 return ret;
1705 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1706 if (ret != X86EMUL_CONTINUE)
1707 return ret;
1708 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1709 if (ret != X86EMUL_CONTINUE)
1710 return ret;
1711
1712 return X86EMUL_CONTINUE;
1713}
1714
1715static int task_switch_16(struct x86_emulate_ctxt *ctxt,
1716 struct x86_emulate_ops *ops,
1717 u16 tss_selector, u16 old_tss_sel,
1718 ulong old_tss_base, struct desc_struct *new_desc)
1719{
1720 struct tss_segment_16 tss_seg;
1721 int ret;
1722 u32 err, new_tss_base = get_desc_base(new_desc);
1723
1724 ret = ops->read_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 save_state_to_tss16(ctxt, ops, &tss_seg);
1733
1734 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1735 &err);
1736 if (ret == X86EMUL_PROPAGATE_FAULT) {
1737 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001738 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001739 return ret;
1740 }
1741
1742 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1743 &err);
1744 if (ret == X86EMUL_PROPAGATE_FAULT) {
1745 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001746 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001747 return ret;
1748 }
1749
1750 if (old_tss_sel != 0xffff) {
1751 tss_seg.prev_task_link = old_tss_sel;
1752
1753 ret = ops->write_std(new_tss_base,
1754 &tss_seg.prev_task_link,
1755 sizeof tss_seg.prev_task_link,
1756 ctxt->vcpu, &err);
1757 if (ret == X86EMUL_PROPAGATE_FAULT) {
1758 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001759 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001760 return ret;
1761 }
1762 }
1763
1764 return load_state_from_tss16(ctxt, ops, &tss_seg);
1765}
1766
1767static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
1768 struct x86_emulate_ops *ops,
1769 struct tss_segment_32 *tss)
1770{
1771 struct decode_cache *c = &ctxt->decode;
1772
1773 tss->cr3 = ops->get_cr(3, ctxt->vcpu);
1774 tss->eip = c->eip;
1775 tss->eflags = ctxt->eflags;
1776 tss->eax = c->regs[VCPU_REGS_RAX];
1777 tss->ecx = c->regs[VCPU_REGS_RCX];
1778 tss->edx = c->regs[VCPU_REGS_RDX];
1779 tss->ebx = c->regs[VCPU_REGS_RBX];
1780 tss->esp = c->regs[VCPU_REGS_RSP];
1781 tss->ebp = c->regs[VCPU_REGS_RBP];
1782 tss->esi = c->regs[VCPU_REGS_RSI];
1783 tss->edi = c->regs[VCPU_REGS_RDI];
1784
1785 tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu);
1786 tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu);
1787 tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu);
1788 tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu);
1789 tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu);
1790 tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu);
1791 tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu);
1792}
1793
1794static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
1795 struct x86_emulate_ops *ops,
1796 struct tss_segment_32 *tss)
1797{
1798 struct decode_cache *c = &ctxt->decode;
1799 int ret;
1800
Gleb Natapov0f122442010-04-28 19:15:31 +03001801 if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001802 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03001803 return X86EMUL_PROPAGATE_FAULT;
1804 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001805 c->eip = tss->eip;
1806 ctxt->eflags = tss->eflags | 2;
1807 c->regs[VCPU_REGS_RAX] = tss->eax;
1808 c->regs[VCPU_REGS_RCX] = tss->ecx;
1809 c->regs[VCPU_REGS_RDX] = tss->edx;
1810 c->regs[VCPU_REGS_RBX] = tss->ebx;
1811 c->regs[VCPU_REGS_RSP] = tss->esp;
1812 c->regs[VCPU_REGS_RBP] = tss->ebp;
1813 c->regs[VCPU_REGS_RSI] = tss->esi;
1814 c->regs[VCPU_REGS_RDI] = tss->edi;
1815
1816 /*
1817 * SDM says that segment selectors are loaded before segment
1818 * descriptors
1819 */
1820 ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu);
1821 ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu);
1822 ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu);
1823 ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu);
1824 ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu);
1825 ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu);
1826 ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu);
1827
1828 /*
1829 * Now load segment descriptors. If fault happenes at this stage
1830 * it is handled in a context of new task
1831 */
1832 ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR);
1833 if (ret != X86EMUL_CONTINUE)
1834 return ret;
1835 ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES);
1836 if (ret != X86EMUL_CONTINUE)
1837 return ret;
1838 ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS);
1839 if (ret != X86EMUL_CONTINUE)
1840 return ret;
1841 ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS);
1842 if (ret != X86EMUL_CONTINUE)
1843 return ret;
1844 ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS);
1845 if (ret != X86EMUL_CONTINUE)
1846 return ret;
1847 ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS);
1848 if (ret != X86EMUL_CONTINUE)
1849 return ret;
1850 ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS);
1851 if (ret != X86EMUL_CONTINUE)
1852 return ret;
1853
1854 return X86EMUL_CONTINUE;
1855}
1856
1857static int task_switch_32(struct x86_emulate_ctxt *ctxt,
1858 struct x86_emulate_ops *ops,
1859 u16 tss_selector, u16 old_tss_sel,
1860 ulong old_tss_base, struct desc_struct *new_desc)
1861{
1862 struct tss_segment_32 tss_seg;
1863 int ret;
1864 u32 err, new_tss_base = get_desc_base(new_desc);
1865
1866 ret = ops->read_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 save_state_to_tss32(ctxt, ops, &tss_seg);
1875
1876 ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1877 &err);
1878 if (ret == X86EMUL_PROPAGATE_FAULT) {
1879 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001880 emulate_pf(ctxt, old_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001881 return ret;
1882 }
1883
1884 ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu,
1885 &err);
1886 if (ret == X86EMUL_PROPAGATE_FAULT) {
1887 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001888 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001889 return ret;
1890 }
1891
1892 if (old_tss_sel != 0xffff) {
1893 tss_seg.prev_task_link = old_tss_sel;
1894
1895 ret = ops->write_std(new_tss_base,
1896 &tss_seg.prev_task_link,
1897 sizeof tss_seg.prev_task_link,
1898 ctxt->vcpu, &err);
1899 if (ret == X86EMUL_PROPAGATE_FAULT) {
1900 /* FIXME: need to provide precise fault address */
Gleb Natapov54b84862010-04-28 19:15:44 +03001901 emulate_pf(ctxt, new_tss_base, err);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001902 return ret;
1903 }
1904 }
1905
1906 return load_state_from_tss32(ctxt, ops, &tss_seg);
1907}
1908
1909static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001910 struct x86_emulate_ops *ops,
1911 u16 tss_selector, int reason,
1912 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001913{
1914 struct desc_struct curr_tss_desc, next_tss_desc;
1915 int ret;
1916 u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu);
1917 ulong old_tss_base =
Gleb Natapov5951c442010-04-28 19:15:29 +03001918 ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu);
Gleb Natapovceffb452010-03-18 15:20:19 +02001919 u32 desc_limit;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001920
1921 /* FIXME: old_tss_base == ~0 ? */
1922
1923 ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc);
1924 if (ret != X86EMUL_CONTINUE)
1925 return ret;
1926 ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc);
1927 if (ret != X86EMUL_CONTINUE)
1928 return ret;
1929
1930 /* FIXME: check that next_tss_desc is tss */
1931
1932 if (reason != TASK_SWITCH_IRET) {
1933 if ((tss_selector & 3) > next_tss_desc.dpl ||
1934 ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001935 emulate_gp(ctxt, 0);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001936 return X86EMUL_PROPAGATE_FAULT;
1937 }
1938 }
1939
Gleb Natapovceffb452010-03-18 15:20:19 +02001940 desc_limit = desc_limit_scaled(&next_tss_desc);
1941 if (!next_tss_desc.p ||
1942 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
1943 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03001944 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001945 return X86EMUL_PROPAGATE_FAULT;
1946 }
1947
1948 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
1949 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
1950 write_segment_descriptor(ctxt, ops, old_tss_sel,
1951 &curr_tss_desc);
1952 }
1953
1954 if (reason == TASK_SWITCH_IRET)
1955 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
1956
1957 /* set back link to prev task only if NT bit is set in eflags
1958 note that old_tss_sel is not used afetr this point */
1959 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
1960 old_tss_sel = 0xffff;
1961
1962 if (next_tss_desc.type & 8)
1963 ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel,
1964 old_tss_base, &next_tss_desc);
1965 else
1966 ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel,
1967 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02001968 if (ret != X86EMUL_CONTINUE)
1969 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001970
1971 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
1972 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
1973
1974 if (reason != TASK_SWITCH_IRET) {
1975 next_tss_desc.type |= (1 << 1); /* set busy flag */
1976 write_segment_descriptor(ctxt, ops, tss_selector,
1977 &next_tss_desc);
1978 }
1979
1980 ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu);
1981 ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu);
1982 ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu);
1983
Jan Kiszkae269fb22010-04-14 15:51:09 +02001984 if (has_error_code) {
1985 struct decode_cache *c = &ctxt->decode;
1986
1987 c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
1988 c->lock_prefix = 0;
1989 c->src.val = (unsigned long) error_code;
Gleb Natapov79168fd2010-04-28 19:15:30 +03001990 emulate_push(ctxt, ops);
Jan Kiszkae269fb22010-04-14 15:51:09 +02001991 }
1992
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001993 return ret;
1994}
1995
1996int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +02001997 u16 tss_selector, int reason,
1998 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001999{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002000 struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002001 struct decode_cache *c = &ctxt->decode;
2002 int rc;
2003
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002004 c->eip = ctxt->eip;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002005 c->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002006
Jan Kiszkae269fb22010-04-14 15:51:09 +02002007 rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason,
2008 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002009
2010 if (rc == X86EMUL_CONTINUE) {
Jan Kiszkae269fb22010-04-14 15:51:09 +02002011 rc = writeback(ctxt, ops);
Gleb Natapov95c55882010-04-28 19:15:39 +03002012 if (rc == X86EMUL_CONTINUE)
2013 ctxt->eip = c->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002014 }
2015
Gleb Natapov19d04432010-04-15 12:29:50 +03002016 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002017}
2018
Gleb Natapova682e352010-03-18 15:20:21 +02002019static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base,
Gleb Natapovd9271122010-03-18 15:20:22 +02002020 int reg, struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002021{
2022 struct decode_cache *c = &ctxt->decode;
2023 int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
2024
Gleb Natapovd9271122010-03-18 15:20:22 +02002025 register_address_increment(c, &c->regs[reg], df * op->bytes);
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002026 op->addr.mem = register_address(c, base, c->regs[reg]);
Gleb Natapova682e352010-03-18 15:20:21 +02002027}
2028
Avi Kivity63540382010-07-29 15:11:55 +03002029static int em_push(struct x86_emulate_ctxt *ctxt)
2030{
2031 emulate_push(ctxt, ctxt->ops);
2032 return X86EMUL_CONTINUE;
2033}
2034
Avi Kivity73fba5f2010-07-29 15:11:53 +03002035#define D(_y) { .flags = (_y) }
2036#define N D(0)
2037#define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
2038#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
2039#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
2040
2041static struct opcode group1[] = {
2042 X7(D(Lock)), N
2043};
2044
2045static struct opcode group1A[] = {
2046 D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
2047};
2048
2049static struct opcode group3[] = {
2050 D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
2051 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2052 X4(D(Undefined)),
2053};
2054
2055static struct opcode group4[] = {
2056 D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
2057 N, N, N, N, N, N,
2058};
2059
2060static struct opcode group5[] = {
2061 D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
2062 D(SrcMem | ModRM | Stack), N,
2063 D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
2064 D(SrcMem | ModRM | Stack), N,
2065};
2066
2067static struct group_dual group7 = { {
2068 N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv),
2069 D(SrcNone | ModRM | DstMem | Mov), N,
2070 D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv),
2071}, {
2072 D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv),
2073 D(SrcNone | ModRM | DstMem | Mov), N,
2074 D(SrcMem16 | ModRM | Mov | Priv), N,
2075} };
2076
2077static struct opcode group8[] = {
2078 N, N, N, N,
2079 D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
2080 D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
2081};
2082
2083static struct group_dual group9 = { {
2084 N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
2085}, {
2086 N, N, N, N, N, N, N, N,
2087} };
2088
2089static struct opcode opcode_table[256] = {
2090 /* 0x00 - 0x07 */
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 /* 0x08 - 0x0F */
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), N,
2100 /* 0x10 - 0x17 */
2101 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2102 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2103 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2104 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2105 /* 0x18 - 0x1F */
2106 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2107 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2108 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2109 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2110 /* 0x20 - 0x27 */
2111 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2112 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2113 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2114 /* 0x28 - 0x2F */
2115 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2116 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2117 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2118 /* 0x30 - 0x37 */
2119 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2120 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2121 D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N,
2122 /* 0x38 - 0x3F */
2123 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2124 D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
2125 D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm),
2126 N, N,
2127 /* 0x40 - 0x4F */
2128 X16(D(DstReg)),
2129 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03002130 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002131 /* 0x58 - 0x5F */
2132 X8(D(DstReg | Stack)),
2133 /* 0x60 - 0x67 */
2134 D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
2135 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
2136 N, N, N, N,
2137 /* 0x68 - 0x6F */
Avi Kivity63540382010-07-29 15:11:55 +03002138 I(SrcImm | Mov | Stack, em_push), N,
2139 I(SrcImmByte | Mov | Stack, em_push), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03002140 D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */
2141 D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */
2142 /* 0x70 - 0x7F */
2143 X16(D(SrcImmByte)),
2144 /* 0x80 - 0x87 */
2145 G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
2146 G(DstMem | SrcImm | ModRM | Group, group1),
2147 G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
2148 G(DstMem | SrcImmByte | ModRM | Group, group1),
2149 D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM),
2150 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2151 /* 0x88 - 0x8F */
2152 D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov),
2153 D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov),
2154 D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg),
2155 D(ImplicitOps | SrcMem16 | ModRM), G(0, group1A),
2156 /* 0x90 - 0x97 */
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002157 X8(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002158 /* 0x98 - 0x9F */
2159 N, N, D(SrcImmFAddr | No64), N,
2160 D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N,
2161 /* 0xA0 - 0xA7 */
2162 D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs),
2163 D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs),
2164 D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String),
2165 D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String),
2166 /* 0xA8 - 0xAF */
2167 D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String),
2168 D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String),
2169 D(ByteOp | DstDI | String), D(DstDI | String),
2170 /* 0xB0 - 0xB7 */
2171 X8(D(ByteOp | DstReg | SrcImm | Mov)),
2172 /* 0xB8 - 0xBF */
2173 X8(D(DstReg | SrcImm | Mov)),
2174 /* 0xC0 - 0xC7 */
2175 D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM),
2176 N, D(ImplicitOps | Stack), N, N,
2177 D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov),
2178 /* 0xC8 - 0xCF */
2179 N, N, N, D(ImplicitOps | Stack),
2180 D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps),
2181 /* 0xD0 - 0xD7 */
2182 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2183 D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM),
2184 N, N, N, N,
2185 /* 0xD8 - 0xDF */
2186 N, N, N, N, N, N, N, N,
2187 /* 0xE0 - 0xE7 */
2188 N, N, N, N,
2189 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2190 D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc),
2191 /* 0xE8 - 0xEF */
2192 D(SrcImm | Stack), D(SrcImm | ImplicitOps),
2193 D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps),
2194 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2195 D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc),
2196 /* 0xF0 - 0xF7 */
2197 N, N, N, N,
2198 D(ImplicitOps | Priv), D(ImplicitOps), G(ByteOp, group3), G(0, group3),
2199 /* 0xF8 - 0xFF */
2200 D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps),
2201 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
2202};
2203
2204static struct opcode twobyte_table[256] = {
2205 /* 0x00 - 0x0F */
2206 N, GD(0, &group7), N, N,
2207 N, D(ImplicitOps), D(ImplicitOps | Priv), N,
2208 D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N,
2209 N, D(ImplicitOps | ModRM), N, N,
2210 /* 0x10 - 0x1F */
2211 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
2212 /* 0x20 - 0x2F */
Avi Kivityb27f3852010-08-01 14:25:22 +03002213 D(ModRM | DstMem | Priv | Op3264), D(ModRM | DstMem | Priv | Op3264),
2214 D(ModRM | SrcMem | Priv | Op3264), D(ModRM | SrcMem | Priv | Op3264),
Avi Kivity73fba5f2010-07-29 15:11:53 +03002215 N, N, N, N,
2216 N, N, N, N, N, N, N, N,
2217 /* 0x30 - 0x3F */
2218 D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N,
2219 D(ImplicitOps), D(ImplicitOps | Priv), N, N,
2220 N, N, N, N, N, N, N, N,
2221 /* 0x40 - 0x4F */
2222 X16(D(DstReg | SrcMem | ModRM | Mov)),
2223 /* 0x50 - 0x5F */
2224 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2225 /* 0x60 - 0x6F */
2226 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2227 /* 0x70 - 0x7F */
2228 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2229 /* 0x80 - 0x8F */
2230 X16(D(SrcImm)),
2231 /* 0x90 - 0x9F */
2232 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2233 /* 0xA0 - 0xA7 */
2234 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2235 N, D(DstMem | SrcReg | ModRM | BitOp),
2236 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2237 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
2238 /* 0xA8 - 0xAF */
2239 D(ImplicitOps | Stack), D(ImplicitOps | Stack),
2240 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2241 D(DstMem | SrcReg | Src2ImmByte | ModRM),
2242 D(DstMem | SrcReg | Src2CL | ModRM),
2243 D(ModRM), N,
2244 /* 0xB0 - 0xB7 */
2245 D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock),
2246 N, D(DstMem | SrcReg | ModRM | BitOp | Lock),
2247 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2248 D(DstReg | SrcMem16 | ModRM | Mov),
2249 /* 0xB8 - 0xBF */
2250 N, N,
2251 G(0, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
2252 N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov),
2253 D(DstReg | SrcMem16 | ModRM | Mov),
2254 /* 0xC0 - 0xCF */
2255 N, N, N, D(DstMem | SrcReg | ModRM | Mov),
2256 N, N, N, GD(0, &group9),
2257 N, N, N, N, N, N, N, N,
2258 /* 0xD0 - 0xDF */
2259 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2260 /* 0xE0 - 0xEF */
2261 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
2262 /* 0xF0 - 0xFF */
2263 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
2264};
2265
2266#undef D
2267#undef N
2268#undef G
2269#undef GD
2270#undef I
2271
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002272int
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002273x86_decode_insn(struct x86_emulate_ctxt *ctxt)
2274{
2275 struct x86_emulate_ops *ops = ctxt->ops;
2276 struct decode_cache *c = &ctxt->decode;
2277 int rc = X86EMUL_CONTINUE;
2278 int mode = ctxt->mode;
2279 int def_op_bytes, def_ad_bytes, dual, goffset;
2280 struct opcode opcode, *g_mod012, *g_mod3;
2281
2282 /* we cannot decode insn before we complete previous rep insn */
2283 WARN_ON(ctxt->restart);
2284
2285 c->eip = ctxt->eip;
2286 c->fetch.start = c->fetch.end = c->eip;
2287 ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS);
2288
2289 switch (mode) {
2290 case X86EMUL_MODE_REAL:
2291 case X86EMUL_MODE_VM86:
2292 case X86EMUL_MODE_PROT16:
2293 def_op_bytes = def_ad_bytes = 2;
2294 break;
2295 case X86EMUL_MODE_PROT32:
2296 def_op_bytes = def_ad_bytes = 4;
2297 break;
2298#ifdef CONFIG_X86_64
2299 case X86EMUL_MODE_PROT64:
2300 def_op_bytes = 4;
2301 def_ad_bytes = 8;
2302 break;
2303#endif
2304 default:
2305 return -1;
2306 }
2307
2308 c->op_bytes = def_op_bytes;
2309 c->ad_bytes = def_ad_bytes;
2310
2311 /* Legacy prefixes. */
2312 for (;;) {
2313 switch (c->b = insn_fetch(u8, 1, c->eip)) {
2314 case 0x66: /* operand-size override */
2315 /* switch between 2/4 bytes */
2316 c->op_bytes = def_op_bytes ^ 6;
2317 break;
2318 case 0x67: /* address-size override */
2319 if (mode == X86EMUL_MODE_PROT64)
2320 /* switch between 4/8 bytes */
2321 c->ad_bytes = def_ad_bytes ^ 12;
2322 else
2323 /* switch between 2/4 bytes */
2324 c->ad_bytes = def_ad_bytes ^ 6;
2325 break;
2326 case 0x26: /* ES override */
2327 case 0x2e: /* CS override */
2328 case 0x36: /* SS override */
2329 case 0x3e: /* DS override */
2330 set_seg_override(c, (c->b >> 3) & 3);
2331 break;
2332 case 0x64: /* FS override */
2333 case 0x65: /* GS override */
2334 set_seg_override(c, c->b & 7);
2335 break;
2336 case 0x40 ... 0x4f: /* REX */
2337 if (mode != X86EMUL_MODE_PROT64)
2338 goto done_prefixes;
2339 c->rex_prefix = c->b;
2340 continue;
2341 case 0xf0: /* LOCK */
2342 c->lock_prefix = 1;
2343 break;
2344 case 0xf2: /* REPNE/REPNZ */
2345 c->rep_prefix = REPNE_PREFIX;
2346 break;
2347 case 0xf3: /* REP/REPE/REPZ */
2348 c->rep_prefix = REPE_PREFIX;
2349 break;
2350 default:
2351 goto done_prefixes;
2352 }
2353
2354 /* Any legacy prefix after a REX prefix nullifies its effect. */
2355
2356 c->rex_prefix = 0;
2357 }
2358
2359done_prefixes:
2360
2361 /* REX prefix. */
Avi Kivity1e87e3e2010-08-01 14:42:51 +03002362 if (c->rex_prefix & 8)
2363 c->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002364
2365 /* Opcode byte(s). */
2366 opcode = opcode_table[c->b];
2367 if (opcode.flags == 0) {
2368 /* Two-byte opcode? */
2369 if (c->b == 0x0f) {
2370 c->twobyte = 1;
2371 c->b = insn_fetch(u8, 1, c->eip);
2372 opcode = twobyte_table[c->b];
2373 }
2374 }
2375 c->d = opcode.flags;
2376
2377 if (c->d & Group) {
2378 dual = c->d & GroupDual;
2379 c->modrm = insn_fetch(u8, 1, c->eip);
2380 --c->eip;
2381
2382 if (c->d & GroupDual) {
2383 g_mod012 = opcode.u.gdual->mod012;
2384 g_mod3 = opcode.u.gdual->mod3;
2385 } else
2386 g_mod012 = g_mod3 = opcode.u.group;
2387
2388 c->d &= ~(Group | GroupDual);
2389
2390 goffset = (c->modrm >> 3) & 7;
2391
2392 if ((c->modrm >> 6) == 3)
2393 opcode = g_mod3[goffset];
2394 else
2395 opcode = g_mod012[goffset];
2396 c->d |= opcode.flags;
2397 }
2398
2399 c->execute = opcode.u.execute;
2400
2401 /* Unrecognised? */
2402 if (c->d == 0 || (c->d & Undefined)) {
2403 DPRINTF("Cannot emulate %02x\n", c->b);
2404 return -1;
2405 }
2406
2407 if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
2408 c->op_bytes = 8;
2409
Avi Kivity7f9b4b72010-08-01 14:46:54 +03002410 if (c->d & Op3264) {
2411 if (mode == X86EMUL_MODE_PROT64)
2412 c->op_bytes = 8;
2413 else
2414 c->op_bytes = 4;
2415 }
2416
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002417 /* ModRM and SIB bytes. */
Avi Kivity09ee57c2010-08-01 12:07:29 +03002418 if (c->d & ModRM) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002419 rc = decode_modrm(ctxt, ops);
Avi Kivity09ee57c2010-08-01 12:07:29 +03002420 if (!c->has_seg_override)
2421 set_seg_override(c, c->modrm_seg);
2422 } else if (c->d & MemAbs)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002423 rc = decode_abs(ctxt, ops);
2424 if (rc != X86EMUL_CONTINUE)
2425 goto done;
2426
2427 if (!c->has_seg_override)
2428 set_seg_override(c, VCPU_SREG_DS);
2429
2430 if (!(!c->twobyte && c->b == 0x8d))
2431 c->modrm_ea += seg_override_base(ctxt, ops, c);
2432
2433 if (c->ad_bytes != 8)
2434 c->modrm_ea = (u32)c->modrm_ea;
2435
2436 if (c->rip_relative)
2437 c->modrm_ea += c->eip;
2438
2439 /*
2440 * Decode and fetch the source operand: register, memory
2441 * or immediate.
2442 */
2443 switch (c->d & SrcMask) {
2444 case SrcNone:
2445 break;
2446 case SrcReg:
2447 decode_register_operand(&c->src, c, 0);
2448 break;
2449 case SrcMem16:
2450 c->src.bytes = 2;
2451 goto srcmem_common;
2452 case SrcMem32:
2453 c->src.bytes = 4;
2454 goto srcmem_common;
2455 case SrcMem:
2456 c->src.bytes = (c->d & ByteOp) ? 1 :
2457 c->op_bytes;
2458 /* Don't fetch the address for invlpg: it could be unmapped. */
2459 if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7)
2460 break;
2461 srcmem_common:
2462 /*
2463 * For instructions with a ModR/M byte, switch to register
2464 * access if Mod = 3.
2465 */
2466 if ((c->d & ModRM) && c->modrm_mod == 3) {
2467 c->src.type = OP_REG;
2468 c->src.val = c->modrm_val;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002469 c->src.addr.reg = c->modrm_ptr;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002470 break;
2471 }
2472 c->src.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002473 c->src.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002474 c->src.val = 0;
2475 break;
2476 case SrcImm:
2477 case SrcImmU:
2478 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002479 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002480 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2481 if (c->src.bytes == 8)
2482 c->src.bytes = 4;
2483 /* NB. Immediates are sign-extended as necessary. */
2484 switch (c->src.bytes) {
2485 case 1:
2486 c->src.val = insn_fetch(s8, 1, c->eip);
2487 break;
2488 case 2:
2489 c->src.val = insn_fetch(s16, 2, c->eip);
2490 break;
2491 case 4:
2492 c->src.val = insn_fetch(s32, 4, c->eip);
2493 break;
2494 }
2495 if ((c->d & SrcMask) == SrcImmU) {
2496 switch (c->src.bytes) {
2497 case 1:
2498 c->src.val &= 0xff;
2499 break;
2500 case 2:
2501 c->src.val &= 0xffff;
2502 break;
2503 case 4:
2504 c->src.val &= 0xffffffff;
2505 break;
2506 }
2507 }
2508 break;
2509 case SrcImmByte:
2510 case SrcImmUByte:
2511 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002512 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002513 c->src.bytes = 1;
2514 if ((c->d & SrcMask) == SrcImmByte)
2515 c->src.val = insn_fetch(s8, 1, c->eip);
2516 else
2517 c->src.val = insn_fetch(u8, 1, c->eip);
2518 break;
2519 case SrcAcc:
2520 c->src.type = OP_REG;
2521 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002522 c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002523 fetch_register_operand(&c->src);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002524 break;
2525 case SrcOne:
2526 c->src.bytes = 1;
2527 c->src.val = 1;
2528 break;
2529 case SrcSI:
2530 c->src.type = OP_MEM;
2531 c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002532 c->src.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002533 register_address(c, seg_override_base(ctxt, ops, c),
2534 c->regs[VCPU_REGS_RSI]);
2535 c->src.val = 0;
2536 break;
2537 case SrcImmFAddr:
2538 c->src.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002539 c->src.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002540 c->src.bytes = c->op_bytes + 2;
2541 insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
2542 break;
2543 case SrcMemFAddr:
2544 c->src.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002545 c->src.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002546 c->src.bytes = c->op_bytes + 2;
2547 break;
2548 }
2549
2550 /*
2551 * Decode and fetch the second source operand: register, memory
2552 * or immediate.
2553 */
2554 switch (c->d & Src2Mask) {
2555 case Src2None:
2556 break;
2557 case Src2CL:
2558 c->src2.bytes = 1;
2559 c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
2560 break;
2561 case Src2ImmByte:
2562 c->src2.type = OP_IMM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002563 c->src2.addr.mem = c->eip;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002564 c->src2.bytes = 1;
2565 c->src2.val = insn_fetch(u8, 1, c->eip);
2566 break;
2567 case Src2One:
2568 c->src2.bytes = 1;
2569 c->src2.val = 1;
2570 break;
2571 }
2572
2573 /* Decode and fetch the destination operand: register or memory. */
2574 switch (c->d & DstMask) {
2575 case ImplicitOps:
2576 /* Special instructions do their own operand decoding. */
2577 return 0;
2578 case DstReg:
2579 decode_register_operand(&c->dst, c,
2580 c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
2581 break;
2582 case DstMem:
2583 case DstMem64:
2584 if ((c->d & ModRM) && c->modrm_mod == 3) {
2585 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2586 c->dst.type = OP_REG;
2587 c->dst.val = c->dst.orig_val = c->modrm_val;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002588 c->dst.addr.reg = c->modrm_ptr;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002589 break;
2590 }
2591 c->dst.type = OP_MEM;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002592 c->dst.addr.mem = c->modrm_ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002593 if ((c->d & DstMask) == DstMem64)
2594 c->dst.bytes = 8;
2595 else
2596 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
2597 c->dst.val = 0;
2598 if (c->d & BitOp) {
2599 unsigned long mask = ~(c->dst.bytes * 8 - 1);
2600
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002601 c->dst.addr.mem = c->dst.addr.mem +
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002602 (c->src.val & mask) / 8;
2603 }
2604 break;
2605 case DstAcc:
2606 c->dst.type = OP_REG;
2607 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002608 c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
Avi Kivity91ff3cb2010-08-01 12:53:09 +03002609 fetch_register_operand(&c->dst);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002610 c->dst.orig_val = c->dst.val;
2611 break;
2612 case DstDI:
2613 c->dst.type = OP_MEM;
2614 c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002615 c->dst.addr.mem =
Avi Kivitydde7e6d122010-07-29 15:11:52 +03002616 register_address(c, es_base(ctxt, ops),
2617 c->regs[VCPU_REGS_RDI]);
2618 c->dst.val = 0;
2619 break;
2620 }
2621
2622done:
2623 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
2624}
2625
2626int
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002627x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002628{
Avi Kivity9aabc88f2010-07-29 15:11:50 +03002629 struct x86_emulate_ops *ops = ctxt->ops;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002630 u64 msr_data;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002631 struct decode_cache *c = &ctxt->decode;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002632 int rc = X86EMUL_CONTINUE;
Gleb Natapov5cd21912010-03-18 15:20:26 +02002633 int saved_dst_type = c->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002634
Gleb Natapov9de41572010-04-28 19:15:22 +03002635 ctxt->decode.mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04002636
Gleb Natapov11616242010-02-11 14:43:14 +02002637 if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002638 emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02002639 goto done;
2640 }
2641
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002642 /* LOCK prefix is allowed only with some instructions */
Gleb Natapova41ffb752010-03-18 15:20:14 +02002643 if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002644 emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02002645 goto done;
2646 }
2647
Gleb Natapove92805a2010-02-10 14:21:35 +02002648 /* Privileged instruction can be executed only in CPL=0 */
Gleb Natapov9c537242010-03-18 15:20:05 +02002649 if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002650 emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02002651 goto done;
2652 }
2653
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002654 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002655 ctxt->restart = true;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002656 /* All REP prefixes have the same first termination condition */
Gleb Natapovc73e1972010-03-15 16:38:29 +02002657 if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
Gleb Natapov5cd21912010-03-18 15:20:26 +02002658 string_done:
2659 ctxt->restart = false;
Gleb Natapov95c55882010-04-28 19:15:39 +03002660 ctxt->eip = c->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002661 goto done;
2662 }
2663 /* The second termination condition only applies for REPE
2664 * and REPNE. Test if the repeat string operation prefix is
2665 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
2666 * corresponding termination condition according to:
2667 * - if REPE/REPZ and ZF = 0 then done
2668 * - if REPNE/REPNZ and ZF = 1 then done
2669 */
2670 if ((c->b == 0xa6) || (c->b == 0xa7) ||
Gleb Natapov5cd21912010-03-18 15:20:26 +02002671 (c->b == 0xae) || (c->b == 0xaf)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002672 if ((c->rep_prefix == REPE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002673 ((ctxt->eflags & EFLG_ZF) == 0))
2674 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002675 if ((c->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov5cd21912010-03-18 15:20:26 +02002676 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))
2677 goto string_done;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002678 }
Gleb Natapov063db062010-03-18 15:20:06 +02002679 c->eip = ctxt->eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02002680 }
2681
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002682 if (c->src.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002683 rc = read_emulated(ctxt, ops, c->src.addr.mem,
Gleb Natapov414e6272010-04-28 19:15:26 +03002684 c->src.valptr, c->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09002685 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002686 goto done;
Avi Kivity16518d52010-08-26 14:31:30 +03002687 c->src.orig_val64 = c->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002688 }
2689
Gleb Natapove35b7b92010-02-25 16:36:42 +02002690 if (c->src2.type == OP_MEM) {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002691 rc = read_emulated(ctxt, ops, c->src2.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002692 &c->src2.val, c->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02002693 if (rc != X86EMUL_CONTINUE)
2694 goto done;
2695 }
2696
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002697 if ((c->d & DstMask) == ImplicitOps)
2698 goto special_insn;
2699
2700
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002701 if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
2702 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002703 rc = read_emulated(ctxt, ops, c->dst.addr.mem,
Gleb Natapov9de41572010-04-28 19:15:22 +03002704 &c->dst.val, c->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02002705 if (rc != X86EMUL_CONTINUE)
2706 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08002707 }
Laurent Viviere4e03de2007-09-18 11:52:50 +02002708 c->dst.orig_val = c->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08002709
Avi Kivity018a98d2007-11-27 19:30:56 +02002710special_insn:
2711
Avi Kivityef65c882010-07-29 15:11:51 +03002712 if (c->execute) {
2713 rc = c->execute(ctxt);
2714 if (rc != X86EMUL_CONTINUE)
2715 goto done;
2716 goto writeback;
2717 }
2718
Laurent Viviere4e03de2007-09-18 11:52:50 +02002719 if (c->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 goto twobyte_insn;
2721
Laurent Viviere4e03de2007-09-18 11:52:50 +02002722 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 case 0x00 ... 0x05:
2724 add: /* add */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002725 emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002727 case 0x06: /* push es */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002728 emulate_push_sreg(ctxt, ops, VCPU_SREG_ES);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002729 break;
2730 case 0x07: /* pop es */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002731 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002732 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002733 goto done;
2734 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 case 0x08 ... 0x0d:
2736 or: /* or */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002737 emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002739 case 0x0e: /* push cs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002740 emulate_push_sreg(ctxt, ops, VCPU_SREG_CS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002741 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 case 0x10 ... 0x15:
2743 adc: /* adc */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002744 emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002746 case 0x16: /* push ss */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002747 emulate_push_sreg(ctxt, ops, VCPU_SREG_SS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002748 break;
2749 case 0x17: /* pop ss */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002750 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002751 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002752 goto done;
2753 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 case 0x18 ... 0x1d:
2755 sbb: /* sbb */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002756 emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002758 case 0x1e: /* push ds */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002759 emulate_push_sreg(ctxt, ops, VCPU_SREG_DS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002760 break;
2761 case 0x1f: /* pop ds */
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002762 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002763 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03002764 goto done;
2765 break;
Guillaume Thouveninaa3a8162008-09-12 13:52:18 +02002766 case 0x20 ... 0x25:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 and: /* and */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002768 emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 break;
2770 case 0x28 ... 0x2d:
2771 sub: /* sub */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002772 emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 break;
2774 case 0x30 ... 0x35:
2775 xor: /* xor */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002776 emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777 break;
2778 case 0x38 ... 0x3d:
2779 cmp: /* cmp */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002780 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002782 case 0x40 ... 0x47: /* inc r16/r32 */
2783 emulate_1op("inc", c->dst, ctxt->eflags);
2784 break;
2785 case 0x48 ... 0x4f: /* dec r16/r32 */
2786 emulate_1op("dec", c->dst, ctxt->eflags);
2787 break;
Avi Kivity33615aa2007-10-31 11:15:56 +02002788 case 0x58 ... 0x5f: /* pop reg */
2789 pop_instruction:
Avi Kivity350f69d2009-01-05 11:12:40 +02002790 rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002791 if (rc != X86EMUL_CONTINUE)
Avi Kivity33615aa2007-10-31 11:15:56 +02002792 goto done;
Avi Kivity33615aa2007-10-31 11:15:56 +02002793 break;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002794 case 0x60: /* pusha */
Wei Yongjunc37eda12010-06-15 09:03:33 +08002795 rc = emulate_pusha(ctxt, ops);
2796 if (rc != X86EMUL_CONTINUE)
2797 goto done;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002798 break;
2799 case 0x61: /* popa */
2800 rc = emulate_popa(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002801 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02002802 goto done;
2803 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02002805 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 goto cannot_emulate;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002807 c->dst.val = (s32) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002809 case 0x6c: /* insb */
2810 case 0x6d: /* insw/insd */
Gleb Natapov79729952010-03-18 15:20:24 +02002811 c->dst.bytes = min(c->dst.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002812 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002813 c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002814 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002815 goto done;
2816 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02002817 if (!pio_in_emulated(ctxt, ops, c->dst.bytes,
2818 c->regs[VCPU_REGS_RDX], &c->dst.val))
Gleb Natapov79729952010-03-18 15:20:24 +02002819 goto done; /* IO is needed, skip writeback */
2820 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002821 case 0x6e: /* outsb */
2822 case 0x6f: /* outsw/outsd */
Gleb Natapov79729952010-03-18 15:20:24 +02002823 c->src.bytes = min(c->src.bytes, 4u);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002824 if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX],
Gleb Natapov79729952010-03-18 15:20:24 +02002825 c->src.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002826 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002827 goto done;
2828 }
Gleb Natapov79729952010-03-18 15:20:24 +02002829 ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX],
2830 &c->src.val, 1, ctxt->vcpu);
2831
2832 c->dst.type = OP_NONE; /* nothing to writeback */
2833 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03002834 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity018a98d2007-11-27 19:30:56 +02002835 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03002836 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02002837 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 case 0x80 ... 0x83: /* Grp1 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002839 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 case 0:
2841 goto add;
2842 case 1:
2843 goto or;
2844 case 2:
2845 goto adc;
2846 case 3:
2847 goto sbb;
2848 case 4:
2849 goto and;
2850 case 5:
2851 goto sub;
2852 case 6:
2853 goto xor;
2854 case 7:
2855 goto cmp;
2856 }
2857 break;
2858 case 0x84 ... 0x85:
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002859 test:
Laurent Vivier05f086f2007-09-24 11:10:55 +02002860 emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 break;
2862 case 0x86 ... 0x87: /* xchg */
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002863 xchg:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 /* Write back the register source. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002865 switch (c->dst.bytes) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 case 1:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002867 *(u8 *) c->src.addr.reg = (u8) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 break;
2869 case 2:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002870 *(u16 *) c->src.addr.reg = (u16) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 break;
2872 case 4:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002873 *c->src.addr.reg = (u32) c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 break; /* 64b reg: zero-extend */
2875 case 8:
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002876 *c->src.addr.reg = c->dst.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 break;
2878 }
2879 /*
2880 * Write back the memory destination with implicit LOCK
2881 * prefix.
2882 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002883 c->dst.val = c->src.val;
2884 c->lock_prefix = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 case 0x88 ... 0x8b: /* mov */
Nitin A Kamble7de75242007-09-15 10:13:07 +03002887 goto mov;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002888 case 0x8c: /* mov r/m, sreg */
2889 if (c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002890 emulate_ud(ctxt);
Gleb Natapov5e3ae6c2010-03-18 15:20:07 +02002891 goto done;
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002892 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002893 c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu);
Guillaume Thouvenin38d5bc62008-05-27 15:13:28 +02002894 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002895 case 0x8d: /* lea r16/r32, m */
Avi Kivityf9b7aab2008-04-14 23:46:37 +03002896 c->dst.val = c->modrm_ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03002897 break;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002898 case 0x8e: { /* mov seg, r/m16 */
2899 uint16_t sel;
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002900
2901 sel = c->src.val;
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002902
Gleb Natapovc6975182010-02-18 12:15:01 +02002903 if (c->modrm_reg == VCPU_SREG_CS ||
2904 c->modrm_reg > VCPU_SREG_GS) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002905 emulate_ud(ctxt);
Gleb Natapov8b9f4412010-02-18 12:14:59 +02002906 goto done;
2907 }
2908
Glauber Costa310b5d32009-05-12 16:21:06 -04002909 if (c->modrm_reg == VCPU_SREG_SS)
Gleb Natapov95cb2292010-04-28 19:15:43 +03002910 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa310b5d32009-05-12 16:21:06 -04002911
Gleb Natapov2e873022010-03-18 15:20:18 +02002912 rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg);
Guillaume Thouvenin42571982008-05-27 14:49:15 +02002913
2914 c->dst.type = OP_NONE; /* Disable writeback. */
2915 break;
2916 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 case 0x8f: /* pop (sole member of Grp1a) */
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002918 rc = emulate_grp1a(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002919 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03002922 case 0x90 ... 0x97: /* nop / xchg reg, rax */
2923 if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
2924 goto done;
Mohammed Gamalb13354f2008-06-15 19:37:38 +03002925 goto xchg;
Nitin A Kamblefd2a7602007-08-28 18:22:47 -07002926 case 0x9c: /* pushf */
Laurent Vivier05f086f2007-09-24 11:10:55 +02002927 c->src.val = (unsigned long) ctxt->eflags;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002928 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002929 break;
Nitin A Kamble535eabc2007-09-15 10:45:05 +03002930 case 0x9d: /* popf */
Avi Kivity2b48cc72008-11-29 20:36:13 +02002931 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002932 c->dst.addr.reg = &ctxt->eflags;
Avi Kivity2b48cc72008-11-29 20:36:13 +02002933 c->dst.bytes = c->op_bytes;
Gleb Natapovd4c6a152010-02-10 14:21:34 +02002934 rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
2935 if (rc != X86EMUL_CONTINUE)
2936 goto done;
2937 break;
Wei Yongjun5d55f292010-07-07 17:43:35 +08002938 case 0xa0 ... 0xa3: /* mov */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939 case 0xa4 ... 0xa5: /* movs */
Gleb Natapova682e352010-03-18 15:20:21 +02002940 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 case 0xa6 ... 0xa7: /* cmps */
Guillaume Thouvenind7e51172007-11-26 13:49:09 +01002942 c->dst.type = OP_NONE; /* Disable writeback. */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002943 DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.addr.mem, c->dst.addr.mem);
Gleb Natapova682e352010-03-18 15:20:21 +02002944 goto cmp;
Mohammed Gamaldfb507c2010-05-11 22:22:40 +03002945 case 0xa8 ... 0xa9: /* test ax, imm */
2946 goto test;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947 case 0xaa ... 0xab: /* stos */
Laurent Viviere4e03de2007-09-18 11:52:50 +02002948 c->dst.val = c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002949 break;
2950 case 0xac ... 0xad: /* lods */
Gleb Natapova682e352010-03-18 15:20:21 +02002951 goto mov;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 case 0xae ... 0xaf: /* scas */
2953 DPRINTF("Urk! I don't handle SCAS.\n");
2954 goto cannot_emulate;
Mohammed Gamala5e2e822008-08-27 05:02:56 +03002955 case 0xb0 ... 0xbf: /* mov r, imm */
Guillaume Thouvenin615ac122008-05-27 10:19:16 +02002956 goto mov;
Avi Kivity018a98d2007-11-27 19:30:56 +02002957 case 0xc0 ... 0xc1:
2958 emulate_grp2(ctxt);
2959 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02002960 case 0xc3: /* ret */
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002961 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03002962 c->dst.addr.reg = &c->eip;
Avi Kivitycf5de4f2008-11-28 00:14:07 +02002963 c->dst.bytes = c->op_bytes;
Avi Kivity111de5d2007-11-27 19:14:21 +02002964 goto pop_instruction;
Avi Kivity018a98d2007-11-27 19:30:56 +02002965 case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */
2966 mov:
2967 c->dst.val = c->src.val;
2968 break;
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002969 case 0xcb: /* ret far */
2970 rc = emulate_ret_far(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002971 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002972 goto done;
2973 break;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002974 case 0xcf: /* iret */
2975 rc = emulate_iret(ctxt, ops);
2976
2977 if (rc != X86EMUL_CONTINUE)
2978 goto done;
2979 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02002980 case 0xd0 ... 0xd1: /* Grp2 */
2981 c->src.val = 1;
2982 emulate_grp2(ctxt);
2983 break;
2984 case 0xd2 ... 0xd3: /* Grp2 */
2985 c->src.val = c->regs[VCPU_REGS_RCX];
2986 emulate_grp2(ctxt);
2987 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002988 case 0xe4: /* inb */
2989 case 0xe5: /* in */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002990 goto do_io_in;
Mohammed Gamala6a30342008-09-06 17:22:29 +03002991 case 0xe6: /* outb */
2992 case 0xe7: /* out */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002993 goto do_io_out;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07002994 case 0xe8: /* call (near) */ {
Gleb Natapovd53c4772009-04-12 13:36:36 +03002995 long int rel = c->src.val;
Laurent Viviere4e03de2007-09-18 11:52:50 +02002996 c->src.val = (unsigned long) c->eip;
Harvey Harrison7a9572752008-02-19 07:40:41 -08002997 jmp_rel(c, rel);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002998 emulate_push(ctxt, ops);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002999 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003000 }
3001 case 0xe9: /* jmp rel */
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003002 goto jmp;
Gleb Natapov414e6272010-04-28 19:15:26 +03003003 case 0xea: { /* jmp far */
3004 unsigned short sel;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003005 jump_far:
Gleb Natapov414e6272010-04-28 19:15:26 +03003006 memcpy(&sel, c->src.valptr + c->op_bytes, 2);
3007
3008 if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
Gleb Natapovc6975182010-02-18 12:15:01 +02003009 goto done;
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003010
Gleb Natapov414e6272010-04-28 19:15:26 +03003011 c->eip = 0;
3012 memcpy(&c->eip, c->src.valptr, c->op_bytes);
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003013 break;
Gleb Natapov414e6272010-04-28 19:15:26 +03003014 }
Guillaume Thouvenin954cd362008-05-27 10:19:08 +02003015 case 0xeb:
3016 jmp: /* jmp rel short */
Harvey Harrison7a9572752008-02-19 07:40:41 -08003017 jmp_rel(c, c->src.val);
Laurent Viviera01af5e2007-09-24 11:10:56 +02003018 c->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07003019 break;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003020 case 0xec: /* in al,dx */
3021 case 0xed: /* in (e/r)ax,dx */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003022 c->src.val = c->regs[VCPU_REGS_RDX];
3023 do_io_in:
3024 c->dst.bytes = min(c->dst.bytes, 4u);
3025 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003026 emulate_gp(ctxt, 0);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003027 goto done;
3028 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02003029 if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val,
3030 &c->dst.val))
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003031 goto done; /* IO is needed */
3032 break;
Wei Yongjunce7a0ad2010-07-06 16:50:21 +08003033 case 0xee: /* out dx,al */
3034 case 0xef: /* out dx,(e/r)ax */
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003035 c->src.val = c->regs[VCPU_REGS_RDX];
3036 do_io_out:
3037 c->dst.bytes = min(c->dst.bytes, 4u);
3038 if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003039 emulate_gp(ctxt, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003040 goto done;
Mohammed Gamala6a30342008-09-06 17:22:29 +03003041 }
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003042 ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1,
3043 ctxt->vcpu);
3044 c->dst.type = OP_NONE; /* Disable writeback. */
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003045 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003046 case 0xf4: /* hlt */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003047 ctxt->vcpu->arch.halt_request = 1;
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03003048 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003049 case 0xf5: /* cmc */
3050 /* complement carry flag from eflags reg */
3051 ctxt->eflags ^= EFLG_CF;
3052 c->dst.type = OP_NONE; /* Disable writeback. */
3053 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003054 case 0xf6 ... 0xf7: /* Grp3 */
Gleb Natapovaca06a82010-03-18 15:20:15 +02003055 if (!emulate_grp3(ctxt, ops))
3056 goto cannot_emulate;
Avi Kivity018a98d2007-11-27 19:30:56 +02003057 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02003058 case 0xf8: /* clc */
3059 ctxt->eflags &= ~EFLG_CF;
3060 c->dst.type = OP_NONE; /* Disable writeback. */
3061 break;
3062 case 0xfa: /* cli */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003063 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003064 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003065 goto done;
3066 } else {
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003067 ctxt->eflags &= ~X86_EFLAGS_IF;
3068 c->dst.type = OP_NONE; /* Disable writeback. */
3069 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003070 break;
3071 case 0xfb: /* sti */
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003072 if (emulator_bad_iopl(ctxt, ops)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003073 emulate_gp(ctxt, 0);
Wei Yongjun07cbc6c2010-07-06 16:54:19 +08003074 goto done;
3075 } else {
Gleb Natapov95cb2292010-04-28 19:15:43 +03003076 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003077 ctxt->eflags |= X86_EFLAGS_IF;
3078 c->dst.type = OP_NONE; /* Disable writeback. */
3079 }
Avi Kivity111de5d2007-11-27 19:14:21 +02003080 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03003081 case 0xfc: /* cld */
3082 ctxt->eflags &= ~EFLG_DF;
3083 c->dst.type = OP_NONE; /* Disable writeback. */
3084 break;
3085 case 0xfd: /* std */
3086 ctxt->eflags |= EFLG_DF;
3087 c->dst.type = OP_NONE; /* Disable writeback. */
3088 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003089 case 0xfe: /* Grp4 */
3090 grp45:
Avi Kivity018a98d2007-11-27 19:30:56 +02003091 rc = emulate_grp45(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003092 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003093 goto done;
3094 break;
Gleb Natapovea79849d2010-02-25 16:36:43 +02003095 case 0xff: /* Grp5 */
3096 if (c->modrm_reg == 5)
3097 goto jump_far;
3098 goto grp45;
Avi Kivity91269b82010-07-25 14:51:16 +03003099 default:
3100 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003102
3103writeback:
3104 rc = writeback(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003105 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02003106 goto done;
3107
Gleb Natapov5cd21912010-03-18 15:20:26 +02003108 /*
3109 * restore dst type in case the decoding will be reused
3110 * (happens for string instruction )
3111 */
3112 c->dst.type = saved_dst_type;
3113
Gleb Natapova682e352010-03-18 15:20:21 +02003114 if ((c->d & SrcMask) == SrcSI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003115 string_addr_inc(ctxt, seg_override_base(ctxt, ops, c),
3116 VCPU_REGS_RSI, &c->src);
Gleb Natapova682e352010-03-18 15:20:21 +02003117
3118 if ((c->d & DstMask) == DstDI)
Gleb Natapov79168fd2010-04-28 19:15:30 +03003119 string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI,
3120 &c->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02003121
Gleb Natapov5cd21912010-03-18 15:20:26 +02003122 if (c->rep_prefix && (c->d & String)) {
Gleb Natapov7b262e92010-03-18 15:20:27 +02003123 struct read_cache *rc = &ctxt->decode.io_read;
Gleb Natapovd9271122010-03-18 15:20:22 +02003124 register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
Gleb Natapov7b262e92010-03-18 15:20:27 +02003125 /*
3126 * Re-enter guest when pio read ahead buffer is empty or,
3127 * if it is not used, after each 1024 iteration.
3128 */
3129 if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) ||
3130 (rc->end != 0 && rc->end == rc->pos))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003131 ctxt->restart = false;
3132 }
Gleb Natapov9de41572010-04-28 19:15:22 +03003133 /*
3134 * reset read cache here in case string instruction is restared
3135 * without decoding
3136 */
3137 ctxt->decode.mem_read.end = 0;
Gleb Natapov95c55882010-04-28 19:15:39 +03003138 ctxt->eip = c->eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02003139
3140done:
Gleb Natapovcb404fe2010-03-18 15:20:25 +02003141 return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142
3143twobyte_insn:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003144 switch (c->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 case 0x01: /* lgdt, lidt, lmsw */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003146 switch (c->modrm_reg) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147 u16 size;
3148 unsigned long address;
3149
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003150 case 0: /* vmcall */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003151 if (c->modrm_mod != 3 || c->modrm_rm != 1)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003152 goto cannot_emulate;
3153
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003154 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003155 if (rc != X86EMUL_CONTINUE)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003156 goto done;
3157
Avi Kivity33e38852008-05-21 15:34:25 +03003158 /* Let the processor re-execute the fixed hypercall */
Gleb Natapov063db062010-03-18 15:20:06 +02003159 c->eip = ctxt->eip;
Avi Kivity16286d02008-04-14 14:40:50 +03003160 /* Disable writeback. */
3161 c->dst.type = OP_NONE;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003162 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 case 2: /* lgdt */
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003164 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003165 &size, &address, c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003166 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 goto done;
3168 realmode_lgdt(ctxt->vcpu, size, address);
Avi Kivity16286d02008-04-14 14:40:50 +03003169 /* Disable writeback. */
3170 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 break;
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003172 case 3: /* lidt/vmmcall */
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003173 if (c->modrm_mod == 3) {
3174 switch (c->modrm_rm) {
3175 case 1:
3176 rc = kvm_fix_hypercall(ctxt->vcpu);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003177 if (rc != X86EMUL_CONTINUE)
Avi Kivity2b3d2a22008-12-23 19:46:01 +02003178 goto done;
3179 break;
3180 default:
3181 goto cannot_emulate;
3182 }
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003183 } else {
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003184 rc = read_descriptor(ctxt, ops, c->src.addr.mem,
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003185 &size, &address,
Laurent Viviere4e03de2007-09-18 11:52:50 +02003186 c->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003187 if (rc != X86EMUL_CONTINUE)
Anthony Liguoriaca7f962007-09-17 14:57:49 -05003188 goto done;
3189 realmode_lidt(ctxt->vcpu, size, address);
3190 }
Avi Kivity16286d02008-04-14 14:40:50 +03003191 /* Disable writeback. */
3192 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 break;
3194 case 4: /* smsw */
Avi Kivity16286d02008-04-14 14:40:50 +03003195 c->dst.bytes = 2;
Gleb Natapov52a46612010-03-18 15:20:03 +02003196 c->dst.val = ops->get_cr(0, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197 break;
3198 case 6: /* lmsw */
Avi Kivity9928ff62010-08-01 18:35:24 +03003199 ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0eul) |
Gleb Natapov93a152b2010-03-18 15:20:04 +02003200 (c->src.val & 0x0f), ctxt->vcpu);
Avi Kivitydc7457e2008-04-30 16:13:36 +03003201 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 break;
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003203 case 5: /* not defined */
Gleb Natapov54b84862010-04-28 19:15:44 +03003204 emulate_ud(ctxt);
Gleb Natapov6e1e5ff2010-03-18 15:20:08 +02003205 goto done;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 case 7: /* invlpg*/
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003207 emulate_invlpg(ctxt->vcpu, c->modrm_ea);
Avi Kivity16286d02008-04-14 14:40:50 +03003208 /* Disable writeback. */
3209 c->dst.type = OP_NONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 break;
3211 default:
3212 goto cannot_emulate;
3213 }
3214 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003215 case 0x05: /* syscall */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003216 rc = emulate_syscall(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003217 if (rc != X86EMUL_CONTINUE)
3218 goto done;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02003219 else
3220 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003221 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003222 case 0x06:
3223 emulate_clts(ctxt->vcpu);
3224 c->dst.type = OP_NONE;
3225 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003226 case 0x09: /* wbinvd */
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003227 kvm_emulate_wbinvd(ctxt->vcpu);
3228 c->dst.type = OP_NONE;
3229 break;
3230 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02003231 case 0x0d: /* GrpP (prefetch) */
3232 case 0x18: /* Grp16 (prefetch/nop) */
3233 c->dst.type = OP_NONE;
3234 break;
3235 case 0x20: /* mov cr, reg */
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003236 switch (c->modrm_reg) {
3237 case 1:
3238 case 5 ... 7:
3239 case 9 ... 15:
Gleb Natapov54b84862010-04-28 19:15:44 +03003240 emulate_ud(ctxt);
Gleb Natapov6aebfa62010-03-18 15:20:10 +02003241 goto done;
3242 }
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003243 c->dst.val = ops->get_cr(c->modrm_reg, ctxt->vcpu);
Avi Kivity018a98d2007-11-27 19:30:56 +02003244 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 case 0x21: /* mov from dr to reg */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003246 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3247 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003248 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003249 goto done;
3250 }
Avi Kivityb27f3852010-08-01 14:25:22 +03003251 ops->get_dr(c->modrm_reg, &c->dst.val, ctxt->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003253 case 0x22: /* mov reg, cr */
Avi Kivity1a0c7d42010-08-01 14:25:22 +03003254 if (ops->set_cr(c->modrm_reg, c->src.val, ctxt->vcpu)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003255 emulate_gp(ctxt, 0);
Gleb Natapov0f122442010-04-28 19:15:31 +03003256 goto done;
3257 }
Avi Kivity018a98d2007-11-27 19:30:56 +02003258 c->dst.type = OP_NONE;
3259 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260 case 0x23: /* mov from reg to dr */
Gleb Natapov1e470be2010-03-18 15:20:11 +02003261 if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) &&
3262 (c->modrm_reg == 4 || c->modrm_reg == 5)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003263 emulate_ud(ctxt);
Gleb Natapov1e470be2010-03-18 15:20:11 +02003264 goto done;
3265 }
Gleb Natapov35aa5372010-04-28 19:15:27 +03003266
Avi Kivityb27f3852010-08-01 14:25:22 +03003267 if (ops->set_dr(c->modrm_reg, c->src.val &
Gleb Natapov338dbc92010-04-28 19:15:32 +03003268 ((ctxt->mode == X86EMUL_MODE_PROT64) ?
3269 ~0ULL : ~0U), ctxt->vcpu) < 0) {
3270 /* #UD condition is already handled by the code above */
Gleb Natapov54b84862010-04-28 19:15:44 +03003271 emulate_gp(ctxt, 0);
Gleb Natapov338dbc92010-04-28 19:15:32 +03003272 goto done;
3273 }
3274
Laurent Viviera01af5e2007-09-24 11:10:56 +02003275 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02003277 case 0x30:
3278 /* wrmsr */
3279 msr_data = (u32)c->regs[VCPU_REGS_RAX]
3280 | ((u64)c->regs[VCPU_REGS_RDX] << 32);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003281 if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003282 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003283 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003284 }
3285 rc = X86EMUL_CONTINUE;
3286 c->dst.type = OP_NONE;
3287 break;
3288 case 0x32:
3289 /* rdmsr */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003290 if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03003291 emulate_gp(ctxt, 0);
Gleb Natapovfd525362010-03-18 15:20:13 +02003292 goto done;
Avi Kivity018a98d2007-11-27 19:30:56 +02003293 } else {
3294 c->regs[VCPU_REGS_RAX] = (u32)msr_data;
3295 c->regs[VCPU_REGS_RDX] = msr_data >> 32;
3296 }
3297 rc = X86EMUL_CONTINUE;
3298 c->dst.type = OP_NONE;
3299 break;
Andre Przywarae99f0502009-06-17 15:50:33 +02003300 case 0x34: /* sysenter */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003301 rc = emulate_sysenter(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003302 if (rc != X86EMUL_CONTINUE)
3303 goto done;
Andre Przywara8c604352009-06-18 12:56:01 +02003304 else
3305 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003306 break;
3307 case 0x35: /* sysexit */
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003308 rc = emulate_sysexit(ctxt, ops);
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02003309 if (rc != X86EMUL_CONTINUE)
3310 goto done;
Andre Przywara4668f052009-06-18 12:56:02 +02003311 else
3312 goto writeback;
Andre Przywarae99f0502009-06-17 15:50:33 +02003313 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 case 0x40 ... 0x4f: /* cmov */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003315 c->dst.val = c->dst.orig_val = c->src.val;
Laurent Viviera01af5e2007-09-24 11:10:56 +02003316 if (!test_cc(c->b, ctxt->eflags))
3317 c->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003318 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03003319 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity018a98d2007-11-27 19:30:56 +02003320 if (test_cc(c->b, ctxt->eflags))
Gleb Natapovb2833e32009-04-12 13:36:30 +03003321 jmp_rel(c, c->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02003322 c->dst.type = OP_NONE;
3323 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003324 case 0xa0: /* push fs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003325 emulate_push_sreg(ctxt, ops, VCPU_SREG_FS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003326 break;
3327 case 0xa1: /* pop fs */
3328 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003329 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003330 goto done;
3331 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003332 case 0xa3:
3333 bt: /* bt */
Qing Hee4f8e032007-09-24 17:22:13 +08003334 c->dst.type = OP_NONE;
Laurent Viviere4e03de2007-09-18 11:52:50 +02003335 /* only subword offset */
3336 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003337 emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003338 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003339 case 0xa4: /* shld imm8, r, r/m */
3340 case 0xa5: /* shld cl, r, r/m */
3341 emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
3342 break;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003343 case 0xa8: /* push gs */
Gleb Natapov79168fd2010-04-28 19:15:30 +03003344 emulate_push_sreg(ctxt, ops, VCPU_SREG_GS);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003345 break;
3346 case 0xa9: /* pop gs */
3347 rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003348 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03003349 goto done;
3350 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003351 case 0xab:
3352 bts: /* bts */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003353 /* only subword offset */
3354 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003355 emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003356 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01003357 case 0xac: /* shrd imm8, r, r/m */
3358 case 0xad: /* shrd cl, r, r/m */
3359 emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
3360 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03003361 case 0xae: /* clflush */
3362 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 case 0xb0 ... 0xb1: /* cmpxchg */
3364 /*
3365 * Save real source value, then compare EAX against
3366 * destination.
3367 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003368 c->src.orig_val = c->src.val;
3369 c->src.val = c->regs[VCPU_REGS_RAX];
Laurent Vivier05f086f2007-09-24 11:10:55 +02003370 emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
3371 if (ctxt->eflags & EFLG_ZF) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 /* Success: write back to memory. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003373 c->dst.val = c->src.orig_val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374 } else {
3375 /* Failure: write the value we saw to EAX. */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003376 c->dst.type = OP_REG;
Avi Kivity1a6440aef2010-08-01 12:35:10 +03003377 c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003378 }
3379 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 case 0xb3:
3381 btr: /* btr */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003382 /* only subword offset */
3383 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003384 emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386 case 0xb6 ... 0xb7: /* movzx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003387 c->dst.bytes = c->op_bytes;
3388 c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
3389 : (u16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003391 case 0xba: /* Grp8 */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003392 switch (c->modrm_reg & 3) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393 case 0:
3394 goto bt;
3395 case 1:
3396 goto bts;
3397 case 2:
3398 goto btr;
3399 case 3:
3400 goto btc;
3401 }
3402 break;
Nitin A Kamble7de75242007-09-15 10:13:07 +03003403 case 0xbb:
3404 btc: /* btc */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003405 /* only subword offset */
3406 c->src.val &= (c->dst.bytes << 3) - 1;
Laurent Vivier05f086f2007-09-24 11:10:55 +02003407 emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
Nitin A Kamble7de75242007-09-15 10:13:07 +03003408 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409 case 0xbe ... 0xbf: /* movsx */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003410 c->dst.bytes = c->op_bytes;
3411 c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
3412 (s16) c->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413 break;
Sheng Yanga012e652007-10-15 14:24:20 +08003414 case 0xc3: /* movnti */
Laurent Viviere4e03de2007-09-18 11:52:50 +02003415 c->dst.bytes = c->op_bytes;
3416 c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
3417 (u64) c->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08003418 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419 case 0xc7: /* Grp9 (cmpxchg8b) */
Gleb Natapov69f55cb2010-03-18 15:20:20 +02003420 rc = emulate_grp9(ctxt, ops);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09003421 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02003422 goto done;
3423 break;
Avi Kivity91269b82010-07-25 14:51:16 +03003424 default:
3425 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 }
3427 goto writeback;
3428
3429cannot_emulate:
Laurent Viviere4e03de2007-09-18 11:52:50 +02003430 DPRINTF("Cannot emulate %02x\n", c->b);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 return -1;
3432}