blob: fe113fbb373772ac4fd171e50113bc945c4a69ca [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
457#define FOP1E(op, dst) \
458 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
459
460#define FASTOP1(op) \
461 FOP_START(op) \
462 FOP1E(op##b, al) \
463 FOP1E(op##w, ax) \
464 FOP1E(op##l, eax) \
465 ON64(FOP1E(op##q, rax)) \
466 FOP_END
467
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300468#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300469 do { \
470 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300471 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
472 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300473 \
474 __asm__ __volatile__ ( \
475 _PRE_EFLAGS("0", "5", "1") \
476 "1: \n\t" \
477 _op _suffix " %6; " \
478 "2: \n\t" \
479 _POST_EFLAGS("0", "5", "1") \
480 ".pushsection .fixup,\"ax\" \n\t" \
481 "3: movb $1, %4 \n\t" \
482 "jmp 2b \n\t" \
483 ".popsection \n\t" \
484 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300485 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
486 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800487 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300488 } while (0)
489
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300490/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300491#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300492 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300493 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300494 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300495 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300496 break; \
497 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300498 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300499 break; \
500 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300501 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300502 break; \
503 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300504 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300505 break; \
506 } \
507 } while (0)
508
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200509static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
510 enum x86_intercept intercept,
511 enum x86_intercept_stage stage)
512{
513 struct x86_instruction_info info = {
514 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300515 .rep_prefix = ctxt->rep_prefix,
516 .modrm_mod = ctxt->modrm_mod,
517 .modrm_reg = ctxt->modrm_reg,
518 .modrm_rm = ctxt->modrm_rm,
519 .src_val = ctxt->src.val64,
520 .src_bytes = ctxt->src.bytes,
521 .dst_bytes = ctxt->dst.bytes,
522 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200523 .next_rip = ctxt->eip,
524 };
525
Avi Kivity29535382011-04-20 13:37:53 +0300526 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200527}
528
Avi Kivityf47cfa32012-06-07 17:49:24 +0300529static void assign_masked(ulong *dest, ulong src, ulong mask)
530{
531 *dest = (*dest & ~mask) | (src & mask);
532}
533
Avi Kivity9dac77f2011-06-01 15:34:25 +0300534static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800535{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300536 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800537}
538
Avi Kivityf47cfa32012-06-07 17:49:24 +0300539static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
540{
541 u16 sel;
542 struct desc_struct ss;
543
544 if (ctxt->mode == X86EMUL_MODE_PROT64)
545 return ~0UL;
546 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
547 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
548}
549
Avi Kivity612e89f2012-06-12 20:03:23 +0300550static int stack_size(struct x86_emulate_ctxt *ctxt)
551{
552 return (__fls(stack_mask(ctxt)) + 1) >> 3;
553}
554
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800556static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300557address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800558{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300559 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800560 return reg;
561 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300562 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800563}
564
565static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300566register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800567{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300568 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800569}
570
Avi Kivity5ad105e2012-08-19 14:34:31 +0300571static void masked_increment(ulong *reg, ulong mask, int inc)
572{
573 assign_masked(reg, *reg + inc, mask);
574}
575
Harvey Harrison7a9572752008-02-19 07:40:41 -0800576static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300577register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800578{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300579 ulong mask;
580
Avi Kivity9dac77f2011-06-01 15:34:25 +0300581 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300582 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800583 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300584 mask = ad_mask(ctxt);
585 masked_increment(reg, mask, inc);
586}
587
588static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
589{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300590 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800591}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592
Avi Kivity9dac77f2011-06-01 15:34:25 +0300593static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800594{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300595 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800596}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300597
Avi Kivity56697682011-04-03 14:08:51 +0300598static u32 desc_limit_scaled(struct desc_struct *desc)
599{
600 u32 limit = get_desc_limit(desc);
601
602 return desc->g ? (limit << 12) | 0xfff : limit;
603}
604
Avi Kivity9dac77f2011-06-01 15:34:25 +0300605static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300606{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300607 ctxt->has_seg_override = true;
608 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300609}
610
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900611static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300612{
613 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
614 return 0;
615
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900616 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300617}
618
Avi Kivity9dac77f2011-06-01 15:34:25 +0300619static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300620{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300621 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300622 return 0;
623
Avi Kivity9dac77f2011-06-01 15:34:25 +0300624 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300625}
626
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200627static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
628 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300629{
Avi Kivityda9cb572010-11-22 17:53:21 +0200630 ctxt->exception.vector = vec;
631 ctxt->exception.error_code = error;
632 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200633 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300634}
635
Joerg Roedel3b88e412011-04-04 12:39:29 +0200636static int emulate_db(struct x86_emulate_ctxt *ctxt)
637{
638 return emulate_exception(ctxt, DB_VECTOR, 0, false);
639}
640
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200641static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300642{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200643 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300644}
645
Avi Kivity618ff152011-04-03 12:32:09 +0300646static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
647{
648 return emulate_exception(ctxt, SS_VECTOR, err, true);
649}
650
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200651static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300652{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200653 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300654}
655
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200656static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300657{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200658 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300659}
660
Avi Kivity34d1f492010-08-26 11:59:01 +0300661static int emulate_de(struct x86_emulate_ctxt *ctxt)
662{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200663 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300664}
665
Avi Kivity12537912011-03-29 11:41:27 +0200666static int emulate_nm(struct x86_emulate_ctxt *ctxt)
667{
668 return emulate_exception(ctxt, NM_VECTOR, 0, false);
669}
670
Avi Kivity1aa36612011-04-27 13:20:30 +0300671static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
672{
673 u16 selector;
674 struct desc_struct desc;
675
676 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
677 return selector;
678}
679
680static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
681 unsigned seg)
682{
683 u16 dummy;
684 u32 base3;
685 struct desc_struct desc;
686
687 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
688 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
689}
690
Avi Kivity1c11b372012-04-09 18:39:59 +0300691/*
692 * x86 defines three classes of vector instructions: explicitly
693 * aligned, explicitly unaligned, and the rest, which change behaviour
694 * depending on whether they're AVX encoded or not.
695 *
696 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
697 * subject to the same check.
698 */
699static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
700{
701 if (likely(size < 16))
702 return false;
703
704 if (ctxt->d & Aligned)
705 return true;
706 else if (ctxt->d & Unaligned)
707 return false;
708 else if (ctxt->d & Avx)
709 return false;
710 else
711 return true;
712}
713
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400714static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300715 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400716 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300717 ulong *linear)
718{
Avi Kivity618ff152011-04-03 12:32:09 +0300719 struct desc_struct desc;
720 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300721 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300722 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300723 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200724 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300725
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900726 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300727 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300728 case X86EMUL_MODE_PROT64:
729 if (((signed long)la << 16) >> 16 != la)
730 return emulate_gp(ctxt, 0);
731 break;
732 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300733 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
734 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300735 if (!usable)
736 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200737 /* code segment in protected mode or read-only data segment */
738 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
739 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300740 goto bad;
741 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400742 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300743 goto bad;
744 lim = desc_limit_scaled(&desc);
745 if ((desc.type & 8) || !(desc.type & 4)) {
746 /* expand-up segment */
747 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
748 goto bad;
749 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800750 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300751 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
752 goto bad;
753 lim = desc.d ? 0xffffffff : 0xffff;
754 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
755 goto bad;
756 }
Avi Kivity717746e2011-04-20 13:37:53 +0300757 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300758 if (!(desc.type & 8)) {
759 /* data segment */
760 if (cpl > desc.dpl)
761 goto bad;
762 } else if ((desc.type & 8) && !(desc.type & 4)) {
763 /* nonconforming code segment */
764 if (cpl != desc.dpl)
765 goto bad;
766 } else if ((desc.type & 8) && (desc.type & 4)) {
767 /* conforming code segment */
768 if (cpl < desc.dpl)
769 goto bad;
770 }
771 break;
772 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300773 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300774 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300775 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
776 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300777 *linear = la;
778 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300779bad:
780 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300781 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300782 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300783 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300784}
785
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400786static int linearize(struct x86_emulate_ctxt *ctxt,
787 struct segmented_address addr,
788 unsigned size, bool write,
789 ulong *linear)
790{
791 return __linearize(ctxt, addr, size, write, false, linear);
792}
793
794
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200795static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
796 struct segmented_address addr,
797 void *data,
798 unsigned size)
799{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200800 int rc;
801 ulong linear;
802
Avi Kivity83b87952011-04-03 11:31:19 +0300803 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200804 if (rc != X86EMUL_CONTINUE)
805 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300806 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200807}
808
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900809/*
810 * Fetch the next byte of the instruction being emulated which is pointed to
811 * by ctxt->_eip, then increment ctxt->_eip.
812 *
813 * Also prefetch the remaining bytes of the instruction without crossing page
814 * boundary if they are not in fetch_cache yet.
815 */
816static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200817{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300818 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200819 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300820 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200821
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900822 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400823 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900824 struct segmented_address addr = { .seg = VCPU_SREG_CS,
825 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300826 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900827 size = min(15UL - cur_size,
828 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400829 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900830 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400831 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900832 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
833 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900834 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200835 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300836 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200837 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900838 *dest = fc->data[ctxt->_eip - fc->start];
839 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900840 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200841}
842
843static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900844 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200845{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900846 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200847
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200848 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900849 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200850 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200851 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900852 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900853 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200854 return rc;
855 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900856 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200857}
858
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900859/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900860#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900861({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900862 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900863 if (rc != X86EMUL_CONTINUE) \
864 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900865 (_type)_x; \
866})
867
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900868#define insn_fetch_arr(_arr, _size, _ctxt) \
869({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900870 if (rc != X86EMUL_CONTINUE) \
871 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900872})
873
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000874/*
875 * Given the 'reg' portion of a ModRM byte, and a register block, return a
876 * pointer into the block that addresses the relevant register.
877 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
878 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300879static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000880 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881{
882 void *p;
883
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300885 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
886 else
887 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 return p;
889}
890
891static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200892 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 u16 *size, unsigned long *address, int op_bytes)
894{
895 int rc;
896
897 if (op_bytes == 2)
898 op_bytes = 3;
899 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200900 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900901 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200903 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200904 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 return rc;
906}
907
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300908static int test_cc(unsigned int condition, unsigned int flags)
909{
910 int rc = 0;
911
912 switch ((condition & 15) >> 1) {
913 case 0: /* o */
914 rc |= (flags & EFLG_OF);
915 break;
916 case 1: /* b/c/nae */
917 rc |= (flags & EFLG_CF);
918 break;
919 case 2: /* z/e */
920 rc |= (flags & EFLG_ZF);
921 break;
922 case 3: /* be/na */
923 rc |= (flags & (EFLG_CF|EFLG_ZF));
924 break;
925 case 4: /* s */
926 rc |= (flags & EFLG_SF);
927 break;
928 case 5: /* p/pe */
929 rc |= (flags & EFLG_PF);
930 break;
931 case 7: /* le/ng */
932 rc |= (flags & EFLG_ZF);
933 /* fall through */
934 case 6: /* l/nge */
935 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
936 break;
937 }
938
939 /* Odd condition identifiers (lsb == 1) have inverted sense. */
940 return (!!rc ^ (condition & 1));
941}
942
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300943static void fetch_register_operand(struct operand *op)
944{
945 switch (op->bytes) {
946 case 1:
947 op->val = *(u8 *)op->addr.reg;
948 break;
949 case 2:
950 op->val = *(u16 *)op->addr.reg;
951 break;
952 case 4:
953 op->val = *(u32 *)op->addr.reg;
954 break;
955 case 8:
956 op->val = *(u64 *)op->addr.reg;
957 break;
958 }
959}
960
Avi Kivity12537912011-03-29 11:41:27 +0200961static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
962{
963 ctxt->ops->get_fpu(ctxt);
964 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200965 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
966 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
967 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
968 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
969 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
970 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
971 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
972 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200973#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200974 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
975 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
976 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
977 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
978 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
979 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
980 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
981 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +0200982#endif
983 default: BUG();
984 }
985 ctxt->ops->put_fpu(ctxt);
986}
987
988static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
989 int reg)
990{
991 ctxt->ops->get_fpu(ctxt);
992 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200993 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
994 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
995 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
996 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
997 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
998 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
999 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1000 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001001#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001002 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1003 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1004 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1005 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1006 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1007 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1008 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1009 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001010#endif
1011 default: BUG();
1012 }
1013 ctxt->ops->put_fpu(ctxt);
1014}
1015
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001016static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1017{
1018 ctxt->ops->get_fpu(ctxt);
1019 switch (reg) {
1020 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1021 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1022 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1023 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1024 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1025 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1026 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1027 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1028 default: BUG();
1029 }
1030 ctxt->ops->put_fpu(ctxt);
1031}
1032
1033static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1034{
1035 ctxt->ops->get_fpu(ctxt);
1036 switch (reg) {
1037 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1038 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1039 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1040 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1041 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1042 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1043 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1044 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1045 default: BUG();
1046 }
1047 ctxt->ops->put_fpu(ctxt);
1048}
1049
Gleb Natapov045a2822012-12-20 16:57:43 +02001050static int em_fninit(struct x86_emulate_ctxt *ctxt)
1051{
1052 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1053 return emulate_nm(ctxt);
1054
1055 ctxt->ops->get_fpu(ctxt);
1056 asm volatile("fninit");
1057 ctxt->ops->put_fpu(ctxt);
1058 return X86EMUL_CONTINUE;
1059}
1060
1061static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1062{
1063 u16 fcw;
1064
1065 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1066 return emulate_nm(ctxt);
1067
1068 ctxt->ops->get_fpu(ctxt);
1069 asm volatile("fnstcw %0": "+m"(fcw));
1070 ctxt->ops->put_fpu(ctxt);
1071
1072 /* force 2 byte destination */
1073 ctxt->dst.bytes = 2;
1074 ctxt->dst.val = fcw;
1075
1076 return X86EMUL_CONTINUE;
1077}
1078
1079static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1080{
1081 u16 fsw;
1082
1083 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1084 return emulate_nm(ctxt);
1085
1086 ctxt->ops->get_fpu(ctxt);
1087 asm volatile("fnstsw %0": "+m"(fsw));
1088 ctxt->ops->put_fpu(ctxt);
1089
1090 /* force 2 byte destination */
1091 ctxt->dst.bytes = 2;
1092 ctxt->dst.val = fsw;
1093
1094 return X86EMUL_CONTINUE;
1095}
1096
Avi Kivity12537912011-03-29 11:41:27 +02001097static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001098 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001099{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001100 unsigned reg = ctxt->modrm_reg;
1101 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001102
Avi Kivity9dac77f2011-06-01 15:34:25 +03001103 if (!(ctxt->d & ModRM))
1104 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001105
Avi Kivity9dac77f2011-06-01 15:34:25 +03001106 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001107 op->type = OP_XMM;
1108 op->bytes = 16;
1109 op->addr.xmm = reg;
1110 read_sse_reg(ctxt, &op->vec_val, reg);
1111 return;
1112 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001113 if (ctxt->d & Mmx) {
1114 reg &= 7;
1115 op->type = OP_MM;
1116 op->bytes = 8;
1117 op->addr.mm = reg;
1118 return;
1119 }
Avi Kivity12537912011-03-29 11:41:27 +02001120
Avi Kivity3c118e22007-10-31 10:27:04 +02001121 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001122 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001123 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001124 op->bytes = 1;
1125 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001126 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001127 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001128 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001129 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001130 op->orig_val = op->val;
1131}
1132
Avi Kivitya6e34072012-06-10 17:15:39 +03001133static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1134{
1135 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1136 ctxt->modrm_seg = VCPU_SREG_SS;
1137}
1138
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001139static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001140 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001141{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001142 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001143 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001144 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001145 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001146
Avi Kivity9dac77f2011-06-01 15:34:25 +03001147 if (ctxt->rex_prefix) {
1148 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1149 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1150 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001151 }
1152
Avi Kivity9dac77f2011-06-01 15:34:25 +03001153 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1154 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1155 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1156 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001157
Avi Kivity9dac77f2011-06-01 15:34:25 +03001158 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001159 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001160 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001161 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001162 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001163 op->type = OP_XMM;
1164 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001165 op->addr.xmm = ctxt->modrm_rm;
1166 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001167 return rc;
1168 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001169 if (ctxt->d & Mmx) {
1170 op->type = OP_MM;
1171 op->bytes = 8;
1172 op->addr.xmm = ctxt->modrm_rm & 7;
1173 return rc;
1174 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001175 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176 return rc;
1177 }
1178
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 op->type = OP_MEM;
1180
Avi Kivity9dac77f2011-06-01 15:34:25 +03001181 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001182 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1183 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1184 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1185 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001186
1187 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001190 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001191 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192 break;
1193 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001194 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001195 break;
1196 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001197 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198 break;
1199 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001200 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001201 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001202 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001203 break;
1204 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001205 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001206 break;
1207 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001208 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001209 break;
1210 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001211 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001212 break;
1213 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001214 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001215 break;
1216 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001217 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001218 break;
1219 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001220 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001221 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001222 break;
1223 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001224 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001225 break;
1226 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001227 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1228 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1229 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001230 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001231 } else {
1232 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001233 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001234 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 index_reg |= (sib >> 3) & 7;
1236 base_reg |= sib & 7;
1237 scale = sib >> 6;
1238
Avi Kivity9dac77f2011-06-01 15:34:25 +03001239 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001240 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001241 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001242 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001243 adjust_modrm_seg(ctxt, base_reg);
1244 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001245 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001246 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001247 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001248 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001249 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001250 } else {
1251 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001252 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001253 adjust_modrm_seg(ctxt, base_reg);
1254 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001255 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001257 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001258 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 break;
1260 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001261 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001262 break;
1263 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001264 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265 break;
1266 }
1267 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001268 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001269done:
1270 return rc;
1271}
1272
1273static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001274 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001275{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001276 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001277
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001278 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001279 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001280 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001281 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001282 break;
1283 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001284 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001285 break;
1286 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001287 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001288 break;
1289 }
1290done:
1291 return rc;
1292}
1293
Avi Kivity9dac77f2011-06-01 15:34:25 +03001294static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001295{
Sheng Yang7129eec2010-09-28 16:33:32 +08001296 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001297
Avi Kivity9dac77f2011-06-01 15:34:25 +03001298 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1299 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001300
Avi Kivity9dac77f2011-06-01 15:34:25 +03001301 if (ctxt->src.bytes == 2)
1302 sv = (s16)ctxt->src.val & (s16)mask;
1303 else if (ctxt->src.bytes == 4)
1304 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001305
Avi Kivity9dac77f2011-06-01 15:34:25 +03001306 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001307 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001308
1309 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001310 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001311}
1312
Gleb Natapov9de41572010-04-28 19:15:22 +03001313static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001314 unsigned long addr, void *dest, unsigned size)
1315{
1316 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001317 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001318
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001319 if (mc->pos < mc->end)
1320 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001321
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001322 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001323
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001324 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1325 &ctxt->exception);
1326 if (rc != X86EMUL_CONTINUE)
1327 return rc;
1328
1329 mc->end += size;
1330
1331read_cached:
1332 memcpy(dest, mc->data + mc->pos, size);
1333 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001334 return X86EMUL_CONTINUE;
1335}
1336
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001337static int segmented_read(struct x86_emulate_ctxt *ctxt,
1338 struct segmented_address addr,
1339 void *data,
1340 unsigned size)
1341{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001342 int rc;
1343 ulong linear;
1344
Avi Kivity83b87952011-04-03 11:31:19 +03001345 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001346 if (rc != X86EMUL_CONTINUE)
1347 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001348 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001349}
1350
1351static int segmented_write(struct x86_emulate_ctxt *ctxt,
1352 struct segmented_address addr,
1353 const void *data,
1354 unsigned size)
1355{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001356 int rc;
1357 ulong linear;
1358
Avi Kivity83b87952011-04-03 11:31:19 +03001359 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001360 if (rc != X86EMUL_CONTINUE)
1361 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001362 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1363 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001364}
1365
1366static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1367 struct segmented_address addr,
1368 const void *orig_data, const void *data,
1369 unsigned size)
1370{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001371 int rc;
1372 ulong linear;
1373
Avi Kivity83b87952011-04-03 11:31:19 +03001374 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001375 if (rc != X86EMUL_CONTINUE)
1376 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001377 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1378 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001379}
1380
Gleb Natapov7b262e92010-03-18 15:20:27 +02001381static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001382 unsigned int size, unsigned short port,
1383 void *dest)
1384{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001385 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001386
1387 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001388 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001389 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001390 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001391 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001392 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1393 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001394 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1395 count);
1396 if (n == 0)
1397 n = 1;
1398 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001399 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001400 return 0;
1401 rc->end = n * size;
1402 }
1403
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001404 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1405 ctxt->dst.data = rc->data + rc->pos;
1406 ctxt->dst.type = OP_MEM_STR;
1407 ctxt->dst.count = (rc->end - rc->pos) / size;
1408 rc->pos = rc->end;
1409 } else {
1410 memcpy(dest, rc->data + rc->pos, size);
1411 rc->pos += size;
1412 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001413 return 1;
1414}
1415
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001416static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1417 u16 index, struct desc_struct *desc)
1418{
1419 struct desc_ptr dt;
1420 ulong addr;
1421
1422 ctxt->ops->get_idt(ctxt, &dt);
1423
1424 if (dt.size < index * 8 + 7)
1425 return emulate_gp(ctxt, index << 3 | 0x2);
1426
1427 addr = dt.address + index * 8;
1428 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1429 &ctxt->exception);
1430}
1431
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001433 u16 selector, struct desc_ptr *dt)
1434{
Mathias Krause0225fb52012-08-30 01:30:16 +02001435 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001436
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001437 if (selector & 1 << 2) {
1438 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001439 u16 sel;
1440
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001441 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001442 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001443 return;
1444
1445 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1446 dt->address = get_desc_base(&desc);
1447 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001448 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001449}
1450
1451/* allowed just for 8 bytes segments */
1452static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001453 u16 selector, struct desc_struct *desc,
1454 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001455{
1456 struct desc_ptr dt;
1457 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001458 ulong addr;
1459
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001460 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001461
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001462 if (dt.size < index * 8 + 7)
1463 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001464
Avi Kivitye9194642012-06-13 16:29:39 +03001465 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001466 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1467 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001468}
1469
1470/* allowed just for 8 bytes segments */
1471static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001472 u16 selector, struct desc_struct *desc)
1473{
1474 struct desc_ptr dt;
1475 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001478 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001479
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001480 if (dt.size < index * 8 + 7)
1481 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482
1483 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001484 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1485 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001486}
1487
Gleb Natapov5601d052011-03-07 14:55:06 +02001488/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001489static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490 u16 selector, int seg)
1491{
Avi Kivity869be992012-06-13 16:30:53 +03001492 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493 u8 dpl, rpl, cpl;
1494 unsigned err_vec = GP_VECTOR;
1495 u32 err_code = 0;
1496 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001497 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001498 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001499 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001500
1501 memset(&seg_desc, 0, sizeof seg_desc);
1502
1503 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1504 || ctxt->mode == X86EMUL_MODE_REAL) {
1505 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001506 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001507 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001508 goto load;
1509 }
1510
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001511 rpl = selector & 3;
1512 cpl = ctxt->ops->cpl(ctxt);
1513
1514 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1515 if ((seg == VCPU_SREG_CS
1516 || (seg == VCPU_SREG_SS
1517 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1518 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001519 && null_selector)
1520 goto exception;
1521
1522 /* TR should be in GDT only */
1523 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1524 goto exception;
1525
1526 if (null_selector) /* for NULL selector skip all following checks */
1527 goto load;
1528
Avi Kivitye9194642012-06-13 16:29:39 +03001529 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530 if (ret != X86EMUL_CONTINUE)
1531 return ret;
1532
1533 err_code = selector & 0xfffc;
1534 err_vec = GP_VECTOR;
1535
Guo Chaofc058682012-06-28 15:19:51 +08001536 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001537 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1538 goto exception;
1539
1540 if (!seg_desc.p) {
1541 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1542 goto exception;
1543 }
1544
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546
1547 switch (seg) {
1548 case VCPU_SREG_SS:
1549 /*
1550 * segment is not a writable data segment or segment
1551 * selector's RPL != CPL or segment selector's RPL != CPL
1552 */
1553 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1554 goto exception;
1555 break;
1556 case VCPU_SREG_CS:
1557 if (!(seg_desc.type & 8))
1558 goto exception;
1559
1560 if (seg_desc.type & 4) {
1561 /* conforming */
1562 if (dpl > cpl)
1563 goto exception;
1564 } else {
1565 /* nonconforming */
1566 if (rpl > cpl || dpl != cpl)
1567 goto exception;
1568 }
1569 /* CS(RPL) <- CPL */
1570 selector = (selector & 0xfffc) | cpl;
1571 break;
1572 case VCPU_SREG_TR:
1573 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1574 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001575 old_desc = seg_desc;
1576 seg_desc.type |= 2; /* busy */
1577 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1578 sizeof(seg_desc), &ctxt->exception);
1579 if (ret != X86EMUL_CONTINUE)
1580 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001581 break;
1582 case VCPU_SREG_LDTR:
1583 if (seg_desc.s || seg_desc.type != 2)
1584 goto exception;
1585 break;
1586 default: /* DS, ES, FS, or GS */
1587 /*
1588 * segment is not a data or readable code segment or
1589 * ((segment is a data or nonconforming code segment)
1590 * and (both RPL and CPL > DPL))
1591 */
1592 if ((seg_desc.type & 0xa) == 0x8 ||
1593 (((seg_desc.type & 0xc) != 0xc) &&
1594 (rpl > dpl && cpl > dpl)))
1595 goto exception;
1596 break;
1597 }
1598
1599 if (seg_desc.s) {
1600 /* mark segment as accessed */
1601 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001602 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001603 if (ret != X86EMUL_CONTINUE)
1604 return ret;
1605 }
1606load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001607 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001608 return X86EMUL_CONTINUE;
1609exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001610 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001611 return X86EMUL_PROPAGATE_FAULT;
1612}
1613
Wei Yongjun31be40b2010-08-17 09:17:30 +08001614static void write_register_operand(struct operand *op)
1615{
1616 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1617 switch (op->bytes) {
1618 case 1:
1619 *(u8 *)op->addr.reg = (u8)op->val;
1620 break;
1621 case 2:
1622 *(u16 *)op->addr.reg = (u16)op->val;
1623 break;
1624 case 4:
1625 *op->addr.reg = (u32)op->val;
1626 break; /* 64b: zero-extend */
1627 case 8:
1628 *op->addr.reg = op->val;
1629 break;
1630 }
1631}
1632
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001633static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001634{
1635 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001636
Avi Kivityb6744dc2013-01-04 16:18:50 +02001637 if (ctxt->d & NoWrite)
1638 return X86EMUL_CONTINUE;
1639
Avi Kivity9dac77f2011-06-01 15:34:25 +03001640 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001641 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001642 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001643 break;
1644 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001645 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001646 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001647 ctxt->dst.addr.mem,
1648 &ctxt->dst.orig_val,
1649 &ctxt->dst.val,
1650 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001651 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001652 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001653 ctxt->dst.addr.mem,
1654 &ctxt->dst.val,
1655 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001656 if (rc != X86EMUL_CONTINUE)
1657 return rc;
1658 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001659 case OP_MEM_STR:
1660 rc = segmented_write(ctxt,
1661 ctxt->dst.addr.mem,
1662 ctxt->dst.data,
1663 ctxt->dst.bytes * ctxt->dst.count);
1664 if (rc != X86EMUL_CONTINUE)
1665 return rc;
1666 break;
Avi Kivity12537912011-03-29 11:41:27 +02001667 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001668 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001669 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001670 case OP_MM:
1671 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1672 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001673 case OP_NONE:
1674 /* no writeback */
1675 break;
1676 default:
1677 break;
1678 }
1679 return X86EMUL_CONTINUE;
1680}
1681
Avi Kivity51ddff52012-06-12 20:19:40 +03001682static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001683{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001684 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001685
Avi Kivity5ad105e2012-08-19 14:34:31 +03001686 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001687 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001688 addr.seg = VCPU_SREG_SS;
1689
Avi Kivity51ddff52012-06-12 20:19:40 +03001690 return segmented_write(ctxt, addr, data, bytes);
1691}
1692
1693static int em_push(struct x86_emulate_ctxt *ctxt)
1694{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001695 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001696 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001697 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001698}
1699
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001700static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001701 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001702{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001703 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001704 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001705
Avi Kivitydd856ef2012-08-27 23:46:17 +03001706 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001707 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001708 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001709 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001710 return rc;
1711
Avi Kivity5ad105e2012-08-19 14:34:31 +03001712 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001713 return rc;
1714}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001715
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001716static int em_pop(struct x86_emulate_ctxt *ctxt)
1717{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001718 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001719}
1720
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001721static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001722 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001723{
1724 int rc;
1725 unsigned long val, change_mask;
1726 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001727 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001728
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001729 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001730 if (rc != X86EMUL_CONTINUE)
1731 return rc;
1732
1733 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1734 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1735
1736 switch(ctxt->mode) {
1737 case X86EMUL_MODE_PROT64:
1738 case X86EMUL_MODE_PROT32:
1739 case X86EMUL_MODE_PROT16:
1740 if (cpl == 0)
1741 change_mask |= EFLG_IOPL;
1742 if (cpl <= iopl)
1743 change_mask |= EFLG_IF;
1744 break;
1745 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001746 if (iopl < 3)
1747 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001748 change_mask |= EFLG_IF;
1749 break;
1750 default: /* real mode */
1751 change_mask |= (EFLG_IOPL | EFLG_IF);
1752 break;
1753 }
1754
1755 *(unsigned long *)dest =
1756 (ctxt->eflags & ~change_mask) | (val & change_mask);
1757
1758 return rc;
1759}
1760
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001761static int em_popf(struct x86_emulate_ctxt *ctxt)
1762{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001763 ctxt->dst.type = OP_REG;
1764 ctxt->dst.addr.reg = &ctxt->eflags;
1765 ctxt->dst.bytes = ctxt->op_bytes;
1766 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001767}
1768
Avi Kivity612e89f2012-06-12 20:03:23 +03001769static int em_enter(struct x86_emulate_ctxt *ctxt)
1770{
1771 int rc;
1772 unsigned frame_size = ctxt->src.val;
1773 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001774 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001775
1776 if (nesting_level)
1777 return X86EMUL_UNHANDLEABLE;
1778
Avi Kivitydd856ef2012-08-27 23:46:17 +03001779 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1780 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001781 if (rc != X86EMUL_CONTINUE)
1782 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001783 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001784 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001785 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1786 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001787 stack_mask(ctxt));
1788 return X86EMUL_CONTINUE;
1789}
1790
Avi Kivityf47cfa32012-06-07 17:49:24 +03001791static int em_leave(struct x86_emulate_ctxt *ctxt)
1792{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001793 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001794 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001795 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001796}
1797
Avi Kivity1cd196e2011-09-13 10:45:51 +03001798static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001799{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001800 int seg = ctxt->src2.val;
1801
Avi Kivity9dac77f2011-06-01 15:34:25 +03001802 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001803
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001804 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001805}
1806
Avi Kivity1cd196e2011-09-13 10:45:51 +03001807static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001808{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001809 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001810 unsigned long selector;
1811 int rc;
1812
Avi Kivity9dac77f2011-06-01 15:34:25 +03001813 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001814 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001815 return rc;
1816
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001817 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001818 return rc;
1819}
1820
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001821static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001822{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001823 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001824 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001825 int reg = VCPU_REGS_RAX;
1826
1827 while (reg <= VCPU_REGS_RDI) {
1828 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001829 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001830
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001831 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001832 if (rc != X86EMUL_CONTINUE)
1833 return rc;
1834
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001835 ++reg;
1836 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001837
Wei Yongjunc37eda12010-06-15 09:03:33 +08001838 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001839}
1840
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001841static int em_pushf(struct x86_emulate_ctxt *ctxt)
1842{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001843 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001844 return em_push(ctxt);
1845}
1846
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001847static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001848{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001849 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001850 int reg = VCPU_REGS_RDI;
1851
1852 while (reg >= VCPU_REGS_RAX) {
1853 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001854 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001855 --reg;
1856 }
1857
Avi Kivitydd856ef2012-08-27 23:46:17 +03001858 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001859 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001860 break;
1861 --reg;
1862 }
1863 return rc;
1864}
1865
Avi Kivitydd856ef2012-08-27 23:46:17 +03001866static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001867{
Mathias Krause0225fb52012-08-30 01:30:16 +02001868 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001869 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001870 struct desc_ptr dt;
1871 gva_t cs_addr;
1872 gva_t eip_addr;
1873 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001874
1875 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001876 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001877 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001878 if (rc != X86EMUL_CONTINUE)
1879 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001880
1881 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1882
Avi Kivity9dac77f2011-06-01 15:34:25 +03001883 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001884 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001885 if (rc != X86EMUL_CONTINUE)
1886 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001887
Avi Kivity9dac77f2011-06-01 15:34:25 +03001888 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001889 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001890 if (rc != X86EMUL_CONTINUE)
1891 return rc;
1892
Avi Kivity4bff1e862011-04-20 13:37:53 +03001893 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001894
1895 eip_addr = dt.address + (irq << 2);
1896 cs_addr = dt.address + (irq << 2) + 2;
1897
Avi Kivity0f65dd72011-04-20 13:37:53 +03001898 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001899 if (rc != X86EMUL_CONTINUE)
1900 return rc;
1901
Avi Kivity0f65dd72011-04-20 13:37:53 +03001902 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001903 if (rc != X86EMUL_CONTINUE)
1904 return rc;
1905
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001906 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001907 if (rc != X86EMUL_CONTINUE)
1908 return rc;
1909
Avi Kivity9dac77f2011-06-01 15:34:25 +03001910 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001911
1912 return rc;
1913}
1914
Avi Kivitydd856ef2012-08-27 23:46:17 +03001915int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1916{
1917 int rc;
1918
1919 invalidate_registers(ctxt);
1920 rc = __emulate_int_real(ctxt, irq);
1921 if (rc == X86EMUL_CONTINUE)
1922 writeback_registers(ctxt);
1923 return rc;
1924}
1925
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001926static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001927{
1928 switch(ctxt->mode) {
1929 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001930 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001931 case X86EMUL_MODE_VM86:
1932 case X86EMUL_MODE_PROT16:
1933 case X86EMUL_MODE_PROT32:
1934 case X86EMUL_MODE_PROT64:
1935 default:
1936 /* Protected mode interrupts unimplemented yet */
1937 return X86EMUL_UNHANDLEABLE;
1938 }
1939}
1940
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001941static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001942{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001943 int rc = X86EMUL_CONTINUE;
1944 unsigned long temp_eip = 0;
1945 unsigned long temp_eflags = 0;
1946 unsigned long cs = 0;
1947 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1948 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1949 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1950 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1951
1952 /* TODO: Add stack limit check */
1953
Avi Kivity9dac77f2011-06-01 15:34:25 +03001954 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001955
1956 if (rc != X86EMUL_CONTINUE)
1957 return rc;
1958
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001959 if (temp_eip & ~0xffff)
1960 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001961
Avi Kivity9dac77f2011-06-01 15:34:25 +03001962 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001963
1964 if (rc != X86EMUL_CONTINUE)
1965 return rc;
1966
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001968
1969 if (rc != X86EMUL_CONTINUE)
1970 return rc;
1971
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001972 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001973
1974 if (rc != X86EMUL_CONTINUE)
1975 return rc;
1976
Avi Kivity9dac77f2011-06-01 15:34:25 +03001977 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001978
1979
Avi Kivity9dac77f2011-06-01 15:34:25 +03001980 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001981 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001982 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001983 ctxt->eflags &= ~0xffff;
1984 ctxt->eflags |= temp_eflags;
1985 }
1986
1987 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1988 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1989
1990 return rc;
1991}
1992
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001993static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001994{
1995 switch(ctxt->mode) {
1996 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001997 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001998 case X86EMUL_MODE_VM86:
1999 case X86EMUL_MODE_PROT16:
2000 case X86EMUL_MODE_PROT32:
2001 case X86EMUL_MODE_PROT64:
2002 default:
2003 /* iret from protected mode unimplemented yet */
2004 return X86EMUL_UNHANDLEABLE;
2005 }
2006}
2007
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002008static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2009{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002010 int rc;
2011 unsigned short sel;
2012
Avi Kivity9dac77f2011-06-01 15:34:25 +03002013 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002014
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002015 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002016 if (rc != X86EMUL_CONTINUE)
2017 return rc;
2018
Avi Kivity9dac77f2011-06-01 15:34:25 +03002019 ctxt->_eip = 0;
2020 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002021 return X86EMUL_CONTINUE;
2022}
2023
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002024static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002025{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002026 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002027 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002028 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002029 break;
2030 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002031 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002032 break;
2033 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002034 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002035 break;
2036 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002037 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002038 break;
2039 case 4: /* sal/shl */
2040 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002041 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002042 break;
2043 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002044 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002045 break;
2046 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002047 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002048 break;
2049 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002050 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002051}
2052
Avi Kivity3329ece2011-09-13 10:45:39 +03002053static int em_not(struct x86_emulate_ctxt *ctxt)
2054{
2055 ctxt->dst.val = ~ctxt->dst.val;
2056 return X86EMUL_CONTINUE;
2057}
2058
2059static int em_neg(struct x86_emulate_ctxt *ctxt)
2060{
2061 emulate_1op(ctxt, "neg");
2062 return X86EMUL_CONTINUE;
2063}
2064
2065static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2066{
2067 u8 ex = 0;
2068
2069 emulate_1op_rax_rdx(ctxt, "mul", ex);
2070 return X86EMUL_CONTINUE;
2071}
2072
2073static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2074{
2075 u8 ex = 0;
2076
2077 emulate_1op_rax_rdx(ctxt, "imul", ex);
2078 return X86EMUL_CONTINUE;
2079}
2080
2081static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002082{
Avi Kivity34d1f492010-08-26 11:59:01 +03002083 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002084
Avi Kivity3329ece2011-09-13 10:45:39 +03002085 emulate_1op_rax_rdx(ctxt, "div", de);
2086 if (de)
2087 return emulate_de(ctxt);
2088 return X86EMUL_CONTINUE;
2089}
2090
2091static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2092{
2093 u8 de = 0;
2094
2095 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002096 if (de)
2097 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002098 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002099}
2100
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002101static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002102{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002103 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002104
Avi Kivity9dac77f2011-06-01 15:34:25 +03002105 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002106 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03002107 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002108 break;
2109 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03002110 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002111 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03002112 case 2: /* call near abs */ {
2113 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002114 old_eip = ctxt->_eip;
2115 ctxt->_eip = ctxt->src.val;
2116 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002117 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002118 break;
2119 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002120 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002121 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002122 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002123 case 5: /* jmp far */
2124 rc = em_jmp_far(ctxt);
2125 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002126 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002127 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002128 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002129 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002130 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002131}
2132
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002133static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002134{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002135 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002136
Avi Kivitydd856ef2012-08-27 23:46:17 +03002137 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2138 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2139 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2140 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002141 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002142 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002143 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2144 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145
Laurent Vivier05f086f2007-09-24 11:10:55 +02002146 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002148 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002149}
2150
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002151static int em_ret(struct x86_emulate_ctxt *ctxt)
2152{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002153 ctxt->dst.type = OP_REG;
2154 ctxt->dst.addr.reg = &ctxt->_eip;
2155 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002156 return em_pop(ctxt);
2157}
2158
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002159static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002160{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002161 int rc;
2162 unsigned long cs;
2163
Avi Kivity9dac77f2011-06-01 15:34:25 +03002164 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002165 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002166 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002167 if (ctxt->op_bytes == 4)
2168 ctxt->_eip = (u32)ctxt->_eip;
2169 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002170 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002171 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002172 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002173 return rc;
2174}
2175
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002176static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2177{
2178 /* Save real source value, then compare EAX against destination. */
2179 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002180 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002181 emulate_2op_SrcV(ctxt, "cmp");
2182
2183 if (ctxt->eflags & EFLG_ZF) {
2184 /* Success: write back to memory. */
2185 ctxt->dst.val = ctxt->src.orig_val;
2186 } else {
2187 /* Failure: write the value we saw to EAX. */
2188 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002189 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002190 }
2191 return X86EMUL_CONTINUE;
2192}
2193
Avi Kivityd4b43252011-09-13 10:45:50 +03002194static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002195{
Avi Kivityd4b43252011-09-13 10:45:50 +03002196 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002197 unsigned short sel;
2198 int rc;
2199
Avi Kivity9dac77f2011-06-01 15:34:25 +03002200 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002201
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002202 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002203 if (rc != X86EMUL_CONTINUE)
2204 return rc;
2205
Avi Kivity9dac77f2011-06-01 15:34:25 +03002206 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002207 return rc;
2208}
2209
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002210static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002211setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002212 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002213{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002214 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002215 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002216 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002217 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002218 cs->type = 0x0b; /* Read, Execute, Accessed */
2219 cs->s = 1;
2220 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002221 cs->p = 1;
2222 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002223 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002224
Gleb Natapov79168fd2010-04-28 19:15:30 +03002225 set_desc_base(ss, 0); /* flat segment */
2226 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002227 ss->g = 1; /* 4kb granularity */
2228 ss->s = 1;
2229 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002230 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002231 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002232 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002233 ss->l = 0;
2234 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002235}
2236
Avi Kivity1a18a692012-02-01 12:23:21 +02002237static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2238{
2239 u32 eax, ebx, ecx, edx;
2240
2241 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002242 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2243 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002244 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2245 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2246}
2247
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002248static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2249{
Mathias Krause0225fb52012-08-30 01:30:16 +02002250 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002251 u32 eax, ebx, ecx, edx;
2252
2253 /*
2254 * syscall should always be enabled in longmode - so only become
2255 * vendor specific (cpuid) if other modes are active...
2256 */
2257 if (ctxt->mode == X86EMUL_MODE_PROT64)
2258 return true;
2259
2260 eax = 0x00000000;
2261 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002262 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2263 /*
2264 * Intel ("GenuineIntel")
2265 * remark: Intel CPUs only support "syscall" in 64bit
2266 * longmode. Also an 64bit guest with a
2267 * 32bit compat-app running will #UD !! While this
2268 * behaviour can be fixed (by emulating) into AMD
2269 * response - CPUs of AMD can't behave like Intel.
2270 */
2271 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2272 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2273 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2274 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002275
Avi Kivity0017f932012-06-07 14:10:16 +03002276 /* AMD ("AuthenticAMD") */
2277 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2278 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2279 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2280 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002281
Avi Kivity0017f932012-06-07 14:10:16 +03002282 /* AMD ("AMDisbetter!") */
2283 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2284 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2285 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2286 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002287
2288 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2289 return false;
2290}
2291
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002292static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002293{
Mathias Krause0225fb52012-08-30 01:30:16 +02002294 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002295 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002296 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002297 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002298 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002299
2300 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002301 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002302 ctxt->mode == X86EMUL_MODE_VM86)
2303 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002304
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002305 if (!(em_syscall_is_enabled(ctxt)))
2306 return emulate_ud(ctxt);
2307
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002308 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002309 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002310
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002311 if (!(efer & EFER_SCE))
2312 return emulate_ud(ctxt);
2313
Avi Kivity717746e2011-04-20 13:37:53 +03002314 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002315 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002316 cs_sel = (u16)(msr_data & 0xfffc);
2317 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002318
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002319 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002320 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002321 cs.l = 1;
2322 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002323 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2324 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002325
Avi Kivitydd856ef2012-08-27 23:46:17 +03002326 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002327 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002328#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002329 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002330
Avi Kivity717746e2011-04-20 13:37:53 +03002331 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002332 ctxt->mode == X86EMUL_MODE_PROT64 ?
2333 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002334 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002335
Avi Kivity717746e2011-04-20 13:37:53 +03002336 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002337 ctxt->eflags &= ~(msr_data | EFLG_RF);
2338#endif
2339 } else {
2340 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002341 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002342 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002343
2344 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2345 }
2346
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002347 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002348}
2349
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002350static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002351{
Mathias Krause0225fb52012-08-30 01:30:16 +02002352 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002353 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002354 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002355 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002356 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002357
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002358 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002359 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002360 if (ctxt->mode == X86EMUL_MODE_REAL)
2361 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002362
Avi Kivity1a18a692012-02-01 12:23:21 +02002363 /*
2364 * Not recognized on AMD in compat mode (but is recognized in legacy
2365 * mode).
2366 */
2367 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2368 && !vendor_intel(ctxt))
2369 return emulate_ud(ctxt);
2370
Andre Przywara8c604352009-06-18 12:56:01 +02002371 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2372 * Therefore, we inject an #UD.
2373 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002374 if (ctxt->mode == X86EMUL_MODE_PROT64)
2375 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002376
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002377 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002378
Avi Kivity717746e2011-04-20 13:37:53 +03002379 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002380 switch (ctxt->mode) {
2381 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002382 if ((msr_data & 0xfffc) == 0x0)
2383 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002384 break;
2385 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002386 if (msr_data == 0x0)
2387 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002388 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002389 default:
2390 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002391 }
2392
2393 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002394 cs_sel = (u16)msr_data;
2395 cs_sel &= ~SELECTOR_RPL_MASK;
2396 ss_sel = cs_sel + 8;
2397 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002398 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002399 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002400 cs.l = 1;
2401 }
2402
Avi Kivity1aa36612011-04-27 13:20:30 +03002403 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2404 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002405
Avi Kivity717746e2011-04-20 13:37:53 +03002406 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002407 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002408
Avi Kivity717746e2011-04-20 13:37:53 +03002409 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002410 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002411
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002412 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002413}
2414
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002415static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002416{
Mathias Krause0225fb52012-08-30 01:30:16 +02002417 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002418 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002419 u64 msr_data;
2420 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002421 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002422
Gleb Natapova0044752010-02-10 14:21:31 +02002423 /* inject #GP if in real mode or Virtual 8086 mode */
2424 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002425 ctxt->mode == X86EMUL_MODE_VM86)
2426 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002427
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002428 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002429
Avi Kivity9dac77f2011-06-01 15:34:25 +03002430 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002431 usermode = X86EMUL_MODE_PROT64;
2432 else
2433 usermode = X86EMUL_MODE_PROT32;
2434
2435 cs.dpl = 3;
2436 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002437 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002438 switch (usermode) {
2439 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002440 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002441 if ((msr_data & 0xfffc) == 0x0)
2442 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002443 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002444 break;
2445 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002446 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002447 if (msr_data == 0x0)
2448 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002449 ss_sel = cs_sel + 8;
2450 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002451 cs.l = 1;
2452 break;
2453 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002454 cs_sel |= SELECTOR_RPL_MASK;
2455 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002456
Avi Kivity1aa36612011-04-27 13:20:30 +03002457 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2458 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002459
Avi Kivitydd856ef2012-08-27 23:46:17 +03002460 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2461 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002462
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002463 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002464}
2465
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002466static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002467{
2468 int iopl;
2469 if (ctxt->mode == X86EMUL_MODE_REAL)
2470 return false;
2471 if (ctxt->mode == X86EMUL_MODE_VM86)
2472 return true;
2473 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002474 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002475}
2476
2477static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002478 u16 port, u16 len)
2479{
Mathias Krause0225fb52012-08-30 01:30:16 +02002480 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002481 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002482 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002483 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002484 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002485 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002486 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002487
Avi Kivity1aa36612011-04-27 13:20:30 +03002488 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002489 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002490 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002491 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002492 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002493 base = get_desc_base(&tr_seg);
2494#ifdef CONFIG_X86_64
2495 base |= ((u64)base3) << 32;
2496#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002497 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002498 if (r != X86EMUL_CONTINUE)
2499 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002500 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002501 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002502 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002503 if (r != X86EMUL_CONTINUE)
2504 return false;
2505 if ((perm >> bit_idx) & mask)
2506 return false;
2507 return true;
2508}
2509
2510static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002511 u16 port, u16 len)
2512{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002513 if (ctxt->perm_ok)
2514 return true;
2515
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002516 if (emulator_bad_iopl(ctxt))
2517 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002518 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002519
2520 ctxt->perm_ok = true;
2521
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002522 return true;
2523}
2524
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002526 struct tss_segment_16 *tss)
2527{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002528 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002530 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2531 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2532 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2533 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2534 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2535 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2536 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2537 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002538
Avi Kivity1aa36612011-04-27 13:20:30 +03002539 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2540 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2541 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2542 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2543 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002544}
2545
2546static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547 struct tss_segment_16 *tss)
2548{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002549 int ret;
2550
Avi Kivity9dac77f2011-06-01 15:34:25 +03002551 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002553 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2554 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2555 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2556 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2557 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2558 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2559 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2560 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002561
2562 /*
2563 * SDM says that segment selectors are loaded before segment
2564 * descriptors
2565 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002566 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2567 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2568 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2569 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2570 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002571
2572 /*
Guo Chaofc058682012-06-28 15:19:51 +08002573 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002574 * it is handled in a context of new task
2575 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002576 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002577 if (ret != X86EMUL_CONTINUE)
2578 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002579 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002580 if (ret != X86EMUL_CONTINUE)
2581 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002582 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002583 if (ret != X86EMUL_CONTINUE)
2584 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002585 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002586 if (ret != X86EMUL_CONTINUE)
2587 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002588 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002589 if (ret != X86EMUL_CONTINUE)
2590 return ret;
2591
2592 return X86EMUL_CONTINUE;
2593}
2594
2595static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 u16 tss_selector, u16 old_tss_sel,
2597 ulong old_tss_base, struct desc_struct *new_desc)
2598{
Mathias Krause0225fb52012-08-30 01:30:16 +02002599 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 struct tss_segment_16 tss_seg;
2601 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002602 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603
Avi Kivity0f65dd72011-04-20 13:37:53 +03002604 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002605 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002606 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002608 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002610 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611
Avi Kivity0f65dd72011-04-20 13:37:53 +03002612 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002613 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002614 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002616 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617
Avi Kivity0f65dd72011-04-20 13:37:53 +03002618 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002619 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002620 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002622 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623
2624 if (old_tss_sel != 0xffff) {
2625 tss_seg.prev_task_link = old_tss_sel;
2626
Avi Kivity0f65dd72011-04-20 13:37:53 +03002627 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 &tss_seg.prev_task_link,
2629 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002630 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002631 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002634 }
2635
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002636 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637}
2638
2639static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 struct tss_segment_32 *tss)
2641{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002642 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002643 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002645 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2646 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2647 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2648 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2649 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2650 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2651 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2652 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
Avi Kivity1aa36612011-04-27 13:20:30 +03002654 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2655 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2656 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2657 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2658 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2659 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2660 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661}
2662
2663static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 struct tss_segment_32 *tss)
2665{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 int ret;
2667
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002668 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002669 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002670 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002671 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002672
2673 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002674 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2675 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2676 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2677 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2678 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2679 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2680 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2681 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002682
2683 /*
2684 * SDM says that segment selectors are loaded before segment
2685 * descriptors
2686 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002687 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2688 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2689 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2690 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2691 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2692 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2693 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002694
2695 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002696 * If we're switching between Protected Mode and VM86, we need to make
2697 * sure to update the mode before loading the segment descriptors so
2698 * that the selectors are interpreted correctly.
2699 *
2700 * Need to get rflags to the vcpu struct immediately because it
2701 * influences the CPL which is checked at least when loading the segment
2702 * descriptors and when pushing an error code to the new kernel stack.
2703 *
2704 * TODO Introduce a separate ctxt->ops->set_cpl callback
2705 */
2706 if (ctxt->eflags & X86_EFLAGS_VM)
2707 ctxt->mode = X86EMUL_MODE_VM86;
2708 else
2709 ctxt->mode = X86EMUL_MODE_PROT32;
2710
2711 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2712
2713 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 * Now load segment descriptors. If fault happenes at this stage
2715 * it is handled in a context of new task
2716 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002717 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 if (ret != X86EMUL_CONTINUE)
2719 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002720 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002721 if (ret != X86EMUL_CONTINUE)
2722 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002723 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002724 if (ret != X86EMUL_CONTINUE)
2725 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002726 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002727 if (ret != X86EMUL_CONTINUE)
2728 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002729 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002730 if (ret != X86EMUL_CONTINUE)
2731 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002732 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002733 if (ret != X86EMUL_CONTINUE)
2734 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002735 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002736 if (ret != X86EMUL_CONTINUE)
2737 return ret;
2738
2739 return X86EMUL_CONTINUE;
2740}
2741
2742static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002743 u16 tss_selector, u16 old_tss_sel,
2744 ulong old_tss_base, struct desc_struct *new_desc)
2745{
Mathias Krause0225fb52012-08-30 01:30:16 +02002746 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747 struct tss_segment_32 tss_seg;
2748 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002749 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750
Avi Kivity0f65dd72011-04-20 13:37:53 +03002751 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002752 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002753 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002755 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002757 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758
Avi Kivity0f65dd72011-04-20 13:37:53 +03002759 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002760 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002761 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002763 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764
Avi Kivity0f65dd72011-04-20 13:37:53 +03002765 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002766 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002767 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770
2771 if (old_tss_sel != 0xffff) {
2772 tss_seg.prev_task_link = old_tss_sel;
2773
Avi Kivity0f65dd72011-04-20 13:37:53 +03002774 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002775 &tss_seg.prev_task_link,
2776 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002777 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002778 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002781 }
2782
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002783 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784}
2785
2786static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002787 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002788 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789{
Mathias Krause0225fb52012-08-30 01:30:16 +02002790 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791 struct desc_struct curr_tss_desc, next_tss_desc;
2792 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002793 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002795 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002796 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002797 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798
2799 /* FIXME: old_tss_base == ~0 ? */
2800
Avi Kivitye9194642012-06-13 16:29:39 +03002801 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002802 if (ret != X86EMUL_CONTINUE)
2803 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002804 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805 if (ret != X86EMUL_CONTINUE)
2806 return ret;
2807
2808 /* FIXME: check that next_tss_desc is tss */
2809
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002810 /*
2811 * Check privileges. The three cases are task switch caused by...
2812 *
2813 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2814 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002815 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002816 */
2817 if (reason == TASK_SWITCH_GATE) {
2818 if (idt_index != -1) {
2819 /* Software interrupts */
2820 struct desc_struct task_gate_desc;
2821 int dpl;
2822
2823 ret = read_interrupt_descriptor(ctxt, idt_index,
2824 &task_gate_desc);
2825 if (ret != X86EMUL_CONTINUE)
2826 return ret;
2827
2828 dpl = task_gate_desc.dpl;
2829 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2830 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2831 }
2832 } else if (reason != TASK_SWITCH_IRET) {
2833 int dpl = next_tss_desc.dpl;
2834 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2835 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002836 }
2837
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002838
Gleb Natapovceffb452010-03-18 15:20:19 +02002839 desc_limit = desc_limit_scaled(&next_tss_desc);
2840 if (!next_tss_desc.p ||
2841 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2842 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002843 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002844 return X86EMUL_PROPAGATE_FAULT;
2845 }
2846
2847 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2848 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002849 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 }
2851
2852 if (reason == TASK_SWITCH_IRET)
2853 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2854
2855 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002856 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002857 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2858 old_tss_sel = 0xffff;
2859
2860 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002861 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002862 old_tss_base, &next_tss_desc);
2863 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002864 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002865 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002866 if (ret != X86EMUL_CONTINUE)
2867 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002868
2869 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2870 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2871
2872 if (reason != TASK_SWITCH_IRET) {
2873 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002874 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002875 }
2876
Avi Kivity717746e2011-04-20 13:37:53 +03002877 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002878 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002879
Jan Kiszkae269fb22010-04-14 15:51:09 +02002880 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002881 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2882 ctxt->lock_prefix = 0;
2883 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002884 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002885 }
2886
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002887 return ret;
2888}
2889
2890int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002891 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002892 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002894 int rc;
2895
Avi Kivitydd856ef2012-08-27 23:46:17 +03002896 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002897 ctxt->_eip = ctxt->eip;
2898 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002899
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002900 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002901 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002902
Avi Kivitydd856ef2012-08-27 23:46:17 +03002903 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002904 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002905 writeback_registers(ctxt);
2906 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002907
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002908 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909}
2910
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002911static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2912 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002913{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002914 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002915
Avi Kivitydd856ef2012-08-27 23:46:17 +03002916 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2917 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002918}
2919
Avi Kivity7af04fc2010-08-18 14:16:35 +03002920static int em_das(struct x86_emulate_ctxt *ctxt)
2921{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002922 u8 al, old_al;
2923 bool af, cf, old_cf;
2924
2925 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002926 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002927
2928 old_al = al;
2929 old_cf = cf;
2930 cf = false;
2931 af = ctxt->eflags & X86_EFLAGS_AF;
2932 if ((al & 0x0f) > 9 || af) {
2933 al -= 6;
2934 cf = old_cf | (al >= 250);
2935 af = true;
2936 } else {
2937 af = false;
2938 }
2939 if (old_al > 0x99 || old_cf) {
2940 al -= 0x60;
2941 cf = true;
2942 }
2943
Avi Kivity9dac77f2011-06-01 15:34:25 +03002944 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002945 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002946 ctxt->src.type = OP_IMM;
2947 ctxt->src.val = 0;
2948 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002949 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002950 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2951 if (cf)
2952 ctxt->eflags |= X86_EFLAGS_CF;
2953 if (af)
2954 ctxt->eflags |= X86_EFLAGS_AF;
2955 return X86EMUL_CONTINUE;
2956}
2957
Gleb Natapov7f662272012-12-10 11:42:30 +02002958static int em_aad(struct x86_emulate_ctxt *ctxt)
2959{
2960 u8 al = ctxt->dst.val & 0xff;
2961 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2962
2963 al = (al + (ah * ctxt->src.val)) & 0xff;
2964
2965 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2966
2967 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2968
2969 if (!al)
2970 ctxt->eflags |= X86_EFLAGS_ZF;
2971 if (!(al & 1))
2972 ctxt->eflags |= X86_EFLAGS_PF;
2973 if (al & 0x80)
2974 ctxt->eflags |= X86_EFLAGS_SF;
2975
2976 return X86EMUL_CONTINUE;
2977}
2978
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002979static int em_call(struct x86_emulate_ctxt *ctxt)
2980{
2981 long rel = ctxt->src.val;
2982
2983 ctxt->src.val = (unsigned long)ctxt->_eip;
2984 jmp_rel(ctxt, rel);
2985 return em_push(ctxt);
2986}
2987
Avi Kivity0ef753b2010-08-18 14:51:45 +03002988static int em_call_far(struct x86_emulate_ctxt *ctxt)
2989{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002990 u16 sel, old_cs;
2991 ulong old_eip;
2992 int rc;
2993
Avi Kivity1aa36612011-04-27 13:20:30 +03002994 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002995 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002996
Avi Kivity9dac77f2011-06-01 15:34:25 +03002997 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002998 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002999 return X86EMUL_CONTINUE;
3000
Avi Kivity9dac77f2011-06-01 15:34:25 +03003001 ctxt->_eip = 0;
3002 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003003
Avi Kivity9dac77f2011-06-01 15:34:25 +03003004 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003005 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003006 if (rc != X86EMUL_CONTINUE)
3007 return rc;
3008
Avi Kivity9dac77f2011-06-01 15:34:25 +03003009 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003010 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003011}
3012
Avi Kivity40ece7c2010-08-18 15:12:09 +03003013static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3014{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003015 int rc;
3016
Avi Kivity9dac77f2011-06-01 15:34:25 +03003017 ctxt->dst.type = OP_REG;
3018 ctxt->dst.addr.reg = &ctxt->_eip;
3019 ctxt->dst.bytes = ctxt->op_bytes;
3020 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003021 if (rc != X86EMUL_CONTINUE)
3022 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003023 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003024 return X86EMUL_CONTINUE;
3025}
3026
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003027static int em_add(struct x86_emulate_ctxt *ctxt)
3028{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003029 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003030 return X86EMUL_CONTINUE;
3031}
3032
3033static int em_or(struct x86_emulate_ctxt *ctxt)
3034{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003035 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003036 return X86EMUL_CONTINUE;
3037}
3038
3039static int em_adc(struct x86_emulate_ctxt *ctxt)
3040{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003041 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003042 return X86EMUL_CONTINUE;
3043}
3044
3045static int em_sbb(struct x86_emulate_ctxt *ctxt)
3046{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003047 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003048 return X86EMUL_CONTINUE;
3049}
3050
3051static int em_and(struct x86_emulate_ctxt *ctxt)
3052{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003053 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003054 return X86EMUL_CONTINUE;
3055}
3056
3057static int em_sub(struct x86_emulate_ctxt *ctxt)
3058{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003059 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003060 return X86EMUL_CONTINUE;
3061}
3062
3063static int em_xor(struct x86_emulate_ctxt *ctxt)
3064{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003065 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003066 return X86EMUL_CONTINUE;
3067}
3068
3069static int em_cmp(struct x86_emulate_ctxt *ctxt)
3070{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003071 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003072 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003073 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003074 return X86EMUL_CONTINUE;
3075}
3076
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003077static int em_test(struct x86_emulate_ctxt *ctxt)
3078{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003079 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03003080 /* Disable writeback. */
3081 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003082 return X86EMUL_CONTINUE;
3083}
3084
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003085static int em_xchg(struct x86_emulate_ctxt *ctxt)
3086{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003087 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 ctxt->src.val = ctxt->dst.val;
3089 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003090
3091 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003092 ctxt->dst.val = ctxt->src.orig_val;
3093 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003094 return X86EMUL_CONTINUE;
3095}
3096
Avi Kivity5c82aa22010-08-18 18:31:43 +03003097static int em_imul(struct x86_emulate_ctxt *ctxt)
3098{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003099 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03003100 return X86EMUL_CONTINUE;
3101}
3102
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003103static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3104{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003105 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03003106 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003107}
3108
Avi Kivity61429142010-08-19 15:13:00 +03003109static int em_cwd(struct x86_emulate_ctxt *ctxt)
3110{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003111 ctxt->dst.type = OP_REG;
3112 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003113 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003114 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003115
3116 return X86EMUL_CONTINUE;
3117}
3118
Avi Kivity48bb5d32010-08-18 18:54:34 +03003119static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3120{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003121 u64 tsc = 0;
3122
Avi Kivity717746e2011-04-20 13:37:53 +03003123 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003124 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3125 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003126 return X86EMUL_CONTINUE;
3127}
3128
Avi Kivity222d21a2011-11-10 14:57:30 +02003129static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3130{
3131 u64 pmc;
3132
Avi Kivitydd856ef2012-08-27 23:46:17 +03003133 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003134 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003135 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3136 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003137 return X86EMUL_CONTINUE;
3138}
3139
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003140static int em_mov(struct x86_emulate_ctxt *ctxt)
3141{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003142 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003143 return X86EMUL_CONTINUE;
3144}
3145
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003146static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3147{
3148 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3149 return emulate_gp(ctxt, 0);
3150
3151 /* Disable writeback. */
3152 ctxt->dst.type = OP_NONE;
3153 return X86EMUL_CONTINUE;
3154}
3155
3156static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3157{
3158 unsigned long val;
3159
3160 if (ctxt->mode == X86EMUL_MODE_PROT64)
3161 val = ctxt->src.val & ~0ULL;
3162 else
3163 val = ctxt->src.val & ~0U;
3164
3165 /* #UD condition is already handled. */
3166 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3167 return emulate_gp(ctxt, 0);
3168
3169 /* Disable writeback. */
3170 ctxt->dst.type = OP_NONE;
3171 return X86EMUL_CONTINUE;
3172}
3173
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003174static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3175{
3176 u64 msr_data;
3177
Avi Kivitydd856ef2012-08-27 23:46:17 +03003178 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3179 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3180 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003181 return emulate_gp(ctxt, 0);
3182
3183 return X86EMUL_CONTINUE;
3184}
3185
3186static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3187{
3188 u64 msr_data;
3189
Avi Kivitydd856ef2012-08-27 23:46:17 +03003190 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003191 return emulate_gp(ctxt, 0);
3192
Avi Kivitydd856ef2012-08-27 23:46:17 +03003193 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3194 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003195 return X86EMUL_CONTINUE;
3196}
3197
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003198static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3199{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003200 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003201 return emulate_ud(ctxt);
3202
Avi Kivity9dac77f2011-06-01 15:34:25 +03003203 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003204 return X86EMUL_CONTINUE;
3205}
3206
3207static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3208{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003209 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003210
Avi Kivity9dac77f2011-06-01 15:34:25 +03003211 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003212 return emulate_ud(ctxt);
3213
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003215 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3216
3217 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003218 ctxt->dst.type = OP_NONE;
3219 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003220}
3221
Avi Kivitya14e5792012-06-13 12:28:33 +03003222static int em_lldt(struct x86_emulate_ctxt *ctxt)
3223{
3224 u16 sel = ctxt->src.val;
3225
3226 /* Disable writeback. */
3227 ctxt->dst.type = OP_NONE;
3228 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3229}
3230
Avi Kivity80890002012-06-13 16:33:29 +03003231static int em_ltr(struct x86_emulate_ctxt *ctxt)
3232{
3233 u16 sel = ctxt->src.val;
3234
3235 /* Disable writeback. */
3236 ctxt->dst.type = OP_NONE;
3237 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3238}
3239
Avi Kivity38503912011-03-31 18:48:09 +02003240static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3241{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003242 int rc;
3243 ulong linear;
3244
Avi Kivity9dac77f2011-06-01 15:34:25 +03003245 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003246 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003247 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003248 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003249 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003250 return X86EMUL_CONTINUE;
3251}
3252
Avi Kivity2d04a052011-04-20 15:32:49 +03003253static int em_clts(struct x86_emulate_ctxt *ctxt)
3254{
3255 ulong cr0;
3256
3257 cr0 = ctxt->ops->get_cr(ctxt, 0);
3258 cr0 &= ~X86_CR0_TS;
3259 ctxt->ops->set_cr(ctxt, 0, cr0);
3260 return X86EMUL_CONTINUE;
3261}
3262
Avi Kivity26d05cc2011-04-21 12:07:59 +03003263static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3264{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003265 int rc;
3266
Avi Kivity9dac77f2011-06-01 15:34:25 +03003267 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003268 return X86EMUL_UNHANDLEABLE;
3269
3270 rc = ctxt->ops->fix_hypercall(ctxt);
3271 if (rc != X86EMUL_CONTINUE)
3272 return rc;
3273
3274 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003275 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003276 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003277 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003278 return X86EMUL_CONTINUE;
3279}
3280
Avi Kivity96051572012-06-10 17:21:18 +03003281static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3282 void (*get)(struct x86_emulate_ctxt *ctxt,
3283 struct desc_ptr *ptr))
3284{
3285 struct desc_ptr desc_ptr;
3286
3287 if (ctxt->mode == X86EMUL_MODE_PROT64)
3288 ctxt->op_bytes = 8;
3289 get(ctxt, &desc_ptr);
3290 if (ctxt->op_bytes == 2) {
3291 ctxt->op_bytes = 4;
3292 desc_ptr.address &= 0x00ffffff;
3293 }
3294 /* Disable writeback. */
3295 ctxt->dst.type = OP_NONE;
3296 return segmented_write(ctxt, ctxt->dst.addr.mem,
3297 &desc_ptr, 2 + ctxt->op_bytes);
3298}
3299
3300static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3301{
3302 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3303}
3304
3305static int em_sidt(struct x86_emulate_ctxt *ctxt)
3306{
3307 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3308}
3309
Avi Kivity26d05cc2011-04-21 12:07:59 +03003310static int em_lgdt(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,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003318 &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_gdt(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
Avi Kivity5ef39c72011-04-21 12:21:50 +03003328static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330 int rc;
3331
Avi Kivity5ef39c72011-04-21 12:21:50 +03003332 rc = ctxt->ops->fix_hypercall(ctxt);
3333
Avi Kivity26d05cc2011-04-21 12:07:59 +03003334 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003335 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003336 return rc;
3337}
3338
3339static int em_lidt(struct x86_emulate_ctxt *ctxt)
3340{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003341 struct desc_ptr desc_ptr;
3342 int rc;
3343
Avi Kivity510425f2012-06-07 17:04:36 +03003344 if (ctxt->mode == X86EMUL_MODE_PROT64)
3345 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003346 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003347 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003348 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003349 if (rc != X86EMUL_CONTINUE)
3350 return rc;
3351 ctxt->ops->set_idt(ctxt, &desc_ptr);
3352 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003353 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003354 return X86EMUL_CONTINUE;
3355}
3356
3357static int em_smsw(struct x86_emulate_ctxt *ctxt)
3358{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003359 ctxt->dst.bytes = 2;
3360 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003361 return X86EMUL_CONTINUE;
3362}
3363
3364static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3365{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003366 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003367 | (ctxt->src.val & 0x0f));
3368 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003369 return X86EMUL_CONTINUE;
3370}
3371
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003372static int em_loop(struct x86_emulate_ctxt *ctxt)
3373{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003374 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3375 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003376 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3377 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003378
3379 return X86EMUL_CONTINUE;
3380}
3381
3382static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3383{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003384 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003385 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003386
3387 return X86EMUL_CONTINUE;
3388}
3389
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003390static int em_in(struct x86_emulate_ctxt *ctxt)
3391{
3392 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3393 &ctxt->dst.val))
3394 return X86EMUL_IO_NEEDED;
3395
3396 return X86EMUL_CONTINUE;
3397}
3398
3399static int em_out(struct x86_emulate_ctxt *ctxt)
3400{
3401 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3402 &ctxt->src.val, 1);
3403 /* Disable writeback. */
3404 ctxt->dst.type = OP_NONE;
3405 return X86EMUL_CONTINUE;
3406}
3407
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003408static int em_cli(struct x86_emulate_ctxt *ctxt)
3409{
3410 if (emulator_bad_iopl(ctxt))
3411 return emulate_gp(ctxt, 0);
3412
3413 ctxt->eflags &= ~X86_EFLAGS_IF;
3414 return X86EMUL_CONTINUE;
3415}
3416
3417static int em_sti(struct x86_emulate_ctxt *ctxt)
3418{
3419 if (emulator_bad_iopl(ctxt))
3420 return emulate_gp(ctxt, 0);
3421
3422 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3423 ctxt->eflags |= X86_EFLAGS_IF;
3424 return X86EMUL_CONTINUE;
3425}
3426
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003427static int em_bt(struct x86_emulate_ctxt *ctxt)
3428{
3429 /* Disable writeback. */
3430 ctxt->dst.type = OP_NONE;
3431 /* only subword offset */
3432 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3433
3434 emulate_2op_SrcV_nobyte(ctxt, "bt");
3435 return X86EMUL_CONTINUE;
3436}
3437
3438static int em_bts(struct x86_emulate_ctxt *ctxt)
3439{
3440 emulate_2op_SrcV_nobyte(ctxt, "bts");
3441 return X86EMUL_CONTINUE;
3442}
3443
3444static int em_btr(struct x86_emulate_ctxt *ctxt)
3445{
3446 emulate_2op_SrcV_nobyte(ctxt, "btr");
3447 return X86EMUL_CONTINUE;
3448}
3449
3450static int em_btc(struct x86_emulate_ctxt *ctxt)
3451{
3452 emulate_2op_SrcV_nobyte(ctxt, "btc");
3453 return X86EMUL_CONTINUE;
3454}
3455
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003456static int em_bsf(struct x86_emulate_ctxt *ctxt)
3457{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003458 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003459 return X86EMUL_CONTINUE;
3460}
3461
3462static int em_bsr(struct x86_emulate_ctxt *ctxt)
3463{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003464 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003465 return X86EMUL_CONTINUE;
3466}
3467
Avi Kivity6d6eede2012-06-07 14:11:36 +03003468static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3469{
3470 u32 eax, ebx, ecx, edx;
3471
Avi Kivitydd856ef2012-08-27 23:46:17 +03003472 eax = reg_read(ctxt, VCPU_REGS_RAX);
3473 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003474 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003475 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3476 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3477 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3478 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003479 return X86EMUL_CONTINUE;
3480}
3481
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003482static int em_lahf(struct x86_emulate_ctxt *ctxt)
3483{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003484 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3485 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003486 return X86EMUL_CONTINUE;
3487}
3488
Avi Kivity92998362012-06-13 12:25:06 +03003489static int em_bswap(struct x86_emulate_ctxt *ctxt)
3490{
3491 switch (ctxt->op_bytes) {
3492#ifdef CONFIG_X86_64
3493 case 8:
3494 asm("bswap %0" : "+r"(ctxt->dst.val));
3495 break;
3496#endif
3497 default:
3498 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3499 break;
3500 }
3501 return X86EMUL_CONTINUE;
3502}
3503
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003504static bool valid_cr(int nr)
3505{
3506 switch (nr) {
3507 case 0:
3508 case 2 ... 4:
3509 case 8:
3510 return true;
3511 default:
3512 return false;
3513 }
3514}
3515
3516static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3517{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003518 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003519 return emulate_ud(ctxt);
3520
3521 return X86EMUL_CONTINUE;
3522}
3523
3524static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3525{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003526 u64 new_val = ctxt->src.val64;
3527 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003528 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003529
3530 static u64 cr_reserved_bits[] = {
3531 0xffffffff00000000ULL,
3532 0, 0, 0, /* CR3 checked later */
3533 CR4_RESERVED_BITS,
3534 0, 0, 0,
3535 CR8_RESERVED_BITS,
3536 };
3537
3538 if (!valid_cr(cr))
3539 return emulate_ud(ctxt);
3540
3541 if (new_val & cr_reserved_bits[cr])
3542 return emulate_gp(ctxt, 0);
3543
3544 switch (cr) {
3545 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003546 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003547 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3548 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3549 return emulate_gp(ctxt, 0);
3550
Avi Kivity717746e2011-04-20 13:37:53 +03003551 cr4 = ctxt->ops->get_cr(ctxt, 4);
3552 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003553
3554 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3555 !(cr4 & X86_CR4_PAE))
3556 return emulate_gp(ctxt, 0);
3557
3558 break;
3559 }
3560 case 3: {
3561 u64 rsvd = 0;
3562
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003563 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3564 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003565 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003566 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003567 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003568 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003569 rsvd = CR3_NONPAE_RESERVED_BITS;
3570
3571 if (new_val & rsvd)
3572 return emulate_gp(ctxt, 0);
3573
3574 break;
3575 }
3576 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003577 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003578
3579 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3580 return emulate_gp(ctxt, 0);
3581
3582 break;
3583 }
3584 }
3585
3586 return X86EMUL_CONTINUE;
3587}
3588
Joerg Roedel3b88e412011-04-04 12:39:29 +02003589static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3590{
3591 unsigned long dr7;
3592
Avi Kivity717746e2011-04-20 13:37:53 +03003593 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003594
3595 /* Check if DR7.Global_Enable is set */
3596 return dr7 & (1 << 13);
3597}
3598
3599static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3600{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003601 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003602 u64 cr4;
3603
3604 if (dr > 7)
3605 return emulate_ud(ctxt);
3606
Avi Kivity717746e2011-04-20 13:37:53 +03003607 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003608 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3609 return emulate_ud(ctxt);
3610
3611 if (check_dr7_gd(ctxt))
3612 return emulate_db(ctxt);
3613
3614 return X86EMUL_CONTINUE;
3615}
3616
3617static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3618{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003619 u64 new_val = ctxt->src.val64;
3620 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003621
3622 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3623 return emulate_gp(ctxt, 0);
3624
3625 return check_dr_read(ctxt);
3626}
3627
Joerg Roedel01de8b02011-04-04 12:39:31 +02003628static int check_svme(struct x86_emulate_ctxt *ctxt)
3629{
3630 u64 efer;
3631
Avi Kivity717746e2011-04-20 13:37:53 +03003632 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003633
3634 if (!(efer & EFER_SVME))
3635 return emulate_ud(ctxt);
3636
3637 return X86EMUL_CONTINUE;
3638}
3639
3640static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3641{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003642 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003643
3644 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003645 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003646 return emulate_gp(ctxt, 0);
3647
3648 return check_svme(ctxt);
3649}
3650
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003651static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3652{
Avi Kivity717746e2011-04-20 13:37:53 +03003653 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003654
Avi Kivity717746e2011-04-20 13:37:53 +03003655 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003656 return emulate_ud(ctxt);
3657
3658 return X86EMUL_CONTINUE;
3659}
3660
Joerg Roedel80612522011-04-04 12:39:33 +02003661static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3662{
Avi Kivity717746e2011-04-20 13:37:53 +03003663 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003664 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003665
Avi Kivity717746e2011-04-20 13:37:53 +03003666 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003667 (rcx > 3))
3668 return emulate_gp(ctxt, 0);
3669
3670 return X86EMUL_CONTINUE;
3671}
3672
Joerg Roedelf6511932011-04-04 12:39:35 +02003673static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3674{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003675 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3676 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003677 return emulate_gp(ctxt, 0);
3678
3679 return X86EMUL_CONTINUE;
3680}
3681
3682static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3683{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003684 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3685 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003686 return emulate_gp(ctxt, 0);
3687
3688 return X86EMUL_CONTINUE;
3689}
3690
Avi Kivity73fba5f2010-07-29 15:11:53 +03003691#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003692#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003693#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3694 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003695#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003696#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003697#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3698#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003699#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003700#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003701#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003702#define II(_f, _e, _i) \
3703 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003704#define IIP(_f, _e, _i, _p) \
3705 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3706 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003707#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003708
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003709#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003710#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003711#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003712#define I2bvIP(_f, _e, _i, _p) \
3713 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003714
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003715#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3716 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3717 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003718
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003719static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003720 DI(SrcNone | Priv, monitor),
3721 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003722 N, N, N, N, N, N,
3723};
3724
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003725static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003726 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3727 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3728 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3729 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3730 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3731 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3732 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3733 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003734};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003735
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003736static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003737 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003738 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003739 N, N, N, N, N, N,
3740};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003741
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003742static const struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003743 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003744 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003745 I(Lock, em_adc),
3746 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003747 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003748 I(Lock, em_sub),
3749 I(Lock, em_xor),
3750 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751};
3752
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003753static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003754 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003755};
3756
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003757static const struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003758 I(DstMem | SrcImm, em_test),
3759 I(DstMem | SrcImm, em_test),
3760 I(DstMem | SrcNone | Lock, em_not),
3761 I(DstMem | SrcNone | Lock, em_neg),
3762 I(SrcMem, em_mul_ex),
3763 I(SrcMem, em_imul_ex),
3764 I(SrcMem, em_div_ex),
3765 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003766};
3767
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003768static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003769 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3770 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771 N, N, N, N, N, N,
3772};
3773
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003774static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003775 I(DstMem | SrcNone | Lock, em_grp45),
3776 I(DstMem | SrcNone | Lock, em_grp45),
3777 I(SrcMem | Stack, em_grp45),
3778 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3779 I(SrcMem | Stack, em_grp45),
3780 I(SrcMemFAddr | ImplicitOps, em_grp45),
3781 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003782};
3783
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003784static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003785 DI(Prot, sldt),
3786 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003787 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003788 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003789 N, N, N, N,
3790};
3791
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003792static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003793 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3794 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003795 II(SrcMem | Priv, em_lgdt, lgdt),
3796 II(SrcMem | Priv, em_lidt, lidt),
3797 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3798 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3799 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003800}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003801 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003802 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003803 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003804 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3805 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3806 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003807} };
3808
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003809static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003810 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003811 I(DstMem | SrcImmByte, em_bt),
3812 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3813 I(DstMem | SrcImmByte | Lock, em_btr),
3814 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815};
3816
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003817static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003818 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003819}, {
3820 N, N, N, N, N, N, N, N,
3821} };
3822
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003823static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003824 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003825 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003826};
3827
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003828static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003829 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003830};
3831
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003832static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003833 I(0, em_mov), N, N, N,
3834};
3835
Gleb Natapov045a2822012-12-20 16:57:43 +02003836static const struct escape escape_d9 = { {
3837 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3838}, {
3839 /* 0xC0 - 0xC7 */
3840 N, N, N, N, N, N, N, N,
3841 /* 0xC8 - 0xCF */
3842 N, N, N, N, N, N, N, N,
3843 /* 0xD0 - 0xC7 */
3844 N, N, N, N, N, N, N, N,
3845 /* 0xD8 - 0xDF */
3846 N, N, N, N, N, N, N, N,
3847 /* 0xE0 - 0xE7 */
3848 N, N, N, N, N, N, N, N,
3849 /* 0xE8 - 0xEF */
3850 N, N, N, N, N, N, N, N,
3851 /* 0xF0 - 0xF7 */
3852 N, N, N, N, N, N, N, N,
3853 /* 0xF8 - 0xFF */
3854 N, N, N, N, N, N, N, N,
3855} };
3856
3857static const struct escape escape_db = { {
3858 N, N, N, N, N, N, N, N,
3859}, {
3860 /* 0xC0 - 0xC7 */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xC8 - 0xCF */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xD0 - 0xC7 */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xD8 - 0xDF */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xE0 - 0xE7 */
3869 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3870 /* 0xE8 - 0xEF */
3871 N, N, N, N, N, N, N, N,
3872 /* 0xF0 - 0xF7 */
3873 N, N, N, N, N, N, N, N,
3874 /* 0xF8 - 0xFF */
3875 N, N, N, N, N, N, N, N,
3876} };
3877
3878static const struct escape escape_dd = { {
3879 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3880}, {
3881 /* 0xC0 - 0xC7 */
3882 N, N, N, N, N, N, N, N,
3883 /* 0xC8 - 0xCF */
3884 N, N, N, N, N, N, N, N,
3885 /* 0xD0 - 0xC7 */
3886 N, N, N, N, N, N, N, N,
3887 /* 0xD8 - 0xDF */
3888 N, N, N, N, N, N, N, N,
3889 /* 0xE0 - 0xE7 */
3890 N, N, N, N, N, N, N, N,
3891 /* 0xE8 - 0xEF */
3892 N, N, N, N, N, N, N, N,
3893 /* 0xF0 - 0xF7 */
3894 N, N, N, N, N, N, N, N,
3895 /* 0xF8 - 0xFF */
3896 N, N, N, N, N, N, N, N,
3897} };
3898
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003899static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003900 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003901 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003902 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3903 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003905 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003906 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3907 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003908 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003909 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003910 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3911 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003912 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003913 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003914 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3915 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003916 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003917 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003918 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003919 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003920 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003921 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003922 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003923 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003924 /* 0x40 - 0x4F */
3925 X16(D(DstReg)),
3926 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003927 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003928 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003929 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003931 I(ImplicitOps | Stack | No64, em_pusha),
3932 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003933 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3934 N, N, N, N,
3935 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003936 I(SrcImm | Mov | Stack, em_push),
3937 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003938 I(SrcImmByte | Mov | Stack, em_push),
3939 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003940 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003941 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0x70 - 0x7F */
3943 X16(D(SrcImmByte)),
3944 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003945 G(ByteOp | DstMem | SrcImm, group1),
3946 G(DstMem | SrcImm, group1),
3947 G(ByteOp | DstMem | SrcImm | No64, group1),
3948 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003949 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003950 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003952 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003953 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003954 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003955 D(ModRM | SrcMem | NoAccess | DstReg),
3956 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3957 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003959 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003960 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003961 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003962 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003963 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003964 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003965 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003966 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003967 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003968 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003969 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003970 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003971 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003972 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3973 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003974 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003976 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003977 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003978 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003979 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003980 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003981 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003982 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003983 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3984 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003985 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003986 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003987 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3988 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003989 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003990 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003991 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003992 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Gleb Natapov7f662272012-12-10 11:42:30 +02003993 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003995 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003996 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003997 X3(I(SrcImmByte, em_loop)),
3998 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003999 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
4000 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09004002 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004003 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004004 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4005 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004007 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004008 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4009 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004010 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004011 D(ImplicitOps), D(ImplicitOps),
4012 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4014};
4015
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004016static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004018 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004019 N, I(ImplicitOps | VendorSpecific, em_syscall),
4020 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004021 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 N, D(ImplicitOps | ModRM), N, N,
4023 /* 0x10 - 0x1F */
4024 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
4025 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004026 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004027 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004028 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4029 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004030 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004031 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4032 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004033 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004034 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004035 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004036 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004037 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004038 I(ImplicitOps | VendorSpecific, em_sysenter),
4039 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004040 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004041 N, N, N, N, N, N, N, N,
4042 /* 0x40 - 0x4F */
4043 X16(D(DstReg | SrcMem | ModRM | Mov)),
4044 /* 0x50 - 0x5F */
4045 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4046 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004047 N, N, N, N,
4048 N, N, N, N,
4049 N, N, N, N,
4050 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004051 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004052 N, N, N, N,
4053 N, N, N, N,
4054 N, N, N, N,
4055 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004056 /* 0x80 - 0x8F */
4057 X16(D(SrcImm)),
4058 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004059 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004061 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03004062 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004063 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4064 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
4065 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004066 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004067 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004068 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004069 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4070 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004071 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004073 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004074 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004075 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004076 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4077 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004078 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004079 /* 0xB8 - 0xBF */
4080 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004081 G(BitOp, group8),
4082 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09004083 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004084 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004085 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004086 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004087 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004088 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004089 /* 0xC8 - 0xCF */
4090 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004091 /* 0xD0 - 0xDF */
4092 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4093 /* 0xE0 - 0xEF */
4094 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4095 /* 0xF0 - 0xFF */
4096 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4097};
4098
4099#undef D
4100#undef N
4101#undef G
4102#undef GD
4103#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004104#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004105#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004106
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004107#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004108#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004109#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004110#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004111#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004112
Avi Kivity9dac77f2011-06-01 15:34:25 +03004113static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004114{
4115 unsigned size;
4116
Avi Kivity9dac77f2011-06-01 15:34:25 +03004117 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004118 if (size == 8)
4119 size = 4;
4120 return size;
4121}
4122
4123static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4124 unsigned size, bool sign_extension)
4125{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004126 int rc = X86EMUL_CONTINUE;
4127
4128 op->type = OP_IMM;
4129 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004130 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004131 /* NB. Immediates are sign-extended as necessary. */
4132 switch (op->bytes) {
4133 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004134 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004135 break;
4136 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004137 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004138 break;
4139 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004140 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004141 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004142 case 8:
4143 op->val = insn_fetch(s64, ctxt);
4144 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004145 }
4146 if (!sign_extension) {
4147 switch (op->bytes) {
4148 case 1:
4149 op->val &= 0xff;
4150 break;
4151 case 2:
4152 op->val &= 0xffff;
4153 break;
4154 case 4:
4155 op->val &= 0xffffffff;
4156 break;
4157 }
4158 }
4159done:
4160 return rc;
4161}
4162
Avi Kivitya9945542011-09-13 10:45:41 +03004163static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4164 unsigned d)
4165{
4166 int rc = X86EMUL_CONTINUE;
4167
4168 switch (d) {
4169 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004170 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004171 break;
4172 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004173 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004174 break;
4175 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004176 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004177 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004178 *op = ctxt->memop;
4179 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004180 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004181 fetch_bit_operand(ctxt);
4182 op->orig_val = op->val;
4183 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004184 case OpMem64:
4185 ctxt->memop.bytes = 8;
4186 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004187 case OpAcc:
4188 op->type = OP_REG;
4189 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004190 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004191 fetch_register_operand(op);
4192 op->orig_val = op->val;
4193 break;
4194 case OpDI:
4195 op->type = OP_MEM;
4196 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4197 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004198 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004199 op->addr.mem.seg = VCPU_SREG_ES;
4200 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004201 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004202 break;
4203 case OpDX:
4204 op->type = OP_REG;
4205 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004206 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004207 fetch_register_operand(op);
4208 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004209 case OpCL:
4210 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004211 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004212 break;
4213 case OpImmByte:
4214 rc = decode_imm(ctxt, op, 1, true);
4215 break;
4216 case OpOne:
4217 op->bytes = 1;
4218 op->val = 1;
4219 break;
4220 case OpImm:
4221 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4222 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004223 case OpImm64:
4224 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4225 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004226 case OpMem8:
4227 ctxt->memop.bytes = 1;
4228 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004229 case OpMem16:
4230 ctxt->memop.bytes = 2;
4231 goto mem_common;
4232 case OpMem32:
4233 ctxt->memop.bytes = 4;
4234 goto mem_common;
4235 case OpImmU16:
4236 rc = decode_imm(ctxt, op, 2, false);
4237 break;
4238 case OpImmU:
4239 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4240 break;
4241 case OpSI:
4242 op->type = OP_MEM;
4243 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4244 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004245 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004246 op->addr.mem.seg = seg_override(ctxt);
4247 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004248 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004249 break;
4250 case OpImmFAddr:
4251 op->type = OP_IMM;
4252 op->addr.mem.ea = ctxt->_eip;
4253 op->bytes = ctxt->op_bytes + 2;
4254 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4255 break;
4256 case OpMemFAddr:
4257 ctxt->memop.bytes = ctxt->op_bytes + 2;
4258 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004259 case OpES:
4260 op->val = VCPU_SREG_ES;
4261 break;
4262 case OpCS:
4263 op->val = VCPU_SREG_CS;
4264 break;
4265 case OpSS:
4266 op->val = VCPU_SREG_SS;
4267 break;
4268 case OpDS:
4269 op->val = VCPU_SREG_DS;
4270 break;
4271 case OpFS:
4272 op->val = VCPU_SREG_FS;
4273 break;
4274 case OpGS:
4275 op->val = VCPU_SREG_GS;
4276 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004277 case OpImplicit:
4278 /* Special instructions do their own operand decoding. */
4279 default:
4280 op->type = OP_NONE; /* Disable writeback. */
4281 break;
4282 }
4283
4284done:
4285 return rc;
4286}
4287
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004288int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004289{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004290 int rc = X86EMUL_CONTINUE;
4291 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004292 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004293 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004294 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004295
Avi Kivityf09ed832011-09-13 10:45:40 +03004296 ctxt->memop.type = OP_NONE;
4297 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004298 ctxt->_eip = ctxt->eip;
4299 ctxt->fetch.start = ctxt->_eip;
4300 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004301 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004302 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004303
4304 switch (mode) {
4305 case X86EMUL_MODE_REAL:
4306 case X86EMUL_MODE_VM86:
4307 case X86EMUL_MODE_PROT16:
4308 def_op_bytes = def_ad_bytes = 2;
4309 break;
4310 case X86EMUL_MODE_PROT32:
4311 def_op_bytes = def_ad_bytes = 4;
4312 break;
4313#ifdef CONFIG_X86_64
4314 case X86EMUL_MODE_PROT64:
4315 def_op_bytes = 4;
4316 def_ad_bytes = 8;
4317 break;
4318#endif
4319 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004320 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004321 }
4322
Avi Kivity9dac77f2011-06-01 15:34:25 +03004323 ctxt->op_bytes = def_op_bytes;
4324 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004325
4326 /* Legacy prefixes. */
4327 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004328 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004329 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004330 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004331 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004332 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004333 break;
4334 case 0x67: /* address-size override */
4335 if (mode == X86EMUL_MODE_PROT64)
4336 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004337 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004338 else
4339 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004340 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004341 break;
4342 case 0x26: /* ES override */
4343 case 0x2e: /* CS override */
4344 case 0x36: /* SS override */
4345 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004346 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004347 break;
4348 case 0x64: /* FS override */
4349 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004350 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004351 break;
4352 case 0x40 ... 0x4f: /* REX */
4353 if (mode != X86EMUL_MODE_PROT64)
4354 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004355 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004356 continue;
4357 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004358 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004359 break;
4360 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004361 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004362 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004363 break;
4364 default:
4365 goto done_prefixes;
4366 }
4367
4368 /* Any legacy prefix after a REX prefix nullifies its effect. */
4369
Avi Kivity9dac77f2011-06-01 15:34:25 +03004370 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004371 }
4372
4373done_prefixes:
4374
4375 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 if (ctxt->rex_prefix & 8)
4377 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004378
4379 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004380 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004381 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004382 if (ctxt->b == 0x0f) {
4383 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004384 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004385 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004386 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004387 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004388
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004389 if (ctxt->d & ModRM)
4390 ctxt->modrm = insn_fetch(u8, ctxt);
4391
Avi Kivity9dac77f2011-06-01 15:34:25 +03004392 while (ctxt->d & GroupMask) {
4393 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004394 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004395 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004396 opcode = opcode.u.group[goffset];
4397 break;
4398 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004399 goffset = (ctxt->modrm >> 3) & 7;
4400 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004401 opcode = opcode.u.gdual->mod3[goffset];
4402 else
4403 opcode = opcode.u.gdual->mod012[goffset];
4404 break;
4405 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004406 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004407 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004408 break;
4409 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004410 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004411 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004413 switch (simd_prefix) {
4414 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4415 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4416 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4417 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4418 }
4419 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004420 case Escape:
4421 if (ctxt->modrm > 0xbf)
4422 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4423 else
4424 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4425 break;
Avi Kivity46561642011-04-24 14:09:59 +03004426 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004427 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004428 }
Avi Kivity46561642011-04-24 14:09:59 +03004429
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004430 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004431 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004432 }
4433
Avi Kivity9dac77f2011-06-01 15:34:25 +03004434 ctxt->execute = opcode.u.execute;
4435 ctxt->check_perm = opcode.check_perm;
4436 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004437
4438 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004440 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004441
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004443 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004444
Avi Kivity9dac77f2011-06-01 15:34:25 +03004445 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4446 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004447
Avi Kivity9dac77f2011-06-01 15:34:25 +03004448 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004449 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004451 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004452 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004453 }
4454
Avi Kivity9dac77f2011-06-01 15:34:25 +03004455 if (ctxt->d & Sse)
4456 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004457 else if (ctxt->d & Mmx)
4458 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004459
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004460 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004461 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004462 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004463 if (!ctxt->has_seg_override)
4464 set_seg_override(ctxt, ctxt->modrm_seg);
4465 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004466 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004467 if (rc != X86EMUL_CONTINUE)
4468 goto done;
4469
Avi Kivity9dac77f2011-06-01 15:34:25 +03004470 if (!ctxt->has_seg_override)
4471 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004472
Avi Kivityf09ed832011-09-13 10:45:40 +03004473 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474
Avi Kivityf09ed832011-09-13 10:45:40 +03004475 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4476 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004477
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004478 /*
4479 * Decode and fetch the source operand: register, memory
4480 * or immediate.
4481 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004482 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004483 if (rc != X86EMUL_CONTINUE)
4484 goto done;
4485
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004486 /*
4487 * Decode and fetch the second source operand: register, memory
4488 * or immediate.
4489 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004490 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004491 if (rc != X86EMUL_CONTINUE)
4492 goto done;
4493
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004494 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004495 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004496
4497done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004498 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4499 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004500
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004501 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004502}
4503
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004504bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4505{
4506 return ctxt->d & PageTable;
4507}
4508
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004509static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4510{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004511 /* The second termination condition only applies for REPE
4512 * and REPNE. Test if the repeat string operation prefix is
4513 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4514 * corresponding termination condition according to:
4515 * - if REPE/REPZ and ZF = 0 then done
4516 * - if REPNE/REPNZ and ZF = 1 then done
4517 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004518 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4519 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4520 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004521 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004522 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004523 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4524 return true;
4525
4526 return false;
4527}
4528
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004529static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4530{
4531 bool fault = false;
4532
4533 ctxt->ops->get_fpu(ctxt);
4534 asm volatile("1: fwait \n\t"
4535 "2: \n\t"
4536 ".pushsection .fixup,\"ax\" \n\t"
4537 "3: \n\t"
4538 "movb $1, %[fault] \n\t"
4539 "jmp 2b \n\t"
4540 ".popsection \n\t"
4541 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004542 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004543 ctxt->ops->put_fpu(ctxt);
4544
4545 if (unlikely(fault))
4546 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4547
4548 return X86EMUL_CONTINUE;
4549}
4550
4551static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4552 struct operand *op)
4553{
4554 if (op->type == OP_MM)
4555 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4556}
4557
Avi Kivitye28bbd42013-01-04 16:18:48 +02004558static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4559{
4560 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4561 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4562 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4563 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4564 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4565 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4566 return X86EMUL_CONTINUE;
4567}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004568
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004569int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004570{
Mathias Krause0225fb52012-08-30 01:30:16 +02004571 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004572 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004574
Avi Kivity9dac77f2011-06-01 15:34:25 +03004575 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004576
Avi Kivity9dac77f2011-06-01 15:34:25 +03004577 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004578 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004579 goto done;
4580 }
4581
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004582 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004583 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004584 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004585 goto done;
4586 }
4587
Avi Kivity9dac77f2011-06-01 15:34:25 +03004588 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004589 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004590 goto done;
4591 }
4592
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004593 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4594 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004595 rc = emulate_ud(ctxt);
4596 goto done;
4597 }
4598
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004599 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004600 rc = emulate_nm(ctxt);
4601 goto done;
4602 }
4603
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004604 if (ctxt->d & Mmx) {
4605 rc = flush_pending_x87_faults(ctxt);
4606 if (rc != X86EMUL_CONTINUE)
4607 goto done;
4608 /*
4609 * Now that we know the fpu is exception safe, we can fetch
4610 * operands from it.
4611 */
4612 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4613 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4614 if (!(ctxt->d & Mov))
4615 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4616 }
4617
Avi Kivity9dac77f2011-06-01 15:34:25 +03004618 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4619 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004620 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004621 if (rc != X86EMUL_CONTINUE)
4622 goto done;
4623 }
4624
Gleb Natapove92805a2010-02-10 14:21:35 +02004625 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004626 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004627 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004628 goto done;
4629 }
4630
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004631 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004632 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004633 rc = emulate_ud(ctxt);
4634 goto done;
4635 }
4636
Joerg Roedeld09beab2011-04-04 12:39:25 +02004637 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004638 if (ctxt->check_perm) {
4639 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004640 if (rc != X86EMUL_CONTINUE)
4641 goto done;
4642 }
4643
Avi Kivity9dac77f2011-06-01 15:34:25 +03004644 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4645 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004646 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004647 if (rc != X86EMUL_CONTINUE)
4648 goto done;
4649 }
4650
Avi Kivity9dac77f2011-06-01 15:34:25 +03004651 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004652 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004653 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004654 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004655 goto done;
4656 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004657 }
4658
Avi Kivity9dac77f2011-06-01 15:34:25 +03004659 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4660 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4661 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004662 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004663 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004664 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004665 }
4666
Avi Kivity9dac77f2011-06-01 15:34:25 +03004667 if (ctxt->src2.type == OP_MEM) {
4668 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4669 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004670 if (rc != X86EMUL_CONTINUE)
4671 goto done;
4672 }
4673
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004675 goto special_insn;
4676
4677
Avi Kivity9dac77f2011-06-01 15:34:25 +03004678 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004679 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004680 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4681 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004682 if (rc != X86EMUL_CONTINUE)
4683 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004684 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004685 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004686
Avi Kivity018a98d2007-11-27 19:30:56 +02004687special_insn:
4688
Avi Kivity9dac77f2011-06-01 15:34:25 +03004689 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4690 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004691 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004692 if (rc != X86EMUL_CONTINUE)
4693 goto done;
4694 }
4695
Avi Kivity9dac77f2011-06-01 15:34:25 +03004696 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004697 if (ctxt->d & Fastop) {
4698 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4699 rc = fastop(ctxt, fop);
4700 if (rc != X86EMUL_CONTINUE)
4701 goto done;
4702 goto writeback;
4703 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004704 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004705 if (rc != X86EMUL_CONTINUE)
4706 goto done;
4707 goto writeback;
4708 }
4709
Avi Kivity9dac77f2011-06-01 15:34:25 +03004710 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711 goto twobyte_insn;
4712
Avi Kivity9dac77f2011-06-01 15:34:25 +03004713 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004714 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004715 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004716 break;
4717 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004718 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004719 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004720 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004721 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004722 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004723 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004725 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004726 if (test_cc(ctxt->b, ctxt->eflags))
4727 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004728 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004729 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004730 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004731 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004732 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004733 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004734 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004735 rc = em_xchg(ctxt);
4736 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004737 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 switch (ctxt->op_bytes) {
4739 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4740 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4741 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004742 }
4743 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004744 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004745 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004746 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004747 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004748 rc = emulate_int(ctxt, 3);
4749 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004750 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004751 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004752 break;
4753 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004754 if (ctxt->eflags & EFLG_OF)
4755 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004756 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004757 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004758 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004759 break;
4760 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004761 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004762 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004763 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004764 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004765 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004766 jmp_rel(ctxt, ctxt->src.val);
4767 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004768 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004769 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004770 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004771 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004772 case 0xf5: /* cmc */
4773 /* complement carry flag from eflags reg */
4774 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004775 break;
4776 case 0xf8: /* clc */
4777 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004778 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004779 case 0xf9: /* stc */
4780 ctxt->eflags |= EFLG_CF;
4781 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004782 case 0xfc: /* cld */
4783 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004784 break;
4785 case 0xfd: /* std */
4786 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004787 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004788 default:
4789 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004791
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004792 if (rc != X86EMUL_CONTINUE)
4793 goto done;
4794
Avi Kivity018a98d2007-11-27 19:30:56 +02004795writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004796 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004797 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004798 goto done;
4799
Gleb Natapov5cd21912010-03-18 15:20:26 +02004800 /*
4801 * restore dst type in case the decoding will be reused
4802 * (happens for string instruction )
4803 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004804 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004805
Avi Kivity9dac77f2011-06-01 15:34:25 +03004806 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004807 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004808
Avi Kivity9dac77f2011-06-01 15:34:25 +03004809 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004810 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004811
Avi Kivity9dac77f2011-06-01 15:34:25 +03004812 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004813 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004814 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004815 if ((ctxt->d & SrcMask) == SrcSI)
4816 count = ctxt->src.count;
4817 else
4818 count = ctxt->dst.count;
4819 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4820 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004821
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004822 if (!string_insn_completed(ctxt)) {
4823 /*
4824 * Re-enter guest when pio read ahead buffer is empty
4825 * or, if it is not used, after each 1024 iteration.
4826 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004827 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004828 (r->end == 0 || r->end != r->pos)) {
4829 /*
4830 * Reset read cache. Usually happens before
4831 * decode, but since instruction is restarted
4832 * we have to do it here.
4833 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004834 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004835 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004836 return EMULATION_RESTART;
4837 }
4838 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004839 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004840 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004841
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004843
4844done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004845 if (rc == X86EMUL_PROPAGATE_FAULT)
4846 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004847 if (rc == X86EMUL_INTERCEPTED)
4848 return EMULATION_INTERCEPTED;
4849
Avi Kivitydd856ef2012-08-27 23:46:17 +03004850 if (rc == X86EMUL_CONTINUE)
4851 writeback_registers(ctxt);
4852
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004853 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004854
4855twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004856 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004857 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004858 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004859 break;
4860 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004861 case 0x0d: /* GrpP (prefetch) */
4862 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004863 break;
4864 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004865 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004866 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004868 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004871 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4872 if (!test_cc(ctxt->b, ctxt->eflags))
4873 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004875 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004876 if (test_cc(ctxt->b, ctxt->eflags))
4877 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004878 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004879 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004880 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004881 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004882 case 0xa4: /* shld imm8, r, r/m */
4883 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004884 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004885 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004886 case 0xac: /* shrd imm8, r, r/m */
4887 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004888 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004889 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004890 case 0xae: /* clflush */
4891 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004893 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004894 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004895 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004897 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004898 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004899 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004900 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004901 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004902 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004903 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004904 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004905 ctxt->src.val = ctxt->dst.orig_val;
4906 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004907 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004908 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004909 ctxt->dst.bytes = ctxt->op_bytes;
4910 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4911 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004912 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004913 default:
4914 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004915 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004916
4917 if (rc != X86EMUL_CONTINUE)
4918 goto done;
4919
Avi Kivity6aa8b732006-12-10 02:21:36 -08004920 goto writeback;
4921
4922cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004923 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004924}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004925
4926void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4927{
4928 invalidate_registers(ctxt);
4929}
4930
4931void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4932{
4933 writeback_registers(ctxt);
4934}