blob: 42c53c8071bee2bd834768e191fdb8d3a42e1f89 [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 Kivitya99455492011-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 Kivitya99455492011-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 Kivitya99455492011-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 Kivitya99455492011-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 Kivity6aa8b732006-12-10 02:21:36 -0800154
Avi Kivityd0e53322010-07-29 15:11:54 +0300155#define X2(x...) x, x
156#define X3(x...) X2(x), x
157#define X4(x...) X2(x), X2(x)
158#define X5(x...) X4(x), x
159#define X6(x...) X4(x), X2(x)
160#define X7(x...) X4(x), X3(x)
161#define X8(x...) X4(x), X4(x)
162#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300163
Avi Kivitye28bbd42013-01-04 16:18:48 +0200164#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
165#define FASTOP_SIZE 8
166
167/*
168 * fastop functions have a special calling convention:
169 *
170 * dst: [rdx]:rax (in/out)
171 * src: rbx (in/out)
172 * src2: rcx (in)
173 * flags: rflags (in/out)
174 *
175 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
176 * different operand sizes can be reached by calculation, rather than a jump
177 * table (which would be bigger than the code).
178 *
179 * fastop functions are declared as taking a never-defined fastop parameter,
180 * so they can't be called from C directly.
181 */
182
183struct fastop;
184
Avi Kivityd65b1de2010-07-29 15:11:35 +0300185struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300186 u64 flags : 56;
187 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300188 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300189 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200190 const struct opcode *group;
191 const struct group_dual *gdual;
192 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200193 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200194 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300195 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200196 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300197};
198
199struct group_dual {
200 struct opcode mod012[8];
201 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300202};
203
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200204struct gprefix {
205 struct opcode pfx_no;
206 struct opcode pfx_66;
207 struct opcode pfx_f2;
208 struct opcode pfx_f3;
209};
210
Gleb Natapov045a2822012-12-20 16:57:43 +0200211struct escape {
212 struct opcode op[8];
213 struct opcode high[64];
214};
215
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200217#define EFLG_ID (1<<21)
218#define EFLG_VIP (1<<20)
219#define EFLG_VIF (1<<19)
220#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200221#define EFLG_VM (1<<17)
222#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200223#define EFLG_IOPL (3<<12)
224#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225#define EFLG_OF (1<<11)
226#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200227#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200228#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229#define EFLG_SF (1<<7)
230#define EFLG_ZF (1<<6)
231#define EFLG_AF (1<<4)
232#define EFLG_PF (1<<2)
233#define EFLG_CF (1<<0)
234
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300235#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
236#define EFLG_RESERVED_ONE_MASK 2
237
Avi Kivitydd856ef2012-08-27 23:46:17 +0300238static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
239{
240 if (!(ctxt->regs_valid & (1 << nr))) {
241 ctxt->regs_valid |= 1 << nr;
242 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
243 }
244 return ctxt->_regs[nr];
245}
246
247static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
248{
249 ctxt->regs_valid |= 1 << nr;
250 ctxt->regs_dirty |= 1 << nr;
251 return &ctxt->_regs[nr];
252}
253
254static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
255{
256 reg_read(ctxt, nr);
257 return reg_write(ctxt, nr);
258}
259
260static void writeback_registers(struct x86_emulate_ctxt *ctxt)
261{
262 unsigned reg;
263
264 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
265 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
266}
267
268static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
269{
270 ctxt->regs_dirty = 0;
271 ctxt->regs_valid = 0;
272}
273
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274/*
275 * Instruction emulation:
276 * Most instructions are emulated directly via a fragment of inline assembly
277 * code. This allows us to save/restore EFLAGS and thus very easily pick up
278 * any modified flags.
279 */
280
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800281#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282#define _LO32 "k" /* force 32-bit operand */
283#define _STK "%%rsp" /* stack pointer */
284#elif defined(__i386__)
285#define _LO32 "" /* force 32-bit operand */
286#define _STK "%%esp" /* stack pointer */
287#endif
288
289/*
290 * These EFLAGS bits are restored from saved value during emulation, and
291 * any changes are written back to the saved value after emulation.
292 */
293#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
294
295/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200296#define _PRE_EFLAGS(_sav, _msk, _tmp) \
297 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
298 "movl %"_sav",%"_LO32 _tmp"; " \
299 "push %"_tmp"; " \
300 "push %"_tmp"; " \
301 "movl %"_msk",%"_LO32 _tmp"; " \
302 "andl %"_LO32 _tmp",("_STK"); " \
303 "pushf; " \
304 "notl %"_LO32 _tmp"; " \
305 "andl %"_LO32 _tmp",("_STK"); " \
306 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
307 "pop %"_tmp"; " \
308 "orl %"_LO32 _tmp",("_STK"); " \
309 "popf; " \
310 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311
312/* After executing instruction: write-back necessary bits in EFLAGS. */
313#define _POST_EFLAGS(_sav, _msk, _tmp) \
314 /* _sav |= EFLAGS & _msk; */ \
315 "pushf; " \
316 "pop %"_tmp"; " \
317 "andl %"_msk",%"_LO32 _tmp"; " \
318 "orl %"_LO32 _tmp",%"_sav"; "
319
Avi Kivitydda96d82008-11-26 15:14:10 +0200320#ifdef CONFIG_X86_64
321#define ON64(x) x
322#else
323#define ON64(x)
324#endif
325
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300326#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200327 do { \
328 __asm__ __volatile__ ( \
329 _PRE_EFLAGS("0", "4", "2") \
330 _op _suffix " %"_x"3,%1; " \
331 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300332 : "=m" ((ctxt)->eflags), \
333 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200334 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300335 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200336 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200337
338
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300340#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200341 do { \
342 unsigned long _tmp; \
343 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300344 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200345 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300346 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200347 break; \
348 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300349 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200350 break; \
351 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300352 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200353 break; \
354 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 } while (0)
356
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300357#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200359 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300360 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300362 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363 break; \
364 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300365 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 _wx, _wy, _lx, _ly, _qx, _qy); \
367 break; \
368 } \
369 } while (0)
370
371/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300372#define emulate_2op_SrcB(ctxt, _op) \
373 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374
375/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300376#define emulate_2op_SrcV(ctxt, _op) \
377 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378
379/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300380#define emulate_2op_SrcV_nobyte(ctxt, _op) \
381 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100383/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300384#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300385 do { \
386 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300387 _type _clv = (ctxt)->src2.val; \
388 _type _srcv = (ctxt)->src.val; \
389 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300390 \
391 __asm__ __volatile__ ( \
392 _PRE_EFLAGS("0", "5", "2") \
393 _op _suffix " %4,%1 \n" \
394 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300395 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300396 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
397 ); \
398 \
Avi Kivity761441b2011-09-07 16:41:36 +0300399 (ctxt)->src2.val = (unsigned long) _clv; \
400 (ctxt)->src2.val = (unsigned long) _srcv; \
401 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100402 } while (0)
403
Avi Kivity761441b2011-09-07 16:41:36 +0300404#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300405 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300406 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300407 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300408 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300409 break; \
410 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300411 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300412 break; \
413 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300414 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300415 break; \
416 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100417 } while (0)
418
Avi Kivityd1eef452011-09-07 16:41:38 +0300419#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 do { \
421 unsigned long _tmp; \
422 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200423 __asm__ __volatile__ ( \
424 _PRE_EFLAGS("0", "3", "2") \
425 _op _suffix " %1; " \
426 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300427 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200428 "=&r" (_tmp) \
429 : "i" (EFLAGS_MASK)); \
430 } while (0)
431
432/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300433#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200434 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300435 switch ((ctxt)->dst.bytes) { \
436 case 1: __emulate_1op(ctxt, _op, "b"); break; \
437 case 2: __emulate_1op(ctxt, _op, "w"); break; \
438 case 4: __emulate_1op(ctxt, _op, "l"); break; \
439 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 } \
441 } while (0)
442
Avi Kivityb7d491e2013-01-04 16:18:49 +0200443#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
444#define FOP_RET "ret \n\t"
445
446#define FOP_START(op) \
447 extern void em_##op(struct fastop *fake); \
448 asm(".pushsection .text, \"ax\" \n\t" \
449 ".global em_" #op " \n\t" \
450 FOP_ALIGN \
451 "em_" #op ": \n\t"
452
453#define FOP_END \
454 ".popsection")
455
456#define FOP1E(op, dst) \
457 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
458
459#define FASTOP1(op) \
460 FOP_START(op) \
461 FOP1E(op##b, al) \
462 FOP1E(op##w, ax) \
463 FOP1E(op##l, eax) \
464 ON64(FOP1E(op##q, rax)) \
465 FOP_END
466
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300467#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300468 do { \
469 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300470 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
471 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300472 \
473 __asm__ __volatile__ ( \
474 _PRE_EFLAGS("0", "5", "1") \
475 "1: \n\t" \
476 _op _suffix " %6; " \
477 "2: \n\t" \
478 _POST_EFLAGS("0", "5", "1") \
479 ".pushsection .fixup,\"ax\" \n\t" \
480 "3: movb $1, %4 \n\t" \
481 "jmp 2b \n\t" \
482 ".popsection \n\t" \
483 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300484 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
485 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800486 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300487 } while (0)
488
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300489/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300490#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300491 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300492 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300493 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300494 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300495 break; \
496 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300497 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300498 break; \
499 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300500 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300501 break; \
502 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300503 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300504 break; \
505 } \
506 } while (0)
507
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200508static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
509 enum x86_intercept intercept,
510 enum x86_intercept_stage stage)
511{
512 struct x86_instruction_info info = {
513 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300514 .rep_prefix = ctxt->rep_prefix,
515 .modrm_mod = ctxt->modrm_mod,
516 .modrm_reg = ctxt->modrm_reg,
517 .modrm_rm = ctxt->modrm_rm,
518 .src_val = ctxt->src.val64,
519 .src_bytes = ctxt->src.bytes,
520 .dst_bytes = ctxt->dst.bytes,
521 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200522 .next_rip = ctxt->eip,
523 };
524
Avi Kivity29535382011-04-20 13:37:53 +0300525 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200526}
527
Avi Kivityf47cfa32012-06-07 17:49:24 +0300528static void assign_masked(ulong *dest, ulong src, ulong mask)
529{
530 *dest = (*dest & ~mask) | (src & mask);
531}
532
Avi Kivity9dac77f2011-06-01 15:34:25 +0300533static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800534{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300535 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800536}
537
Avi Kivityf47cfa32012-06-07 17:49:24 +0300538static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
539{
540 u16 sel;
541 struct desc_struct ss;
542
543 if (ctxt->mode == X86EMUL_MODE_PROT64)
544 return ~0UL;
545 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
546 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
547}
548
Avi Kivity612e89f2012-06-12 20:03:23 +0300549static int stack_size(struct x86_emulate_ctxt *ctxt)
550{
551 return (__fls(stack_mask(ctxt)) + 1) >> 3;
552}
553
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800555static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300556address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800557{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300558 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800559 return reg;
560 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300561 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800562}
563
564static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300565register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800566{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300567 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800568}
569
Avi Kivity5ad105e2012-08-19 14:34:31 +0300570static void masked_increment(ulong *reg, ulong mask, int inc)
571{
572 assign_masked(reg, *reg + inc, mask);
573}
574
Harvey Harrison7a9572752008-02-19 07:40:41 -0800575static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300576register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800577{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300578 ulong mask;
579
Avi Kivity9dac77f2011-06-01 15:34:25 +0300580 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300581 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800582 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300583 mask = ad_mask(ctxt);
584 masked_increment(reg, mask, inc);
585}
586
587static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
588{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300589 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800590}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
Avi Kivity9dac77f2011-06-01 15:34:25 +0300592static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800593{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300594 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800595}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300596
Avi Kivity56697682011-04-03 14:08:51 +0300597static u32 desc_limit_scaled(struct desc_struct *desc)
598{
599 u32 limit = get_desc_limit(desc);
600
601 return desc->g ? (limit << 12) | 0xfff : limit;
602}
603
Avi Kivity9dac77f2011-06-01 15:34:25 +0300604static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300605{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300606 ctxt->has_seg_override = true;
607 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300608}
609
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900610static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300611{
612 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
613 return 0;
614
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900615 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300616}
617
Avi Kivity9dac77f2011-06-01 15:34:25 +0300618static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300619{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300620 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300621 return 0;
622
Avi Kivity9dac77f2011-06-01 15:34:25 +0300623 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300624}
625
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200626static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
627 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300628{
Avi Kivityda9cb572010-11-22 17:53:21 +0200629 ctxt->exception.vector = vec;
630 ctxt->exception.error_code = error;
631 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200632 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300633}
634
Joerg Roedel3b88e412011-04-04 12:39:29 +0200635static int emulate_db(struct x86_emulate_ctxt *ctxt)
636{
637 return emulate_exception(ctxt, DB_VECTOR, 0, false);
638}
639
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200640static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300641{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200642 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300643}
644
Avi Kivity618ff152011-04-03 12:32:09 +0300645static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
646{
647 return emulate_exception(ctxt, SS_VECTOR, err, true);
648}
649
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200650static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300651{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200652 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300653}
654
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200655static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300656{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200657 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300658}
659
Avi Kivity34d1f492010-08-26 11:59:01 +0300660static int emulate_de(struct x86_emulate_ctxt *ctxt)
661{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200662 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300663}
664
Avi Kivity1253791d2011-03-29 11:41:27 +0200665static int emulate_nm(struct x86_emulate_ctxt *ctxt)
666{
667 return emulate_exception(ctxt, NM_VECTOR, 0, false);
668}
669
Avi Kivity1aa36612011-04-27 13:20:30 +0300670static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
671{
672 u16 selector;
673 struct desc_struct desc;
674
675 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
676 return selector;
677}
678
679static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
680 unsigned seg)
681{
682 u16 dummy;
683 u32 base3;
684 struct desc_struct desc;
685
686 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
687 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
688}
689
Avi Kivity1c11b372012-04-09 18:39:59 +0300690/*
691 * x86 defines three classes of vector instructions: explicitly
692 * aligned, explicitly unaligned, and the rest, which change behaviour
693 * depending on whether they're AVX encoded or not.
694 *
695 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
696 * subject to the same check.
697 */
698static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
699{
700 if (likely(size < 16))
701 return false;
702
703 if (ctxt->d & Aligned)
704 return true;
705 else if (ctxt->d & Unaligned)
706 return false;
707 else if (ctxt->d & Avx)
708 return false;
709 else
710 return true;
711}
712
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400713static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300714 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400715 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300716 ulong *linear)
717{
Avi Kivity618ff152011-04-03 12:32:09 +0300718 struct desc_struct desc;
719 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300720 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300721 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300722 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200723 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300724
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900725 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300726 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300727 case X86EMUL_MODE_PROT64:
728 if (((signed long)la << 16) >> 16 != la)
729 return emulate_gp(ctxt, 0);
730 break;
731 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300732 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
733 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300734 if (!usable)
735 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200736 /* code segment in protected mode or read-only data segment */
737 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
738 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300739 goto bad;
740 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400741 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300742 goto bad;
743 lim = desc_limit_scaled(&desc);
744 if ((desc.type & 8) || !(desc.type & 4)) {
745 /* expand-up segment */
746 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
747 goto bad;
748 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800749 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300750 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
751 goto bad;
752 lim = desc.d ? 0xffffffff : 0xffff;
753 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
754 goto bad;
755 }
Avi Kivity717746e2011-04-20 13:37:53 +0300756 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300757 if (!(desc.type & 8)) {
758 /* data segment */
759 if (cpl > desc.dpl)
760 goto bad;
761 } else if ((desc.type & 8) && !(desc.type & 4)) {
762 /* nonconforming code segment */
763 if (cpl != desc.dpl)
764 goto bad;
765 } else if ((desc.type & 8) && (desc.type & 4)) {
766 /* conforming code segment */
767 if (cpl < desc.dpl)
768 goto bad;
769 }
770 break;
771 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300772 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300773 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300774 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
775 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300776 *linear = la;
777 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300778bad:
779 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300780 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300781 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300782 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300783}
784
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400785static int linearize(struct x86_emulate_ctxt *ctxt,
786 struct segmented_address addr,
787 unsigned size, bool write,
788 ulong *linear)
789{
790 return __linearize(ctxt, addr, size, write, false, linear);
791}
792
793
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200794static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
795 struct segmented_address addr,
796 void *data,
797 unsigned size)
798{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200799 int rc;
800 ulong linear;
801
Avi Kivity83b87952011-04-03 11:31:19 +0300802 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200803 if (rc != X86EMUL_CONTINUE)
804 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300805 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200806}
807
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900808/*
809 * Fetch the next byte of the instruction being emulated which is pointed to
810 * by ctxt->_eip, then increment ctxt->_eip.
811 *
812 * Also prefetch the remaining bytes of the instruction without crossing page
813 * boundary if they are not in fetch_cache yet.
814 */
815static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200816{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300817 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200818 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300819 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200820
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900821 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400822 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900823 struct segmented_address addr = { .seg = VCPU_SREG_CS,
824 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300825 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900826 size = min(15UL - cur_size,
827 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400828 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900829 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400830 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900831 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
832 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900833 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200834 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300835 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200836 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900837 *dest = fc->data[ctxt->_eip - fc->start];
838 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900839 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200840}
841
842static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900843 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200844{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900845 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200846
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200847 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900848 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200849 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200850 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900851 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900852 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200853 return rc;
854 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900855 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200856}
857
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900858/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900859#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900860({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900861 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900862 if (rc != X86EMUL_CONTINUE) \
863 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900864 (_type)_x; \
865})
866
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900867#define insn_fetch_arr(_arr, _size, _ctxt) \
868({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900869 if (rc != X86EMUL_CONTINUE) \
870 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900871})
872
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000873/*
874 * Given the 'reg' portion of a ModRM byte, and a register block, return a
875 * pointer into the block that addresses the relevant register.
876 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
877 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300878static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000879 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880{
881 void *p;
882
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300884 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
885 else
886 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 return p;
888}
889
890static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200891 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 u16 *size, unsigned long *address, int op_bytes)
893{
894 int rc;
895
896 if (op_bytes == 2)
897 op_bytes = 3;
898 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200899 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900900 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200902 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200903 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 return rc;
905}
906
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300907static int test_cc(unsigned int condition, unsigned int flags)
908{
909 int rc = 0;
910
911 switch ((condition & 15) >> 1) {
912 case 0: /* o */
913 rc |= (flags & EFLG_OF);
914 break;
915 case 1: /* b/c/nae */
916 rc |= (flags & EFLG_CF);
917 break;
918 case 2: /* z/e */
919 rc |= (flags & EFLG_ZF);
920 break;
921 case 3: /* be/na */
922 rc |= (flags & (EFLG_CF|EFLG_ZF));
923 break;
924 case 4: /* s */
925 rc |= (flags & EFLG_SF);
926 break;
927 case 5: /* p/pe */
928 rc |= (flags & EFLG_PF);
929 break;
930 case 7: /* le/ng */
931 rc |= (flags & EFLG_ZF);
932 /* fall through */
933 case 6: /* l/nge */
934 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
935 break;
936 }
937
938 /* Odd condition identifiers (lsb == 1) have inverted sense. */
939 return (!!rc ^ (condition & 1));
940}
941
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300942static void fetch_register_operand(struct operand *op)
943{
944 switch (op->bytes) {
945 case 1:
946 op->val = *(u8 *)op->addr.reg;
947 break;
948 case 2:
949 op->val = *(u16 *)op->addr.reg;
950 break;
951 case 4:
952 op->val = *(u32 *)op->addr.reg;
953 break;
954 case 8:
955 op->val = *(u64 *)op->addr.reg;
956 break;
957 }
958}
959
Avi Kivity1253791d2011-03-29 11:41:27 +0200960static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
961{
962 ctxt->ops->get_fpu(ctxt);
963 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200964 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
965 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
966 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
967 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
968 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
969 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
970 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
971 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200972#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +0200973 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
974 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
975 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
976 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
977 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
978 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
979 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
980 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +0200981#endif
982 default: BUG();
983 }
984 ctxt->ops->put_fpu(ctxt);
985}
986
987static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
988 int reg)
989{
990 ctxt->ops->get_fpu(ctxt);
991 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200992 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
993 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
994 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
995 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
996 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
997 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
998 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
999 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001000#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001001 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1002 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1003 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1004 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1005 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1006 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1007 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1008 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001009#endif
1010 default: BUG();
1011 }
1012 ctxt->ops->put_fpu(ctxt);
1013}
1014
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001015static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1016{
1017 ctxt->ops->get_fpu(ctxt);
1018 switch (reg) {
1019 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1020 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1021 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1022 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1023 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1024 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1025 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1026 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1027 default: BUG();
1028 }
1029 ctxt->ops->put_fpu(ctxt);
1030}
1031
1032static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1033{
1034 ctxt->ops->get_fpu(ctxt);
1035 switch (reg) {
1036 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1037 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1038 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1039 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1040 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1041 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1042 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1043 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1044 default: BUG();
1045 }
1046 ctxt->ops->put_fpu(ctxt);
1047}
1048
Gleb Natapov045a2822012-12-20 16:57:43 +02001049static int em_fninit(struct x86_emulate_ctxt *ctxt)
1050{
1051 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1052 return emulate_nm(ctxt);
1053
1054 ctxt->ops->get_fpu(ctxt);
1055 asm volatile("fninit");
1056 ctxt->ops->put_fpu(ctxt);
1057 return X86EMUL_CONTINUE;
1058}
1059
1060static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1061{
1062 u16 fcw;
1063
1064 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1065 return emulate_nm(ctxt);
1066
1067 ctxt->ops->get_fpu(ctxt);
1068 asm volatile("fnstcw %0": "+m"(fcw));
1069 ctxt->ops->put_fpu(ctxt);
1070
1071 /* force 2 byte destination */
1072 ctxt->dst.bytes = 2;
1073 ctxt->dst.val = fcw;
1074
1075 return X86EMUL_CONTINUE;
1076}
1077
1078static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1079{
1080 u16 fsw;
1081
1082 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1083 return emulate_nm(ctxt);
1084
1085 ctxt->ops->get_fpu(ctxt);
1086 asm volatile("fnstsw %0": "+m"(fsw));
1087 ctxt->ops->put_fpu(ctxt);
1088
1089 /* force 2 byte destination */
1090 ctxt->dst.bytes = 2;
1091 ctxt->dst.val = fsw;
1092
1093 return X86EMUL_CONTINUE;
1094}
1095
Avi Kivity1253791d2011-03-29 11:41:27 +02001096static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001097 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001098{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001099 unsigned reg = ctxt->modrm_reg;
1100 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001101
Avi Kivity9dac77f2011-06-01 15:34:25 +03001102 if (!(ctxt->d & ModRM))
1103 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity1253791d2011-03-29 11:41:27 +02001104
Avi Kivity9dac77f2011-06-01 15:34:25 +03001105 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001106 op->type = OP_XMM;
1107 op->bytes = 16;
1108 op->addr.xmm = reg;
1109 read_sse_reg(ctxt, &op->vec_val, reg);
1110 return;
1111 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001112 if (ctxt->d & Mmx) {
1113 reg &= 7;
1114 op->type = OP_MM;
1115 op->bytes = 8;
1116 op->addr.mm = reg;
1117 return;
1118 }
Avi Kivity1253791d2011-03-29 11:41:27 +02001119
Avi Kivity3c118e22007-10-31 10:27:04 +02001120 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001121 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001122 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001123 op->bytes = 1;
1124 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001125 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001126 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001127 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001128 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001129 op->orig_val = op->val;
1130}
1131
Avi Kivitya6e34072012-06-10 17:15:39 +03001132static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1133{
1134 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1135 ctxt->modrm_seg = VCPU_SREG_SS;
1136}
1137
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001138static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001139 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001140{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001141 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001142 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001143 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001144 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001145
Avi Kivity9dac77f2011-06-01 15:34:25 +03001146 if (ctxt->rex_prefix) {
1147 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1148 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1149 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001150 }
1151
Avi Kivity9dac77f2011-06-01 15:34:25 +03001152 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1153 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1154 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1155 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001156
Avi Kivity9dac77f2011-06-01 15:34:25 +03001157 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001158 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001159 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001160 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001161 if (ctxt->d & Sse) {
Avi Kivity1253791d2011-03-29 11:41:27 +02001162 op->type = OP_XMM;
1163 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001164 op->addr.xmm = ctxt->modrm_rm;
1165 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001166 return rc;
1167 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001168 if (ctxt->d & Mmx) {
1169 op->type = OP_MM;
1170 op->bytes = 8;
1171 op->addr.xmm = ctxt->modrm_rm & 7;
1172 return rc;
1173 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001174 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175 return rc;
1176 }
1177
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001178 op->type = OP_MEM;
1179
Avi Kivity9dac77f2011-06-01 15:34:25 +03001180 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001181 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1182 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1183 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1184 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185
1186 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001187 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001188 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001189 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001190 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191 break;
1192 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001193 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001194 break;
1195 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001196 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001197 break;
1198 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001199 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001200 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001201 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001202 break;
1203 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001204 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001205 break;
1206 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001207 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 break;
1209 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001210 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001211 break;
1212 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001213 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001214 break;
1215 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001216 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217 break;
1218 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001219 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001220 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001221 break;
1222 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001223 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224 break;
1225 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001226 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1227 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1228 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001229 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001230 } else {
1231 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001233 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001234 index_reg |= (sib >> 3) & 7;
1235 base_reg |= sib & 7;
1236 scale = sib >> 6;
1237
Avi Kivity9dac77f2011-06-01 15:34:25 +03001238 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001239 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001240 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001241 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001242 adjust_modrm_seg(ctxt, base_reg);
1243 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001244 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001245 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001246 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001247 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001248 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001249 } else {
1250 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001251 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001252 adjust_modrm_seg(ctxt, base_reg);
1253 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001254 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001255 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001256 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001257 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001258 break;
1259 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001260 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001261 break;
1262 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001263 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001264 break;
1265 }
1266 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001267 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001268done:
1269 return rc;
1270}
1271
1272static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001273 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001274{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001275 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001277 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001278 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001279 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001280 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001281 break;
1282 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001283 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001284 break;
1285 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001286 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001287 break;
1288 }
1289done:
1290 return rc;
1291}
1292
Avi Kivity9dac77f2011-06-01 15:34:25 +03001293static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001294{
Sheng Yang7129eec2010-09-28 16:33:32 +08001295 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001296
Avi Kivity9dac77f2011-06-01 15:34:25 +03001297 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1298 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001299
Avi Kivity9dac77f2011-06-01 15:34:25 +03001300 if (ctxt->src.bytes == 2)
1301 sv = (s16)ctxt->src.val & (s16)mask;
1302 else if (ctxt->src.bytes == 4)
1303 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001304
Avi Kivity9dac77f2011-06-01 15:34:25 +03001305 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001306 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001307
1308 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001309 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001310}
1311
Gleb Natapov9de41572010-04-28 19:15:22 +03001312static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001313 unsigned long addr, void *dest, unsigned size)
1314{
1315 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001316 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001317
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001318 if (mc->pos < mc->end)
1319 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001320
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001321 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001322
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001323 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1324 &ctxt->exception);
1325 if (rc != X86EMUL_CONTINUE)
1326 return rc;
1327
1328 mc->end += size;
1329
1330read_cached:
1331 memcpy(dest, mc->data + mc->pos, size);
1332 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001333 return X86EMUL_CONTINUE;
1334}
1335
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001336static int segmented_read(struct x86_emulate_ctxt *ctxt,
1337 struct segmented_address addr,
1338 void *data,
1339 unsigned size)
1340{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001341 int rc;
1342 ulong linear;
1343
Avi Kivity83b87952011-04-03 11:31:19 +03001344 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001345 if (rc != X86EMUL_CONTINUE)
1346 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001347 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001348}
1349
1350static int segmented_write(struct x86_emulate_ctxt *ctxt,
1351 struct segmented_address addr,
1352 const void *data,
1353 unsigned size)
1354{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001355 int rc;
1356 ulong linear;
1357
Avi Kivity83b87952011-04-03 11:31:19 +03001358 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001359 if (rc != X86EMUL_CONTINUE)
1360 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001361 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1362 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001363}
1364
1365static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1366 struct segmented_address addr,
1367 const void *orig_data, const void *data,
1368 unsigned size)
1369{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001370 int rc;
1371 ulong linear;
1372
Avi Kivity83b87952011-04-03 11:31:19 +03001373 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001374 if (rc != X86EMUL_CONTINUE)
1375 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001376 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1377 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001378}
1379
Gleb Natapov7b262e92010-03-18 15:20:27 +02001380static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001381 unsigned int size, unsigned short port,
1382 void *dest)
1383{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001384 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001385
1386 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001387 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001388 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001389 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001390 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001391 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1392 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001393 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1394 count);
1395 if (n == 0)
1396 n = 1;
1397 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001398 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001399 return 0;
1400 rc->end = n * size;
1401 }
1402
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001403 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1404 ctxt->dst.data = rc->data + rc->pos;
1405 ctxt->dst.type = OP_MEM_STR;
1406 ctxt->dst.count = (rc->end - rc->pos) / size;
1407 rc->pos = rc->end;
1408 } else {
1409 memcpy(dest, rc->data + rc->pos, size);
1410 rc->pos += size;
1411 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001412 return 1;
1413}
1414
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001415static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1416 u16 index, struct desc_struct *desc)
1417{
1418 struct desc_ptr dt;
1419 ulong addr;
1420
1421 ctxt->ops->get_idt(ctxt, &dt);
1422
1423 if (dt.size < index * 8 + 7)
1424 return emulate_gp(ctxt, index << 3 | 0x2);
1425
1426 addr = dt.address + index * 8;
1427 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1428 &ctxt->exception);
1429}
1430
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001431static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001432 u16 selector, struct desc_ptr *dt)
1433{
Mathias Krause0225fb52012-08-30 01:30:16 +02001434 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001435
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001436 if (selector & 1 << 2) {
1437 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001438 u16 sel;
1439
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001440 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001441 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001442 return;
1443
1444 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1445 dt->address = get_desc_base(&desc);
1446 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001447 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001448}
1449
1450/* allowed just for 8 bytes segments */
1451static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001452 u16 selector, struct desc_struct *desc,
1453 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001454{
1455 struct desc_ptr dt;
1456 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001457 ulong addr;
1458
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001459 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001460
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001461 if (dt.size < index * 8 + 7)
1462 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001463
Avi Kivitye9194642012-06-13 16:29:39 +03001464 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001465 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1466 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467}
1468
1469/* allowed just for 8 bytes segments */
1470static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001471 u16 selector, struct desc_struct *desc)
1472{
1473 struct desc_ptr dt;
1474 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001476
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001477 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001479 if (dt.size < index * 8 + 7)
1480 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001481
1482 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001483 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1484 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001485}
1486
Gleb Natapov5601d052011-03-07 14:55:06 +02001487/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001488static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001489 u16 selector, int seg)
1490{
Avi Kivity869be992012-06-13 16:30:53 +03001491 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001492 u8 dpl, rpl, cpl;
1493 unsigned err_vec = GP_VECTOR;
1494 u32 err_code = 0;
1495 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001496 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001497 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001498 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001499
1500 memset(&seg_desc, 0, sizeof seg_desc);
1501
1502 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1503 || ctxt->mode == X86EMUL_MODE_REAL) {
1504 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001505 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001506 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001507 goto load;
1508 }
1509
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001510 rpl = selector & 3;
1511 cpl = ctxt->ops->cpl(ctxt);
1512
1513 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1514 if ((seg == VCPU_SREG_CS
1515 || (seg == VCPU_SREG_SS
1516 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1517 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001518 && null_selector)
1519 goto exception;
1520
1521 /* TR should be in GDT only */
1522 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1523 goto exception;
1524
1525 if (null_selector) /* for NULL selector skip all following checks */
1526 goto load;
1527
Avi Kivitye9194642012-06-13 16:29:39 +03001528 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001529 if (ret != X86EMUL_CONTINUE)
1530 return ret;
1531
1532 err_code = selector & 0xfffc;
1533 err_vec = GP_VECTOR;
1534
Guo Chaofc058682012-06-28 15:19:51 +08001535 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001536 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1537 goto exception;
1538
1539 if (!seg_desc.p) {
1540 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1541 goto exception;
1542 }
1543
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001544 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001545
1546 switch (seg) {
1547 case VCPU_SREG_SS:
1548 /*
1549 * segment is not a writable data segment or segment
1550 * selector's RPL != CPL or segment selector's RPL != CPL
1551 */
1552 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1553 goto exception;
1554 break;
1555 case VCPU_SREG_CS:
1556 if (!(seg_desc.type & 8))
1557 goto exception;
1558
1559 if (seg_desc.type & 4) {
1560 /* conforming */
1561 if (dpl > cpl)
1562 goto exception;
1563 } else {
1564 /* nonconforming */
1565 if (rpl > cpl || dpl != cpl)
1566 goto exception;
1567 }
1568 /* CS(RPL) <- CPL */
1569 selector = (selector & 0xfffc) | cpl;
1570 break;
1571 case VCPU_SREG_TR:
1572 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1573 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001574 old_desc = seg_desc;
1575 seg_desc.type |= 2; /* busy */
1576 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1577 sizeof(seg_desc), &ctxt->exception);
1578 if (ret != X86EMUL_CONTINUE)
1579 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001580 break;
1581 case VCPU_SREG_LDTR:
1582 if (seg_desc.s || seg_desc.type != 2)
1583 goto exception;
1584 break;
1585 default: /* DS, ES, FS, or GS */
1586 /*
1587 * segment is not a data or readable code segment or
1588 * ((segment is a data or nonconforming code segment)
1589 * and (both RPL and CPL > DPL))
1590 */
1591 if ((seg_desc.type & 0xa) == 0x8 ||
1592 (((seg_desc.type & 0xc) != 0xc) &&
1593 (rpl > dpl && cpl > dpl)))
1594 goto exception;
1595 break;
1596 }
1597
1598 if (seg_desc.s) {
1599 /* mark segment as accessed */
1600 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001601 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001602 if (ret != X86EMUL_CONTINUE)
1603 return ret;
1604 }
1605load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001606 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001607 return X86EMUL_CONTINUE;
1608exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001609 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001610 return X86EMUL_PROPAGATE_FAULT;
1611}
1612
Wei Yongjun31be40b2010-08-17 09:17:30 +08001613static void write_register_operand(struct operand *op)
1614{
1615 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1616 switch (op->bytes) {
1617 case 1:
1618 *(u8 *)op->addr.reg = (u8)op->val;
1619 break;
1620 case 2:
1621 *(u16 *)op->addr.reg = (u16)op->val;
1622 break;
1623 case 4:
1624 *op->addr.reg = (u32)op->val;
1625 break; /* 64b: zero-extend */
1626 case 8:
1627 *op->addr.reg = op->val;
1628 break;
1629 }
1630}
1631
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001632static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001633{
1634 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001635
Avi Kivity9dac77f2011-06-01 15:34:25 +03001636 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001637 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001638 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001639 break;
1640 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001641 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001642 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001643 ctxt->dst.addr.mem,
1644 &ctxt->dst.orig_val,
1645 &ctxt->dst.val,
1646 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001647 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001648 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001649 ctxt->dst.addr.mem,
1650 &ctxt->dst.val,
1651 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001652 if (rc != X86EMUL_CONTINUE)
1653 return rc;
1654 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001655 case OP_MEM_STR:
1656 rc = segmented_write(ctxt,
1657 ctxt->dst.addr.mem,
1658 ctxt->dst.data,
1659 ctxt->dst.bytes * ctxt->dst.count);
1660 if (rc != X86EMUL_CONTINUE)
1661 return rc;
1662 break;
Avi Kivity1253791d2011-03-29 11:41:27 +02001663 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001664 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity1253791d2011-03-29 11:41:27 +02001665 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001666 case OP_MM:
1667 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1668 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001669 case OP_NONE:
1670 /* no writeback */
1671 break;
1672 default:
1673 break;
1674 }
1675 return X86EMUL_CONTINUE;
1676}
1677
Avi Kivity51ddff52012-06-12 20:19:40 +03001678static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001679{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001680 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001681
Avi Kivity5ad105e2012-08-19 14:34:31 +03001682 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001683 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001684 addr.seg = VCPU_SREG_SS;
1685
Avi Kivity51ddff52012-06-12 20:19:40 +03001686 return segmented_write(ctxt, addr, data, bytes);
1687}
1688
1689static int em_push(struct x86_emulate_ctxt *ctxt)
1690{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001691 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001692 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001693 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001694}
1695
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001696static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001697 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001698{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001699 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001700 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001701
Avi Kivitydd856ef2012-08-27 23:46:17 +03001702 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001703 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001704 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001705 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001706 return rc;
1707
Avi Kivity5ad105e2012-08-19 14:34:31 +03001708 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001709 return rc;
1710}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001711
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001712static int em_pop(struct x86_emulate_ctxt *ctxt)
1713{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001714 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001715}
1716
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001717static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001718 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001719{
1720 int rc;
1721 unsigned long val, change_mask;
1722 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001723 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001724
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001725 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001726 if (rc != X86EMUL_CONTINUE)
1727 return rc;
1728
1729 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1730 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1731
1732 switch(ctxt->mode) {
1733 case X86EMUL_MODE_PROT64:
1734 case X86EMUL_MODE_PROT32:
1735 case X86EMUL_MODE_PROT16:
1736 if (cpl == 0)
1737 change_mask |= EFLG_IOPL;
1738 if (cpl <= iopl)
1739 change_mask |= EFLG_IF;
1740 break;
1741 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001742 if (iopl < 3)
1743 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001744 change_mask |= EFLG_IF;
1745 break;
1746 default: /* real mode */
1747 change_mask |= (EFLG_IOPL | EFLG_IF);
1748 break;
1749 }
1750
1751 *(unsigned long *)dest =
1752 (ctxt->eflags & ~change_mask) | (val & change_mask);
1753
1754 return rc;
1755}
1756
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001757static int em_popf(struct x86_emulate_ctxt *ctxt)
1758{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001759 ctxt->dst.type = OP_REG;
1760 ctxt->dst.addr.reg = &ctxt->eflags;
1761 ctxt->dst.bytes = ctxt->op_bytes;
1762 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001763}
1764
Avi Kivity612e89f2012-06-12 20:03:23 +03001765static int em_enter(struct x86_emulate_ctxt *ctxt)
1766{
1767 int rc;
1768 unsigned frame_size = ctxt->src.val;
1769 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001770 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001771
1772 if (nesting_level)
1773 return X86EMUL_UNHANDLEABLE;
1774
Avi Kivitydd856ef2012-08-27 23:46:17 +03001775 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1776 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001777 if (rc != X86EMUL_CONTINUE)
1778 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001779 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001780 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001781 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1782 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001783 stack_mask(ctxt));
1784 return X86EMUL_CONTINUE;
1785}
1786
Avi Kivityf47cfa32012-06-07 17:49:24 +03001787static int em_leave(struct x86_emulate_ctxt *ctxt)
1788{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001789 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001790 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001791 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001792}
1793
Avi Kivity1cd196e2011-09-13 10:45:51 +03001794static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001795{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001796 int seg = ctxt->src2.val;
1797
Avi Kivity9dac77f2011-06-01 15:34:25 +03001798 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001799
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001800 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001801}
1802
Avi Kivity1cd196e2011-09-13 10:45:51 +03001803static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001804{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001805 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001806 unsigned long selector;
1807 int rc;
1808
Avi Kivity9dac77f2011-06-01 15:34:25 +03001809 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001810 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001811 return rc;
1812
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001813 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001814 return rc;
1815}
1816
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001817static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001818{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001819 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001820 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001821 int reg = VCPU_REGS_RAX;
1822
1823 while (reg <= VCPU_REGS_RDI) {
1824 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001825 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001826
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001827 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001828 if (rc != X86EMUL_CONTINUE)
1829 return rc;
1830
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001831 ++reg;
1832 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001833
Wei Yongjunc37eda12010-06-15 09:03:33 +08001834 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001835}
1836
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001837static int em_pushf(struct x86_emulate_ctxt *ctxt)
1838{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001839 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001840 return em_push(ctxt);
1841}
1842
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001843static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001844{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001845 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001846 int reg = VCPU_REGS_RDI;
1847
1848 while (reg >= VCPU_REGS_RAX) {
1849 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001850 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001851 --reg;
1852 }
1853
Avi Kivitydd856ef2012-08-27 23:46:17 +03001854 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001855 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001856 break;
1857 --reg;
1858 }
1859 return rc;
1860}
1861
Avi Kivitydd856ef2012-08-27 23:46:17 +03001862static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001863{
Mathias Krause0225fb52012-08-30 01:30:16 +02001864 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001865 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001866 struct desc_ptr dt;
1867 gva_t cs_addr;
1868 gva_t eip_addr;
1869 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001870
1871 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001872 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001873 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001874 if (rc != X86EMUL_CONTINUE)
1875 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001876
1877 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1878
Avi Kivity9dac77f2011-06-01 15:34:25 +03001879 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001880 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001881 if (rc != X86EMUL_CONTINUE)
1882 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001883
Avi Kivity9dac77f2011-06-01 15:34:25 +03001884 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001885 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001886 if (rc != X86EMUL_CONTINUE)
1887 return rc;
1888
Avi Kivity4bff1e862011-04-20 13:37:53 +03001889 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001890
1891 eip_addr = dt.address + (irq << 2);
1892 cs_addr = dt.address + (irq << 2) + 2;
1893
Avi Kivity0f65dd72011-04-20 13:37:53 +03001894 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001895 if (rc != X86EMUL_CONTINUE)
1896 return rc;
1897
Avi Kivity0f65dd72011-04-20 13:37:53 +03001898 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001899 if (rc != X86EMUL_CONTINUE)
1900 return rc;
1901
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001902 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001903 if (rc != X86EMUL_CONTINUE)
1904 return rc;
1905
Avi Kivity9dac77f2011-06-01 15:34:25 +03001906 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001907
1908 return rc;
1909}
1910
Avi Kivitydd856ef2012-08-27 23:46:17 +03001911int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1912{
1913 int rc;
1914
1915 invalidate_registers(ctxt);
1916 rc = __emulate_int_real(ctxt, irq);
1917 if (rc == X86EMUL_CONTINUE)
1918 writeback_registers(ctxt);
1919 return rc;
1920}
1921
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001922static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001923{
1924 switch(ctxt->mode) {
1925 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001926 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001927 case X86EMUL_MODE_VM86:
1928 case X86EMUL_MODE_PROT16:
1929 case X86EMUL_MODE_PROT32:
1930 case X86EMUL_MODE_PROT64:
1931 default:
1932 /* Protected mode interrupts unimplemented yet */
1933 return X86EMUL_UNHANDLEABLE;
1934 }
1935}
1936
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001937static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001938{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001939 int rc = X86EMUL_CONTINUE;
1940 unsigned long temp_eip = 0;
1941 unsigned long temp_eflags = 0;
1942 unsigned long cs = 0;
1943 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1944 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1945 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1946 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1947
1948 /* TODO: Add stack limit check */
1949
Avi Kivity9dac77f2011-06-01 15:34:25 +03001950 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001951
1952 if (rc != X86EMUL_CONTINUE)
1953 return rc;
1954
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001955 if (temp_eip & ~0xffff)
1956 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001957
Avi Kivity9dac77f2011-06-01 15:34:25 +03001958 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001959
1960 if (rc != X86EMUL_CONTINUE)
1961 return rc;
1962
Avi Kivity9dac77f2011-06-01 15:34:25 +03001963 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001964
1965 if (rc != X86EMUL_CONTINUE)
1966 return rc;
1967
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001968 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001969
1970 if (rc != X86EMUL_CONTINUE)
1971 return rc;
1972
Avi Kivity9dac77f2011-06-01 15:34:25 +03001973 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001974
1975
Avi Kivity9dac77f2011-06-01 15:34:25 +03001976 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001977 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03001978 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001979 ctxt->eflags &= ~0xffff;
1980 ctxt->eflags |= temp_eflags;
1981 }
1982
1983 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
1984 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
1985
1986 return rc;
1987}
1988
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09001989static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001990{
1991 switch(ctxt->mode) {
1992 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001993 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001994 case X86EMUL_MODE_VM86:
1995 case X86EMUL_MODE_PROT16:
1996 case X86EMUL_MODE_PROT32:
1997 case X86EMUL_MODE_PROT64:
1998 default:
1999 /* iret from protected mode unimplemented yet */
2000 return X86EMUL_UNHANDLEABLE;
2001 }
2002}
2003
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002004static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2005{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002006 int rc;
2007 unsigned short sel;
2008
Avi Kivity9dac77f2011-06-01 15:34:25 +03002009 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002010
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002011 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002012 if (rc != X86EMUL_CONTINUE)
2013 return rc;
2014
Avi Kivity9dac77f2011-06-01 15:34:25 +03002015 ctxt->_eip = 0;
2016 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002017 return X86EMUL_CONTINUE;
2018}
2019
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002020static int em_grp2(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002021{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002022 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002023 case 0: /* rol */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002024 emulate_2op_SrcB(ctxt, "rol");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002025 break;
2026 case 1: /* ror */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002027 emulate_2op_SrcB(ctxt, "ror");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002028 break;
2029 case 2: /* rcl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002030 emulate_2op_SrcB(ctxt, "rcl");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002031 break;
2032 case 3: /* rcr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002033 emulate_2op_SrcB(ctxt, "rcr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002034 break;
2035 case 4: /* sal/shl */
2036 case 6: /* sal/shl */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002037 emulate_2op_SrcB(ctxt, "sal");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002038 break;
2039 case 5: /* shr */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002040 emulate_2op_SrcB(ctxt, "shr");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002041 break;
2042 case 7: /* sar */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002043 emulate_2op_SrcB(ctxt, "sar");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002044 break;
2045 }
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002046 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002047}
2048
Avi Kivity3329ece2011-09-13 10:45:39 +03002049static int em_not(struct x86_emulate_ctxt *ctxt)
2050{
2051 ctxt->dst.val = ~ctxt->dst.val;
2052 return X86EMUL_CONTINUE;
2053}
2054
2055static int em_neg(struct x86_emulate_ctxt *ctxt)
2056{
2057 emulate_1op(ctxt, "neg");
2058 return X86EMUL_CONTINUE;
2059}
2060
2061static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2062{
2063 u8 ex = 0;
2064
2065 emulate_1op_rax_rdx(ctxt, "mul", ex);
2066 return X86EMUL_CONTINUE;
2067}
2068
2069static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2070{
2071 u8 ex = 0;
2072
2073 emulate_1op_rax_rdx(ctxt, "imul", ex);
2074 return X86EMUL_CONTINUE;
2075}
2076
2077static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002078{
Avi Kivity34d1f492010-08-26 11:59:01 +03002079 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002080
Avi Kivity3329ece2011-09-13 10:45:39 +03002081 emulate_1op_rax_rdx(ctxt, "div", de);
2082 if (de)
2083 return emulate_de(ctxt);
2084 return X86EMUL_CONTINUE;
2085}
2086
2087static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2088{
2089 u8 de = 0;
2090
2091 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002092 if (de)
2093 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002094 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002095}
2096
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002097static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002098{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002099 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002100
Avi Kivity9dac77f2011-06-01 15:34:25 +03002101 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002102 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03002103 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002104 break;
2105 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03002106 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002107 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03002108 case 2: /* call near abs */ {
2109 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 old_eip = ctxt->_eip;
2111 ctxt->_eip = ctxt->src.val;
2112 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002113 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002114 break;
2115 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002116 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002117 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002118 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002119 case 5: /* jmp far */
2120 rc = em_jmp_far(ctxt);
2121 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002122 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002123 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002125 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002126 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002127}
2128
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002129static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002130{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002131 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002132
Avi Kivitydd856ef2012-08-27 23:46:17 +03002133 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2134 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2135 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2136 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002137 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002138 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002139 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2140 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002141
Laurent Vivier05f086f2007-09-24 11:10:55 +02002142 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002143 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002144 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145}
2146
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002147static int em_ret(struct x86_emulate_ctxt *ctxt)
2148{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002149 ctxt->dst.type = OP_REG;
2150 ctxt->dst.addr.reg = &ctxt->_eip;
2151 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002152 return em_pop(ctxt);
2153}
2154
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002155static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002156{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002157 int rc;
2158 unsigned long cs;
2159
Avi Kivity9dac77f2011-06-01 15:34:25 +03002160 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002161 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002162 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002163 if (ctxt->op_bytes == 4)
2164 ctxt->_eip = (u32)ctxt->_eip;
2165 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002166 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002167 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002168 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002169 return rc;
2170}
2171
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002172static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2173{
2174 /* Save real source value, then compare EAX against destination. */
2175 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002176 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002177 emulate_2op_SrcV(ctxt, "cmp");
2178
2179 if (ctxt->eflags & EFLG_ZF) {
2180 /* Success: write back to memory. */
2181 ctxt->dst.val = ctxt->src.orig_val;
2182 } else {
2183 /* Failure: write the value we saw to EAX. */
2184 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002185 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002186 }
2187 return X86EMUL_CONTINUE;
2188}
2189
Avi Kivityd4b43252011-09-13 10:45:50 +03002190static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002191{
Avi Kivityd4b43252011-09-13 10:45:50 +03002192 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002193 unsigned short sel;
2194 int rc;
2195
Avi Kivity9dac77f2011-06-01 15:34:25 +03002196 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002197
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002198 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002199 if (rc != X86EMUL_CONTINUE)
2200 return rc;
2201
Avi Kivity9dac77f2011-06-01 15:34:25 +03002202 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002203 return rc;
2204}
2205
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002206static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002207setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002208 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002209{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002210 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002211 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002214 cs->type = 0x0b; /* Read, Execute, Accessed */
2215 cs->s = 1;
2216 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002217 cs->p = 1;
2218 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002219 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220
Gleb Natapov79168fd2010-04-28 19:15:30 +03002221 set_desc_base(ss, 0); /* flat segment */
2222 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002223 ss->g = 1; /* 4kb granularity */
2224 ss->s = 1;
2225 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002226 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002227 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002228 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002229 ss->l = 0;
2230 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002231}
2232
Avi Kivity1a18a692012-02-01 12:23:21 +02002233static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2234{
2235 u32 eax, ebx, ecx, edx;
2236
2237 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002238 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2239 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002240 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2241 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2242}
2243
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002244static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2245{
Mathias Krause0225fb52012-08-30 01:30:16 +02002246 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002247 u32 eax, ebx, ecx, edx;
2248
2249 /*
2250 * syscall should always be enabled in longmode - so only become
2251 * vendor specific (cpuid) if other modes are active...
2252 */
2253 if (ctxt->mode == X86EMUL_MODE_PROT64)
2254 return true;
2255
2256 eax = 0x00000000;
2257 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002258 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2259 /*
2260 * Intel ("GenuineIntel")
2261 * remark: Intel CPUs only support "syscall" in 64bit
2262 * longmode. Also an 64bit guest with a
2263 * 32bit compat-app running will #UD !! While this
2264 * behaviour can be fixed (by emulating) into AMD
2265 * response - CPUs of AMD can't behave like Intel.
2266 */
2267 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2268 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2269 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2270 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002271
Avi Kivity0017f932012-06-07 14:10:16 +03002272 /* AMD ("AuthenticAMD") */
2273 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2274 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2275 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2276 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002277
Avi Kivity0017f932012-06-07 14:10:16 +03002278 /* AMD ("AMDisbetter!") */
2279 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2280 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2281 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2282 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002283
2284 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2285 return false;
2286}
2287
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002288static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002289{
Mathias Krause0225fb52012-08-30 01:30:16 +02002290 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002291 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002292 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002293 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002294 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002295
2296 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002297 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002298 ctxt->mode == X86EMUL_MODE_VM86)
2299 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002300
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002301 if (!(em_syscall_is_enabled(ctxt)))
2302 return emulate_ud(ctxt);
2303
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002304 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002305 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002306
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002307 if (!(efer & EFER_SCE))
2308 return emulate_ud(ctxt);
2309
Avi Kivity717746e2011-04-20 13:37:53 +03002310 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002311 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002312 cs_sel = (u16)(msr_data & 0xfffc);
2313 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002314
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002315 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002316 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002317 cs.l = 1;
2318 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002319 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2320 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002321
Avi Kivitydd856ef2012-08-27 23:46:17 +03002322 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002323 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002324#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002325 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002326
Avi Kivity717746e2011-04-20 13:37:53 +03002327 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002328 ctxt->mode == X86EMUL_MODE_PROT64 ?
2329 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002330 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002331
Avi Kivity717746e2011-04-20 13:37:53 +03002332 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002333 ctxt->eflags &= ~(msr_data | EFLG_RF);
2334#endif
2335 } else {
2336 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002337 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002338 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339
2340 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2341 }
2342
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002343 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002344}
2345
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002346static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002347{
Mathias Krause0225fb52012-08-30 01:30:16 +02002348 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002349 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002350 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002351 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002352 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002353
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002354 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002355 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002356 if (ctxt->mode == X86EMUL_MODE_REAL)
2357 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002358
Avi Kivity1a18a692012-02-01 12:23:21 +02002359 /*
2360 * Not recognized on AMD in compat mode (but is recognized in legacy
2361 * mode).
2362 */
2363 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2364 && !vendor_intel(ctxt))
2365 return emulate_ud(ctxt);
2366
Andre Przywara8c604352009-06-18 12:56:01 +02002367 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2368 * Therefore, we inject an #UD.
2369 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002370 if (ctxt->mode == X86EMUL_MODE_PROT64)
2371 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002372
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002373 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002374
Avi Kivity717746e2011-04-20 13:37:53 +03002375 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002376 switch (ctxt->mode) {
2377 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002378 if ((msr_data & 0xfffc) == 0x0)
2379 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002380 break;
2381 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002382 if (msr_data == 0x0)
2383 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002384 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002385 default:
2386 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002387 }
2388
2389 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002390 cs_sel = (u16)msr_data;
2391 cs_sel &= ~SELECTOR_RPL_MASK;
2392 ss_sel = cs_sel + 8;
2393 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002394 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002395 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002396 cs.l = 1;
2397 }
2398
Avi Kivity1aa36612011-04-27 13:20:30 +03002399 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2400 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002401
Avi Kivity717746e2011-04-20 13:37:53 +03002402 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002403 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002404
Avi Kivity717746e2011-04-20 13:37:53 +03002405 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002406 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002407
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002408 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002409}
2410
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002411static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002412{
Mathias Krause0225fb52012-08-30 01:30:16 +02002413 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002414 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002415 u64 msr_data;
2416 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002417 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002418
Gleb Natapova0044752010-02-10 14:21:31 +02002419 /* inject #GP if in real mode or Virtual 8086 mode */
2420 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002421 ctxt->mode == X86EMUL_MODE_VM86)
2422 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002423
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002424 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002425
Avi Kivity9dac77f2011-06-01 15:34:25 +03002426 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002427 usermode = X86EMUL_MODE_PROT64;
2428 else
2429 usermode = X86EMUL_MODE_PROT32;
2430
2431 cs.dpl = 3;
2432 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002433 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002434 switch (usermode) {
2435 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002436 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002437 if ((msr_data & 0xfffc) == 0x0)
2438 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002439 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002440 break;
2441 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002442 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002443 if (msr_data == 0x0)
2444 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002445 ss_sel = cs_sel + 8;
2446 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002447 cs.l = 1;
2448 break;
2449 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002450 cs_sel |= SELECTOR_RPL_MASK;
2451 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002452
Avi Kivity1aa36612011-04-27 13:20:30 +03002453 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2454 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002455
Avi Kivitydd856ef2012-08-27 23:46:17 +03002456 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2457 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002458
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002459 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002460}
2461
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002462static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002463{
2464 int iopl;
2465 if (ctxt->mode == X86EMUL_MODE_REAL)
2466 return false;
2467 if (ctxt->mode == X86EMUL_MODE_VM86)
2468 return true;
2469 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002470 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002471}
2472
2473static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002474 u16 port, u16 len)
2475{
Mathias Krause0225fb52012-08-30 01:30:16 +02002476 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002477 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002478 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002479 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002480 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002481 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002482 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002483
Avi Kivity1aa36612011-04-27 13:20:30 +03002484 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002485 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002486 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002487 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002488 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002489 base = get_desc_base(&tr_seg);
2490#ifdef CONFIG_X86_64
2491 base |= ((u64)base3) << 32;
2492#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002493 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002494 if (r != X86EMUL_CONTINUE)
2495 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002496 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002497 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002498 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002499 if (r != X86EMUL_CONTINUE)
2500 return false;
2501 if ((perm >> bit_idx) & mask)
2502 return false;
2503 return true;
2504}
2505
2506static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002507 u16 port, u16 len)
2508{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002509 if (ctxt->perm_ok)
2510 return true;
2511
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002512 if (emulator_bad_iopl(ctxt))
2513 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002514 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002515
2516 ctxt->perm_ok = true;
2517
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002518 return true;
2519}
2520
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002521static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002522 struct tss_segment_16 *tss)
2523{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002524 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002525 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002526 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2527 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2528 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2529 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2530 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2531 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2532 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2533 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002534
Avi Kivity1aa36612011-04-27 13:20:30 +03002535 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2536 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2537 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2538 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2539 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540}
2541
2542static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543 struct tss_segment_16 *tss)
2544{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 int ret;
2546
Avi Kivity9dac77f2011-06-01 15:34:25 +03002547 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002549 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2550 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2551 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2552 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2553 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2554 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2555 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2556 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002557
2558 /*
2559 * SDM says that segment selectors are loaded before segment
2560 * descriptors
2561 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002562 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2563 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2564 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2565 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2566 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002567
2568 /*
Guo Chaofc058682012-06-28 15:19:51 +08002569 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 * it is handled in a context of new task
2571 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002572 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 if (ret != X86EMUL_CONTINUE)
2574 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002575 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002576 if (ret != X86EMUL_CONTINUE)
2577 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002578 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002579 if (ret != X86EMUL_CONTINUE)
2580 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002581 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582 if (ret != X86EMUL_CONTINUE)
2583 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002584 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585 if (ret != X86EMUL_CONTINUE)
2586 return ret;
2587
2588 return X86EMUL_CONTINUE;
2589}
2590
2591static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002592 u16 tss_selector, u16 old_tss_sel,
2593 ulong old_tss_base, struct desc_struct *new_desc)
2594{
Mathias Krause0225fb52012-08-30 01:30:16 +02002595 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 struct tss_segment_16 tss_seg;
2597 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002598 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002599
Avi Kivity0f65dd72011-04-20 13:37:53 +03002600 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002601 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002602 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002606 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607
Avi Kivity0f65dd72011-04-20 13:37:53 +03002608 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002609 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002610 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613
Avi Kivity0f65dd72011-04-20 13:37:53 +03002614 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002615 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002616 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619
2620 if (old_tss_sel != 0xffff) {
2621 tss_seg.prev_task_link = old_tss_sel;
2622
Avi Kivity0f65dd72011-04-20 13:37:53 +03002623 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 &tss_seg.prev_task_link,
2625 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002626 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002627 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002628 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 }
2631
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002632 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633}
2634
2635static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 struct tss_segment_32 *tss)
2637{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002638 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002639 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002641 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2642 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2643 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2644 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2645 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2646 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2647 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2648 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002649
Avi Kivity1aa36612011-04-27 13:20:30 +03002650 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2651 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2652 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2653 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2654 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2655 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2656 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657}
2658
2659static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 struct tss_segment_32 *tss)
2661{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 int ret;
2663
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002664 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002665 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002666 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002668
2669 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002670 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2671 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2672 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2673 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2674 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2675 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2676 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2677 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678
2679 /*
2680 * SDM says that segment selectors are loaded before segment
2681 * descriptors
2682 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002683 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2684 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2685 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2686 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2687 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2688 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2689 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002690
2691 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002692 * If we're switching between Protected Mode and VM86, we need to make
2693 * sure to update the mode before loading the segment descriptors so
2694 * that the selectors are interpreted correctly.
2695 *
2696 * Need to get rflags to the vcpu struct immediately because it
2697 * influences the CPL which is checked at least when loading the segment
2698 * descriptors and when pushing an error code to the new kernel stack.
2699 *
2700 * TODO Introduce a separate ctxt->ops->set_cpl callback
2701 */
2702 if (ctxt->eflags & X86_EFLAGS_VM)
2703 ctxt->mode = X86EMUL_MODE_VM86;
2704 else
2705 ctxt->mode = X86EMUL_MODE_PROT32;
2706
2707 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2708
2709 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710 * Now load segment descriptors. If fault happenes at this stage
2711 * it is handled in a context of new task
2712 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002713 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002714 if (ret != X86EMUL_CONTINUE)
2715 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002716 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002717 if (ret != X86EMUL_CONTINUE)
2718 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002719 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002720 if (ret != X86EMUL_CONTINUE)
2721 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002722 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002723 if (ret != X86EMUL_CONTINUE)
2724 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002725 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726 if (ret != X86EMUL_CONTINUE)
2727 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002728 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002729 if (ret != X86EMUL_CONTINUE)
2730 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002731 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732 if (ret != X86EMUL_CONTINUE)
2733 return ret;
2734
2735 return X86EMUL_CONTINUE;
2736}
2737
2738static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002739 u16 tss_selector, u16 old_tss_sel,
2740 ulong old_tss_base, struct desc_struct *new_desc)
2741{
Mathias Krause0225fb52012-08-30 01:30:16 +02002742 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002743 struct tss_segment_32 tss_seg;
2744 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002745 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002746
Avi Kivity0f65dd72011-04-20 13:37:53 +03002747 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002748 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002749 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002752
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002753 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754
Avi Kivity0f65dd72011-04-20 13:37:53 +03002755 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002756 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002757 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760
Avi Kivity0f65dd72011-04-20 13:37:53 +03002761 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002762 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002763 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766
2767 if (old_tss_sel != 0xffff) {
2768 tss_seg.prev_task_link = old_tss_sel;
2769
Avi Kivity0f65dd72011-04-20 13:37:53 +03002770 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 &tss_seg.prev_task_link,
2772 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002773 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002774 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002775 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 }
2778
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002779 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780}
2781
2782static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002783 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002784 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785{
Mathias Krause0225fb52012-08-30 01:30:16 +02002786 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787 struct desc_struct curr_tss_desc, next_tss_desc;
2788 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002789 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002790 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002791 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002792 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002793 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794
2795 /* FIXME: old_tss_base == ~0 ? */
2796
Avi Kivitye9194642012-06-13 16:29:39 +03002797 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798 if (ret != X86EMUL_CONTINUE)
2799 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002800 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002801 if (ret != X86EMUL_CONTINUE)
2802 return ret;
2803
2804 /* FIXME: check that next_tss_desc is tss */
2805
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002806 /*
2807 * Check privileges. The three cases are task switch caused by...
2808 *
2809 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2810 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002811 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002812 */
2813 if (reason == TASK_SWITCH_GATE) {
2814 if (idt_index != -1) {
2815 /* Software interrupts */
2816 struct desc_struct task_gate_desc;
2817 int dpl;
2818
2819 ret = read_interrupt_descriptor(ctxt, idt_index,
2820 &task_gate_desc);
2821 if (ret != X86EMUL_CONTINUE)
2822 return ret;
2823
2824 dpl = task_gate_desc.dpl;
2825 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2826 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2827 }
2828 } else if (reason != TASK_SWITCH_IRET) {
2829 int dpl = next_tss_desc.dpl;
2830 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2831 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002832 }
2833
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002834
Gleb Natapovceffb452010-03-18 15:20:19 +02002835 desc_limit = desc_limit_scaled(&next_tss_desc);
2836 if (!next_tss_desc.p ||
2837 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2838 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002839 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002840 return X86EMUL_PROPAGATE_FAULT;
2841 }
2842
2843 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2844 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002845 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846 }
2847
2848 if (reason == TASK_SWITCH_IRET)
2849 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2850
2851 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002852 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002853 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2854 old_tss_sel = 0xffff;
2855
2856 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002857 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002858 old_tss_base, &next_tss_desc);
2859 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002860 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002861 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002862 if (ret != X86EMUL_CONTINUE)
2863 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002864
2865 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2866 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2867
2868 if (reason != TASK_SWITCH_IRET) {
2869 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002870 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002871 }
2872
Avi Kivity717746e2011-04-20 13:37:53 +03002873 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002874 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002875
Jan Kiszkae269fb22010-04-14 15:51:09 +02002876 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002877 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2878 ctxt->lock_prefix = 0;
2879 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002880 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002881 }
2882
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002883 return ret;
2884}
2885
2886int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002887 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002888 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002889{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002890 int rc;
2891
Avi Kivitydd856ef2012-08-27 23:46:17 +03002892 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002893 ctxt->_eip = ctxt->eip;
2894 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002895
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002896 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002897 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002898
Avi Kivitydd856ef2012-08-27 23:46:17 +03002899 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002900 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002901 writeback_registers(ctxt);
2902 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002903
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002904 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002905}
2906
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002907static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2908 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002909{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002910 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002911
Avi Kivitydd856ef2012-08-27 23:46:17 +03002912 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2913 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002914}
2915
Avi Kivity7af04fc2010-08-18 14:16:35 +03002916static int em_das(struct x86_emulate_ctxt *ctxt)
2917{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002918 u8 al, old_al;
2919 bool af, cf, old_cf;
2920
2921 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002922 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002923
2924 old_al = al;
2925 old_cf = cf;
2926 cf = false;
2927 af = ctxt->eflags & X86_EFLAGS_AF;
2928 if ((al & 0x0f) > 9 || af) {
2929 al -= 6;
2930 cf = old_cf | (al >= 250);
2931 af = true;
2932 } else {
2933 af = false;
2934 }
2935 if (old_al > 0x99 || old_cf) {
2936 al -= 0x60;
2937 cf = true;
2938 }
2939
Avi Kivity9dac77f2011-06-01 15:34:25 +03002940 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002941 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002942 ctxt->src.type = OP_IMM;
2943 ctxt->src.val = 0;
2944 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002945 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002946 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2947 if (cf)
2948 ctxt->eflags |= X86_EFLAGS_CF;
2949 if (af)
2950 ctxt->eflags |= X86_EFLAGS_AF;
2951 return X86EMUL_CONTINUE;
2952}
2953
Gleb Natapov7f662272012-12-10 11:42:30 +02002954static int em_aad(struct x86_emulate_ctxt *ctxt)
2955{
2956 u8 al = ctxt->dst.val & 0xff;
2957 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2958
2959 al = (al + (ah * ctxt->src.val)) & 0xff;
2960
2961 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2962
2963 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2964
2965 if (!al)
2966 ctxt->eflags |= X86_EFLAGS_ZF;
2967 if (!(al & 1))
2968 ctxt->eflags |= X86_EFLAGS_PF;
2969 if (al & 0x80)
2970 ctxt->eflags |= X86_EFLAGS_SF;
2971
2972 return X86EMUL_CONTINUE;
2973}
2974
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002975static int em_call(struct x86_emulate_ctxt *ctxt)
2976{
2977 long rel = ctxt->src.val;
2978
2979 ctxt->src.val = (unsigned long)ctxt->_eip;
2980 jmp_rel(ctxt, rel);
2981 return em_push(ctxt);
2982}
2983
Avi Kivity0ef753b2010-08-18 14:51:45 +03002984static int em_call_far(struct x86_emulate_ctxt *ctxt)
2985{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002986 u16 sel, old_cs;
2987 ulong old_eip;
2988 int rc;
2989
Avi Kivity1aa36612011-04-27 13:20:30 +03002990 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002991 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002992
Avi Kivity9dac77f2011-06-01 15:34:25 +03002993 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002994 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002995 return X86EMUL_CONTINUE;
2996
Avi Kivity9dac77f2011-06-01 15:34:25 +03002997 ctxt->_eip = 0;
2998 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03002999
Avi Kivity9dac77f2011-06-01 15:34:25 +03003000 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003001 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003002 if (rc != X86EMUL_CONTINUE)
3003 return rc;
3004
Avi Kivity9dac77f2011-06-01 15:34:25 +03003005 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003006 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003007}
3008
Avi Kivity40ece7c2010-08-18 15:12:09 +03003009static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3010{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003011 int rc;
3012
Avi Kivity9dac77f2011-06-01 15:34:25 +03003013 ctxt->dst.type = OP_REG;
3014 ctxt->dst.addr.reg = &ctxt->_eip;
3015 ctxt->dst.bytes = ctxt->op_bytes;
3016 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003017 if (rc != X86EMUL_CONTINUE)
3018 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003019 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003020 return X86EMUL_CONTINUE;
3021}
3022
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003023static int em_add(struct x86_emulate_ctxt *ctxt)
3024{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003025 emulate_2op_SrcV(ctxt, "add");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003026 return X86EMUL_CONTINUE;
3027}
3028
3029static int em_or(struct x86_emulate_ctxt *ctxt)
3030{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003031 emulate_2op_SrcV(ctxt, "or");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003032 return X86EMUL_CONTINUE;
3033}
3034
3035static int em_adc(struct x86_emulate_ctxt *ctxt)
3036{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003037 emulate_2op_SrcV(ctxt, "adc");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003038 return X86EMUL_CONTINUE;
3039}
3040
3041static int em_sbb(struct x86_emulate_ctxt *ctxt)
3042{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003043 emulate_2op_SrcV(ctxt, "sbb");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003044 return X86EMUL_CONTINUE;
3045}
3046
3047static int em_and(struct x86_emulate_ctxt *ctxt)
3048{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003049 emulate_2op_SrcV(ctxt, "and");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003050 return X86EMUL_CONTINUE;
3051}
3052
3053static int em_sub(struct x86_emulate_ctxt *ctxt)
3054{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003055 emulate_2op_SrcV(ctxt, "sub");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003056 return X86EMUL_CONTINUE;
3057}
3058
3059static int em_xor(struct x86_emulate_ctxt *ctxt)
3060{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003061 emulate_2op_SrcV(ctxt, "xor");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003062 return X86EMUL_CONTINUE;
3063}
3064
3065static int em_cmp(struct x86_emulate_ctxt *ctxt)
3066{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003067 emulate_2op_SrcV(ctxt, "cmp");
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003068 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003069 ctxt->dst.type = OP_NONE;
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003070 return X86EMUL_CONTINUE;
3071}
3072
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003073static int em_test(struct x86_emulate_ctxt *ctxt)
3074{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003075 emulate_2op_SrcV(ctxt, "test");
Avi Kivitycaa8a162011-09-11 11:23:02 +03003076 /* Disable writeback. */
3077 ctxt->dst.type = OP_NONE;
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003078 return X86EMUL_CONTINUE;
3079}
3080
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003081static int em_xchg(struct x86_emulate_ctxt *ctxt)
3082{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003083 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003084 ctxt->src.val = ctxt->dst.val;
3085 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003086
3087 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003088 ctxt->dst.val = ctxt->src.orig_val;
3089 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003090 return X86EMUL_CONTINUE;
3091}
3092
Avi Kivity5c82aa22010-08-18 18:31:43 +03003093static int em_imul(struct x86_emulate_ctxt *ctxt)
3094{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003095 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03003096 return X86EMUL_CONTINUE;
3097}
3098
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003099static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3100{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003101 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03003102 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003103}
3104
Avi Kivity61429142010-08-19 15:13:00 +03003105static int em_cwd(struct x86_emulate_ctxt *ctxt)
3106{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003107 ctxt->dst.type = OP_REG;
3108 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003109 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003110 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003111
3112 return X86EMUL_CONTINUE;
3113}
3114
Avi Kivity48bb5d32010-08-18 18:54:34 +03003115static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3116{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003117 u64 tsc = 0;
3118
Avi Kivity717746e2011-04-20 13:37:53 +03003119 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003120 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3121 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003122 return X86EMUL_CONTINUE;
3123}
3124
Avi Kivity222d21a2011-11-10 14:57:30 +02003125static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3126{
3127 u64 pmc;
3128
Avi Kivitydd856ef2012-08-27 23:46:17 +03003129 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003130 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003131 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3132 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003133 return X86EMUL_CONTINUE;
3134}
3135
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003136static int em_mov(struct x86_emulate_ctxt *ctxt)
3137{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003138 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003139 return X86EMUL_CONTINUE;
3140}
3141
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003142static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3143{
3144 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3145 return emulate_gp(ctxt, 0);
3146
3147 /* Disable writeback. */
3148 ctxt->dst.type = OP_NONE;
3149 return X86EMUL_CONTINUE;
3150}
3151
3152static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3153{
3154 unsigned long val;
3155
3156 if (ctxt->mode == X86EMUL_MODE_PROT64)
3157 val = ctxt->src.val & ~0ULL;
3158 else
3159 val = ctxt->src.val & ~0U;
3160
3161 /* #UD condition is already handled. */
3162 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3163 return emulate_gp(ctxt, 0);
3164
3165 /* Disable writeback. */
3166 ctxt->dst.type = OP_NONE;
3167 return X86EMUL_CONTINUE;
3168}
3169
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003170static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3171{
3172 u64 msr_data;
3173
Avi Kivitydd856ef2012-08-27 23:46:17 +03003174 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3175 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3176 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003177 return emulate_gp(ctxt, 0);
3178
3179 return X86EMUL_CONTINUE;
3180}
3181
3182static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3183{
3184 u64 msr_data;
3185
Avi Kivitydd856ef2012-08-27 23:46:17 +03003186 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003187 return emulate_gp(ctxt, 0);
3188
Avi Kivitydd856ef2012-08-27 23:46:17 +03003189 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3190 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003191 return X86EMUL_CONTINUE;
3192}
3193
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003194static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3195{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003196 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003197 return emulate_ud(ctxt);
3198
Avi Kivity9dac77f2011-06-01 15:34:25 +03003199 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003200 return X86EMUL_CONTINUE;
3201}
3202
3203static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3204{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003205 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003206
Avi Kivity9dac77f2011-06-01 15:34:25 +03003207 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003208 return emulate_ud(ctxt);
3209
Avi Kivity9dac77f2011-06-01 15:34:25 +03003210 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003211 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3212
3213 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 ctxt->dst.type = OP_NONE;
3215 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003216}
3217
Avi Kivitya14e5792012-06-13 12:28:33 +03003218static int em_lldt(struct x86_emulate_ctxt *ctxt)
3219{
3220 u16 sel = ctxt->src.val;
3221
3222 /* Disable writeback. */
3223 ctxt->dst.type = OP_NONE;
3224 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3225}
3226
Avi Kivity80890002012-06-13 16:33:29 +03003227static int em_ltr(struct x86_emulate_ctxt *ctxt)
3228{
3229 u16 sel = ctxt->src.val;
3230
3231 /* Disable writeback. */
3232 ctxt->dst.type = OP_NONE;
3233 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3234}
3235
Avi Kivity38503912011-03-31 18:48:09 +02003236static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3237{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003238 int rc;
3239 ulong linear;
3240
Avi Kivity9dac77f2011-06-01 15:34:25 +03003241 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003242 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003243 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003244 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003245 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003246 return X86EMUL_CONTINUE;
3247}
3248
Avi Kivity2d04a052011-04-20 15:32:49 +03003249static int em_clts(struct x86_emulate_ctxt *ctxt)
3250{
3251 ulong cr0;
3252
3253 cr0 = ctxt->ops->get_cr(ctxt, 0);
3254 cr0 &= ~X86_CR0_TS;
3255 ctxt->ops->set_cr(ctxt, 0, cr0);
3256 return X86EMUL_CONTINUE;
3257}
3258
Avi Kivity26d05cc2011-04-21 12:07:59 +03003259static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3260{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003261 int rc;
3262
Avi Kivity9dac77f2011-06-01 15:34:25 +03003263 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003264 return X86EMUL_UNHANDLEABLE;
3265
3266 rc = ctxt->ops->fix_hypercall(ctxt);
3267 if (rc != X86EMUL_CONTINUE)
3268 return rc;
3269
3270 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003271 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003272 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003273 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003274 return X86EMUL_CONTINUE;
3275}
3276
Avi Kivity96051572012-06-10 17:21:18 +03003277static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3278 void (*get)(struct x86_emulate_ctxt *ctxt,
3279 struct desc_ptr *ptr))
3280{
3281 struct desc_ptr desc_ptr;
3282
3283 if (ctxt->mode == X86EMUL_MODE_PROT64)
3284 ctxt->op_bytes = 8;
3285 get(ctxt, &desc_ptr);
3286 if (ctxt->op_bytes == 2) {
3287 ctxt->op_bytes = 4;
3288 desc_ptr.address &= 0x00ffffff;
3289 }
3290 /* Disable writeback. */
3291 ctxt->dst.type = OP_NONE;
3292 return segmented_write(ctxt, ctxt->dst.addr.mem,
3293 &desc_ptr, 2 + ctxt->op_bytes);
3294}
3295
3296static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3297{
3298 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3299}
3300
3301static int em_sidt(struct x86_emulate_ctxt *ctxt)
3302{
3303 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3304}
3305
Avi Kivity26d05cc2011-04-21 12:07:59 +03003306static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3307{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003308 struct desc_ptr desc_ptr;
3309 int rc;
3310
Avi Kivity510425f2012-06-07 17:04:36 +03003311 if (ctxt->mode == X86EMUL_MODE_PROT64)
3312 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003313 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003316 if (rc != X86EMUL_CONTINUE)
3317 return rc;
3318 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3319 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003320 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003321 return X86EMUL_CONTINUE;
3322}
3323
Avi Kivity5ef39c72011-04-21 12:21:50 +03003324static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003325{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003326 int rc;
3327
Avi Kivity5ef39c72011-04-21 12:21:50 +03003328 rc = ctxt->ops->fix_hypercall(ctxt);
3329
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003331 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003332 return rc;
3333}
3334
3335static int em_lidt(struct x86_emulate_ctxt *ctxt)
3336{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003337 struct desc_ptr desc_ptr;
3338 int rc;
3339
Avi Kivity510425f2012-06-07 17:04:36 +03003340 if (ctxt->mode == X86EMUL_MODE_PROT64)
3341 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003342 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003343 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003344 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003345 if (rc != X86EMUL_CONTINUE)
3346 return rc;
3347 ctxt->ops->set_idt(ctxt, &desc_ptr);
3348 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003349 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003350 return X86EMUL_CONTINUE;
3351}
3352
3353static int em_smsw(struct x86_emulate_ctxt *ctxt)
3354{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003355 ctxt->dst.bytes = 2;
3356 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003357 return X86EMUL_CONTINUE;
3358}
3359
3360static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3361{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003362 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003363 | (ctxt->src.val & 0x0f));
3364 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003365 return X86EMUL_CONTINUE;
3366}
3367
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003368static int em_loop(struct x86_emulate_ctxt *ctxt)
3369{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003370 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3371 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003372 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3373 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003374
3375 return X86EMUL_CONTINUE;
3376}
3377
3378static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3379{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003380 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003381 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003382
3383 return X86EMUL_CONTINUE;
3384}
3385
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003386static int em_in(struct x86_emulate_ctxt *ctxt)
3387{
3388 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3389 &ctxt->dst.val))
3390 return X86EMUL_IO_NEEDED;
3391
3392 return X86EMUL_CONTINUE;
3393}
3394
3395static int em_out(struct x86_emulate_ctxt *ctxt)
3396{
3397 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3398 &ctxt->src.val, 1);
3399 /* Disable writeback. */
3400 ctxt->dst.type = OP_NONE;
3401 return X86EMUL_CONTINUE;
3402}
3403
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003404static int em_cli(struct x86_emulate_ctxt *ctxt)
3405{
3406 if (emulator_bad_iopl(ctxt))
3407 return emulate_gp(ctxt, 0);
3408
3409 ctxt->eflags &= ~X86_EFLAGS_IF;
3410 return X86EMUL_CONTINUE;
3411}
3412
3413static int em_sti(struct x86_emulate_ctxt *ctxt)
3414{
3415 if (emulator_bad_iopl(ctxt))
3416 return emulate_gp(ctxt, 0);
3417
3418 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3419 ctxt->eflags |= X86_EFLAGS_IF;
3420 return X86EMUL_CONTINUE;
3421}
3422
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003423static int em_bt(struct x86_emulate_ctxt *ctxt)
3424{
3425 /* Disable writeback. */
3426 ctxt->dst.type = OP_NONE;
3427 /* only subword offset */
3428 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3429
3430 emulate_2op_SrcV_nobyte(ctxt, "bt");
3431 return X86EMUL_CONTINUE;
3432}
3433
3434static int em_bts(struct x86_emulate_ctxt *ctxt)
3435{
3436 emulate_2op_SrcV_nobyte(ctxt, "bts");
3437 return X86EMUL_CONTINUE;
3438}
3439
3440static int em_btr(struct x86_emulate_ctxt *ctxt)
3441{
3442 emulate_2op_SrcV_nobyte(ctxt, "btr");
3443 return X86EMUL_CONTINUE;
3444}
3445
3446static int em_btc(struct x86_emulate_ctxt *ctxt)
3447{
3448 emulate_2op_SrcV_nobyte(ctxt, "btc");
3449 return X86EMUL_CONTINUE;
3450}
3451
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003452static int em_bsf(struct x86_emulate_ctxt *ctxt)
3453{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003454 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003455 return X86EMUL_CONTINUE;
3456}
3457
3458static int em_bsr(struct x86_emulate_ctxt *ctxt)
3459{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003460 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003461 return X86EMUL_CONTINUE;
3462}
3463
Avi Kivity6d6eede2012-06-07 14:11:36 +03003464static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3465{
3466 u32 eax, ebx, ecx, edx;
3467
Avi Kivitydd856ef2012-08-27 23:46:17 +03003468 eax = reg_read(ctxt, VCPU_REGS_RAX);
3469 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003470 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003471 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3472 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3473 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3474 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003475 return X86EMUL_CONTINUE;
3476}
3477
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003478static int em_lahf(struct x86_emulate_ctxt *ctxt)
3479{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003480 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3481 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003482 return X86EMUL_CONTINUE;
3483}
3484
Avi Kivity92998362012-06-13 12:25:06 +03003485static int em_bswap(struct x86_emulate_ctxt *ctxt)
3486{
3487 switch (ctxt->op_bytes) {
3488#ifdef CONFIG_X86_64
3489 case 8:
3490 asm("bswap %0" : "+r"(ctxt->dst.val));
3491 break;
3492#endif
3493 default:
3494 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3495 break;
3496 }
3497 return X86EMUL_CONTINUE;
3498}
3499
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003500static bool valid_cr(int nr)
3501{
3502 switch (nr) {
3503 case 0:
3504 case 2 ... 4:
3505 case 8:
3506 return true;
3507 default:
3508 return false;
3509 }
3510}
3511
3512static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3513{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003514 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003515 return emulate_ud(ctxt);
3516
3517 return X86EMUL_CONTINUE;
3518}
3519
3520static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3521{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003522 u64 new_val = ctxt->src.val64;
3523 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003524 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003525
3526 static u64 cr_reserved_bits[] = {
3527 0xffffffff00000000ULL,
3528 0, 0, 0, /* CR3 checked later */
3529 CR4_RESERVED_BITS,
3530 0, 0, 0,
3531 CR8_RESERVED_BITS,
3532 };
3533
3534 if (!valid_cr(cr))
3535 return emulate_ud(ctxt);
3536
3537 if (new_val & cr_reserved_bits[cr])
3538 return emulate_gp(ctxt, 0);
3539
3540 switch (cr) {
3541 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003542 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003543 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3544 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3545 return emulate_gp(ctxt, 0);
3546
Avi Kivity717746e2011-04-20 13:37:53 +03003547 cr4 = ctxt->ops->get_cr(ctxt, 4);
3548 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003549
3550 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3551 !(cr4 & X86_CR4_PAE))
3552 return emulate_gp(ctxt, 0);
3553
3554 break;
3555 }
3556 case 3: {
3557 u64 rsvd = 0;
3558
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003559 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3560 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003561 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003562 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003563 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003564 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003565 rsvd = CR3_NONPAE_RESERVED_BITS;
3566
3567 if (new_val & rsvd)
3568 return emulate_gp(ctxt, 0);
3569
3570 break;
3571 }
3572 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003573 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003574
3575 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3576 return emulate_gp(ctxt, 0);
3577
3578 break;
3579 }
3580 }
3581
3582 return X86EMUL_CONTINUE;
3583}
3584
Joerg Roedel3b88e412011-04-04 12:39:29 +02003585static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3586{
3587 unsigned long dr7;
3588
Avi Kivity717746e2011-04-20 13:37:53 +03003589 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003590
3591 /* Check if DR7.Global_Enable is set */
3592 return dr7 & (1 << 13);
3593}
3594
3595static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3596{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003597 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003598 u64 cr4;
3599
3600 if (dr > 7)
3601 return emulate_ud(ctxt);
3602
Avi Kivity717746e2011-04-20 13:37:53 +03003603 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003604 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3605 return emulate_ud(ctxt);
3606
3607 if (check_dr7_gd(ctxt))
3608 return emulate_db(ctxt);
3609
3610 return X86EMUL_CONTINUE;
3611}
3612
3613static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3614{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003615 u64 new_val = ctxt->src.val64;
3616 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003617
3618 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3619 return emulate_gp(ctxt, 0);
3620
3621 return check_dr_read(ctxt);
3622}
3623
Joerg Roedel01de8b02011-04-04 12:39:31 +02003624static int check_svme(struct x86_emulate_ctxt *ctxt)
3625{
3626 u64 efer;
3627
Avi Kivity717746e2011-04-20 13:37:53 +03003628 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003629
3630 if (!(efer & EFER_SVME))
3631 return emulate_ud(ctxt);
3632
3633 return X86EMUL_CONTINUE;
3634}
3635
3636static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3637{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003638 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003639
3640 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003641 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003642 return emulate_gp(ctxt, 0);
3643
3644 return check_svme(ctxt);
3645}
3646
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003647static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3648{
Avi Kivity717746e2011-04-20 13:37:53 +03003649 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003650
Avi Kivity717746e2011-04-20 13:37:53 +03003651 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003652 return emulate_ud(ctxt);
3653
3654 return X86EMUL_CONTINUE;
3655}
3656
Joerg Roedel80612522011-04-04 12:39:33 +02003657static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3658{
Avi Kivity717746e2011-04-20 13:37:53 +03003659 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003660 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003661
Avi Kivity717746e2011-04-20 13:37:53 +03003662 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003663 (rcx > 3))
3664 return emulate_gp(ctxt, 0);
3665
3666 return X86EMUL_CONTINUE;
3667}
3668
Joerg Roedelf6511932011-04-04 12:39:35 +02003669static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3670{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003671 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3672 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003673 return emulate_gp(ctxt, 0);
3674
3675 return X86EMUL_CONTINUE;
3676}
3677
3678static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3679{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003680 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3681 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003682 return emulate_gp(ctxt, 0);
3683
3684 return X86EMUL_CONTINUE;
3685}
3686
Avi Kivity73fba5f2010-07-29 15:11:53 +03003687#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003688#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003689#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3690 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003691#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003692#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003693#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3694#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003695#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003696#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003697#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003698#define II(_f, _e, _i) \
3699 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003700#define IIP(_f, _e, _i, _p) \
3701 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3702 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003703#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003704
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003705#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003706#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003707#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003708#define I2bvIP(_f, _e, _i, _p) \
3709 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003710
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003711#define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3712 I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3713 I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003714
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003715static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003716 DI(SrcNone | Priv, monitor),
3717 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003718 N, N, N, N, N, N,
3719};
3720
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003721static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003722 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3723 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3724 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3725 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3726 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3727 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3728 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3729 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003730};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003731
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003732static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003733 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003734 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003735 N, N, N, N, N, N,
3736};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003737
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003738static const struct opcode group1[] = {
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003739 I(Lock, em_add),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003740 I(Lock | PageTable, em_or),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003741 I(Lock, em_adc),
3742 I(Lock, em_sbb),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003743 I(Lock | PageTable, em_and),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003744 I(Lock, em_sub),
3745 I(Lock, em_xor),
3746 I(0, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747};
3748
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003749static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003750 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003751};
3752
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003753static const struct opcode group3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003754 I(DstMem | SrcImm, em_test),
3755 I(DstMem | SrcImm, em_test),
3756 I(DstMem | SrcNone | Lock, em_not),
3757 I(DstMem | SrcNone | Lock, em_neg),
3758 I(SrcMem, em_mul_ex),
3759 I(SrcMem, em_imul_ex),
3760 I(SrcMem, em_div_ex),
3761 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003762};
3763
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003764static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003765 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3766 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003767 N, N, N, N, N, N,
3768};
3769
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003770static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003771 I(DstMem | SrcNone | Lock, em_grp45),
3772 I(DstMem | SrcNone | Lock, em_grp45),
3773 I(SrcMem | Stack, em_grp45),
3774 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3775 I(SrcMem | Stack, em_grp45),
3776 I(SrcMemFAddr | ImplicitOps, em_grp45),
3777 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003778};
3779
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003780static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003781 DI(Prot, sldt),
3782 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003783 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003784 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003785 N, N, N, N,
3786};
3787
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003788static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003789 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3790 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003791 II(SrcMem | Priv, em_lgdt, lgdt),
3792 II(SrcMem | Priv, em_lidt, lidt),
3793 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3794 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3795 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003796}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003797 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003798 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003799 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003800 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3801 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3802 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003803} };
3804
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003805static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003806 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003807 I(DstMem | SrcImmByte, em_bt),
3808 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3809 I(DstMem | SrcImmByte | Lock, em_btr),
3810 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003811};
3812
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003813static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003814 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003815}, {
3816 N, N, N, N, N, N, N, N,
3817} };
3818
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003819static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003820 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003821 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003822};
3823
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003824static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003825 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003826};
3827
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003828static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003829 I(0, em_mov), N, N, N,
3830};
3831
Gleb Natapov045a2822012-12-20 16:57:43 +02003832static const struct escape escape_d9 = { {
3833 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3834}, {
3835 /* 0xC0 - 0xC7 */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xC8 - 0xCF */
3838 N, N, N, N, N, N, N, N,
3839 /* 0xD0 - 0xC7 */
3840 N, N, N, N, N, N, N, N,
3841 /* 0xD8 - 0xDF */
3842 N, N, N, N, N, N, N, N,
3843 /* 0xE0 - 0xE7 */
3844 N, N, N, N, N, N, N, N,
3845 /* 0xE8 - 0xEF */
3846 N, N, N, N, N, N, N, N,
3847 /* 0xF0 - 0xF7 */
3848 N, N, N, N, N, N, N, N,
3849 /* 0xF8 - 0xFF */
3850 N, N, N, N, N, N, N, N,
3851} };
3852
3853static const struct escape escape_db = { {
3854 N, N, N, N, N, N, N, N,
3855}, {
3856 /* 0xC0 - 0xC7 */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xC8 - 0xCF */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xD0 - 0xC7 */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xD8 - 0xDF */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xE0 - 0xE7 */
3865 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3866 /* 0xE8 - 0xEF */
3867 N, N, N, N, N, N, N, N,
3868 /* 0xF0 - 0xF7 */
3869 N, N, N, N, N, N, N, N,
3870 /* 0xF8 - 0xFF */
3871 N, N, N, N, N, N, N, N,
3872} };
3873
3874static const struct escape escape_dd = { {
3875 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3876}, {
3877 /* 0xC0 - 0xC7 */
3878 N, N, N, N, N, N, N, N,
3879 /* 0xC8 - 0xCF */
3880 N, N, N, N, N, N, N, N,
3881 /* 0xD0 - 0xC7 */
3882 N, N, N, N, N, N, N, N,
3883 /* 0xD8 - 0xDF */
3884 N, N, N, N, N, N, N, N,
3885 /* 0xE0 - 0xE7 */
3886 N, N, N, N, N, N, N, N,
3887 /* 0xE8 - 0xEF */
3888 N, N, N, N, N, N, N, N,
3889 /* 0xF0 - 0xF7 */
3890 N, N, N, N, N, N, N, N,
3891 /* 0xF8 - 0xFF */
3892 N, N, N, N, N, N, N, N,
3893} };
3894
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003895static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003896 /* 0x00 - 0x07 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003897 I6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003898 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3899 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003900 /* 0x08 - 0x0F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003901 I6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003902 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3903 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904 /* 0x10 - 0x17 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003905 I6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003906 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3907 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003908 /* 0x18 - 0x1F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003909 I6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003910 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3911 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003912 /* 0x20 - 0x27 */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003913 I6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003914 /* 0x28 - 0x2F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003915 I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003916 /* 0x30 - 0x37 */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003917 I6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003918 /* 0x38 - 0x3F */
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003919 I6ALU(0, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003920 /* 0x40 - 0x4F */
3921 X16(D(DstReg)),
3922 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003923 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003924 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003925 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003926 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003927 I(ImplicitOps | Stack | No64, em_pusha),
3928 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3930 N, N, N, N,
3931 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003932 I(SrcImm | Mov | Stack, em_push),
3933 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003934 I(SrcImmByte | Mov | Stack, em_push),
3935 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003936 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003937 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003938 /* 0x70 - 0x7F */
3939 X16(D(SrcImmByte)),
3940 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003941 G(ByteOp | DstMem | SrcImm, group1),
3942 G(DstMem | SrcImm, group1),
3943 G(ByteOp | DstMem | SrcImm | No64, group1),
3944 G(DstMem | SrcImmByte, group1),
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003945 I2bv(DstMem | SrcReg | ModRM, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003946 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003947 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003948 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003949 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003950 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003951 D(ModRM | SrcMem | NoAccess | DstReg),
3952 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3953 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003954 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003955 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003956 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003957 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003958 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003959 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003960 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003962 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003963 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003964 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003965 I2bv(SrcSI | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 /* 0xA8 - 0xAF */
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003967 I2bv(DstAcc | SrcImm, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003968 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3969 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003970 I2bv(SrcAcc | DstDI | String, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003971 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003972 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003974 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0xC0 - 0xC7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003976 D2bv(DstMem | SrcImmByte | ModRM),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003977 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003978 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003979 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3980 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003981 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003982 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003983 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3984 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003985 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003986 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003987 /* 0xD0 - 0xD7 */
Avi Kivityd2c6c7a2010-08-26 11:56:11 +03003988 D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
Gleb Natapov7f662272012-12-10 11:42:30 +02003989 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003990 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003991 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003992 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003993 X3(I(SrcImmByte, em_loop)),
3994 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003995 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3996 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003997 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003998 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003999 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004000 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
4001 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02004003 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004004 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
4005 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004006 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09004007 D(ImplicitOps), D(ImplicitOps),
4008 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004009 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
4010};
4011
Mathias Krausefd0a0d82012-08-30 01:30:15 +02004012static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004014 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004015 N, I(ImplicitOps | VendorSpecific, em_syscall),
4016 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02004017 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004018 N, D(ImplicitOps | ModRM), N, N,
4019 /* 0x10 - 0x1F */
4020 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
4021 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004022 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004023 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004024 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4025 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004026 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004027 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4028 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004029 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004030 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004031 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004032 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004033 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004034 I(ImplicitOps | VendorSpecific, em_sysenter),
4035 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004036 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 N, N, N, N, N, N, N, N,
4038 /* 0x40 - 0x4F */
4039 X16(D(DstReg | SrcMem | ModRM | Mov)),
4040 /* 0x50 - 0x5F */
4041 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4042 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004043 N, N, N, N,
4044 N, N, N, N,
4045 N, N, N, N,
4046 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004047 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004048 N, N, N, N,
4049 N, N, N, N,
4050 N, N, N, N,
4051 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004052 /* 0x80 - 0x8F */
4053 X16(D(SrcImm)),
4054 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004055 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004056 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004057 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03004058 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004059 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4060 D(DstMem | SrcReg | Src2CL | ModRM), N, N,
4061 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004062 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004063 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004064 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004065 D(DstMem | SrcReg | Src2ImmByte | ModRM),
4066 D(DstMem | SrcReg | Src2CL | ModRM),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004067 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004068 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004069 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004070 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004071 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004072 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4073 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004074 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004075 /* 0xB8 - 0xBF */
4076 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004077 G(BitOp, group8),
4078 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09004079 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004080 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004081 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004082 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a52010-08-17 09:19:34 +08004083 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004084 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004085 /* 0xC8 - 0xCF */
4086 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004087 /* 0xD0 - 0xDF */
4088 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4089 /* 0xE0 - 0xEF */
4090 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4091 /* 0xF0 - 0xFF */
4092 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4093};
4094
4095#undef D
4096#undef N
4097#undef G
4098#undef GD
4099#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004100#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004101#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004102
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004103#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004104#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004105#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004106#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004107#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004108
Avi Kivity9dac77f2011-06-01 15:34:25 +03004109static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004110{
4111 unsigned size;
4112
Avi Kivity9dac77f2011-06-01 15:34:25 +03004113 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004114 if (size == 8)
4115 size = 4;
4116 return size;
4117}
4118
4119static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4120 unsigned size, bool sign_extension)
4121{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004122 int rc = X86EMUL_CONTINUE;
4123
4124 op->type = OP_IMM;
4125 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004126 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004127 /* NB. Immediates are sign-extended as necessary. */
4128 switch (op->bytes) {
4129 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004130 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004131 break;
4132 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004133 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004134 break;
4135 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004136 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004137 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004138 case 8:
4139 op->val = insn_fetch(s64, ctxt);
4140 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004141 }
4142 if (!sign_extension) {
4143 switch (op->bytes) {
4144 case 1:
4145 op->val &= 0xff;
4146 break;
4147 case 2:
4148 op->val &= 0xffff;
4149 break;
4150 case 4:
4151 op->val &= 0xffffffff;
4152 break;
4153 }
4154 }
4155done:
4156 return rc;
4157}
4158
Avi Kivitya99455492011-09-13 10:45:41 +03004159static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4160 unsigned d)
4161{
4162 int rc = X86EMUL_CONTINUE;
4163
4164 switch (d) {
4165 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004166 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004167 break;
4168 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004169 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004170 break;
4171 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004172 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004173 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004174 *op = ctxt->memop;
4175 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004176 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03004177 fetch_bit_operand(ctxt);
4178 op->orig_val = op->val;
4179 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004180 case OpMem64:
4181 ctxt->memop.bytes = 8;
4182 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004183 case OpAcc:
4184 op->type = OP_REG;
4185 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004186 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004187 fetch_register_operand(op);
4188 op->orig_val = op->val;
4189 break;
4190 case OpDI:
4191 op->type = OP_MEM;
4192 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4193 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004194 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004195 op->addr.mem.seg = VCPU_SREG_ES;
4196 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004197 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004198 break;
4199 case OpDX:
4200 op->type = OP_REG;
4201 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004202 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004203 fetch_register_operand(op);
4204 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004205 case OpCL:
4206 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004207 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004208 break;
4209 case OpImmByte:
4210 rc = decode_imm(ctxt, op, 1, true);
4211 break;
4212 case OpOne:
4213 op->bytes = 1;
4214 op->val = 1;
4215 break;
4216 case OpImm:
4217 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4218 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004219 case OpImm64:
4220 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4221 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004222 case OpMem8:
4223 ctxt->memop.bytes = 1;
4224 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004225 case OpMem16:
4226 ctxt->memop.bytes = 2;
4227 goto mem_common;
4228 case OpMem32:
4229 ctxt->memop.bytes = 4;
4230 goto mem_common;
4231 case OpImmU16:
4232 rc = decode_imm(ctxt, op, 2, false);
4233 break;
4234 case OpImmU:
4235 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4236 break;
4237 case OpSI:
4238 op->type = OP_MEM;
4239 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4240 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004241 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004242 op->addr.mem.seg = seg_override(ctxt);
4243 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004244 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004245 break;
4246 case OpImmFAddr:
4247 op->type = OP_IMM;
4248 op->addr.mem.ea = ctxt->_eip;
4249 op->bytes = ctxt->op_bytes + 2;
4250 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4251 break;
4252 case OpMemFAddr:
4253 ctxt->memop.bytes = ctxt->op_bytes + 2;
4254 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004255 case OpES:
4256 op->val = VCPU_SREG_ES;
4257 break;
4258 case OpCS:
4259 op->val = VCPU_SREG_CS;
4260 break;
4261 case OpSS:
4262 op->val = VCPU_SREG_SS;
4263 break;
4264 case OpDS:
4265 op->val = VCPU_SREG_DS;
4266 break;
4267 case OpFS:
4268 op->val = VCPU_SREG_FS;
4269 break;
4270 case OpGS:
4271 op->val = VCPU_SREG_GS;
4272 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004273 case OpImplicit:
4274 /* Special instructions do their own operand decoding. */
4275 default:
4276 op->type = OP_NONE; /* Disable writeback. */
4277 break;
4278 }
4279
4280done:
4281 return rc;
4282}
4283
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004284int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004285{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004286 int rc = X86EMUL_CONTINUE;
4287 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004288 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004289 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004290 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004291
Avi Kivityf09ed832011-09-13 10:45:40 +03004292 ctxt->memop.type = OP_NONE;
4293 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004294 ctxt->_eip = ctxt->eip;
4295 ctxt->fetch.start = ctxt->_eip;
4296 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004297 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004298 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299
4300 switch (mode) {
4301 case X86EMUL_MODE_REAL:
4302 case X86EMUL_MODE_VM86:
4303 case X86EMUL_MODE_PROT16:
4304 def_op_bytes = def_ad_bytes = 2;
4305 break;
4306 case X86EMUL_MODE_PROT32:
4307 def_op_bytes = def_ad_bytes = 4;
4308 break;
4309#ifdef CONFIG_X86_64
4310 case X86EMUL_MODE_PROT64:
4311 def_op_bytes = 4;
4312 def_ad_bytes = 8;
4313 break;
4314#endif
4315 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004316 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004317 }
4318
Avi Kivity9dac77f2011-06-01 15:34:25 +03004319 ctxt->op_bytes = def_op_bytes;
4320 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004321
4322 /* Legacy prefixes. */
4323 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004324 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004325 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004326 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004327 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004328 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004329 break;
4330 case 0x67: /* address-size override */
4331 if (mode == X86EMUL_MODE_PROT64)
4332 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004333 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004334 else
4335 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004336 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004337 break;
4338 case 0x26: /* ES override */
4339 case 0x2e: /* CS override */
4340 case 0x36: /* SS override */
4341 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004343 break;
4344 case 0x64: /* FS override */
4345 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004346 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004347 break;
4348 case 0x40 ... 0x4f: /* REX */
4349 if (mode != X86EMUL_MODE_PROT64)
4350 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004351 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004352 continue;
4353 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004354 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004355 break;
4356 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004357 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004358 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004359 break;
4360 default:
4361 goto done_prefixes;
4362 }
4363
4364 /* Any legacy prefix after a REX prefix nullifies its effect. */
4365
Avi Kivity9dac77f2011-06-01 15:34:25 +03004366 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004367 }
4368
4369done_prefixes:
4370
4371 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004372 if (ctxt->rex_prefix & 8)
4373 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004374
4375 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004377 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004378 if (ctxt->b == 0x0f) {
4379 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004380 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004381 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004382 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004383 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004384
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004385 if (ctxt->d & ModRM)
4386 ctxt->modrm = insn_fetch(u8, ctxt);
4387
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 while (ctxt->d & GroupMask) {
4389 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004390 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004391 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004392 opcode = opcode.u.group[goffset];
4393 break;
4394 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004395 goffset = (ctxt->modrm >> 3) & 7;
4396 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004397 opcode = opcode.u.gdual->mod3[goffset];
4398 else
4399 opcode = opcode.u.gdual->mod012[goffset];
4400 break;
4401 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004402 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004403 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004404 break;
4405 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004406 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004407 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004408 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004409 switch (simd_prefix) {
4410 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4411 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4412 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4413 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4414 }
4415 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004416 case Escape:
4417 if (ctxt->modrm > 0xbf)
4418 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4419 else
4420 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4421 break;
Avi Kivity46561642011-04-24 14:09:59 +03004422 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004423 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004424 }
Avi Kivity46561642011-04-24 14:09:59 +03004425
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004426 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004427 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004428 }
4429
Avi Kivity9dac77f2011-06-01 15:34:25 +03004430 ctxt->execute = opcode.u.execute;
4431 ctxt->check_perm = opcode.check_perm;
4432 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004433
4434 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004436 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004437
Avi Kivity9dac77f2011-06-01 15:34:25 +03004438 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004439 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004440
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4442 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004443
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004445 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004446 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004447 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004448 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004449 }
4450
Avi Kivity9dac77f2011-06-01 15:34:25 +03004451 if (ctxt->d & Sse)
4452 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004453 else if (ctxt->d & Mmx)
4454 ctxt->op_bytes = 8;
Avi Kivity1253791d2011-03-29 11:41:27 +02004455
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004456 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004457 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004458 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004459 if (!ctxt->has_seg_override)
4460 set_seg_override(ctxt, ctxt->modrm_seg);
4461 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004462 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004463 if (rc != X86EMUL_CONTINUE)
4464 goto done;
4465
Avi Kivity9dac77f2011-06-01 15:34:25 +03004466 if (!ctxt->has_seg_override)
4467 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004468
Avi Kivityf09ed832011-09-13 10:45:40 +03004469 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004470
Avi Kivityf09ed832011-09-13 10:45:40 +03004471 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4472 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004473
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474 /*
4475 * Decode and fetch the source operand: register, memory
4476 * or immediate.
4477 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004478 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004479 if (rc != X86EMUL_CONTINUE)
4480 goto done;
4481
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004482 /*
4483 * Decode and fetch the second source operand: register, memory
4484 * or immediate.
4485 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004486 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004487 if (rc != X86EMUL_CONTINUE)
4488 goto done;
4489
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004490 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004491 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004492
4493done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004494 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4495 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004496
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004497 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004498}
4499
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004500bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4501{
4502 return ctxt->d & PageTable;
4503}
4504
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004505static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4506{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004507 /* The second termination condition only applies for REPE
4508 * and REPNE. Test if the repeat string operation prefix is
4509 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4510 * corresponding termination condition according to:
4511 * - if REPE/REPZ and ZF = 0 then done
4512 * - if REPNE/REPNZ and ZF = 1 then done
4513 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004514 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4515 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4516 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004517 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004518 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004519 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4520 return true;
4521
4522 return false;
4523}
4524
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004525static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4526{
4527 bool fault = false;
4528
4529 ctxt->ops->get_fpu(ctxt);
4530 asm volatile("1: fwait \n\t"
4531 "2: \n\t"
4532 ".pushsection .fixup,\"ax\" \n\t"
4533 "3: \n\t"
4534 "movb $1, %[fault] \n\t"
4535 "jmp 2b \n\t"
4536 ".popsection \n\t"
4537 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004538 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004539 ctxt->ops->put_fpu(ctxt);
4540
4541 if (unlikely(fault))
4542 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4543
4544 return X86EMUL_CONTINUE;
4545}
4546
4547static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4548 struct operand *op)
4549{
4550 if (op->type == OP_MM)
4551 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4552}
4553
Avi Kivitye28bbd42013-01-04 16:18:48 +02004554static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4555{
4556 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4557 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4558 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4559 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4560 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4561 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4562 return X86EMUL_CONTINUE;
4563}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004564
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004565int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004566{
Mathias Krause0225fb52012-08-30 01:30:16 +02004567 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004568 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004569 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004570
Avi Kivity9dac77f2011-06-01 15:34:25 +03004571 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004572
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004574 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004575 goto done;
4576 }
4577
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004578 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004579 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004580 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004581 goto done;
4582 }
4583
Avi Kivity9dac77f2011-06-01 15:34:25 +03004584 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004585 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004586 goto done;
4587 }
4588
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004589 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4590 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004591 rc = emulate_ud(ctxt);
4592 goto done;
4593 }
4594
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004595 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity1253791d2011-03-29 11:41:27 +02004596 rc = emulate_nm(ctxt);
4597 goto done;
4598 }
4599
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004600 if (ctxt->d & Mmx) {
4601 rc = flush_pending_x87_faults(ctxt);
4602 if (rc != X86EMUL_CONTINUE)
4603 goto done;
4604 /*
4605 * Now that we know the fpu is exception safe, we can fetch
4606 * operands from it.
4607 */
4608 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4609 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4610 if (!(ctxt->d & Mov))
4611 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4612 }
4613
Avi Kivity9dac77f2011-06-01 15:34:25 +03004614 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4615 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004616 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004617 if (rc != X86EMUL_CONTINUE)
4618 goto done;
4619 }
4620
Gleb Natapove92805a2010-02-10 14:21:35 +02004621 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004622 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004623 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004624 goto done;
4625 }
4626
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004627 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004628 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004629 rc = emulate_ud(ctxt);
4630 goto done;
4631 }
4632
Joerg Roedeld09beab2011-04-04 12:39:25 +02004633 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004634 if (ctxt->check_perm) {
4635 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004636 if (rc != X86EMUL_CONTINUE)
4637 goto done;
4638 }
4639
Avi Kivity9dac77f2011-06-01 15:34:25 +03004640 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4641 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004642 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004643 if (rc != X86EMUL_CONTINUE)
4644 goto done;
4645 }
4646
Avi Kivity9dac77f2011-06-01 15:34:25 +03004647 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004648 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004649 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004651 goto done;
4652 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004653 }
4654
Avi Kivity9dac77f2011-06-01 15:34:25 +03004655 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4656 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4657 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004658 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004659 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004660 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004661 }
4662
Avi Kivity9dac77f2011-06-01 15:34:25 +03004663 if (ctxt->src2.type == OP_MEM) {
4664 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4665 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004666 if (rc != X86EMUL_CONTINUE)
4667 goto done;
4668 }
4669
Avi Kivity9dac77f2011-06-01 15:34:25 +03004670 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004671 goto special_insn;
4672
4673
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004675 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4677 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004678 if (rc != X86EMUL_CONTINUE)
4679 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004680 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004681 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004682
Avi Kivity018a98d2007-11-27 19:30:56 +02004683special_insn:
4684
Avi Kivity9dac77f2011-06-01 15:34:25 +03004685 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4686 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004687 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004688 if (rc != X86EMUL_CONTINUE)
4689 goto done;
4690 }
4691
Avi Kivity9dac77f2011-06-01 15:34:25 +03004692 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004693 if (ctxt->d & Fastop) {
4694 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4695 rc = fastop(ctxt, fop);
4696 if (rc != X86EMUL_CONTINUE)
4697 goto done;
4698 goto writeback;
4699 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004700 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004701 if (rc != X86EMUL_CONTINUE)
4702 goto done;
4703 goto writeback;
4704 }
4705
Avi Kivity9dac77f2011-06-01 15:34:25 +03004706 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707 goto twobyte_insn;
4708
Avi Kivity9dac77f2011-06-01 15:34:25 +03004709 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004710 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004711 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004712 break;
4713 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004714 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004715 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004717 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004719 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004720 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004721 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004722 if (test_cc(ctxt->b, ctxt->eflags))
4723 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004724 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004725 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004726 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004727 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004728 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004729 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004730 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004731 rc = em_xchg(ctxt);
4732 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004733 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004734 switch (ctxt->op_bytes) {
4735 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4736 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4737 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004738 }
4739 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004740 case 0xc0 ... 0xc1:
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004741 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004742 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004743 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004744 rc = emulate_int(ctxt, 3);
4745 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004746 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004747 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004748 break;
4749 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004750 if (ctxt->eflags & EFLG_OF)
4751 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004752 break;
Avi Kivity018a98d2007-11-27 19:30:56 +02004753 case 0xd0 ... 0xd1: /* Grp2 */
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004754 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004755 break;
4756 case 0xd2 ... 0xd3: /* Grp2 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004757 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RCX);
Takuya Yoshikawa51187682011-05-02 02:29:17 +09004758 rc = em_grp2(ctxt);
Avi Kivity018a98d2007-11-27 19:30:56 +02004759 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004760 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004761 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004762 jmp_rel(ctxt, ctxt->src.val);
4763 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004764 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004765 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004766 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004767 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004768 case 0xf5: /* cmc */
4769 /* complement carry flag from eflags reg */
4770 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004771 break;
4772 case 0xf8: /* clc */
4773 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004774 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004775 case 0xf9: /* stc */
4776 ctxt->eflags |= EFLG_CF;
4777 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004778 case 0xfc: /* cld */
4779 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004780 break;
4781 case 0xfd: /* std */
4782 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004783 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004784 default:
4785 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004786 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004787
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004788 if (rc != X86EMUL_CONTINUE)
4789 goto done;
4790
Avi Kivity018a98d2007-11-27 19:30:56 +02004791writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004792 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004793 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004794 goto done;
4795
Gleb Natapov5cd21912010-03-18 15:20:26 +02004796 /*
4797 * restore dst type in case the decoding will be reused
4798 * (happens for string instruction )
4799 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004800 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004801
Avi Kivity9dac77f2011-06-01 15:34:25 +03004802 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004803 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004804
Avi Kivity9dac77f2011-06-01 15:34:25 +03004805 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004806 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004807
Avi Kivity9dac77f2011-06-01 15:34:25 +03004808 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004809 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004810 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004811 if ((ctxt->d & SrcMask) == SrcSI)
4812 count = ctxt->src.count;
4813 else
4814 count = ctxt->dst.count;
4815 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4816 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004817
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004818 if (!string_insn_completed(ctxt)) {
4819 /*
4820 * Re-enter guest when pio read ahead buffer is empty
4821 * or, if it is not used, after each 1024 iteration.
4822 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004823 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004824 (r->end == 0 || r->end != r->pos)) {
4825 /*
4826 * Reset read cache. Usually happens before
4827 * decode, but since instruction is restarted
4828 * we have to do it here.
4829 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004831 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004832 return EMULATION_RESTART;
4833 }
4834 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004835 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004836 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004837
Avi Kivity9dac77f2011-06-01 15:34:25 +03004838 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004839
4840done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004841 if (rc == X86EMUL_PROPAGATE_FAULT)
4842 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004843 if (rc == X86EMUL_INTERCEPTED)
4844 return EMULATION_INTERCEPTED;
4845
Avi Kivitydd856ef2012-08-27 23:46:17 +03004846 if (rc == X86EMUL_CONTINUE)
4847 writeback_registers(ctxt);
4848
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004849 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850
4851twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004853 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004854 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004855 break;
4856 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004857 case 0x0d: /* GrpP (prefetch) */
4858 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004859 break;
4860 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004861 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004862 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004864 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004867 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4868 if (!test_cc(ctxt->b, ctxt->eflags))
4869 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004871 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 if (test_cc(ctxt->b, ctxt->eflags))
4873 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004874 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004875 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004876 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004877 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004878 case 0xa4: /* shld imm8, r, r/m */
4879 case 0xa5: /* shld cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004880 emulate_2op_cl(ctxt, "shld");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004881 break;
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004882 case 0xac: /* shrd imm8, r, r/m */
4883 case 0xad: /* shrd cl, r, r/m */
Avi Kivity761441b2011-09-07 16:41:36 +03004884 emulate_2op_cl(ctxt, "shrd");
Guillaume Thouvenin9bf8ea42008-12-04 14:30:13 +01004885 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004886 case 0xae: /* clflush */
4887 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004889 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004890 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004891 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004894 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004895 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004896 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004897 break;
Wei Yongjun92f738a52010-08-17 09:19:34 +08004898 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004899 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a52010-08-17 09:19:34 +08004900 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004901 ctxt->src.val = ctxt->dst.orig_val;
4902 write_register_operand(&ctxt->src);
Wei Yongjun92f738a52010-08-17 09:19:34 +08004903 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004904 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004905 ctxt->dst.bytes = ctxt->op_bytes;
4906 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4907 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004908 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004909 default:
4910 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004911 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004912
4913 if (rc != X86EMUL_CONTINUE)
4914 goto done;
4915
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916 goto writeback;
4917
4918cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004919 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004920}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004921
4922void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4923{
4924 invalidate_registers(ctxt);
4925}
4926
4927void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4928{
4929 writeback_registers(ctxt);
4930}