blob: edb09e9c111cff4abde0763a76c13502e22e1953 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya99455492011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Avi Kivitya99455492011-09-13 10:45:41 +030063
Avi Kivity0fe59122011-09-13 10:45:47 +030064#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030065#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya99455492011-09-13 10:45:41 +030066
67/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080068 * Opcode effective-address decode tables.
69 * Note that we only emulate instructions that have at least one memory
70 * operand (excluding implicit stack references). We assume that stack
71 * references and instruction fetches will never occur in special memory
72 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
73 * not be handled.
74 */
75
76/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030077#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080078/* Destination operand type. */
Avi Kivitya99455492011-09-13 10:45:41 +030079#define DstShift 1
80#define ImplicitOps (OpImplicit << DstShift)
81#define DstReg (OpReg << DstShift)
82#define DstMem (OpMem << DstShift)
83#define DstAcc (OpAcc << DstShift)
84#define DstDI (OpDI << DstShift)
85#define DstMem64 (OpMem64 << DstShift)
86#define DstImmUByte (OpImmUByte << DstShift)
87#define DstDX (OpDX << DstShift)
88#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080089/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030090#define SrcShift 6
91#define SrcNone (OpNone << SrcShift)
92#define SrcReg (OpReg << SrcShift)
93#define SrcMem (OpMem << SrcShift)
94#define SrcMem16 (OpMem16 << SrcShift)
95#define SrcMem32 (OpMem32 << SrcShift)
96#define SrcImm (OpImm << SrcShift)
97#define SrcImmByte (OpImmByte << SrcShift)
98#define SrcOne (OpOne << SrcShift)
99#define SrcImmUByte (OpImmUByte << SrcShift)
100#define SrcImmU (OpImmU << SrcShift)
101#define SrcSI (OpSI << SrcShift)
102#define SrcImmFAddr (OpImmFAddr << SrcShift)
103#define SrcMemFAddr (OpMemFAddr << SrcShift)
104#define SrcAcc (OpAcc << SrcShift)
105#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200106#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200108#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300110#define BitOp (1<<11)
111#define MemAbs (1<<12) /* Memory operand is absolute displacement */
112#define String (1<<13) /* String instruction (rep capable) */
113#define Stack (1<<14) /* Stack instruction (push/pop) */
114#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
115#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
116#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
117#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
118#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200119#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300120#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300121/* Generic ModRM decode. */
122#define ModRM (1<<19)
123/* Destination is only written; never read. */
124#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300125/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200126#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200127#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300128#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300129#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300130#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200131#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200132#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300133#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800134#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100135/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800136#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300137#define Src2None (OpNone << Src2Shift)
138#define Src2CL (OpCL << Src2Shift)
139#define Src2ImmByte (OpImmByte << Src2Shift)
140#define Src2One (OpOne << Src2Shift)
141#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300142#define Src2ES (OpES << Src2Shift)
143#define Src2CS (OpCS << Src2Shift)
144#define Src2SS (OpSS << Src2Shift)
145#define Src2DS (OpDS << Src2Shift)
146#define Src2FS (OpFS << Src2Shift)
147#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300148#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300149#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300150#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
151#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
152#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200153#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200154#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
Avi Kivityd0e53322010-07-29 15:11:54 +0300156#define X2(x...) x, x
157#define X3(x...) X2(x), x
158#define X4(x...) X2(x), X2(x)
159#define X5(x...) X4(x), x
160#define X6(x...) X4(x), X2(x)
161#define X7(x...) X4(x), X3(x)
162#define X8(x...) X4(x), X4(x)
163#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300164
Avi Kivitye28bbd42013-01-04 16:18:48 +0200165#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
166#define FASTOP_SIZE 8
167
168/*
169 * fastop functions have a special calling convention:
170 *
171 * dst: [rdx]:rax (in/out)
172 * src: rbx (in/out)
173 * src2: rcx (in)
174 * flags: rflags (in/out)
175 *
176 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
177 * different operand sizes can be reached by calculation, rather than a jump
178 * table (which would be bigger than the code).
179 *
180 * fastop functions are declared as taking a never-defined fastop parameter,
181 * so they can't be called from C directly.
182 */
183
184struct fastop;
185
Avi Kivityd65b1de2010-07-29 15:11:35 +0300186struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300187 u64 flags : 56;
188 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300189 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300190 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200191 const struct opcode *group;
192 const struct group_dual *gdual;
193 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200194 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300196 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200197 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300198};
199
200struct group_dual {
201 struct opcode mod012[8];
202 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203};
204
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200205struct gprefix {
206 struct opcode pfx_no;
207 struct opcode pfx_66;
208 struct opcode pfx_f2;
209 struct opcode pfx_f3;
210};
211
Gleb Natapov045a2822012-12-20 16:57:43 +0200212struct escape {
213 struct opcode op[8];
214 struct opcode high[64];
215};
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200218#define EFLG_ID (1<<21)
219#define EFLG_VIP (1<<20)
220#define EFLG_VIF (1<<19)
221#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200222#define EFLG_VM (1<<17)
223#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200224#define EFLG_IOPL (3<<12)
225#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226#define EFLG_OF (1<<11)
227#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200228#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200229#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230#define EFLG_SF (1<<7)
231#define EFLG_ZF (1<<6)
232#define EFLG_AF (1<<4)
233#define EFLG_PF (1<<2)
234#define EFLG_CF (1<<0)
235
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300236#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
237#define EFLG_RESERVED_ONE_MASK 2
238
Avi Kivitydd856ef2012-08-27 23:46:17 +0300239static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
240{
241 if (!(ctxt->regs_valid & (1 << nr))) {
242 ctxt->regs_valid |= 1 << nr;
243 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
244 }
245 return ctxt->_regs[nr];
246}
247
248static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
249{
250 ctxt->regs_valid |= 1 << nr;
251 ctxt->regs_dirty |= 1 << nr;
252 return &ctxt->_regs[nr];
253}
254
255static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
256{
257 reg_read(ctxt, nr);
258 return reg_write(ctxt, nr);
259}
260
261static void writeback_registers(struct x86_emulate_ctxt *ctxt)
262{
263 unsigned reg;
264
265 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
266 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
267}
268
269static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
270{
271 ctxt->regs_dirty = 0;
272 ctxt->regs_valid = 0;
273}
274
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275/*
276 * Instruction emulation:
277 * Most instructions are emulated directly via a fragment of inline assembly
278 * code. This allows us to save/restore EFLAGS and thus very easily pick up
279 * any modified flags.
280 */
281
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800282#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283#define _LO32 "k" /* force 32-bit operand */
284#define _STK "%%rsp" /* stack pointer */
285#elif defined(__i386__)
286#define _LO32 "" /* force 32-bit operand */
287#define _STK "%%esp" /* stack pointer */
288#endif
289
290/*
291 * These EFLAGS bits are restored from saved value during emulation, and
292 * any changes are written back to the saved value after emulation.
293 */
294#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
295
296/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200297#define _PRE_EFLAGS(_sav, _msk, _tmp) \
298 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
299 "movl %"_sav",%"_LO32 _tmp"; " \
300 "push %"_tmp"; " \
301 "push %"_tmp"; " \
302 "movl %"_msk",%"_LO32 _tmp"; " \
303 "andl %"_LO32 _tmp",("_STK"); " \
304 "pushf; " \
305 "notl %"_LO32 _tmp"; " \
306 "andl %"_LO32 _tmp",("_STK"); " \
307 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
308 "pop %"_tmp"; " \
309 "orl %"_LO32 _tmp",("_STK"); " \
310 "popf; " \
311 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313/* After executing instruction: write-back necessary bits in EFLAGS. */
314#define _POST_EFLAGS(_sav, _msk, _tmp) \
315 /* _sav |= EFLAGS & _msk; */ \
316 "pushf; " \
317 "pop %"_tmp"; " \
318 "andl %"_msk",%"_LO32 _tmp"; " \
319 "orl %"_LO32 _tmp",%"_sav"; "
320
Avi Kivitydda96d82008-11-26 15:14:10 +0200321#ifdef CONFIG_X86_64
322#define ON64(x) x
323#else
324#define ON64(x)
325#endif
326
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300327#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200328 do { \
329 __asm__ __volatile__ ( \
330 _PRE_EFLAGS("0", "4", "2") \
331 _op _suffix " %"_x"3,%1; " \
332 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300333 : "=m" ((ctxt)->eflags), \
334 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200335 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300336 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200337 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200338
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300341#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200342 do { \
343 unsigned long _tmp; \
344 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300345 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200346 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300347 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200348 break; \
349 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300350 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200351 break; \
352 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300353 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200354 break; \
355 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 } while (0)
357
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300358#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200360 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300361 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300363 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 break; \
365 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300366 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 _wx, _wy, _lx, _ly, _qx, _qy); \
368 break; \
369 } \
370 } while (0)
371
372/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300373#define emulate_2op_SrcB(ctxt, _op) \
374 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375
376/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300377#define emulate_2op_SrcV(ctxt, _op) \
378 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379
380/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300381#define emulate_2op_SrcV_nobyte(ctxt, _op) \
382 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100384/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300385#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300386 do { \
387 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300388 _type _clv = (ctxt)->src2.val; \
389 _type _srcv = (ctxt)->src.val; \
390 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300391 \
392 __asm__ __volatile__ ( \
393 _PRE_EFLAGS("0", "5", "2") \
394 _op _suffix " %4,%1 \n" \
395 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300396 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300397 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
398 ); \
399 \
Avi Kivity761441b2011-09-07 16:41:36 +0300400 (ctxt)->src2.val = (unsigned long) _clv; \
401 (ctxt)->src2.val = (unsigned long) _srcv; \
402 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100403 } while (0)
404
Avi Kivity761441b2011-09-07 16:41:36 +0300405#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300406 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300407 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300408 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300409 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300410 break; \
411 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300412 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300413 break; \
414 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300415 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300416 break; \
417 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100418 } while (0)
419
Avi Kivityd1eef452011-09-07 16:41:38 +0300420#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421 do { \
422 unsigned long _tmp; \
423 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200424 __asm__ __volatile__ ( \
425 _PRE_EFLAGS("0", "3", "2") \
426 _op _suffix " %1; " \
427 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300428 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200429 "=&r" (_tmp) \
430 : "i" (EFLAGS_MASK)); \
431 } while (0)
432
433/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300434#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200435 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300436 switch ((ctxt)->dst.bytes) { \
437 case 1: __emulate_1op(ctxt, _op, "b"); break; \
438 case 2: __emulate_1op(ctxt, _op, "w"); break; \
439 case 4: __emulate_1op(ctxt, _op, "l"); break; \
440 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 } \
442 } while (0)
443
Avi Kivityb7d491e2013-01-04 16:18:49 +0200444#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
445#define FOP_RET "ret \n\t"
446
447#define FOP_START(op) \
448 extern void em_##op(struct fastop *fake); \
449 asm(".pushsection .text, \"ax\" \n\t" \
450 ".global em_" #op " \n\t" \
451 FOP_ALIGN \
452 "em_" #op ": \n\t"
453
454#define FOP_END \
455 ".popsection")
456
Avi Kivity0bdea062013-01-19 19:51:50 +0200457#define FOPNOP() FOP_ALIGN FOP_RET
458
Avi Kivityb7d491e2013-01-04 16:18:49 +0200459#define FOP1E(op, dst) \
460 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
461
462#define FASTOP1(op) \
463 FOP_START(op) \
464 FOP1E(op##b, al) \
465 FOP1E(op##w, ax) \
466 FOP1E(op##l, eax) \
467 ON64(FOP1E(op##q, rax)) \
468 FOP_END
469
Avi Kivityf7857f32013-01-04 16:18:53 +0200470#define FOP2E(op, dst, src) \
471 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
472
473#define FASTOP2(op) \
474 FOP_START(op) \
475 FOP2E(op##b, al, bl) \
476 FOP2E(op##w, ax, bx) \
477 FOP2E(op##l, eax, ebx) \
478 ON64(FOP2E(op##q, rax, rbx)) \
479 FOP_END
480
Avi Kivity007a3b52013-01-19 19:51:51 +0200481/* 2 operand, src is CL */
482#define FASTOP2CL(op) \
483 FOP_START(op) \
484 FOP2E(op##b, al, cl) \
485 FOP2E(op##w, ax, cl) \
486 FOP2E(op##l, eax, cl) \
487 ON64(FOP2E(op##q, rax, cl)) \
488 FOP_END
489
Avi Kivity0bdea062013-01-19 19:51:50 +0200490#define FOP3E(op, dst, src, src2) \
491 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
492
493/* 3-operand, word-only, src2=cl */
494#define FASTOP3WCL(op) \
495 FOP_START(op) \
496 FOPNOP() \
497 FOP3E(op##w, ax, bx, cl) \
498 FOP3E(op##l, eax, ebx, cl) \
499 ON64(FOP3E(op##q, rax, rbx, cl)) \
500 FOP_END
501
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200502/* Special case for SETcc - 1 instruction per cc */
503#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
504
505FOP_START(setcc)
506FOP_SETCC(seto)
507FOP_SETCC(setno)
508FOP_SETCC(setc)
509FOP_SETCC(setnc)
510FOP_SETCC(setz)
511FOP_SETCC(setnz)
512FOP_SETCC(setbe)
513FOP_SETCC(setnbe)
514FOP_SETCC(sets)
515FOP_SETCC(setns)
516FOP_SETCC(setp)
517FOP_SETCC(setnp)
518FOP_SETCC(setl)
519FOP_SETCC(setnl)
520FOP_SETCC(setle)
521FOP_SETCC(setnle)
522FOP_END;
523
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300524#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300525 do { \
526 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300527 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
528 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300529 \
530 __asm__ __volatile__ ( \
531 _PRE_EFLAGS("0", "5", "1") \
532 "1: \n\t" \
533 _op _suffix " %6; " \
534 "2: \n\t" \
535 _POST_EFLAGS("0", "5", "1") \
536 ".pushsection .fixup,\"ax\" \n\t" \
537 "3: movb $1, %4 \n\t" \
538 "jmp 2b \n\t" \
539 ".popsection \n\t" \
540 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300541 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
542 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800543 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300544 } while (0)
545
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300546/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300547#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300548 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300549 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300550 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300551 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300552 break; \
553 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300554 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300555 break; \
556 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300557 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300558 break; \
559 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300560 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300561 break; \
562 } \
563 } while (0)
564
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200565static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
566 enum x86_intercept intercept,
567 enum x86_intercept_stage stage)
568{
569 struct x86_instruction_info info = {
570 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300571 .rep_prefix = ctxt->rep_prefix,
572 .modrm_mod = ctxt->modrm_mod,
573 .modrm_reg = ctxt->modrm_reg,
574 .modrm_rm = ctxt->modrm_rm,
575 .src_val = ctxt->src.val64,
576 .src_bytes = ctxt->src.bytes,
577 .dst_bytes = ctxt->dst.bytes,
578 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200579 .next_rip = ctxt->eip,
580 };
581
Avi Kivity29535382011-04-20 13:37:53 +0300582 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200583}
584
Avi Kivityf47cfa32012-06-07 17:49:24 +0300585static void assign_masked(ulong *dest, ulong src, ulong mask)
586{
587 *dest = (*dest & ~mask) | (src & mask);
588}
589
Avi Kivity9dac77f2011-06-01 15:34:25 +0300590static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800591{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300592 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800593}
594
Avi Kivityf47cfa32012-06-07 17:49:24 +0300595static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
596{
597 u16 sel;
598 struct desc_struct ss;
599
600 if (ctxt->mode == X86EMUL_MODE_PROT64)
601 return ~0UL;
602 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
603 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
604}
605
Avi Kivity612e89f2012-06-12 20:03:23 +0300606static int stack_size(struct x86_emulate_ctxt *ctxt)
607{
608 return (__fls(stack_mask(ctxt)) + 1) >> 3;
609}
610
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800612static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300613address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800614{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300615 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800616 return reg;
617 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300618 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800619}
620
621static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300622register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800623{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300624 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800625}
626
Avi Kivity5ad105e2012-08-19 14:34:31 +0300627static void masked_increment(ulong *reg, ulong mask, int inc)
628{
629 assign_masked(reg, *reg + inc, mask);
630}
631
Harvey Harrison7a9572752008-02-19 07:40:41 -0800632static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300633register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800634{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300635 ulong mask;
636
Avi Kivity9dac77f2011-06-01 15:34:25 +0300637 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300638 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800639 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300640 mask = ad_mask(ctxt);
641 masked_increment(reg, mask, inc);
642}
643
644static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
645{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300646 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800647}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648
Avi Kivity9dac77f2011-06-01 15:34:25 +0300649static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800650{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300651 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800652}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300653
Avi Kivity56697682011-04-03 14:08:51 +0300654static u32 desc_limit_scaled(struct desc_struct *desc)
655{
656 u32 limit = get_desc_limit(desc);
657
658 return desc->g ? (limit << 12) | 0xfff : limit;
659}
660
Avi Kivity9dac77f2011-06-01 15:34:25 +0300661static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300662{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300663 ctxt->has_seg_override = true;
664 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300665}
666
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900667static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300668{
669 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
670 return 0;
671
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900672 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300673}
674
Avi Kivity9dac77f2011-06-01 15:34:25 +0300675static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300676{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300677 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300678 return 0;
679
Avi Kivity9dac77f2011-06-01 15:34:25 +0300680 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300681}
682
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200683static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
684 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300685{
Avi Kivityda9cb572010-11-22 17:53:21 +0200686 ctxt->exception.vector = vec;
687 ctxt->exception.error_code = error;
688 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200689 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300690}
691
Joerg Roedel3b88e412011-04-04 12:39:29 +0200692static int emulate_db(struct x86_emulate_ctxt *ctxt)
693{
694 return emulate_exception(ctxt, DB_VECTOR, 0, false);
695}
696
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200697static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300698{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200699 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300700}
701
Avi Kivity618ff152011-04-03 12:32:09 +0300702static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
703{
704 return emulate_exception(ctxt, SS_VECTOR, err, true);
705}
706
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200707static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300708{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200709 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300710}
711
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200712static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300713{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200714 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300715}
716
Avi Kivity34d1f492010-08-26 11:59:01 +0300717static int emulate_de(struct x86_emulate_ctxt *ctxt)
718{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200719 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300720}
721
Avi Kivity12537912011-03-29 11:41:27 +0200722static int emulate_nm(struct x86_emulate_ctxt *ctxt)
723{
724 return emulate_exception(ctxt, NM_VECTOR, 0, false);
725}
726
Avi Kivity1aa36612011-04-27 13:20:30 +0300727static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
728{
729 u16 selector;
730 struct desc_struct desc;
731
732 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
733 return selector;
734}
735
736static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
737 unsigned seg)
738{
739 u16 dummy;
740 u32 base3;
741 struct desc_struct desc;
742
743 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
744 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
745}
746
Avi Kivity1c11b372012-04-09 18:39:59 +0300747/*
748 * x86 defines three classes of vector instructions: explicitly
749 * aligned, explicitly unaligned, and the rest, which change behaviour
750 * depending on whether they're AVX encoded or not.
751 *
752 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
753 * subject to the same check.
754 */
755static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
756{
757 if (likely(size < 16))
758 return false;
759
760 if (ctxt->d & Aligned)
761 return true;
762 else if (ctxt->d & Unaligned)
763 return false;
764 else if (ctxt->d & Avx)
765 return false;
766 else
767 return true;
768}
769
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400770static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300771 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400772 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300773 ulong *linear)
774{
Avi Kivity618ff152011-04-03 12:32:09 +0300775 struct desc_struct desc;
776 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300777 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300778 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300779 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200780 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300781
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900782 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300783 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300784 case X86EMUL_MODE_PROT64:
785 if (((signed long)la << 16) >> 16 != la)
786 return emulate_gp(ctxt, 0);
787 break;
788 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300789 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
790 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300791 if (!usable)
792 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200793 /* code segment in protected mode or read-only data segment */
794 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
795 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300796 goto bad;
797 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400798 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300799 goto bad;
800 lim = desc_limit_scaled(&desc);
801 if ((desc.type & 8) || !(desc.type & 4)) {
802 /* expand-up segment */
803 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
804 goto bad;
805 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800806 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300807 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
808 goto bad;
809 lim = desc.d ? 0xffffffff : 0xffff;
810 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
811 goto bad;
812 }
Avi Kivity717746e2011-04-20 13:37:53 +0300813 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300814 if (!(desc.type & 8)) {
815 /* data segment */
816 if (cpl > desc.dpl)
817 goto bad;
818 } else if ((desc.type & 8) && !(desc.type & 4)) {
819 /* nonconforming code segment */
820 if (cpl != desc.dpl)
821 goto bad;
822 } else if ((desc.type & 8) && (desc.type & 4)) {
823 /* conforming code segment */
824 if (cpl < desc.dpl)
825 goto bad;
826 }
827 break;
828 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300829 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300830 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300831 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
832 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300833 *linear = la;
834 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300835bad:
836 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300837 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300838 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300839 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300840}
841
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400842static int linearize(struct x86_emulate_ctxt *ctxt,
843 struct segmented_address addr,
844 unsigned size, bool write,
845 ulong *linear)
846{
847 return __linearize(ctxt, addr, size, write, false, linear);
848}
849
850
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200851static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
852 struct segmented_address addr,
853 void *data,
854 unsigned size)
855{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200856 int rc;
857 ulong linear;
858
Avi Kivity83b87952011-04-03 11:31:19 +0300859 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200860 if (rc != X86EMUL_CONTINUE)
861 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300862 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200863}
864
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900865/*
866 * Fetch the next byte of the instruction being emulated which is pointed to
867 * by ctxt->_eip, then increment ctxt->_eip.
868 *
869 * Also prefetch the remaining bytes of the instruction without crossing page
870 * boundary if they are not in fetch_cache yet.
871 */
872static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200873{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300874 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200875 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300876 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200877
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900878 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400879 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900880 struct segmented_address addr = { .seg = VCPU_SREG_CS,
881 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300882 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900883 size = min(15UL - cur_size,
884 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400885 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900886 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400887 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900888 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
889 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900890 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200891 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300892 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200893 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900894 *dest = fc->data[ctxt->_eip - fc->start];
895 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900896 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200897}
898
899static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900900 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200901{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900902 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200903
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200904 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900905 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200906 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200907 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900908 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900909 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200910 return rc;
911 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900912 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200913}
914
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900915/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900916#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900917({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900918 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900919 if (rc != X86EMUL_CONTINUE) \
920 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900921 (_type)_x; \
922})
923
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900924#define insn_fetch_arr(_arr, _size, _ctxt) \
925({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900926 if (rc != X86EMUL_CONTINUE) \
927 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900928})
929
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000930/*
931 * Given the 'reg' portion of a ModRM byte, and a register block, return a
932 * pointer into the block that addresses the relevant register.
933 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
934 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300935static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000936 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937{
938 void *p;
939
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300941 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
942 else
943 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 return p;
945}
946
947static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200948 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 u16 *size, unsigned long *address, int op_bytes)
950{
951 int rc;
952
953 if (op_bytes == 2)
954 op_bytes = 3;
955 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200956 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900957 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200959 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200960 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 return rc;
962}
963
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200964static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300965{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200966 u8 rc;
967 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300968
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200969 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
970 asm("pushq %[flags]; popf; call *%[fastop]"
971 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
972 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300973}
974
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300975static void fetch_register_operand(struct operand *op)
976{
977 switch (op->bytes) {
978 case 1:
979 op->val = *(u8 *)op->addr.reg;
980 break;
981 case 2:
982 op->val = *(u16 *)op->addr.reg;
983 break;
984 case 4:
985 op->val = *(u32 *)op->addr.reg;
986 break;
987 case 8:
988 op->val = *(u64 *)op->addr.reg;
989 break;
990 }
991}
992
Avi Kivity12537912011-03-29 11:41:27 +0200993static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
994{
995 ctxt->ops->get_fpu(ctxt);
996 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200997 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
998 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
999 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1000 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1001 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1002 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1003 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1004 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001005#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001006 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1007 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1008 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1009 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1010 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1011 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1012 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1013 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001014#endif
1015 default: BUG();
1016 }
1017 ctxt->ops->put_fpu(ctxt);
1018}
1019
1020static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1021 int reg)
1022{
1023 ctxt->ops->get_fpu(ctxt);
1024 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001025 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1026 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1027 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1028 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1029 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1030 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1031 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1032 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001033#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001034 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1035 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1036 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1037 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1038 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1039 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1040 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1041 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001042#endif
1043 default: BUG();
1044 }
1045 ctxt->ops->put_fpu(ctxt);
1046}
1047
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001048static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1049{
1050 ctxt->ops->get_fpu(ctxt);
1051 switch (reg) {
1052 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1053 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1054 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1055 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1056 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1057 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1058 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1059 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1060 default: BUG();
1061 }
1062 ctxt->ops->put_fpu(ctxt);
1063}
1064
1065static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1066{
1067 ctxt->ops->get_fpu(ctxt);
1068 switch (reg) {
1069 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1070 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1071 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1072 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1073 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1074 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1075 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1076 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1077 default: BUG();
1078 }
1079 ctxt->ops->put_fpu(ctxt);
1080}
1081
Gleb Natapov045a2822012-12-20 16:57:43 +02001082static int em_fninit(struct x86_emulate_ctxt *ctxt)
1083{
1084 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1085 return emulate_nm(ctxt);
1086
1087 ctxt->ops->get_fpu(ctxt);
1088 asm volatile("fninit");
1089 ctxt->ops->put_fpu(ctxt);
1090 return X86EMUL_CONTINUE;
1091}
1092
1093static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1094{
1095 u16 fcw;
1096
1097 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1098 return emulate_nm(ctxt);
1099
1100 ctxt->ops->get_fpu(ctxt);
1101 asm volatile("fnstcw %0": "+m"(fcw));
1102 ctxt->ops->put_fpu(ctxt);
1103
1104 /* force 2 byte destination */
1105 ctxt->dst.bytes = 2;
1106 ctxt->dst.val = fcw;
1107
1108 return X86EMUL_CONTINUE;
1109}
1110
1111static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1112{
1113 u16 fsw;
1114
1115 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1116 return emulate_nm(ctxt);
1117
1118 ctxt->ops->get_fpu(ctxt);
1119 asm volatile("fnstsw %0": "+m"(fsw));
1120 ctxt->ops->put_fpu(ctxt);
1121
1122 /* force 2 byte destination */
1123 ctxt->dst.bytes = 2;
1124 ctxt->dst.val = fsw;
1125
1126 return X86EMUL_CONTINUE;
1127}
1128
Avi Kivity12537912011-03-29 11:41:27 +02001129static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001130 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001131{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001132 unsigned reg = ctxt->modrm_reg;
1133 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001134
Avi Kivity9dac77f2011-06-01 15:34:25 +03001135 if (!(ctxt->d & ModRM))
1136 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001137
Avi Kivity9dac77f2011-06-01 15:34:25 +03001138 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001139 op->type = OP_XMM;
1140 op->bytes = 16;
1141 op->addr.xmm = reg;
1142 read_sse_reg(ctxt, &op->vec_val, reg);
1143 return;
1144 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001145 if (ctxt->d & Mmx) {
1146 reg &= 7;
1147 op->type = OP_MM;
1148 op->bytes = 8;
1149 op->addr.mm = reg;
1150 return;
1151 }
Avi Kivity12537912011-03-29 11:41:27 +02001152
Avi Kivity3c118e22007-10-31 10:27:04 +02001153 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001154 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001155 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001156 op->bytes = 1;
1157 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001158 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001159 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001160 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001161 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001162 op->orig_val = op->val;
1163}
1164
Avi Kivitya6e34072012-06-10 17:15:39 +03001165static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1166{
1167 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1168 ctxt->modrm_seg = VCPU_SREG_SS;
1169}
1170
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001171static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001172 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001173{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001174 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001175 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001176 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001177 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001178
Avi Kivity9dac77f2011-06-01 15:34:25 +03001179 if (ctxt->rex_prefix) {
1180 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1181 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1182 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 }
1184
Avi Kivity9dac77f2011-06-01 15:34:25 +03001185 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1186 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1187 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1188 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001189
Avi Kivity9dac77f2011-06-01 15:34:25 +03001190 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001191 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001192 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001193 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001194 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001195 op->type = OP_XMM;
1196 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001197 op->addr.xmm = ctxt->modrm_rm;
1198 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001199 return rc;
1200 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001201 if (ctxt->d & Mmx) {
1202 op->type = OP_MM;
1203 op->bytes = 8;
1204 op->addr.xmm = ctxt->modrm_rm & 7;
1205 return rc;
1206 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001207 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001208 return rc;
1209 }
1210
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001211 op->type = OP_MEM;
1212
Avi Kivity9dac77f2011-06-01 15:34:25 +03001213 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001214 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1215 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1216 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1217 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001218
1219 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001220 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001221 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001222 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001223 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001224 break;
1225 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001226 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001227 break;
1228 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001229 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001230 break;
1231 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001232 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001234 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 break;
1236 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001237 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001238 break;
1239 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001240 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001241 break;
1242 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001243 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001244 break;
1245 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001246 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001247 break;
1248 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001249 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001250 break;
1251 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001252 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001253 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001254 break;
1255 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001256 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001257 break;
1258 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001259 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1260 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1261 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001262 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001263 } else {
1264 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001265 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001266 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001267 index_reg |= (sib >> 3) & 7;
1268 base_reg |= sib & 7;
1269 scale = sib >> 6;
1270
Avi Kivity9dac77f2011-06-01 15:34:25 +03001271 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001272 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001273 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001274 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001275 adjust_modrm_seg(ctxt, base_reg);
1276 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001277 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001278 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001279 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001280 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001282 } else {
1283 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001284 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001285 adjust_modrm_seg(ctxt, base_reg);
1286 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001287 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001288 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001289 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001290 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001291 break;
1292 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001293 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001294 break;
1295 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001296 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001297 break;
1298 }
1299 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001300 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001301done:
1302 return rc;
1303}
1304
1305static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001306 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001307{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001308 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001310 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001311 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001312 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001313 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001314 break;
1315 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001316 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001317 break;
1318 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001319 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001320 break;
1321 }
1322done:
1323 return rc;
1324}
1325
Avi Kivity9dac77f2011-06-01 15:34:25 +03001326static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001327{
Sheng Yang7129eec2010-09-28 16:33:32 +08001328 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001329
Avi Kivity9dac77f2011-06-01 15:34:25 +03001330 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1331 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001332
Avi Kivity9dac77f2011-06-01 15:34:25 +03001333 if (ctxt->src.bytes == 2)
1334 sv = (s16)ctxt->src.val & (s16)mask;
1335 else if (ctxt->src.bytes == 4)
1336 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001337
Avi Kivity9dac77f2011-06-01 15:34:25 +03001338 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001339 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001340
1341 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001342 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001343}
1344
Gleb Natapov9de41572010-04-28 19:15:22 +03001345static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001346 unsigned long addr, void *dest, unsigned size)
1347{
1348 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001349 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001350
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001351 if (mc->pos < mc->end)
1352 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001353
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001354 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001355
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001356 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1357 &ctxt->exception);
1358 if (rc != X86EMUL_CONTINUE)
1359 return rc;
1360
1361 mc->end += size;
1362
1363read_cached:
1364 memcpy(dest, mc->data + mc->pos, size);
1365 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001366 return X86EMUL_CONTINUE;
1367}
1368
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001369static int segmented_read(struct x86_emulate_ctxt *ctxt,
1370 struct segmented_address addr,
1371 void *data,
1372 unsigned size)
1373{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001374 int rc;
1375 ulong linear;
1376
Avi Kivity83b87952011-04-03 11:31:19 +03001377 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001378 if (rc != X86EMUL_CONTINUE)
1379 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001380 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001381}
1382
1383static int segmented_write(struct x86_emulate_ctxt *ctxt,
1384 struct segmented_address addr,
1385 const void *data,
1386 unsigned size)
1387{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001388 int rc;
1389 ulong linear;
1390
Avi Kivity83b87952011-04-03 11:31:19 +03001391 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001392 if (rc != X86EMUL_CONTINUE)
1393 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001394 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1395 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001396}
1397
1398static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1399 struct segmented_address addr,
1400 const void *orig_data, const void *data,
1401 unsigned size)
1402{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001403 int rc;
1404 ulong linear;
1405
Avi Kivity83b87952011-04-03 11:31:19 +03001406 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001407 if (rc != X86EMUL_CONTINUE)
1408 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001409 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1410 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001411}
1412
Gleb Natapov7b262e92010-03-18 15:20:27 +02001413static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001414 unsigned int size, unsigned short port,
1415 void *dest)
1416{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001417 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001418
1419 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001420 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001421 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001422 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001423 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001424 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1425 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001426 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1427 count);
1428 if (n == 0)
1429 n = 1;
1430 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001431 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001432 return 0;
1433 rc->end = n * size;
1434 }
1435
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001436 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1437 ctxt->dst.data = rc->data + rc->pos;
1438 ctxt->dst.type = OP_MEM_STR;
1439 ctxt->dst.count = (rc->end - rc->pos) / size;
1440 rc->pos = rc->end;
1441 } else {
1442 memcpy(dest, rc->data + rc->pos, size);
1443 rc->pos += size;
1444 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001445 return 1;
1446}
1447
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001448static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1449 u16 index, struct desc_struct *desc)
1450{
1451 struct desc_ptr dt;
1452 ulong addr;
1453
1454 ctxt->ops->get_idt(ctxt, &dt);
1455
1456 if (dt.size < index * 8 + 7)
1457 return emulate_gp(ctxt, index << 3 | 0x2);
1458
1459 addr = dt.address + index * 8;
1460 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1461 &ctxt->exception);
1462}
1463
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001464static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001465 u16 selector, struct desc_ptr *dt)
1466{
Mathias Krause0225fb52012-08-30 01:30:16 +02001467 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001468
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001469 if (selector & 1 << 2) {
1470 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001471 u16 sel;
1472
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001474 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 return;
1476
1477 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1478 dt->address = get_desc_base(&desc);
1479 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001480 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001481}
1482
1483/* allowed just for 8 bytes segments */
1484static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001485 u16 selector, struct desc_struct *desc,
1486 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001487{
1488 struct desc_ptr dt;
1489 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490 ulong addr;
1491
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001492 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001493
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001494 if (dt.size < index * 8 + 7)
1495 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001496
Avi Kivitye9194642012-06-13 16:29:39 +03001497 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001498 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1499 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001500}
1501
1502/* allowed just for 8 bytes segments */
1503static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001504 u16 selector, struct desc_struct *desc)
1505{
1506 struct desc_ptr dt;
1507 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001508 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001509
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001510 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001511
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001512 if (dt.size < index * 8 + 7)
1513 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001514
1515 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001516 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1517 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001518}
1519
Gleb Natapov5601d052011-03-07 14:55:06 +02001520/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001521static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001522 u16 selector, int seg)
1523{
Avi Kivity869be992012-06-13 16:30:53 +03001524 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001525 u8 dpl, rpl, cpl;
1526 unsigned err_vec = GP_VECTOR;
1527 u32 err_code = 0;
1528 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001529 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001531 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001532
1533 memset(&seg_desc, 0, sizeof seg_desc);
1534
1535 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1536 || ctxt->mode == X86EMUL_MODE_REAL) {
1537 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001538 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001540 goto load;
1541 }
1542
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001543 rpl = selector & 3;
1544 cpl = ctxt->ops->cpl(ctxt);
1545
1546 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1547 if ((seg == VCPU_SREG_CS
1548 || (seg == VCPU_SREG_SS
1549 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1550 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001551 && null_selector)
1552 goto exception;
1553
1554 /* TR should be in GDT only */
1555 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1556 goto exception;
1557
1558 if (null_selector) /* for NULL selector skip all following checks */
1559 goto load;
1560
Avi Kivitye9194642012-06-13 16:29:39 +03001561 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001562 if (ret != X86EMUL_CONTINUE)
1563 return ret;
1564
1565 err_code = selector & 0xfffc;
1566 err_vec = GP_VECTOR;
1567
Guo Chaofc058682012-06-28 15:19:51 +08001568 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001569 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1570 goto exception;
1571
1572 if (!seg_desc.p) {
1573 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1574 goto exception;
1575 }
1576
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001577 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001578
1579 switch (seg) {
1580 case VCPU_SREG_SS:
1581 /*
1582 * segment is not a writable data segment or segment
1583 * selector's RPL != CPL or segment selector's RPL != CPL
1584 */
1585 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1586 goto exception;
1587 break;
1588 case VCPU_SREG_CS:
1589 if (!(seg_desc.type & 8))
1590 goto exception;
1591
1592 if (seg_desc.type & 4) {
1593 /* conforming */
1594 if (dpl > cpl)
1595 goto exception;
1596 } else {
1597 /* nonconforming */
1598 if (rpl > cpl || dpl != cpl)
1599 goto exception;
1600 }
1601 /* CS(RPL) <- CPL */
1602 selector = (selector & 0xfffc) | cpl;
1603 break;
1604 case VCPU_SREG_TR:
1605 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1606 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001607 old_desc = seg_desc;
1608 seg_desc.type |= 2; /* busy */
1609 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1610 sizeof(seg_desc), &ctxt->exception);
1611 if (ret != X86EMUL_CONTINUE)
1612 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001613 break;
1614 case VCPU_SREG_LDTR:
1615 if (seg_desc.s || seg_desc.type != 2)
1616 goto exception;
1617 break;
1618 default: /* DS, ES, FS, or GS */
1619 /*
1620 * segment is not a data or readable code segment or
1621 * ((segment is a data or nonconforming code segment)
1622 * and (both RPL and CPL > DPL))
1623 */
1624 if ((seg_desc.type & 0xa) == 0x8 ||
1625 (((seg_desc.type & 0xc) != 0xc) &&
1626 (rpl > dpl && cpl > dpl)))
1627 goto exception;
1628 break;
1629 }
1630
1631 if (seg_desc.s) {
1632 /* mark segment as accessed */
1633 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001634 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001635 if (ret != X86EMUL_CONTINUE)
1636 return ret;
1637 }
1638load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001639 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001640 return X86EMUL_CONTINUE;
1641exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001642 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001643 return X86EMUL_PROPAGATE_FAULT;
1644}
1645
Wei Yongjun31be40b2010-08-17 09:17:30 +08001646static void write_register_operand(struct operand *op)
1647{
1648 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1649 switch (op->bytes) {
1650 case 1:
1651 *(u8 *)op->addr.reg = (u8)op->val;
1652 break;
1653 case 2:
1654 *(u16 *)op->addr.reg = (u16)op->val;
1655 break;
1656 case 4:
1657 *op->addr.reg = (u32)op->val;
1658 break; /* 64b: zero-extend */
1659 case 8:
1660 *op->addr.reg = op->val;
1661 break;
1662 }
1663}
1664
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001665static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001666{
1667 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001668
Avi Kivityb6744dc2013-01-04 16:18:50 +02001669 if (ctxt->d & NoWrite)
1670 return X86EMUL_CONTINUE;
1671
Avi Kivity9dac77f2011-06-01 15:34:25 +03001672 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001673 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001674 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001675 break;
1676 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001677 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001678 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001679 ctxt->dst.addr.mem,
1680 &ctxt->dst.orig_val,
1681 &ctxt->dst.val,
1682 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001683 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001684 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001685 ctxt->dst.addr.mem,
1686 &ctxt->dst.val,
1687 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001688 if (rc != X86EMUL_CONTINUE)
1689 return rc;
1690 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001691 case OP_MEM_STR:
1692 rc = segmented_write(ctxt,
1693 ctxt->dst.addr.mem,
1694 ctxt->dst.data,
1695 ctxt->dst.bytes * ctxt->dst.count);
1696 if (rc != X86EMUL_CONTINUE)
1697 return rc;
1698 break;
Avi Kivity12537912011-03-29 11:41:27 +02001699 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001700 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001701 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001702 case OP_MM:
1703 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1704 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001705 case OP_NONE:
1706 /* no writeback */
1707 break;
1708 default:
1709 break;
1710 }
1711 return X86EMUL_CONTINUE;
1712}
1713
Avi Kivity51ddff52012-06-12 20:19:40 +03001714static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001715{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001716 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717
Avi Kivity5ad105e2012-08-19 14:34:31 +03001718 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001719 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001720 addr.seg = VCPU_SREG_SS;
1721
Avi Kivity51ddff52012-06-12 20:19:40 +03001722 return segmented_write(ctxt, addr, data, bytes);
1723}
1724
1725static int em_push(struct x86_emulate_ctxt *ctxt)
1726{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001727 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001728 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001729 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001730}
1731
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001732static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001733 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001734{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001735 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001736 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737
Avi Kivitydd856ef2012-08-27 23:46:17 +03001738 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001739 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001740 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001741 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001742 return rc;
1743
Avi Kivity5ad105e2012-08-19 14:34:31 +03001744 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001745 return rc;
1746}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001747
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001748static int em_pop(struct x86_emulate_ctxt *ctxt)
1749{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001750 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001751}
1752
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001753static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001754 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001755{
1756 int rc;
1757 unsigned long val, change_mask;
1758 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001759 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001760
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001761 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001762 if (rc != X86EMUL_CONTINUE)
1763 return rc;
1764
1765 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1766 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1767
1768 switch(ctxt->mode) {
1769 case X86EMUL_MODE_PROT64:
1770 case X86EMUL_MODE_PROT32:
1771 case X86EMUL_MODE_PROT16:
1772 if (cpl == 0)
1773 change_mask |= EFLG_IOPL;
1774 if (cpl <= iopl)
1775 change_mask |= EFLG_IF;
1776 break;
1777 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001778 if (iopl < 3)
1779 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001780 change_mask |= EFLG_IF;
1781 break;
1782 default: /* real mode */
1783 change_mask |= (EFLG_IOPL | EFLG_IF);
1784 break;
1785 }
1786
1787 *(unsigned long *)dest =
1788 (ctxt->eflags & ~change_mask) | (val & change_mask);
1789
1790 return rc;
1791}
1792
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001793static int em_popf(struct x86_emulate_ctxt *ctxt)
1794{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001795 ctxt->dst.type = OP_REG;
1796 ctxt->dst.addr.reg = &ctxt->eflags;
1797 ctxt->dst.bytes = ctxt->op_bytes;
1798 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001799}
1800
Avi Kivity612e89f2012-06-12 20:03:23 +03001801static int em_enter(struct x86_emulate_ctxt *ctxt)
1802{
1803 int rc;
1804 unsigned frame_size = ctxt->src.val;
1805 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001806 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001807
1808 if (nesting_level)
1809 return X86EMUL_UNHANDLEABLE;
1810
Avi Kivitydd856ef2012-08-27 23:46:17 +03001811 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1812 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001813 if (rc != X86EMUL_CONTINUE)
1814 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001815 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001816 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001817 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1818 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001819 stack_mask(ctxt));
1820 return X86EMUL_CONTINUE;
1821}
1822
Avi Kivityf47cfa32012-06-07 17:49:24 +03001823static int em_leave(struct x86_emulate_ctxt *ctxt)
1824{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001825 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001826 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001827 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001828}
1829
Avi Kivity1cd196e2011-09-13 10:45:51 +03001830static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001831{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001832 int seg = ctxt->src2.val;
1833
Avi Kivity9dac77f2011-06-01 15:34:25 +03001834 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001835
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001836 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001837}
1838
Avi Kivity1cd196e2011-09-13 10:45:51 +03001839static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001840{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001841 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001842 unsigned long selector;
1843 int rc;
1844
Avi Kivity9dac77f2011-06-01 15:34:25 +03001845 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001846 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001847 return rc;
1848
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001849 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001850 return rc;
1851}
1852
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001853static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001854{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001855 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001856 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001857 int reg = VCPU_REGS_RAX;
1858
1859 while (reg <= VCPU_REGS_RDI) {
1860 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001861 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001862
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001863 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001864 if (rc != X86EMUL_CONTINUE)
1865 return rc;
1866
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001867 ++reg;
1868 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001869
Wei Yongjunc37eda12010-06-15 09:03:33 +08001870 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001871}
1872
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001873static int em_pushf(struct x86_emulate_ctxt *ctxt)
1874{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001875 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001876 return em_push(ctxt);
1877}
1878
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001879static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001880{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001881 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001882 int reg = VCPU_REGS_RDI;
1883
1884 while (reg >= VCPU_REGS_RAX) {
1885 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001886 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001887 --reg;
1888 }
1889
Avi Kivitydd856ef2012-08-27 23:46:17 +03001890 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001891 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001892 break;
1893 --reg;
1894 }
1895 return rc;
1896}
1897
Avi Kivitydd856ef2012-08-27 23:46:17 +03001898static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001899{
Mathias Krause0225fb52012-08-30 01:30:16 +02001900 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001901 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001902 struct desc_ptr dt;
1903 gva_t cs_addr;
1904 gva_t eip_addr;
1905 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001906
1907 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001908 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001909 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001910 if (rc != X86EMUL_CONTINUE)
1911 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001912
1913 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1914
Avi Kivity9dac77f2011-06-01 15:34:25 +03001915 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001916 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001917 if (rc != X86EMUL_CONTINUE)
1918 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001919
Avi Kivity9dac77f2011-06-01 15:34:25 +03001920 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001921 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001922 if (rc != X86EMUL_CONTINUE)
1923 return rc;
1924
Avi Kivity4bff1e862011-04-20 13:37:53 +03001925 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001926
1927 eip_addr = dt.address + (irq << 2);
1928 cs_addr = dt.address + (irq << 2) + 2;
1929
Avi Kivity0f65dd72011-04-20 13:37:53 +03001930 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001931 if (rc != X86EMUL_CONTINUE)
1932 return rc;
1933
Avi Kivity0f65dd72011-04-20 13:37:53 +03001934 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001935 if (rc != X86EMUL_CONTINUE)
1936 return rc;
1937
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001938 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001939 if (rc != X86EMUL_CONTINUE)
1940 return rc;
1941
Avi Kivity9dac77f2011-06-01 15:34:25 +03001942 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001943
1944 return rc;
1945}
1946
Avi Kivitydd856ef2012-08-27 23:46:17 +03001947int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1948{
1949 int rc;
1950
1951 invalidate_registers(ctxt);
1952 rc = __emulate_int_real(ctxt, irq);
1953 if (rc == X86EMUL_CONTINUE)
1954 writeback_registers(ctxt);
1955 return rc;
1956}
1957
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001958static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001959{
1960 switch(ctxt->mode) {
1961 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001962 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001963 case X86EMUL_MODE_VM86:
1964 case X86EMUL_MODE_PROT16:
1965 case X86EMUL_MODE_PROT32:
1966 case X86EMUL_MODE_PROT64:
1967 default:
1968 /* Protected mode interrupts unimplemented yet */
1969 return X86EMUL_UNHANDLEABLE;
1970 }
1971}
1972
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001973static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001974{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001975 int rc = X86EMUL_CONTINUE;
1976 unsigned long temp_eip = 0;
1977 unsigned long temp_eflags = 0;
1978 unsigned long cs = 0;
1979 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1980 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1981 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1982 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1983
1984 /* TODO: Add stack limit check */
1985
Avi Kivity9dac77f2011-06-01 15:34:25 +03001986 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001987
1988 if (rc != X86EMUL_CONTINUE)
1989 return rc;
1990
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001991 if (temp_eip & ~0xffff)
1992 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001993
Avi Kivity9dac77f2011-06-01 15:34:25 +03001994 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001995
1996 if (rc != X86EMUL_CONTINUE)
1997 return rc;
1998
Avi Kivity9dac77f2011-06-01 15:34:25 +03001999 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002000
2001 if (rc != X86EMUL_CONTINUE)
2002 return rc;
2003
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002004 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002005
2006 if (rc != X86EMUL_CONTINUE)
2007 return rc;
2008
Avi Kivity9dac77f2011-06-01 15:34:25 +03002009 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002010
2011
Avi Kivity9dac77f2011-06-01 15:34:25 +03002012 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002013 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002014 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002015 ctxt->eflags &= ~0xffff;
2016 ctxt->eflags |= temp_eflags;
2017 }
2018
2019 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2020 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2021
2022 return rc;
2023}
2024
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002025static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002026{
2027 switch(ctxt->mode) {
2028 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002029 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002030 case X86EMUL_MODE_VM86:
2031 case X86EMUL_MODE_PROT16:
2032 case X86EMUL_MODE_PROT32:
2033 case X86EMUL_MODE_PROT64:
2034 default:
2035 /* iret from protected mode unimplemented yet */
2036 return X86EMUL_UNHANDLEABLE;
2037 }
2038}
2039
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002040static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2041{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002042 int rc;
2043 unsigned short sel;
2044
Avi Kivity9dac77f2011-06-01 15:34:25 +03002045 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002046
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002047 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002048 if (rc != X86EMUL_CONTINUE)
2049 return rc;
2050
Avi Kivity9dac77f2011-06-01 15:34:25 +03002051 ctxt->_eip = 0;
2052 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002053 return X86EMUL_CONTINUE;
2054}
2055
Avi Kivity45a14672013-01-04 16:18:52 +02002056FASTOP1(not);
2057FASTOP1(neg);
Avi Kivity95413dc2013-01-19 19:51:53 +02002058FASTOP1(inc);
2059FASTOP1(dec);
Avi Kivity3329ece2011-09-13 10:45:39 +03002060
Avi Kivity007a3b52013-01-19 19:51:51 +02002061FASTOP2CL(rol);
2062FASTOP2CL(ror);
2063FASTOP2CL(rcl);
2064FASTOP2CL(rcr);
2065FASTOP2CL(shl);
2066FASTOP2CL(shr);
2067FASTOP2CL(sar);
2068
Avi Kivity3329ece2011-09-13 10:45:39 +03002069static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2070{
2071 u8 ex = 0;
2072
2073 emulate_1op_rax_rdx(ctxt, "mul", ex);
2074 return X86EMUL_CONTINUE;
2075}
2076
2077static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2078{
2079 u8 ex = 0;
2080
2081 emulate_1op_rax_rdx(ctxt, "imul", ex);
2082 return X86EMUL_CONTINUE;
2083}
2084
2085static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002086{
Avi Kivity34d1f492010-08-26 11:59:01 +03002087 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002088
Avi Kivity3329ece2011-09-13 10:45:39 +03002089 emulate_1op_rax_rdx(ctxt, "div", de);
2090 if (de)
2091 return emulate_de(ctxt);
2092 return X86EMUL_CONTINUE;
2093}
2094
2095static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2096{
2097 u8 de = 0;
2098
2099 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002100 if (de)
2101 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002102 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002103}
2104
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002105static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002106{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002107 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002108
Avi Kivity9dac77f2011-06-01 15:34:25 +03002109 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002110 case 2: /* call near abs */ {
2111 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002112 old_eip = ctxt->_eip;
2113 ctxt->_eip = ctxt->src.val;
2114 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002115 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002116 break;
2117 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002118 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002119 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002120 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002121 case 5: /* jmp far */
2122 rc = em_jmp_far(ctxt);
2123 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002125 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002126 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002127 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002128 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002129}
2130
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002131static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002132{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002133 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002134
Avi Kivitydd856ef2012-08-27 23:46:17 +03002135 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2136 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2137 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2138 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002139 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002140 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002141 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2142 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002143
Laurent Vivier05f086f2007-09-24 11:10:55 +02002144 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002146 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002147}
2148
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002149static int em_ret(struct x86_emulate_ctxt *ctxt)
2150{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002151 ctxt->dst.type = OP_REG;
2152 ctxt->dst.addr.reg = &ctxt->_eip;
2153 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002154 return em_pop(ctxt);
2155}
2156
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002157static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002158{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002159 int rc;
2160 unsigned long cs;
2161
Avi Kivity9dac77f2011-06-01 15:34:25 +03002162 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002163 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002164 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002165 if (ctxt->op_bytes == 4)
2166 ctxt->_eip = (u32)ctxt->_eip;
2167 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002168 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002169 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002170 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002171 return rc;
2172}
2173
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002174static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2175{
2176 /* Save real source value, then compare EAX against destination. */
2177 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002178 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002179 emulate_2op_SrcV(ctxt, "cmp");
2180
2181 if (ctxt->eflags & EFLG_ZF) {
2182 /* Success: write back to memory. */
2183 ctxt->dst.val = ctxt->src.orig_val;
2184 } else {
2185 /* Failure: write the value we saw to EAX. */
2186 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002187 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002188 }
2189 return X86EMUL_CONTINUE;
2190}
2191
Avi Kivityd4b43252011-09-13 10:45:50 +03002192static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002193{
Avi Kivityd4b43252011-09-13 10:45:50 +03002194 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002195 unsigned short sel;
2196 int rc;
2197
Avi Kivity9dac77f2011-06-01 15:34:25 +03002198 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002199
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002200 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002201 if (rc != X86EMUL_CONTINUE)
2202 return rc;
2203
Avi Kivity9dac77f2011-06-01 15:34:25 +03002204 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002205 return rc;
2206}
2207
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002208static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002209setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002210 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002211{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002212 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002213 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002214 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002215 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002216 cs->type = 0x0b; /* Read, Execute, Accessed */
2217 cs->s = 1;
2218 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002219 cs->p = 1;
2220 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002221 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002222
Gleb Natapov79168fd2010-04-28 19:15:30 +03002223 set_desc_base(ss, 0); /* flat segment */
2224 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002225 ss->g = 1; /* 4kb granularity */
2226 ss->s = 1;
2227 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002228 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002229 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002230 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002231 ss->l = 0;
2232 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002233}
2234
Avi Kivity1a18a692012-02-01 12:23:21 +02002235static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2236{
2237 u32 eax, ebx, ecx, edx;
2238
2239 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002240 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2241 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002242 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2243 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2244}
2245
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002246static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2247{
Mathias Krause0225fb52012-08-30 01:30:16 +02002248 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002249 u32 eax, ebx, ecx, edx;
2250
2251 /*
2252 * syscall should always be enabled in longmode - so only become
2253 * vendor specific (cpuid) if other modes are active...
2254 */
2255 if (ctxt->mode == X86EMUL_MODE_PROT64)
2256 return true;
2257
2258 eax = 0x00000000;
2259 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002260 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2261 /*
2262 * Intel ("GenuineIntel")
2263 * remark: Intel CPUs only support "syscall" in 64bit
2264 * longmode. Also an 64bit guest with a
2265 * 32bit compat-app running will #UD !! While this
2266 * behaviour can be fixed (by emulating) into AMD
2267 * response - CPUs of AMD can't behave like Intel.
2268 */
2269 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2270 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2271 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2272 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002273
Avi Kivity0017f932012-06-07 14:10:16 +03002274 /* AMD ("AuthenticAMD") */
2275 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2276 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2277 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2278 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002279
Avi Kivity0017f932012-06-07 14:10:16 +03002280 /* AMD ("AMDisbetter!") */
2281 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2282 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2283 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2284 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002285
2286 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2287 return false;
2288}
2289
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002290static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002291{
Mathias Krause0225fb52012-08-30 01:30:16 +02002292 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002293 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002294 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002295 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002296 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002297
2298 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002299 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002300 ctxt->mode == X86EMUL_MODE_VM86)
2301 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002302
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002303 if (!(em_syscall_is_enabled(ctxt)))
2304 return emulate_ud(ctxt);
2305
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002306 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002307 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002308
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002309 if (!(efer & EFER_SCE))
2310 return emulate_ud(ctxt);
2311
Avi Kivity717746e2011-04-20 13:37:53 +03002312 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002313 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002314 cs_sel = (u16)(msr_data & 0xfffc);
2315 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002316
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002317 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002318 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002319 cs.l = 1;
2320 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002321 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2322 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002323
Avi Kivitydd856ef2012-08-27 23:46:17 +03002324 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002325 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002326#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002327 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002328
Avi Kivity717746e2011-04-20 13:37:53 +03002329 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002330 ctxt->mode == X86EMUL_MODE_PROT64 ?
2331 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002332 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002333
Avi Kivity717746e2011-04-20 13:37:53 +03002334 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002335 ctxt->eflags &= ~(msr_data | EFLG_RF);
2336#endif
2337 } else {
2338 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002339 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002340 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002341
2342 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2343 }
2344
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002345 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002346}
2347
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002348static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002349{
Mathias Krause0225fb52012-08-30 01:30:16 +02002350 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002351 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002352 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002353 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002354 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002355
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002356 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002357 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002358 if (ctxt->mode == X86EMUL_MODE_REAL)
2359 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002360
Avi Kivity1a18a692012-02-01 12:23:21 +02002361 /*
2362 * Not recognized on AMD in compat mode (but is recognized in legacy
2363 * mode).
2364 */
2365 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2366 && !vendor_intel(ctxt))
2367 return emulate_ud(ctxt);
2368
Andre Przywara8c604352009-06-18 12:56:01 +02002369 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2370 * Therefore, we inject an #UD.
2371 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002372 if (ctxt->mode == X86EMUL_MODE_PROT64)
2373 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002374
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002375 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002376
Avi Kivity717746e2011-04-20 13:37:53 +03002377 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002378 switch (ctxt->mode) {
2379 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002380 if ((msr_data & 0xfffc) == 0x0)
2381 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002382 break;
2383 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002384 if (msr_data == 0x0)
2385 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002386 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002387 default:
2388 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002389 }
2390
2391 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002392 cs_sel = (u16)msr_data;
2393 cs_sel &= ~SELECTOR_RPL_MASK;
2394 ss_sel = cs_sel + 8;
2395 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002396 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002397 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002398 cs.l = 1;
2399 }
2400
Avi Kivity1aa36612011-04-27 13:20:30 +03002401 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2402 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002403
Avi Kivity717746e2011-04-20 13:37:53 +03002404 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002405 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002406
Avi Kivity717746e2011-04-20 13:37:53 +03002407 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002408 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002409
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002410 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002411}
2412
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002413static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002414{
Mathias Krause0225fb52012-08-30 01:30:16 +02002415 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002416 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002417 u64 msr_data;
2418 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002419 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002420
Gleb Natapova0044752010-02-10 14:21:31 +02002421 /* inject #GP if in real mode or Virtual 8086 mode */
2422 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002423 ctxt->mode == X86EMUL_MODE_VM86)
2424 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002425
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002426 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002427
Avi Kivity9dac77f2011-06-01 15:34:25 +03002428 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002429 usermode = X86EMUL_MODE_PROT64;
2430 else
2431 usermode = X86EMUL_MODE_PROT32;
2432
2433 cs.dpl = 3;
2434 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002435 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002436 switch (usermode) {
2437 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002438 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002439 if ((msr_data & 0xfffc) == 0x0)
2440 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002441 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002442 break;
2443 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002444 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002445 if (msr_data == 0x0)
2446 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002447 ss_sel = cs_sel + 8;
2448 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002449 cs.l = 1;
2450 break;
2451 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002452 cs_sel |= SELECTOR_RPL_MASK;
2453 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002454
Avi Kivity1aa36612011-04-27 13:20:30 +03002455 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2456 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002457
Avi Kivitydd856ef2012-08-27 23:46:17 +03002458 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2459 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002460
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002461 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002462}
2463
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002464static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002465{
2466 int iopl;
2467 if (ctxt->mode == X86EMUL_MODE_REAL)
2468 return false;
2469 if (ctxt->mode == X86EMUL_MODE_VM86)
2470 return true;
2471 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002472 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002473}
2474
2475static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002476 u16 port, u16 len)
2477{
Mathias Krause0225fb52012-08-30 01:30:16 +02002478 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002479 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002480 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002481 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002482 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002483 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002484 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002485
Avi Kivity1aa36612011-04-27 13:20:30 +03002486 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002487 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002488 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002489 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002490 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002491 base = get_desc_base(&tr_seg);
2492#ifdef CONFIG_X86_64
2493 base |= ((u64)base3) << 32;
2494#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002495 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002496 if (r != X86EMUL_CONTINUE)
2497 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002498 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002499 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002500 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002501 if (r != X86EMUL_CONTINUE)
2502 return false;
2503 if ((perm >> bit_idx) & mask)
2504 return false;
2505 return true;
2506}
2507
2508static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002509 u16 port, u16 len)
2510{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002511 if (ctxt->perm_ok)
2512 return true;
2513
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002514 if (emulator_bad_iopl(ctxt))
2515 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002516 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002517
2518 ctxt->perm_ok = true;
2519
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002520 return true;
2521}
2522
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002523static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002524 struct tss_segment_16 *tss)
2525{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002526 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002527 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002528 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2529 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2530 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2531 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2532 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2533 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2534 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2535 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002536
Avi Kivity1aa36612011-04-27 13:20:30 +03002537 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2538 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2539 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2540 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2541 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002542}
2543
2544static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002545 struct tss_segment_16 *tss)
2546{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002547 int ret;
2548
Avi Kivity9dac77f2011-06-01 15:34:25 +03002549 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002550 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002551 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2552 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2553 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2554 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2555 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2556 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2557 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2558 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002559
2560 /*
2561 * SDM says that segment selectors are loaded before segment
2562 * descriptors
2563 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002564 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2565 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2566 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2567 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2568 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002569
2570 /*
Guo Chaofc058682012-06-28 15:19:51 +08002571 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002572 * it is handled in a context of new task
2573 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002574 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575 if (ret != X86EMUL_CONTINUE)
2576 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002577 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 if (ret != X86EMUL_CONTINUE)
2579 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002580 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581 if (ret != X86EMUL_CONTINUE)
2582 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002583 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584 if (ret != X86EMUL_CONTINUE)
2585 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002586 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 if (ret != X86EMUL_CONTINUE)
2588 return ret;
2589
2590 return X86EMUL_CONTINUE;
2591}
2592
2593static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 u16 tss_selector, u16 old_tss_sel,
2595 ulong old_tss_base, struct desc_struct *new_desc)
2596{
Mathias Krause0225fb52012-08-30 01:30:16 +02002597 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002598 struct tss_segment_16 tss_seg;
2599 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002600 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002601
Avi Kivity0f65dd72011-04-20 13:37:53 +03002602 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002603 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002604 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002605 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002606 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002608 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002609
Avi Kivity0f65dd72011-04-20 13:37:53 +03002610 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002611 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002612 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615
Avi Kivity0f65dd72011-04-20 13:37:53 +03002616 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002617 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002618 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621
2622 if (old_tss_sel != 0xffff) {
2623 tss_seg.prev_task_link = old_tss_sel;
2624
Avi Kivity0f65dd72011-04-20 13:37:53 +03002625 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 &tss_seg.prev_task_link,
2627 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002628 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002629 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 }
2633
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002634 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635}
2636
2637static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 struct tss_segment_32 *tss)
2639{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002640 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002641 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002643 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2644 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2645 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2646 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2647 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2648 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2649 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2650 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651
Avi Kivity1aa36612011-04-27 13:20:30 +03002652 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2653 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2654 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2655 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2656 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2657 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2658 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002659}
2660
2661static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002662 struct tss_segment_32 *tss)
2663{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002664 int ret;
2665
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002666 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002667 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002668 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002669 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002670
2671 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002672 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2673 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2674 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2675 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2676 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2677 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2678 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2679 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680
2681 /*
2682 * SDM says that segment selectors are loaded before segment
2683 * descriptors
2684 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002685 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2686 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2687 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2688 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2689 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2690 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2691 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002692
2693 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002694 * If we're switching between Protected Mode and VM86, we need to make
2695 * sure to update the mode before loading the segment descriptors so
2696 * that the selectors are interpreted correctly.
2697 *
2698 * Need to get rflags to the vcpu struct immediately because it
2699 * influences the CPL which is checked at least when loading the segment
2700 * descriptors and when pushing an error code to the new kernel stack.
2701 *
2702 * TODO Introduce a separate ctxt->ops->set_cpl callback
2703 */
2704 if (ctxt->eflags & X86_EFLAGS_VM)
2705 ctxt->mode = X86EMUL_MODE_VM86;
2706 else
2707 ctxt->mode = X86EMUL_MODE_PROT32;
2708
2709 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2710
2711 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002712 * Now load segment descriptors. If fault happenes at this stage
2713 * it is handled in a context of new task
2714 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002715 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002716 if (ret != X86EMUL_CONTINUE)
2717 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002718 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002719 if (ret != X86EMUL_CONTINUE)
2720 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002721 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722 if (ret != X86EMUL_CONTINUE)
2723 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002724 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725 if (ret != X86EMUL_CONTINUE)
2726 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002727 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 if (ret != X86EMUL_CONTINUE)
2729 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002730 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731 if (ret != X86EMUL_CONTINUE)
2732 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002733 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002734 if (ret != X86EMUL_CONTINUE)
2735 return ret;
2736
2737 return X86EMUL_CONTINUE;
2738}
2739
2740static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741 u16 tss_selector, u16 old_tss_sel,
2742 ulong old_tss_base, struct desc_struct *new_desc)
2743{
Mathias Krause0225fb52012-08-30 01:30:16 +02002744 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002745 struct tss_segment_32 tss_seg;
2746 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002747 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002748
Avi Kivity0f65dd72011-04-20 13:37:53 +03002749 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002750 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002751 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002752 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002753 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002755 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002756
Avi Kivity0f65dd72011-04-20 13:37:53 +03002757 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002758 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002759 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762
Avi Kivity0f65dd72011-04-20 13:37:53 +03002763 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002764 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002765 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768
2769 if (old_tss_sel != 0xffff) {
2770 tss_seg.prev_task_link = old_tss_sel;
2771
Avi Kivity0f65dd72011-04-20 13:37:53 +03002772 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 &tss_seg.prev_task_link,
2774 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002775 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002776 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 }
2780
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002781 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782}
2783
2784static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002785 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002786 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002787{
Mathias Krause0225fb52012-08-30 01:30:16 +02002788 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789 struct desc_struct curr_tss_desc, next_tss_desc;
2790 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002791 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002792 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002793 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002794 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002795 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002796
2797 /* FIXME: old_tss_base == ~0 ? */
2798
Avi Kivitye9194642012-06-13 16:29:39 +03002799 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002800 if (ret != X86EMUL_CONTINUE)
2801 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002802 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803 if (ret != X86EMUL_CONTINUE)
2804 return ret;
2805
2806 /* FIXME: check that next_tss_desc is tss */
2807
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002808 /*
2809 * Check privileges. The three cases are task switch caused by...
2810 *
2811 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2812 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002813 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002814 */
2815 if (reason == TASK_SWITCH_GATE) {
2816 if (idt_index != -1) {
2817 /* Software interrupts */
2818 struct desc_struct task_gate_desc;
2819 int dpl;
2820
2821 ret = read_interrupt_descriptor(ctxt, idt_index,
2822 &task_gate_desc);
2823 if (ret != X86EMUL_CONTINUE)
2824 return ret;
2825
2826 dpl = task_gate_desc.dpl;
2827 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2828 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2829 }
2830 } else if (reason != TASK_SWITCH_IRET) {
2831 int dpl = next_tss_desc.dpl;
2832 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2833 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002834 }
2835
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002836
Gleb Natapovceffb452010-03-18 15:20:19 +02002837 desc_limit = desc_limit_scaled(&next_tss_desc);
2838 if (!next_tss_desc.p ||
2839 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2840 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002841 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002842 return X86EMUL_PROPAGATE_FAULT;
2843 }
2844
2845 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2846 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002847 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002848 }
2849
2850 if (reason == TASK_SWITCH_IRET)
2851 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2852
2853 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002854 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002855 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2856 old_tss_sel = 0xffff;
2857
2858 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002859 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002860 old_tss_base, &next_tss_desc);
2861 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002862 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002863 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002864 if (ret != X86EMUL_CONTINUE)
2865 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002866
2867 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2868 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2869
2870 if (reason != TASK_SWITCH_IRET) {
2871 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002872 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002873 }
2874
Avi Kivity717746e2011-04-20 13:37:53 +03002875 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002876 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002877
Jan Kiszkae269fb22010-04-14 15:51:09 +02002878 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002879 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2880 ctxt->lock_prefix = 0;
2881 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002882 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002883 }
2884
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002885 return ret;
2886}
2887
2888int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002889 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002890 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002891{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002892 int rc;
2893
Avi Kivitydd856ef2012-08-27 23:46:17 +03002894 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002895 ctxt->_eip = ctxt->eip;
2896 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002897
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002898 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002899 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002900
Avi Kivitydd856ef2012-08-27 23:46:17 +03002901 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002902 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002903 writeback_registers(ctxt);
2904 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002905
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002906 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002907}
2908
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002909static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2910 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002911{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002912 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002913
Avi Kivitydd856ef2012-08-27 23:46:17 +03002914 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2915 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002916}
2917
Avi Kivity7af04fc2010-08-18 14:16:35 +03002918static int em_das(struct x86_emulate_ctxt *ctxt)
2919{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002920 u8 al, old_al;
2921 bool af, cf, old_cf;
2922
2923 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002924 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002925
2926 old_al = al;
2927 old_cf = cf;
2928 cf = false;
2929 af = ctxt->eflags & X86_EFLAGS_AF;
2930 if ((al & 0x0f) > 9 || af) {
2931 al -= 6;
2932 cf = old_cf | (al >= 250);
2933 af = true;
2934 } else {
2935 af = false;
2936 }
2937 if (old_al > 0x99 || old_cf) {
2938 al -= 0x60;
2939 cf = true;
2940 }
2941
Avi Kivity9dac77f2011-06-01 15:34:25 +03002942 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002943 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002944 ctxt->src.type = OP_IMM;
2945 ctxt->src.val = 0;
2946 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002947 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002948 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2949 if (cf)
2950 ctxt->eflags |= X86_EFLAGS_CF;
2951 if (af)
2952 ctxt->eflags |= X86_EFLAGS_AF;
2953 return X86EMUL_CONTINUE;
2954}
2955
Gleb Natapov7f662272012-12-10 11:42:30 +02002956static int em_aad(struct x86_emulate_ctxt *ctxt)
2957{
2958 u8 al = ctxt->dst.val & 0xff;
2959 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2960
2961 al = (al + (ah * ctxt->src.val)) & 0xff;
2962
2963 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2964
2965 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2966
2967 if (!al)
2968 ctxt->eflags |= X86_EFLAGS_ZF;
2969 if (!(al & 1))
2970 ctxt->eflags |= X86_EFLAGS_PF;
2971 if (al & 0x80)
2972 ctxt->eflags |= X86_EFLAGS_SF;
2973
2974 return X86EMUL_CONTINUE;
2975}
2976
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002977static int em_call(struct x86_emulate_ctxt *ctxt)
2978{
2979 long rel = ctxt->src.val;
2980
2981 ctxt->src.val = (unsigned long)ctxt->_eip;
2982 jmp_rel(ctxt, rel);
2983 return em_push(ctxt);
2984}
2985
Avi Kivity0ef753b2010-08-18 14:51:45 +03002986static int em_call_far(struct x86_emulate_ctxt *ctxt)
2987{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002988 u16 sel, old_cs;
2989 ulong old_eip;
2990 int rc;
2991
Avi Kivity1aa36612011-04-27 13:20:30 +03002992 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002993 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03002994
Avi Kivity9dac77f2011-06-01 15:34:25 +03002995 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002996 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03002997 return X86EMUL_CONTINUE;
2998
Avi Kivity9dac77f2011-06-01 15:34:25 +03002999 ctxt->_eip = 0;
3000 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003001
Avi Kivity9dac77f2011-06-01 15:34:25 +03003002 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003003 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003004 if (rc != X86EMUL_CONTINUE)
3005 return rc;
3006
Avi Kivity9dac77f2011-06-01 15:34:25 +03003007 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003008 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003009}
3010
Avi Kivity40ece7c2010-08-18 15:12:09 +03003011static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3012{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003013 int rc;
3014
Avi Kivity9dac77f2011-06-01 15:34:25 +03003015 ctxt->dst.type = OP_REG;
3016 ctxt->dst.addr.reg = &ctxt->_eip;
3017 ctxt->dst.bytes = ctxt->op_bytes;
3018 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003019 if (rc != X86EMUL_CONTINUE)
3020 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003021 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003022 return X86EMUL_CONTINUE;
3023}
3024
Avi Kivityfb864fb2013-01-04 16:18:54 +02003025FASTOP2(add);
3026FASTOP2(or);
3027FASTOP2(adc);
3028FASTOP2(sbb);
3029FASTOP2(and);
3030FASTOP2(sub);
3031FASTOP2(xor);
3032FASTOP2(cmp);
3033FASTOP2(test);
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003034
Avi Kivity0bdea062013-01-19 19:51:50 +02003035FASTOP3WCL(shld);
3036FASTOP3WCL(shrd);
3037
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003038static int em_xchg(struct x86_emulate_ctxt *ctxt)
3039{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003040 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003041 ctxt->src.val = ctxt->dst.val;
3042 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003043
3044 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003045 ctxt->dst.val = ctxt->src.orig_val;
3046 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003047 return X86EMUL_CONTINUE;
3048}
3049
Avi Kivity5c82aa22010-08-18 18:31:43 +03003050static int em_imul(struct x86_emulate_ctxt *ctxt)
3051{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003052 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03003053 return X86EMUL_CONTINUE;
3054}
3055
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003056static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3057{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003058 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03003059 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003060}
3061
Avi Kivity61429142010-08-19 15:13:00 +03003062static int em_cwd(struct x86_emulate_ctxt *ctxt)
3063{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003064 ctxt->dst.type = OP_REG;
3065 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003066 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003067 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003068
3069 return X86EMUL_CONTINUE;
3070}
3071
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03003072static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3073{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03003074 u64 tsc = 0;
3075
Avi Kivity717746e2011-04-20 13:37:53 +03003076 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003077 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3078 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03003079 return X86EMUL_CONTINUE;
3080}
3081
Avi Kivity222d21a2011-11-10 14:57:30 +02003082static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3083{
3084 u64 pmc;
3085
Avi Kivitydd856ef2012-08-27 23:46:17 +03003086 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003087 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003088 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3089 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003090 return X86EMUL_CONTINUE;
3091}
3092
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003093static int em_mov(struct x86_emulate_ctxt *ctxt)
3094{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003095 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003096 return X86EMUL_CONTINUE;
3097}
3098
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003099static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3100{
3101 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3102 return emulate_gp(ctxt, 0);
3103
3104 /* Disable writeback. */
3105 ctxt->dst.type = OP_NONE;
3106 return X86EMUL_CONTINUE;
3107}
3108
3109static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3110{
3111 unsigned long val;
3112
3113 if (ctxt->mode == X86EMUL_MODE_PROT64)
3114 val = ctxt->src.val & ~0ULL;
3115 else
3116 val = ctxt->src.val & ~0U;
3117
3118 /* #UD condition is already handled. */
3119 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3120 return emulate_gp(ctxt, 0);
3121
3122 /* Disable writeback. */
3123 ctxt->dst.type = OP_NONE;
3124 return X86EMUL_CONTINUE;
3125}
3126
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003127static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3128{
3129 u64 msr_data;
3130
Avi Kivitydd856ef2012-08-27 23:46:17 +03003131 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3132 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3133 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003134 return emulate_gp(ctxt, 0);
3135
3136 return X86EMUL_CONTINUE;
3137}
3138
3139static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3140{
3141 u64 msr_data;
3142
Avi Kivitydd856ef2012-08-27 23:46:17 +03003143 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003144 return emulate_gp(ctxt, 0);
3145
Avi Kivitydd856ef2012-08-27 23:46:17 +03003146 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3147 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003148 return X86EMUL_CONTINUE;
3149}
3150
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003151static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3152{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003153 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003154 return emulate_ud(ctxt);
3155
Avi Kivity9dac77f2011-06-01 15:34:25 +03003156 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003157 return X86EMUL_CONTINUE;
3158}
3159
3160static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3161{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003162 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003163
Avi Kivity9dac77f2011-06-01 15:34:25 +03003164 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003165 return emulate_ud(ctxt);
3166
Avi Kivity9dac77f2011-06-01 15:34:25 +03003167 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003168 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3169
3170 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003171 ctxt->dst.type = OP_NONE;
3172 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003173}
3174
Avi Kivitya14e5792012-06-13 12:28:33 +03003175static int em_lldt(struct x86_emulate_ctxt *ctxt)
3176{
3177 u16 sel = ctxt->src.val;
3178
3179 /* Disable writeback. */
3180 ctxt->dst.type = OP_NONE;
3181 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3182}
3183
Avi Kivity80890002012-06-13 16:33:29 +03003184static int em_ltr(struct x86_emulate_ctxt *ctxt)
3185{
3186 u16 sel = ctxt->src.val;
3187
3188 /* Disable writeback. */
3189 ctxt->dst.type = OP_NONE;
3190 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3191}
3192
Avi Kivity38503912011-03-31 18:48:09 +02003193static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3194{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003195 int rc;
3196 ulong linear;
3197
Avi Kivity9dac77f2011-06-01 15:34:25 +03003198 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003199 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003200 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003201 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003202 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003203 return X86EMUL_CONTINUE;
3204}
3205
Avi Kivity2d04a052011-04-20 15:32:49 +03003206static int em_clts(struct x86_emulate_ctxt *ctxt)
3207{
3208 ulong cr0;
3209
3210 cr0 = ctxt->ops->get_cr(ctxt, 0);
3211 cr0 &= ~X86_CR0_TS;
3212 ctxt->ops->set_cr(ctxt, 0, cr0);
3213 return X86EMUL_CONTINUE;
3214}
3215
Avi Kivity26d05cc2011-04-21 12:07:59 +03003216static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3217{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003218 int rc;
3219
Avi Kivity9dac77f2011-06-01 15:34:25 +03003220 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003221 return X86EMUL_UNHANDLEABLE;
3222
3223 rc = ctxt->ops->fix_hypercall(ctxt);
3224 if (rc != X86EMUL_CONTINUE)
3225 return rc;
3226
3227 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003228 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003229 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003230 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003231 return X86EMUL_CONTINUE;
3232}
3233
Avi Kivity96051572012-06-10 17:21:18 +03003234static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3235 void (*get)(struct x86_emulate_ctxt *ctxt,
3236 struct desc_ptr *ptr))
3237{
3238 struct desc_ptr desc_ptr;
3239
3240 if (ctxt->mode == X86EMUL_MODE_PROT64)
3241 ctxt->op_bytes = 8;
3242 get(ctxt, &desc_ptr);
3243 if (ctxt->op_bytes == 2) {
3244 ctxt->op_bytes = 4;
3245 desc_ptr.address &= 0x00ffffff;
3246 }
3247 /* Disable writeback. */
3248 ctxt->dst.type = OP_NONE;
3249 return segmented_write(ctxt, ctxt->dst.addr.mem,
3250 &desc_ptr, 2 + ctxt->op_bytes);
3251}
3252
3253static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3254{
3255 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3256}
3257
3258static int em_sidt(struct x86_emulate_ctxt *ctxt)
3259{
3260 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3261}
3262
Avi Kivity26d05cc2011-04-21 12:07:59 +03003263static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3264{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003265 struct desc_ptr desc_ptr;
3266 int rc;
3267
Avi Kivity510425f2012-06-07 17:04:36 +03003268 if (ctxt->mode == X86EMUL_MODE_PROT64)
3269 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003270 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003271 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003272 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003273 if (rc != X86EMUL_CONTINUE)
3274 return rc;
3275 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3276 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003277 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003278 return X86EMUL_CONTINUE;
3279}
3280
Avi Kivity5ef39c72011-04-21 12:21:50 +03003281static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003282{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003283 int rc;
3284
Avi Kivity5ef39c72011-04-21 12:21:50 +03003285 rc = ctxt->ops->fix_hypercall(ctxt);
3286
Avi Kivity26d05cc2011-04-21 12:07:59 +03003287 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003288 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003289 return rc;
3290}
3291
3292static int em_lidt(struct x86_emulate_ctxt *ctxt)
3293{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003294 struct desc_ptr desc_ptr;
3295 int rc;
3296
Avi Kivity510425f2012-06-07 17:04:36 +03003297 if (ctxt->mode == X86EMUL_MODE_PROT64)
3298 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003299 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003300 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003301 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003302 if (rc != X86EMUL_CONTINUE)
3303 return rc;
3304 ctxt->ops->set_idt(ctxt, &desc_ptr);
3305 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003306 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003307 return X86EMUL_CONTINUE;
3308}
3309
3310static int em_smsw(struct x86_emulate_ctxt *ctxt)
3311{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003312 ctxt->dst.bytes = 2;
3313 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003314 return X86EMUL_CONTINUE;
3315}
3316
3317static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3318{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003319 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003320 | (ctxt->src.val & 0x0f));
3321 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003322 return X86EMUL_CONTINUE;
3323}
3324
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003325static int em_loop(struct x86_emulate_ctxt *ctxt)
3326{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003327 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3328 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003329 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3330 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003331
3332 return X86EMUL_CONTINUE;
3333}
3334
3335static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3336{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003337 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003338 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003339
3340 return X86EMUL_CONTINUE;
3341}
3342
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003343static int em_in(struct x86_emulate_ctxt *ctxt)
3344{
3345 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3346 &ctxt->dst.val))
3347 return X86EMUL_IO_NEEDED;
3348
3349 return X86EMUL_CONTINUE;
3350}
3351
3352static int em_out(struct x86_emulate_ctxt *ctxt)
3353{
3354 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3355 &ctxt->src.val, 1);
3356 /* Disable writeback. */
3357 ctxt->dst.type = OP_NONE;
3358 return X86EMUL_CONTINUE;
3359}
3360
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003361static int em_cli(struct x86_emulate_ctxt *ctxt)
3362{
3363 if (emulator_bad_iopl(ctxt))
3364 return emulate_gp(ctxt, 0);
3365
3366 ctxt->eflags &= ~X86_EFLAGS_IF;
3367 return X86EMUL_CONTINUE;
3368}
3369
3370static int em_sti(struct x86_emulate_ctxt *ctxt)
3371{
3372 if (emulator_bad_iopl(ctxt))
3373 return emulate_gp(ctxt, 0);
3374
3375 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3376 ctxt->eflags |= X86_EFLAGS_IF;
3377 return X86EMUL_CONTINUE;
3378}
3379
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003380static int em_bt(struct x86_emulate_ctxt *ctxt)
3381{
3382 /* Disable writeback. */
3383 ctxt->dst.type = OP_NONE;
3384 /* only subword offset */
3385 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3386
3387 emulate_2op_SrcV_nobyte(ctxt, "bt");
3388 return X86EMUL_CONTINUE;
3389}
3390
3391static int em_bts(struct x86_emulate_ctxt *ctxt)
3392{
3393 emulate_2op_SrcV_nobyte(ctxt, "bts");
3394 return X86EMUL_CONTINUE;
3395}
3396
3397static int em_btr(struct x86_emulate_ctxt *ctxt)
3398{
3399 emulate_2op_SrcV_nobyte(ctxt, "btr");
3400 return X86EMUL_CONTINUE;
3401}
3402
3403static int em_btc(struct x86_emulate_ctxt *ctxt)
3404{
3405 emulate_2op_SrcV_nobyte(ctxt, "btc");
3406 return X86EMUL_CONTINUE;
3407}
3408
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003409static int em_bsf(struct x86_emulate_ctxt *ctxt)
3410{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003411 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003412 return X86EMUL_CONTINUE;
3413}
3414
3415static int em_bsr(struct x86_emulate_ctxt *ctxt)
3416{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003417 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003418 return X86EMUL_CONTINUE;
3419}
3420
Avi Kivity6d6eede2012-06-07 14:11:36 +03003421static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3422{
3423 u32 eax, ebx, ecx, edx;
3424
Avi Kivitydd856ef2012-08-27 23:46:17 +03003425 eax = reg_read(ctxt, VCPU_REGS_RAX);
3426 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003427 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003428 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3429 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3430 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3431 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003432 return X86EMUL_CONTINUE;
3433}
3434
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003435static int em_lahf(struct x86_emulate_ctxt *ctxt)
3436{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003437 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3438 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003439 return X86EMUL_CONTINUE;
3440}
3441
Avi Kivity92998362012-06-13 12:25:06 +03003442static int em_bswap(struct x86_emulate_ctxt *ctxt)
3443{
3444 switch (ctxt->op_bytes) {
3445#ifdef CONFIG_X86_64
3446 case 8:
3447 asm("bswap %0" : "+r"(ctxt->dst.val));
3448 break;
3449#endif
3450 default:
3451 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3452 break;
3453 }
3454 return X86EMUL_CONTINUE;
3455}
3456
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003457static bool valid_cr(int nr)
3458{
3459 switch (nr) {
3460 case 0:
3461 case 2 ... 4:
3462 case 8:
3463 return true;
3464 default:
3465 return false;
3466 }
3467}
3468
3469static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3470{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003471 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003472 return emulate_ud(ctxt);
3473
3474 return X86EMUL_CONTINUE;
3475}
3476
3477static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3478{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003479 u64 new_val = ctxt->src.val64;
3480 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003481 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003482
3483 static u64 cr_reserved_bits[] = {
3484 0xffffffff00000000ULL,
3485 0, 0, 0, /* CR3 checked later */
3486 CR4_RESERVED_BITS,
3487 0, 0, 0,
3488 CR8_RESERVED_BITS,
3489 };
3490
3491 if (!valid_cr(cr))
3492 return emulate_ud(ctxt);
3493
3494 if (new_val & cr_reserved_bits[cr])
3495 return emulate_gp(ctxt, 0);
3496
3497 switch (cr) {
3498 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003499 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003500 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3501 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3502 return emulate_gp(ctxt, 0);
3503
Avi Kivity717746e2011-04-20 13:37:53 +03003504 cr4 = ctxt->ops->get_cr(ctxt, 4);
3505 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003506
3507 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3508 !(cr4 & X86_CR4_PAE))
3509 return emulate_gp(ctxt, 0);
3510
3511 break;
3512 }
3513 case 3: {
3514 u64 rsvd = 0;
3515
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003516 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3517 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003518 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003519 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003520 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003521 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003522 rsvd = CR3_NONPAE_RESERVED_BITS;
3523
3524 if (new_val & rsvd)
3525 return emulate_gp(ctxt, 0);
3526
3527 break;
3528 }
3529 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003530 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003531
3532 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3533 return emulate_gp(ctxt, 0);
3534
3535 break;
3536 }
3537 }
3538
3539 return X86EMUL_CONTINUE;
3540}
3541
Joerg Roedel3b88e412011-04-04 12:39:29 +02003542static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3543{
3544 unsigned long dr7;
3545
Avi Kivity717746e2011-04-20 13:37:53 +03003546 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003547
3548 /* Check if DR7.Global_Enable is set */
3549 return dr7 & (1 << 13);
3550}
3551
3552static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3553{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003554 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003555 u64 cr4;
3556
3557 if (dr > 7)
3558 return emulate_ud(ctxt);
3559
Avi Kivity717746e2011-04-20 13:37:53 +03003560 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003561 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3562 return emulate_ud(ctxt);
3563
3564 if (check_dr7_gd(ctxt))
3565 return emulate_db(ctxt);
3566
3567 return X86EMUL_CONTINUE;
3568}
3569
3570static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3571{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003572 u64 new_val = ctxt->src.val64;
3573 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003574
3575 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3576 return emulate_gp(ctxt, 0);
3577
3578 return check_dr_read(ctxt);
3579}
3580
Joerg Roedel01de8b02011-04-04 12:39:31 +02003581static int check_svme(struct x86_emulate_ctxt *ctxt)
3582{
3583 u64 efer;
3584
Avi Kivity717746e2011-04-20 13:37:53 +03003585 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003586
3587 if (!(efer & EFER_SVME))
3588 return emulate_ud(ctxt);
3589
3590 return X86EMUL_CONTINUE;
3591}
3592
3593static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3594{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003595 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003596
3597 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003598 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003599 return emulate_gp(ctxt, 0);
3600
3601 return check_svme(ctxt);
3602}
3603
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003604static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3605{
Avi Kivity717746e2011-04-20 13:37:53 +03003606 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003607
Avi Kivity717746e2011-04-20 13:37:53 +03003608 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003609 return emulate_ud(ctxt);
3610
3611 return X86EMUL_CONTINUE;
3612}
3613
Joerg Roedel80612522011-04-04 12:39:33 +02003614static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3615{
Avi Kivity717746e2011-04-20 13:37:53 +03003616 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003617 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003618
Avi Kivity717746e2011-04-20 13:37:53 +03003619 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003620 (rcx > 3))
3621 return emulate_gp(ctxt, 0);
3622
3623 return X86EMUL_CONTINUE;
3624}
3625
Joerg Roedelf6511932011-04-04 12:39:35 +02003626static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3627{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003628 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3629 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003630 return emulate_gp(ctxt, 0);
3631
3632 return X86EMUL_CONTINUE;
3633}
3634
3635static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3636{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003637 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3638 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003639 return emulate_gp(ctxt, 0);
3640
3641 return X86EMUL_CONTINUE;
3642}
3643
Avi Kivity73fba5f2010-07-29 15:11:53 +03003644#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003645#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003646#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3647 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003648#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003649#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003650#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3651#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003652#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003653#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003654#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003655#define II(_f, _e, _i) \
3656 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003657#define IIP(_f, _e, _i, _p) \
3658 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3659 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003660#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003661
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003662#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003663#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003664#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003665#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003666#define I2bvIP(_f, _e, _i, _p) \
3667 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003668
Avi Kivityfb864fb2013-01-04 16:18:54 +02003669#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3670 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3671 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003672
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003673static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003674 DI(SrcNone | Priv, monitor),
3675 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003676 N, N, N, N, N, N,
3677};
3678
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003679static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003680 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3681 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3682 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3683 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3684 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3685 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3686 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3687 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003688};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003689
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003690static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003691 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003692 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003693 N, N, N, N, N, N,
3694};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003695
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003696static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003697 F(Lock, em_add),
3698 F(Lock | PageTable, em_or),
3699 F(Lock, em_adc),
3700 F(Lock, em_sbb),
3701 F(Lock | PageTable, em_and),
3702 F(Lock, em_sub),
3703 F(Lock, em_xor),
3704 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003705};
3706
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003707static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003708 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003709};
3710
Avi Kivity007a3b52013-01-19 19:51:51 +02003711static const struct opcode group2[] = {
3712 F(DstMem | ModRM, em_rol),
3713 F(DstMem | ModRM, em_ror),
3714 F(DstMem | ModRM, em_rcl),
3715 F(DstMem | ModRM, em_rcr),
3716 F(DstMem | ModRM, em_shl),
3717 F(DstMem | ModRM, em_shr),
3718 F(DstMem | ModRM, em_shl),
3719 F(DstMem | ModRM, em_sar),
3720};
3721
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003722static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003723 F(DstMem | SrcImm | NoWrite, em_test),
3724 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003725 F(DstMem | SrcNone | Lock, em_not),
3726 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003727 I(SrcMem, em_mul_ex),
3728 I(SrcMem, em_imul_ex),
3729 I(SrcMem, em_div_ex),
3730 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003731};
3732
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003733static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003734 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3735 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003736 N, N, N, N, N, N,
3737};
3738
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003739static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003740 F(DstMem | SrcNone | Lock, em_inc),
3741 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003742 I(SrcMem | Stack, em_grp45),
3743 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3744 I(SrcMem | Stack, em_grp45),
3745 I(SrcMemFAddr | ImplicitOps, em_grp45),
3746 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747};
3748
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003749static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003750 DI(Prot, sldt),
3751 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003752 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003753 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003754 N, N, N, N,
3755};
3756
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003757static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003758 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3759 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003760 II(SrcMem | Priv, em_lgdt, lgdt),
3761 II(SrcMem | Priv, em_lidt, lidt),
3762 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3763 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3764 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003765}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003766 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003767 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003768 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003769 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3770 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3771 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003772} };
3773
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003774static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003775 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003776 I(DstMem | SrcImmByte, em_bt),
3777 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3778 I(DstMem | SrcImmByte | Lock, em_btr),
3779 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003780};
3781
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003782static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003783 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003784}, {
3785 N, N, N, N, N, N, N, N,
3786} };
3787
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003788static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003789 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003790 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003791};
3792
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003793static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003794 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003795};
3796
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003797static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003798 I(0, em_mov), N, N, N,
3799};
3800
Gleb Natapov045a2822012-12-20 16:57:43 +02003801static const struct escape escape_d9 = { {
3802 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3803}, {
3804 /* 0xC0 - 0xC7 */
3805 N, N, N, N, N, N, N, N,
3806 /* 0xC8 - 0xCF */
3807 N, N, N, N, N, N, N, N,
3808 /* 0xD0 - 0xC7 */
3809 N, N, N, N, N, N, N, N,
3810 /* 0xD8 - 0xDF */
3811 N, N, N, N, N, N, N, N,
3812 /* 0xE0 - 0xE7 */
3813 N, N, N, N, N, N, N, N,
3814 /* 0xE8 - 0xEF */
3815 N, N, N, N, N, N, N, N,
3816 /* 0xF0 - 0xF7 */
3817 N, N, N, N, N, N, N, N,
3818 /* 0xF8 - 0xFF */
3819 N, N, N, N, N, N, N, N,
3820} };
3821
3822static const struct escape escape_db = { {
3823 N, N, N, N, N, N, N, N,
3824}, {
3825 /* 0xC0 - 0xC7 */
3826 N, N, N, N, N, N, N, N,
3827 /* 0xC8 - 0xCF */
3828 N, N, N, N, N, N, N, N,
3829 /* 0xD0 - 0xC7 */
3830 N, N, N, N, N, N, N, N,
3831 /* 0xD8 - 0xDF */
3832 N, N, N, N, N, N, N, N,
3833 /* 0xE0 - 0xE7 */
3834 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3835 /* 0xE8 - 0xEF */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xF0 - 0xF7 */
3838 N, N, N, N, N, N, N, N,
3839 /* 0xF8 - 0xFF */
3840 N, N, N, N, N, N, N, N,
3841} };
3842
3843static const struct escape escape_dd = { {
3844 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3845}, {
3846 /* 0xC0 - 0xC7 */
3847 N, N, N, N, N, N, N, N,
3848 /* 0xC8 - 0xCF */
3849 N, N, N, N, N, N, N, N,
3850 /* 0xD0 - 0xC7 */
3851 N, N, N, N, N, N, N, N,
3852 /* 0xD8 - 0xDF */
3853 N, N, N, N, N, N, N, N,
3854 /* 0xE0 - 0xE7 */
3855 N, N, N, N, N, N, N, N,
3856 /* 0xE8 - 0xEF */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xF0 - 0xF7 */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xF8 - 0xFF */
3861 N, N, N, N, N, N, N, N,
3862} };
3863
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003864static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003865 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003866 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003867 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3868 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003869 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003870 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003871 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3872 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003873 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003874 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003875 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3876 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003877 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003878 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003879 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3880 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003881 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003882 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003884 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003885 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003886 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003888 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003890 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003892 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003894 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003896 I(ImplicitOps | Stack | No64, em_pusha),
3897 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3899 N, N, N, N,
3900 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003901 I(SrcImm | Mov | Stack, em_push),
3902 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003903 I(SrcImmByte | Mov | Stack, em_push),
3904 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003905 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003906 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003907 /* 0x70 - 0x7F */
3908 X16(D(SrcImmByte)),
3909 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003910 G(ByteOp | DstMem | SrcImm, group1),
3911 G(DstMem | SrcImm, group1),
3912 G(ByteOp | DstMem | SrcImm | No64, group1),
3913 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003914 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003915 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003916 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003917 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003918 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003919 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003920 D(ModRM | SrcMem | NoAccess | DstReg),
3921 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3922 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003923 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003924 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003925 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003926 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003927 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003928 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003929 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003930 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003931 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003932 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003933 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003934 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003936 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003937 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3938 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003939 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003940 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003941 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003943 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003944 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003945 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003946 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003947 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003948 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3949 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003950 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003952 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3953 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003954 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003955 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003956 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003957 G(Src2One | ByteOp, group2), G(Src2One, group2),
3958 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Gleb Natapov7f662272012-12-10 11:42:30 +02003959 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003960 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003961 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003962 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003963 X3(I(SrcImmByte, em_loop)),
3964 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003965 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3966 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003967 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003968 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003969 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003970 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3971 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003972 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003973 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003974 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3975 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003976 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003977 D(ImplicitOps), D(ImplicitOps),
3978 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003979 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3980};
3981
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003982static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003983 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003984 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003985 N, I(ImplicitOps | VendorSpecific, em_syscall),
3986 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003987 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003988 N, D(ImplicitOps | ModRM), N, N,
3989 /* 0x10 - 0x1F */
3990 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3991 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003992 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003993 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003994 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3995 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003996 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003997 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3998 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003999 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004000 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004001 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004002 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004003 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004004 I(ImplicitOps | VendorSpecific, em_sysenter),
4005 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004006 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004007 N, N, N, N, N, N, N, N,
4008 /* 0x40 - 0x4F */
4009 X16(D(DstReg | SrcMem | ModRM | Mov)),
4010 /* 0x50 - 0x5F */
4011 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4012 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004013 N, N, N, N,
4014 N, N, N, N,
4015 N, N, N, N,
4016 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004017 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004018 N, N, N, N,
4019 N, N, N, N,
4020 N, N, N, N,
4021 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004022 /* 0x80 - 0x8F */
4023 X16(D(SrcImm)),
4024 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004025 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004026 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004027 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03004028 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004029 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4030 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004031 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004032 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004033 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004034 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004035 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4036 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004037 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004038 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004039 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004040 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004041 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004042 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4043 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004044 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004045 /* 0xB8 - 0xBF */
4046 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004047 G(BitOp, group8),
4048 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09004049 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004050 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004051 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004052 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004053 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004054 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004055 /* 0xC8 - 0xCF */
4056 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004057 /* 0xD0 - 0xDF */
4058 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4059 /* 0xE0 - 0xEF */
4060 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4061 /* 0xF0 - 0xFF */
4062 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4063};
4064
4065#undef D
4066#undef N
4067#undef G
4068#undef GD
4069#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004070#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004071#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004072
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004073#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004074#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004075#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004076#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004077#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004078
Avi Kivity9dac77f2011-06-01 15:34:25 +03004079static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004080{
4081 unsigned size;
4082
Avi Kivity9dac77f2011-06-01 15:34:25 +03004083 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004084 if (size == 8)
4085 size = 4;
4086 return size;
4087}
4088
4089static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4090 unsigned size, bool sign_extension)
4091{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004092 int rc = X86EMUL_CONTINUE;
4093
4094 op->type = OP_IMM;
4095 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004096 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004097 /* NB. Immediates are sign-extended as necessary. */
4098 switch (op->bytes) {
4099 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004100 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004101 break;
4102 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004103 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004104 break;
4105 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004106 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004107 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004108 case 8:
4109 op->val = insn_fetch(s64, ctxt);
4110 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004111 }
4112 if (!sign_extension) {
4113 switch (op->bytes) {
4114 case 1:
4115 op->val &= 0xff;
4116 break;
4117 case 2:
4118 op->val &= 0xffff;
4119 break;
4120 case 4:
4121 op->val &= 0xffffffff;
4122 break;
4123 }
4124 }
4125done:
4126 return rc;
4127}
4128
Avi Kivitya99455492011-09-13 10:45:41 +03004129static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4130 unsigned d)
4131{
4132 int rc = X86EMUL_CONTINUE;
4133
4134 switch (d) {
4135 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004136 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004137 break;
4138 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004139 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004140 break;
4141 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004142 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004143 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004144 *op = ctxt->memop;
4145 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004146 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03004147 fetch_bit_operand(ctxt);
4148 op->orig_val = op->val;
4149 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004150 case OpMem64:
4151 ctxt->memop.bytes = 8;
4152 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004153 case OpAcc:
4154 op->type = OP_REG;
4155 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004156 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004157 fetch_register_operand(op);
4158 op->orig_val = op->val;
4159 break;
4160 case OpDI:
4161 op->type = OP_MEM;
4162 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4163 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004164 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004165 op->addr.mem.seg = VCPU_SREG_ES;
4166 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004167 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004168 break;
4169 case OpDX:
4170 op->type = OP_REG;
4171 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004172 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004173 fetch_register_operand(op);
4174 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004175 case OpCL:
4176 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004177 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004178 break;
4179 case OpImmByte:
4180 rc = decode_imm(ctxt, op, 1, true);
4181 break;
4182 case OpOne:
4183 op->bytes = 1;
4184 op->val = 1;
4185 break;
4186 case OpImm:
4187 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4188 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004189 case OpImm64:
4190 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4191 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004192 case OpMem8:
4193 ctxt->memop.bytes = 1;
4194 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004195 case OpMem16:
4196 ctxt->memop.bytes = 2;
4197 goto mem_common;
4198 case OpMem32:
4199 ctxt->memop.bytes = 4;
4200 goto mem_common;
4201 case OpImmU16:
4202 rc = decode_imm(ctxt, op, 2, false);
4203 break;
4204 case OpImmU:
4205 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4206 break;
4207 case OpSI:
4208 op->type = OP_MEM;
4209 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4210 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004211 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004212 op->addr.mem.seg = seg_override(ctxt);
4213 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004214 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004215 break;
4216 case OpImmFAddr:
4217 op->type = OP_IMM;
4218 op->addr.mem.ea = ctxt->_eip;
4219 op->bytes = ctxt->op_bytes + 2;
4220 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4221 break;
4222 case OpMemFAddr:
4223 ctxt->memop.bytes = ctxt->op_bytes + 2;
4224 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004225 case OpES:
4226 op->val = VCPU_SREG_ES;
4227 break;
4228 case OpCS:
4229 op->val = VCPU_SREG_CS;
4230 break;
4231 case OpSS:
4232 op->val = VCPU_SREG_SS;
4233 break;
4234 case OpDS:
4235 op->val = VCPU_SREG_DS;
4236 break;
4237 case OpFS:
4238 op->val = VCPU_SREG_FS;
4239 break;
4240 case OpGS:
4241 op->val = VCPU_SREG_GS;
4242 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004243 case OpImplicit:
4244 /* Special instructions do their own operand decoding. */
4245 default:
4246 op->type = OP_NONE; /* Disable writeback. */
4247 break;
4248 }
4249
4250done:
4251 return rc;
4252}
4253
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004254int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004255{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004256 int rc = X86EMUL_CONTINUE;
4257 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004258 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004259 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004260 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004261
Avi Kivityf09ed832011-09-13 10:45:40 +03004262 ctxt->memop.type = OP_NONE;
4263 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004264 ctxt->_eip = ctxt->eip;
4265 ctxt->fetch.start = ctxt->_eip;
4266 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004267 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004268 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004269
4270 switch (mode) {
4271 case X86EMUL_MODE_REAL:
4272 case X86EMUL_MODE_VM86:
4273 case X86EMUL_MODE_PROT16:
4274 def_op_bytes = def_ad_bytes = 2;
4275 break;
4276 case X86EMUL_MODE_PROT32:
4277 def_op_bytes = def_ad_bytes = 4;
4278 break;
4279#ifdef CONFIG_X86_64
4280 case X86EMUL_MODE_PROT64:
4281 def_op_bytes = 4;
4282 def_ad_bytes = 8;
4283 break;
4284#endif
4285 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004286 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004287 }
4288
Avi Kivity9dac77f2011-06-01 15:34:25 +03004289 ctxt->op_bytes = def_op_bytes;
4290 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004291
4292 /* Legacy prefixes. */
4293 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004294 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004295 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004296 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004297 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004298 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004299 break;
4300 case 0x67: /* address-size override */
4301 if (mode == X86EMUL_MODE_PROT64)
4302 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004303 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004304 else
4305 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004306 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004307 break;
4308 case 0x26: /* ES override */
4309 case 0x2e: /* CS override */
4310 case 0x36: /* SS override */
4311 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004313 break;
4314 case 0x64: /* FS override */
4315 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004316 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004317 break;
4318 case 0x40 ... 0x4f: /* REX */
4319 if (mode != X86EMUL_MODE_PROT64)
4320 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004321 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004322 continue;
4323 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004324 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004325 break;
4326 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004327 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004328 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004329 break;
4330 default:
4331 goto done_prefixes;
4332 }
4333
4334 /* Any legacy prefix after a REX prefix nullifies its effect. */
4335
Avi Kivity9dac77f2011-06-01 15:34:25 +03004336 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004337 }
4338
4339done_prefixes:
4340
4341 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 if (ctxt->rex_prefix & 8)
4343 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004344
4345 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004346 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004347 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 if (ctxt->b == 0x0f) {
4349 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004350 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004351 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004352 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004353 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004354
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004355 if (ctxt->d & ModRM)
4356 ctxt->modrm = insn_fetch(u8, ctxt);
4357
Avi Kivity9dac77f2011-06-01 15:34:25 +03004358 while (ctxt->d & GroupMask) {
4359 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004360 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004361 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004362 opcode = opcode.u.group[goffset];
4363 break;
4364 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004365 goffset = (ctxt->modrm >> 3) & 7;
4366 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004367 opcode = opcode.u.gdual->mod3[goffset];
4368 else
4369 opcode = opcode.u.gdual->mod012[goffset];
4370 break;
4371 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004372 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004373 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004374 break;
4375 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004377 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004378 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004379 switch (simd_prefix) {
4380 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4381 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4382 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4383 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4384 }
4385 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004386 case Escape:
4387 if (ctxt->modrm > 0xbf)
4388 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4389 else
4390 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4391 break;
Avi Kivity46561642011-04-24 14:09:59 +03004392 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004393 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004394 }
Avi Kivity46561642011-04-24 14:09:59 +03004395
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004396 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004398 }
4399
Avi Kivity9dac77f2011-06-01 15:34:25 +03004400 ctxt->execute = opcode.u.execute;
4401 ctxt->check_perm = opcode.check_perm;
4402 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004403
4404 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004406 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004407
Avi Kivity9dac77f2011-06-01 15:34:25 +03004408 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004409 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004410
Avi Kivity9dac77f2011-06-01 15:34:25 +03004411 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4412 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004413
Avi Kivity9dac77f2011-06-01 15:34:25 +03004414 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004415 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004416 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004417 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004418 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004419 }
4420
Avi Kivity9dac77f2011-06-01 15:34:25 +03004421 if (ctxt->d & Sse)
4422 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004423 else if (ctxt->d & Mmx)
4424 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004425
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004426 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004427 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004428 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004429 if (!ctxt->has_seg_override)
4430 set_seg_override(ctxt, ctxt->modrm_seg);
4431 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004432 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004433 if (rc != X86EMUL_CONTINUE)
4434 goto done;
4435
Avi Kivity9dac77f2011-06-01 15:34:25 +03004436 if (!ctxt->has_seg_override)
4437 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438
Avi Kivityf09ed832011-09-13 10:45:40 +03004439 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004440
Avi Kivityf09ed832011-09-13 10:45:40 +03004441 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4442 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004443
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004444 /*
4445 * Decode and fetch the source operand: register, memory
4446 * or immediate.
4447 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004448 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004449 if (rc != X86EMUL_CONTINUE)
4450 goto done;
4451
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004452 /*
4453 * Decode and fetch the second source operand: register, memory
4454 * or immediate.
4455 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004456 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004457 if (rc != X86EMUL_CONTINUE)
4458 goto done;
4459
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004460 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004461 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004462
4463done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004464 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4465 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004466
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004467 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004468}
4469
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004470bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4471{
4472 return ctxt->d & PageTable;
4473}
4474
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004475static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4476{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004477 /* The second termination condition only applies for REPE
4478 * and REPNE. Test if the repeat string operation prefix is
4479 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4480 * corresponding termination condition according to:
4481 * - if REPE/REPZ and ZF = 0 then done
4482 * - if REPNE/REPNZ and ZF = 1 then done
4483 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004484 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4485 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4486 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004487 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004488 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004489 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4490 return true;
4491
4492 return false;
4493}
4494
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004495static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4496{
4497 bool fault = false;
4498
4499 ctxt->ops->get_fpu(ctxt);
4500 asm volatile("1: fwait \n\t"
4501 "2: \n\t"
4502 ".pushsection .fixup,\"ax\" \n\t"
4503 "3: \n\t"
4504 "movb $1, %[fault] \n\t"
4505 "jmp 2b \n\t"
4506 ".popsection \n\t"
4507 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004508 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004509 ctxt->ops->put_fpu(ctxt);
4510
4511 if (unlikely(fault))
4512 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4513
4514 return X86EMUL_CONTINUE;
4515}
4516
4517static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4518 struct operand *op)
4519{
4520 if (op->type == OP_MM)
4521 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4522}
4523
Avi Kivitye28bbd42013-01-04 16:18:48 +02004524static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4525{
4526 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4527 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4528 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4529 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4530 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4531 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4532 return X86EMUL_CONTINUE;
4533}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004534
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004535int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004536{
Mathias Krause0225fb52012-08-30 01:30:16 +02004537 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004538 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004539 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004540
Avi Kivity9dac77f2011-06-01 15:34:25 +03004541 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004542
Avi Kivity9dac77f2011-06-01 15:34:25 +03004543 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004544 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004545 goto done;
4546 }
4547
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004548 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004550 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004551 goto done;
4552 }
4553
Avi Kivity9dac77f2011-06-01 15:34:25 +03004554 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004555 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004556 goto done;
4557 }
4558
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004559 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4560 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004561 rc = emulate_ud(ctxt);
4562 goto done;
4563 }
4564
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004565 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004566 rc = emulate_nm(ctxt);
4567 goto done;
4568 }
4569
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004570 if (ctxt->d & Mmx) {
4571 rc = flush_pending_x87_faults(ctxt);
4572 if (rc != X86EMUL_CONTINUE)
4573 goto done;
4574 /*
4575 * Now that we know the fpu is exception safe, we can fetch
4576 * operands from it.
4577 */
4578 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4579 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4580 if (!(ctxt->d & Mov))
4581 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4582 }
4583
Avi Kivity9dac77f2011-06-01 15:34:25 +03004584 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4585 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004586 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004587 if (rc != X86EMUL_CONTINUE)
4588 goto done;
4589 }
4590
Gleb Natapove92805a2010-02-10 14:21:35 +02004591 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004592 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004593 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004594 goto done;
4595 }
4596
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004597 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004598 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004599 rc = emulate_ud(ctxt);
4600 goto done;
4601 }
4602
Joerg Roedeld09beab2011-04-04 12:39:25 +02004603 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004604 if (ctxt->check_perm) {
4605 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004606 if (rc != X86EMUL_CONTINUE)
4607 goto done;
4608 }
4609
Avi Kivity9dac77f2011-06-01 15:34:25 +03004610 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4611 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004612 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004613 if (rc != X86EMUL_CONTINUE)
4614 goto done;
4615 }
4616
Avi Kivity9dac77f2011-06-01 15:34:25 +03004617 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004618 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004619 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004620 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004621 goto done;
4622 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004623 }
4624
Avi Kivity9dac77f2011-06-01 15:34:25 +03004625 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4626 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4627 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004628 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004629 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004630 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004631 }
4632
Avi Kivity9dac77f2011-06-01 15:34:25 +03004633 if (ctxt->src2.type == OP_MEM) {
4634 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4635 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004636 if (rc != X86EMUL_CONTINUE)
4637 goto done;
4638 }
4639
Avi Kivity9dac77f2011-06-01 15:34:25 +03004640 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004641 goto special_insn;
4642
4643
Avi Kivity9dac77f2011-06-01 15:34:25 +03004644 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004645 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004646 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4647 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004648 if (rc != X86EMUL_CONTINUE)
4649 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004650 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004651 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004652
Avi Kivity018a98d2007-11-27 19:30:56 +02004653special_insn:
4654
Avi Kivity9dac77f2011-06-01 15:34:25 +03004655 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4656 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004657 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004658 if (rc != X86EMUL_CONTINUE)
4659 goto done;
4660 }
4661
Avi Kivity9dac77f2011-06-01 15:34:25 +03004662 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004663 if (ctxt->d & Fastop) {
4664 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4665 rc = fastop(ctxt, fop);
4666 if (rc != X86EMUL_CONTINUE)
4667 goto done;
4668 goto writeback;
4669 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004670 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004671 if (rc != X86EMUL_CONTINUE)
4672 goto done;
4673 goto writeback;
4674 }
4675
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677 goto twobyte_insn;
4678
Avi Kivity9dac77f2011-06-01 15:34:25 +03004679 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004681 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004683 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004684 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004685 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004686 if (test_cc(ctxt->b, ctxt->eflags))
4687 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004688 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004689 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004690 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004691 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004692 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004693 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004694 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004695 rc = em_xchg(ctxt);
4696 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004697 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 switch (ctxt->op_bytes) {
4699 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4700 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4701 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004702 }
4703 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004704 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004705 rc = emulate_int(ctxt, 3);
4706 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004707 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004708 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004709 break;
4710 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004711 if (ctxt->eflags & EFLG_OF)
4712 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004713 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004714 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004715 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004716 jmp_rel(ctxt, ctxt->src.val);
4717 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004718 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004719 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004720 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004721 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004722 case 0xf5: /* cmc */
4723 /* complement carry flag from eflags reg */
4724 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004725 break;
4726 case 0xf8: /* clc */
4727 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004728 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004729 case 0xf9: /* stc */
4730 ctxt->eflags |= EFLG_CF;
4731 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004732 case 0xfc: /* cld */
4733 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004734 break;
4735 case 0xfd: /* std */
4736 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004737 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004738 default:
4739 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004741
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004742 if (rc != X86EMUL_CONTINUE)
4743 goto done;
4744
Avi Kivity018a98d2007-11-27 19:30:56 +02004745writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004746 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004747 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004748 goto done;
4749
Gleb Natapov5cd21912010-03-18 15:20:26 +02004750 /*
4751 * restore dst type in case the decoding will be reused
4752 * (happens for string instruction )
4753 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004754 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004755
Avi Kivity9dac77f2011-06-01 15:34:25 +03004756 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004757 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004758
Avi Kivity9dac77f2011-06-01 15:34:25 +03004759 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004760 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004761
Avi Kivity9dac77f2011-06-01 15:34:25 +03004762 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004763 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004764 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004765 if ((ctxt->d & SrcMask) == SrcSI)
4766 count = ctxt->src.count;
4767 else
4768 count = ctxt->dst.count;
4769 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4770 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004771
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004772 if (!string_insn_completed(ctxt)) {
4773 /*
4774 * Re-enter guest when pio read ahead buffer is empty
4775 * or, if it is not used, after each 1024 iteration.
4776 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004777 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004778 (r->end == 0 || r->end != r->pos)) {
4779 /*
4780 * Reset read cache. Usually happens before
4781 * decode, but since instruction is restarted
4782 * we have to do it here.
4783 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004784 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004785 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004786 return EMULATION_RESTART;
4787 }
4788 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004789 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004790 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004791
Avi Kivity9dac77f2011-06-01 15:34:25 +03004792 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004793
4794done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004795 if (rc == X86EMUL_PROPAGATE_FAULT)
4796 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004797 if (rc == X86EMUL_INTERCEPTED)
4798 return EMULATION_INTERCEPTED;
4799
Avi Kivitydd856ef2012-08-27 23:46:17 +03004800 if (rc == X86EMUL_CONTINUE)
4801 writeback_registers(ctxt);
4802
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004803 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804
4805twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004806 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004807 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004808 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004809 break;
4810 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004811 case 0x0d: /* GrpP (prefetch) */
4812 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004813 break;
4814 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004815 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004816 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004818 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004819 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004821 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4822 if (!test_cc(ctxt->b, ctxt->eflags))
4823 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004824 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004825 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004826 if (test_cc(ctxt->b, ctxt->eflags))
4827 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004828 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004829 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004831 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004832 case 0xae: /* clflush */
4833 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004834 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004835 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004836 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004837 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004840 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004841 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004843 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004844 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004845 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004846 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004847 ctxt->src.val = ctxt->dst.orig_val;
4848 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004849 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004850 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004851 ctxt->dst.bytes = ctxt->op_bytes;
4852 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4853 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004854 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004855 default:
4856 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004857 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004858
4859 if (rc != X86EMUL_CONTINUE)
4860 goto done;
4861
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862 goto writeback;
4863
4864cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004865 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004867
4868void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4869{
4870 invalidate_registers(ctxt);
4871}
4872
4873void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4874{
4875 writeback_registers(ctxt);
4876}