blob: dd71567d7c716b43b5e99eacf395106d92060d79 [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
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
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
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivity3eeb3282010-01-21 15:31:48 +020028#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020029#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031/*
Avi Kivitya99455492011-09-13 10:45:41 +030032 * Operand types
33 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030034#define OpNone 0ull
35#define OpImplicit 1ull /* No generic decode */
36#define OpReg 2ull /* Register */
37#define OpMem 3ull /* Memory */
38#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
39#define OpDI 5ull /* ES:DI/EDI/RDI */
40#define OpMem64 6ull /* Memory, 64-bit */
41#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
42#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030043#define OpCL 9ull /* CL register (for shifts) */
44#define OpImmByte 10ull /* 8-bit sign extended immediate */
45#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020046#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030047#define OpMem16 13ull /* Memory operand (16-bit). */
48#define OpMem32 14ull /* Memory operand (32-bit). */
49#define OpImmU 15ull /* Immediate operand, zero extended */
50#define OpSI 16ull /* SI/ESI/RSI */
51#define OpImmFAddr 17ull /* Immediate far address */
52#define OpMemFAddr 18ull /* Far address in memory */
53#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030054#define OpES 20ull /* ES */
55#define OpCS 21ull /* CS */
56#define OpSS 22ull /* SS */
57#define OpDS 23ull /* DS */
58#define OpFS 24ull /* FS */
59#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020060#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020061#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Avi Kivitya99455492011-09-13 10:45:41 +030062
Avi Kivity0fe59122011-09-13 10:45:47 +030063#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030064#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030065
66/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080067 * Opcode effective-address decode tables.
68 * Note that we only emulate instructions that have at least one memory
69 * operand (excluding implicit stack references). We assume that stack
70 * references and instruction fetches will never occur in special memory
71 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
72 * not be handled.
73 */
74
75/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030076#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080077/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030078#define DstShift 1
79#define ImplicitOps (OpImplicit << DstShift)
80#define DstReg (OpReg << DstShift)
81#define DstMem (OpMem << DstShift)
82#define DstAcc (OpAcc << DstShift)
83#define DstDI (OpDI << DstShift)
84#define DstMem64 (OpMem64 << DstShift)
85#define DstImmUByte (OpImmUByte << DstShift)
86#define DstDX (OpDX << DstShift)
87#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080088/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030089#define SrcShift 6
90#define SrcNone (OpNone << SrcShift)
91#define SrcReg (OpReg << SrcShift)
92#define SrcMem (OpMem << SrcShift)
93#define SrcMem16 (OpMem16 << SrcShift)
94#define SrcMem32 (OpMem32 << SrcShift)
95#define SrcImm (OpImm << SrcShift)
96#define SrcImmByte (OpImmByte << SrcShift)
97#define SrcOne (OpOne << SrcShift)
98#define SrcImmUByte (OpImmUByte << SrcShift)
99#define SrcImmU (OpImmU << SrcShift)
100#define SrcSI (OpSI << SrcShift)
101#define SrcImmFAddr (OpImmFAddr << SrcShift)
102#define SrcMemFAddr (OpMemFAddr << SrcShift)
103#define SrcAcc (OpAcc << SrcShift)
104#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200105#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300106#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200107#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300108#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300109#define BitOp (1<<11)
110#define MemAbs (1<<12) /* Memory operand is absolute displacement */
111#define String (1<<13) /* String instruction (rep capable) */
112#define Stack (1<<14) /* Stack instruction (push/pop) */
113#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
114#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
115#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
116#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
117#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200118#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300119#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300120/* Generic ModRM decode. */
121#define ModRM (1<<19)
122/* Destination is only written; never read. */
123#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300124/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200125#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200126#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300127#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300128#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300129#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200130#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200131#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300132#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800133#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100134/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800135#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300136#define Src2None (OpNone << Src2Shift)
137#define Src2CL (OpCL << Src2Shift)
138#define Src2ImmByte (OpImmByte << Src2Shift)
139#define Src2One (OpOne << Src2Shift)
140#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300141#define Src2ES (OpES << Src2Shift)
142#define Src2CS (OpCS << Src2Shift)
143#define Src2SS (OpSS << Src2Shift)
144#define Src2DS (OpDS << Src2Shift)
145#define Src2FS (OpFS << Src2Shift)
146#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300147#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300148#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300149#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
150#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
151#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200152#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153
Avi Kivityd0e53322010-07-29 15:11:54 +0300154#define X2(x...) x, x
155#define X3(x...) X2(x), x
156#define X4(x...) X2(x), X2(x)
157#define X5(x...) X4(x), x
158#define X6(x...) X4(x), X2(x)
159#define X7(x...) X4(x), X3(x)
160#define X8(x...) X4(x), X4(x)
161#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300162
Avi Kivitye28bbd42013-01-04 16:18:48 +0200163#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
164#define FASTOP_SIZE 8
165
166/*
167 * fastop functions have a special calling convention:
168 *
169 * dst: [rdx]:rax (in/out)
170 * src: rbx (in/out)
171 * src2: rcx (in)
172 * flags: rflags (in/out)
173 *
174 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
175 * different operand sizes can be reached by calculation, rather than a jump
176 * table (which would be bigger than the code).
177 *
178 * fastop functions are declared as taking a never-defined fastop parameter,
179 * so they can't be called from C directly.
180 */
181
182struct fastop;
183
Avi Kivityd65b1de2010-07-29 15:11:35 +0300184struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300185 u64 flags : 56;
186 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300187 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300188 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200189 const struct opcode *group;
190 const struct group_dual *gdual;
191 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200192 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200193 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300194 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200195 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300196};
197
198struct group_dual {
199 struct opcode mod012[8];
200 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300201};
202
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200203struct gprefix {
204 struct opcode pfx_no;
205 struct opcode pfx_66;
206 struct opcode pfx_f2;
207 struct opcode pfx_f3;
208};
209
Gleb Natapov045a2822012-12-20 16:57:43 +0200210struct escape {
211 struct opcode op[8];
212 struct opcode high[64];
213};
214
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200216#define EFLG_ID (1<<21)
217#define EFLG_VIP (1<<20)
218#define EFLG_VIF (1<<19)
219#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200220#define EFLG_VM (1<<17)
221#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200222#define EFLG_IOPL (3<<12)
223#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224#define EFLG_OF (1<<11)
225#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200226#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200227#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228#define EFLG_SF (1<<7)
229#define EFLG_ZF (1<<6)
230#define EFLG_AF (1<<4)
231#define EFLG_PF (1<<2)
232#define EFLG_CF (1<<0)
233
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300234#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
235#define EFLG_RESERVED_ONE_MASK 2
236
Avi Kivitydd856ef2012-08-27 23:46:17 +0300237static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
238{
239 if (!(ctxt->regs_valid & (1 << nr))) {
240 ctxt->regs_valid |= 1 << nr;
241 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
242 }
243 return ctxt->_regs[nr];
244}
245
246static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
247{
248 ctxt->regs_valid |= 1 << nr;
249 ctxt->regs_dirty |= 1 << nr;
250 return &ctxt->_regs[nr];
251}
252
253static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
254{
255 reg_read(ctxt, nr);
256 return reg_write(ctxt, nr);
257}
258
259static void writeback_registers(struct x86_emulate_ctxt *ctxt)
260{
261 unsigned reg;
262
263 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
264 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
265}
266
267static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
268{
269 ctxt->regs_dirty = 0;
270 ctxt->regs_valid = 0;
271}
272
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273/*
274 * Instruction emulation:
275 * Most instructions are emulated directly via a fragment of inline assembly
276 * code. This allows us to save/restore EFLAGS and thus very easily pick up
277 * any modified flags.
278 */
279
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800280#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281#define _LO32 "k" /* force 32-bit operand */
282#define _STK "%%rsp" /* stack pointer */
283#elif defined(__i386__)
284#define _LO32 "" /* force 32-bit operand */
285#define _STK "%%esp" /* stack pointer */
286#endif
287
288/*
289 * These EFLAGS bits are restored from saved value during emulation, and
290 * any changes are written back to the saved value after emulation.
291 */
292#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
293
294/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200295#define _PRE_EFLAGS(_sav, _msk, _tmp) \
296 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
297 "movl %"_sav",%"_LO32 _tmp"; " \
298 "push %"_tmp"; " \
299 "push %"_tmp"; " \
300 "movl %"_msk",%"_LO32 _tmp"; " \
301 "andl %"_LO32 _tmp",("_STK"); " \
302 "pushf; " \
303 "notl %"_LO32 _tmp"; " \
304 "andl %"_LO32 _tmp",("_STK"); " \
305 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
306 "pop %"_tmp"; " \
307 "orl %"_LO32 _tmp",("_STK"); " \
308 "popf; " \
309 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310
311/* After executing instruction: write-back necessary bits in EFLAGS. */
312#define _POST_EFLAGS(_sav, _msk, _tmp) \
313 /* _sav |= EFLAGS & _msk; */ \
314 "pushf; " \
315 "pop %"_tmp"; " \
316 "andl %"_msk",%"_LO32 _tmp"; " \
317 "orl %"_LO32 _tmp",%"_sav"; "
318
Avi Kivitydda96d82008-11-26 15:14:10 +0200319#ifdef CONFIG_X86_64
320#define ON64(x) x
321#else
322#define ON64(x)
323#endif
324
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300325#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200326 do { \
327 __asm__ __volatile__ ( \
328 _PRE_EFLAGS("0", "4", "2") \
329 _op _suffix " %"_x"3,%1; " \
330 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300331 : "=m" ((ctxt)->eflags), \
332 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200333 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300334 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200335 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200336
337
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300339#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200340 do { \
341 unsigned long _tmp; \
342 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300343 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200344 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300345 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200346 break; \
347 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300348 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200349 break; \
350 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300351 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200352 break; \
353 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354 } while (0)
355
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300356#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200358 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300359 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300361 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 break; \
363 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300364 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365 _wx, _wy, _lx, _ly, _qx, _qy); \
366 break; \
367 } \
368 } while (0)
369
370/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300371#define emulate_2op_SrcB(ctxt, _op) \
372 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373
374/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300375#define emulate_2op_SrcV(ctxt, _op) \
376 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377
378/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300379#define emulate_2op_SrcV_nobyte(ctxt, _op) \
380 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100382/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300383#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300384 do { \
385 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300386 _type _clv = (ctxt)->src2.val; \
387 _type _srcv = (ctxt)->src.val; \
388 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300389 \
390 __asm__ __volatile__ ( \
391 _PRE_EFLAGS("0", "5", "2") \
392 _op _suffix " %4,%1 \n" \
393 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300394 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300395 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
396 ); \
397 \
Avi Kivity761441b2011-09-07 16:41:36 +0300398 (ctxt)->src2.val = (unsigned long) _clv; \
399 (ctxt)->src2.val = (unsigned long) _srcv; \
400 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100401 } while (0)
402
Avi Kivity761441b2011-09-07 16:41:36 +0300403#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300404 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300405 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300406 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300407 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300408 break; \
409 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300410 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300411 break; \
412 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300413 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300414 break; \
415 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100416 } while (0)
417
Avi Kivityd1eef452011-09-07 16:41:38 +0300418#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 do { \
420 unsigned long _tmp; \
421 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200422 __asm__ __volatile__ ( \
423 _PRE_EFLAGS("0", "3", "2") \
424 _op _suffix " %1; " \
425 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300426 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200427 "=&r" (_tmp) \
428 : "i" (EFLAGS_MASK)); \
429 } while (0)
430
431/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300432#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200433 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300434 switch ((ctxt)->dst.bytes) { \
435 case 1: __emulate_1op(ctxt, _op, "b"); break; \
436 case 2: __emulate_1op(ctxt, _op, "w"); break; \
437 case 4: __emulate_1op(ctxt, _op, "l"); break; \
438 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 } \
440 } while (0)
441
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300442#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300443 do { \
444 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300445 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
446 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300447 \
448 __asm__ __volatile__ ( \
449 _PRE_EFLAGS("0", "5", "1") \
450 "1: \n\t" \
451 _op _suffix " %6; " \
452 "2: \n\t" \
453 _POST_EFLAGS("0", "5", "1") \
454 ".pushsection .fixup,\"ax\" \n\t" \
455 "3: movb $1, %4 \n\t" \
456 "jmp 2b \n\t" \
457 ".popsection \n\t" \
458 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300459 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
460 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800461 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300462 } while (0)
463
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300464/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300465#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300466 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300467 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300468 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300469 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300470 break; \
471 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300472 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300473 break; \
474 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300475 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300476 break; \
477 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300478 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300479 break; \
480 } \
481 } while (0)
482
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200483static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
484 enum x86_intercept intercept,
485 enum x86_intercept_stage stage)
486{
487 struct x86_instruction_info info = {
488 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300489 .rep_prefix = ctxt->rep_prefix,
490 .modrm_mod = ctxt->modrm_mod,
491 .modrm_reg = ctxt->modrm_reg,
492 .modrm_rm = ctxt->modrm_rm,
493 .src_val = ctxt->src.val64,
494 .src_bytes = ctxt->src.bytes,
495 .dst_bytes = ctxt->dst.bytes,
496 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200497 .next_rip = ctxt->eip,
498 };
499
Avi Kivity29535382011-04-20 13:37:53 +0300500 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200501}
502
Avi Kivityf47cfa32012-06-07 17:49:24 +0300503static void assign_masked(ulong *dest, ulong src, ulong mask)
504{
505 *dest = (*dest & ~mask) | (src & mask);
506}
507
Avi Kivity9dac77f2011-06-01 15:34:25 +0300508static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800509{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300510 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800511}
512
Avi Kivityf47cfa32012-06-07 17:49:24 +0300513static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
514{
515 u16 sel;
516 struct desc_struct ss;
517
518 if (ctxt->mode == X86EMUL_MODE_PROT64)
519 return ~0UL;
520 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
521 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
522}
523
Avi Kivity612e89f2012-06-12 20:03:23 +0300524static int stack_size(struct x86_emulate_ctxt *ctxt)
525{
526 return (__fls(stack_mask(ctxt)) + 1) >> 3;
527}
528
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800530static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300531address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800532{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300533 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800534 return reg;
535 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300536 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800537}
538
539static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300540register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800541{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300542 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800543}
544
Avi Kivity5ad105e2012-08-19 14:34:31 +0300545static void masked_increment(ulong *reg, ulong mask, int inc)
546{
547 assign_masked(reg, *reg + inc, mask);
548}
549
Harvey Harrison7a9572752008-02-19 07:40:41 -0800550static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300551register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800552{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300553 ulong mask;
554
Avi Kivity9dac77f2011-06-01 15:34:25 +0300555 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300556 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800557 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300558 mask = ad_mask(ctxt);
559 masked_increment(reg, mask, inc);
560}
561
562static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
563{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300564 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800565}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566
Avi Kivity9dac77f2011-06-01 15:34:25 +0300567static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800568{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300569 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800570}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300571
Avi Kivity56697682011-04-03 14:08:51 +0300572static u32 desc_limit_scaled(struct desc_struct *desc)
573{
574 u32 limit = get_desc_limit(desc);
575
576 return desc->g ? (limit << 12) | 0xfff : limit;
577}
578
Avi Kivity9dac77f2011-06-01 15:34:25 +0300579static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300580{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300581 ctxt->has_seg_override = true;
582 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300583}
584
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900585static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300586{
587 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
588 return 0;
589
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900590 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300591}
592
Avi Kivity9dac77f2011-06-01 15:34:25 +0300593static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300594{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300595 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300596 return 0;
597
Avi Kivity9dac77f2011-06-01 15:34:25 +0300598 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300599}
600
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200601static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
602 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300603{
Avi Kivityda9cb572010-11-22 17:53:21 +0200604 ctxt->exception.vector = vec;
605 ctxt->exception.error_code = error;
606 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200607 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300608}
609
Joerg Roedel3b88e412011-04-04 12:39:29 +0200610static int emulate_db(struct x86_emulate_ctxt *ctxt)
611{
612 return emulate_exception(ctxt, DB_VECTOR, 0, false);
613}
614
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200615static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300616{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200617 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300618}
619
Avi Kivity618ff152011-04-03 12:32:09 +0300620static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
621{
622 return emulate_exception(ctxt, SS_VECTOR, err, true);
623}
624
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200625static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300626{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200627 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300628}
629
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200630static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300631{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200632 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300633}
634
Avi Kivity34d1f492010-08-26 11:59:01 +0300635static int emulate_de(struct x86_emulate_ctxt *ctxt)
636{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200637 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300638}
639
Avi Kivity1253791d2011-03-29 11:41:27 +0200640static int emulate_nm(struct x86_emulate_ctxt *ctxt)
641{
642 return emulate_exception(ctxt, NM_VECTOR, 0, false);
643}
644
Avi Kivity1aa36612011-04-27 13:20:30 +0300645static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
646{
647 u16 selector;
648 struct desc_struct desc;
649
650 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
651 return selector;
652}
653
654static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
655 unsigned seg)
656{
657 u16 dummy;
658 u32 base3;
659 struct desc_struct desc;
660
661 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
662 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
663}
664
Avi Kivity1c11b372012-04-09 18:39:59 +0300665/*
666 * x86 defines three classes of vector instructions: explicitly
667 * aligned, explicitly unaligned, and the rest, which change behaviour
668 * depending on whether they're AVX encoded or not.
669 *
670 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
671 * subject to the same check.
672 */
673static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
674{
675 if (likely(size < 16))
676 return false;
677
678 if (ctxt->d & Aligned)
679 return true;
680 else if (ctxt->d & Unaligned)
681 return false;
682 else if (ctxt->d & Avx)
683 return false;
684 else
685 return true;
686}
687
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400688static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300689 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400690 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300691 ulong *linear)
692{
Avi Kivity618ff152011-04-03 12:32:09 +0300693 struct desc_struct desc;
694 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300695 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300696 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300697 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200698 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300699
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900700 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300701 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300702 case X86EMUL_MODE_PROT64:
703 if (((signed long)la << 16) >> 16 != la)
704 return emulate_gp(ctxt, 0);
705 break;
706 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300707 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
708 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300709 if (!usable)
710 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200711 /* code segment in protected mode or read-only data segment */
712 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
713 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300714 goto bad;
715 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400716 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300717 goto bad;
718 lim = desc_limit_scaled(&desc);
719 if ((desc.type & 8) || !(desc.type & 4)) {
720 /* expand-up segment */
721 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
722 goto bad;
723 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800724 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300725 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
726 goto bad;
727 lim = desc.d ? 0xffffffff : 0xffff;
728 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
729 goto bad;
730 }
Avi Kivity717746e2011-04-20 13:37:53 +0300731 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300732 if (!(desc.type & 8)) {
733 /* data segment */
734 if (cpl > desc.dpl)
735 goto bad;
736 } else if ((desc.type & 8) && !(desc.type & 4)) {
737 /* nonconforming code segment */
738 if (cpl != desc.dpl)
739 goto bad;
740 } else if ((desc.type & 8) && (desc.type & 4)) {
741 /* conforming code segment */
742 if (cpl < desc.dpl)
743 goto bad;
744 }
745 break;
746 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300747 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300748 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300749 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
750 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300751 *linear = la;
752 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300753bad:
754 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300755 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300756 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300757 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300758}
759
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400760static int linearize(struct x86_emulate_ctxt *ctxt,
761 struct segmented_address addr,
762 unsigned size, bool write,
763 ulong *linear)
764{
765 return __linearize(ctxt, addr, size, write, false, linear);
766}
767
768
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200769static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
770 struct segmented_address addr,
771 void *data,
772 unsigned size)
773{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200774 int rc;
775 ulong linear;
776
Avi Kivity83b87952011-04-03 11:31:19 +0300777 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200778 if (rc != X86EMUL_CONTINUE)
779 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300780 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200781}
782
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900783/*
784 * Fetch the next byte of the instruction being emulated which is pointed to
785 * by ctxt->_eip, then increment ctxt->_eip.
786 *
787 * Also prefetch the remaining bytes of the instruction without crossing page
788 * boundary if they are not in fetch_cache yet.
789 */
790static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200791{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300792 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200793 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300794 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200795
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900796 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400797 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900798 struct segmented_address addr = { .seg = VCPU_SREG_CS,
799 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300800 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900801 size = min(15UL - cur_size,
802 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400803 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900804 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400805 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900806 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
807 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900808 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200809 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300810 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200811 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900812 *dest = fc->data[ctxt->_eip - fc->start];
813 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900814 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200815}
816
817static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900818 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200819{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900820 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200821
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200822 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900823 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200824 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200825 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900826 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900827 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200828 return rc;
829 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900830 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200831}
832
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900833/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900834#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900835({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900836 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900837 if (rc != X86EMUL_CONTINUE) \
838 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900839 (_type)_x; \
840})
841
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900842#define insn_fetch_arr(_arr, _size, _ctxt) \
843({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900844 if (rc != X86EMUL_CONTINUE) \
845 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900846})
847
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000848/*
849 * Given the 'reg' portion of a ModRM byte, and a register block, return a
850 * pointer into the block that addresses the relevant register.
851 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
852 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300853static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000854 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855{
856 void *p;
857
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300859 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
860 else
861 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 return p;
863}
864
865static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200866 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 u16 *size, unsigned long *address, int op_bytes)
868{
869 int rc;
870
871 if (op_bytes == 2)
872 op_bytes = 3;
873 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200874 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900875 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200877 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200878 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 return rc;
880}
881
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300882static int test_cc(unsigned int condition, unsigned int flags)
883{
884 int rc = 0;
885
886 switch ((condition & 15) >> 1) {
887 case 0: /* o */
888 rc |= (flags & EFLG_OF);
889 break;
890 case 1: /* b/c/nae */
891 rc |= (flags & EFLG_CF);
892 break;
893 case 2: /* z/e */
894 rc |= (flags & EFLG_ZF);
895 break;
896 case 3: /* be/na */
897 rc |= (flags & (EFLG_CF|EFLG_ZF));
898 break;
899 case 4: /* s */
900 rc |= (flags & EFLG_SF);
901 break;
902 case 5: /* p/pe */
903 rc |= (flags & EFLG_PF);
904 break;
905 case 7: /* le/ng */
906 rc |= (flags & EFLG_ZF);
907 /* fall through */
908 case 6: /* l/nge */
909 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
910 break;
911 }
912
913 /* Odd condition identifiers (lsb == 1) have inverted sense. */
914 return (!!rc ^ (condition & 1));
915}
916
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300917static void fetch_register_operand(struct operand *op)
918{
919 switch (op->bytes) {
920 case 1:
921 op->val = *(u8 *)op->addr.reg;
922 break;
923 case 2:
924 op->val = *(u16 *)op->addr.reg;
925 break;
926 case 4:
927 op->val = *(u32 *)op->addr.reg;
928 break;
929 case 8:
930 op->val = *(u64 *)op->addr.reg;
931 break;
932 }
933}
934
Avi Kivity1253791d2011-03-29 11:41:27 +0200935static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
936{
937 ctxt->ops->get_fpu(ctxt);
938 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200939 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
940 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
941 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
942 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
943 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
944 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
945 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
946 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200947#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200948 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
949 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
950 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
951 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
952 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
953 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
954 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
955 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200956#endif
957 default: BUG();
958 }
959 ctxt->ops->put_fpu(ctxt);
960}
961
962static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
963 int reg)
964{
965 ctxt->ops->get_fpu(ctxt);
966 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200967 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
968 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
969 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
970 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
971 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
972 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
973 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
974 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200975#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200976 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
977 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
978 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
979 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
980 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
981 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
982 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
983 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200984#endif
985 default: BUG();
986 }
987 ctxt->ops->put_fpu(ctxt);
988}
989
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300990static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
991{
992 ctxt->ops->get_fpu(ctxt);
993 switch (reg) {
994 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
995 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
996 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
997 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
998 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
999 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1000 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1001 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1002 default: BUG();
1003 }
1004 ctxt->ops->put_fpu(ctxt);
1005}
1006
1007static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1008{
1009 ctxt->ops->get_fpu(ctxt);
1010 switch (reg) {
1011 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1012 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1013 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1014 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1015 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1016 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1017 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1018 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1019 default: BUG();
1020 }
1021 ctxt->ops->put_fpu(ctxt);
1022}
1023
Gleb Natapov045a2822012-12-20 16:57:43 +02001024static int em_fninit(struct x86_emulate_ctxt *ctxt)
1025{
1026 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1027 return emulate_nm(ctxt);
1028
1029 ctxt->ops->get_fpu(ctxt);
1030 asm volatile("fninit");
1031 ctxt->ops->put_fpu(ctxt);
1032 return X86EMUL_CONTINUE;
1033}
1034
1035static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1036{
1037 u16 fcw;
1038
1039 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1040 return emulate_nm(ctxt);
1041
1042 ctxt->ops->get_fpu(ctxt);
1043 asm volatile("fnstcw %0": "+m"(fcw));
1044 ctxt->ops->put_fpu(ctxt);
1045
1046 /* force 2 byte destination */
1047 ctxt->dst.bytes = 2;
1048 ctxt->dst.val = fcw;
1049
1050 return X86EMUL_CONTINUE;
1051}
1052
1053static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1054{
1055 u16 fsw;
1056
1057 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1058 return emulate_nm(ctxt);
1059
1060 ctxt->ops->get_fpu(ctxt);
1061 asm volatile("fnstsw %0": "+m"(fsw));
1062 ctxt->ops->put_fpu(ctxt);
1063
1064 /* force 2 byte destination */
1065 ctxt->dst.bytes = 2;
1066 ctxt->dst.val = fsw;
1067
1068 return X86EMUL_CONTINUE;
1069}
1070
Avi Kivity1253791d2011-03-29 11:41:27 +02001071static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001072 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001073{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001074 unsigned reg = ctxt->modrm_reg;
1075 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001076
Avi Kivity9dac77f2011-06-01 15:34:25 +03001077 if (!(ctxt->d & ModRM))
1078 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001079
Avi Kivity9dac77f2011-06-01 15:34:25 +03001080 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001081 op->type = OP_XMM;
1082 op->bytes = 16;
1083 op->addr.xmm = reg;
1084 read_sse_reg(ctxt, &op->vec_val, reg);
1085 return;
1086 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001087 if (ctxt->d & Mmx) {
1088 reg &= 7;
1089 op->type = OP_MM;
1090 op->bytes = 8;
1091 op->addr.mm = reg;
1092 return;
1093 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001094
Avi Kivity3c118e22007-10-31 10:27:04 +02001095 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001096 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001097 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001098 op->bytes = 1;
1099 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001100 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001101 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001102 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001103 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001104 op->orig_val = op->val;
1105}
1106
Avi Kivitya6e34072012-06-10 17:15:39 +03001107static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1108{
1109 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1110 ctxt->modrm_seg = VCPU_SREG_SS;
1111}
1112
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001113static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001114 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001115{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001116 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001117 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001118 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001119 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001120
Avi Kivity9dac77f2011-06-01 15:34:25 +03001121 if (ctxt->rex_prefix) {
1122 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1123 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1124 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001125 }
1126
Avi Kivity9dac77f2011-06-01 15:34:25 +03001127 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1128 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1129 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1130 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001131
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001133 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001134 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001135 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001136 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001137 op->type = OP_XMM;
1138 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001139 op->addr.xmm = ctxt->modrm_rm;
1140 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001141 return rc;
1142 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001143 if (ctxt->d & Mmx) {
1144 op->type = OP_MM;
1145 op->bytes = 8;
1146 op->addr.xmm = ctxt->modrm_rm & 7;
1147 return rc;
1148 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001149 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001150 return rc;
1151 }
1152
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001153 op->type = OP_MEM;
1154
Avi Kivity9dac77f2011-06-01 15:34:25 +03001155 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001156 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1157 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1158 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1159 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001160
1161 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001163 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001164 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001165 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166 break;
1167 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001168 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001169 break;
1170 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001171 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001172 break;
1173 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001174 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001176 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001177 break;
1178 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180 break;
1181 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001182 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 break;
1184 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001185 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186 break;
1187 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001188 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189 break;
1190 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001191 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192 break;
1193 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001194 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001195 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001196 break;
1197 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001198 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001199 break;
1200 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001201 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1202 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1203 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001204 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 } else {
1206 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001207 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001208 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 index_reg |= (sib >> 3) & 7;
1210 base_reg |= sib & 7;
1211 scale = sib >> 6;
1212
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001214 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001215 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001216 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001217 adjust_modrm_seg(ctxt, base_reg);
1218 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001219 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001220 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001221 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001222 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001223 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001224 } else {
1225 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001226 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001227 adjust_modrm_seg(ctxt, base_reg);
1228 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001230 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001232 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233 break;
1234 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001235 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 break;
1237 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001238 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 break;
1240 }
1241 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001242 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001243done:
1244 return rc;
1245}
1246
1247static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001248 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001249{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001250 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001251
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001252 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001253 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001255 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 break;
1257 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001258 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 break;
1260 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001261 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001262 break;
1263 }
1264done:
1265 return rc;
1266}
1267
Avi Kivity9dac77f2011-06-01 15:34:25 +03001268static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001269{
Sheng Yang7129eec2010-09-28 16:33:32 +08001270 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001271
Avi Kivity9dac77f2011-06-01 15:34:25 +03001272 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1273 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001274
Avi Kivity9dac77f2011-06-01 15:34:25 +03001275 if (ctxt->src.bytes == 2)
1276 sv = (s16)ctxt->src.val & (s16)mask;
1277 else if (ctxt->src.bytes == 4)
1278 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001279
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001281 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001282
1283 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001284 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001285}
1286
Gleb Natapov9de41572010-04-28 19:15:22 +03001287static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001288 unsigned long addr, void *dest, unsigned size)
1289{
1290 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001291 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001292
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001293 if (mc->pos < mc->end)
1294 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001295
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001296 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001297
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001298 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1299 &ctxt->exception);
1300 if (rc != X86EMUL_CONTINUE)
1301 return rc;
1302
1303 mc->end += size;
1304
1305read_cached:
1306 memcpy(dest, mc->data + mc->pos, size);
1307 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001308 return X86EMUL_CONTINUE;
1309}
1310
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001311static int segmented_read(struct x86_emulate_ctxt *ctxt,
1312 struct segmented_address addr,
1313 void *data,
1314 unsigned size)
1315{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001316 int rc;
1317 ulong linear;
1318
Avi Kivity83b87952011-04-03 11:31:19 +03001319 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001320 if (rc != X86EMUL_CONTINUE)
1321 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001322 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001323}
1324
1325static int segmented_write(struct x86_emulate_ctxt *ctxt,
1326 struct segmented_address addr,
1327 const void *data,
1328 unsigned size)
1329{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001330 int rc;
1331 ulong linear;
1332
Avi Kivity83b87952011-04-03 11:31:19 +03001333 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001334 if (rc != X86EMUL_CONTINUE)
1335 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001336 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1337 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001338}
1339
1340static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1341 struct segmented_address addr,
1342 const void *orig_data, const void *data,
1343 unsigned size)
1344{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001345 int rc;
1346 ulong linear;
1347
Avi Kivity83b87952011-04-03 11:31:19 +03001348 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001349 if (rc != X86EMUL_CONTINUE)
1350 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001351 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1352 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001353}
1354
Gleb Natapov7b262e92010-03-18 15:20:27 +02001355static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001356 unsigned int size, unsigned short port,
1357 void *dest)
1358{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001359 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001360
1361 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001362 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001363 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001364 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001365 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001366 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1367 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001368 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1369 count);
1370 if (n == 0)
1371 n = 1;
1372 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001373 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001374 return 0;
1375 rc->end = n * size;
1376 }
1377
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001378 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1379 ctxt->dst.data = rc->data + rc->pos;
1380 ctxt->dst.type = OP_MEM_STR;
1381 ctxt->dst.count = (rc->end - rc->pos) / size;
1382 rc->pos = rc->end;
1383 } else {
1384 memcpy(dest, rc->data + rc->pos, size);
1385 rc->pos += size;
1386 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001387 return 1;
1388}
1389
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001390static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1391 u16 index, struct desc_struct *desc)
1392{
1393 struct desc_ptr dt;
1394 ulong addr;
1395
1396 ctxt->ops->get_idt(ctxt, &dt);
1397
1398 if (dt.size < index * 8 + 7)
1399 return emulate_gp(ctxt, index << 3 | 0x2);
1400
1401 addr = dt.address + index * 8;
1402 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1403 &ctxt->exception);
1404}
1405
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001406static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001407 u16 selector, struct desc_ptr *dt)
1408{
Mathias Krause0225fb52012-08-30 01:30:16 +02001409 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001410
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001411 if (selector & 1 << 2) {
1412 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001413 u16 sel;
1414
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001415 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001416 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001417 return;
1418
1419 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1420 dt->address = get_desc_base(&desc);
1421 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001422 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001423}
1424
1425/* allowed just for 8 bytes segments */
1426static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001427 u16 selector, struct desc_struct *desc,
1428 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001429{
1430 struct desc_ptr dt;
1431 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432 ulong addr;
1433
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001434 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001435
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001436 if (dt.size < index * 8 + 7)
1437 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001438
Avi Kivitye9194642012-06-13 16:29:39 +03001439 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001440 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1441 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442}
1443
1444/* allowed just for 8 bytes segments */
1445static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001446 u16 selector, struct desc_struct *desc)
1447{
1448 struct desc_ptr dt;
1449 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001450 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001451
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001452 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001453
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001454 if (dt.size < index * 8 + 7)
1455 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001456
1457 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001458 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1459 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460}
1461
Gleb Natapov5601d052011-03-07 14:55:06 +02001462/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001464 u16 selector, int seg)
1465{
Avi Kivity869be992012-06-13 16:30:53 +03001466 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467 u8 dpl, rpl, cpl;
1468 unsigned err_vec = GP_VECTOR;
1469 u32 err_code = 0;
1470 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001471 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001472 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001473 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001474
1475 memset(&seg_desc, 0, sizeof seg_desc);
1476
1477 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1478 || ctxt->mode == X86EMUL_MODE_REAL) {
1479 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001480 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001481 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482 goto load;
1483 }
1484
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001485 rpl = selector & 3;
1486 cpl = ctxt->ops->cpl(ctxt);
1487
1488 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1489 if ((seg == VCPU_SREG_CS
1490 || (seg == VCPU_SREG_SS
1491 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1492 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493 && null_selector)
1494 goto exception;
1495
1496 /* TR should be in GDT only */
1497 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1498 goto exception;
1499
1500 if (null_selector) /* for NULL selector skip all following checks */
1501 goto load;
1502
Avi Kivitye9194642012-06-13 16:29:39 +03001503 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 if (ret != X86EMUL_CONTINUE)
1505 return ret;
1506
1507 err_code = selector & 0xfffc;
1508 err_vec = GP_VECTOR;
1509
Guo Chaofc058682012-06-28 15:19:51 +08001510 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001511 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1512 goto exception;
1513
1514 if (!seg_desc.p) {
1515 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1516 goto exception;
1517 }
1518
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001520
1521 switch (seg) {
1522 case VCPU_SREG_SS:
1523 /*
1524 * segment is not a writable data segment or segment
1525 * selector's RPL != CPL or segment selector's RPL != CPL
1526 */
1527 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1528 goto exception;
1529 break;
1530 case VCPU_SREG_CS:
1531 if (!(seg_desc.type & 8))
1532 goto exception;
1533
1534 if (seg_desc.type & 4) {
1535 /* conforming */
1536 if (dpl > cpl)
1537 goto exception;
1538 } else {
1539 /* nonconforming */
1540 if (rpl > cpl || dpl != cpl)
1541 goto exception;
1542 }
1543 /* CS(RPL) <- CPL */
1544 selector = (selector & 0xfffc) | cpl;
1545 break;
1546 case VCPU_SREG_TR:
1547 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1548 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001549 old_desc = seg_desc;
1550 seg_desc.type |= 2; /* busy */
1551 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1552 sizeof(seg_desc), &ctxt->exception);
1553 if (ret != X86EMUL_CONTINUE)
1554 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555 break;
1556 case VCPU_SREG_LDTR:
1557 if (seg_desc.s || seg_desc.type != 2)
1558 goto exception;
1559 break;
1560 default: /* DS, ES, FS, or GS */
1561 /*
1562 * segment is not a data or readable code segment or
1563 * ((segment is a data or nonconforming code segment)
1564 * and (both RPL and CPL > DPL))
1565 */
1566 if ((seg_desc.type & 0xa) == 0x8 ||
1567 (((seg_desc.type & 0xc) != 0xc) &&
1568 (rpl > dpl && cpl > dpl)))
1569 goto exception;
1570 break;
1571 }
1572
1573 if (seg_desc.s) {
1574 /* mark segment as accessed */
1575 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001576 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001577 if (ret != X86EMUL_CONTINUE)
1578 return ret;
1579 }
1580load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001581 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001582 return X86EMUL_CONTINUE;
1583exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001584 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001585 return X86EMUL_PROPAGATE_FAULT;
1586}
1587
Wei Yongjun31be40b2010-08-17 09:17:30 +08001588static void write_register_operand(struct operand *op)
1589{
1590 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1591 switch (op->bytes) {
1592 case 1:
1593 *(u8 *)op->addr.reg = (u8)op->val;
1594 break;
1595 case 2:
1596 *(u16 *)op->addr.reg = (u16)op->val;
1597 break;
1598 case 4:
1599 *op->addr.reg = (u32)op->val;
1600 break; /* 64b: zero-extend */
1601 case 8:
1602 *op->addr.reg = op->val;
1603 break;
1604 }
1605}
1606
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001607static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001608{
1609 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001610
Avi Kivity9dac77f2011-06-01 15:34:25 +03001611 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001612 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001613 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001614 break;
1615 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001616 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001617 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001618 ctxt->dst.addr.mem,
1619 &ctxt->dst.orig_val,
1620 &ctxt->dst.val,
1621 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001622 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001623 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001624 ctxt->dst.addr.mem,
1625 &ctxt->dst.val,
1626 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001627 if (rc != X86EMUL_CONTINUE)
1628 return rc;
1629 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001630 case OP_MEM_STR:
1631 rc = segmented_write(ctxt,
1632 ctxt->dst.addr.mem,
1633 ctxt->dst.data,
1634 ctxt->dst.bytes * ctxt->dst.count);
1635 if (rc != X86EMUL_CONTINUE)
1636 return rc;
1637 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001638 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001639 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001640 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001641 case OP_MM:
1642 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1643 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001644 case OP_NONE:
1645 /* no writeback */
1646 break;
1647 default:
1648 break;
1649 }
1650 return X86EMUL_CONTINUE;
1651}
1652
Avi Kivity51ddff52012-06-12 20:19:40 +03001653static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001654{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001655 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001656
Avi Kivity5ad105e2012-08-19 14:34:31 +03001657 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001658 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001659 addr.seg = VCPU_SREG_SS;
1660
Avi Kivity51ddff52012-06-12 20:19:40 +03001661 return segmented_write(ctxt, addr, data, bytes);
1662}
1663
1664static int em_push(struct x86_emulate_ctxt *ctxt)
1665{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001666 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001667 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001668 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001669}
1670
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001671static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001672 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001673{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001674 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001675 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001676
Avi Kivitydd856ef2012-08-27 23:46:17 +03001677 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001678 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001679 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001680 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681 return rc;
1682
Avi Kivity5ad105e2012-08-19 14:34:31 +03001683 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001684 return rc;
1685}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001686
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001687static int em_pop(struct x86_emulate_ctxt *ctxt)
1688{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001689 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001690}
1691
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001692static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001693 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001694{
1695 int rc;
1696 unsigned long val, change_mask;
1697 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001698 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001699
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001700 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001701 if (rc != X86EMUL_CONTINUE)
1702 return rc;
1703
1704 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1705 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1706
1707 switch(ctxt->mode) {
1708 case X86EMUL_MODE_PROT64:
1709 case X86EMUL_MODE_PROT32:
1710 case X86EMUL_MODE_PROT16:
1711 if (cpl == 0)
1712 change_mask |= EFLG_IOPL;
1713 if (cpl <= iopl)
1714 change_mask |= EFLG_IF;
1715 break;
1716 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001717 if (iopl < 3)
1718 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001719 change_mask |= EFLG_IF;
1720 break;
1721 default: /* real mode */
1722 change_mask |= (EFLG_IOPL | EFLG_IF);
1723 break;
1724 }
1725
1726 *(unsigned long *)dest =
1727 (ctxt->eflags & ~change_mask) | (val & change_mask);
1728
1729 return rc;
1730}
1731
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001732static int em_popf(struct x86_emulate_ctxt *ctxt)
1733{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001734 ctxt->dst.type = OP_REG;
1735 ctxt->dst.addr.reg = &ctxt->eflags;
1736 ctxt->dst.bytes = ctxt->op_bytes;
1737 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001738}
1739
Avi Kivity612e89f2012-06-12 20:03:23 +03001740static int em_enter(struct x86_emulate_ctxt *ctxt)
1741{
1742 int rc;
1743 unsigned frame_size = ctxt->src.val;
1744 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001745 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001746
1747 if (nesting_level)
1748 return X86EMUL_UNHANDLEABLE;
1749
Avi Kivitydd856ef2012-08-27 23:46:17 +03001750 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1751 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001752 if (rc != X86EMUL_CONTINUE)
1753 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001754 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001755 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001756 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1757 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001758 stack_mask(ctxt));
1759 return X86EMUL_CONTINUE;
1760}
1761
Avi Kivityf47cfa32012-06-07 17:49:24 +03001762static int em_leave(struct x86_emulate_ctxt *ctxt)
1763{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001764 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001765 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001766 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001767}
1768
Avi Kivity1cd196e2011-09-13 10:45:51 +03001769static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001770{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001771 int seg = ctxt->src2.val;
1772
Avi Kivity9dac77f2011-06-01 15:34:25 +03001773 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001774
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001775 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001776}
1777
Avi Kivity1cd196e2011-09-13 10:45:51 +03001778static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001779{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001780 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001781 unsigned long selector;
1782 int rc;
1783
Avi Kivity9dac77f2011-06-01 15:34:25 +03001784 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001785 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001786 return rc;
1787
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001788 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001789 return rc;
1790}
1791
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001792static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001793{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001794 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001795 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001796 int reg = VCPU_REGS_RAX;
1797
1798 while (reg <= VCPU_REGS_RDI) {
1799 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001800 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001801
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001802 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001803 if (rc != X86EMUL_CONTINUE)
1804 return rc;
1805
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001806 ++reg;
1807 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001808
Wei Yongjunc37eda12010-06-15 09:03:33 +08001809 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001810}
1811
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001812static int em_pushf(struct x86_emulate_ctxt *ctxt)
1813{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001814 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001815 return em_push(ctxt);
1816}
1817
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001818static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001819{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001820 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001821 int reg = VCPU_REGS_RDI;
1822
1823 while (reg >= VCPU_REGS_RAX) {
1824 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001825 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001826 --reg;
1827 }
1828
Avi Kivitydd856ef2012-08-27 23:46:17 +03001829 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001830 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001831 break;
1832 --reg;
1833 }
1834 return rc;
1835}
1836
Avi Kivitydd856ef2012-08-27 23:46:17 +03001837static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001838{
Mathias Krause0225fb52012-08-30 01:30:16 +02001839 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001840 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001841 struct desc_ptr dt;
1842 gva_t cs_addr;
1843 gva_t eip_addr;
1844 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001845
1846 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001847 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001848 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001849 if (rc != X86EMUL_CONTINUE)
1850 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001851
1852 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1853
Avi Kivity9dac77f2011-06-01 15:34:25 +03001854 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001855 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001856 if (rc != X86EMUL_CONTINUE)
1857 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001858
Avi Kivity9dac77f2011-06-01 15:34:25 +03001859 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001860 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001861 if (rc != X86EMUL_CONTINUE)
1862 return rc;
1863
Avi Kivity4bff1e862011-04-20 13:37:53 +03001864 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001865
1866 eip_addr = dt.address + (irq << 2);
1867 cs_addr = dt.address + (irq << 2) + 2;
1868
Avi Kivity0f65dd72011-04-20 13:37:53 +03001869 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001870 if (rc != X86EMUL_CONTINUE)
1871 return rc;
1872
Avi Kivity0f65dd72011-04-20 13:37:53 +03001873 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001874 if (rc != X86EMUL_CONTINUE)
1875 return rc;
1876
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001877 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001878 if (rc != X86EMUL_CONTINUE)
1879 return rc;
1880
Avi Kivity9dac77f2011-06-01 15:34:25 +03001881 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001882
1883 return rc;
1884}
1885
Avi Kivitydd856ef2012-08-27 23:46:17 +03001886int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1887{
1888 int rc;
1889
1890 invalidate_registers(ctxt);
1891 rc = __emulate_int_real(ctxt, irq);
1892 if (rc == X86EMUL_CONTINUE)
1893 writeback_registers(ctxt);
1894 return rc;
1895}
1896
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001897static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001898{
1899 switch(ctxt->mode) {
1900 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001901 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001902 case X86EMUL_MODE_VM86:
1903 case X86EMUL_MODE_PROT16:
1904 case X86EMUL_MODE_PROT32:
1905 case X86EMUL_MODE_PROT64:
1906 default:
1907 /* Protected mode interrupts unimplemented yet */
1908 return X86EMUL_UNHANDLEABLE;
1909 }
1910}
1911
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001912static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001913{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001914 int rc = X86EMUL_CONTINUE;
1915 unsigned long temp_eip = 0;
1916 unsigned long temp_eflags = 0;
1917 unsigned long cs = 0;
1918 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1919 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1920 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1921 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1922
1923 /* TODO: Add stack limit check */
1924
Avi Kivity9dac77f2011-06-01 15:34:25 +03001925 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001926
1927 if (rc != X86EMUL_CONTINUE)
1928 return rc;
1929
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001930 if (temp_eip & ~0xffff)
1931 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001932
Avi Kivity9dac77f2011-06-01 15:34:25 +03001933 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001934
1935 if (rc != X86EMUL_CONTINUE)
1936 return rc;
1937
Avi Kivity9dac77f2011-06-01 15:34:25 +03001938 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001939
1940 if (rc != X86EMUL_CONTINUE)
1941 return rc;
1942
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001943 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001944
1945 if (rc != X86EMUL_CONTINUE)
1946 return rc;
1947
Avi Kivity9dac77f2011-06-01 15:34:25 +03001948 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001949
1950
Avi Kivity9dac77f2011-06-01 15:34:25 +03001951 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001952 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001953 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001954 ctxt->eflags &= ~0xffff;
1955 ctxt->eflags |= temp_eflags;
1956 }
1957
1958 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1959 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1960
1961 return rc;
1962}
1963
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001964static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001965{
1966 switch(ctxt->mode) {
1967 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001968 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001969 case X86EMUL_MODE_VM86:
1970 case X86EMUL_MODE_PROT16:
1971 case X86EMUL_MODE_PROT32:
1972 case X86EMUL_MODE_PROT64:
1973 default:
1974 /* iret from protected mode unimplemented yet */
1975 return X86EMUL_UNHANDLEABLE;
1976 }
1977}
1978
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001979static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
1980{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001981 int rc;
1982 unsigned short sel;
1983
Avi Kivity9dac77f2011-06-01 15:34:25 +03001984 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001985
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001986 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001987 if (rc != X86EMUL_CONTINUE)
1988 return rc;
1989
Avi Kivity9dac77f2011-06-01 15:34:25 +03001990 ctxt->_eip = 0;
1991 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09001992 return X86EMUL_CONTINUE;
1993}
1994
Takuya Yoshikawa51187682011-05-02 02:29:17 +09001995static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001996{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001997 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001998 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03001999 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002000 break;
2001 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002002 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002003 break;
2004 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002005 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002006 break;
2007 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002008 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002009 break;
2010 case 4: /* sal/shl */
2011 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002012 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002013 break;
2014 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002015 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002016 break;
2017 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002018 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002019 break;
2020 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002021 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002022}
2023
Avi Kivity3329ece2011-09-13 10:45:39 +03002024static int em_not(struct x86_emulate_ctxt *ctxt)
2025{
2026 ctxt->dst.val = ~ctxt->dst.val;
2027 return X86EMUL_CONTINUE;
2028}
2029
2030static int em_neg(struct x86_emulate_ctxt *ctxt)
2031{
2032 emulate_1op(ctxt, "neg");
2033 return X86EMUL_CONTINUE;
2034}
2035
2036static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2037{
2038 u8 ex = 0;
2039
2040 emulate_1op_rax_rdx(ctxt, "mul", ex);
2041 return X86EMUL_CONTINUE;
2042}
2043
2044static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2045{
2046 u8 ex = 0;
2047
2048 emulate_1op_rax_rdx(ctxt, "imul", ex);
2049 return X86EMUL_CONTINUE;
2050}
2051
2052static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002053{
Avi Kivity34d1f492010-08-26 11:59:01 +03002054 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002055
Avi Kivity3329ece2011-09-13 10:45:39 +03002056 emulate_1op_rax_rdx(ctxt, "div", de);
2057 if (de)
2058 return emulate_de(ctxt);
2059 return X86EMUL_CONTINUE;
2060}
2061
2062static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2063{
2064 u8 de = 0;
2065
2066 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002067 if (de)
2068 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002069 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002070}
2071
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002072static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002073{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002074 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002075
Avi Kivity9dac77f2011-06-01 15:34:25 +03002076 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002077 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03002078 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002079 break;
2080 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03002081 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002082 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03002083 case 2: /* call near abs */ {
2084 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002085 old_eip = ctxt->_eip;
2086 ctxt->_eip = ctxt->src.val;
2087 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002088 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002089 break;
2090 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002091 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002092 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002093 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002094 case 5: /* jmp far */
2095 rc = em_jmp_far(ctxt);
2096 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002097 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002098 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002099 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002100 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002101 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002102}
2103
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002104static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002105{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002106 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002107
Avi Kivitydd856ef2012-08-27 23:46:17 +03002108 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2109 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2110 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2111 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002112 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002113 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002114 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2115 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002116
Laurent Vivier05f086f2007-09-24 11:10:55 +02002117 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002118 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002119 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002120}
2121
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002122static int em_ret(struct x86_emulate_ctxt *ctxt)
2123{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002124 ctxt->dst.type = OP_REG;
2125 ctxt->dst.addr.reg = &ctxt->_eip;
2126 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002127 return em_pop(ctxt);
2128}
2129
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002130static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002131{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002132 int rc;
2133 unsigned long cs;
2134
Avi Kivity9dac77f2011-06-01 15:34:25 +03002135 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002136 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002137 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002138 if (ctxt->op_bytes == 4)
2139 ctxt->_eip = (u32)ctxt->_eip;
2140 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002141 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002142 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002143 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002144 return rc;
2145}
2146
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002147static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2148{
2149 /* Save real source value, then compare EAX against destination. */
2150 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002151 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002152 emulate_2op_SrcV(ctxt, "cmp");
2153
2154 if (ctxt->eflags & EFLG_ZF) {
2155 /* Success: write back to memory. */
2156 ctxt->dst.val = ctxt->src.orig_val;
2157 } else {
2158 /* Failure: write the value we saw to EAX. */
2159 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002160 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002161 }
2162 return X86EMUL_CONTINUE;
2163}
2164
Avi Kivityd4b43252011-09-13 10:45:50 +03002165static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002166{
Avi Kivityd4b43252011-09-13 10:45:50 +03002167 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002168 unsigned short sel;
2169 int rc;
2170
Avi Kivity9dac77f2011-06-01 15:34:25 +03002171 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002172
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002173 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002174 if (rc != X86EMUL_CONTINUE)
2175 return rc;
2176
Avi Kivity9dac77f2011-06-01 15:34:25 +03002177 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002178 return rc;
2179}
2180
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002181static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002182setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002183 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002184{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002185 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002186 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002187 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002188 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002189 cs->type = 0x0b; /* Read, Execute, Accessed */
2190 cs->s = 1;
2191 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002192 cs->p = 1;
2193 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002194 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002195
Gleb Natapov79168fd2010-04-28 19:15:30 +03002196 set_desc_base(ss, 0); /* flat segment */
2197 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002198 ss->g = 1; /* 4kb granularity */
2199 ss->s = 1;
2200 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002201 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002202 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002203 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002204 ss->l = 0;
2205 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002206}
2207
Avi Kivity1a18a692012-02-01 12:23:21 +02002208static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2209{
2210 u32 eax, ebx, ecx, edx;
2211
2212 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002213 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2214 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002215 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2216 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2217}
2218
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002219static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2220{
Mathias Krause0225fb52012-08-30 01:30:16 +02002221 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002222 u32 eax, ebx, ecx, edx;
2223
2224 /*
2225 * syscall should always be enabled in longmode - so only become
2226 * vendor specific (cpuid) if other modes are active...
2227 */
2228 if (ctxt->mode == X86EMUL_MODE_PROT64)
2229 return true;
2230
2231 eax = 0x00000000;
2232 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002233 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2234 /*
2235 * Intel ("GenuineIntel")
2236 * remark: Intel CPUs only support "syscall" in 64bit
2237 * longmode. Also an 64bit guest with a
2238 * 32bit compat-app running will #UD !! While this
2239 * behaviour can be fixed (by emulating) into AMD
2240 * response - CPUs of AMD can't behave like Intel.
2241 */
2242 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2243 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2244 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2245 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002246
Avi Kivity0017f932012-06-07 14:10:16 +03002247 /* AMD ("AuthenticAMD") */
2248 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2249 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2250 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2251 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002252
Avi Kivity0017f932012-06-07 14:10:16 +03002253 /* AMD ("AMDisbetter!") */
2254 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2255 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2256 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2257 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002258
2259 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2260 return false;
2261}
2262
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002263static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002264{
Mathias Krause0225fb52012-08-30 01:30:16 +02002265 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002266 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002267 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002268 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002269 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002270
2271 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002272 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002273 ctxt->mode == X86EMUL_MODE_VM86)
2274 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002275
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002276 if (!(em_syscall_is_enabled(ctxt)))
2277 return emulate_ud(ctxt);
2278
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002279 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002280 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002281
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002282 if (!(efer & EFER_SCE))
2283 return emulate_ud(ctxt);
2284
Avi Kivity717746e2011-04-20 13:37:53 +03002285 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002286 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002287 cs_sel = (u16)(msr_data & 0xfffc);
2288 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002289
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002290 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002291 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002292 cs.l = 1;
2293 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002294 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2295 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002296
Avi Kivitydd856ef2012-08-27 23:46:17 +03002297 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002298 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002299#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002300 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002301
Avi Kivity717746e2011-04-20 13:37:53 +03002302 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002303 ctxt->mode == X86EMUL_MODE_PROT64 ?
2304 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002305 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002306
Avi Kivity717746e2011-04-20 13:37:53 +03002307 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002308 ctxt->eflags &= ~(msr_data | EFLG_RF);
2309#endif
2310 } else {
2311 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002312 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002313 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002314
2315 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2316 }
2317
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002318 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002319}
2320
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002321static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002322{
Mathias Krause0225fb52012-08-30 01:30:16 +02002323 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002324 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002325 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002326 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002327 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002328
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002329 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002330 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002331 if (ctxt->mode == X86EMUL_MODE_REAL)
2332 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002333
Avi Kivity1a18a692012-02-01 12:23:21 +02002334 /*
2335 * Not recognized on AMD in compat mode (but is recognized in legacy
2336 * mode).
2337 */
2338 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2339 && !vendor_intel(ctxt))
2340 return emulate_ud(ctxt);
2341
Andre Przywara8c604352009-06-18 12:56:01 +02002342 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2343 * Therefore, we inject an #UD.
2344 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002345 if (ctxt->mode == X86EMUL_MODE_PROT64)
2346 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002347
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002348 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002349
Avi Kivity717746e2011-04-20 13:37:53 +03002350 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002351 switch (ctxt->mode) {
2352 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002353 if ((msr_data & 0xfffc) == 0x0)
2354 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002355 break;
2356 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002357 if (msr_data == 0x0)
2358 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002359 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002360 default:
2361 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002362 }
2363
2364 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002365 cs_sel = (u16)msr_data;
2366 cs_sel &= ~SELECTOR_RPL_MASK;
2367 ss_sel = cs_sel + 8;
2368 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002369 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002370 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002371 cs.l = 1;
2372 }
2373
Avi Kivity1aa36612011-04-27 13:20:30 +03002374 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2375 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002376
Avi Kivity717746e2011-04-20 13:37:53 +03002377 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002378 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002379
Avi Kivity717746e2011-04-20 13:37:53 +03002380 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002381 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002382
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002383 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002384}
2385
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002386static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002387{
Mathias Krause0225fb52012-08-30 01:30:16 +02002388 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002389 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002390 u64 msr_data;
2391 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002392 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002393
Gleb Natapova0044752010-02-10 14:21:31 +02002394 /* inject #GP if in real mode or Virtual 8086 mode */
2395 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002396 ctxt->mode == X86EMUL_MODE_VM86)
2397 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002398
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002399 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002400
Avi Kivity9dac77f2011-06-01 15:34:25 +03002401 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002402 usermode = X86EMUL_MODE_PROT64;
2403 else
2404 usermode = X86EMUL_MODE_PROT32;
2405
2406 cs.dpl = 3;
2407 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002408 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002409 switch (usermode) {
2410 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002411 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002412 if ((msr_data & 0xfffc) == 0x0)
2413 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002414 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002415 break;
2416 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002417 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002418 if (msr_data == 0x0)
2419 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002420 ss_sel = cs_sel + 8;
2421 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002422 cs.l = 1;
2423 break;
2424 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002425 cs_sel |= SELECTOR_RPL_MASK;
2426 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002427
Avi Kivity1aa36612011-04-27 13:20:30 +03002428 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2429 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002430
Avi Kivitydd856ef2012-08-27 23:46:17 +03002431 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2432 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002433
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002434 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002435}
2436
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002437static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002438{
2439 int iopl;
2440 if (ctxt->mode == X86EMUL_MODE_REAL)
2441 return false;
2442 if (ctxt->mode == X86EMUL_MODE_VM86)
2443 return true;
2444 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002445 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002446}
2447
2448static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002449 u16 port, u16 len)
2450{
Mathias Krause0225fb52012-08-30 01:30:16 +02002451 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002452 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002453 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002454 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002455 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002456 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002457 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002458
Avi Kivity1aa36612011-04-27 13:20:30 +03002459 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002460 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002461 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002462 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002463 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002464 base = get_desc_base(&tr_seg);
2465#ifdef CONFIG_X86_64
2466 base |= ((u64)base3) << 32;
2467#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002468 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002469 if (r != X86EMUL_CONTINUE)
2470 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002471 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002472 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002473 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002474 if (r != X86EMUL_CONTINUE)
2475 return false;
2476 if ((perm >> bit_idx) & mask)
2477 return false;
2478 return true;
2479}
2480
2481static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002482 u16 port, u16 len)
2483{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002484 if (ctxt->perm_ok)
2485 return true;
2486
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002487 if (emulator_bad_iopl(ctxt))
2488 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002489 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002490
2491 ctxt->perm_ok = true;
2492
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002493 return true;
2494}
2495
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002496static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002497 struct tss_segment_16 *tss)
2498{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002499 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002500 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002501 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2502 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2503 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2504 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2505 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2506 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2507 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2508 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002509
Avi Kivity1aa36612011-04-27 13:20:30 +03002510 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2511 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2512 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2513 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2514 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002515}
2516
2517static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002518 struct tss_segment_16 *tss)
2519{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002520 int ret;
2521
Avi Kivity9dac77f2011-06-01 15:34:25 +03002522 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002524 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2525 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2526 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2527 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2528 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2529 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2530 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2531 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002532
2533 /*
2534 * SDM says that segment selectors are loaded before segment
2535 * descriptors
2536 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002537 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2538 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2539 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2540 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2541 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002542
2543 /*
Guo Chaofc058682012-06-28 15:19:51 +08002544 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 * it is handled in a context of new task
2546 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002547 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548 if (ret != X86EMUL_CONTINUE)
2549 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002550 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551 if (ret != X86EMUL_CONTINUE)
2552 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002553 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554 if (ret != X86EMUL_CONTINUE)
2555 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002556 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557 if (ret != X86EMUL_CONTINUE)
2558 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002559 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560 if (ret != X86EMUL_CONTINUE)
2561 return ret;
2562
2563 return X86EMUL_CONTINUE;
2564}
2565
2566static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567 u16 tss_selector, u16 old_tss_sel,
2568 ulong old_tss_base, struct desc_struct *new_desc)
2569{
Mathias Krause0225fb52012-08-30 01:30:16 +02002570 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571 struct tss_segment_16 tss_seg;
2572 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002573 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574
Avi Kivity0f65dd72011-04-20 13:37:53 +03002575 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002576 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002577 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002580
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002581 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582
Avi Kivity0f65dd72011-04-20 13:37:53 +03002583 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002584 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002585 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002588
Avi Kivity0f65dd72011-04-20 13:37:53 +03002589 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002590 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002591 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594
2595 if (old_tss_sel != 0xffff) {
2596 tss_seg.prev_task_link = old_tss_sel;
2597
Avi Kivity0f65dd72011-04-20 13:37:53 +03002598 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 &tss_seg.prev_task_link,
2600 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002601 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002602 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 }
2606
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002607 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608}
2609
2610static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611 struct tss_segment_32 *tss)
2612{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002613 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002614 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002616 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2617 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2618 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2619 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2620 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2621 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2622 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2623 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624
Avi Kivity1aa36612011-04-27 13:20:30 +03002625 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2626 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2627 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2628 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2629 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2630 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2631 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632}
2633
2634static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 struct tss_segment_32 *tss)
2636{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637 int ret;
2638
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002639 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002640 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002641 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002643
2644 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002645 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2646 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2647 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2648 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2649 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2650 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2651 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2652 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
2654 /*
2655 * SDM says that segment selectors are loaded before segment
2656 * descriptors
2657 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002658 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2659 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2660 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2661 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2662 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2663 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2664 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665
2666 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002667 * If we're switching between Protected Mode and VM86, we need to make
2668 * sure to update the mode before loading the segment descriptors so
2669 * that the selectors are interpreted correctly.
2670 *
2671 * Need to get rflags to the vcpu struct immediately because it
2672 * influences the CPL which is checked at least when loading the segment
2673 * descriptors and when pushing an error code to the new kernel stack.
2674 *
2675 * TODO Introduce a separate ctxt->ops->set_cpl callback
2676 */
2677 if (ctxt->eflags & X86_EFLAGS_VM)
2678 ctxt->mode = X86EMUL_MODE_VM86;
2679 else
2680 ctxt->mode = X86EMUL_MODE_PROT32;
2681
2682 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2683
2684 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 * Now load segment descriptors. If fault happenes at this stage
2686 * it is handled in a context of new task
2687 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002688 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002689 if (ret != X86EMUL_CONTINUE)
2690 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002691 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002692 if (ret != X86EMUL_CONTINUE)
2693 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002694 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002695 if (ret != X86EMUL_CONTINUE)
2696 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002697 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002698 if (ret != X86EMUL_CONTINUE)
2699 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002700 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002701 if (ret != X86EMUL_CONTINUE)
2702 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002703 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002704 if (ret != X86EMUL_CONTINUE)
2705 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002706 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002707 if (ret != X86EMUL_CONTINUE)
2708 return ret;
2709
2710 return X86EMUL_CONTINUE;
2711}
2712
2713static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 u16 tss_selector, u16 old_tss_sel,
2715 ulong old_tss_base, struct desc_struct *new_desc)
2716{
Mathias Krause0225fb52012-08-30 01:30:16 +02002717 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 struct tss_segment_32 tss_seg;
2719 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002720 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002721
Avi Kivity0f65dd72011-04-20 13:37:53 +03002722 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002723 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002724 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002728 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729
Avi Kivity0f65dd72011-04-20 13:37:53 +03002730 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002731 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002732 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002734 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735
Avi Kivity0f65dd72011-04-20 13:37:53 +03002736 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002737 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002738 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741
2742 if (old_tss_sel != 0xffff) {
2743 tss_seg.prev_task_link = old_tss_sel;
2744
Avi Kivity0f65dd72011-04-20 13:37:53 +03002745 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 &tss_seg.prev_task_link,
2747 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002748 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002749 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002752 }
2753
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002754 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755}
2756
2757static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002758 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002759 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760{
Mathias Krause0225fb52012-08-30 01:30:16 +02002761 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762 struct desc_struct curr_tss_desc, next_tss_desc;
2763 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002764 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002766 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002767 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002768 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769
2770 /* FIXME: old_tss_base == ~0 ? */
2771
Avi Kivitye9194642012-06-13 16:29:39 +03002772 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 if (ret != X86EMUL_CONTINUE)
2774 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002775 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 if (ret != X86EMUL_CONTINUE)
2777 return ret;
2778
2779 /* FIXME: check that next_tss_desc is tss */
2780
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002781 /*
2782 * Check privileges. The three cases are task switch caused by...
2783 *
2784 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2785 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002786 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002787 */
2788 if (reason == TASK_SWITCH_GATE) {
2789 if (idt_index != -1) {
2790 /* Software interrupts */
2791 struct desc_struct task_gate_desc;
2792 int dpl;
2793
2794 ret = read_interrupt_descriptor(ctxt, idt_index,
2795 &task_gate_desc);
2796 if (ret != X86EMUL_CONTINUE)
2797 return ret;
2798
2799 dpl = task_gate_desc.dpl;
2800 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2801 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2802 }
2803 } else if (reason != TASK_SWITCH_IRET) {
2804 int dpl = next_tss_desc.dpl;
2805 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2806 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807 }
2808
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002809
Gleb Natapovceffb452010-03-18 15:20:19 +02002810 desc_limit = desc_limit_scaled(&next_tss_desc);
2811 if (!next_tss_desc.p ||
2812 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2813 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002814 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002815 return X86EMUL_PROPAGATE_FAULT;
2816 }
2817
2818 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2819 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002820 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002821 }
2822
2823 if (reason == TASK_SWITCH_IRET)
2824 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2825
2826 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002827 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002828 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2829 old_tss_sel = 0xffff;
2830
2831 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002832 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002833 old_tss_base, &next_tss_desc);
2834 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002835 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002837 if (ret != X86EMUL_CONTINUE)
2838 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002839
2840 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2841 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2842
2843 if (reason != TASK_SWITCH_IRET) {
2844 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002845 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846 }
2847
Avi Kivity717746e2011-04-20 13:37:53 +03002848 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002849 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850
Jan Kiszkae269fb22010-04-14 15:51:09 +02002851 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002852 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2853 ctxt->lock_prefix = 0;
2854 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002855 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002856 }
2857
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002858 return ret;
2859}
2860
2861int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002862 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002863 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002864{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002865 int rc;
2866
Avi Kivitydd856ef2012-08-27 23:46:17 +03002867 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002868 ctxt->_eip = ctxt->eip;
2869 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002870
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002871 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002872 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002873
Avi Kivitydd856ef2012-08-27 23:46:17 +03002874 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002875 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002876 writeback_registers(ctxt);
2877 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002878
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002879 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002880}
2881
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002882static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2883 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002884{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002885 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002886
Avi Kivitydd856ef2012-08-27 23:46:17 +03002887 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2888 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002889}
2890
Avi Kivity7af04fc2010-08-18 14:16:35 +03002891static int em_das(struct x86_emulate_ctxt *ctxt)
2892{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002893 u8 al, old_al;
2894 bool af, cf, old_cf;
2895
2896 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002897 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002898
2899 old_al = al;
2900 old_cf = cf;
2901 cf = false;
2902 af = ctxt->eflags & X86_EFLAGS_AF;
2903 if ((al & 0x0f) > 9 || af) {
2904 al -= 6;
2905 cf = old_cf | (al >= 250);
2906 af = true;
2907 } else {
2908 af = false;
2909 }
2910 if (old_al > 0x99 || old_cf) {
2911 al -= 0x60;
2912 cf = true;
2913 }
2914
Avi Kivity9dac77f2011-06-01 15:34:25 +03002915 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002916 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002917 ctxt->src.type = OP_IMM;
2918 ctxt->src.val = 0;
2919 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002920 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002921 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2922 if (cf)
2923 ctxt->eflags |= X86_EFLAGS_CF;
2924 if (af)
2925 ctxt->eflags |= X86_EFLAGS_AF;
2926 return X86EMUL_CONTINUE;
2927}
2928
Gleb Natapov7f662272012-12-10 11:42:30 +02002929static int em_aad(struct x86_emulate_ctxt *ctxt)
2930{
2931 u8 al = ctxt->dst.val & 0xff;
2932 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2933
2934 al = (al + (ah * ctxt->src.val)) & 0xff;
2935
2936 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2937
2938 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2939
2940 if (!al)
2941 ctxt->eflags |= X86_EFLAGS_ZF;
2942 if (!(al & 1))
2943 ctxt->eflags |= X86_EFLAGS_PF;
2944 if (al & 0x80)
2945 ctxt->eflags |= X86_EFLAGS_SF;
2946
2947 return X86EMUL_CONTINUE;
2948}
2949
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002950static int em_call(struct x86_emulate_ctxt *ctxt)
2951{
2952 long rel = ctxt->src.val;
2953
2954 ctxt->src.val = (unsigned long)ctxt->_eip;
2955 jmp_rel(ctxt, rel);
2956 return em_push(ctxt);
2957}
2958
Avi Kivity0ef753b2010-08-18 14:51:45 +03002959static int em_call_far(struct x86_emulate_ctxt *ctxt)
2960{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002961 u16 sel, old_cs;
2962 ulong old_eip;
2963 int rc;
2964
Avi Kivity1aa36612011-04-27 13:20:30 +03002965 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002966 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002967
Avi Kivity9dac77f2011-06-01 15:34:25 +03002968 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002969 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002970 return X86EMUL_CONTINUE;
2971
Avi Kivity9dac77f2011-06-01 15:34:25 +03002972 ctxt->_eip = 0;
2973 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002974
Avi Kivity9dac77f2011-06-01 15:34:25 +03002975 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002976 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002977 if (rc != X86EMUL_CONTINUE)
2978 return rc;
2979
Avi Kivity9dac77f2011-06-01 15:34:25 +03002980 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002981 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002982}
2983
Avi Kivity40ece7c2010-08-18 15:12:09 +03002984static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
2985{
Avi Kivity40ece7c2010-08-18 15:12:09 +03002986 int rc;
2987
Avi Kivity9dac77f2011-06-01 15:34:25 +03002988 ctxt->dst.type = OP_REG;
2989 ctxt->dst.addr.reg = &ctxt->_eip;
2990 ctxt->dst.bytes = ctxt->op_bytes;
2991 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002992 if (rc != X86EMUL_CONTINUE)
2993 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03002994 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03002995 return X86EMUL_CONTINUE;
2996}
2997
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09002998static int em_add(struct x86_emulate_ctxt *ctxt)
2999{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003000 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003001 return X86EMUL_CONTINUE;
3002}
3003
3004static int em_or(struct x86_emulate_ctxt *ctxt)
3005{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003006 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003007 return X86EMUL_CONTINUE;
3008}
3009
3010static int em_adc(struct x86_emulate_ctxt *ctxt)
3011{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003012 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003013 return X86EMUL_CONTINUE;
3014}
3015
3016static int em_sbb(struct x86_emulate_ctxt *ctxt)
3017{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003018 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003019 return X86EMUL_CONTINUE;
3020}
3021
3022static int em_and(struct x86_emulate_ctxt *ctxt)
3023{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003024 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003025 return X86EMUL_CONTINUE;
3026}
3027
3028static int em_sub(struct x86_emulate_ctxt *ctxt)
3029{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003030 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003031 return X86EMUL_CONTINUE;
3032}
3033
3034static int em_xor(struct x86_emulate_ctxt *ctxt)
3035{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003036 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003037 return X86EMUL_CONTINUE;
3038}
3039
3040static int em_cmp(struct x86_emulate_ctxt *ctxt)
3041{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003042 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003043 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003044 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003045 return X86EMUL_CONTINUE;
3046}
3047
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003048static int em_test(struct x86_emulate_ctxt *ctxt)
3049{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003050 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03003051 /* Disable writeback. */
3052 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003053 return X86EMUL_CONTINUE;
3054}
3055
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003056static int em_xchg(struct x86_emulate_ctxt *ctxt)
3057{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003058 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003059 ctxt->src.val = ctxt->dst.val;
3060 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003061
3062 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003063 ctxt->dst.val = ctxt->src.orig_val;
3064 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003065 return X86EMUL_CONTINUE;
3066}
3067
Avi Kivity5c82aa22010-08-18 18:31:43 +03003068static int em_imul(struct x86_emulate_ctxt *ctxt)
3069{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003070 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03003071 return X86EMUL_CONTINUE;
3072}
3073
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003074static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3075{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003076 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03003077 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003078}
3079
Avi Kivity61429142010-08-19 15:13:00 +03003080static int em_cwd(struct x86_emulate_ctxt *ctxt)
3081{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003082 ctxt->dst.type = OP_REG;
3083 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003084 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003085 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003086
3087 return X86EMUL_CONTINUE;
3088}
3089
Avi Kivity48bb5d32010-08-18 18:54:34 +03003090static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3091{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003092 u64 tsc = 0;
3093
Avi Kivity717746e2011-04-20 13:37:53 +03003094 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003095 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3096 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003097 return X86EMUL_CONTINUE;
3098}
3099
Avi Kivity222d21a2011-11-10 14:57:30 +02003100static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3101{
3102 u64 pmc;
3103
Avi Kivitydd856ef2012-08-27 23:46:17 +03003104 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003105 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003106 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3107 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003108 return X86EMUL_CONTINUE;
3109}
3110
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003111static int em_mov(struct x86_emulate_ctxt *ctxt)
3112{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003113 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003114 return X86EMUL_CONTINUE;
3115}
3116
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003117static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3118{
3119 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3120 return emulate_gp(ctxt, 0);
3121
3122 /* Disable writeback. */
3123 ctxt->dst.type = OP_NONE;
3124 return X86EMUL_CONTINUE;
3125}
3126
3127static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3128{
3129 unsigned long val;
3130
3131 if (ctxt->mode == X86EMUL_MODE_PROT64)
3132 val = ctxt->src.val & ~0ULL;
3133 else
3134 val = ctxt->src.val & ~0U;
3135
3136 /* #UD condition is already handled. */
3137 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3138 return emulate_gp(ctxt, 0);
3139
3140 /* Disable writeback. */
3141 ctxt->dst.type = OP_NONE;
3142 return X86EMUL_CONTINUE;
3143}
3144
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003145static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3146{
3147 u64 msr_data;
3148
Avi Kivitydd856ef2012-08-27 23:46:17 +03003149 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3150 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3151 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003152 return emulate_gp(ctxt, 0);
3153
3154 return X86EMUL_CONTINUE;
3155}
3156
3157static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3158{
3159 u64 msr_data;
3160
Avi Kivitydd856ef2012-08-27 23:46:17 +03003161 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003162 return emulate_gp(ctxt, 0);
3163
Avi Kivitydd856ef2012-08-27 23:46:17 +03003164 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3165 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003166 return X86EMUL_CONTINUE;
3167}
3168
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003169static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3170{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003171 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003172 return emulate_ud(ctxt);
3173
Avi Kivity9dac77f2011-06-01 15:34:25 +03003174 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003175 return X86EMUL_CONTINUE;
3176}
3177
3178static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3179{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003180 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003181
Avi Kivity9dac77f2011-06-01 15:34:25 +03003182 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003183 return emulate_ud(ctxt);
3184
Avi Kivity9dac77f2011-06-01 15:34:25 +03003185 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003186 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3187
3188 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003189 ctxt->dst.type = OP_NONE;
3190 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003191}
3192
Avi Kivitya14e5792012-06-13 12:28:33 +03003193static int em_lldt(struct x86_emulate_ctxt *ctxt)
3194{
3195 u16 sel = ctxt->src.val;
3196
3197 /* Disable writeback. */
3198 ctxt->dst.type = OP_NONE;
3199 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3200}
3201
Avi Kivity80890002012-06-13 16:33:29 +03003202static int em_ltr(struct x86_emulate_ctxt *ctxt)
3203{
3204 u16 sel = ctxt->src.val;
3205
3206 /* Disable writeback. */
3207 ctxt->dst.type = OP_NONE;
3208 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3209}
3210
Avi Kivity38503912011-03-31 18:48:09 +02003211static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3212{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003213 int rc;
3214 ulong linear;
3215
Avi Kivity9dac77f2011-06-01 15:34:25 +03003216 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003217 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003218 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003219 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003220 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003221 return X86EMUL_CONTINUE;
3222}
3223
Avi Kivity2d04a052011-04-20 15:32:49 +03003224static int em_clts(struct x86_emulate_ctxt *ctxt)
3225{
3226 ulong cr0;
3227
3228 cr0 = ctxt->ops->get_cr(ctxt, 0);
3229 cr0 &= ~X86_CR0_TS;
3230 ctxt->ops->set_cr(ctxt, 0, cr0);
3231 return X86EMUL_CONTINUE;
3232}
3233
Avi Kivity26d05cc2011-04-21 12:07:59 +03003234static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3235{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003236 int rc;
3237
Avi Kivity9dac77f2011-06-01 15:34:25 +03003238 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003239 return X86EMUL_UNHANDLEABLE;
3240
3241 rc = ctxt->ops->fix_hypercall(ctxt);
3242 if (rc != X86EMUL_CONTINUE)
3243 return rc;
3244
3245 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003246 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003247 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003248 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003249 return X86EMUL_CONTINUE;
3250}
3251
Avi Kivity96051572012-06-10 17:21:18 +03003252static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3253 void (*get)(struct x86_emulate_ctxt *ctxt,
3254 struct desc_ptr *ptr))
3255{
3256 struct desc_ptr desc_ptr;
3257
3258 if (ctxt->mode == X86EMUL_MODE_PROT64)
3259 ctxt->op_bytes = 8;
3260 get(ctxt, &desc_ptr);
3261 if (ctxt->op_bytes == 2) {
3262 ctxt->op_bytes = 4;
3263 desc_ptr.address &= 0x00ffffff;
3264 }
3265 /* Disable writeback. */
3266 ctxt->dst.type = OP_NONE;
3267 return segmented_write(ctxt, ctxt->dst.addr.mem,
3268 &desc_ptr, 2 + ctxt->op_bytes);
3269}
3270
3271static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3272{
3273 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3274}
3275
3276static int em_sidt(struct x86_emulate_ctxt *ctxt)
3277{
3278 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3279}
3280
Avi Kivity26d05cc2011-04-21 12:07:59 +03003281static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3282{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003283 struct desc_ptr desc_ptr;
3284 int rc;
3285
Avi Kivity510425f2012-06-07 17:04:36 +03003286 if (ctxt->mode == X86EMUL_MODE_PROT64)
3287 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003288 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003289 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003290 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003291 if (rc != X86EMUL_CONTINUE)
3292 return rc;
3293 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3294 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003295 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003296 return X86EMUL_CONTINUE;
3297}
3298
Avi Kivity5ef39c72011-04-21 12:21:50 +03003299static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003300{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003301 int rc;
3302
Avi Kivity5ef39c72011-04-21 12:21:50 +03003303 rc = ctxt->ops->fix_hypercall(ctxt);
3304
Avi Kivity26d05cc2011-04-21 12:07:59 +03003305 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003306 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003307 return rc;
3308}
3309
3310static int em_lidt(struct x86_emulate_ctxt *ctxt)
3311{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003312 struct desc_ptr desc_ptr;
3313 int rc;
3314
Avi Kivity510425f2012-06-07 17:04:36 +03003315 if (ctxt->mode == X86EMUL_MODE_PROT64)
3316 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003317 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003318 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003319 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003320 if (rc != X86EMUL_CONTINUE)
3321 return rc;
3322 ctxt->ops->set_idt(ctxt, &desc_ptr);
3323 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003324 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003325 return X86EMUL_CONTINUE;
3326}
3327
3328static int em_smsw(struct x86_emulate_ctxt *ctxt)
3329{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003330 ctxt->dst.bytes = 2;
3331 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003332 return X86EMUL_CONTINUE;
3333}
3334
3335static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3336{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003338 | (ctxt->src.val & 0x0f));
3339 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340 return X86EMUL_CONTINUE;
3341}
3342
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003343static int em_loop(struct x86_emulate_ctxt *ctxt)
3344{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003345 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3346 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003347 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3348 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003349
3350 return X86EMUL_CONTINUE;
3351}
3352
3353static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3354{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003355 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003356 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003357
3358 return X86EMUL_CONTINUE;
3359}
3360
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003361static int em_in(struct x86_emulate_ctxt *ctxt)
3362{
3363 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3364 &ctxt->dst.val))
3365 return X86EMUL_IO_NEEDED;
3366
3367 return X86EMUL_CONTINUE;
3368}
3369
3370static int em_out(struct x86_emulate_ctxt *ctxt)
3371{
3372 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3373 &ctxt->src.val, 1);
3374 /* Disable writeback. */
3375 ctxt->dst.type = OP_NONE;
3376 return X86EMUL_CONTINUE;
3377}
3378
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003379static int em_cli(struct x86_emulate_ctxt *ctxt)
3380{
3381 if (emulator_bad_iopl(ctxt))
3382 return emulate_gp(ctxt, 0);
3383
3384 ctxt->eflags &= ~X86_EFLAGS_IF;
3385 return X86EMUL_CONTINUE;
3386}
3387
3388static int em_sti(struct x86_emulate_ctxt *ctxt)
3389{
3390 if (emulator_bad_iopl(ctxt))
3391 return emulate_gp(ctxt, 0);
3392
3393 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3394 ctxt->eflags |= X86_EFLAGS_IF;
3395 return X86EMUL_CONTINUE;
3396}
3397
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003398static int em_bt(struct x86_emulate_ctxt *ctxt)
3399{
3400 /* Disable writeback. */
3401 ctxt->dst.type = OP_NONE;
3402 /* only subword offset */
3403 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3404
3405 emulate_2op_SrcV_nobyte(ctxt, "bt");
3406 return X86EMUL_CONTINUE;
3407}
3408
3409static int em_bts(struct x86_emulate_ctxt *ctxt)
3410{
3411 emulate_2op_SrcV_nobyte(ctxt, "bts");
3412 return X86EMUL_CONTINUE;
3413}
3414
3415static int em_btr(struct x86_emulate_ctxt *ctxt)
3416{
3417 emulate_2op_SrcV_nobyte(ctxt, "btr");
3418 return X86EMUL_CONTINUE;
3419}
3420
3421static int em_btc(struct x86_emulate_ctxt *ctxt)
3422{
3423 emulate_2op_SrcV_nobyte(ctxt, "btc");
3424 return X86EMUL_CONTINUE;
3425}
3426
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003427static int em_bsf(struct x86_emulate_ctxt *ctxt)
3428{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003429 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003430 return X86EMUL_CONTINUE;
3431}
3432
3433static int em_bsr(struct x86_emulate_ctxt *ctxt)
3434{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003435 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003436 return X86EMUL_CONTINUE;
3437}
3438
Avi Kivity6d6eede2012-06-07 14:11:36 +03003439static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3440{
3441 u32 eax, ebx, ecx, edx;
3442
Avi Kivitydd856ef2012-08-27 23:46:17 +03003443 eax = reg_read(ctxt, VCPU_REGS_RAX);
3444 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003445 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003446 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3447 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3448 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3449 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003450 return X86EMUL_CONTINUE;
3451}
3452
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003453static int em_lahf(struct x86_emulate_ctxt *ctxt)
3454{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003455 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3456 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003457 return X86EMUL_CONTINUE;
3458}
3459
Avi Kivity92998362012-06-13 12:25:06 +03003460static int em_bswap(struct x86_emulate_ctxt *ctxt)
3461{
3462 switch (ctxt->op_bytes) {
3463#ifdef CONFIG_X86_64
3464 case 8:
3465 asm("bswap %0" : "+r"(ctxt->dst.val));
3466 break;
3467#endif
3468 default:
3469 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3470 break;
3471 }
3472 return X86EMUL_CONTINUE;
3473}
3474
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003475static bool valid_cr(int nr)
3476{
3477 switch (nr) {
3478 case 0:
3479 case 2 ... 4:
3480 case 8:
3481 return true;
3482 default:
3483 return false;
3484 }
3485}
3486
3487static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3488{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003489 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003490 return emulate_ud(ctxt);
3491
3492 return X86EMUL_CONTINUE;
3493}
3494
3495static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3496{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003497 u64 new_val = ctxt->src.val64;
3498 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003499 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003500
3501 static u64 cr_reserved_bits[] = {
3502 0xffffffff00000000ULL,
3503 0, 0, 0, /* CR3 checked later */
3504 CR4_RESERVED_BITS,
3505 0, 0, 0,
3506 CR8_RESERVED_BITS,
3507 };
3508
3509 if (!valid_cr(cr))
3510 return emulate_ud(ctxt);
3511
3512 if (new_val & cr_reserved_bits[cr])
3513 return emulate_gp(ctxt, 0);
3514
3515 switch (cr) {
3516 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003517 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003518 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3519 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3520 return emulate_gp(ctxt, 0);
3521
Avi Kivity717746e2011-04-20 13:37:53 +03003522 cr4 = ctxt->ops->get_cr(ctxt, 4);
3523 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003524
3525 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3526 !(cr4 & X86_CR4_PAE))
3527 return emulate_gp(ctxt, 0);
3528
3529 break;
3530 }
3531 case 3: {
3532 u64 rsvd = 0;
3533
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003534 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3535 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003536 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003537 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003538 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003539 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003540 rsvd = CR3_NONPAE_RESERVED_BITS;
3541
3542 if (new_val & rsvd)
3543 return emulate_gp(ctxt, 0);
3544
3545 break;
3546 }
3547 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003548 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003549
3550 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3551 return emulate_gp(ctxt, 0);
3552
3553 break;
3554 }
3555 }
3556
3557 return X86EMUL_CONTINUE;
3558}
3559
Joerg Roedel3b88e412011-04-04 12:39:29 +02003560static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3561{
3562 unsigned long dr7;
3563
Avi Kivity717746e2011-04-20 13:37:53 +03003564 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003565
3566 /* Check if DR7.Global_Enable is set */
3567 return dr7 & (1 << 13);
3568}
3569
3570static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3571{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003572 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003573 u64 cr4;
3574
3575 if (dr > 7)
3576 return emulate_ud(ctxt);
3577
Avi Kivity717746e2011-04-20 13:37:53 +03003578 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003579 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3580 return emulate_ud(ctxt);
3581
3582 if (check_dr7_gd(ctxt))
3583 return emulate_db(ctxt);
3584
3585 return X86EMUL_CONTINUE;
3586}
3587
3588static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3589{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003590 u64 new_val = ctxt->src.val64;
3591 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003592
3593 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3594 return emulate_gp(ctxt, 0);
3595
3596 return check_dr_read(ctxt);
3597}
3598
Joerg Roedel01de8b02011-04-04 12:39:31 +02003599static int check_svme(struct x86_emulate_ctxt *ctxt)
3600{
3601 u64 efer;
3602
Avi Kivity717746e2011-04-20 13:37:53 +03003603 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003604
3605 if (!(efer & EFER_SVME))
3606 return emulate_ud(ctxt);
3607
3608 return X86EMUL_CONTINUE;
3609}
3610
3611static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3612{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003613 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003614
3615 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003616 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003617 return emulate_gp(ctxt, 0);
3618
3619 return check_svme(ctxt);
3620}
3621
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003622static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3623{
Avi Kivity717746e2011-04-20 13:37:53 +03003624 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003625
Avi Kivity717746e2011-04-20 13:37:53 +03003626 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003627 return emulate_ud(ctxt);
3628
3629 return X86EMUL_CONTINUE;
3630}
3631
Joerg Roedel80612522011-04-04 12:39:33 +02003632static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3633{
Avi Kivity717746e2011-04-20 13:37:53 +03003634 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003635 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003636
Avi Kivity717746e2011-04-20 13:37:53 +03003637 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003638 (rcx > 3))
3639 return emulate_gp(ctxt, 0);
3640
3641 return X86EMUL_CONTINUE;
3642}
3643
Joerg Roedelf6511932011-04-04 12:39:35 +02003644static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3645{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003646 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3647 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003648 return emulate_gp(ctxt, 0);
3649
3650 return X86EMUL_CONTINUE;
3651}
3652
3653static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3654{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003655 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3656 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003657 return emulate_gp(ctxt, 0);
3658
3659 return X86EMUL_CONTINUE;
3660}
3661
Avi Kivity73fba5f2010-07-29 15:11:53 +03003662#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003663#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003664#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3665 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003666#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003667#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003668#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3669#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003670#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003671#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003672#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003673#define II(_f, _e, _i) \
3674 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003675#define IIP(_f, _e, _i, _p) \
3676 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3677 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003678#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003679
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003680#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003681#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003682#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003683#define I2bvIP(_f, _e, _i, _p) \
3684 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003685
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003686#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3687 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3688 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003689
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003690static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003691 DI(SrcNone | Priv, monitor),
3692 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003693 N, N, N, N, N, N,
3694};
3695
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003696static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003697 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3698 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3699 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3700 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3701 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3702 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3703 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3704 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003705};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003706
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003707static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003708 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003709 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003710 N, N, N, N, N, N,
3711};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003712
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003713static const struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003714 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003715 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003716 I(Lock, em_adc),
3717 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003718 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003719 I(Lock, em_sub),
3720 I(Lock, em_xor),
3721 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003722};
3723
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003724static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003725 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003726};
3727
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003728static const struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003729 I(DstMem | SrcImm, em_test),
3730 I(DstMem | SrcImm, em_test),
3731 I(DstMem | SrcNone | Lock, em_not),
3732 I(DstMem | SrcNone | Lock, em_neg),
3733 I(SrcMem, em_mul_ex),
3734 I(SrcMem, em_imul_ex),
3735 I(SrcMem, em_div_ex),
3736 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003737};
3738
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003739static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003740 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3741 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003742 N, N, N, N, N, N,
3743};
3744
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003745static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003746 I(DstMem | SrcNone | Lock, em_grp45),
3747 I(DstMem | SrcNone | Lock, em_grp45),
3748 I(SrcMem | Stack, em_grp45),
3749 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3750 I(SrcMem | Stack, em_grp45),
3751 I(SrcMemFAddr | ImplicitOps, em_grp45),
3752 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003753};
3754
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003755static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003756 DI(Prot, sldt),
3757 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003758 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003759 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003760 N, N, N, N,
3761};
3762
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003763static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003764 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3765 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003766 II(SrcMem | Priv, em_lgdt, lgdt),
3767 II(SrcMem | Priv, em_lidt, lidt),
3768 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3769 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3770 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003772 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003773 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003774 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003775 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3776 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3777 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003778} };
3779
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003780static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003782 I(DstMem | SrcImmByte, em_bt),
3783 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3784 I(DstMem | SrcImmByte | Lock, em_btr),
3785 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003786};
3787
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003788static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003789 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003790}, {
3791 N, N, N, N, N, N, N, N,
3792} };
3793
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003794static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003795 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003796 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003797};
3798
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003799static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003800 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003801};
3802
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003803static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003804 I(0, em_mov), N, N, N,
3805};
3806
Gleb Natapov045a2822012-12-20 16:57:43 +02003807static const struct escape escape_d9 = { {
3808 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3809}, {
3810 /* 0xC0 - 0xC7 */
3811 N, N, N, N, N, N, N, N,
3812 /* 0xC8 - 0xCF */
3813 N, N, N, N, N, N, N, N,
3814 /* 0xD0 - 0xC7 */
3815 N, N, N, N, N, N, N, N,
3816 /* 0xD8 - 0xDF */
3817 N, N, N, N, N, N, N, N,
3818 /* 0xE0 - 0xE7 */
3819 N, N, N, N, N, N, N, N,
3820 /* 0xE8 - 0xEF */
3821 N, N, N, N, N, N, N, N,
3822 /* 0xF0 - 0xF7 */
3823 N, N, N, N, N, N, N, N,
3824 /* 0xF8 - 0xFF */
3825 N, N, N, N, N, N, N, N,
3826} };
3827
3828static const struct escape escape_db = { {
3829 N, N, N, N, N, N, N, N,
3830}, {
3831 /* 0xC0 - 0xC7 */
3832 N, N, N, N, N, N, N, N,
3833 /* 0xC8 - 0xCF */
3834 N, N, N, N, N, N, N, N,
3835 /* 0xD0 - 0xC7 */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xD8 - 0xDF */
3838 N, N, N, N, N, N, N, N,
3839 /* 0xE0 - 0xE7 */
3840 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3841 /* 0xE8 - 0xEF */
3842 N, N, N, N, N, N, N, N,
3843 /* 0xF0 - 0xF7 */
3844 N, N, N, N, N, N, N, N,
3845 /* 0xF8 - 0xFF */
3846 N, N, N, N, N, N, N, N,
3847} };
3848
3849static const struct escape escape_dd = { {
3850 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3851}, {
3852 /* 0xC0 - 0xC7 */
3853 N, N, N, N, N, N, N, N,
3854 /* 0xC8 - 0xCF */
3855 N, N, N, N, N, N, N, N,
3856 /* 0xD0 - 0xC7 */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xD8 - 0xDF */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xE0 - 0xE7 */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xE8 - 0xEF */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xF0 - 0xF7 */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xF8 - 0xFF */
3867 N, N, N, N, N, N, N, N,
3868} };
3869
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003870static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003871 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003872 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003873 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3874 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003876 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003877 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3878 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003880 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003881 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3882 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003884 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003885 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3886 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003888 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003890 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003892 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003894 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0x40 - 0x4F */
3896 X16(D(DstReg)),
3897 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003898 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003900 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003902 I(ImplicitOps | Stack | No64, em_pusha),
3903 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3905 N, N, N, N,
3906 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003907 I(SrcImm | Mov | Stack, em_push),
3908 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003909 I(SrcImmByte | Mov | Stack, em_push),
3910 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003911 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003912 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003913 /* 0x70 - 0x7F */
3914 X16(D(SrcImmByte)),
3915 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003916 G(ByteOp | DstMem | SrcImm, group1),
3917 G(DstMem | SrcImm, group1),
3918 G(ByteOp | DstMem | SrcImm | No64, group1),
3919 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003920 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003921 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003922 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003923 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003924 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003925 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003926 D(ModRM | SrcMem | NoAccess | DstReg),
3927 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3928 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003930 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003931 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003932 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003933 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003934 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003935 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003937 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003938 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003939 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003940 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003942 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003943 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3944 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003945 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003947 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003949 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003951 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003952 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003953 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003954 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3955 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003956 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003958 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3959 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003960 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003961 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003962 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003963 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Gleb Natapov7f662272012-12-10 11:42:30 +02003964 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003965 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003966 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003968 X3(I(SrcImmByte, em_loop)),
3969 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003970 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3971 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003972 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003973 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003974 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003975 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3976 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003977 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003978 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003979 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3980 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003981 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003982 D(ImplicitOps), D(ImplicitOps),
3983 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003984 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3985};
3986
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003987static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003988 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003989 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003990 N, I(ImplicitOps | VendorSpecific, em_syscall),
3991 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003992 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003993 N, D(ImplicitOps | ModRM), N, N,
3994 /* 0x10 - 0x1F */
3995 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3996 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003997 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003998 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003999 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4000 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004002 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4003 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004004 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004005 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004006 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004007 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004008 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004009 I(ImplicitOps | VendorSpecific, em_sysenter),
4010 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004011 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004012 N, N, N, N, N, N, N, N,
4013 /* 0x40 - 0x4F */
4014 X16(D(DstReg | SrcMem | ModRM | Mov)),
4015 /* 0x50 - 0x5F */
4016 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4017 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004018 N, N, N, N,
4019 N, N, N, N,
4020 N, N, N, N,
4021 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004023 N, N, N, N,
4024 N, N, N, N,
4025 N, N, N, N,
4026 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004027 /* 0x80 - 0x8F */
4028 X16(D(SrcImm)),
4029 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004030 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004032 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03004033 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004034 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4035 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
4036 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004037 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004038 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004039 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4041 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004042 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004043 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004044 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004045 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004046 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004047 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4048 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004049 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004050 /* 0xB8 - 0xBF */
4051 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004052 G(BitOp, group8),
4053 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09004054 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004055 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004056 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004057 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a52010-08-17 09:19:34 +08004058 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004059 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004060 /* 0xC8 - 0xCF */
4061 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004062 /* 0xD0 - 0xDF */
4063 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4064 /* 0xE0 - 0xEF */
4065 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4066 /* 0xF0 - 0xFF */
4067 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4068};
4069
4070#undef D
4071#undef N
4072#undef G
4073#undef GD
4074#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004075#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004076#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004077
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004078#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004079#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004080#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004081#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004082#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004083
Avi Kivity9dac77f2011-06-01 15:34:25 +03004084static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004085{
4086 unsigned size;
4087
Avi Kivity9dac77f2011-06-01 15:34:25 +03004088 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004089 if (size == 8)
4090 size = 4;
4091 return size;
4092}
4093
4094static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4095 unsigned size, bool sign_extension)
4096{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004097 int rc = X86EMUL_CONTINUE;
4098
4099 op->type = OP_IMM;
4100 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004101 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004102 /* NB. Immediates are sign-extended as necessary. */
4103 switch (op->bytes) {
4104 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004105 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004106 break;
4107 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004108 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004109 break;
4110 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004111 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004112 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004113 case 8:
4114 op->val = insn_fetch(s64, ctxt);
4115 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004116 }
4117 if (!sign_extension) {
4118 switch (op->bytes) {
4119 case 1:
4120 op->val &= 0xff;
4121 break;
4122 case 2:
4123 op->val &= 0xffff;
4124 break;
4125 case 4:
4126 op->val &= 0xffffffff;
4127 break;
4128 }
4129 }
4130done:
4131 return rc;
4132}
4133
Avi Kivitya99455492011-09-13 10:45:41 +03004134static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4135 unsigned d)
4136{
4137 int rc = X86EMUL_CONTINUE;
4138
4139 switch (d) {
4140 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004141 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004142 break;
4143 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004144 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004145 break;
4146 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004147 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004148 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004149 *op = ctxt->memop;
4150 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004151 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03004152 fetch_bit_operand(ctxt);
4153 op->orig_val = op->val;
4154 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004155 case OpMem64:
4156 ctxt->memop.bytes = 8;
4157 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004158 case OpAcc:
4159 op->type = OP_REG;
4160 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004161 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004162 fetch_register_operand(op);
4163 op->orig_val = op->val;
4164 break;
4165 case OpDI:
4166 op->type = OP_MEM;
4167 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4168 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004169 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004170 op->addr.mem.seg = VCPU_SREG_ES;
4171 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004172 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004173 break;
4174 case OpDX:
4175 op->type = OP_REG;
4176 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004177 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004178 fetch_register_operand(op);
4179 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004180 case OpCL:
4181 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004182 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004183 break;
4184 case OpImmByte:
4185 rc = decode_imm(ctxt, op, 1, true);
4186 break;
4187 case OpOne:
4188 op->bytes = 1;
4189 op->val = 1;
4190 break;
4191 case OpImm:
4192 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4193 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004194 case OpImm64:
4195 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4196 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004197 case OpMem8:
4198 ctxt->memop.bytes = 1;
4199 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004200 case OpMem16:
4201 ctxt->memop.bytes = 2;
4202 goto mem_common;
4203 case OpMem32:
4204 ctxt->memop.bytes = 4;
4205 goto mem_common;
4206 case OpImmU16:
4207 rc = decode_imm(ctxt, op, 2, false);
4208 break;
4209 case OpImmU:
4210 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4211 break;
4212 case OpSI:
4213 op->type = OP_MEM;
4214 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4215 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004216 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004217 op->addr.mem.seg = seg_override(ctxt);
4218 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004219 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004220 break;
4221 case OpImmFAddr:
4222 op->type = OP_IMM;
4223 op->addr.mem.ea = ctxt->_eip;
4224 op->bytes = ctxt->op_bytes + 2;
4225 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4226 break;
4227 case OpMemFAddr:
4228 ctxt->memop.bytes = ctxt->op_bytes + 2;
4229 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004230 case OpES:
4231 op->val = VCPU_SREG_ES;
4232 break;
4233 case OpCS:
4234 op->val = VCPU_SREG_CS;
4235 break;
4236 case OpSS:
4237 op->val = VCPU_SREG_SS;
4238 break;
4239 case OpDS:
4240 op->val = VCPU_SREG_DS;
4241 break;
4242 case OpFS:
4243 op->val = VCPU_SREG_FS;
4244 break;
4245 case OpGS:
4246 op->val = VCPU_SREG_GS;
4247 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004248 case OpImplicit:
4249 /* Special instructions do their own operand decoding. */
4250 default:
4251 op->type = OP_NONE; /* Disable writeback. */
4252 break;
4253 }
4254
4255done:
4256 return rc;
4257}
4258
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004259int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004260{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004261 int rc = X86EMUL_CONTINUE;
4262 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004263 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004264 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004265 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004266
Avi Kivityf09ed832011-09-13 10:45:40 +03004267 ctxt->memop.type = OP_NONE;
4268 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004269 ctxt->_eip = ctxt->eip;
4270 ctxt->fetch.start = ctxt->_eip;
4271 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004272 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004273 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004274
4275 switch (mode) {
4276 case X86EMUL_MODE_REAL:
4277 case X86EMUL_MODE_VM86:
4278 case X86EMUL_MODE_PROT16:
4279 def_op_bytes = def_ad_bytes = 2;
4280 break;
4281 case X86EMUL_MODE_PROT32:
4282 def_op_bytes = def_ad_bytes = 4;
4283 break;
4284#ifdef CONFIG_X86_64
4285 case X86EMUL_MODE_PROT64:
4286 def_op_bytes = 4;
4287 def_ad_bytes = 8;
4288 break;
4289#endif
4290 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004291 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004292 }
4293
Avi Kivity9dac77f2011-06-01 15:34:25 +03004294 ctxt->op_bytes = def_op_bytes;
4295 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004296
4297 /* Legacy prefixes. */
4298 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004299 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004300 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004301 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004302 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004303 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004304 break;
4305 case 0x67: /* address-size override */
4306 if (mode == X86EMUL_MODE_PROT64)
4307 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004308 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004309 else
4310 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004311 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004312 break;
4313 case 0x26: /* ES override */
4314 case 0x2e: /* CS override */
4315 case 0x36: /* SS override */
4316 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004317 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004318 break;
4319 case 0x64: /* FS override */
4320 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004321 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004322 break;
4323 case 0x40 ... 0x4f: /* REX */
4324 if (mode != X86EMUL_MODE_PROT64)
4325 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004326 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004327 continue;
4328 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004329 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004330 break;
4331 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004332 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004333 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004334 break;
4335 default:
4336 goto done_prefixes;
4337 }
4338
4339 /* Any legacy prefix after a REX prefix nullifies its effect. */
4340
Avi Kivity9dac77f2011-06-01 15:34:25 +03004341 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004342 }
4343
4344done_prefixes:
4345
4346 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004347 if (ctxt->rex_prefix & 8)
4348 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004349
4350 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004351 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004352 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 if (ctxt->b == 0x0f) {
4354 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004355 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004356 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004357 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004358 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004359
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004360 if (ctxt->d & ModRM)
4361 ctxt->modrm = insn_fetch(u8, ctxt);
4362
Avi Kivity9dac77f2011-06-01 15:34:25 +03004363 while (ctxt->d & GroupMask) {
4364 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004365 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004366 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004367 opcode = opcode.u.group[goffset];
4368 break;
4369 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004370 goffset = (ctxt->modrm >> 3) & 7;
4371 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004372 opcode = opcode.u.gdual->mod3[goffset];
4373 else
4374 opcode = opcode.u.gdual->mod012[goffset];
4375 break;
4376 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004377 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004378 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004379 break;
4380 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004381 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004382 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004383 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004384 switch (simd_prefix) {
4385 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4386 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4387 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4388 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4389 }
4390 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004391 case Escape:
4392 if (ctxt->modrm > 0xbf)
4393 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4394 else
4395 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4396 break;
Avi Kivity46561642011-04-24 14:09:59 +03004397 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004398 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004399 }
Avi Kivity46561642011-04-24 14:09:59 +03004400
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004401 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004402 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004403 }
4404
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 ctxt->execute = opcode.u.execute;
4406 ctxt->check_perm = opcode.check_perm;
4407 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004408
4409 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004410 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004411 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004412
Avi Kivity9dac77f2011-06-01 15:34:25 +03004413 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004414 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004415
Avi Kivity9dac77f2011-06-01 15:34:25 +03004416 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4417 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004418
Avi Kivity9dac77f2011-06-01 15:34:25 +03004419 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004420 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004421 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004422 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004423 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004424 }
4425
Avi Kivity9dac77f2011-06-01 15:34:25 +03004426 if (ctxt->d & Sse)
4427 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004428 else if (ctxt->d & Mmx)
4429 ctxt->op_bytes = 8;
Avi Kivity1253791d2011-03-29 11:41:27 +02004430
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004431 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004432 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004433 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004434 if (!ctxt->has_seg_override)
4435 set_seg_override(ctxt, ctxt->modrm_seg);
4436 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004437 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438 if (rc != X86EMUL_CONTINUE)
4439 goto done;
4440
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 if (!ctxt->has_seg_override)
4442 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004443
Avi Kivityf09ed832011-09-13 10:45:40 +03004444 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004445
Avi Kivityf09ed832011-09-13 10:45:40 +03004446 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4447 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004448
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004449 /*
4450 * Decode and fetch the source operand: register, memory
4451 * or immediate.
4452 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004453 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004454 if (rc != X86EMUL_CONTINUE)
4455 goto done;
4456
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004457 /*
4458 * Decode and fetch the second source operand: register, memory
4459 * or immediate.
4460 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004461 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004462 if (rc != X86EMUL_CONTINUE)
4463 goto done;
4464
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004465 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004466 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004467
4468done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004469 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4470 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004471
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004472 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473}
4474
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004475bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4476{
4477 return ctxt->d & PageTable;
4478}
4479
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004480static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4481{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004482 /* The second termination condition only applies for REPE
4483 * and REPNE. Test if the repeat string operation prefix is
4484 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4485 * corresponding termination condition according to:
4486 * - if REPE/REPZ and ZF = 0 then done
4487 * - if REPNE/REPNZ and ZF = 1 then done
4488 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004489 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4490 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4491 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004492 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004493 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004494 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4495 return true;
4496
4497 return false;
4498}
4499
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004500static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4501{
4502 bool fault = false;
4503
4504 ctxt->ops->get_fpu(ctxt);
4505 asm volatile("1: fwait \n\t"
4506 "2: \n\t"
4507 ".pushsection .fixup,\"ax\" \n\t"
4508 "3: \n\t"
4509 "movb $1, %[fault] \n\t"
4510 "jmp 2b \n\t"
4511 ".popsection \n\t"
4512 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004513 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004514 ctxt->ops->put_fpu(ctxt);
4515
4516 if (unlikely(fault))
4517 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4518
4519 return X86EMUL_CONTINUE;
4520}
4521
4522static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4523 struct operand *op)
4524{
4525 if (op->type == OP_MM)
4526 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4527}
4528
Avi Kivitye28bbd42013-01-04 16:18:48 +02004529static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4530{
4531 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4532 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4533 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4534 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4535 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4536 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4537 return X86EMUL_CONTINUE;
4538}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004539
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004540int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004541{
Mathias Krause0225fb52012-08-30 01:30:16 +02004542 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004543 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004544 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004545
Avi Kivity9dac77f2011-06-01 15:34:25 +03004546 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004547
Avi Kivity9dac77f2011-06-01 15:34:25 +03004548 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004549 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004550 goto done;
4551 }
4552
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004553 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004554 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004555 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004556 goto done;
4557 }
4558
Avi Kivity9dac77f2011-06-01 15:34:25 +03004559 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004560 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004561 goto done;
4562 }
4563
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004564 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4565 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004566 rc = emulate_ud(ctxt);
4567 goto done;
4568 }
4569
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004570 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004571 rc = emulate_nm(ctxt);
4572 goto done;
4573 }
4574
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004575 if (ctxt->d & Mmx) {
4576 rc = flush_pending_x87_faults(ctxt);
4577 if (rc != X86EMUL_CONTINUE)
4578 goto done;
4579 /*
4580 * Now that we know the fpu is exception safe, we can fetch
4581 * operands from it.
4582 */
4583 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4584 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4585 if (!(ctxt->d & Mov))
4586 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4587 }
4588
Avi Kivity9dac77f2011-06-01 15:34:25 +03004589 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4590 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004591 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004592 if (rc != X86EMUL_CONTINUE)
4593 goto done;
4594 }
4595
Gleb Natapove92805a2010-02-10 14:21:35 +02004596 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004597 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004598 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004599 goto done;
4600 }
4601
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004602 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004603 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004604 rc = emulate_ud(ctxt);
4605 goto done;
4606 }
4607
Joerg Roedeld09beab2011-04-04 12:39:25 +02004608 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004609 if (ctxt->check_perm) {
4610 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004611 if (rc != X86EMUL_CONTINUE)
4612 goto done;
4613 }
4614
Avi Kivity9dac77f2011-06-01 15:34:25 +03004615 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4616 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004617 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004618 if (rc != X86EMUL_CONTINUE)
4619 goto done;
4620 }
4621
Avi Kivity9dac77f2011-06-01 15:34:25 +03004622 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004623 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004624 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004625 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004626 goto done;
4627 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004628 }
4629
Avi Kivity9dac77f2011-06-01 15:34:25 +03004630 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4631 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4632 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004633 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004634 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004635 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004636 }
4637
Avi Kivity9dac77f2011-06-01 15:34:25 +03004638 if (ctxt->src2.type == OP_MEM) {
4639 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4640 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004641 if (rc != X86EMUL_CONTINUE)
4642 goto done;
4643 }
4644
Avi Kivity9dac77f2011-06-01 15:34:25 +03004645 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004646 goto special_insn;
4647
4648
Avi Kivity9dac77f2011-06-01 15:34:25 +03004649 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004650 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004651 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4652 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004653 if (rc != X86EMUL_CONTINUE)
4654 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004655 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004656 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004657
Avi Kivity018a98d2007-11-27 19:30:56 +02004658special_insn:
4659
Avi Kivity9dac77f2011-06-01 15:34:25 +03004660 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4661 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004662 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004663 if (rc != X86EMUL_CONTINUE)
4664 goto done;
4665 }
4666
Avi Kivity9dac77f2011-06-01 15:34:25 +03004667 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004668 if (ctxt->d & Fastop) {
4669 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4670 rc = fastop(ctxt, fop);
4671 if (rc != X86EMUL_CONTINUE)
4672 goto done;
4673 goto writeback;
4674 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004675 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004676 if (rc != X86EMUL_CONTINUE)
4677 goto done;
4678 goto writeback;
4679 }
4680
Avi Kivity9dac77f2011-06-01 15:34:25 +03004681 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682 goto twobyte_insn;
4683
Avi Kivity9dac77f2011-06-01 15:34:25 +03004684 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004685 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004686 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004687 break;
4688 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004689 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004690 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004691 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004692 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004694 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004695 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004696 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004697 if (test_cc(ctxt->b, ctxt->eflags))
4698 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004699 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004700 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004701 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004702 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004703 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004704 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004705 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004706 rc = em_xchg(ctxt);
4707 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004708 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004709 switch (ctxt->op_bytes) {
4710 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4711 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4712 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004713 }
4714 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004715 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004716 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004717 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004718 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004719 rc = emulate_int(ctxt, 3);
4720 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004721 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004722 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004723 break;
4724 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004725 if (ctxt->eflags & EFLG_OF)
4726 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004727 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004728 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004729 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004730 break;
4731 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004732 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004733 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004734 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004735 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004736 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004737 jmp_rel(ctxt, ctxt->src.val);
4738 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004739 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004740 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004741 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004742 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004743 case 0xf5: /* cmc */
4744 /* complement carry flag from eflags reg */
4745 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004746 break;
4747 case 0xf8: /* clc */
4748 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004749 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004750 case 0xf9: /* stc */
4751 ctxt->eflags |= EFLG_CF;
4752 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004753 case 0xfc: /* cld */
4754 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004755 break;
4756 case 0xfd: /* std */
4757 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004758 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004759 default:
4760 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004762
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004763 if (rc != X86EMUL_CONTINUE)
4764 goto done;
4765
Avi Kivity018a98d2007-11-27 19:30:56 +02004766writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004767 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004768 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004769 goto done;
4770
Gleb Natapov5cd21912010-03-18 15:20:26 +02004771 /*
4772 * restore dst type in case the decoding will be reused
4773 * (happens for string instruction )
4774 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004775 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004776
Avi Kivity9dac77f2011-06-01 15:34:25 +03004777 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004778 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004779
Avi Kivity9dac77f2011-06-01 15:34:25 +03004780 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004781 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004782
Avi Kivity9dac77f2011-06-01 15:34:25 +03004783 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004784 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004785 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004786 if ((ctxt->d & SrcMask) == SrcSI)
4787 count = ctxt->src.count;
4788 else
4789 count = ctxt->dst.count;
4790 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4791 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004792
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004793 if (!string_insn_completed(ctxt)) {
4794 /*
4795 * Re-enter guest when pio read ahead buffer is empty
4796 * or, if it is not used, after each 1024 iteration.
4797 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004798 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004799 (r->end == 0 || r->end != r->pos)) {
4800 /*
4801 * Reset read cache. Usually happens before
4802 * decode, but since instruction is restarted
4803 * we have to do it here.
4804 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004805 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004806 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004807 return EMULATION_RESTART;
4808 }
4809 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004810 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004811 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004812
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004814
4815done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004816 if (rc == X86EMUL_PROPAGATE_FAULT)
4817 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004818 if (rc == X86EMUL_INTERCEPTED)
4819 return EMULATION_INTERCEPTED;
4820
Avi Kivitydd856ef2012-08-27 23:46:17 +03004821 if (rc == X86EMUL_CONTINUE)
4822 writeback_registers(ctxt);
4823
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004824 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004825
4826twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004827 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004828 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004829 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004830 break;
4831 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004832 case 0x0d: /* GrpP (prefetch) */
4833 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004834 break;
4835 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004836 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004837 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004839 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004840 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4843 if (!test_cc(ctxt->b, ctxt->eflags))
4844 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004845 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004846 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004847 if (test_cc(ctxt->b, ctxt->eflags))
4848 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004849 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004850 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004851 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004852 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004853 case 0xa4: /* shld imm8, r, r/m */
4854 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004855 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004856 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004857 case 0xac: /* shrd imm8, r, r/m */
4858 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004859 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004860 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004861 case 0xae: /* clflush */
4862 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004865 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004866 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004870 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004871 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 break;
Wei Yongjun92f738a52010-08-17 09:19:34 +08004873 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004874 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a52010-08-17 09:19:34 +08004875 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004876 ctxt->src.val = ctxt->dst.orig_val;
4877 write_register_operand(&ctxt->src);
Wei Yongjun92f738a52010-08-17 09:19:34 +08004878 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004879 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 ctxt->dst.bytes = ctxt->op_bytes;
4881 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4882 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004883 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004884 default:
4885 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004887
4888 if (rc != X86EMUL_CONTINUE)
4889 goto done;
4890
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891 goto writeback;
4892
4893cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004894 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004896
4897void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4898{
4899 invalidate_registers(ctxt);
4900}
4901
4902void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4903{
4904 writeback_registers(ctxt);
4905}