blob: a21773f221077bc2bc8742e45f0255778e1b7feb [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 Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Avi Kivitya9945542011-09-13 10:45:41 +030063
Avi Kivity0fe59122011-09-13 10:45:47 +030064#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030065#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030066
67/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080068 * Opcode effective-address decode tables.
69 * Note that we only emulate instructions that have at least one memory
70 * operand (excluding implicit stack references). We assume that stack
71 * references and instruction fetches will never occur in special memory
72 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
73 * not be handled.
74 */
75
76/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030077#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080078/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030079#define DstShift 1
80#define ImplicitOps (OpImplicit << DstShift)
81#define DstReg (OpReg << DstShift)
82#define DstMem (OpMem << DstShift)
83#define DstAcc (OpAcc << DstShift)
84#define DstDI (OpDI << DstShift)
85#define DstMem64 (OpMem64 << DstShift)
86#define DstImmUByte (OpImmUByte << DstShift)
87#define DstDX (OpDX << DstShift)
88#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080089/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030090#define SrcShift 6
91#define SrcNone (OpNone << SrcShift)
92#define SrcReg (OpReg << SrcShift)
93#define SrcMem (OpMem << SrcShift)
94#define SrcMem16 (OpMem16 << SrcShift)
95#define SrcMem32 (OpMem32 << SrcShift)
96#define SrcImm (OpImm << SrcShift)
97#define SrcImmByte (OpImmByte << SrcShift)
98#define SrcOne (OpOne << SrcShift)
99#define SrcImmUByte (OpImmUByte << SrcShift)
100#define SrcImmU (OpImmU << SrcShift)
101#define SrcSI (OpSI << SrcShift)
102#define SrcImmFAddr (OpImmFAddr << SrcShift)
103#define SrcMemFAddr (OpMemFAddr << SrcShift)
104#define SrcAcc (OpAcc << SrcShift)
105#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200106#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200108#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300110#define BitOp (1<<11)
111#define MemAbs (1<<12) /* Memory operand is absolute displacement */
112#define String (1<<13) /* String instruction (rep capable) */
113#define Stack (1<<14) /* Stack instruction (push/pop) */
114#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
115#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
116#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
117#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
118#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200119#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300120#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300121/* Generic ModRM decode. */
122#define ModRM (1<<19)
123/* Destination is only written; never read. */
124#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300125/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200126#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200127#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300128#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300129#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300130#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200131#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200132#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300133#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800134#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100135/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800136#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300137#define Src2None (OpNone << Src2Shift)
138#define Src2CL (OpCL << Src2Shift)
139#define Src2ImmByte (OpImmByte << Src2Shift)
140#define Src2One (OpOne << Src2Shift)
141#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300142#define Src2ES (OpES << Src2Shift)
143#define Src2CS (OpCS << Src2Shift)
144#define Src2SS (OpSS << Src2Shift)
145#define Src2DS (OpDS << Src2Shift)
146#define Src2FS (OpFS << Src2Shift)
147#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300148#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300149#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300150#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
151#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
152#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200153#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200154#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
Avi Kivityd0e53322010-07-29 15:11:54 +0300156#define X2(x...) x, x
157#define X3(x...) X2(x), x
158#define X4(x...) X2(x), X2(x)
159#define X5(x...) X4(x), x
160#define X6(x...) X4(x), X2(x)
161#define X7(x...) X4(x), X3(x)
162#define X8(x...) X4(x), X4(x)
163#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300164
Avi Kivitye28bbd42013-01-04 16:18:48 +0200165#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
166#define FASTOP_SIZE 8
167
168/*
169 * fastop functions have a special calling convention:
170 *
171 * dst: [rdx]:rax (in/out)
172 * src: rbx (in/out)
173 * src2: rcx (in)
174 * flags: rflags (in/out)
175 *
176 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
177 * different operand sizes can be reached by calculation, rather than a jump
178 * table (which would be bigger than the code).
179 *
180 * fastop functions are declared as taking a never-defined fastop parameter,
181 * so they can't be called from C directly.
182 */
183
184struct fastop;
185
Avi Kivityd65b1de2010-07-29 15:11:35 +0300186struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300187 u64 flags : 56;
188 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300189 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300190 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200191 const struct opcode *group;
192 const struct group_dual *gdual;
193 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200194 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300196 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200197 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300198};
199
200struct group_dual {
201 struct opcode mod012[8];
202 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203};
204
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200205struct gprefix {
206 struct opcode pfx_no;
207 struct opcode pfx_66;
208 struct opcode pfx_f2;
209 struct opcode pfx_f3;
210};
211
Gleb Natapov045a2822012-12-20 16:57:43 +0200212struct escape {
213 struct opcode op[8];
214 struct opcode high[64];
215};
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200218#define EFLG_ID (1<<21)
219#define EFLG_VIP (1<<20)
220#define EFLG_VIF (1<<19)
221#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200222#define EFLG_VM (1<<17)
223#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200224#define EFLG_IOPL (3<<12)
225#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226#define EFLG_OF (1<<11)
227#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200228#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200229#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230#define EFLG_SF (1<<7)
231#define EFLG_ZF (1<<6)
232#define EFLG_AF (1<<4)
233#define EFLG_PF (1<<2)
234#define EFLG_CF (1<<0)
235
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300236#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
237#define EFLG_RESERVED_ONE_MASK 2
238
Avi Kivitydd856ef2012-08-27 23:46:17 +0300239static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
240{
241 if (!(ctxt->regs_valid & (1 << nr))) {
242 ctxt->regs_valid |= 1 << nr;
243 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
244 }
245 return ctxt->_regs[nr];
246}
247
248static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
249{
250 ctxt->regs_valid |= 1 << nr;
251 ctxt->regs_dirty |= 1 << nr;
252 return &ctxt->_regs[nr];
253}
254
255static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
256{
257 reg_read(ctxt, nr);
258 return reg_write(ctxt, nr);
259}
260
261static void writeback_registers(struct x86_emulate_ctxt *ctxt)
262{
263 unsigned reg;
264
265 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
266 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
267}
268
269static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
270{
271 ctxt->regs_dirty = 0;
272 ctxt->regs_valid = 0;
273}
274
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275/*
276 * Instruction emulation:
277 * Most instructions are emulated directly via a fragment of inline assembly
278 * code. This allows us to save/restore EFLAGS and thus very easily pick up
279 * any modified flags.
280 */
281
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800282#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283#define _LO32 "k" /* force 32-bit operand */
284#define _STK "%%rsp" /* stack pointer */
285#elif defined(__i386__)
286#define _LO32 "" /* force 32-bit operand */
287#define _STK "%%esp" /* stack pointer */
288#endif
289
290/*
291 * These EFLAGS bits are restored from saved value during emulation, and
292 * any changes are written back to the saved value after emulation.
293 */
294#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
295
296/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200297#define _PRE_EFLAGS(_sav, _msk, _tmp) \
298 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
299 "movl %"_sav",%"_LO32 _tmp"; " \
300 "push %"_tmp"; " \
301 "push %"_tmp"; " \
302 "movl %"_msk",%"_LO32 _tmp"; " \
303 "andl %"_LO32 _tmp",("_STK"); " \
304 "pushf; " \
305 "notl %"_LO32 _tmp"; " \
306 "andl %"_LO32 _tmp",("_STK"); " \
307 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
308 "pop %"_tmp"; " \
309 "orl %"_LO32 _tmp",("_STK"); " \
310 "popf; " \
311 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313/* After executing instruction: write-back necessary bits in EFLAGS. */
314#define _POST_EFLAGS(_sav, _msk, _tmp) \
315 /* _sav |= EFLAGS & _msk; */ \
316 "pushf; " \
317 "pop %"_tmp"; " \
318 "andl %"_msk",%"_LO32 _tmp"; " \
319 "orl %"_LO32 _tmp",%"_sav"; "
320
Avi Kivitydda96d82008-11-26 15:14:10 +0200321#ifdef CONFIG_X86_64
322#define ON64(x) x
323#else
324#define ON64(x)
325#endif
326
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300327#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200328 do { \
329 __asm__ __volatile__ ( \
330 _PRE_EFLAGS("0", "4", "2") \
331 _op _suffix " %"_x"3,%1; " \
332 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300333 : "=m" ((ctxt)->eflags), \
334 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200335 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300336 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200337 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200338
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300341#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200342 do { \
343 unsigned long _tmp; \
344 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300345 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200346 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300347 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200348 break; \
349 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300350 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200351 break; \
352 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300353 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200354 break; \
355 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 } while (0)
357
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300358#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200360 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300361 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300363 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 break; \
365 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300366 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 _wx, _wy, _lx, _ly, _qx, _qy); \
368 break; \
369 } \
370 } while (0)
371
372/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300373#define emulate_2op_SrcB(ctxt, _op) \
374 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375
376/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300377#define emulate_2op_SrcV(ctxt, _op) \
378 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379
380/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300381#define emulate_2op_SrcV_nobyte(ctxt, _op) \
382 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100384/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300385#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300386 do { \
387 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300388 _type _clv = (ctxt)->src2.val; \
389 _type _srcv = (ctxt)->src.val; \
390 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300391 \
392 __asm__ __volatile__ ( \
393 _PRE_EFLAGS("0", "5", "2") \
394 _op _suffix " %4,%1 \n" \
395 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300396 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300397 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
398 ); \
399 \
Avi Kivity761441b2011-09-07 16:41:36 +0300400 (ctxt)->src2.val = (unsigned long) _clv; \
401 (ctxt)->src2.val = (unsigned long) _srcv; \
402 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100403 } while (0)
404
Avi Kivity761441b2011-09-07 16:41:36 +0300405#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300406 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300407 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300408 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300409 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300410 break; \
411 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300412 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300413 break; \
414 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300415 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300416 break; \
417 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100418 } while (0)
419
Avi Kivityd1eef452011-09-07 16:41:38 +0300420#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421 do { \
422 unsigned long _tmp; \
423 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200424 __asm__ __volatile__ ( \
425 _PRE_EFLAGS("0", "3", "2") \
426 _op _suffix " %1; " \
427 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300428 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200429 "=&r" (_tmp) \
430 : "i" (EFLAGS_MASK)); \
431 } while (0)
432
433/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300434#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200435 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300436 switch ((ctxt)->dst.bytes) { \
437 case 1: __emulate_1op(ctxt, _op, "b"); break; \
438 case 2: __emulate_1op(ctxt, _op, "w"); break; \
439 case 4: __emulate_1op(ctxt, _op, "l"); break; \
440 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 } \
442 } while (0)
443
Avi Kivityb7d491e2013-01-04 16:18:49 +0200444#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
445#define FOP_RET "ret \n\t"
446
447#define FOP_START(op) \
448 extern void em_##op(struct fastop *fake); \
449 asm(".pushsection .text, \"ax\" \n\t" \
450 ".global em_" #op " \n\t" \
451 FOP_ALIGN \
452 "em_" #op ": \n\t"
453
454#define FOP_END \
455 ".popsection")
456
Avi Kivity0bdea062013-01-19 19:51:50 +0200457#define FOPNOP() FOP_ALIGN FOP_RET
458
Avi Kivityb7d491e2013-01-04 16:18:49 +0200459#define FOP1E(op, dst) \
460 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
461
462#define FASTOP1(op) \
463 FOP_START(op) \
464 FOP1E(op##b, al) \
465 FOP1E(op##w, ax) \
466 FOP1E(op##l, eax) \
467 ON64(FOP1E(op##q, rax)) \
468 FOP_END
469
Avi Kivityf7857f32013-01-04 16:18:53 +0200470#define FOP2E(op, dst, src) \
471 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
472
473#define FASTOP2(op) \
474 FOP_START(op) \
475 FOP2E(op##b, al, bl) \
476 FOP2E(op##w, ax, bx) \
477 FOP2E(op##l, eax, ebx) \
478 ON64(FOP2E(op##q, rax, rbx)) \
479 FOP_END
480
Avi Kivity0bdea062013-01-19 19:51:50 +0200481#define FOP3E(op, dst, src, src2) \
482 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
483
484/* 3-operand, word-only, src2=cl */
485#define FASTOP3WCL(op) \
486 FOP_START(op) \
487 FOPNOP() \
488 FOP3E(op##w, ax, bx, cl) \
489 FOP3E(op##l, eax, ebx, cl) \
490 ON64(FOP3E(op##q, rax, rbx, cl)) \
491 FOP_END
492
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300493#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300494 do { \
495 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300496 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
497 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300498 \
499 __asm__ __volatile__ ( \
500 _PRE_EFLAGS("0", "5", "1") \
501 "1: \n\t" \
502 _op _suffix " %6; " \
503 "2: \n\t" \
504 _POST_EFLAGS("0", "5", "1") \
505 ".pushsection .fixup,\"ax\" \n\t" \
506 "3: movb $1, %4 \n\t" \
507 "jmp 2b \n\t" \
508 ".popsection \n\t" \
509 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300510 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
511 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800512 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300513 } while (0)
514
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300515/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300516#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300517 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300518 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300519 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300520 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300521 break; \
522 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300523 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300524 break; \
525 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300526 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300527 break; \
528 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300529 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300530 break; \
531 } \
532 } while (0)
533
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200534static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
535 enum x86_intercept intercept,
536 enum x86_intercept_stage stage)
537{
538 struct x86_instruction_info info = {
539 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300540 .rep_prefix = ctxt->rep_prefix,
541 .modrm_mod = ctxt->modrm_mod,
542 .modrm_reg = ctxt->modrm_reg,
543 .modrm_rm = ctxt->modrm_rm,
544 .src_val = ctxt->src.val64,
545 .src_bytes = ctxt->src.bytes,
546 .dst_bytes = ctxt->dst.bytes,
547 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200548 .next_rip = ctxt->eip,
549 };
550
Avi Kivity29535382011-04-20 13:37:53 +0300551 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200552}
553
Avi Kivityf47cfa32012-06-07 17:49:24 +0300554static void assign_masked(ulong *dest, ulong src, ulong mask)
555{
556 *dest = (*dest & ~mask) | (src & mask);
557}
558
Avi Kivity9dac77f2011-06-01 15:34:25 +0300559static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800560{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300561 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800562}
563
Avi Kivityf47cfa32012-06-07 17:49:24 +0300564static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
565{
566 u16 sel;
567 struct desc_struct ss;
568
569 if (ctxt->mode == X86EMUL_MODE_PROT64)
570 return ~0UL;
571 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
572 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
573}
574
Avi Kivity612e89f2012-06-12 20:03:23 +0300575static int stack_size(struct x86_emulate_ctxt *ctxt)
576{
577 return (__fls(stack_mask(ctxt)) + 1) >> 3;
578}
579
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800581static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300582address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800583{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300584 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800585 return reg;
586 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300587 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800588}
589
590static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300591register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800592{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300593 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800594}
595
Avi Kivity5ad105e2012-08-19 14:34:31 +0300596static void masked_increment(ulong *reg, ulong mask, int inc)
597{
598 assign_masked(reg, *reg + inc, mask);
599}
600
Harvey Harrison7a9572752008-02-19 07:40:41 -0800601static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300602register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800603{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300604 ulong mask;
605
Avi Kivity9dac77f2011-06-01 15:34:25 +0300606 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300607 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800608 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300609 mask = ad_mask(ctxt);
610 masked_increment(reg, mask, inc);
611}
612
613static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
614{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300615 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800616}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617
Avi Kivity9dac77f2011-06-01 15:34:25 +0300618static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800619{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300620 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800621}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300622
Avi Kivity56697682011-04-03 14:08:51 +0300623static u32 desc_limit_scaled(struct desc_struct *desc)
624{
625 u32 limit = get_desc_limit(desc);
626
627 return desc->g ? (limit << 12) | 0xfff : limit;
628}
629
Avi Kivity9dac77f2011-06-01 15:34:25 +0300630static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300631{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300632 ctxt->has_seg_override = true;
633 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300634}
635
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900636static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300637{
638 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
639 return 0;
640
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900641 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300642}
643
Avi Kivity9dac77f2011-06-01 15:34:25 +0300644static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300645{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300646 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300647 return 0;
648
Avi Kivity9dac77f2011-06-01 15:34:25 +0300649 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300650}
651
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200652static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
653 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300654{
Avi Kivityda9cb572010-11-22 17:53:21 +0200655 ctxt->exception.vector = vec;
656 ctxt->exception.error_code = error;
657 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200658 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300659}
660
Joerg Roedel3b88e412011-04-04 12:39:29 +0200661static int emulate_db(struct x86_emulate_ctxt *ctxt)
662{
663 return emulate_exception(ctxt, DB_VECTOR, 0, false);
664}
665
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200666static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300667{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200668 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300669}
670
Avi Kivity618ff152011-04-03 12:32:09 +0300671static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
672{
673 return emulate_exception(ctxt, SS_VECTOR, err, true);
674}
675
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200676static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300677{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200678 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300679}
680
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200681static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300682{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200683 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300684}
685
Avi Kivity34d1f492010-08-26 11:59:01 +0300686static int emulate_de(struct x86_emulate_ctxt *ctxt)
687{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200688 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300689}
690
Avi Kivity12537912011-03-29 11:41:27 +0200691static int emulate_nm(struct x86_emulate_ctxt *ctxt)
692{
693 return emulate_exception(ctxt, NM_VECTOR, 0, false);
694}
695
Avi Kivity1aa36612011-04-27 13:20:30 +0300696static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
697{
698 u16 selector;
699 struct desc_struct desc;
700
701 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
702 return selector;
703}
704
705static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
706 unsigned seg)
707{
708 u16 dummy;
709 u32 base3;
710 struct desc_struct desc;
711
712 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
713 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
714}
715
Avi Kivity1c11b372012-04-09 18:39:59 +0300716/*
717 * x86 defines three classes of vector instructions: explicitly
718 * aligned, explicitly unaligned, and the rest, which change behaviour
719 * depending on whether they're AVX encoded or not.
720 *
721 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
722 * subject to the same check.
723 */
724static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
725{
726 if (likely(size < 16))
727 return false;
728
729 if (ctxt->d & Aligned)
730 return true;
731 else if (ctxt->d & Unaligned)
732 return false;
733 else if (ctxt->d & Avx)
734 return false;
735 else
736 return true;
737}
738
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400739static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300740 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400741 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300742 ulong *linear)
743{
Avi Kivity618ff152011-04-03 12:32:09 +0300744 struct desc_struct desc;
745 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300746 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300747 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300748 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200749 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300750
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900751 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300752 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300753 case X86EMUL_MODE_PROT64:
754 if (((signed long)la << 16) >> 16 != la)
755 return emulate_gp(ctxt, 0);
756 break;
757 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300758 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
759 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300760 if (!usable)
761 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200762 /* code segment in protected mode or read-only data segment */
763 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
764 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300765 goto bad;
766 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400767 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300768 goto bad;
769 lim = desc_limit_scaled(&desc);
770 if ((desc.type & 8) || !(desc.type & 4)) {
771 /* expand-up segment */
772 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
773 goto bad;
774 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800775 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300776 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
777 goto bad;
778 lim = desc.d ? 0xffffffff : 0xffff;
779 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
780 goto bad;
781 }
Avi Kivity717746e2011-04-20 13:37:53 +0300782 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300783 if (!(desc.type & 8)) {
784 /* data segment */
785 if (cpl > desc.dpl)
786 goto bad;
787 } else if ((desc.type & 8) && !(desc.type & 4)) {
788 /* nonconforming code segment */
789 if (cpl != desc.dpl)
790 goto bad;
791 } else if ((desc.type & 8) && (desc.type & 4)) {
792 /* conforming code segment */
793 if (cpl < desc.dpl)
794 goto bad;
795 }
796 break;
797 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300798 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300799 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300800 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
801 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300802 *linear = la;
803 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300804bad:
805 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300806 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300807 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300808 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300809}
810
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400811static int linearize(struct x86_emulate_ctxt *ctxt,
812 struct segmented_address addr,
813 unsigned size, bool write,
814 ulong *linear)
815{
816 return __linearize(ctxt, addr, size, write, false, linear);
817}
818
819
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200820static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
821 struct segmented_address addr,
822 void *data,
823 unsigned size)
824{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200825 int rc;
826 ulong linear;
827
Avi Kivity83b87952011-04-03 11:31:19 +0300828 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200829 if (rc != X86EMUL_CONTINUE)
830 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300831 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200832}
833
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900834/*
835 * Fetch the next byte of the instruction being emulated which is pointed to
836 * by ctxt->_eip, then increment ctxt->_eip.
837 *
838 * Also prefetch the remaining bytes of the instruction without crossing page
839 * boundary if they are not in fetch_cache yet.
840 */
841static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200842{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300843 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200844 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300845 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200846
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900847 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400848 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900849 struct segmented_address addr = { .seg = VCPU_SREG_CS,
850 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300851 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900852 size = min(15UL - cur_size,
853 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400854 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900855 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400856 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900857 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
858 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900859 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200860 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300861 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200862 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900863 *dest = fc->data[ctxt->_eip - fc->start];
864 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900865 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200866}
867
868static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900869 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200870{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900871 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200872
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200873 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900874 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200875 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200876 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900877 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900878 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200879 return rc;
880 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900881 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200882}
883
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900884/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900885#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900886({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900887 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900888 if (rc != X86EMUL_CONTINUE) \
889 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900890 (_type)_x; \
891})
892
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900893#define insn_fetch_arr(_arr, _size, _ctxt) \
894({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900895 if (rc != X86EMUL_CONTINUE) \
896 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900897})
898
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000899/*
900 * Given the 'reg' portion of a ModRM byte, and a register block, return a
901 * pointer into the block that addresses the relevant register.
902 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
903 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300904static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000905 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906{
907 void *p;
908
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300910 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
911 else
912 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 return p;
914}
915
916static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200917 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 u16 *size, unsigned long *address, int op_bytes)
919{
920 int rc;
921
922 if (op_bytes == 2)
923 op_bytes = 3;
924 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200925 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900926 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200928 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200929 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 return rc;
931}
932
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300933static int test_cc(unsigned int condition, unsigned int flags)
934{
935 int rc = 0;
936
937 switch ((condition & 15) >> 1) {
938 case 0: /* o */
939 rc |= (flags & EFLG_OF);
940 break;
941 case 1: /* b/c/nae */
942 rc |= (flags & EFLG_CF);
943 break;
944 case 2: /* z/e */
945 rc |= (flags & EFLG_ZF);
946 break;
947 case 3: /* be/na */
948 rc |= (flags & (EFLG_CF|EFLG_ZF));
949 break;
950 case 4: /* s */
951 rc |= (flags & EFLG_SF);
952 break;
953 case 5: /* p/pe */
954 rc |= (flags & EFLG_PF);
955 break;
956 case 7: /* le/ng */
957 rc |= (flags & EFLG_ZF);
958 /* fall through */
959 case 6: /* l/nge */
960 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
961 break;
962 }
963
964 /* Odd condition identifiers (lsb == 1) have inverted sense. */
965 return (!!rc ^ (condition & 1));
966}
967
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300968static void fetch_register_operand(struct operand *op)
969{
970 switch (op->bytes) {
971 case 1:
972 op->val = *(u8 *)op->addr.reg;
973 break;
974 case 2:
975 op->val = *(u16 *)op->addr.reg;
976 break;
977 case 4:
978 op->val = *(u32 *)op->addr.reg;
979 break;
980 case 8:
981 op->val = *(u64 *)op->addr.reg;
982 break;
983 }
984}
985
Avi Kivity12537912011-03-29 11:41:27 +0200986static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
987{
988 ctxt->ops->get_fpu(ctxt);
989 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200990 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
991 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
992 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
993 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
994 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
995 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
996 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
997 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200998#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200999 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1000 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1001 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1002 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1003 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1004 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1005 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1006 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001007#endif
1008 default: BUG();
1009 }
1010 ctxt->ops->put_fpu(ctxt);
1011}
1012
1013static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1014 int reg)
1015{
1016 ctxt->ops->get_fpu(ctxt);
1017 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001018 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1019 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1020 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1021 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1022 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1023 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1024 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1025 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001026#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001027 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1028 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1029 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1030 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1031 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1032 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1033 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1034 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001035#endif
1036 default: BUG();
1037 }
1038 ctxt->ops->put_fpu(ctxt);
1039}
1040
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001041static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1042{
1043 ctxt->ops->get_fpu(ctxt);
1044 switch (reg) {
1045 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1046 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1047 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1048 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1049 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1050 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1051 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1052 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1053 default: BUG();
1054 }
1055 ctxt->ops->put_fpu(ctxt);
1056}
1057
1058static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1059{
1060 ctxt->ops->get_fpu(ctxt);
1061 switch (reg) {
1062 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1063 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1064 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1065 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1066 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1067 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1068 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1069 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1070 default: BUG();
1071 }
1072 ctxt->ops->put_fpu(ctxt);
1073}
1074
Gleb Natapov045a2822012-12-20 16:57:43 +02001075static int em_fninit(struct x86_emulate_ctxt *ctxt)
1076{
1077 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1078 return emulate_nm(ctxt);
1079
1080 ctxt->ops->get_fpu(ctxt);
1081 asm volatile("fninit");
1082 ctxt->ops->put_fpu(ctxt);
1083 return X86EMUL_CONTINUE;
1084}
1085
1086static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1087{
1088 u16 fcw;
1089
1090 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1091 return emulate_nm(ctxt);
1092
1093 ctxt->ops->get_fpu(ctxt);
1094 asm volatile("fnstcw %0": "+m"(fcw));
1095 ctxt->ops->put_fpu(ctxt);
1096
1097 /* force 2 byte destination */
1098 ctxt->dst.bytes = 2;
1099 ctxt->dst.val = fcw;
1100
1101 return X86EMUL_CONTINUE;
1102}
1103
1104static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1105{
1106 u16 fsw;
1107
1108 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1109 return emulate_nm(ctxt);
1110
1111 ctxt->ops->get_fpu(ctxt);
1112 asm volatile("fnstsw %0": "+m"(fsw));
1113 ctxt->ops->put_fpu(ctxt);
1114
1115 /* force 2 byte destination */
1116 ctxt->dst.bytes = 2;
1117 ctxt->dst.val = fsw;
1118
1119 return X86EMUL_CONTINUE;
1120}
1121
Avi Kivity12537912011-03-29 11:41:27 +02001122static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001123 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001124{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001125 unsigned reg = ctxt->modrm_reg;
1126 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001127
Avi Kivity9dac77f2011-06-01 15:34:25 +03001128 if (!(ctxt->d & ModRM))
1129 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001130
Avi Kivity9dac77f2011-06-01 15:34:25 +03001131 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001132 op->type = OP_XMM;
1133 op->bytes = 16;
1134 op->addr.xmm = reg;
1135 read_sse_reg(ctxt, &op->vec_val, reg);
1136 return;
1137 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001138 if (ctxt->d & Mmx) {
1139 reg &= 7;
1140 op->type = OP_MM;
1141 op->bytes = 8;
1142 op->addr.mm = reg;
1143 return;
1144 }
Avi Kivity12537912011-03-29 11:41:27 +02001145
Avi Kivity3c118e22007-10-31 10:27:04 +02001146 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001147 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001148 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001149 op->bytes = 1;
1150 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001151 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001153 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001154 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001155 op->orig_val = op->val;
1156}
1157
Avi Kivitya6e34072012-06-10 17:15:39 +03001158static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1159{
1160 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1161 ctxt->modrm_seg = VCPU_SREG_SS;
1162}
1163
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001164static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001165 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001166{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001167 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001168 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001169 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001170 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001171
Avi Kivity9dac77f2011-06-01 15:34:25 +03001172 if (ctxt->rex_prefix) {
1173 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1174 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1175 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176 }
1177
Avi Kivity9dac77f2011-06-01 15:34:25 +03001178 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1179 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1180 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1181 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182
Avi Kivity9dac77f2011-06-01 15:34:25 +03001183 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001184 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001186 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001187 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001188 op->type = OP_XMM;
1189 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001190 op->addr.xmm = ctxt->modrm_rm;
1191 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001192 return rc;
1193 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001194 if (ctxt->d & Mmx) {
1195 op->type = OP_MM;
1196 op->bytes = 8;
1197 op->addr.xmm = ctxt->modrm_rm & 7;
1198 return rc;
1199 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001200 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001201 return rc;
1202 }
1203
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001204 op->type = OP_MEM;
1205
Avi Kivity9dac77f2011-06-01 15:34:25 +03001206 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001207 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1208 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1209 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1210 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211
1212 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001214 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001215 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001216 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217 break;
1218 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001219 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001220 break;
1221 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001222 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223 break;
1224 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001225 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001227 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001228 break;
1229 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001230 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001231 break;
1232 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001233 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001234 break;
1235 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001236 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001237 break;
1238 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001239 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001240 break;
1241 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001242 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001243 break;
1244 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001245 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001246 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001247 break;
1248 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001249 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001250 break;
1251 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001252 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1253 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1254 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001255 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 } else {
1257 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001258 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001259 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001260 index_reg |= (sib >> 3) & 7;
1261 base_reg |= sib & 7;
1262 scale = sib >> 6;
1263
Avi Kivity9dac77f2011-06-01 15:34:25 +03001264 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001265 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001266 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001267 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001268 adjust_modrm_seg(ctxt, base_reg);
1269 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001270 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001271 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001272 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001273 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001275 } else {
1276 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001277 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001278 adjust_modrm_seg(ctxt, base_reg);
1279 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001281 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001282 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001283 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001284 break;
1285 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001286 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001287 break;
1288 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001289 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001290 break;
1291 }
1292 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001293 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001294done:
1295 return rc;
1296}
1297
1298static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001299 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001300{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001301 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001302
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001303 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001304 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001305 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001306 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001307 break;
1308 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001309 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001310 break;
1311 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001312 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001313 break;
1314 }
1315done:
1316 return rc;
1317}
1318
Avi Kivity9dac77f2011-06-01 15:34:25 +03001319static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001320{
Sheng Yang7129eec2010-09-28 16:33:32 +08001321 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001322
Avi Kivity9dac77f2011-06-01 15:34:25 +03001323 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1324 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001325
Avi Kivity9dac77f2011-06-01 15:34:25 +03001326 if (ctxt->src.bytes == 2)
1327 sv = (s16)ctxt->src.val & (s16)mask;
1328 else if (ctxt->src.bytes == 4)
1329 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001330
Avi Kivity9dac77f2011-06-01 15:34:25 +03001331 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001332 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001333
1334 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001335 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001336}
1337
Gleb Natapov9de41572010-04-28 19:15:22 +03001338static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001339 unsigned long addr, void *dest, unsigned size)
1340{
1341 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001342 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001343
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001344 if (mc->pos < mc->end)
1345 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001346
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001347 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001348
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001349 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1350 &ctxt->exception);
1351 if (rc != X86EMUL_CONTINUE)
1352 return rc;
1353
1354 mc->end += size;
1355
1356read_cached:
1357 memcpy(dest, mc->data + mc->pos, size);
1358 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001359 return X86EMUL_CONTINUE;
1360}
1361
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001362static int segmented_read(struct x86_emulate_ctxt *ctxt,
1363 struct segmented_address addr,
1364 void *data,
1365 unsigned size)
1366{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001367 int rc;
1368 ulong linear;
1369
Avi Kivity83b87952011-04-03 11:31:19 +03001370 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001371 if (rc != X86EMUL_CONTINUE)
1372 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001373 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001374}
1375
1376static int segmented_write(struct x86_emulate_ctxt *ctxt,
1377 struct segmented_address addr,
1378 const void *data,
1379 unsigned size)
1380{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001381 int rc;
1382 ulong linear;
1383
Avi Kivity83b87952011-04-03 11:31:19 +03001384 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001385 if (rc != X86EMUL_CONTINUE)
1386 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001387 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1388 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001389}
1390
1391static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1392 struct segmented_address addr,
1393 const void *orig_data, const void *data,
1394 unsigned size)
1395{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001396 int rc;
1397 ulong linear;
1398
Avi Kivity83b87952011-04-03 11:31:19 +03001399 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001400 if (rc != X86EMUL_CONTINUE)
1401 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001402 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1403 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001404}
1405
Gleb Natapov7b262e92010-03-18 15:20:27 +02001406static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001407 unsigned int size, unsigned short port,
1408 void *dest)
1409{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001410 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001411
1412 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001413 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001414 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001415 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001416 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001417 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1418 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001419 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1420 count);
1421 if (n == 0)
1422 n = 1;
1423 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001424 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001425 return 0;
1426 rc->end = n * size;
1427 }
1428
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001429 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1430 ctxt->dst.data = rc->data + rc->pos;
1431 ctxt->dst.type = OP_MEM_STR;
1432 ctxt->dst.count = (rc->end - rc->pos) / size;
1433 rc->pos = rc->end;
1434 } else {
1435 memcpy(dest, rc->data + rc->pos, size);
1436 rc->pos += size;
1437 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001438 return 1;
1439}
1440
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001441static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1442 u16 index, struct desc_struct *desc)
1443{
1444 struct desc_ptr dt;
1445 ulong addr;
1446
1447 ctxt->ops->get_idt(ctxt, &dt);
1448
1449 if (dt.size < index * 8 + 7)
1450 return emulate_gp(ctxt, index << 3 | 0x2);
1451
1452 addr = dt.address + index * 8;
1453 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1454 &ctxt->exception);
1455}
1456
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001457static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458 u16 selector, struct desc_ptr *dt)
1459{
Mathias Krause0225fb52012-08-30 01:30:16 +02001460 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001461
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001462 if (selector & 1 << 2) {
1463 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001464 u16 sel;
1465
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001467 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468 return;
1469
1470 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1471 dt->address = get_desc_base(&desc);
1472 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001473 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001474}
1475
1476/* allowed just for 8 bytes segments */
1477static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001478 u16 selector, struct desc_struct *desc,
1479 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001480{
1481 struct desc_ptr dt;
1482 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483 ulong addr;
1484
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001485 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001487 if (dt.size < index * 8 + 7)
1488 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001489
Avi Kivitye9194642012-06-13 16:29:39 +03001490 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001491 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1492 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493}
1494
1495/* allowed just for 8 bytes segments */
1496static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001497 u16 selector, struct desc_struct *desc)
1498{
1499 struct desc_ptr dt;
1500 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001501 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001502
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001503 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001505 if (dt.size < index * 8 + 7)
1506 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001507
1508 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001509 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1510 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001511}
1512
Gleb Natapov5601d052011-03-07 14:55:06 +02001513/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001514static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001515 u16 selector, int seg)
1516{
Avi Kivity869be992012-06-13 16:30:53 +03001517 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001518 u8 dpl, rpl, cpl;
1519 unsigned err_vec = GP_VECTOR;
1520 u32 err_code = 0;
1521 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001522 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001524 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001525
1526 memset(&seg_desc, 0, sizeof seg_desc);
1527
1528 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1529 || ctxt->mode == X86EMUL_MODE_REAL) {
1530 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001531 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001532 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001533 goto load;
1534 }
1535
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001536 rpl = selector & 3;
1537 cpl = ctxt->ops->cpl(ctxt);
1538
1539 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1540 if ((seg == VCPU_SREG_CS
1541 || (seg == VCPU_SREG_SS
1542 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1543 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 && null_selector)
1545 goto exception;
1546
1547 /* TR should be in GDT only */
1548 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1549 goto exception;
1550
1551 if (null_selector) /* for NULL selector skip all following checks */
1552 goto load;
1553
Avi Kivitye9194642012-06-13 16:29:39 +03001554 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001555 if (ret != X86EMUL_CONTINUE)
1556 return ret;
1557
1558 err_code = selector & 0xfffc;
1559 err_vec = GP_VECTOR;
1560
Guo Chaofc058682012-06-28 15:19:51 +08001561 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001562 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1563 goto exception;
1564
1565 if (!seg_desc.p) {
1566 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1567 goto exception;
1568 }
1569
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001570 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571
1572 switch (seg) {
1573 case VCPU_SREG_SS:
1574 /*
1575 * segment is not a writable data segment or segment
1576 * selector's RPL != CPL or segment selector's RPL != CPL
1577 */
1578 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1579 goto exception;
1580 break;
1581 case VCPU_SREG_CS:
1582 if (!(seg_desc.type & 8))
1583 goto exception;
1584
1585 if (seg_desc.type & 4) {
1586 /* conforming */
1587 if (dpl > cpl)
1588 goto exception;
1589 } else {
1590 /* nonconforming */
1591 if (rpl > cpl || dpl != cpl)
1592 goto exception;
1593 }
1594 /* CS(RPL) <- CPL */
1595 selector = (selector & 0xfffc) | cpl;
1596 break;
1597 case VCPU_SREG_TR:
1598 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1599 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001600 old_desc = seg_desc;
1601 seg_desc.type |= 2; /* busy */
1602 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1603 sizeof(seg_desc), &ctxt->exception);
1604 if (ret != X86EMUL_CONTINUE)
1605 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001606 break;
1607 case VCPU_SREG_LDTR:
1608 if (seg_desc.s || seg_desc.type != 2)
1609 goto exception;
1610 break;
1611 default: /* DS, ES, FS, or GS */
1612 /*
1613 * segment is not a data or readable code segment or
1614 * ((segment is a data or nonconforming code segment)
1615 * and (both RPL and CPL > DPL))
1616 */
1617 if ((seg_desc.type & 0xa) == 0x8 ||
1618 (((seg_desc.type & 0xc) != 0xc) &&
1619 (rpl > dpl && cpl > dpl)))
1620 goto exception;
1621 break;
1622 }
1623
1624 if (seg_desc.s) {
1625 /* mark segment as accessed */
1626 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001627 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001628 if (ret != X86EMUL_CONTINUE)
1629 return ret;
1630 }
1631load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001632 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001633 return X86EMUL_CONTINUE;
1634exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001635 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001636 return X86EMUL_PROPAGATE_FAULT;
1637}
1638
Wei Yongjun31be40b2010-08-17 09:17:30 +08001639static void write_register_operand(struct operand *op)
1640{
1641 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1642 switch (op->bytes) {
1643 case 1:
1644 *(u8 *)op->addr.reg = (u8)op->val;
1645 break;
1646 case 2:
1647 *(u16 *)op->addr.reg = (u16)op->val;
1648 break;
1649 case 4:
1650 *op->addr.reg = (u32)op->val;
1651 break; /* 64b: zero-extend */
1652 case 8:
1653 *op->addr.reg = op->val;
1654 break;
1655 }
1656}
1657
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001658static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001659{
1660 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001661
Avi Kivityb6744dc2013-01-04 16:18:50 +02001662 if (ctxt->d & NoWrite)
1663 return X86EMUL_CONTINUE;
1664
Avi Kivity9dac77f2011-06-01 15:34:25 +03001665 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001666 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001667 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001668 break;
1669 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001670 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001671 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001672 ctxt->dst.addr.mem,
1673 &ctxt->dst.orig_val,
1674 &ctxt->dst.val,
1675 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001676 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001677 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001678 ctxt->dst.addr.mem,
1679 &ctxt->dst.val,
1680 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001681 if (rc != X86EMUL_CONTINUE)
1682 return rc;
1683 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001684 case OP_MEM_STR:
1685 rc = segmented_write(ctxt,
1686 ctxt->dst.addr.mem,
1687 ctxt->dst.data,
1688 ctxt->dst.bytes * ctxt->dst.count);
1689 if (rc != X86EMUL_CONTINUE)
1690 return rc;
1691 break;
Avi Kivity12537912011-03-29 11:41:27 +02001692 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001693 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001694 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001695 case OP_MM:
1696 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1697 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001698 case OP_NONE:
1699 /* no writeback */
1700 break;
1701 default:
1702 break;
1703 }
1704 return X86EMUL_CONTINUE;
1705}
1706
Avi Kivity51ddff52012-06-12 20:19:40 +03001707static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001708{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001709 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710
Avi Kivity5ad105e2012-08-19 14:34:31 +03001711 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001712 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001713 addr.seg = VCPU_SREG_SS;
1714
Avi Kivity51ddff52012-06-12 20:19:40 +03001715 return segmented_write(ctxt, addr, data, bytes);
1716}
1717
1718static int em_push(struct x86_emulate_ctxt *ctxt)
1719{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001720 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001721 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001722 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001723}
1724
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001725static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001726 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001727{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001728 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001729 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001730
Avi Kivitydd856ef2012-08-27 23:46:17 +03001731 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001732 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001733 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001734 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001735 return rc;
1736
Avi Kivity5ad105e2012-08-19 14:34:31 +03001737 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001738 return rc;
1739}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001740
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001741static int em_pop(struct x86_emulate_ctxt *ctxt)
1742{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001743 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001744}
1745
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001746static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001747 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001748{
1749 int rc;
1750 unsigned long val, change_mask;
1751 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001752 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001753
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001754 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001755 if (rc != X86EMUL_CONTINUE)
1756 return rc;
1757
1758 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1759 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1760
1761 switch(ctxt->mode) {
1762 case X86EMUL_MODE_PROT64:
1763 case X86EMUL_MODE_PROT32:
1764 case X86EMUL_MODE_PROT16:
1765 if (cpl == 0)
1766 change_mask |= EFLG_IOPL;
1767 if (cpl <= iopl)
1768 change_mask |= EFLG_IF;
1769 break;
1770 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001771 if (iopl < 3)
1772 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001773 change_mask |= EFLG_IF;
1774 break;
1775 default: /* real mode */
1776 change_mask |= (EFLG_IOPL | EFLG_IF);
1777 break;
1778 }
1779
1780 *(unsigned long *)dest =
1781 (ctxt->eflags & ~change_mask) | (val & change_mask);
1782
1783 return rc;
1784}
1785
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001786static int em_popf(struct x86_emulate_ctxt *ctxt)
1787{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001788 ctxt->dst.type = OP_REG;
1789 ctxt->dst.addr.reg = &ctxt->eflags;
1790 ctxt->dst.bytes = ctxt->op_bytes;
1791 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001792}
1793
Avi Kivity612e89f2012-06-12 20:03:23 +03001794static int em_enter(struct x86_emulate_ctxt *ctxt)
1795{
1796 int rc;
1797 unsigned frame_size = ctxt->src.val;
1798 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001799 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001800
1801 if (nesting_level)
1802 return X86EMUL_UNHANDLEABLE;
1803
Avi Kivitydd856ef2012-08-27 23:46:17 +03001804 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1805 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001806 if (rc != X86EMUL_CONTINUE)
1807 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001808 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001809 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001810 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1811 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001812 stack_mask(ctxt));
1813 return X86EMUL_CONTINUE;
1814}
1815
Avi Kivityf47cfa32012-06-07 17:49:24 +03001816static int em_leave(struct x86_emulate_ctxt *ctxt)
1817{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001818 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001819 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001820 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001821}
1822
Avi Kivity1cd196e2011-09-13 10:45:51 +03001823static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001824{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001825 int seg = ctxt->src2.val;
1826
Avi Kivity9dac77f2011-06-01 15:34:25 +03001827 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001828
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001829 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001830}
1831
Avi Kivity1cd196e2011-09-13 10:45:51 +03001832static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001833{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001834 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001835 unsigned long selector;
1836 int rc;
1837
Avi Kivity9dac77f2011-06-01 15:34:25 +03001838 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001839 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001840 return rc;
1841
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001842 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001843 return rc;
1844}
1845
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001846static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001847{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001848 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001849 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001850 int reg = VCPU_REGS_RAX;
1851
1852 while (reg <= VCPU_REGS_RDI) {
1853 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001854 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001855
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001856 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001857 if (rc != X86EMUL_CONTINUE)
1858 return rc;
1859
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001860 ++reg;
1861 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001862
Wei Yongjunc37eda12010-06-15 09:03:33 +08001863 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001864}
1865
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001866static int em_pushf(struct x86_emulate_ctxt *ctxt)
1867{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001868 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001869 return em_push(ctxt);
1870}
1871
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001872static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001873{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001874 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001875 int reg = VCPU_REGS_RDI;
1876
1877 while (reg >= VCPU_REGS_RAX) {
1878 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001879 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001880 --reg;
1881 }
1882
Avi Kivitydd856ef2012-08-27 23:46:17 +03001883 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001884 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001885 break;
1886 --reg;
1887 }
1888 return rc;
1889}
1890
Avi Kivitydd856ef2012-08-27 23:46:17 +03001891static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001892{
Mathias Krause0225fb52012-08-30 01:30:16 +02001893 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001894 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001895 struct desc_ptr dt;
1896 gva_t cs_addr;
1897 gva_t eip_addr;
1898 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001899
1900 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001901 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001902 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001903 if (rc != X86EMUL_CONTINUE)
1904 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001905
1906 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1907
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001909 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001910 if (rc != X86EMUL_CONTINUE)
1911 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001912
Avi Kivity9dac77f2011-06-01 15:34:25 +03001913 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001914 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001915 if (rc != X86EMUL_CONTINUE)
1916 return rc;
1917
Avi Kivity4bff1e862011-04-20 13:37:53 +03001918 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001919
1920 eip_addr = dt.address + (irq << 2);
1921 cs_addr = dt.address + (irq << 2) + 2;
1922
Avi Kivity0f65dd72011-04-20 13:37:53 +03001923 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001924 if (rc != X86EMUL_CONTINUE)
1925 return rc;
1926
Avi Kivity0f65dd72011-04-20 13:37:53 +03001927 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001928 if (rc != X86EMUL_CONTINUE)
1929 return rc;
1930
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001931 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001932 if (rc != X86EMUL_CONTINUE)
1933 return rc;
1934
Avi Kivity9dac77f2011-06-01 15:34:25 +03001935 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001936
1937 return rc;
1938}
1939
Avi Kivitydd856ef2012-08-27 23:46:17 +03001940int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1941{
1942 int rc;
1943
1944 invalidate_registers(ctxt);
1945 rc = __emulate_int_real(ctxt, irq);
1946 if (rc == X86EMUL_CONTINUE)
1947 writeback_registers(ctxt);
1948 return rc;
1949}
1950
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001951static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001952{
1953 switch(ctxt->mode) {
1954 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001955 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001956 case X86EMUL_MODE_VM86:
1957 case X86EMUL_MODE_PROT16:
1958 case X86EMUL_MODE_PROT32:
1959 case X86EMUL_MODE_PROT64:
1960 default:
1961 /* Protected mode interrupts unimplemented yet */
1962 return X86EMUL_UNHANDLEABLE;
1963 }
1964}
1965
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001966static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001967{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001968 int rc = X86EMUL_CONTINUE;
1969 unsigned long temp_eip = 0;
1970 unsigned long temp_eflags = 0;
1971 unsigned long cs = 0;
1972 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1973 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1974 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1975 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1976
1977 /* TODO: Add stack limit check */
1978
Avi Kivity9dac77f2011-06-01 15:34:25 +03001979 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001980
1981 if (rc != X86EMUL_CONTINUE)
1982 return rc;
1983
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001984 if (temp_eip & ~0xffff)
1985 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001986
Avi Kivity9dac77f2011-06-01 15:34:25 +03001987 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001988
1989 if (rc != X86EMUL_CONTINUE)
1990 return rc;
1991
Avi Kivity9dac77f2011-06-01 15:34:25 +03001992 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001993
1994 if (rc != X86EMUL_CONTINUE)
1995 return rc;
1996
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001997 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001998
1999 if (rc != X86EMUL_CONTINUE)
2000 return rc;
2001
Avi Kivity9dac77f2011-06-01 15:34:25 +03002002 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002003
2004
Avi Kivity9dac77f2011-06-01 15:34:25 +03002005 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002006 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002007 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002008 ctxt->eflags &= ~0xffff;
2009 ctxt->eflags |= temp_eflags;
2010 }
2011
2012 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2013 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2014
2015 return rc;
2016}
2017
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002018static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002019{
2020 switch(ctxt->mode) {
2021 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002022 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002023 case X86EMUL_MODE_VM86:
2024 case X86EMUL_MODE_PROT16:
2025 case X86EMUL_MODE_PROT32:
2026 case X86EMUL_MODE_PROT64:
2027 default:
2028 /* iret from protected mode unimplemented yet */
2029 return X86EMUL_UNHANDLEABLE;
2030 }
2031}
2032
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002033static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2034{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002035 int rc;
2036 unsigned short sel;
2037
Avi Kivity9dac77f2011-06-01 15:34:25 +03002038 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002039
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002040 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002041 if (rc != X86EMUL_CONTINUE)
2042 return rc;
2043
Avi Kivity9dac77f2011-06-01 15:34:25 +03002044 ctxt->_eip = 0;
2045 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002046 return X86EMUL_CONTINUE;
2047}
2048
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002049static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002050{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002051 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002052 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002053 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002054 break;
2055 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002056 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002057 break;
2058 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002059 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002060 break;
2061 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002062 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002063 break;
2064 case 4: /* sal/shl */
2065 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002066 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002067 break;
2068 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002069 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002070 break;
2071 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002072 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002073 break;
2074 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002075 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002076}
2077
Avi Kivity45a14672013-01-04 16:18:52 +02002078FASTOP1(not);
2079FASTOP1(neg);
Avi Kivity3329ece2011-09-13 10:45:39 +03002080
2081static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2082{
2083 u8 ex = 0;
2084
2085 emulate_1op_rax_rdx(ctxt, "mul", ex);
2086 return X86EMUL_CONTINUE;
2087}
2088
2089static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2090{
2091 u8 ex = 0;
2092
2093 emulate_1op_rax_rdx(ctxt, "imul", ex);
2094 return X86EMUL_CONTINUE;
2095}
2096
2097static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002098{
Avi Kivity34d1f492010-08-26 11:59:01 +03002099 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002100
Avi Kivity3329ece2011-09-13 10:45:39 +03002101 emulate_1op_rax_rdx(ctxt, "div", de);
2102 if (de)
2103 return emulate_de(ctxt);
2104 return X86EMUL_CONTINUE;
2105}
2106
2107static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2108{
2109 u8 de = 0;
2110
2111 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002112 if (de)
2113 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002114 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002115}
2116
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002117static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002118{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002119 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002120
Avi Kivity9dac77f2011-06-01 15:34:25 +03002121 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002122 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03002123 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124 break;
2125 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03002126 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002127 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03002128 case 2: /* call near abs */ {
2129 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002130 old_eip = ctxt->_eip;
2131 ctxt->_eip = ctxt->src.val;
2132 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002133 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002134 break;
2135 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002136 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002137 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002138 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002139 case 5: /* jmp far */
2140 rc = em_jmp_far(ctxt);
2141 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002142 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002143 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002144 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002146 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147}
2148
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002149static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002150{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002151 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002152
Avi Kivitydd856ef2012-08-27 23:46:17 +03002153 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2154 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2155 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2156 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002157 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002158 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002159 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2160 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002161
Laurent Vivier05f086f2007-09-24 11:10:55 +02002162 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002163 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002164 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002165}
2166
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002167static int em_ret(struct x86_emulate_ctxt *ctxt)
2168{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002169 ctxt->dst.type = OP_REG;
2170 ctxt->dst.addr.reg = &ctxt->_eip;
2171 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002172 return em_pop(ctxt);
2173}
2174
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002175static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002176{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002177 int rc;
2178 unsigned long cs;
2179
Avi Kivity9dac77f2011-06-01 15:34:25 +03002180 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002181 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002182 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002183 if (ctxt->op_bytes == 4)
2184 ctxt->_eip = (u32)ctxt->_eip;
2185 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002186 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002187 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002188 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002189 return rc;
2190}
2191
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002192static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2193{
2194 /* Save real source value, then compare EAX against destination. */
2195 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002196 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002197 emulate_2op_SrcV(ctxt, "cmp");
2198
2199 if (ctxt->eflags & EFLG_ZF) {
2200 /* Success: write back to memory. */
2201 ctxt->dst.val = ctxt->src.orig_val;
2202 } else {
2203 /* Failure: write the value we saw to EAX. */
2204 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002205 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002206 }
2207 return X86EMUL_CONTINUE;
2208}
2209
Avi Kivityd4b43252011-09-13 10:45:50 +03002210static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002211{
Avi Kivityd4b43252011-09-13 10:45:50 +03002212 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002213 unsigned short sel;
2214 int rc;
2215
Avi Kivity9dac77f2011-06-01 15:34:25 +03002216 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002217
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002218 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002219 if (rc != X86EMUL_CONTINUE)
2220 return rc;
2221
Avi Kivity9dac77f2011-06-01 15:34:25 +03002222 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002223 return rc;
2224}
2225
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002226static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002227setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002228 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002229{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002230 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002231 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002232 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002233 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002234 cs->type = 0x0b; /* Read, Execute, Accessed */
2235 cs->s = 1;
2236 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002237 cs->p = 1;
2238 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002239 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002240
Gleb Natapov79168fd2010-04-28 19:15:30 +03002241 set_desc_base(ss, 0); /* flat segment */
2242 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002243 ss->g = 1; /* 4kb granularity */
2244 ss->s = 1;
2245 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002246 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002247 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002248 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002249 ss->l = 0;
2250 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002251}
2252
Avi Kivity1a18a692012-02-01 12:23:21 +02002253static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2254{
2255 u32 eax, ebx, ecx, edx;
2256
2257 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002258 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2259 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002260 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2261 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2262}
2263
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002264static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2265{
Mathias Krause0225fb52012-08-30 01:30:16 +02002266 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002267 u32 eax, ebx, ecx, edx;
2268
2269 /*
2270 * syscall should always be enabled in longmode - so only become
2271 * vendor specific (cpuid) if other modes are active...
2272 */
2273 if (ctxt->mode == X86EMUL_MODE_PROT64)
2274 return true;
2275
2276 eax = 0x00000000;
2277 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002278 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2279 /*
2280 * Intel ("GenuineIntel")
2281 * remark: Intel CPUs only support "syscall" in 64bit
2282 * longmode. Also an 64bit guest with a
2283 * 32bit compat-app running will #UD !! While this
2284 * behaviour can be fixed (by emulating) into AMD
2285 * response - CPUs of AMD can't behave like Intel.
2286 */
2287 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2288 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2289 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2290 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002291
Avi Kivity0017f932012-06-07 14:10:16 +03002292 /* AMD ("AuthenticAMD") */
2293 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2294 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2295 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2296 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002297
Avi Kivity0017f932012-06-07 14:10:16 +03002298 /* AMD ("AMDisbetter!") */
2299 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2300 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2301 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2302 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002303
2304 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2305 return false;
2306}
2307
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002308static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002309{
Mathias Krause0225fb52012-08-30 01:30:16 +02002310 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002311 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002312 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002313 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002314 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002315
2316 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002317 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002318 ctxt->mode == X86EMUL_MODE_VM86)
2319 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002320
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002321 if (!(em_syscall_is_enabled(ctxt)))
2322 return emulate_ud(ctxt);
2323
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002324 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002325 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002326
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002327 if (!(efer & EFER_SCE))
2328 return emulate_ud(ctxt);
2329
Avi Kivity717746e2011-04-20 13:37:53 +03002330 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002331 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002332 cs_sel = (u16)(msr_data & 0xfffc);
2333 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002334
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002335 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002336 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002337 cs.l = 1;
2338 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002339 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2340 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002341
Avi Kivitydd856ef2012-08-27 23:46:17 +03002342 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002343 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002344#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002345 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002346
Avi Kivity717746e2011-04-20 13:37:53 +03002347 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002348 ctxt->mode == X86EMUL_MODE_PROT64 ?
2349 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002350 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002351
Avi Kivity717746e2011-04-20 13:37:53 +03002352 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002353 ctxt->eflags &= ~(msr_data | EFLG_RF);
2354#endif
2355 } else {
2356 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002357 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002358 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002359
2360 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2361 }
2362
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002363 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002364}
2365
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002366static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002367{
Mathias Krause0225fb52012-08-30 01:30:16 +02002368 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002369 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002370 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002371 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002372 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002373
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002374 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002375 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002376 if (ctxt->mode == X86EMUL_MODE_REAL)
2377 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002378
Avi Kivity1a18a692012-02-01 12:23:21 +02002379 /*
2380 * Not recognized on AMD in compat mode (but is recognized in legacy
2381 * mode).
2382 */
2383 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2384 && !vendor_intel(ctxt))
2385 return emulate_ud(ctxt);
2386
Andre Przywara8c604352009-06-18 12:56:01 +02002387 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2388 * Therefore, we inject an #UD.
2389 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002390 if (ctxt->mode == X86EMUL_MODE_PROT64)
2391 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002392
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002393 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002394
Avi Kivity717746e2011-04-20 13:37:53 +03002395 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002396 switch (ctxt->mode) {
2397 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002398 if ((msr_data & 0xfffc) == 0x0)
2399 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002400 break;
2401 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002402 if (msr_data == 0x0)
2403 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002404 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002405 default:
2406 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002407 }
2408
2409 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002410 cs_sel = (u16)msr_data;
2411 cs_sel &= ~SELECTOR_RPL_MASK;
2412 ss_sel = cs_sel + 8;
2413 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002414 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002415 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002416 cs.l = 1;
2417 }
2418
Avi Kivity1aa36612011-04-27 13:20:30 +03002419 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2420 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002421
Avi Kivity717746e2011-04-20 13:37:53 +03002422 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002423 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002424
Avi Kivity717746e2011-04-20 13:37:53 +03002425 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002426 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002427
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002428 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002429}
2430
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002431static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002432{
Mathias Krause0225fb52012-08-30 01:30:16 +02002433 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002434 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002435 u64 msr_data;
2436 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002437 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002438
Gleb Natapova0044752010-02-10 14:21:31 +02002439 /* inject #GP if in real mode or Virtual 8086 mode */
2440 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002441 ctxt->mode == X86EMUL_MODE_VM86)
2442 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002443
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002444 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002445
Avi Kivity9dac77f2011-06-01 15:34:25 +03002446 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002447 usermode = X86EMUL_MODE_PROT64;
2448 else
2449 usermode = X86EMUL_MODE_PROT32;
2450
2451 cs.dpl = 3;
2452 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002453 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002454 switch (usermode) {
2455 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002456 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002457 if ((msr_data & 0xfffc) == 0x0)
2458 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002459 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002460 break;
2461 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002462 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002463 if (msr_data == 0x0)
2464 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002465 ss_sel = cs_sel + 8;
2466 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002467 cs.l = 1;
2468 break;
2469 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002470 cs_sel |= SELECTOR_RPL_MASK;
2471 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002472
Avi Kivity1aa36612011-04-27 13:20:30 +03002473 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2474 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002475
Avi Kivitydd856ef2012-08-27 23:46:17 +03002476 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2477 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002478
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002479 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002480}
2481
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002482static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002483{
2484 int iopl;
2485 if (ctxt->mode == X86EMUL_MODE_REAL)
2486 return false;
2487 if (ctxt->mode == X86EMUL_MODE_VM86)
2488 return true;
2489 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002490 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002491}
2492
2493static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002494 u16 port, u16 len)
2495{
Mathias Krause0225fb52012-08-30 01:30:16 +02002496 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002497 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002498 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002499 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002500 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002501 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002502 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002503
Avi Kivity1aa36612011-04-27 13:20:30 +03002504 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002505 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002506 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002507 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002508 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002509 base = get_desc_base(&tr_seg);
2510#ifdef CONFIG_X86_64
2511 base |= ((u64)base3) << 32;
2512#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002513 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002514 if (r != X86EMUL_CONTINUE)
2515 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002516 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002517 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002518 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002519 if (r != X86EMUL_CONTINUE)
2520 return false;
2521 if ((perm >> bit_idx) & mask)
2522 return false;
2523 return true;
2524}
2525
2526static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002527 u16 port, u16 len)
2528{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002529 if (ctxt->perm_ok)
2530 return true;
2531
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002532 if (emulator_bad_iopl(ctxt))
2533 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002534 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002535
2536 ctxt->perm_ok = true;
2537
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002538 return true;
2539}
2540
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002541static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002542 struct tss_segment_16 *tss)
2543{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002544 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002546 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2547 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2548 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2549 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2550 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2551 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2552 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2553 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002554
Avi Kivity1aa36612011-04-27 13:20:30 +03002555 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2556 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2557 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2558 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2559 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002560}
2561
2562static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563 struct tss_segment_16 *tss)
2564{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565 int ret;
2566
Avi Kivity9dac77f2011-06-01 15:34:25 +03002567 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002568 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002569 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2570 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2571 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2572 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2573 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2574 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2575 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2576 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577
2578 /*
2579 * SDM says that segment selectors are loaded before segment
2580 * descriptors
2581 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002582 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2583 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2584 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2585 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2586 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587
2588 /*
Guo Chaofc058682012-06-28 15:19:51 +08002589 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590 * it is handled in a context of new task
2591 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002592 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 if (ret != X86EMUL_CONTINUE)
2594 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002595 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 if (ret != X86EMUL_CONTINUE)
2597 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002598 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599 if (ret != X86EMUL_CONTINUE)
2600 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002601 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002602 if (ret != X86EMUL_CONTINUE)
2603 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002604 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 if (ret != X86EMUL_CONTINUE)
2606 return ret;
2607
2608 return X86EMUL_CONTINUE;
2609}
2610
2611static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 u16 tss_selector, u16 old_tss_sel,
2613 ulong old_tss_base, struct desc_struct *new_desc)
2614{
Mathias Krause0225fb52012-08-30 01:30:16 +02002615 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 struct tss_segment_16 tss_seg;
2617 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002618 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619
Avi Kivity0f65dd72011-04-20 13:37:53 +03002620 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002621 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002622 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002626 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627
Avi Kivity0f65dd72011-04-20 13:37:53 +03002628 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002629 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002630 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633
Avi Kivity0f65dd72011-04-20 13:37:53 +03002634 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002635 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002636 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002639
2640 if (old_tss_sel != 0xffff) {
2641 tss_seg.prev_task_link = old_tss_sel;
2642
Avi Kivity0f65dd72011-04-20 13:37:53 +03002643 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644 &tss_seg.prev_task_link,
2645 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002646 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002647 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002650 }
2651
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002652 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653}
2654
2655static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002656 struct tss_segment_32 *tss)
2657{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002658 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002659 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002661 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2662 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2663 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2664 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2665 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2666 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2667 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2668 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669
Avi Kivity1aa36612011-04-27 13:20:30 +03002670 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2671 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2672 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2673 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2674 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2675 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2676 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677}
2678
2679static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 struct tss_segment_32 *tss)
2681{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682 int ret;
2683
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002684 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002685 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002686 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002687 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002688
2689 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002690 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2691 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2692 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2693 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2694 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2695 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2696 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2697 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002698
2699 /*
2700 * SDM says that segment selectors are loaded before segment
2701 * descriptors
2702 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002703 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2704 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2705 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2706 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2707 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2708 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2709 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710
2711 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002712 * If we're switching between Protected Mode and VM86, we need to make
2713 * sure to update the mode before loading the segment descriptors so
2714 * that the selectors are interpreted correctly.
2715 *
2716 * Need to get rflags to the vcpu struct immediately because it
2717 * influences the CPL which is checked at least when loading the segment
2718 * descriptors and when pushing an error code to the new kernel stack.
2719 *
2720 * TODO Introduce a separate ctxt->ops->set_cpl callback
2721 */
2722 if (ctxt->eflags & X86_EFLAGS_VM)
2723 ctxt->mode = X86EMUL_MODE_VM86;
2724 else
2725 ctxt->mode = X86EMUL_MODE_PROT32;
2726
2727 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2728
2729 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 * Now load segment descriptors. If fault happenes at this stage
2731 * it is handled in a context of new task
2732 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002733 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002734 if (ret != X86EMUL_CONTINUE)
2735 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002736 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737 if (ret != X86EMUL_CONTINUE)
2738 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002739 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740 if (ret != X86EMUL_CONTINUE)
2741 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002742 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002743 if (ret != X86EMUL_CONTINUE)
2744 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002745 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746 if (ret != X86EMUL_CONTINUE)
2747 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002748 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002749 if (ret != X86EMUL_CONTINUE)
2750 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002751 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002752 if (ret != X86EMUL_CONTINUE)
2753 return ret;
2754
2755 return X86EMUL_CONTINUE;
2756}
2757
2758static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759 u16 tss_selector, u16 old_tss_sel,
2760 ulong old_tss_base, struct desc_struct *new_desc)
2761{
Mathias Krause0225fb52012-08-30 01:30:16 +02002762 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763 struct tss_segment_32 tss_seg;
2764 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002765 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766
Avi Kivity0f65dd72011-04-20 13:37:53 +03002767 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002768 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002769 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002773 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774
Avi Kivity0f65dd72011-04-20 13:37:53 +03002775 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002776 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002777 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780
Avi Kivity0f65dd72011-04-20 13:37:53 +03002781 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002782 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002783 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002786
2787 if (old_tss_sel != 0xffff) {
2788 tss_seg.prev_task_link = old_tss_sel;
2789
Avi Kivity0f65dd72011-04-20 13:37:53 +03002790 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791 &tss_seg.prev_task_link,
2792 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002793 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002794 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002797 }
2798
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002799 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800}
2801
2802static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002803 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002804 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805{
Mathias Krause0225fb52012-08-30 01:30:16 +02002806 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807 struct desc_struct curr_tss_desc, next_tss_desc;
2808 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002809 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002810 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002811 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002812 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002813 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002814
2815 /* FIXME: old_tss_base == ~0 ? */
2816
Avi Kivitye9194642012-06-13 16:29:39 +03002817 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002818 if (ret != X86EMUL_CONTINUE)
2819 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002820 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002821 if (ret != X86EMUL_CONTINUE)
2822 return ret;
2823
2824 /* FIXME: check that next_tss_desc is tss */
2825
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002826 /*
2827 * Check privileges. The three cases are task switch caused by...
2828 *
2829 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2830 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002831 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002832 */
2833 if (reason == TASK_SWITCH_GATE) {
2834 if (idt_index != -1) {
2835 /* Software interrupts */
2836 struct desc_struct task_gate_desc;
2837 int dpl;
2838
2839 ret = read_interrupt_descriptor(ctxt, idt_index,
2840 &task_gate_desc);
2841 if (ret != X86EMUL_CONTINUE)
2842 return ret;
2843
2844 dpl = task_gate_desc.dpl;
2845 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2846 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2847 }
2848 } else if (reason != TASK_SWITCH_IRET) {
2849 int dpl = next_tss_desc.dpl;
2850 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2851 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002852 }
2853
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002854
Gleb Natapovceffb452010-03-18 15:20:19 +02002855 desc_limit = desc_limit_scaled(&next_tss_desc);
2856 if (!next_tss_desc.p ||
2857 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2858 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002859 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002860 return X86EMUL_PROPAGATE_FAULT;
2861 }
2862
2863 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2864 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002865 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002866 }
2867
2868 if (reason == TASK_SWITCH_IRET)
2869 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2870
2871 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002872 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002873 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2874 old_tss_sel = 0xffff;
2875
2876 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002877 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002878 old_tss_base, &next_tss_desc);
2879 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002880 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002881 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002882 if (ret != X86EMUL_CONTINUE)
2883 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002884
2885 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2886 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2887
2888 if (reason != TASK_SWITCH_IRET) {
2889 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002890 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002891 }
2892
Avi Kivity717746e2011-04-20 13:37:53 +03002893 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002894 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002895
Jan Kiszkae269fb22010-04-14 15:51:09 +02002896 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002897 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2898 ctxt->lock_prefix = 0;
2899 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002900 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002901 }
2902
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002903 return ret;
2904}
2905
2906int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002907 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002908 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002910 int rc;
2911
Avi Kivitydd856ef2012-08-27 23:46:17 +03002912 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002913 ctxt->_eip = ctxt->eip;
2914 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002915
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002916 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002917 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002918
Avi Kivitydd856ef2012-08-27 23:46:17 +03002919 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002920 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002921 writeback_registers(ctxt);
2922 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002923
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002924 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002925}
2926
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002927static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2928 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002929{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002930 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002931
Avi Kivitydd856ef2012-08-27 23:46:17 +03002932 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2933 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002934}
2935
Avi Kivity7af04fc2010-08-18 14:16:35 +03002936static int em_das(struct x86_emulate_ctxt *ctxt)
2937{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002938 u8 al, old_al;
2939 bool af, cf, old_cf;
2940
2941 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002942 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002943
2944 old_al = al;
2945 old_cf = cf;
2946 cf = false;
2947 af = ctxt->eflags & X86_EFLAGS_AF;
2948 if ((al & 0x0f) > 9 || af) {
2949 al -= 6;
2950 cf = old_cf | (al >= 250);
2951 af = true;
2952 } else {
2953 af = false;
2954 }
2955 if (old_al > 0x99 || old_cf) {
2956 al -= 0x60;
2957 cf = true;
2958 }
2959
Avi Kivity9dac77f2011-06-01 15:34:25 +03002960 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002961 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002962 ctxt->src.type = OP_IMM;
2963 ctxt->src.val = 0;
2964 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002965 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002966 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2967 if (cf)
2968 ctxt->eflags |= X86_EFLAGS_CF;
2969 if (af)
2970 ctxt->eflags |= X86_EFLAGS_AF;
2971 return X86EMUL_CONTINUE;
2972}
2973
Gleb Natapov7f662272012-12-10 11:42:30 +02002974static int em_aad(struct x86_emulate_ctxt *ctxt)
2975{
2976 u8 al = ctxt->dst.val & 0xff;
2977 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2978
2979 al = (al + (ah * ctxt->src.val)) & 0xff;
2980
2981 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2982
2983 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2984
2985 if (!al)
2986 ctxt->eflags |= X86_EFLAGS_ZF;
2987 if (!(al & 1))
2988 ctxt->eflags |= X86_EFLAGS_PF;
2989 if (al & 0x80)
2990 ctxt->eflags |= X86_EFLAGS_SF;
2991
2992 return X86EMUL_CONTINUE;
2993}
2994
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002995static int em_call(struct x86_emulate_ctxt *ctxt)
2996{
2997 long rel = ctxt->src.val;
2998
2999 ctxt->src.val = (unsigned long)ctxt->_eip;
3000 jmp_rel(ctxt, rel);
3001 return em_push(ctxt);
3002}
3003
Avi Kivity0ef753b2010-08-18 14:51:45 +03003004static int em_call_far(struct x86_emulate_ctxt *ctxt)
3005{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003006 u16 sel, old_cs;
3007 ulong old_eip;
3008 int rc;
3009
Avi Kivity1aa36612011-04-27 13:20:30 +03003010 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003011 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003012
Avi Kivity9dac77f2011-06-01 15:34:25 +03003013 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003014 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003015 return X86EMUL_CONTINUE;
3016
Avi Kivity9dac77f2011-06-01 15:34:25 +03003017 ctxt->_eip = 0;
3018 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003019
Avi Kivity9dac77f2011-06-01 15:34:25 +03003020 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003021 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003022 if (rc != X86EMUL_CONTINUE)
3023 return rc;
3024
Avi Kivity9dac77f2011-06-01 15:34:25 +03003025 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003026 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003027}
3028
Avi Kivity40ece7c2010-08-18 15:12:09 +03003029static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3030{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003031 int rc;
3032
Avi Kivity9dac77f2011-06-01 15:34:25 +03003033 ctxt->dst.type = OP_REG;
3034 ctxt->dst.addr.reg = &ctxt->_eip;
3035 ctxt->dst.bytes = ctxt->op_bytes;
3036 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003037 if (rc != X86EMUL_CONTINUE)
3038 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003039 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003040 return X86EMUL_CONTINUE;
3041}
3042
Avi Kivityfb864fb2013-01-04 16:18:54 +02003043FASTOP2(add);
3044FASTOP2(or);
3045FASTOP2(adc);
3046FASTOP2(sbb);
3047FASTOP2(and);
3048FASTOP2(sub);
3049FASTOP2(xor);
3050FASTOP2(cmp);
3051FASTOP2(test);
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003052
Avi Kivity0bdea062013-01-19 19:51:50 +02003053FASTOP3WCL(shld);
3054FASTOP3WCL(shrd);
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)
Avi Kivityf7857f32013-01-04 16:18:53 +02003683#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003684#define I2bvIP(_f, _e, _i, _p) \
3685 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003686
Avi Kivityfb864fb2013-01-04 16:18:54 +02003687#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3688 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3689 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003690
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003691static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003692 DI(SrcNone | Priv, monitor),
3693 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003694 N, N, N, N, N, N,
3695};
3696
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003697static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003698 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3699 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3700 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3701 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3702 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3703 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3704 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3705 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003706};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003707
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003708static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003709 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003710 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003711 N, N, N, N, N, N,
3712};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003713
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003714static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003715 F(Lock, em_add),
3716 F(Lock | PageTable, em_or),
3717 F(Lock, em_adc),
3718 F(Lock, em_sbb),
3719 F(Lock | PageTable, em_and),
3720 F(Lock, em_sub),
3721 F(Lock, em_xor),
3722 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003723};
3724
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003725static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003726 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003727};
3728
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003729static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003730 F(DstMem | SrcImm | NoWrite, em_test),
3731 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003732 F(DstMem | SrcNone | Lock, em_not),
3733 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003734 I(SrcMem, em_mul_ex),
3735 I(SrcMem, em_imul_ex),
3736 I(SrcMem, em_div_ex),
3737 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003738};
3739
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003740static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003741 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3742 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003743 N, N, N, N, N, N,
3744};
3745
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003746static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003747 I(DstMem | SrcNone | Lock, em_grp45),
3748 I(DstMem | SrcNone | Lock, em_grp45),
3749 I(SrcMem | Stack, em_grp45),
3750 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3751 I(SrcMem | Stack, em_grp45),
3752 I(SrcMemFAddr | ImplicitOps, em_grp45),
3753 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003754};
3755
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003756static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003757 DI(Prot, sldt),
3758 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003759 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003760 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003761 N, N, N, N,
3762};
3763
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003764static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003765 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3766 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003767 II(SrcMem | Priv, em_lgdt, lgdt),
3768 II(SrcMem | Priv, em_lidt, lidt),
3769 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3770 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3771 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003772}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003773 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003774 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003775 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003776 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3777 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3778 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003779} };
3780
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003781static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003782 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003783 I(DstMem | SrcImmByte, em_bt),
3784 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3785 I(DstMem | SrcImmByte | Lock, em_btr),
3786 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003787};
3788
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003789static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003790 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003791}, {
3792 N, N, N, N, N, N, N, N,
3793} };
3794
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003795static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003796 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003797 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003798};
3799
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003800static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003801 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003802};
3803
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003804static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003805 I(0, em_mov), N, N, N,
3806};
3807
Gleb Natapov045a2822012-12-20 16:57:43 +02003808static const struct escape escape_d9 = { {
3809 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3810}, {
3811 /* 0xC0 - 0xC7 */
3812 N, N, N, N, N, N, N, N,
3813 /* 0xC8 - 0xCF */
3814 N, N, N, N, N, N, N, N,
3815 /* 0xD0 - 0xC7 */
3816 N, N, N, N, N, N, N, N,
3817 /* 0xD8 - 0xDF */
3818 N, N, N, N, N, N, N, N,
3819 /* 0xE0 - 0xE7 */
3820 N, N, N, N, N, N, N, N,
3821 /* 0xE8 - 0xEF */
3822 N, N, N, N, N, N, N, N,
3823 /* 0xF0 - 0xF7 */
3824 N, N, N, N, N, N, N, N,
3825 /* 0xF8 - 0xFF */
3826 N, N, N, N, N, N, N, N,
3827} };
3828
3829static const struct escape escape_db = { {
3830 N, N, N, N, N, N, N, N,
3831}, {
3832 /* 0xC0 - 0xC7 */
3833 N, N, N, N, N, N, N, N,
3834 /* 0xC8 - 0xCF */
3835 N, N, N, N, N, N, N, N,
3836 /* 0xD0 - 0xC7 */
3837 N, N, N, N, N, N, N, N,
3838 /* 0xD8 - 0xDF */
3839 N, N, N, N, N, N, N, N,
3840 /* 0xE0 - 0xE7 */
3841 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3842 /* 0xE8 - 0xEF */
3843 N, N, N, N, N, N, N, N,
3844 /* 0xF0 - 0xF7 */
3845 N, N, N, N, N, N, N, N,
3846 /* 0xF8 - 0xFF */
3847 N, N, N, N, N, N, N, N,
3848} };
3849
3850static const struct escape escape_dd = { {
3851 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3852}, {
3853 /* 0xC0 - 0xC7 */
3854 N, N, N, N, N, N, N, N,
3855 /* 0xC8 - 0xCF */
3856 N, N, N, N, N, N, N, N,
3857 /* 0xD0 - 0xC7 */
3858 N, N, N, N, N, N, N, N,
3859 /* 0xD8 - 0xDF */
3860 N, N, N, N, N, N, N, N,
3861 /* 0xE0 - 0xE7 */
3862 N, N, N, N, N, N, N, N,
3863 /* 0xE8 - 0xEF */
3864 N, N, N, N, N, N, N, N,
3865 /* 0xF0 - 0xF7 */
3866 N, N, N, N, N, N, N, N,
3867 /* 0xF8 - 0xFF */
3868 N, N, N, N, N, N, N, N,
3869} };
3870
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003871static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003872 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003873 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003874 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3875 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003876 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003877 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003878 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3879 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003880 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003881 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003882 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3883 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003884 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003885 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003886 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3887 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003888 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003889 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003890 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003891 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003892 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003893 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003894 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003895 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003896 /* 0x40 - 0x4F */
3897 X16(D(DstReg)),
3898 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003899 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003900 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003901 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003902 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003903 I(ImplicitOps | Stack | No64, em_pusha),
3904 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003905 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3906 N, N, N, N,
3907 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003908 I(SrcImm | Mov | Stack, em_push),
3909 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003910 I(SrcImmByte | Mov | Stack, em_push),
3911 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003912 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003913 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003914 /* 0x70 - 0x7F */
3915 X16(D(SrcImmByte)),
3916 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003917 G(ByteOp | DstMem | SrcImm, group1),
3918 G(DstMem | SrcImm, group1),
3919 G(ByteOp | DstMem | SrcImm | No64, group1),
3920 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003921 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003922 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003924 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003925 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003926 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003927 D(ModRM | SrcMem | NoAccess | DstReg),
3928 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3929 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003931 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003932 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003933 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003934 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003935 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003936 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003937 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003938 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003939 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003940 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003941 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003943 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003944 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3945 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003946 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003947 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003948 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003950 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003952 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003953 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003954 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003955 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3956 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003957 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003959 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3960 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003961 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003962 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003963 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003964 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Gleb Natapov7f662272012-12-10 11:42:30 +02003965 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003967 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003968 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003969 X3(I(SrcImmByte, em_loop)),
3970 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003971 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3972 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003974 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003975 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003976 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3977 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003979 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003980 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3981 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003982 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003983 D(ImplicitOps), D(ImplicitOps),
3984 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003985 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3986};
3987
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003988static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003989 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003990 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003991 N, I(ImplicitOps | VendorSpecific, em_syscall),
3992 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003993 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 N, D(ImplicitOps | ModRM), N, N,
3995 /* 0x10 - 0x1F */
3996 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3997 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003998 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003999 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004000 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4001 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004003 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4004 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004006 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004007 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004008 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004009 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004010 I(ImplicitOps | VendorSpecific, em_sysenter),
4011 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004012 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 N, N, N, N, N, N, N, N,
4014 /* 0x40 - 0x4F */
4015 X16(D(DstReg | SrcMem | ModRM | Mov)),
4016 /* 0x50 - 0x5F */
4017 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4018 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004019 N, N, N, N,
4020 N, N, N, N,
4021 N, N, N, N,
4022 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004023 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004024 N, N, N, N,
4025 N, N, N, N,
4026 N, N, N, N,
4027 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 /* 0x80 - 0x8F */
4029 X16(D(SrcImm)),
4030 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004031 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004032 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004033 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03004034 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004035 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4036 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004038 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004039 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004040 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004041 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4042 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004043 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004044 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004045 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004046 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004047 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004048 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4049 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004050 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004051 /* 0xB8 - 0xBF */
4052 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004053 G(BitOp, group8),
4054 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09004055 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004056 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004057 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004058 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004059 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004061 /* 0xC8 - 0xCF */
4062 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004063 /* 0xD0 - 0xDF */
4064 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4065 /* 0xE0 - 0xEF */
4066 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4067 /* 0xF0 - 0xFF */
4068 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4069};
4070
4071#undef D
4072#undef N
4073#undef G
4074#undef GD
4075#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004076#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004077#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004078
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004079#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004080#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004081#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004082#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004083#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004084
Avi Kivity9dac77f2011-06-01 15:34:25 +03004085static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004086{
4087 unsigned size;
4088
Avi Kivity9dac77f2011-06-01 15:34:25 +03004089 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004090 if (size == 8)
4091 size = 4;
4092 return size;
4093}
4094
4095static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4096 unsigned size, bool sign_extension)
4097{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004098 int rc = X86EMUL_CONTINUE;
4099
4100 op->type = OP_IMM;
4101 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004103 /* NB. Immediates are sign-extended as necessary. */
4104 switch (op->bytes) {
4105 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004106 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004107 break;
4108 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004109 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004110 break;
4111 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004112 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004113 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004114 case 8:
4115 op->val = insn_fetch(s64, ctxt);
4116 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004117 }
4118 if (!sign_extension) {
4119 switch (op->bytes) {
4120 case 1:
4121 op->val &= 0xff;
4122 break;
4123 case 2:
4124 op->val &= 0xffff;
4125 break;
4126 case 4:
4127 op->val &= 0xffffffff;
4128 break;
4129 }
4130 }
4131done:
4132 return rc;
4133}
4134
Avi Kivitya9945542011-09-13 10:45:41 +03004135static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4136 unsigned d)
4137{
4138 int rc = X86EMUL_CONTINUE;
4139
4140 switch (d) {
4141 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004142 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004143 break;
4144 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004145 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004146 break;
4147 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004148 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004149 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004150 *op = ctxt->memop;
4151 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004152 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004153 fetch_bit_operand(ctxt);
4154 op->orig_val = op->val;
4155 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004156 case OpMem64:
4157 ctxt->memop.bytes = 8;
4158 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004159 case OpAcc:
4160 op->type = OP_REG;
4161 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004162 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004163 fetch_register_operand(op);
4164 op->orig_val = op->val;
4165 break;
4166 case OpDI:
4167 op->type = OP_MEM;
4168 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4169 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004170 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004171 op->addr.mem.seg = VCPU_SREG_ES;
4172 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004173 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004174 break;
4175 case OpDX:
4176 op->type = OP_REG;
4177 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004178 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004179 fetch_register_operand(op);
4180 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004181 case OpCL:
4182 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004183 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004184 break;
4185 case OpImmByte:
4186 rc = decode_imm(ctxt, op, 1, true);
4187 break;
4188 case OpOne:
4189 op->bytes = 1;
4190 op->val = 1;
4191 break;
4192 case OpImm:
4193 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4194 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004195 case OpImm64:
4196 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4197 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004198 case OpMem8:
4199 ctxt->memop.bytes = 1;
4200 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004201 case OpMem16:
4202 ctxt->memop.bytes = 2;
4203 goto mem_common;
4204 case OpMem32:
4205 ctxt->memop.bytes = 4;
4206 goto mem_common;
4207 case OpImmU16:
4208 rc = decode_imm(ctxt, op, 2, false);
4209 break;
4210 case OpImmU:
4211 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4212 break;
4213 case OpSI:
4214 op->type = OP_MEM;
4215 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4216 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004217 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004218 op->addr.mem.seg = seg_override(ctxt);
4219 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004220 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004221 break;
4222 case OpImmFAddr:
4223 op->type = OP_IMM;
4224 op->addr.mem.ea = ctxt->_eip;
4225 op->bytes = ctxt->op_bytes + 2;
4226 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4227 break;
4228 case OpMemFAddr:
4229 ctxt->memop.bytes = ctxt->op_bytes + 2;
4230 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004231 case OpES:
4232 op->val = VCPU_SREG_ES;
4233 break;
4234 case OpCS:
4235 op->val = VCPU_SREG_CS;
4236 break;
4237 case OpSS:
4238 op->val = VCPU_SREG_SS;
4239 break;
4240 case OpDS:
4241 op->val = VCPU_SREG_DS;
4242 break;
4243 case OpFS:
4244 op->val = VCPU_SREG_FS;
4245 break;
4246 case OpGS:
4247 op->val = VCPU_SREG_GS;
4248 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004249 case OpImplicit:
4250 /* Special instructions do their own operand decoding. */
4251 default:
4252 op->type = OP_NONE; /* Disable writeback. */
4253 break;
4254 }
4255
4256done:
4257 return rc;
4258}
4259
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004260int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004261{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004262 int rc = X86EMUL_CONTINUE;
4263 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004264 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004265 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004266 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267
Avi Kivityf09ed832011-09-13 10:45:40 +03004268 ctxt->memop.type = OP_NONE;
4269 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004270 ctxt->_eip = ctxt->eip;
4271 ctxt->fetch.start = ctxt->_eip;
4272 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004273 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004274 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275
4276 switch (mode) {
4277 case X86EMUL_MODE_REAL:
4278 case X86EMUL_MODE_VM86:
4279 case X86EMUL_MODE_PROT16:
4280 def_op_bytes = def_ad_bytes = 2;
4281 break;
4282 case X86EMUL_MODE_PROT32:
4283 def_op_bytes = def_ad_bytes = 4;
4284 break;
4285#ifdef CONFIG_X86_64
4286 case X86EMUL_MODE_PROT64:
4287 def_op_bytes = 4;
4288 def_ad_bytes = 8;
4289 break;
4290#endif
4291 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004292 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004293 }
4294
Avi Kivity9dac77f2011-06-01 15:34:25 +03004295 ctxt->op_bytes = def_op_bytes;
4296 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004297
4298 /* Legacy prefixes. */
4299 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004300 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004301 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004302 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004303 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004304 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004305 break;
4306 case 0x67: /* address-size override */
4307 if (mode == X86EMUL_MODE_PROT64)
4308 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004309 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004310 else
4311 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004313 break;
4314 case 0x26: /* ES override */
4315 case 0x2e: /* CS override */
4316 case 0x36: /* SS override */
4317 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004318 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004319 break;
4320 case 0x64: /* FS override */
4321 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004323 break;
4324 case 0x40 ... 0x4f: /* REX */
4325 if (mode != X86EMUL_MODE_PROT64)
4326 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004327 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004328 continue;
4329 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004330 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004331 break;
4332 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004333 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004334 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004335 break;
4336 default:
4337 goto done_prefixes;
4338 }
4339
4340 /* Any legacy prefix after a REX prefix nullifies its effect. */
4341
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004343 }
4344
4345done_prefixes:
4346
4347 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 if (ctxt->rex_prefix & 8)
4349 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004350
4351 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004353 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004354 if (ctxt->b == 0x0f) {
4355 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004356 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004358 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004361 if (ctxt->d & ModRM)
4362 ctxt->modrm = insn_fetch(u8, ctxt);
4363
Avi Kivity9dac77f2011-06-01 15:34:25 +03004364 while (ctxt->d & GroupMask) {
4365 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004366 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004367 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004368 opcode = opcode.u.group[goffset];
4369 break;
4370 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 goffset = (ctxt->modrm >> 3) & 7;
4372 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004373 opcode = opcode.u.gdual->mod3[goffset];
4374 else
4375 opcode = opcode.u.gdual->mod012[goffset];
4376 break;
4377 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004378 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004379 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004380 break;
4381 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004382 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004383 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004384 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004385 switch (simd_prefix) {
4386 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4387 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4388 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4389 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4390 }
4391 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004392 case Escape:
4393 if (ctxt->modrm > 0xbf)
4394 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4395 else
4396 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4397 break;
Avi Kivity46561642011-04-24 14:09:59 +03004398 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004399 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004400 }
Avi Kivity46561642011-04-24 14:09:59 +03004401
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004402 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004403 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004404 }
4405
Avi Kivity9dac77f2011-06-01 15:34:25 +03004406 ctxt->execute = opcode.u.execute;
4407 ctxt->check_perm = opcode.check_perm;
4408 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004409
4410 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004411 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004412 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004413
Avi Kivity9dac77f2011-06-01 15:34:25 +03004414 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004415 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004416
Avi Kivity9dac77f2011-06-01 15:34:25 +03004417 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4418 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004419
Avi Kivity9dac77f2011-06-01 15:34:25 +03004420 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004421 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004423 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004424 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004425 }
4426
Avi Kivity9dac77f2011-06-01 15:34:25 +03004427 if (ctxt->d & Sse)
4428 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004429 else if (ctxt->d & Mmx)
4430 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004431
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004432 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004434 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 if (!ctxt->has_seg_override)
4436 set_seg_override(ctxt, ctxt->modrm_seg);
4437 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004438 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004439 if (rc != X86EMUL_CONTINUE)
4440 goto done;
4441
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 if (!ctxt->has_seg_override)
4443 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004444
Avi Kivityf09ed832011-09-13 10:45:40 +03004445 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004446
Avi Kivityf09ed832011-09-13 10:45:40 +03004447 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4448 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004449
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004450 /*
4451 * Decode and fetch the source operand: register, memory
4452 * or immediate.
4453 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004454 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004455 if (rc != X86EMUL_CONTINUE)
4456 goto done;
4457
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004458 /*
4459 * Decode and fetch the second source operand: register, memory
4460 * or immediate.
4461 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004462 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004463 if (rc != X86EMUL_CONTINUE)
4464 goto done;
4465
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004466 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004467 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004468
4469done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004470 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4471 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004472
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004473 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474}
4475
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004476bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4477{
4478 return ctxt->d & PageTable;
4479}
4480
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004481static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4482{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004483 /* The second termination condition only applies for REPE
4484 * and REPNE. Test if the repeat string operation prefix is
4485 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4486 * corresponding termination condition according to:
4487 * - if REPE/REPZ and ZF = 0 then done
4488 * - if REPNE/REPNZ and ZF = 1 then done
4489 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004490 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4491 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4492 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004493 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004495 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4496 return true;
4497
4498 return false;
4499}
4500
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004501static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4502{
4503 bool fault = false;
4504
4505 ctxt->ops->get_fpu(ctxt);
4506 asm volatile("1: fwait \n\t"
4507 "2: \n\t"
4508 ".pushsection .fixup,\"ax\" \n\t"
4509 "3: \n\t"
4510 "movb $1, %[fault] \n\t"
4511 "jmp 2b \n\t"
4512 ".popsection \n\t"
4513 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004514 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004515 ctxt->ops->put_fpu(ctxt);
4516
4517 if (unlikely(fault))
4518 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4519
4520 return X86EMUL_CONTINUE;
4521}
4522
4523static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4524 struct operand *op)
4525{
4526 if (op->type == OP_MM)
4527 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4528}
4529
Avi Kivitye28bbd42013-01-04 16:18:48 +02004530static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4531{
4532 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4533 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4534 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4535 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4536 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4537 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4538 return X86EMUL_CONTINUE;
4539}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004540
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004541int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004542{
Mathias Krause0225fb52012-08-30 01:30:16 +02004543 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004544 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004545 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004546
Avi Kivity9dac77f2011-06-01 15:34:25 +03004547 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004548
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004550 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004551 goto done;
4552 }
4553
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004554 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004555 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004556 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004557 goto done;
4558 }
4559
Avi Kivity9dac77f2011-06-01 15:34:25 +03004560 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004561 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004562 goto done;
4563 }
4564
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004565 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4566 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004567 rc = emulate_ud(ctxt);
4568 goto done;
4569 }
4570
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004571 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004572 rc = emulate_nm(ctxt);
4573 goto done;
4574 }
4575
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004576 if (ctxt->d & Mmx) {
4577 rc = flush_pending_x87_faults(ctxt);
4578 if (rc != X86EMUL_CONTINUE)
4579 goto done;
4580 /*
4581 * Now that we know the fpu is exception safe, we can fetch
4582 * operands from it.
4583 */
4584 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4585 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4586 if (!(ctxt->d & Mov))
4587 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4588 }
4589
Avi Kivity9dac77f2011-06-01 15:34:25 +03004590 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4591 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004592 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004593 if (rc != X86EMUL_CONTINUE)
4594 goto done;
4595 }
4596
Gleb Natapove92805a2010-02-10 14:21:35 +02004597 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004598 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004599 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004600 goto done;
4601 }
4602
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004603 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004604 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004605 rc = emulate_ud(ctxt);
4606 goto done;
4607 }
4608
Joerg Roedeld09beab2011-04-04 12:39:25 +02004609 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004610 if (ctxt->check_perm) {
4611 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004612 if (rc != X86EMUL_CONTINUE)
4613 goto done;
4614 }
4615
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4617 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004618 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004619 if (rc != X86EMUL_CONTINUE)
4620 goto done;
4621 }
4622
Avi Kivity9dac77f2011-06-01 15:34:25 +03004623 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004624 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004625 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004626 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004627 goto done;
4628 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004629 }
4630
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4632 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4633 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004634 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004635 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004636 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004637 }
4638
Avi Kivity9dac77f2011-06-01 15:34:25 +03004639 if (ctxt->src2.type == OP_MEM) {
4640 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4641 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004642 if (rc != X86EMUL_CONTINUE)
4643 goto done;
4644 }
4645
Avi Kivity9dac77f2011-06-01 15:34:25 +03004646 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004647 goto special_insn;
4648
4649
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004651 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4653 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004654 if (rc != X86EMUL_CONTINUE)
4655 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004656 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004657 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004658
Avi Kivity018a98d2007-11-27 19:30:56 +02004659special_insn:
4660
Avi Kivity9dac77f2011-06-01 15:34:25 +03004661 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4662 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004663 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004664 if (rc != X86EMUL_CONTINUE)
4665 goto done;
4666 }
4667
Avi Kivity9dac77f2011-06-01 15:34:25 +03004668 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004669 if (ctxt->d & Fastop) {
4670 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4671 rc = fastop(ctxt, fop);
4672 if (rc != X86EMUL_CONTINUE)
4673 goto done;
4674 goto writeback;
4675 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004677 if (rc != X86EMUL_CONTINUE)
4678 goto done;
4679 goto writeback;
4680 }
4681
Avi Kivity9dac77f2011-06-01 15:34:25 +03004682 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683 goto twobyte_insn;
4684
Avi Kivity9dac77f2011-06-01 15:34:25 +03004685 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004686 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004687 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004688 break;
4689 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004690 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004691 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004693 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004695 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004697 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 if (test_cc(ctxt->b, ctxt->eflags))
4699 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004700 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004701 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004702 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004703 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004704 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004705 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004706 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004707 rc = em_xchg(ctxt);
4708 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004709 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004710 switch (ctxt->op_bytes) {
4711 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4712 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4713 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004714 }
4715 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004716 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004717 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004718 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004719 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004720 rc = emulate_int(ctxt, 3);
4721 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004722 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004723 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004724 break;
4725 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004726 if (ctxt->eflags & EFLG_OF)
4727 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004728 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004729 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004730 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004731 break;
4732 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004733 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004734 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004735 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004736 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004737 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 jmp_rel(ctxt, ctxt->src.val);
4739 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004740 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004741 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004742 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004743 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004744 case 0xf5: /* cmc */
4745 /* complement carry flag from eflags reg */
4746 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004747 break;
4748 case 0xf8: /* clc */
4749 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004750 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004751 case 0xf9: /* stc */
4752 ctxt->eflags |= EFLG_CF;
4753 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004754 case 0xfc: /* cld */
4755 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004756 break;
4757 case 0xfd: /* std */
4758 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004759 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004760 default:
4761 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004763
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004764 if (rc != X86EMUL_CONTINUE)
4765 goto done;
4766
Avi Kivity018a98d2007-11-27 19:30:56 +02004767writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004768 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004769 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004770 goto done;
4771
Gleb Natapov5cd21912010-03-18 15:20:26 +02004772 /*
4773 * restore dst type in case the decoding will be reused
4774 * (happens for string instruction )
4775 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004776 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004777
Avi Kivity9dac77f2011-06-01 15:34:25 +03004778 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004779 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004780
Avi Kivity9dac77f2011-06-01 15:34:25 +03004781 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004782 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004783
Avi Kivity9dac77f2011-06-01 15:34:25 +03004784 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004785 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004787 if ((ctxt->d & SrcMask) == SrcSI)
4788 count = ctxt->src.count;
4789 else
4790 count = ctxt->dst.count;
4791 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4792 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004793
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004794 if (!string_insn_completed(ctxt)) {
4795 /*
4796 * Re-enter guest when pio read ahead buffer is empty
4797 * or, if it is not used, after each 1024 iteration.
4798 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004799 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004800 (r->end == 0 || r->end != r->pos)) {
4801 /*
4802 * Reset read cache. Usually happens before
4803 * decode, but since instruction is restarted
4804 * we have to do it here.
4805 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004806 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004807 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004808 return EMULATION_RESTART;
4809 }
4810 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004811 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004812 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004813
Avi Kivity9dac77f2011-06-01 15:34:25 +03004814 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004815
4816done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004817 if (rc == X86EMUL_PROPAGATE_FAULT)
4818 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004819 if (rc == X86EMUL_INTERCEPTED)
4820 return EMULATION_INTERCEPTED;
4821
Avi Kivitydd856ef2012-08-27 23:46:17 +03004822 if (rc == X86EMUL_CONTINUE)
4823 writeback_registers(ctxt);
4824
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004825 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826
4827twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004828 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004829 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004830 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004831 break;
4832 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004833 case 0x0d: /* GrpP (prefetch) */
4834 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004835 break;
4836 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004837 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004838 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004840 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004842 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004843 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4844 if (!test_cc(ctxt->b, ctxt->eflags))
4845 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004847 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004848 if (test_cc(ctxt->b, ctxt->eflags))
4849 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004850 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004851 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004853 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004854 case 0xae: /* clflush */
4855 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004857 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004858 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004859 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004862 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004863 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004866 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004867 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004868 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 ctxt->src.val = ctxt->dst.orig_val;
4870 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004871 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004872 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004873 ctxt->dst.bytes = ctxt->op_bytes;
4874 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4875 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004876 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004877 default:
4878 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004880
4881 if (rc != X86EMUL_CONTINUE)
4882 goto done;
4883
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 goto writeback;
4885
4886cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004887 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004889
4890void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4891{
4892 invalidate_registers(ctxt);
4893}
4894
4895void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4896{
4897 writeback_registers(ctxt);
4898}