blob: a4c266e99e5032b8a024f9d3153b613020e2fe98 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Paolo Bonzini7fa57952013-05-09 11:32:50 +020063#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
Avi Kivitya9945542011-09-13 10:45:41 +030064
Avi Kivity0fe59122011-09-13 10:45:47 +030065#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030066#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030067
68/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080069 * Opcode effective-address decode tables.
70 * Note that we only emulate instructions that have at least one memory
71 * operand (excluding implicit stack references). We assume that stack
72 * references and instruction fetches will never occur in special memory
73 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
74 * not be handled.
75 */
76
77/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030078#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080079/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030080#define DstShift 1
81#define ImplicitOps (OpImplicit << DstShift)
82#define DstReg (OpReg << DstShift)
83#define DstMem (OpMem << DstShift)
84#define DstAcc (OpAcc << DstShift)
85#define DstDI (OpDI << DstShift)
86#define DstMem64 (OpMem64 << DstShift)
87#define DstImmUByte (OpImmUByte << DstShift)
88#define DstDX (OpDX << DstShift)
89#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080090/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030091#define SrcShift 6
92#define SrcNone (OpNone << SrcShift)
93#define SrcReg (OpReg << SrcShift)
94#define SrcMem (OpMem << SrcShift)
95#define SrcMem16 (OpMem16 << SrcShift)
96#define SrcMem32 (OpMem32 << SrcShift)
97#define SrcImm (OpImm << SrcShift)
98#define SrcImmByte (OpImmByte << SrcShift)
99#define SrcOne (OpOne << SrcShift)
100#define SrcImmUByte (OpImmUByte << SrcShift)
101#define SrcImmU (OpImmU << SrcShift)
102#define SrcSI (OpSI << SrcShift)
Paolo Bonzini7fa57952013-05-09 11:32:50 +0200103#define SrcXLat (OpXLat << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300104#define SrcImmFAddr (OpImmFAddr << SrcShift)
105#define SrcMemFAddr (OpMemFAddr << SrcShift)
106#define SrcAcc (OpAcc << SrcShift)
107#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200108#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200110#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300111#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300112#define BitOp (1<<11)
113#define MemAbs (1<<12) /* Memory operand is absolute displacement */
114#define String (1<<13) /* String instruction (rep capable) */
115#define Stack (1<<14) /* Stack instruction (push/pop) */
116#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
117#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
118#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
119#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
120#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200121#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300122#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300123/* Generic ModRM decode. */
124#define ModRM (1<<19)
125/* Destination is only written; never read. */
126#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300127/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200128#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200129#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300130#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300131#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300132#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200133#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200134#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300135#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800136#define PageTable (1 << 29) /* instruction used to write page table */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300137#define NotImpl (1 << 30) /* instruction is not implemented */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100138/* Source 2 operand type */
Gleb Natapov0b789ee2013-04-11 11:59:55 +0300139#define Src2Shift (31)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300140#define Src2None (OpNone << Src2Shift)
141#define Src2CL (OpCL << Src2Shift)
142#define Src2ImmByte (OpImmByte << Src2Shift)
143#define Src2One (OpOne << Src2Shift)
144#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300145#define Src2ES (OpES << Src2Shift)
146#define Src2CS (OpCS << Src2Shift)
147#define Src2SS (OpSS << Src2Shift)
148#define Src2DS (OpDS << Src2Shift)
149#define Src2FS (OpFS << Src2Shift)
150#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300151#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300152#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300153#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
154#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
155#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200156#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200157#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivityfb32b1e2013-02-09 11:31:44 +0200158#define SrcWrite ((u64)1 << 46) /* Write back src operand */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800159
Avi Kivityd0e53322010-07-29 15:11:54 +0300160#define X2(x...) x, x
161#define X3(x...) X2(x), x
162#define X4(x...) X2(x), X2(x)
163#define X5(x...) X4(x), x
164#define X6(x...) X4(x), X2(x)
165#define X7(x...) X4(x), X3(x)
166#define X8(x...) X4(x), X4(x)
167#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300168
Avi Kivitye28bbd42013-01-04 16:18:48 +0200169#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
170#define FASTOP_SIZE 8
171
172/*
173 * fastop functions have a special calling convention:
174 *
175 * dst: [rdx]:rax (in/out)
176 * src: rbx (in/out)
177 * src2: rcx (in)
178 * flags: rflags (in/out)
179 *
180 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
181 * different operand sizes can be reached by calculation, rather than a jump
182 * table (which would be bigger than the code).
183 *
184 * fastop functions are declared as taking a never-defined fastop parameter,
185 * so they can't be called from C directly.
186 */
187
188struct fastop;
189
Avi Kivityd65b1de2010-07-29 15:11:35 +0300190struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300191 u64 flags : 56;
192 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300193 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300194 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200195 const struct opcode *group;
196 const struct group_dual *gdual;
197 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200198 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200199 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300200 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200201 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300202};
203
204struct group_dual {
205 struct opcode mod012[8];
206 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300207};
208
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200209struct gprefix {
210 struct opcode pfx_no;
211 struct opcode pfx_66;
212 struct opcode pfx_f2;
213 struct opcode pfx_f3;
214};
215
Gleb Natapov045a2822012-12-20 16:57:43 +0200216struct escape {
217 struct opcode op[8];
218 struct opcode high[64];
219};
220
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200222#define EFLG_ID (1<<21)
223#define EFLG_VIP (1<<20)
224#define EFLG_VIF (1<<19)
225#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200226#define EFLG_VM (1<<17)
227#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200228#define EFLG_IOPL (3<<12)
229#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230#define EFLG_OF (1<<11)
231#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200232#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200233#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234#define EFLG_SF (1<<7)
235#define EFLG_ZF (1<<6)
236#define EFLG_AF (1<<4)
237#define EFLG_PF (1<<2)
238#define EFLG_CF (1<<0)
239
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300240#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
241#define EFLG_RESERVED_ONE_MASK 2
242
Avi Kivitydd856ef2012-08-27 23:46:17 +0300243static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
244{
245 if (!(ctxt->regs_valid & (1 << nr))) {
246 ctxt->regs_valid |= 1 << nr;
247 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
248 }
249 return ctxt->_regs[nr];
250}
251
252static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
253{
254 ctxt->regs_valid |= 1 << nr;
255 ctxt->regs_dirty |= 1 << nr;
256 return &ctxt->_regs[nr];
257}
258
259static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
260{
261 reg_read(ctxt, nr);
262 return reg_write(ctxt, nr);
263}
264
265static void writeback_registers(struct x86_emulate_ctxt *ctxt)
266{
267 unsigned reg;
268
269 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
270 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
271}
272
273static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
274{
275 ctxt->regs_dirty = 0;
276 ctxt->regs_valid = 0;
277}
278
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279/*
280 * Instruction emulation:
281 * Most instructions are emulated directly via a fragment of inline assembly
282 * code. This allows us to save/restore EFLAGS and thus very easily pick up
283 * any modified flags.
284 */
285
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800286#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287#define _LO32 "k" /* force 32-bit operand */
288#define _STK "%%rsp" /* stack pointer */
289#elif defined(__i386__)
290#define _LO32 "" /* force 32-bit operand */
291#define _STK "%%esp" /* stack pointer */
292#endif
293
294/*
295 * These EFLAGS bits are restored from saved value during emulation, and
296 * any changes are written back to the saved value after emulation.
297 */
298#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
299
300/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200301#define _PRE_EFLAGS(_sav, _msk, _tmp) \
302 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
303 "movl %"_sav",%"_LO32 _tmp"; " \
304 "push %"_tmp"; " \
305 "push %"_tmp"; " \
306 "movl %"_msk",%"_LO32 _tmp"; " \
307 "andl %"_LO32 _tmp",("_STK"); " \
308 "pushf; " \
309 "notl %"_LO32 _tmp"; " \
310 "andl %"_LO32 _tmp",("_STK"); " \
311 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
312 "pop %"_tmp"; " \
313 "orl %"_LO32 _tmp",("_STK"); " \
314 "popf; " \
315 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316
317/* After executing instruction: write-back necessary bits in EFLAGS. */
318#define _POST_EFLAGS(_sav, _msk, _tmp) \
319 /* _sav |= EFLAGS & _msk; */ \
320 "pushf; " \
321 "pop %"_tmp"; " \
322 "andl %"_msk",%"_LO32 _tmp"; " \
323 "orl %"_LO32 _tmp",%"_sav"; "
324
Avi Kivitydda96d82008-11-26 15:14:10 +0200325#ifdef CONFIG_X86_64
326#define ON64(x) x
327#else
328#define ON64(x)
329#endif
330
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300331#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200332 do { \
333 __asm__ __volatile__ ( \
334 _PRE_EFLAGS("0", "4", "2") \
335 _op _suffix " %"_x"3,%1; " \
336 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300337 : "=m" ((ctxt)->eflags), \
338 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200339 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300340 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200341 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200342
343
Avi Kivity6aa8b732006-12-10 02:21:36 -0800344/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300345#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200346 do { \
347 unsigned long _tmp; \
348 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300349 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200350 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300351 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200352 break; \
353 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300354 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200355 break; \
356 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300357 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200358 break; \
359 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360 } while (0)
361
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300362#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200364 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300365 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300367 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 break; \
369 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300370 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 _wx, _wy, _lx, _ly, _qx, _qy); \
372 break; \
373 } \
374 } while (0)
375
376/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300377#define emulate_2op_SrcB(ctxt, _op) \
378 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379
380/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300381#define emulate_2op_SrcV(ctxt, _op) \
382 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383
384/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300385#define emulate_2op_SrcV_nobyte(ctxt, _op) \
386 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100388/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300389#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300390 do { \
391 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300392 _type _clv = (ctxt)->src2.val; \
393 _type _srcv = (ctxt)->src.val; \
394 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300395 \
396 __asm__ __volatile__ ( \
397 _PRE_EFLAGS("0", "5", "2") \
398 _op _suffix " %4,%1 \n" \
399 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300400 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300401 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
402 ); \
403 \
Avi Kivity761441b2011-09-07 16:41:36 +0300404 (ctxt)->src2.val = (unsigned long) _clv; \
405 (ctxt)->src2.val = (unsigned long) _srcv; \
406 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100407 } while (0)
408
Avi Kivity761441b2011-09-07 16:41:36 +0300409#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300410 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300411 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300412 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300413 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300414 break; \
415 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300416 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300417 break; \
418 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300419 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300420 break; \
421 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100422 } while (0)
423
Avi Kivityd1eef452011-09-07 16:41:38 +0300424#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 do { \
426 unsigned long _tmp; \
427 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200428 __asm__ __volatile__ ( \
429 _PRE_EFLAGS("0", "3", "2") \
430 _op _suffix " %1; " \
431 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300432 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200433 "=&r" (_tmp) \
434 : "i" (EFLAGS_MASK)); \
435 } while (0)
436
437/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300438#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200439 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300440 switch ((ctxt)->dst.bytes) { \
441 case 1: __emulate_1op(ctxt, _op, "b"); break; \
442 case 2: __emulate_1op(ctxt, _op, "w"); break; \
443 case 4: __emulate_1op(ctxt, _op, "l"); break; \
444 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445 } \
446 } while (0)
447
Avi Kivity4d758342013-01-19 19:51:55 +0200448static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
449
Avi Kivityb7d491e2013-01-04 16:18:49 +0200450#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
451#define FOP_RET "ret \n\t"
452
453#define FOP_START(op) \
454 extern void em_##op(struct fastop *fake); \
455 asm(".pushsection .text, \"ax\" \n\t" \
456 ".global em_" #op " \n\t" \
457 FOP_ALIGN \
458 "em_" #op ": \n\t"
459
460#define FOP_END \
461 ".popsection")
462
Avi Kivity0bdea062013-01-19 19:51:50 +0200463#define FOPNOP() FOP_ALIGN FOP_RET
464
Avi Kivityb7d491e2013-01-04 16:18:49 +0200465#define FOP1E(op, dst) \
466 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
467
468#define FASTOP1(op) \
469 FOP_START(op) \
470 FOP1E(op##b, al) \
471 FOP1E(op##w, ax) \
472 FOP1E(op##l, eax) \
473 ON64(FOP1E(op##q, rax)) \
474 FOP_END
475
Avi Kivityf7857f32013-01-04 16:18:53 +0200476#define FOP2E(op, dst, src) \
477 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
478
479#define FASTOP2(op) \
480 FOP_START(op) \
481 FOP2E(op##b, al, bl) \
482 FOP2E(op##w, ax, bx) \
483 FOP2E(op##l, eax, ebx) \
484 ON64(FOP2E(op##q, rax, rbx)) \
485 FOP_END
486
Avi Kivity11c363b2013-01-19 19:51:54 +0200487/* 2 operand, word only */
488#define FASTOP2W(op) \
489 FOP_START(op) \
490 FOPNOP() \
491 FOP2E(op##w, ax, bx) \
492 FOP2E(op##l, eax, ebx) \
493 ON64(FOP2E(op##q, rax, rbx)) \
494 FOP_END
495
Avi Kivity007a3b52013-01-19 19:51:51 +0200496/* 2 operand, src is CL */
497#define FASTOP2CL(op) \
498 FOP_START(op) \
499 FOP2E(op##b, al, cl) \
500 FOP2E(op##w, ax, cl) \
501 FOP2E(op##l, eax, cl) \
502 ON64(FOP2E(op##q, rax, cl)) \
503 FOP_END
504
Avi Kivity0bdea062013-01-19 19:51:50 +0200505#define FOP3E(op, dst, src, src2) \
506 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
507
508/* 3-operand, word-only, src2=cl */
509#define FASTOP3WCL(op) \
510 FOP_START(op) \
511 FOPNOP() \
512 FOP3E(op##w, ax, bx, cl) \
513 FOP3E(op##l, eax, ebx, cl) \
514 ON64(FOP3E(op##q, rax, rbx, cl)) \
515 FOP_END
516
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200517/* Special case for SETcc - 1 instruction per cc */
518#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
519
520FOP_START(setcc)
521FOP_SETCC(seto)
522FOP_SETCC(setno)
523FOP_SETCC(setc)
524FOP_SETCC(setnc)
525FOP_SETCC(setz)
526FOP_SETCC(setnz)
527FOP_SETCC(setbe)
528FOP_SETCC(setnbe)
529FOP_SETCC(sets)
530FOP_SETCC(setns)
531FOP_SETCC(setp)
532FOP_SETCC(setnp)
533FOP_SETCC(setl)
534FOP_SETCC(setnl)
535FOP_SETCC(setle)
536FOP_SETCC(setnle)
537FOP_END;
538
Paolo Bonzini326f5782013-05-09 11:32:51 +0200539FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
540FOP_END;
541
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300542#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300543 do { \
544 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300545 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
546 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300547 \
548 __asm__ __volatile__ ( \
549 _PRE_EFLAGS("0", "5", "1") \
550 "1: \n\t" \
551 _op _suffix " %6; " \
552 "2: \n\t" \
553 _POST_EFLAGS("0", "5", "1") \
554 ".pushsection .fixup,\"ax\" \n\t" \
555 "3: movb $1, %4 \n\t" \
556 "jmp 2b \n\t" \
557 ".popsection \n\t" \
558 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300559 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
560 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800561 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300562 } while (0)
563
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300564/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300565#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300566 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300567 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300568 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300569 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300570 break; \
571 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300572 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300573 break; \
574 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300575 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300576 break; \
577 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300578 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300579 break; \
580 } \
581 } while (0)
582
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200583static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
584 enum x86_intercept intercept,
585 enum x86_intercept_stage stage)
586{
587 struct x86_instruction_info info = {
588 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300589 .rep_prefix = ctxt->rep_prefix,
590 .modrm_mod = ctxt->modrm_mod,
591 .modrm_reg = ctxt->modrm_reg,
592 .modrm_rm = ctxt->modrm_rm,
593 .src_val = ctxt->src.val64,
594 .src_bytes = ctxt->src.bytes,
595 .dst_bytes = ctxt->dst.bytes,
596 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200597 .next_rip = ctxt->eip,
598 };
599
Avi Kivity29535382011-04-20 13:37:53 +0300600 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200601}
602
Avi Kivityf47cfa32012-06-07 17:49:24 +0300603static void assign_masked(ulong *dest, ulong src, ulong mask)
604{
605 *dest = (*dest & ~mask) | (src & mask);
606}
607
Avi Kivity9dac77f2011-06-01 15:34:25 +0300608static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800609{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300610 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800611}
612
Avi Kivityf47cfa32012-06-07 17:49:24 +0300613static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
614{
615 u16 sel;
616 struct desc_struct ss;
617
618 if (ctxt->mode == X86EMUL_MODE_PROT64)
619 return ~0UL;
620 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
621 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
622}
623
Avi Kivity612e89f2012-06-12 20:03:23 +0300624static int stack_size(struct x86_emulate_ctxt *ctxt)
625{
626 return (__fls(stack_mask(ctxt)) + 1) >> 3;
627}
628
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800630static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300631address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800632{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300633 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800634 return reg;
635 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300636 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800637}
638
639static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300640register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800641{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300642 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800643}
644
Avi Kivity5ad105e2012-08-19 14:34:31 +0300645static void masked_increment(ulong *reg, ulong mask, int inc)
646{
647 assign_masked(reg, *reg + inc, mask);
648}
649
Harvey Harrison7a9572752008-02-19 07:40:41 -0800650static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300651register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800652{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300653 ulong mask;
654
Avi Kivity9dac77f2011-06-01 15:34:25 +0300655 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300656 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800657 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300658 mask = ad_mask(ctxt);
659 masked_increment(reg, mask, inc);
660}
661
662static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
663{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300664 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800665}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666
Avi Kivity9dac77f2011-06-01 15:34:25 +0300667static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800668{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300669 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800670}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300671
Avi Kivity56697682011-04-03 14:08:51 +0300672static u32 desc_limit_scaled(struct desc_struct *desc)
673{
674 u32 limit = get_desc_limit(desc);
675
676 return desc->g ? (limit << 12) | 0xfff : limit;
677}
678
Avi Kivity9dac77f2011-06-01 15:34:25 +0300679static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300680{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300681 ctxt->has_seg_override = true;
682 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300683}
684
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900685static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300686{
687 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
688 return 0;
689
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900690 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300691}
692
Avi Kivity9dac77f2011-06-01 15:34:25 +0300693static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300694{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300695 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300696 return 0;
697
Avi Kivity9dac77f2011-06-01 15:34:25 +0300698 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300699}
700
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200701static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
702 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300703{
Avi Kivityda9cb572010-11-22 17:53:21 +0200704 ctxt->exception.vector = vec;
705 ctxt->exception.error_code = error;
706 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200707 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300708}
709
Joerg Roedel3b88e412011-04-04 12:39:29 +0200710static int emulate_db(struct x86_emulate_ctxt *ctxt)
711{
712 return emulate_exception(ctxt, DB_VECTOR, 0, false);
713}
714
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200715static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300716{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200717 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300718}
719
Avi Kivity618ff152011-04-03 12:32:09 +0300720static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
721{
722 return emulate_exception(ctxt, SS_VECTOR, err, true);
723}
724
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200725static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300726{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200727 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300728}
729
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200730static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300731{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200732 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300733}
734
Avi Kivity34d1f492010-08-26 11:59:01 +0300735static int emulate_de(struct x86_emulate_ctxt *ctxt)
736{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200737 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300738}
739
Avi Kivity12537912011-03-29 11:41:27 +0200740static int emulate_nm(struct x86_emulate_ctxt *ctxt)
741{
742 return emulate_exception(ctxt, NM_VECTOR, 0, false);
743}
744
Avi Kivity1aa36612011-04-27 13:20:30 +0300745static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
746{
747 u16 selector;
748 struct desc_struct desc;
749
750 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
751 return selector;
752}
753
754static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
755 unsigned seg)
756{
757 u16 dummy;
758 u32 base3;
759 struct desc_struct desc;
760
761 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
762 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
763}
764
Avi Kivity1c11b372012-04-09 18:39:59 +0300765/*
766 * x86 defines three classes of vector instructions: explicitly
767 * aligned, explicitly unaligned, and the rest, which change behaviour
768 * depending on whether they're AVX encoded or not.
769 *
770 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
771 * subject to the same check.
772 */
773static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
774{
775 if (likely(size < 16))
776 return false;
777
778 if (ctxt->d & Aligned)
779 return true;
780 else if (ctxt->d & Unaligned)
781 return false;
782 else if (ctxt->d & Avx)
783 return false;
784 else
785 return true;
786}
787
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400788static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300789 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400790 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300791 ulong *linear)
792{
Avi Kivity618ff152011-04-03 12:32:09 +0300793 struct desc_struct desc;
794 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300795 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300796 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300797 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200798 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300799
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900800 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300801 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300802 case X86EMUL_MODE_PROT64:
803 if (((signed long)la << 16) >> 16 != la)
804 return emulate_gp(ctxt, 0);
805 break;
806 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300807 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
808 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300809 if (!usable)
810 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200811 /* code segment in protected mode or read-only data segment */
812 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
813 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300814 goto bad;
815 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400816 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300817 goto bad;
818 lim = desc_limit_scaled(&desc);
819 if ((desc.type & 8) || !(desc.type & 4)) {
820 /* expand-up segment */
821 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
822 goto bad;
823 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800824 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300825 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
826 goto bad;
827 lim = desc.d ? 0xffffffff : 0xffff;
828 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
829 goto bad;
830 }
Avi Kivity717746e2011-04-20 13:37:53 +0300831 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300832 if (!(desc.type & 8)) {
833 /* data segment */
834 if (cpl > desc.dpl)
835 goto bad;
836 } else if ((desc.type & 8) && !(desc.type & 4)) {
837 /* nonconforming code segment */
838 if (cpl != desc.dpl)
839 goto bad;
840 } else if ((desc.type & 8) && (desc.type & 4)) {
841 /* conforming code segment */
842 if (cpl < desc.dpl)
843 goto bad;
844 }
845 break;
846 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300847 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300848 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300849 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
850 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300851 *linear = la;
852 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300853bad:
854 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300855 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300856 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300857 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300858}
859
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400860static int linearize(struct x86_emulate_ctxt *ctxt,
861 struct segmented_address addr,
862 unsigned size, bool write,
863 ulong *linear)
864{
865 return __linearize(ctxt, addr, size, write, false, linear);
866}
867
868
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200869static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
870 struct segmented_address addr,
871 void *data,
872 unsigned size)
873{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200874 int rc;
875 ulong linear;
876
Avi Kivity83b87952011-04-03 11:31:19 +0300877 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200878 if (rc != X86EMUL_CONTINUE)
879 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300880 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200881}
882
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900883/*
884 * Fetch the next byte of the instruction being emulated which is pointed to
885 * by ctxt->_eip, then increment ctxt->_eip.
886 *
887 * Also prefetch the remaining bytes of the instruction without crossing page
888 * boundary if they are not in fetch_cache yet.
889 */
890static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200891{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300892 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200893 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300894 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200895
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900896 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400897 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900898 struct segmented_address addr = { .seg = VCPU_SREG_CS,
899 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300900 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900901 size = min(15UL - cur_size,
902 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400903 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900904 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400905 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900906 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
907 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900908 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200909 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300910 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200911 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900912 *dest = fc->data[ctxt->_eip - fc->start];
913 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900914 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200915}
916
917static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900918 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200919{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900920 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200921
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200922 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900923 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200924 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200925 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900926 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900927 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200928 return rc;
929 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900930 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200931}
932
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900933/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900934#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900935({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900936 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900937 if (rc != X86EMUL_CONTINUE) \
938 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900939 (_type)_x; \
940})
941
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900942#define insn_fetch_arr(_arr, _size, _ctxt) \
943({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900944 if (rc != X86EMUL_CONTINUE) \
945 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900946})
947
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000948/*
949 * Given the 'reg' portion of a ModRM byte, and a register block, return a
950 * pointer into the block that addresses the relevant register.
951 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
952 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300953static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000954 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955{
956 void *p;
957
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300959 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
960 else
961 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 return p;
963}
964
965static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200966 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 u16 *size, unsigned long *address, int op_bytes)
968{
969 int rc;
970
971 if (op_bytes == 2)
972 op_bytes = 3;
973 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200974 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900975 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200977 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200978 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 return rc;
980}
981
Avi Kivity34b77652013-01-19 19:51:56 +0200982FASTOP2(add);
983FASTOP2(or);
984FASTOP2(adc);
985FASTOP2(sbb);
986FASTOP2(and);
987FASTOP2(sub);
988FASTOP2(xor);
989FASTOP2(cmp);
990FASTOP2(test);
991
992FASTOP3WCL(shld);
993FASTOP3WCL(shrd);
994
995FASTOP2W(imul);
996
997FASTOP1(not);
998FASTOP1(neg);
999FASTOP1(inc);
1000FASTOP1(dec);
1001
1002FASTOP2CL(rol);
1003FASTOP2CL(ror);
1004FASTOP2CL(rcl);
1005FASTOP2CL(rcr);
1006FASTOP2CL(shl);
1007FASTOP2CL(shr);
1008FASTOP2CL(sar);
1009
1010FASTOP2W(bsf);
1011FASTOP2W(bsr);
1012FASTOP2W(bt);
1013FASTOP2W(bts);
1014FASTOP2W(btr);
1015FASTOP2W(btc);
1016
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001017static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001018{
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001019 u8 rc;
1020 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001021
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001022 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
Avi Kivity3f0c3d02013-01-26 23:56:04 +02001023 asm("push %[flags]; popf; call *%[fastop]"
Avi Kivity9ae9feb2013-01-19 19:51:52 +02001024 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
1025 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +03001026}
1027
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001028static void fetch_register_operand(struct operand *op)
1029{
1030 switch (op->bytes) {
1031 case 1:
1032 op->val = *(u8 *)op->addr.reg;
1033 break;
1034 case 2:
1035 op->val = *(u16 *)op->addr.reg;
1036 break;
1037 case 4:
1038 op->val = *(u32 *)op->addr.reg;
1039 break;
1040 case 8:
1041 op->val = *(u64 *)op->addr.reg;
1042 break;
1043 }
1044}
1045
Avi Kivity12537912011-03-29 11:41:27 +02001046static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1047{
1048 ctxt->ops->get_fpu(ctxt);
1049 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001050 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1051 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1052 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1053 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1054 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1055 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1056 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1057 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001058#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001059 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1060 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1061 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1062 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1063 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1064 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1065 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1066 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001067#endif
1068 default: BUG();
1069 }
1070 ctxt->ops->put_fpu(ctxt);
1071}
1072
1073static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1074 int reg)
1075{
1076 ctxt->ops->get_fpu(ctxt);
1077 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001078 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1079 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1080 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1081 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1082 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1083 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1084 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1085 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001086#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001087 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1088 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1089 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1090 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1091 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1092 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1093 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1094 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001095#endif
1096 default: BUG();
1097 }
1098 ctxt->ops->put_fpu(ctxt);
1099}
1100
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001101static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1102{
1103 ctxt->ops->get_fpu(ctxt);
1104 switch (reg) {
1105 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1106 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1107 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1108 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1109 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1110 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1111 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1112 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1113 default: BUG();
1114 }
1115 ctxt->ops->put_fpu(ctxt);
1116}
1117
1118static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1119{
1120 ctxt->ops->get_fpu(ctxt);
1121 switch (reg) {
1122 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1123 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1124 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1125 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1126 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1127 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1128 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1129 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1130 default: BUG();
1131 }
1132 ctxt->ops->put_fpu(ctxt);
1133}
1134
Gleb Natapov045a2822012-12-20 16:57:43 +02001135static int em_fninit(struct x86_emulate_ctxt *ctxt)
1136{
1137 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1138 return emulate_nm(ctxt);
1139
1140 ctxt->ops->get_fpu(ctxt);
1141 asm volatile("fninit");
1142 ctxt->ops->put_fpu(ctxt);
1143 return X86EMUL_CONTINUE;
1144}
1145
1146static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1147{
1148 u16 fcw;
1149
1150 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1151 return emulate_nm(ctxt);
1152
1153 ctxt->ops->get_fpu(ctxt);
1154 asm volatile("fnstcw %0": "+m"(fcw));
1155 ctxt->ops->put_fpu(ctxt);
1156
1157 /* force 2 byte destination */
1158 ctxt->dst.bytes = 2;
1159 ctxt->dst.val = fcw;
1160
1161 return X86EMUL_CONTINUE;
1162}
1163
1164static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1165{
1166 u16 fsw;
1167
1168 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1169 return emulate_nm(ctxt);
1170
1171 ctxt->ops->get_fpu(ctxt);
1172 asm volatile("fnstsw %0": "+m"(fsw));
1173 ctxt->ops->put_fpu(ctxt);
1174
1175 /* force 2 byte destination */
1176 ctxt->dst.bytes = 2;
1177 ctxt->dst.val = fsw;
1178
1179 return X86EMUL_CONTINUE;
1180}
1181
Avi Kivity12537912011-03-29 11:41:27 +02001182static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001183 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001184{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 unsigned reg = ctxt->modrm_reg;
1186 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001187
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 if (!(ctxt->d & ModRM))
1189 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001190
Avi Kivity9dac77f2011-06-01 15:34:25 +03001191 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001192 op->type = OP_XMM;
1193 op->bytes = 16;
1194 op->addr.xmm = reg;
1195 read_sse_reg(ctxt, &op->vec_val, reg);
1196 return;
1197 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001198 if (ctxt->d & Mmx) {
1199 reg &= 7;
1200 op->type = OP_MM;
1201 op->bytes = 8;
1202 op->addr.mm = reg;
1203 return;
1204 }
Avi Kivity12537912011-03-29 11:41:27 +02001205
Avi Kivity3c118e22007-10-31 10:27:04 +02001206 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001207 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001208 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001209 op->bytes = 1;
1210 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001211 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001212 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001213 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001214 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001215 op->orig_val = op->val;
1216}
1217
Avi Kivitya6e34072012-06-10 17:15:39 +03001218static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1219{
1220 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1221 ctxt->modrm_seg = VCPU_SREG_SS;
1222}
1223
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001225 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001227 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001228 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001229 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001230 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001231
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 if (ctxt->rex_prefix) {
1233 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1234 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1235 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 }
1237
Avi Kivity9dac77f2011-06-01 15:34:25 +03001238 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1239 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1240 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1241 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242
Avi Kivity9dac77f2011-06-01 15:34:25 +03001243 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001244 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001245 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001246 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001247 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001248 op->type = OP_XMM;
1249 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001250 op->addr.xmm = ctxt->modrm_rm;
1251 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001252 return rc;
1253 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001254 if (ctxt->d & Mmx) {
1255 op->type = OP_MM;
1256 op->bytes = 8;
1257 op->addr.xmm = ctxt->modrm_rm & 7;
1258 return rc;
1259 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001260 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001261 return rc;
1262 }
1263
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001264 op->type = OP_MEM;
1265
Avi Kivity9dac77f2011-06-01 15:34:25 +03001266 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001267 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1268 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1269 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1270 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001271
1272 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001273 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001274 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001275 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001276 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001277 break;
1278 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001279 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001280 break;
1281 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001282 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001283 break;
1284 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001285 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001286 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001287 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001288 break;
1289 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001290 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001291 break;
1292 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001293 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001294 break;
1295 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001296 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001297 break;
1298 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001299 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001300 break;
1301 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001302 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001303 break;
1304 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001305 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001306 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001307 break;
1308 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001309 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001310 break;
1311 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001312 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1313 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1314 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001315 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001316 } else {
1317 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001318 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001319 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001320 index_reg |= (sib >> 3) & 7;
1321 base_reg |= sib & 7;
1322 scale = sib >> 6;
1323
Avi Kivity9dac77f2011-06-01 15:34:25 +03001324 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001325 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001326 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001327 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001328 adjust_modrm_seg(ctxt, base_reg);
1329 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001330 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001331 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001332 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001333 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001334 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001335 } else {
1336 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001337 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001338 adjust_modrm_seg(ctxt, base_reg);
1339 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001340 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001341 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001342 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001343 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001344 break;
1345 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001346 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001347 break;
1348 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001349 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001350 break;
1351 }
1352 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001353 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001354done:
1355 return rc;
1356}
1357
1358static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001359 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001360{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001361 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001362
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001363 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001364 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001365 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001366 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001367 break;
1368 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001369 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001370 break;
1371 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001372 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001373 break;
1374 }
1375done:
1376 return rc;
1377}
1378
Avi Kivity9dac77f2011-06-01 15:34:25 +03001379static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001380{
Sheng Yang7129eec2010-09-28 16:33:32 +08001381 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001382
Avi Kivity9dac77f2011-06-01 15:34:25 +03001383 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1384 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001385
Avi Kivity9dac77f2011-06-01 15:34:25 +03001386 if (ctxt->src.bytes == 2)
1387 sv = (s16)ctxt->src.val & (s16)mask;
1388 else if (ctxt->src.bytes == 4)
1389 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001390
Avi Kivity9dac77f2011-06-01 15:34:25 +03001391 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001392 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001393
1394 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001395 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001396}
1397
Gleb Natapov9de41572010-04-28 19:15:22 +03001398static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001399 unsigned long addr, void *dest, unsigned size)
1400{
1401 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001402 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001403
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001404 if (mc->pos < mc->end)
1405 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001406
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001407 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001408
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001409 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1410 &ctxt->exception);
1411 if (rc != X86EMUL_CONTINUE)
1412 return rc;
1413
1414 mc->end += size;
1415
1416read_cached:
1417 memcpy(dest, mc->data + mc->pos, size);
1418 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001419 return X86EMUL_CONTINUE;
1420}
1421
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001422static int segmented_read(struct x86_emulate_ctxt *ctxt,
1423 struct segmented_address addr,
1424 void *data,
1425 unsigned size)
1426{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001427 int rc;
1428 ulong linear;
1429
Avi Kivity83b87952011-04-03 11:31:19 +03001430 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001431 if (rc != X86EMUL_CONTINUE)
1432 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001433 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001434}
1435
1436static int segmented_write(struct x86_emulate_ctxt *ctxt,
1437 struct segmented_address addr,
1438 const void *data,
1439 unsigned size)
1440{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001441 int rc;
1442 ulong linear;
1443
Avi Kivity83b87952011-04-03 11:31:19 +03001444 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001445 if (rc != X86EMUL_CONTINUE)
1446 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001447 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1448 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001449}
1450
1451static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1452 struct segmented_address addr,
1453 const void *orig_data, const void *data,
1454 unsigned size)
1455{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001456 int rc;
1457 ulong linear;
1458
Avi Kivity83b87952011-04-03 11:31:19 +03001459 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001460 if (rc != X86EMUL_CONTINUE)
1461 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001462 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1463 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001464}
1465
Gleb Natapov7b262e92010-03-18 15:20:27 +02001466static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001467 unsigned int size, unsigned short port,
1468 void *dest)
1469{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001470 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001471
1472 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001473 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001474 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001475 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001476 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001477 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1478 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001479 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1480 count);
1481 if (n == 0)
1482 n = 1;
1483 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001484 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001485 return 0;
1486 rc->end = n * size;
1487 }
1488
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001489 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1490 ctxt->dst.data = rc->data + rc->pos;
1491 ctxt->dst.type = OP_MEM_STR;
1492 ctxt->dst.count = (rc->end - rc->pos) / size;
1493 rc->pos = rc->end;
1494 } else {
1495 memcpy(dest, rc->data + rc->pos, size);
1496 rc->pos += size;
1497 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001498 return 1;
1499}
1500
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001501static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1502 u16 index, struct desc_struct *desc)
1503{
1504 struct desc_ptr dt;
1505 ulong addr;
1506
1507 ctxt->ops->get_idt(ctxt, &dt);
1508
1509 if (dt.size < index * 8 + 7)
1510 return emulate_gp(ctxt, index << 3 | 0x2);
1511
1512 addr = dt.address + index * 8;
1513 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1514 &ctxt->exception);
1515}
1516
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001517static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001518 u16 selector, struct desc_ptr *dt)
1519{
Mathias Krause0225fb52012-08-30 01:30:16 +02001520 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001521
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001522 if (selector & 1 << 2) {
1523 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001524 u16 sel;
1525
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001526 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001527 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001528 return;
1529
1530 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1531 dt->address = get_desc_base(&desc);
1532 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001533 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001534}
1535
1536/* allowed just for 8 bytes segments */
1537static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001538 u16 selector, struct desc_struct *desc,
1539 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001540{
1541 struct desc_ptr dt;
1542 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001543 ulong addr;
1544
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001545 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001546
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001547 if (dt.size < index * 8 + 7)
1548 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549
Avi Kivitye9194642012-06-13 16:29:39 +03001550 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001551 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1552 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553}
1554
1555/* allowed just for 8 bytes segments */
1556static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001557 u16 selector, struct desc_struct *desc)
1558{
1559 struct desc_ptr dt;
1560 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001561 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001562
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001563 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001564
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001565 if (dt.size < index * 8 + 7)
1566 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001567
1568 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001569 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1570 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571}
1572
Gleb Natapov5601d052011-03-07 14:55:06 +02001573/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001574static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001575 u16 selector, int seg)
1576{
Avi Kivity869be992012-06-13 16:30:53 +03001577 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001578 u8 dpl, rpl, cpl;
1579 unsigned err_vec = GP_VECTOR;
1580 u32 err_code = 0;
1581 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001582 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001583 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001584 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001585
1586 memset(&seg_desc, 0, sizeof seg_desc);
1587
Kevin Wolff8da94e2013-04-11 14:06:03 +02001588 if (ctxt->mode == X86EMUL_MODE_REAL) {
1589 /* set real mode segment descriptor (keep limit etc. for
1590 * unreal mode) */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001591 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001592 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001593 goto load;
Kevin Wolff8da94e2013-04-11 14:06:03 +02001594 } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
1595 /* VM86 needs a clean new segment descriptor */
1596 set_desc_base(&seg_desc, selector << 4);
1597 set_desc_limit(&seg_desc, 0xffff);
1598 seg_desc.type = 3;
1599 seg_desc.p = 1;
1600 seg_desc.s = 1;
1601 seg_desc.dpl = 3;
1602 goto load;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001603 }
1604
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001605 rpl = selector & 3;
1606 cpl = ctxt->ops->cpl(ctxt);
1607
1608 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1609 if ((seg == VCPU_SREG_CS
1610 || (seg == VCPU_SREG_SS
1611 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1612 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001613 && null_selector)
1614 goto exception;
1615
1616 /* TR should be in GDT only */
1617 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1618 goto exception;
1619
1620 if (null_selector) /* for NULL selector skip all following checks */
1621 goto load;
1622
Avi Kivitye9194642012-06-13 16:29:39 +03001623 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001624 if (ret != X86EMUL_CONTINUE)
1625 return ret;
1626
1627 err_code = selector & 0xfffc;
1628 err_vec = GP_VECTOR;
1629
Guo Chaofc058682012-06-28 15:19:51 +08001630 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001631 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1632 goto exception;
1633
1634 if (!seg_desc.p) {
1635 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1636 goto exception;
1637 }
1638
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001639 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001640
1641 switch (seg) {
1642 case VCPU_SREG_SS:
1643 /*
1644 * segment is not a writable data segment or segment
1645 * selector's RPL != CPL or segment selector's RPL != CPL
1646 */
1647 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1648 goto exception;
1649 break;
1650 case VCPU_SREG_CS:
1651 if (!(seg_desc.type & 8))
1652 goto exception;
1653
1654 if (seg_desc.type & 4) {
1655 /* conforming */
1656 if (dpl > cpl)
1657 goto exception;
1658 } else {
1659 /* nonconforming */
1660 if (rpl > cpl || dpl != cpl)
1661 goto exception;
1662 }
1663 /* CS(RPL) <- CPL */
1664 selector = (selector & 0xfffc) | cpl;
1665 break;
1666 case VCPU_SREG_TR:
1667 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1668 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001669 old_desc = seg_desc;
1670 seg_desc.type |= 2; /* busy */
1671 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1672 sizeof(seg_desc), &ctxt->exception);
1673 if (ret != X86EMUL_CONTINUE)
1674 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001675 break;
1676 case VCPU_SREG_LDTR:
1677 if (seg_desc.s || seg_desc.type != 2)
1678 goto exception;
1679 break;
1680 default: /* DS, ES, FS, or GS */
1681 /*
1682 * segment is not a data or readable code segment or
1683 * ((segment is a data or nonconforming code segment)
1684 * and (both RPL and CPL > DPL))
1685 */
1686 if ((seg_desc.type & 0xa) == 0x8 ||
1687 (((seg_desc.type & 0xc) != 0xc) &&
1688 (rpl > dpl && cpl > dpl)))
1689 goto exception;
1690 break;
1691 }
1692
1693 if (seg_desc.s) {
1694 /* mark segment as accessed */
1695 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001696 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001697 if (ret != X86EMUL_CONTINUE)
1698 return ret;
1699 }
1700load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001701 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001702 return X86EMUL_CONTINUE;
1703exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001704 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001705 return X86EMUL_PROPAGATE_FAULT;
1706}
1707
Wei Yongjun31be40b2010-08-17 09:17:30 +08001708static void write_register_operand(struct operand *op)
1709{
1710 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1711 switch (op->bytes) {
1712 case 1:
1713 *(u8 *)op->addr.reg = (u8)op->val;
1714 break;
1715 case 2:
1716 *(u16 *)op->addr.reg = (u16)op->val;
1717 break;
1718 case 4:
1719 *op->addr.reg = (u32)op->val;
1720 break; /* 64b: zero-extend */
1721 case 8:
1722 *op->addr.reg = op->val;
1723 break;
1724 }
1725}
1726
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001727static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001728{
1729 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001730
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001731 switch (op->type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001732 case OP_REG:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001733 write_register_operand(op);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001734 break;
1735 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001736 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001737 rc = segmented_cmpxchg(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001738 op->addr.mem,
1739 &op->orig_val,
1740 &op->val,
1741 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001742 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001743 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001744 op->addr.mem,
1745 &op->val,
1746 op->bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001747 if (rc != X86EMUL_CONTINUE)
1748 return rc;
1749 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001750 case OP_MEM_STR:
1751 rc = segmented_write(ctxt,
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001752 op->addr.mem,
1753 op->data,
1754 op->bytes * op->count);
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001755 if (rc != X86EMUL_CONTINUE)
1756 return rc;
1757 break;
Avi Kivity12537912011-03-29 11:41:27 +02001758 case OP_XMM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001759 write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001760 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001761 case OP_MM:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02001762 write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001763 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001764 case OP_NONE:
1765 /* no writeback */
1766 break;
1767 default:
1768 break;
1769 }
1770 return X86EMUL_CONTINUE;
1771}
1772
Avi Kivity51ddff52012-06-12 20:19:40 +03001773static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001774{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001775 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001776
Avi Kivity5ad105e2012-08-19 14:34:31 +03001777 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001778 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001779 addr.seg = VCPU_SREG_SS;
1780
Avi Kivity51ddff52012-06-12 20:19:40 +03001781 return segmented_write(ctxt, addr, data, bytes);
1782}
1783
1784static int em_push(struct x86_emulate_ctxt *ctxt)
1785{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001786 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001787 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001788 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001789}
1790
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001791static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001792 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001793{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001794 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001795 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001796
Avi Kivitydd856ef2012-08-27 23:46:17 +03001797 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001798 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001799 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001800 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001801 return rc;
1802
Avi Kivity5ad105e2012-08-19 14:34:31 +03001803 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001804 return rc;
1805}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001806
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001807static int em_pop(struct x86_emulate_ctxt *ctxt)
1808{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001809 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001810}
1811
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001812static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001813 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001814{
1815 int rc;
1816 unsigned long val, change_mask;
1817 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001818 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001819
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001820 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001821 if (rc != X86EMUL_CONTINUE)
1822 return rc;
1823
1824 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1825 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1826
1827 switch(ctxt->mode) {
1828 case X86EMUL_MODE_PROT64:
1829 case X86EMUL_MODE_PROT32:
1830 case X86EMUL_MODE_PROT16:
1831 if (cpl == 0)
1832 change_mask |= EFLG_IOPL;
1833 if (cpl <= iopl)
1834 change_mask |= EFLG_IF;
1835 break;
1836 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001837 if (iopl < 3)
1838 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001839 change_mask |= EFLG_IF;
1840 break;
1841 default: /* real mode */
1842 change_mask |= (EFLG_IOPL | EFLG_IF);
1843 break;
1844 }
1845
1846 *(unsigned long *)dest =
1847 (ctxt->eflags & ~change_mask) | (val & change_mask);
1848
1849 return rc;
1850}
1851
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001852static int em_popf(struct x86_emulate_ctxt *ctxt)
1853{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001854 ctxt->dst.type = OP_REG;
1855 ctxt->dst.addr.reg = &ctxt->eflags;
1856 ctxt->dst.bytes = ctxt->op_bytes;
1857 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001858}
1859
Avi Kivity612e89f2012-06-12 20:03:23 +03001860static int em_enter(struct x86_emulate_ctxt *ctxt)
1861{
1862 int rc;
1863 unsigned frame_size = ctxt->src.val;
1864 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001865 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001866
1867 if (nesting_level)
1868 return X86EMUL_UNHANDLEABLE;
1869
Avi Kivitydd856ef2012-08-27 23:46:17 +03001870 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1871 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001872 if (rc != X86EMUL_CONTINUE)
1873 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001874 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001875 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001876 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1877 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001878 stack_mask(ctxt));
1879 return X86EMUL_CONTINUE;
1880}
1881
Avi Kivityf47cfa32012-06-07 17:49:24 +03001882static int em_leave(struct x86_emulate_ctxt *ctxt)
1883{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001884 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001885 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001886 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001887}
1888
Avi Kivity1cd196e2011-09-13 10:45:51 +03001889static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001890{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001891 int seg = ctxt->src2.val;
1892
Avi Kivity9dac77f2011-06-01 15:34:25 +03001893 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001894
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001895 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001896}
1897
Avi Kivity1cd196e2011-09-13 10:45:51 +03001898static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001899{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001900 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001901 unsigned long selector;
1902 int rc;
1903
Avi Kivity9dac77f2011-06-01 15:34:25 +03001904 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001905 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001906 return rc;
1907
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001908 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001909 return rc;
1910}
1911
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001912static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001913{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001914 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001915 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001916 int reg = VCPU_REGS_RAX;
1917
1918 while (reg <= VCPU_REGS_RDI) {
1919 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001920 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001921
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001922 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001923 if (rc != X86EMUL_CONTINUE)
1924 return rc;
1925
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001926 ++reg;
1927 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001928
Wei Yongjunc37eda12010-06-15 09:03:33 +08001929 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001930}
1931
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001932static int em_pushf(struct x86_emulate_ctxt *ctxt)
1933{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001934 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001935 return em_push(ctxt);
1936}
1937
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001938static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001939{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001940 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001941 int reg = VCPU_REGS_RDI;
1942
1943 while (reg >= VCPU_REGS_RAX) {
1944 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001945 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001946 --reg;
1947 }
1948
Avi Kivitydd856ef2012-08-27 23:46:17 +03001949 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001950 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001951 break;
1952 --reg;
1953 }
1954 return rc;
1955}
1956
Avi Kivitydd856ef2012-08-27 23:46:17 +03001957static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001958{
Mathias Krause0225fb52012-08-30 01:30:16 +02001959 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001960 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001961 struct desc_ptr dt;
1962 gva_t cs_addr;
1963 gva_t eip_addr;
1964 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001965
1966 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001967 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001968 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001969 if (rc != X86EMUL_CONTINUE)
1970 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001971
1972 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1973
Avi Kivity9dac77f2011-06-01 15:34:25 +03001974 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001975 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001976 if (rc != X86EMUL_CONTINUE)
1977 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001978
Avi Kivity9dac77f2011-06-01 15:34:25 +03001979 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001980 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001981 if (rc != X86EMUL_CONTINUE)
1982 return rc;
1983
Avi Kivity4bff1e862011-04-20 13:37:53 +03001984 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001985
1986 eip_addr = dt.address + (irq << 2);
1987 cs_addr = dt.address + (irq << 2) + 2;
1988
Avi Kivity0f65dd72011-04-20 13:37:53 +03001989 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001990 if (rc != X86EMUL_CONTINUE)
1991 return rc;
1992
Avi Kivity0f65dd72011-04-20 13:37:53 +03001993 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001994 if (rc != X86EMUL_CONTINUE)
1995 return rc;
1996
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001997 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001998 if (rc != X86EMUL_CONTINUE)
1999 return rc;
2000
Avi Kivity9dac77f2011-06-01 15:34:25 +03002001 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002002
2003 return rc;
2004}
2005
Avi Kivitydd856ef2012-08-27 23:46:17 +03002006int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
2007{
2008 int rc;
2009
2010 invalidate_registers(ctxt);
2011 rc = __emulate_int_real(ctxt, irq);
2012 if (rc == X86EMUL_CONTINUE)
2013 writeback_registers(ctxt);
2014 return rc;
2015}
2016
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002017static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002018{
2019 switch(ctxt->mode) {
2020 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03002021 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03002022 case X86EMUL_MODE_VM86:
2023 case X86EMUL_MODE_PROT16:
2024 case X86EMUL_MODE_PROT32:
2025 case X86EMUL_MODE_PROT64:
2026 default:
2027 /* Protected mode interrupts unimplemented yet */
2028 return X86EMUL_UNHANDLEABLE;
2029 }
2030}
2031
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002032static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002033{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002034 int rc = X86EMUL_CONTINUE;
2035 unsigned long temp_eip = 0;
2036 unsigned long temp_eflags = 0;
2037 unsigned long cs = 0;
2038 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
2039 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
2040 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
2041 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
2042
2043 /* TODO: Add stack limit check */
2044
Avi Kivity9dac77f2011-06-01 15:34:25 +03002045 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002046
2047 if (rc != X86EMUL_CONTINUE)
2048 return rc;
2049
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002050 if (temp_eip & ~0xffff)
2051 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002052
Avi Kivity9dac77f2011-06-01 15:34:25 +03002053 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002054
2055 if (rc != X86EMUL_CONTINUE)
2056 return rc;
2057
Avi Kivity9dac77f2011-06-01 15:34:25 +03002058 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002059
2060 if (rc != X86EMUL_CONTINUE)
2061 return rc;
2062
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002063 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002064
2065 if (rc != X86EMUL_CONTINUE)
2066 return rc;
2067
Avi Kivity9dac77f2011-06-01 15:34:25 +03002068 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002069
2070
Avi Kivity9dac77f2011-06-01 15:34:25 +03002071 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002072 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002073 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002074 ctxt->eflags &= ~0xffff;
2075 ctxt->eflags |= temp_eflags;
2076 }
2077
2078 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2079 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2080
2081 return rc;
2082}
2083
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002084static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002085{
2086 switch(ctxt->mode) {
2087 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002088 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002089 case X86EMUL_MODE_VM86:
2090 case X86EMUL_MODE_PROT16:
2091 case X86EMUL_MODE_PROT32:
2092 case X86EMUL_MODE_PROT64:
2093 default:
2094 /* iret from protected mode unimplemented yet */
2095 return X86EMUL_UNHANDLEABLE;
2096 }
2097}
2098
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002099static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2100{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002101 int rc;
2102 unsigned short sel;
2103
Avi Kivity9dac77f2011-06-01 15:34:25 +03002104 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002105
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002106 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002107 if (rc != X86EMUL_CONTINUE)
2108 return rc;
2109
Avi Kivity9dac77f2011-06-01 15:34:25 +03002110 ctxt->_eip = 0;
2111 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002112 return X86EMUL_CONTINUE;
2113}
2114
Avi Kivity3329ece2011-09-13 10:45:39 +03002115static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2116{
2117 u8 ex = 0;
2118
2119 emulate_1op_rax_rdx(ctxt, "mul", ex);
2120 return X86EMUL_CONTINUE;
2121}
2122
2123static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2124{
2125 u8 ex = 0;
2126
2127 emulate_1op_rax_rdx(ctxt, "imul", ex);
2128 return X86EMUL_CONTINUE;
2129}
2130
2131static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002132{
Avi Kivity34d1f492010-08-26 11:59:01 +03002133 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002134
Avi Kivity3329ece2011-09-13 10:45:39 +03002135 emulate_1op_rax_rdx(ctxt, "div", de);
2136 if (de)
2137 return emulate_de(ctxt);
2138 return X86EMUL_CONTINUE;
2139}
2140
2141static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2142{
2143 u8 de = 0;
2144
2145 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002146 if (de)
2147 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002148 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002149}
2150
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002151static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002152{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002153 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002154
Avi Kivity9dac77f2011-06-01 15:34:25 +03002155 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002156 case 2: /* call near abs */ {
2157 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002158 old_eip = ctxt->_eip;
2159 ctxt->_eip = ctxt->src.val;
2160 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002161 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002162 break;
2163 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002164 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002165 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002166 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002167 case 5: /* jmp far */
2168 rc = em_jmp_far(ctxt);
2169 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002170 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002171 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002172 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002173 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002174 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002175}
2176
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002177static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002178{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002179 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002180
Avi Kivitydd856ef2012-08-27 23:46:17 +03002181 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2182 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2183 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2184 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002185 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002186 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002187 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2188 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002189
Laurent Vivier05f086f2007-09-24 11:10:55 +02002190 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002191 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002192 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002193}
2194
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002195static int em_ret(struct x86_emulate_ctxt *ctxt)
2196{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002197 ctxt->dst.type = OP_REG;
2198 ctxt->dst.addr.reg = &ctxt->_eip;
2199 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002200 return em_pop(ctxt);
2201}
2202
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002203static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002204{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002205 int rc;
2206 unsigned long cs;
2207
Avi Kivity9dac77f2011-06-01 15:34:25 +03002208 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002209 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002210 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002211 if (ctxt->op_bytes == 4)
2212 ctxt->_eip = (u32)ctxt->_eip;
2213 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002214 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002215 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002216 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002217 return rc;
2218}
2219
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002220static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2221{
2222 /* Save real source value, then compare EAX against destination. */
2223 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002224 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Avi Kivity158de572013-01-19 19:51:57 +02002225 fastop(ctxt, em_cmp);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002226
2227 if (ctxt->eflags & EFLG_ZF) {
2228 /* Success: write back to memory. */
2229 ctxt->dst.val = ctxt->src.orig_val;
2230 } else {
2231 /* Failure: write the value we saw to EAX. */
2232 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002233 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002234 }
2235 return X86EMUL_CONTINUE;
2236}
2237
Avi Kivityd4b43252011-09-13 10:45:50 +03002238static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002239{
Avi Kivityd4b43252011-09-13 10:45:50 +03002240 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002241 unsigned short sel;
2242 int rc;
2243
Avi Kivity9dac77f2011-06-01 15:34:25 +03002244 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002245
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002246 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002247 if (rc != X86EMUL_CONTINUE)
2248 return rc;
2249
Avi Kivity9dac77f2011-06-01 15:34:25 +03002250 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002251 return rc;
2252}
2253
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002254static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002255setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002256 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002257{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002258 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002259 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002260 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002261 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002262 cs->type = 0x0b; /* Read, Execute, Accessed */
2263 cs->s = 1;
2264 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002265 cs->p = 1;
2266 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002267 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002268
Gleb Natapov79168fd2010-04-28 19:15:30 +03002269 set_desc_base(ss, 0); /* flat segment */
2270 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002271 ss->g = 1; /* 4kb granularity */
2272 ss->s = 1;
2273 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002274 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002275 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002276 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002277 ss->l = 0;
2278 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002279}
2280
Avi Kivity1a18a692012-02-01 12:23:21 +02002281static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2282{
2283 u32 eax, ebx, ecx, edx;
2284
2285 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002286 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2287 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002288 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2289 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2290}
2291
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002292static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2293{
Mathias Krause0225fb52012-08-30 01:30:16 +02002294 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002295 u32 eax, ebx, ecx, edx;
2296
2297 /*
2298 * syscall should always be enabled in longmode - so only become
2299 * vendor specific (cpuid) if other modes are active...
2300 */
2301 if (ctxt->mode == X86EMUL_MODE_PROT64)
2302 return true;
2303
2304 eax = 0x00000000;
2305 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002306 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2307 /*
2308 * Intel ("GenuineIntel")
2309 * remark: Intel CPUs only support "syscall" in 64bit
2310 * longmode. Also an 64bit guest with a
2311 * 32bit compat-app running will #UD !! While this
2312 * behaviour can be fixed (by emulating) into AMD
2313 * response - CPUs of AMD can't behave like Intel.
2314 */
2315 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2316 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2317 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2318 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002319
Avi Kivity0017f932012-06-07 14:10:16 +03002320 /* AMD ("AuthenticAMD") */
2321 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2322 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2323 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2324 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002325
Avi Kivity0017f932012-06-07 14:10:16 +03002326 /* AMD ("AMDisbetter!") */
2327 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2328 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2329 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2330 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002331
2332 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2333 return false;
2334}
2335
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002336static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002337{
Mathias Krause0225fb52012-08-30 01:30:16 +02002338 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002339 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002340 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002341 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002342 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002343
2344 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002345 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002346 ctxt->mode == X86EMUL_MODE_VM86)
2347 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002348
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002349 if (!(em_syscall_is_enabled(ctxt)))
2350 return emulate_ud(ctxt);
2351
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002352 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002353 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002354
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002355 if (!(efer & EFER_SCE))
2356 return emulate_ud(ctxt);
2357
Avi Kivity717746e2011-04-20 13:37:53 +03002358 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002359 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002360 cs_sel = (u16)(msr_data & 0xfffc);
2361 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002362
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002363 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002364 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002365 cs.l = 1;
2366 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002367 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2368 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002369
Avi Kivitydd856ef2012-08-27 23:46:17 +03002370 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002371 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002372#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002373 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002374
Avi Kivity717746e2011-04-20 13:37:53 +03002375 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002376 ctxt->mode == X86EMUL_MODE_PROT64 ?
2377 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002378 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002379
Avi Kivity717746e2011-04-20 13:37:53 +03002380 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002381 ctxt->eflags &= ~(msr_data | EFLG_RF);
2382#endif
2383 } else {
2384 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002385 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002386 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002387
2388 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2389 }
2390
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002391 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002392}
2393
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002394static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002395{
Mathias Krause0225fb52012-08-30 01:30:16 +02002396 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002397 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002398 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002399 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002400 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002401
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002402 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002403 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002404 if (ctxt->mode == X86EMUL_MODE_REAL)
2405 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002406
Avi Kivity1a18a692012-02-01 12:23:21 +02002407 /*
2408 * Not recognized on AMD in compat mode (but is recognized in legacy
2409 * mode).
2410 */
2411 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2412 && !vendor_intel(ctxt))
2413 return emulate_ud(ctxt);
2414
Andre Przywara8c604352009-06-18 12:56:01 +02002415 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2416 * Therefore, we inject an #UD.
2417 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002418 if (ctxt->mode == X86EMUL_MODE_PROT64)
2419 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002420
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002421 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002422
Avi Kivity717746e2011-04-20 13:37:53 +03002423 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002424 switch (ctxt->mode) {
2425 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002426 if ((msr_data & 0xfffc) == 0x0)
2427 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002428 break;
2429 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002430 if (msr_data == 0x0)
2431 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002432 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002433 default:
2434 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002435 }
2436
2437 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002438 cs_sel = (u16)msr_data;
2439 cs_sel &= ~SELECTOR_RPL_MASK;
2440 ss_sel = cs_sel + 8;
2441 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002442 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002443 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002444 cs.l = 1;
2445 }
2446
Avi Kivity1aa36612011-04-27 13:20:30 +03002447 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2448 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002449
Avi Kivity717746e2011-04-20 13:37:53 +03002450 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002451 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002452
Avi Kivity717746e2011-04-20 13:37:53 +03002453 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002454 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002455
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002456 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002457}
2458
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002459static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002460{
Mathias Krause0225fb52012-08-30 01:30:16 +02002461 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002462 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002463 u64 msr_data;
2464 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002465 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002466
Gleb Natapova0044752010-02-10 14:21:31 +02002467 /* inject #GP if in real mode or Virtual 8086 mode */
2468 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002469 ctxt->mode == X86EMUL_MODE_VM86)
2470 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002471
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002472 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002473
Avi Kivity9dac77f2011-06-01 15:34:25 +03002474 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002475 usermode = X86EMUL_MODE_PROT64;
2476 else
2477 usermode = X86EMUL_MODE_PROT32;
2478
2479 cs.dpl = 3;
2480 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002481 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002482 switch (usermode) {
2483 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002484 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002485 if ((msr_data & 0xfffc) == 0x0)
2486 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002487 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002488 break;
2489 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002490 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002491 if (msr_data == 0x0)
2492 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002493 ss_sel = cs_sel + 8;
2494 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002495 cs.l = 1;
2496 break;
2497 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002498 cs_sel |= SELECTOR_RPL_MASK;
2499 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002500
Avi Kivity1aa36612011-04-27 13:20:30 +03002501 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2502 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002503
Avi Kivitydd856ef2012-08-27 23:46:17 +03002504 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2505 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002506
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002507 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002508}
2509
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002510static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002511{
2512 int iopl;
2513 if (ctxt->mode == X86EMUL_MODE_REAL)
2514 return false;
2515 if (ctxt->mode == X86EMUL_MODE_VM86)
2516 return true;
2517 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002518 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002519}
2520
2521static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002522 u16 port, u16 len)
2523{
Mathias Krause0225fb52012-08-30 01:30:16 +02002524 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002525 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002526 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002527 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002528 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002529 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002530 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002531
Avi Kivity1aa36612011-04-27 13:20:30 +03002532 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002533 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002534 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002535 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002536 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002537 base = get_desc_base(&tr_seg);
2538#ifdef CONFIG_X86_64
2539 base |= ((u64)base3) << 32;
2540#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002541 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002542 if (r != X86EMUL_CONTINUE)
2543 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002544 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002545 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002546 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002547 if (r != X86EMUL_CONTINUE)
2548 return false;
2549 if ((perm >> bit_idx) & mask)
2550 return false;
2551 return true;
2552}
2553
2554static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002555 u16 port, u16 len)
2556{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002557 if (ctxt->perm_ok)
2558 return true;
2559
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002560 if (emulator_bad_iopl(ctxt))
2561 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002562 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002563
2564 ctxt->perm_ok = true;
2565
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002566 return true;
2567}
2568
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002570 struct tss_segment_16 *tss)
2571{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002572 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002573 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002574 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2575 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2576 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2577 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2578 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2579 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2580 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2581 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002582
Avi Kivity1aa36612011-04-27 13:20:30 +03002583 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2584 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2585 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2586 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2587 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002588}
2589
2590static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 struct tss_segment_16 *tss)
2592{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 int ret;
2594
Avi Kivity9dac77f2011-06-01 15:34:25 +03002595 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002596 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002597 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2598 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2599 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2600 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2601 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2602 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2603 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2604 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605
2606 /*
2607 * SDM says that segment selectors are loaded before segment
2608 * descriptors
2609 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002610 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2611 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2612 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2613 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2614 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615
2616 /*
Guo Chaofc058682012-06-28 15:19:51 +08002617 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002618 * it is handled in a context of new task
2619 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002620 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 if (ret != X86EMUL_CONTINUE)
2622 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002623 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002624 if (ret != X86EMUL_CONTINUE)
2625 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002626 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627 if (ret != X86EMUL_CONTINUE)
2628 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002629 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 if (ret != X86EMUL_CONTINUE)
2631 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002632 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002633 if (ret != X86EMUL_CONTINUE)
2634 return ret;
2635
2636 return X86EMUL_CONTINUE;
2637}
2638
2639static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002640 u16 tss_selector, u16 old_tss_sel,
2641 ulong old_tss_base, struct desc_struct *new_desc)
2642{
Mathias Krause0225fb52012-08-30 01:30:16 +02002643 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644 struct tss_segment_16 tss_seg;
2645 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002646 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647
Avi Kivity0f65dd72011-04-20 13:37:53 +03002648 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002649 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002650 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002652 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002653
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002654 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002655
Avi Kivity0f65dd72011-04-20 13:37:53 +03002656 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002657 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002658 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002660 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002661
Avi Kivity0f65dd72011-04-20 13:37:53 +03002662 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002663 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002664 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002666 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667
2668 if (old_tss_sel != 0xffff) {
2669 tss_seg.prev_task_link = old_tss_sel;
2670
Avi Kivity0f65dd72011-04-20 13:37:53 +03002671 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002672 &tss_seg.prev_task_link,
2673 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002674 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002675 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002676 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002677 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678 }
2679
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002680 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002681}
2682
2683static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002684 struct tss_segment_32 *tss)
2685{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002686 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002687 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002688 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002689 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2690 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2691 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2692 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2693 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2694 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2695 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2696 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002697
Avi Kivity1aa36612011-04-27 13:20:30 +03002698 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2699 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2700 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2701 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2702 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2703 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2704 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002705}
2706
2707static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002708 struct tss_segment_32 *tss)
2709{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002710 int ret;
2711
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002712 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002713 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002714 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002715 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002716
2717 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002718 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2719 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2720 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2721 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2722 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2723 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2724 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2725 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002726
2727 /*
2728 * SDM says that segment selectors are loaded before segment
2729 * descriptors
2730 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002731 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2732 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2733 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2734 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2735 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2736 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2737 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738
2739 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002740 * If we're switching between Protected Mode and VM86, we need to make
2741 * sure to update the mode before loading the segment descriptors so
2742 * that the selectors are interpreted correctly.
2743 *
2744 * Need to get rflags to the vcpu struct immediately because it
2745 * influences the CPL which is checked at least when loading the segment
2746 * descriptors and when pushing an error code to the new kernel stack.
2747 *
2748 * TODO Introduce a separate ctxt->ops->set_cpl callback
2749 */
2750 if (ctxt->eflags & X86_EFLAGS_VM)
2751 ctxt->mode = X86EMUL_MODE_VM86;
2752 else
2753 ctxt->mode = X86EMUL_MODE_PROT32;
2754
2755 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2756
2757 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758 * Now load segment descriptors. If fault happenes at this stage
2759 * it is handled in a context of new task
2760 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002761 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762 if (ret != X86EMUL_CONTINUE)
2763 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002764 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002765 if (ret != X86EMUL_CONTINUE)
2766 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002767 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 if (ret != X86EMUL_CONTINUE)
2769 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002770 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002771 if (ret != X86EMUL_CONTINUE)
2772 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002773 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774 if (ret != X86EMUL_CONTINUE)
2775 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002776 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 if (ret != X86EMUL_CONTINUE)
2778 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002779 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002780 if (ret != X86EMUL_CONTINUE)
2781 return ret;
2782
2783 return X86EMUL_CONTINUE;
2784}
2785
2786static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787 u16 tss_selector, u16 old_tss_sel,
2788 ulong old_tss_base, struct desc_struct *new_desc)
2789{
Mathias Krause0225fb52012-08-30 01:30:16 +02002790 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002791 struct tss_segment_32 tss_seg;
2792 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002793 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794
Avi Kivity0f65dd72011-04-20 13:37:53 +03002795 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002796 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002797 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002799 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002801 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002802
Avi Kivity0f65dd72011-04-20 13:37:53 +03002803 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002804 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002805 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002806 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002807 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002808
Avi Kivity0f65dd72011-04-20 13:37:53 +03002809 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002810 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002811 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002813 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002814
2815 if (old_tss_sel != 0xffff) {
2816 tss_seg.prev_task_link = old_tss_sel;
2817
Avi Kivity0f65dd72011-04-20 13:37:53 +03002818 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002819 &tss_seg.prev_task_link,
2820 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002821 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002822 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002823 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002824 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002825 }
2826
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002827 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002828}
2829
2830static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002831 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002832 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002833{
Mathias Krause0225fb52012-08-30 01:30:16 +02002834 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002835 struct desc_struct curr_tss_desc, next_tss_desc;
2836 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002837 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002838 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002839 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002840 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002841 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002842
2843 /* FIXME: old_tss_base == ~0 ? */
2844
Avi Kivitye9194642012-06-13 16:29:39 +03002845 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002846 if (ret != X86EMUL_CONTINUE)
2847 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002848 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002849 if (ret != X86EMUL_CONTINUE)
2850 return ret;
2851
2852 /* FIXME: check that next_tss_desc is tss */
2853
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002854 /*
2855 * Check privileges. The three cases are task switch caused by...
2856 *
2857 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2858 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002859 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002860 */
2861 if (reason == TASK_SWITCH_GATE) {
2862 if (idt_index != -1) {
2863 /* Software interrupts */
2864 struct desc_struct task_gate_desc;
2865 int dpl;
2866
2867 ret = read_interrupt_descriptor(ctxt, idt_index,
2868 &task_gate_desc);
2869 if (ret != X86EMUL_CONTINUE)
2870 return ret;
2871
2872 dpl = task_gate_desc.dpl;
2873 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2874 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2875 }
2876 } else if (reason != TASK_SWITCH_IRET) {
2877 int dpl = next_tss_desc.dpl;
2878 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2879 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002880 }
2881
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002882
Gleb Natapovceffb452010-03-18 15:20:19 +02002883 desc_limit = desc_limit_scaled(&next_tss_desc);
2884 if (!next_tss_desc.p ||
2885 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2886 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002887 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002888 return X86EMUL_PROPAGATE_FAULT;
2889 }
2890
2891 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2892 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002893 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002894 }
2895
2896 if (reason == TASK_SWITCH_IRET)
2897 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2898
2899 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002900 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002901 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2902 old_tss_sel = 0xffff;
2903
2904 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002905 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002906 old_tss_base, &next_tss_desc);
2907 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002908 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002909 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002910 if (ret != X86EMUL_CONTINUE)
2911 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002912
2913 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2914 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2915
2916 if (reason != TASK_SWITCH_IRET) {
2917 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002918 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002919 }
2920
Avi Kivity717746e2011-04-20 13:37:53 +03002921 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002922 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002923
Jan Kiszkae269fb22010-04-14 15:51:09 +02002924 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002925 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2926 ctxt->lock_prefix = 0;
2927 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002928 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002929 }
2930
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002931 return ret;
2932}
2933
2934int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002935 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002936 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002937{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002938 int rc;
2939
Avi Kivitydd856ef2012-08-27 23:46:17 +03002940 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002941 ctxt->_eip = ctxt->eip;
2942 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002943
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002944 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002945 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002946
Avi Kivitydd856ef2012-08-27 23:46:17 +03002947 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002948 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002949 writeback_registers(ctxt);
2950 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002951
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002952 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002953}
2954
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002955static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2956 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002957{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002958 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002959
Avi Kivitydd856ef2012-08-27 23:46:17 +03002960 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2961 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002962}
2963
Avi Kivity7af04fc2010-08-18 14:16:35 +03002964static int em_das(struct x86_emulate_ctxt *ctxt)
2965{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002966 u8 al, old_al;
2967 bool af, cf, old_cf;
2968
2969 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002970 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002971
2972 old_al = al;
2973 old_cf = cf;
2974 cf = false;
2975 af = ctxt->eflags & X86_EFLAGS_AF;
2976 if ((al & 0x0f) > 9 || af) {
2977 al -= 6;
2978 cf = old_cf | (al >= 250);
2979 af = true;
2980 } else {
2981 af = false;
2982 }
2983 if (old_al > 0x99 || old_cf) {
2984 al -= 0x60;
2985 cf = true;
2986 }
2987
Avi Kivity9dac77f2011-06-01 15:34:25 +03002988 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002989 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002990 ctxt->src.type = OP_IMM;
2991 ctxt->src.val = 0;
2992 ctxt->src.bytes = 1;
Avi Kivity158de572013-01-19 19:51:57 +02002993 fastop(ctxt, em_or);
Avi Kivity7af04fc2010-08-18 14:16:35 +03002994 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2995 if (cf)
2996 ctxt->eflags |= X86_EFLAGS_CF;
2997 if (af)
2998 ctxt->eflags |= X86_EFLAGS_AF;
2999 return X86EMUL_CONTINUE;
3000}
3001
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003002static int em_aam(struct x86_emulate_ctxt *ctxt)
3003{
3004 u8 al, ah;
3005
3006 if (ctxt->src.val == 0)
3007 return emulate_de(ctxt);
3008
3009 al = ctxt->dst.val & 0xff;
3010 ah = al / ctxt->src.val;
3011 al %= ctxt->src.val;
3012
3013 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
3014
3015 /* Set PF, ZF, SF */
3016 ctxt->src.type = OP_IMM;
3017 ctxt->src.val = 0;
3018 ctxt->src.bytes = 1;
3019 fastop(ctxt, em_or);
3020
3021 return X86EMUL_CONTINUE;
3022}
3023
Gleb Natapov7f662272012-12-10 11:42:30 +02003024static int em_aad(struct x86_emulate_ctxt *ctxt)
3025{
3026 u8 al = ctxt->dst.val & 0xff;
3027 u8 ah = (ctxt->dst.val >> 8) & 0xff;
3028
3029 al = (al + (ah * ctxt->src.val)) & 0xff;
3030
3031 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
3032
Gleb Natapovf583c292013-02-13 17:50:39 +02003033 /* Set PF, ZF, SF */
3034 ctxt->src.type = OP_IMM;
3035 ctxt->src.val = 0;
3036 ctxt->src.bytes = 1;
3037 fastop(ctxt, em_or);
Gleb Natapov7f662272012-12-10 11:42:30 +02003038
3039 return X86EMUL_CONTINUE;
3040}
3041
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003042static int em_call(struct x86_emulate_ctxt *ctxt)
3043{
3044 long rel = ctxt->src.val;
3045
3046 ctxt->src.val = (unsigned long)ctxt->_eip;
3047 jmp_rel(ctxt, rel);
3048 return em_push(ctxt);
3049}
3050
Avi Kivity0ef753b2010-08-18 14:51:45 +03003051static int em_call_far(struct x86_emulate_ctxt *ctxt)
3052{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003053 u16 sel, old_cs;
3054 ulong old_eip;
3055 int rc;
3056
Avi Kivity1aa36612011-04-27 13:20:30 +03003057 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003058 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003059
Avi Kivity9dac77f2011-06-01 15:34:25 +03003060 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003061 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003062 return X86EMUL_CONTINUE;
3063
Avi Kivity9dac77f2011-06-01 15:34:25 +03003064 ctxt->_eip = 0;
3065 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003066
Avi Kivity9dac77f2011-06-01 15:34:25 +03003067 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003068 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003069 if (rc != X86EMUL_CONTINUE)
3070 return rc;
3071
Avi Kivity9dac77f2011-06-01 15:34:25 +03003072 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003073 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003074}
3075
Avi Kivity40ece7c2010-08-18 15:12:09 +03003076static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3077{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003078 int rc;
3079
Avi Kivity9dac77f2011-06-01 15:34:25 +03003080 ctxt->dst.type = OP_REG;
3081 ctxt->dst.addr.reg = &ctxt->_eip;
3082 ctxt->dst.bytes = ctxt->op_bytes;
3083 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003084 if (rc != X86EMUL_CONTINUE)
3085 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003086 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003087 return X86EMUL_CONTINUE;
3088}
3089
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003090static int em_xchg(struct x86_emulate_ctxt *ctxt)
3091{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003092 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003093 ctxt->src.val = ctxt->dst.val;
3094 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003095
3096 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003097 ctxt->dst.val = ctxt->src.orig_val;
3098 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003099 return X86EMUL_CONTINUE;
3100}
3101
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003102static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3103{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003104 ctxt->dst.val = ctxt->src2.val;
Avi Kivity4d758342013-01-19 19:51:55 +02003105 return fastop(ctxt, em_imul);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003106}
3107
Avi Kivity61429142010-08-19 15:13:00 +03003108static int em_cwd(struct x86_emulate_ctxt *ctxt)
3109{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003110 ctxt->dst.type = OP_REG;
3111 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003112 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003113 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003114
3115 return X86EMUL_CONTINUE;
3116}
3117
Avi Kivity48bb5d32010-08-18 18:54:34 +03003118static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3119{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003120 u64 tsc = 0;
3121
Avi Kivity717746e2011-04-20 13:37:53 +03003122 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003123 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3124 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003125 return X86EMUL_CONTINUE;
3126}
3127
Avi Kivity222d21a2011-11-10 14:57:30 +02003128static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3129{
3130 u64 pmc;
3131
Avi Kivitydd856ef2012-08-27 23:46:17 +03003132 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003133 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003134 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3135 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003136 return X86EMUL_CONTINUE;
3137}
3138
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003139static int em_mov(struct x86_emulate_ctxt *ctxt)
3140{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003141 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003142 return X86EMUL_CONTINUE;
3143}
3144
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003145static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3146{
3147 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3148 return emulate_gp(ctxt, 0);
3149
3150 /* Disable writeback. */
3151 ctxt->dst.type = OP_NONE;
3152 return X86EMUL_CONTINUE;
3153}
3154
3155static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3156{
3157 unsigned long val;
3158
3159 if (ctxt->mode == X86EMUL_MODE_PROT64)
3160 val = ctxt->src.val & ~0ULL;
3161 else
3162 val = ctxt->src.val & ~0U;
3163
3164 /* #UD condition is already handled. */
3165 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3166 return emulate_gp(ctxt, 0);
3167
3168 /* Disable writeback. */
3169 ctxt->dst.type = OP_NONE;
3170 return X86EMUL_CONTINUE;
3171}
3172
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003173static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3174{
3175 u64 msr_data;
3176
Avi Kivitydd856ef2012-08-27 23:46:17 +03003177 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3178 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3179 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003180 return emulate_gp(ctxt, 0);
3181
3182 return X86EMUL_CONTINUE;
3183}
3184
3185static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3186{
3187 u64 msr_data;
3188
Avi Kivitydd856ef2012-08-27 23:46:17 +03003189 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003190 return emulate_gp(ctxt, 0);
3191
Avi Kivitydd856ef2012-08-27 23:46:17 +03003192 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3193 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003194 return X86EMUL_CONTINUE;
3195}
3196
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003197static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3198{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003199 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003200 return emulate_ud(ctxt);
3201
Avi Kivity9dac77f2011-06-01 15:34:25 +03003202 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003203 return X86EMUL_CONTINUE;
3204}
3205
3206static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3207{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003209
Avi Kivity9dac77f2011-06-01 15:34:25 +03003210 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003211 return emulate_ud(ctxt);
3212
Avi Kivity9dac77f2011-06-01 15:34:25 +03003213 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003214 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3215
3216 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003217 ctxt->dst.type = OP_NONE;
3218 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003219}
3220
Avi Kivitya14e5792012-06-13 12:28:33 +03003221static int em_lldt(struct x86_emulate_ctxt *ctxt)
3222{
3223 u16 sel = ctxt->src.val;
3224
3225 /* Disable writeback. */
3226 ctxt->dst.type = OP_NONE;
3227 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3228}
3229
Avi Kivity80890002012-06-13 16:33:29 +03003230static int em_ltr(struct x86_emulate_ctxt *ctxt)
3231{
3232 u16 sel = ctxt->src.val;
3233
3234 /* Disable writeback. */
3235 ctxt->dst.type = OP_NONE;
3236 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3237}
3238
Avi Kivity38503912011-03-31 18:48:09 +02003239static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3240{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003241 int rc;
3242 ulong linear;
3243
Avi Kivity9dac77f2011-06-01 15:34:25 +03003244 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003245 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003246 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003247 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003248 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003249 return X86EMUL_CONTINUE;
3250}
3251
Avi Kivity2d04a052011-04-20 15:32:49 +03003252static int em_clts(struct x86_emulate_ctxt *ctxt)
3253{
3254 ulong cr0;
3255
3256 cr0 = ctxt->ops->get_cr(ctxt, 0);
3257 cr0 &= ~X86_CR0_TS;
3258 ctxt->ops->set_cr(ctxt, 0, cr0);
3259 return X86EMUL_CONTINUE;
3260}
3261
Avi Kivity26d05cc2011-04-21 12:07:59 +03003262static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3263{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003264 int rc;
3265
Avi Kivity9dac77f2011-06-01 15:34:25 +03003266 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003267 return X86EMUL_UNHANDLEABLE;
3268
3269 rc = ctxt->ops->fix_hypercall(ctxt);
3270 if (rc != X86EMUL_CONTINUE)
3271 return rc;
3272
3273 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003274 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003275 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003276 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003277 return X86EMUL_CONTINUE;
3278}
3279
Avi Kivity96051572012-06-10 17:21:18 +03003280static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3281 void (*get)(struct x86_emulate_ctxt *ctxt,
3282 struct desc_ptr *ptr))
3283{
3284 struct desc_ptr desc_ptr;
3285
3286 if (ctxt->mode == X86EMUL_MODE_PROT64)
3287 ctxt->op_bytes = 8;
3288 get(ctxt, &desc_ptr);
3289 if (ctxt->op_bytes == 2) {
3290 ctxt->op_bytes = 4;
3291 desc_ptr.address &= 0x00ffffff;
3292 }
3293 /* Disable writeback. */
3294 ctxt->dst.type = OP_NONE;
3295 return segmented_write(ctxt, ctxt->dst.addr.mem,
3296 &desc_ptr, 2 + ctxt->op_bytes);
3297}
3298
3299static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3300{
3301 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3302}
3303
3304static int em_sidt(struct x86_emulate_ctxt *ctxt)
3305{
3306 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3307}
3308
Avi Kivity26d05cc2011-04-21 12:07:59 +03003309static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3310{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003311 struct desc_ptr desc_ptr;
3312 int rc;
3313
Avi Kivity510425f2012-06-07 17:04:36 +03003314 if (ctxt->mode == X86EMUL_MODE_PROT64)
3315 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003316 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003317 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003318 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003319 if (rc != X86EMUL_CONTINUE)
3320 return rc;
3321 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3322 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003323 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003324 return X86EMUL_CONTINUE;
3325}
3326
Avi Kivity5ef39c72011-04-21 12:21:50 +03003327static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003328{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003329 int rc;
3330
Avi Kivity5ef39c72011-04-21 12:21:50 +03003331 rc = ctxt->ops->fix_hypercall(ctxt);
3332
Avi Kivity26d05cc2011-04-21 12:07:59 +03003333 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003334 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003335 return rc;
3336}
3337
3338static int em_lidt(struct x86_emulate_ctxt *ctxt)
3339{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003340 struct desc_ptr desc_ptr;
3341 int rc;
3342
Avi Kivity510425f2012-06-07 17:04:36 +03003343 if (ctxt->mode == X86EMUL_MODE_PROT64)
3344 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003345 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003346 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003347 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003348 if (rc != X86EMUL_CONTINUE)
3349 return rc;
3350 ctxt->ops->set_idt(ctxt, &desc_ptr);
3351 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003352 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003353 return X86EMUL_CONTINUE;
3354}
3355
3356static int em_smsw(struct x86_emulate_ctxt *ctxt)
3357{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003358 ctxt->dst.bytes = 2;
3359 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003360 return X86EMUL_CONTINUE;
3361}
3362
3363static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3364{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003365 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003366 | (ctxt->src.val & 0x0f));
3367 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003368 return X86EMUL_CONTINUE;
3369}
3370
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003371static int em_loop(struct x86_emulate_ctxt *ctxt)
3372{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003373 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3374 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003375 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3376 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003377
3378 return X86EMUL_CONTINUE;
3379}
3380
3381static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3382{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003383 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003384 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003385
3386 return X86EMUL_CONTINUE;
3387}
3388
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003389static int em_in(struct x86_emulate_ctxt *ctxt)
3390{
3391 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3392 &ctxt->dst.val))
3393 return X86EMUL_IO_NEEDED;
3394
3395 return X86EMUL_CONTINUE;
3396}
3397
3398static int em_out(struct x86_emulate_ctxt *ctxt)
3399{
3400 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3401 &ctxt->src.val, 1);
3402 /* Disable writeback. */
3403 ctxt->dst.type = OP_NONE;
3404 return X86EMUL_CONTINUE;
3405}
3406
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003407static int em_cli(struct x86_emulate_ctxt *ctxt)
3408{
3409 if (emulator_bad_iopl(ctxt))
3410 return emulate_gp(ctxt, 0);
3411
3412 ctxt->eflags &= ~X86_EFLAGS_IF;
3413 return X86EMUL_CONTINUE;
3414}
3415
3416static int em_sti(struct x86_emulate_ctxt *ctxt)
3417{
3418 if (emulator_bad_iopl(ctxt))
3419 return emulate_gp(ctxt, 0);
3420
3421 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3422 ctxt->eflags |= X86_EFLAGS_IF;
3423 return X86EMUL_CONTINUE;
3424}
3425
Avi Kivity6d6eede2012-06-07 14:11:36 +03003426static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3427{
3428 u32 eax, ebx, ecx, edx;
3429
Avi Kivitydd856ef2012-08-27 23:46:17 +03003430 eax = reg_read(ctxt, VCPU_REGS_RAX);
3431 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003432 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003433 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3434 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3435 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3436 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003437 return X86EMUL_CONTINUE;
3438}
3439
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003440static int em_lahf(struct x86_emulate_ctxt *ctxt)
3441{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003442 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3443 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003444 return X86EMUL_CONTINUE;
3445}
3446
Avi Kivity92998362012-06-13 12:25:06 +03003447static int em_bswap(struct x86_emulate_ctxt *ctxt)
3448{
3449 switch (ctxt->op_bytes) {
3450#ifdef CONFIG_X86_64
3451 case 8:
3452 asm("bswap %0" : "+r"(ctxt->dst.val));
3453 break;
3454#endif
3455 default:
3456 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3457 break;
3458 }
3459 return X86EMUL_CONTINUE;
3460}
3461
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003462static bool valid_cr(int nr)
3463{
3464 switch (nr) {
3465 case 0:
3466 case 2 ... 4:
3467 case 8:
3468 return true;
3469 default:
3470 return false;
3471 }
3472}
3473
3474static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3475{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003476 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003477 return emulate_ud(ctxt);
3478
3479 return X86EMUL_CONTINUE;
3480}
3481
3482static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3483{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003484 u64 new_val = ctxt->src.val64;
3485 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003486 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003487
3488 static u64 cr_reserved_bits[] = {
3489 0xffffffff00000000ULL,
3490 0, 0, 0, /* CR3 checked later */
3491 CR4_RESERVED_BITS,
3492 0, 0, 0,
3493 CR8_RESERVED_BITS,
3494 };
3495
3496 if (!valid_cr(cr))
3497 return emulate_ud(ctxt);
3498
3499 if (new_val & cr_reserved_bits[cr])
3500 return emulate_gp(ctxt, 0);
3501
3502 switch (cr) {
3503 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003504 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003505 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3506 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3507 return emulate_gp(ctxt, 0);
3508
Avi Kivity717746e2011-04-20 13:37:53 +03003509 cr4 = ctxt->ops->get_cr(ctxt, 4);
3510 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003511
3512 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3513 !(cr4 & X86_CR4_PAE))
3514 return emulate_gp(ctxt, 0);
3515
3516 break;
3517 }
3518 case 3: {
3519 u64 rsvd = 0;
3520
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003521 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3522 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003523 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003524 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003525 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003526 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003527 rsvd = CR3_NONPAE_RESERVED_BITS;
3528
3529 if (new_val & rsvd)
3530 return emulate_gp(ctxt, 0);
3531
3532 break;
3533 }
3534 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003535 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003536
3537 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3538 return emulate_gp(ctxt, 0);
3539
3540 break;
3541 }
3542 }
3543
3544 return X86EMUL_CONTINUE;
3545}
3546
Joerg Roedel3b88e412011-04-04 12:39:29 +02003547static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3548{
3549 unsigned long dr7;
3550
Avi Kivity717746e2011-04-20 13:37:53 +03003551 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003552
3553 /* Check if DR7.Global_Enable is set */
3554 return dr7 & (1 << 13);
3555}
3556
3557static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3558{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003559 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003560 u64 cr4;
3561
3562 if (dr > 7)
3563 return emulate_ud(ctxt);
3564
Avi Kivity717746e2011-04-20 13:37:53 +03003565 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003566 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3567 return emulate_ud(ctxt);
3568
3569 if (check_dr7_gd(ctxt))
3570 return emulate_db(ctxt);
3571
3572 return X86EMUL_CONTINUE;
3573}
3574
3575static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3576{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003577 u64 new_val = ctxt->src.val64;
3578 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003579
3580 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3581 return emulate_gp(ctxt, 0);
3582
3583 return check_dr_read(ctxt);
3584}
3585
Joerg Roedel01de8b02011-04-04 12:39:31 +02003586static int check_svme(struct x86_emulate_ctxt *ctxt)
3587{
3588 u64 efer;
3589
Avi Kivity717746e2011-04-20 13:37:53 +03003590 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003591
3592 if (!(efer & EFER_SVME))
3593 return emulate_ud(ctxt);
3594
3595 return X86EMUL_CONTINUE;
3596}
3597
3598static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3599{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003600 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003601
3602 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003603 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003604 return emulate_gp(ctxt, 0);
3605
3606 return check_svme(ctxt);
3607}
3608
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003609static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3610{
Avi Kivity717746e2011-04-20 13:37:53 +03003611 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003612
Avi Kivity717746e2011-04-20 13:37:53 +03003613 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003614 return emulate_ud(ctxt);
3615
3616 return X86EMUL_CONTINUE;
3617}
3618
Joerg Roedel80612522011-04-04 12:39:33 +02003619static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3620{
Avi Kivity717746e2011-04-20 13:37:53 +03003621 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003622 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003623
Avi Kivity717746e2011-04-20 13:37:53 +03003624 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003625 (rcx > 3))
3626 return emulate_gp(ctxt, 0);
3627
3628 return X86EMUL_CONTINUE;
3629}
3630
Joerg Roedelf6511932011-04-04 12:39:35 +02003631static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3632{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003633 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3634 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003635 return emulate_gp(ctxt, 0);
3636
3637 return X86EMUL_CONTINUE;
3638}
3639
3640static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3641{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003642 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3643 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003644 return emulate_gp(ctxt, 0);
3645
3646 return X86EMUL_CONTINUE;
3647}
3648
Avi Kivity73fba5f2010-07-29 15:11:53 +03003649#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003650#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003651#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3652 .check_perm = (_p) }
Gleb Natapov0b789ee2013-04-11 11:59:55 +03003653#define N D(NotImpl)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003654#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003655#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3656#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003657#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003658#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003659#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003660#define II(_f, _e, _i) \
3661 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003662#define IIP(_f, _e, _i, _p) \
3663 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3664 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003665#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003666
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003667#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003668#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003669#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003670#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003671#define I2bvIP(_f, _e, _i, _p) \
3672 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003673
Avi Kivityfb864fb2013-01-04 16:18:54 +02003674#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3675 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3676 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003677
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003678static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003679 DI(SrcNone | Priv, monitor),
3680 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003681 N, N, N, N, N, N,
3682};
3683
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003684static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003685 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3686 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3687 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3688 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3689 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3690 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3691 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3692 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003693};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003694
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003695static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003696 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003697 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003698 N, N, N, N, N, N,
3699};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003700
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003701static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003702 F(Lock, em_add),
3703 F(Lock | PageTable, em_or),
3704 F(Lock, em_adc),
3705 F(Lock, em_sbb),
3706 F(Lock | PageTable, em_and),
3707 F(Lock, em_sub),
3708 F(Lock, em_xor),
3709 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003710};
3711
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003712static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003713 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003714};
3715
Avi Kivity007a3b52013-01-19 19:51:51 +02003716static const struct opcode group2[] = {
3717 F(DstMem | ModRM, em_rol),
3718 F(DstMem | ModRM, em_ror),
3719 F(DstMem | ModRM, em_rcl),
3720 F(DstMem | ModRM, em_rcr),
3721 F(DstMem | ModRM, em_shl),
3722 F(DstMem | ModRM, em_shr),
3723 F(DstMem | ModRM, em_shl),
3724 F(DstMem | ModRM, em_sar),
3725};
3726
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003727static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003728 F(DstMem | SrcImm | NoWrite, em_test),
3729 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003730 F(DstMem | SrcNone | Lock, em_not),
3731 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003732 I(SrcMem, em_mul_ex),
3733 I(SrcMem, em_imul_ex),
3734 I(SrcMem, em_div_ex),
3735 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003736};
3737
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003738static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003739 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3740 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003741 N, N, N, N, N, N,
3742};
3743
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003744static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003745 F(DstMem | SrcNone | Lock, em_inc),
3746 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003747 I(SrcMem | Stack, em_grp45),
3748 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3749 I(SrcMem | Stack, em_grp45),
3750 I(SrcMemFAddr | ImplicitOps, em_grp45),
Gleb Natapov188424b2013-04-11 12:32:14 +03003751 I(SrcMem | Stack, em_grp45), D(Undefined),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003752};
3753
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003754static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003755 DI(Prot, sldt),
3756 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003757 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003758 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003759 N, N, N, N,
3760};
3761
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003762static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003763 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3764 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003765 II(SrcMem | Priv, em_lgdt, lgdt),
3766 II(SrcMem | Priv, em_lidt, lidt),
3767 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3768 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3769 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003770}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003771 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003772 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003773 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003774 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3775 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3776 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003777} };
3778
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003779static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003781 F(DstMem | SrcImmByte | NoWrite, em_bt),
3782 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3783 F(DstMem | SrcImmByte | Lock, em_btr),
3784 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003785};
3786
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003787static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003788 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003789}, {
3790 N, N, N, N, N, N, N, N,
3791} };
3792
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003793static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003794 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003795 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003796};
3797
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003798static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003799 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003800};
3801
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003802static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003803 I(0, em_mov), N, N, N,
3804};
3805
Gleb Natapov045a2822012-12-20 16:57:43 +02003806static const struct escape escape_d9 = { {
3807 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3808}, {
3809 /* 0xC0 - 0xC7 */
3810 N, N, N, N, N, N, N, N,
3811 /* 0xC8 - 0xCF */
3812 N, N, N, N, N, N, N, N,
3813 /* 0xD0 - 0xC7 */
3814 N, N, N, N, N, N, N, N,
3815 /* 0xD8 - 0xDF */
3816 N, N, N, N, N, N, N, N,
3817 /* 0xE0 - 0xE7 */
3818 N, N, N, N, N, N, N, N,
3819 /* 0xE8 - 0xEF */
3820 N, N, N, N, N, N, N, N,
3821 /* 0xF0 - 0xF7 */
3822 N, N, N, N, N, N, N, N,
3823 /* 0xF8 - 0xFF */
3824 N, N, N, N, N, N, N, N,
3825} };
3826
3827static const struct escape escape_db = { {
3828 N, N, N, N, N, N, N, N,
3829}, {
3830 /* 0xC0 - 0xC7 */
3831 N, N, N, N, N, N, N, N,
3832 /* 0xC8 - 0xCF */
3833 N, N, N, N, N, N, N, N,
3834 /* 0xD0 - 0xC7 */
3835 N, N, N, N, N, N, N, N,
3836 /* 0xD8 - 0xDF */
3837 N, N, N, N, N, N, N, N,
3838 /* 0xE0 - 0xE7 */
3839 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3840 /* 0xE8 - 0xEF */
3841 N, N, N, N, N, N, N, N,
3842 /* 0xF0 - 0xF7 */
3843 N, N, N, N, N, N, N, N,
3844 /* 0xF8 - 0xFF */
3845 N, N, N, N, N, N, N, N,
3846} };
3847
3848static const struct escape escape_dd = { {
3849 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3850}, {
3851 /* 0xC0 - 0xC7 */
3852 N, N, N, N, N, N, N, N,
3853 /* 0xC8 - 0xCF */
3854 N, N, N, N, N, N, N, N,
3855 /* 0xD0 - 0xC7 */
3856 N, N, N, N, N, N, N, N,
3857 /* 0xD8 - 0xDF */
3858 N, N, N, N, N, N, N, N,
3859 /* 0xE0 - 0xE7 */
3860 N, N, N, N, N, N, N, N,
3861 /* 0xE8 - 0xEF */
3862 N, N, N, N, N, N, N, N,
3863 /* 0xF0 - 0xF7 */
3864 N, N, N, N, N, N, N, N,
3865 /* 0xF8 - 0xFF */
3866 N, N, N, N, N, N, N, N,
3867} };
3868
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003869static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003871 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003872 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3873 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003874 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003875 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003876 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3877 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003878 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003879 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003880 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3881 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003882 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003883 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003884 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3885 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003886 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003887 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003888 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003889 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003890 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003891 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003892 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003893 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003894 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003895 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003896 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003897 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003899 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003900 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003901 I(ImplicitOps | Stack | No64, em_pusha),
3902 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003903 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3904 N, N, N, N,
3905 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003906 I(SrcImm | Mov | Stack, em_push),
3907 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003908 I(SrcImmByte | Mov | Stack, em_push),
3909 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003910 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003911 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003912 /* 0x70 - 0x7F */
3913 X16(D(SrcImmByte)),
3914 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003915 G(ByteOp | DstMem | SrcImm, group1),
3916 G(DstMem | SrcImm, group1),
3917 G(ByteOp | DstMem | SrcImm | No64, group1),
3918 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003919 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003920 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003921 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003922 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003923 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003924 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003925 D(ModRM | SrcMem | NoAccess | DstReg),
3926 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3927 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003928 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003929 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003931 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003932 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003933 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003934 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003936 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003937 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003938 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003939 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003940 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003941 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003942 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3943 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003944 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003945 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003946 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003947 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003948 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003949 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003950 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003951 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003952 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003953 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3954 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003955 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003956 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003957 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3958 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003959 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003960 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003961 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003962 G(Src2One | ByteOp, group2), G(Src2One, group2),
3963 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Paolo Bonzinia035d5c62013-05-09 11:32:49 +02003964 I(DstAcc | SrcImmUByte | No64, em_aam),
Paolo Bonzini326f5782013-05-09 11:32:51 +02003965 I(DstAcc | SrcImmUByte | No64, em_aad),
3966 F(DstAcc | ByteOp | No64, em_salc),
Paolo Bonzini7fa57952013-05-09 11:32:50 +02003967 I(DstAcc | SrcXLat | ByteOp, em_mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003968 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003969 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003970 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003971 X3(I(SrcImmByte, em_loop)),
3972 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003973 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3974 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003975 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003976 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003977 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003978 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3979 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003980 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003981 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003982 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3983 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003984 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003985 D(ImplicitOps), D(ImplicitOps),
3986 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003987 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3988};
3989
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003990static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003991 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003992 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003993 N, I(ImplicitOps | VendorSpecific, em_syscall),
3994 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003995 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003996 N, D(ImplicitOps | ModRM), N, N,
3997 /* 0x10 - 0x1F */
3998 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3999 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004000 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004001 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004002 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4003 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004004 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004005 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4006 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004007 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004008 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004009 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004010 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004011 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004012 I(ImplicitOps | VendorSpecific, em_sysenter),
4013 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004014 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004015 N, N, N, N, N, N, N, N,
4016 /* 0x40 - 0x4F */
4017 X16(D(DstReg | SrcMem | ModRM | Mov)),
4018 /* 0x50 - 0x5F */
4019 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4020 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004021 N, N, N, N,
4022 N, N, N, N,
4023 N, N, N, N,
4024 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004025 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004026 N, N, N, N,
4027 N, N, N, N,
4028 N, N, N, N,
4029 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004030 /* 0x80 - 0x8F */
4031 X16(D(SrcImm)),
4032 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004033 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004034 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004035 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004036 II(ImplicitOps, em_cpuid, cpuid),
4037 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004038 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4039 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004040 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004041 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004042 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004043 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004044 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4045 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity4d758342013-01-19 19:51:55 +02004046 D(ModRM), F(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004047 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004048 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004049 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004050 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004051 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4052 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004053 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004054 /* 0xB8 - 0xBF */
4055 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004056 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004057 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4058 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004059 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004060 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004061 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004062 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004063 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004064 /* 0xC8 - 0xCF */
4065 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004066 /* 0xD0 - 0xDF */
4067 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4068 /* 0xE0 - 0xEF */
4069 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4070 /* 0xF0 - 0xFF */
4071 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4072};
4073
4074#undef D
4075#undef N
4076#undef G
4077#undef GD
4078#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004079#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004080#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004081
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004082#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004083#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004084#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004085#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004086#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004087
Avi Kivity9dac77f2011-06-01 15:34:25 +03004088static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004089{
4090 unsigned size;
4091
Avi Kivity9dac77f2011-06-01 15:34:25 +03004092 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004093 if (size == 8)
4094 size = 4;
4095 return size;
4096}
4097
4098static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4099 unsigned size, bool sign_extension)
4100{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004101 int rc = X86EMUL_CONTINUE;
4102
4103 op->type = OP_IMM;
4104 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004105 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004106 /* NB. Immediates are sign-extended as necessary. */
4107 switch (op->bytes) {
4108 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004109 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004110 break;
4111 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004112 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004113 break;
4114 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004115 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004116 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004117 case 8:
4118 op->val = insn_fetch(s64, ctxt);
4119 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004120 }
4121 if (!sign_extension) {
4122 switch (op->bytes) {
4123 case 1:
4124 op->val &= 0xff;
4125 break;
4126 case 2:
4127 op->val &= 0xffff;
4128 break;
4129 case 4:
4130 op->val &= 0xffffffff;
4131 break;
4132 }
4133 }
4134done:
4135 return rc;
4136}
4137
Avi Kivitya9945542011-09-13 10:45:41 +03004138static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4139 unsigned d)
4140{
4141 int rc = X86EMUL_CONTINUE;
4142
4143 switch (d) {
4144 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004145 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004146 break;
4147 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004148 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004149 break;
4150 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004151 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004152 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004153 *op = ctxt->memop;
4154 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004155 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004156 fetch_bit_operand(ctxt);
4157 op->orig_val = op->val;
4158 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004159 case OpMem64:
4160 ctxt->memop.bytes = 8;
4161 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004162 case OpAcc:
4163 op->type = OP_REG;
4164 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004165 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004166 fetch_register_operand(op);
4167 op->orig_val = op->val;
4168 break;
4169 case OpDI:
4170 op->type = OP_MEM;
4171 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4172 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004173 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004174 op->addr.mem.seg = VCPU_SREG_ES;
4175 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004176 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004177 break;
4178 case OpDX:
4179 op->type = OP_REG;
4180 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004181 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004182 fetch_register_operand(op);
4183 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004184 case OpCL:
4185 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004186 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004187 break;
4188 case OpImmByte:
4189 rc = decode_imm(ctxt, op, 1, true);
4190 break;
4191 case OpOne:
4192 op->bytes = 1;
4193 op->val = 1;
4194 break;
4195 case OpImm:
4196 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4197 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004198 case OpImm64:
4199 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4200 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004201 case OpMem8:
4202 ctxt->memop.bytes = 1;
Gleb Natapov660696d2013-04-24 13:38:36 +03004203 if (ctxt->memop.type == OP_REG) {
4204 ctxt->memop.addr.reg = decode_register(ctxt, ctxt->modrm_rm, 1);
4205 fetch_register_operand(&ctxt->memop);
4206 }
Avi Kivity28867ce2012-01-16 15:08:44 +02004207 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004208 case OpMem16:
4209 ctxt->memop.bytes = 2;
4210 goto mem_common;
4211 case OpMem32:
4212 ctxt->memop.bytes = 4;
4213 goto mem_common;
4214 case OpImmU16:
4215 rc = decode_imm(ctxt, op, 2, false);
4216 break;
4217 case OpImmU:
4218 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4219 break;
4220 case OpSI:
4221 op->type = OP_MEM;
4222 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4223 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004224 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004225 op->addr.mem.seg = seg_override(ctxt);
4226 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004227 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004228 break;
Paolo Bonzini7fa57952013-05-09 11:32:50 +02004229 case OpXLat:
4230 op->type = OP_MEM;
4231 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4232 op->addr.mem.ea =
4233 register_address(ctxt,
4234 reg_read(ctxt, VCPU_REGS_RBX) +
4235 (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
4236 op->addr.mem.seg = seg_override(ctxt);
4237 op->val = 0;
4238 break;
Avi Kivity0fe59122011-09-13 10:45:47 +03004239 case OpImmFAddr:
4240 op->type = OP_IMM;
4241 op->addr.mem.ea = ctxt->_eip;
4242 op->bytes = ctxt->op_bytes + 2;
4243 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4244 break;
4245 case OpMemFAddr:
4246 ctxt->memop.bytes = ctxt->op_bytes + 2;
4247 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004248 case OpES:
4249 op->val = VCPU_SREG_ES;
4250 break;
4251 case OpCS:
4252 op->val = VCPU_SREG_CS;
4253 break;
4254 case OpSS:
4255 op->val = VCPU_SREG_SS;
4256 break;
4257 case OpDS:
4258 op->val = VCPU_SREG_DS;
4259 break;
4260 case OpFS:
4261 op->val = VCPU_SREG_FS;
4262 break;
4263 case OpGS:
4264 op->val = VCPU_SREG_GS;
4265 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004266 case OpImplicit:
4267 /* Special instructions do their own operand decoding. */
4268 default:
4269 op->type = OP_NONE; /* Disable writeback. */
4270 break;
4271 }
4272
4273done:
4274 return rc;
4275}
4276
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004277int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004278{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004279 int rc = X86EMUL_CONTINUE;
4280 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004281 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004282 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004283 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004284
Avi Kivityf09ed832011-09-13 10:45:40 +03004285 ctxt->memop.type = OP_NONE;
4286 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004287 ctxt->_eip = ctxt->eip;
4288 ctxt->fetch.start = ctxt->_eip;
4289 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004290 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004291 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004292
4293 switch (mode) {
4294 case X86EMUL_MODE_REAL:
4295 case X86EMUL_MODE_VM86:
4296 case X86EMUL_MODE_PROT16:
4297 def_op_bytes = def_ad_bytes = 2;
4298 break;
4299 case X86EMUL_MODE_PROT32:
4300 def_op_bytes = def_ad_bytes = 4;
4301 break;
4302#ifdef CONFIG_X86_64
4303 case X86EMUL_MODE_PROT64:
4304 def_op_bytes = 4;
4305 def_ad_bytes = 8;
4306 break;
4307#endif
4308 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004309 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004310 }
4311
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 ctxt->op_bytes = def_op_bytes;
4313 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004314
4315 /* Legacy prefixes. */
4316 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004317 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004318 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004319 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004320 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004321 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004322 break;
4323 case 0x67: /* address-size override */
4324 if (mode == X86EMUL_MODE_PROT64)
4325 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004326 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004327 else
4328 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004329 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004330 break;
4331 case 0x26: /* ES override */
4332 case 0x2e: /* CS override */
4333 case 0x36: /* SS override */
4334 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004335 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004336 break;
4337 case 0x64: /* FS override */
4338 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004339 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004340 break;
4341 case 0x40 ... 0x4f: /* REX */
4342 if (mode != X86EMUL_MODE_PROT64)
4343 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004344 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004345 continue;
4346 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004347 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004348 break;
4349 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004350 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004351 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004352 break;
4353 default:
4354 goto done_prefixes;
4355 }
4356
4357 /* Any legacy prefix after a REX prefix nullifies its effect. */
4358
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360 }
4361
4362done_prefixes:
4363
4364 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 if (ctxt->rex_prefix & 8)
4366 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004367
4368 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004369 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004370 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 if (ctxt->b == 0x0f) {
4372 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004373 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004374 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004375 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004377
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004378 if (ctxt->d & ModRM)
4379 ctxt->modrm = insn_fetch(u8, ctxt);
4380
Avi Kivity9dac77f2011-06-01 15:34:25 +03004381 while (ctxt->d & GroupMask) {
4382 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004383 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004384 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004385 opcode = opcode.u.group[goffset];
4386 break;
4387 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004388 goffset = (ctxt->modrm >> 3) & 7;
4389 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004390 opcode = opcode.u.gdual->mod3[goffset];
4391 else
4392 opcode = opcode.u.gdual->mod012[goffset];
4393 break;
4394 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004395 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004396 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004397 break;
4398 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004399 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004400 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004401 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004402 switch (simd_prefix) {
4403 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4404 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4405 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4406 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4407 }
4408 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004409 case Escape:
4410 if (ctxt->modrm > 0xbf)
4411 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4412 else
4413 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4414 break;
Avi Kivity46561642011-04-24 14:09:59 +03004415 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004416 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004417 }
Avi Kivity46561642011-04-24 14:09:59 +03004418
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004419 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004420 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004421 }
4422
Avi Kivity9dac77f2011-06-01 15:34:25 +03004423 ctxt->execute = opcode.u.execute;
4424 ctxt->check_perm = opcode.check_perm;
4425 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004426
4427 /* Unrecognised? */
Gleb Natapov1146a782013-04-11 12:30:01 +03004428 if (ctxt->d == 0 || (ctxt->d & NotImpl))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004429 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004430
Avi Kivity9dac77f2011-06-01 15:34:25 +03004431 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004432 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004433
Avi Kivity9dac77f2011-06-01 15:34:25 +03004434 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4435 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004436
Avi Kivity9dac77f2011-06-01 15:34:25 +03004437 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004438 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004439 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004440 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004441 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004442 }
4443
Avi Kivity9dac77f2011-06-01 15:34:25 +03004444 if (ctxt->d & Sse)
4445 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004446 else if (ctxt->d & Mmx)
4447 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004448
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004449 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004450 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004451 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004452 if (!ctxt->has_seg_override)
4453 set_seg_override(ctxt, ctxt->modrm_seg);
4454 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004455 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004456 if (rc != X86EMUL_CONTINUE)
4457 goto done;
4458
Avi Kivity9dac77f2011-06-01 15:34:25 +03004459 if (!ctxt->has_seg_override)
4460 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004461
Avi Kivityf09ed832011-09-13 10:45:40 +03004462 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004463
Avi Kivityf09ed832011-09-13 10:45:40 +03004464 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4465 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004466
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004467 /*
4468 * Decode and fetch the source operand: register, memory
4469 * or immediate.
4470 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004471 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004472 if (rc != X86EMUL_CONTINUE)
4473 goto done;
4474
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004475 /*
4476 * Decode and fetch the second source operand: register, memory
4477 * or immediate.
4478 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004479 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004480 if (rc != X86EMUL_CONTINUE)
4481 goto done;
4482
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004483 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004484 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004485
4486done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004487 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4488 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004489
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004490 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004491}
4492
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004493bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4494{
4495 return ctxt->d & PageTable;
4496}
4497
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004498static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4499{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004500 /* The second termination condition only applies for REPE
4501 * and REPNE. Test if the repeat string operation prefix is
4502 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4503 * corresponding termination condition according to:
4504 * - if REPE/REPZ and ZF = 0 then done
4505 * - if REPNE/REPNZ and ZF = 1 then done
4506 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004507 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4508 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4509 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004510 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004511 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004512 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4513 return true;
4514
4515 return false;
4516}
4517
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004518static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4519{
4520 bool fault = false;
4521
4522 ctxt->ops->get_fpu(ctxt);
4523 asm volatile("1: fwait \n\t"
4524 "2: \n\t"
4525 ".pushsection .fixup,\"ax\" \n\t"
4526 "3: \n\t"
4527 "movb $1, %[fault] \n\t"
4528 "jmp 2b \n\t"
4529 ".popsection \n\t"
4530 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004531 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004532 ctxt->ops->put_fpu(ctxt);
4533
4534 if (unlikely(fault))
4535 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4536
4537 return X86EMUL_CONTINUE;
4538}
4539
4540static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4541 struct operand *op)
4542{
4543 if (op->type == OP_MM)
4544 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4545}
4546
Avi Kivitye28bbd42013-01-04 16:18:48 +02004547static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4548{
4549 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4550 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4551 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4552 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4553 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4554 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4555 return X86EMUL_CONTINUE;
4556}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004557
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004558int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004559{
Mathias Krause0225fb52012-08-30 01:30:16 +02004560 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004561 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004562 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004563
Avi Kivity9dac77f2011-06-01 15:34:25 +03004564 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004565
Gleb Natapov1146a782013-04-11 12:30:01 +03004566 if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
4567 (ctxt->d & Undefined)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004568 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004569 goto done;
4570 }
4571
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004572 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004573 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004574 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004575 goto done;
4576 }
4577
Avi Kivity9dac77f2011-06-01 15:34:25 +03004578 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004579 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004580 goto done;
4581 }
4582
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004583 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4584 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004585 rc = emulate_ud(ctxt);
4586 goto done;
4587 }
4588
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004589 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004590 rc = emulate_nm(ctxt);
4591 goto done;
4592 }
4593
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004594 if (ctxt->d & Mmx) {
4595 rc = flush_pending_x87_faults(ctxt);
4596 if (rc != X86EMUL_CONTINUE)
4597 goto done;
4598 /*
4599 * Now that we know the fpu is exception safe, we can fetch
4600 * operands from it.
4601 */
4602 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4603 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4604 if (!(ctxt->d & Mov))
4605 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4606 }
4607
Avi Kivity9dac77f2011-06-01 15:34:25 +03004608 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4609 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004610 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004611 if (rc != X86EMUL_CONTINUE)
4612 goto done;
4613 }
4614
Gleb Natapove92805a2010-02-10 14:21:35 +02004615 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004617 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004618 goto done;
4619 }
4620
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004621 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004622 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004623 rc = emulate_ud(ctxt);
4624 goto done;
4625 }
4626
Joerg Roedeld09beab2011-04-04 12:39:25 +02004627 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004628 if (ctxt->check_perm) {
4629 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004630 if (rc != X86EMUL_CONTINUE)
4631 goto done;
4632 }
4633
Avi Kivity9dac77f2011-06-01 15:34:25 +03004634 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4635 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004636 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004637 if (rc != X86EMUL_CONTINUE)
4638 goto done;
4639 }
4640
Avi Kivity9dac77f2011-06-01 15:34:25 +03004641 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004642 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004643 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004644 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004645 goto done;
4646 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004647 }
4648
Avi Kivity9dac77f2011-06-01 15:34:25 +03004649 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4650 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4651 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004652 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004653 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004654 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004655 }
4656
Avi Kivity9dac77f2011-06-01 15:34:25 +03004657 if (ctxt->src2.type == OP_MEM) {
4658 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4659 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004660 if (rc != X86EMUL_CONTINUE)
4661 goto done;
4662 }
4663
Avi Kivity9dac77f2011-06-01 15:34:25 +03004664 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004665 goto special_insn;
4666
4667
Avi Kivity9dac77f2011-06-01 15:34:25 +03004668 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004669 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004670 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4671 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004672 if (rc != X86EMUL_CONTINUE)
4673 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004674 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004675 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004676
Avi Kivity018a98d2007-11-27 19:30:56 +02004677special_insn:
4678
Avi Kivity9dac77f2011-06-01 15:34:25 +03004679 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4680 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004681 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004682 if (rc != X86EMUL_CONTINUE)
4683 goto done;
4684 }
4685
Avi Kivity9dac77f2011-06-01 15:34:25 +03004686 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004687 if (ctxt->d & Fastop) {
4688 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4689 rc = fastop(ctxt, fop);
4690 if (rc != X86EMUL_CONTINUE)
4691 goto done;
4692 goto writeback;
4693 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004694 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004695 if (rc != X86EMUL_CONTINUE)
4696 goto done;
4697 goto writeback;
4698 }
4699
Avi Kivity9dac77f2011-06-01 15:34:25 +03004700 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701 goto twobyte_insn;
4702
Avi Kivity9dac77f2011-06-01 15:34:25 +03004703 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004705 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004707 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004709 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004710 if (test_cc(ctxt->b, ctxt->eflags))
4711 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004712 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004713 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004714 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004715 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004716 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004717 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004718 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004719 rc = em_xchg(ctxt);
4720 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004721 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004722 switch (ctxt->op_bytes) {
4723 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4724 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4725 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004726 }
4727 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004728 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004729 rc = emulate_int(ctxt, 3);
4730 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004731 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004732 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004733 break;
4734 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004735 if (ctxt->eflags & EFLG_OF)
4736 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004737 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004738 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004739 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004740 jmp_rel(ctxt, ctxt->src.val);
4741 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004742 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004743 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004744 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004745 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004746 case 0xf5: /* cmc */
4747 /* complement carry flag from eflags reg */
4748 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004749 break;
4750 case 0xf8: /* clc */
4751 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004752 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004753 case 0xf9: /* stc */
4754 ctxt->eflags |= EFLG_CF;
4755 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004756 case 0xfc: /* cld */
4757 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004758 break;
4759 case 0xfd: /* std */
4760 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004761 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004762 default:
4763 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004764 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004765
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004766 if (rc != X86EMUL_CONTINUE)
4767 goto done;
4768
Avi Kivity018a98d2007-11-27 19:30:56 +02004769writeback:
Avi Kivityfb32b1e2013-02-09 11:31:44 +02004770 if (!(ctxt->d & NoWrite)) {
4771 rc = writeback(ctxt, &ctxt->dst);
4772 if (rc != X86EMUL_CONTINUE)
4773 goto done;
4774 }
4775 if (ctxt->d & SrcWrite) {
4776 BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
4777 rc = writeback(ctxt, &ctxt->src);
4778 if (rc != X86EMUL_CONTINUE)
4779 goto done;
4780 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004781
Gleb Natapov5cd21912010-03-18 15:20:26 +02004782 /*
4783 * restore dst type in case the decoding will be reused
4784 * (happens for string instruction )
4785 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004786 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004787
Avi Kivity9dac77f2011-06-01 15:34:25 +03004788 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004789 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004790
Avi Kivity9dac77f2011-06-01 15:34:25 +03004791 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004792 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004793
Avi Kivity9dac77f2011-06-01 15:34:25 +03004794 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004795 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004796 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004797 if ((ctxt->d & SrcMask) == SrcSI)
4798 count = ctxt->src.count;
4799 else
4800 count = ctxt->dst.count;
4801 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4802 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004803
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004804 if (!string_insn_completed(ctxt)) {
4805 /*
4806 * Re-enter guest when pio read ahead buffer is empty
4807 * or, if it is not used, after each 1024 iteration.
4808 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004809 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004810 (r->end == 0 || r->end != r->pos)) {
4811 /*
4812 * Reset read cache. Usually happens before
4813 * decode, but since instruction is restarted
4814 * we have to do it here.
4815 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004816 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004817 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004818 return EMULATION_RESTART;
4819 }
4820 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004821 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004822 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004823
Avi Kivity9dac77f2011-06-01 15:34:25 +03004824 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004825
4826done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004827 if (rc == X86EMUL_PROPAGATE_FAULT)
4828 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004829 if (rc == X86EMUL_INTERCEPTED)
4830 return EMULATION_INTERCEPTED;
4831
Avi Kivitydd856ef2012-08-27 23:46:17 +03004832 if (rc == X86EMUL_CONTINUE)
4833 writeback_registers(ctxt);
4834
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004835 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004836
4837twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004838 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004839 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004840 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004841 break;
4842 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004843 case 0x0d: /* GrpP (prefetch) */
4844 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004845 break;
4846 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004847 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004848 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004849 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004850 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004853 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4854 if (!test_cc(ctxt->b, ctxt->eflags))
4855 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004857 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004858 if (test_cc(ctxt->b, ctxt->eflags))
4859 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004860 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004861 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004862 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004863 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004864 case 0xae: /* clflush */
4865 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004867 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004868 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004869 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004872 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004873 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004874 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004875 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004876 case 0xc0 ... 0xc1: /* xadd */
Avi Kivity158de572013-01-19 19:51:57 +02004877 fastop(ctxt, em_add);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004878 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004879 ctxt->src.val = ctxt->dst.orig_val;
4880 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004881 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004882 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004883 ctxt->dst.bytes = ctxt->op_bytes;
4884 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4885 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004886 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004887 default:
4888 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004889 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004890
4891 if (rc != X86EMUL_CONTINUE)
4892 goto done;
4893
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 goto writeback;
4895
4896cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004897 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004898}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004899
4900void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4901{
4902 invalidate_registers(ctxt);
4903}
4904
4905void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4906{
4907 writeback_registers(ctxt);
4908}