blob: 62014dca9e2637652a8ebac3b99774d9c400d066 [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 Kivity11c363b2013-01-19 19:51:54 +0200481/* 2 operand, word only */
482#define FASTOP2W(op) \
483 FOP_START(op) \
484 FOPNOP() \
485 FOP2E(op##w, ax, bx) \
486 FOP2E(op##l, eax, ebx) \
487 ON64(FOP2E(op##q, rax, rbx)) \
488 FOP_END
489
Avi Kivity007a3b52013-01-19 19:51:51 +0200490/* 2 operand, src is CL */
491#define FASTOP2CL(op) \
492 FOP_START(op) \
493 FOP2E(op##b, al, cl) \
494 FOP2E(op##w, ax, cl) \
495 FOP2E(op##l, eax, cl) \
496 ON64(FOP2E(op##q, rax, cl)) \
497 FOP_END
498
Avi Kivity0bdea062013-01-19 19:51:50 +0200499#define FOP3E(op, dst, src, src2) \
500 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
501
502/* 3-operand, word-only, src2=cl */
503#define FASTOP3WCL(op) \
504 FOP_START(op) \
505 FOPNOP() \
506 FOP3E(op##w, ax, bx, cl) \
507 FOP3E(op##l, eax, ebx, cl) \
508 ON64(FOP3E(op##q, rax, rbx, cl)) \
509 FOP_END
510
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200511/* Special case for SETcc - 1 instruction per cc */
512#define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t"
513
514FOP_START(setcc)
515FOP_SETCC(seto)
516FOP_SETCC(setno)
517FOP_SETCC(setc)
518FOP_SETCC(setnc)
519FOP_SETCC(setz)
520FOP_SETCC(setnz)
521FOP_SETCC(setbe)
522FOP_SETCC(setnbe)
523FOP_SETCC(sets)
524FOP_SETCC(setns)
525FOP_SETCC(setp)
526FOP_SETCC(setnp)
527FOP_SETCC(setl)
528FOP_SETCC(setnl)
529FOP_SETCC(setle)
530FOP_SETCC(setnle)
531FOP_END;
532
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300533#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300534 do { \
535 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300536 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
537 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300538 \
539 __asm__ __volatile__ ( \
540 _PRE_EFLAGS("0", "5", "1") \
541 "1: \n\t" \
542 _op _suffix " %6; " \
543 "2: \n\t" \
544 _POST_EFLAGS("0", "5", "1") \
545 ".pushsection .fixup,\"ax\" \n\t" \
546 "3: movb $1, %4 \n\t" \
547 "jmp 2b \n\t" \
548 ".popsection \n\t" \
549 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300550 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
551 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800552 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300553 } while (0)
554
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300555/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300556#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300557 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300558 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300559 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300560 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300561 break; \
562 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300563 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300564 break; \
565 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300566 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300567 break; \
568 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300569 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300570 break; \
571 } \
572 } while (0)
573
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200574static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
575 enum x86_intercept intercept,
576 enum x86_intercept_stage stage)
577{
578 struct x86_instruction_info info = {
579 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300580 .rep_prefix = ctxt->rep_prefix,
581 .modrm_mod = ctxt->modrm_mod,
582 .modrm_reg = ctxt->modrm_reg,
583 .modrm_rm = ctxt->modrm_rm,
584 .src_val = ctxt->src.val64,
585 .src_bytes = ctxt->src.bytes,
586 .dst_bytes = ctxt->dst.bytes,
587 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200588 .next_rip = ctxt->eip,
589 };
590
Avi Kivity29535382011-04-20 13:37:53 +0300591 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200592}
593
Avi Kivityf47cfa32012-06-07 17:49:24 +0300594static void assign_masked(ulong *dest, ulong src, ulong mask)
595{
596 *dest = (*dest & ~mask) | (src & mask);
597}
598
Avi Kivity9dac77f2011-06-01 15:34:25 +0300599static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800600{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300601 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800602}
603
Avi Kivityf47cfa32012-06-07 17:49:24 +0300604static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
605{
606 u16 sel;
607 struct desc_struct ss;
608
609 if (ctxt->mode == X86EMUL_MODE_PROT64)
610 return ~0UL;
611 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
612 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
613}
614
Avi Kivity612e89f2012-06-12 20:03:23 +0300615static int stack_size(struct x86_emulate_ctxt *ctxt)
616{
617 return (__fls(stack_mask(ctxt)) + 1) >> 3;
618}
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800621static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300622address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800623{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300624 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800625 return reg;
626 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300627 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800628}
629
630static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300631register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800632{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300633 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800634}
635
Avi Kivity5ad105e2012-08-19 14:34:31 +0300636static void masked_increment(ulong *reg, ulong mask, int inc)
637{
638 assign_masked(reg, *reg + inc, mask);
639}
640
Harvey Harrison7a9572752008-02-19 07:40:41 -0800641static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300642register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800643{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300644 ulong mask;
645
Avi Kivity9dac77f2011-06-01 15:34:25 +0300646 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300647 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800648 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300649 mask = ad_mask(ctxt);
650 masked_increment(reg, mask, inc);
651}
652
653static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
654{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300655 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800656}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
Avi Kivity9dac77f2011-06-01 15:34:25 +0300658static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800659{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300660 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800661}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300662
Avi Kivity56697682011-04-03 14:08:51 +0300663static u32 desc_limit_scaled(struct desc_struct *desc)
664{
665 u32 limit = get_desc_limit(desc);
666
667 return desc->g ? (limit << 12) | 0xfff : limit;
668}
669
Avi Kivity9dac77f2011-06-01 15:34:25 +0300670static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300671{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300672 ctxt->has_seg_override = true;
673 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300674}
675
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900676static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300677{
678 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
679 return 0;
680
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900681 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300682}
683
Avi Kivity9dac77f2011-06-01 15:34:25 +0300684static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300685{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300686 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300687 return 0;
688
Avi Kivity9dac77f2011-06-01 15:34:25 +0300689 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300690}
691
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200692static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
693 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300694{
Avi Kivityda9cb572010-11-22 17:53:21 +0200695 ctxt->exception.vector = vec;
696 ctxt->exception.error_code = error;
697 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200698 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300699}
700
Joerg Roedel3b88e412011-04-04 12:39:29 +0200701static int emulate_db(struct x86_emulate_ctxt *ctxt)
702{
703 return emulate_exception(ctxt, DB_VECTOR, 0, false);
704}
705
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200706static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300707{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200708 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300709}
710
Avi Kivity618ff152011-04-03 12:32:09 +0300711static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
712{
713 return emulate_exception(ctxt, SS_VECTOR, err, true);
714}
715
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200716static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300717{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200718 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300719}
720
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200721static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300722{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200723 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300724}
725
Avi Kivity34d1f492010-08-26 11:59:01 +0300726static int emulate_de(struct x86_emulate_ctxt *ctxt)
727{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200728 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300729}
730
Avi Kivity12537912011-03-29 11:41:27 +0200731static int emulate_nm(struct x86_emulate_ctxt *ctxt)
732{
733 return emulate_exception(ctxt, NM_VECTOR, 0, false);
734}
735
Avi Kivity1aa36612011-04-27 13:20:30 +0300736static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
737{
738 u16 selector;
739 struct desc_struct desc;
740
741 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
742 return selector;
743}
744
745static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
746 unsigned seg)
747{
748 u16 dummy;
749 u32 base3;
750 struct desc_struct desc;
751
752 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
753 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
754}
755
Avi Kivity1c11b372012-04-09 18:39:59 +0300756/*
757 * x86 defines three classes of vector instructions: explicitly
758 * aligned, explicitly unaligned, and the rest, which change behaviour
759 * depending on whether they're AVX encoded or not.
760 *
761 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
762 * subject to the same check.
763 */
764static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
765{
766 if (likely(size < 16))
767 return false;
768
769 if (ctxt->d & Aligned)
770 return true;
771 else if (ctxt->d & Unaligned)
772 return false;
773 else if (ctxt->d & Avx)
774 return false;
775 else
776 return true;
777}
778
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400779static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300780 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400781 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300782 ulong *linear)
783{
Avi Kivity618ff152011-04-03 12:32:09 +0300784 struct desc_struct desc;
785 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300786 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300787 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300788 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200789 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300790
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900791 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300792 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300793 case X86EMUL_MODE_PROT64:
794 if (((signed long)la << 16) >> 16 != la)
795 return emulate_gp(ctxt, 0);
796 break;
797 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300798 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
799 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300800 if (!usable)
801 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200802 /* code segment in protected mode or read-only data segment */
803 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
804 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300805 goto bad;
806 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400807 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300808 goto bad;
809 lim = desc_limit_scaled(&desc);
810 if ((desc.type & 8) || !(desc.type & 4)) {
811 /* expand-up segment */
812 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
813 goto bad;
814 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800815 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300816 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
817 goto bad;
818 lim = desc.d ? 0xffffffff : 0xffff;
819 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
820 goto bad;
821 }
Avi Kivity717746e2011-04-20 13:37:53 +0300822 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300823 if (!(desc.type & 8)) {
824 /* data segment */
825 if (cpl > desc.dpl)
826 goto bad;
827 } else if ((desc.type & 8) && !(desc.type & 4)) {
828 /* nonconforming code segment */
829 if (cpl != desc.dpl)
830 goto bad;
831 } else if ((desc.type & 8) && (desc.type & 4)) {
832 /* conforming code segment */
833 if (cpl < desc.dpl)
834 goto bad;
835 }
836 break;
837 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300838 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300839 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300840 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
841 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300842 *linear = la;
843 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300844bad:
845 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300846 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300847 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300848 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300849}
850
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400851static int linearize(struct x86_emulate_ctxt *ctxt,
852 struct segmented_address addr,
853 unsigned size, bool write,
854 ulong *linear)
855{
856 return __linearize(ctxt, addr, size, write, false, linear);
857}
858
859
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200860static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
861 struct segmented_address addr,
862 void *data,
863 unsigned size)
864{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200865 int rc;
866 ulong linear;
867
Avi Kivity83b87952011-04-03 11:31:19 +0300868 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200869 if (rc != X86EMUL_CONTINUE)
870 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300871 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200872}
873
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900874/*
875 * Fetch the next byte of the instruction being emulated which is pointed to
876 * by ctxt->_eip, then increment ctxt->_eip.
877 *
878 * Also prefetch the remaining bytes of the instruction without crossing page
879 * boundary if they are not in fetch_cache yet.
880 */
881static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200882{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300883 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200884 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300885 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200886
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900887 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400888 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900889 struct segmented_address addr = { .seg = VCPU_SREG_CS,
890 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300891 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900892 size = min(15UL - cur_size,
893 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400894 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900895 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400896 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900897 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
898 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900899 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200900 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300901 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200902 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900903 *dest = fc->data[ctxt->_eip - fc->start];
904 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900905 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200906}
907
908static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900909 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200910{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900911 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200912
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200913 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900914 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200915 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200916 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900917 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900918 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200919 return rc;
920 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900921 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200922}
923
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900924/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900925#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900926({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900927 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900928 if (rc != X86EMUL_CONTINUE) \
929 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900930 (_type)_x; \
931})
932
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900933#define insn_fetch_arr(_arr, _size, _ctxt) \
934({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900935 if (rc != X86EMUL_CONTINUE) \
936 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900937})
938
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000939/*
940 * Given the 'reg' portion of a ModRM byte, and a register block, return a
941 * pointer into the block that addresses the relevant register.
942 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
943 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300944static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000945 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946{
947 void *p;
948
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300950 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
951 else
952 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 return p;
954}
955
956static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200957 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 u16 *size, unsigned long *address, int op_bytes)
959{
960 int rc;
961
962 if (op_bytes == 2)
963 op_bytes = 3;
964 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200965 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900966 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200968 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200969 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 return rc;
971}
972
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200973static u8 test_cc(unsigned int condition, unsigned long flags)
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300974{
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200975 u8 rc;
976 void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300977
Avi Kivity9ae9feb2013-01-19 19:51:52 +0200978 flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
979 asm("pushq %[flags]; popf; call *%[fastop]"
980 : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags));
981 return rc;
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300982}
983
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300984static void fetch_register_operand(struct operand *op)
985{
986 switch (op->bytes) {
987 case 1:
988 op->val = *(u8 *)op->addr.reg;
989 break;
990 case 2:
991 op->val = *(u16 *)op->addr.reg;
992 break;
993 case 4:
994 op->val = *(u32 *)op->addr.reg;
995 break;
996 case 8:
997 op->val = *(u64 *)op->addr.reg;
998 break;
999 }
1000}
1001
Avi Kivity12537912011-03-29 11:41:27 +02001002static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
1003{
1004 ctxt->ops->get_fpu(ctxt);
1005 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001006 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1007 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1008 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1009 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1010 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1011 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1012 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1013 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001014#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001015 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1016 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1017 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1018 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1019 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1020 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1021 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1022 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001023#endif
1024 default: BUG();
1025 }
1026 ctxt->ops->put_fpu(ctxt);
1027}
1028
1029static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1030 int reg)
1031{
1032 ctxt->ops->get_fpu(ctxt);
1033 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001034 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1035 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1036 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1037 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1038 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1039 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1040 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1041 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001042#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001043 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1044 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1045 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1046 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1047 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1048 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1049 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1050 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001051#endif
1052 default: BUG();
1053 }
1054 ctxt->ops->put_fpu(ctxt);
1055}
1056
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001057static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1058{
1059 ctxt->ops->get_fpu(ctxt);
1060 switch (reg) {
1061 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1062 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1063 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1064 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1065 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1066 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1067 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1068 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1069 default: BUG();
1070 }
1071 ctxt->ops->put_fpu(ctxt);
1072}
1073
1074static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1075{
1076 ctxt->ops->get_fpu(ctxt);
1077 switch (reg) {
1078 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1079 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1080 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1081 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1082 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1083 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1084 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1085 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1086 default: BUG();
1087 }
1088 ctxt->ops->put_fpu(ctxt);
1089}
1090
Gleb Natapov045a2822012-12-20 16:57:43 +02001091static int em_fninit(struct x86_emulate_ctxt *ctxt)
1092{
1093 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1094 return emulate_nm(ctxt);
1095
1096 ctxt->ops->get_fpu(ctxt);
1097 asm volatile("fninit");
1098 ctxt->ops->put_fpu(ctxt);
1099 return X86EMUL_CONTINUE;
1100}
1101
1102static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1103{
1104 u16 fcw;
1105
1106 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1107 return emulate_nm(ctxt);
1108
1109 ctxt->ops->get_fpu(ctxt);
1110 asm volatile("fnstcw %0": "+m"(fcw));
1111 ctxt->ops->put_fpu(ctxt);
1112
1113 /* force 2 byte destination */
1114 ctxt->dst.bytes = 2;
1115 ctxt->dst.val = fcw;
1116
1117 return X86EMUL_CONTINUE;
1118}
1119
1120static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1121{
1122 u16 fsw;
1123
1124 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1125 return emulate_nm(ctxt);
1126
1127 ctxt->ops->get_fpu(ctxt);
1128 asm volatile("fnstsw %0": "+m"(fsw));
1129 ctxt->ops->put_fpu(ctxt);
1130
1131 /* force 2 byte destination */
1132 ctxt->dst.bytes = 2;
1133 ctxt->dst.val = fsw;
1134
1135 return X86EMUL_CONTINUE;
1136}
1137
Avi Kivity12537912011-03-29 11:41:27 +02001138static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001139 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001140{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001141 unsigned reg = ctxt->modrm_reg;
1142 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001143
Avi Kivity9dac77f2011-06-01 15:34:25 +03001144 if (!(ctxt->d & ModRM))
1145 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001146
Avi Kivity9dac77f2011-06-01 15:34:25 +03001147 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001148 op->type = OP_XMM;
1149 op->bytes = 16;
1150 op->addr.xmm = reg;
1151 read_sse_reg(ctxt, &op->vec_val, reg);
1152 return;
1153 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001154 if (ctxt->d & Mmx) {
1155 reg &= 7;
1156 op->type = OP_MM;
1157 op->bytes = 8;
1158 op->addr.mm = reg;
1159 return;
1160 }
Avi Kivity12537912011-03-29 11:41:27 +02001161
Avi Kivity3c118e22007-10-31 10:27:04 +02001162 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001163 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001164 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001165 op->bytes = 1;
1166 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001167 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001168 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001169 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001170 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001171 op->orig_val = op->val;
1172}
1173
Avi Kivitya6e34072012-06-10 17:15:39 +03001174static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1175{
1176 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1177 ctxt->modrm_seg = VCPU_SREG_SS;
1178}
1179
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001181 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001182{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001183 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001184 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001185 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001186 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001187
Avi Kivity9dac77f2011-06-01 15:34:25 +03001188 if (ctxt->rex_prefix) {
1189 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1190 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1191 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001192 }
1193
Avi Kivity9dac77f2011-06-01 15:34:25 +03001194 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1195 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1196 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1197 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001198
Avi Kivity9dac77f2011-06-01 15:34:25 +03001199 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001200 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001201 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001202 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001203 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001204 op->type = OP_XMM;
1205 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001206 op->addr.xmm = ctxt->modrm_rm;
1207 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001208 return rc;
1209 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001210 if (ctxt->d & Mmx) {
1211 op->type = OP_MM;
1212 op->bytes = 8;
1213 op->addr.xmm = ctxt->modrm_rm & 7;
1214 return rc;
1215 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001216 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001217 return rc;
1218 }
1219
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001220 op->type = OP_MEM;
1221
Avi Kivity9dac77f2011-06-01 15:34:25 +03001222 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001223 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1224 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1225 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1226 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001227
1228 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001229 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001230 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001231 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001232 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001233 break;
1234 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001235 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001236 break;
1237 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001238 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001239 break;
1240 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001241 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001242 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001243 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001244 break;
1245 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001246 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001247 break;
1248 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001249 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001250 break;
1251 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001252 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001253 break;
1254 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001255 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 break;
1257 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001258 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 break;
1260 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001261 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001262 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001263 break;
1264 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001265 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001266 break;
1267 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001268 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1269 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1270 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001271 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001272 } else {
1273 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001274 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001275 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001276 index_reg |= (sib >> 3) & 7;
1277 base_reg |= sib & 7;
1278 scale = sib >> 6;
1279
Avi Kivity9dac77f2011-06-01 15:34:25 +03001280 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001281 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001282 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001283 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001284 adjust_modrm_seg(ctxt, base_reg);
1285 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001286 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001287 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001288 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001289 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001290 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001291 } else {
1292 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001293 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001294 adjust_modrm_seg(ctxt, base_reg);
1295 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001296 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001297 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001298 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001299 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001300 break;
1301 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001302 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001303 break;
1304 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001305 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001306 break;
1307 }
1308 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001309 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001310done:
1311 return rc;
1312}
1313
1314static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001315 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001316{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001317 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001318
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001319 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001320 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001321 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001322 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001323 break;
1324 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001325 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001326 break;
1327 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001328 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001329 break;
1330 }
1331done:
1332 return rc;
1333}
1334
Avi Kivity9dac77f2011-06-01 15:34:25 +03001335static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001336{
Sheng Yang7129eec2010-09-28 16:33:32 +08001337 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001338
Avi Kivity9dac77f2011-06-01 15:34:25 +03001339 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1340 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001341
Avi Kivity9dac77f2011-06-01 15:34:25 +03001342 if (ctxt->src.bytes == 2)
1343 sv = (s16)ctxt->src.val & (s16)mask;
1344 else if (ctxt->src.bytes == 4)
1345 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001346
Avi Kivity9dac77f2011-06-01 15:34:25 +03001347 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001348 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001349
1350 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001351 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001352}
1353
Gleb Natapov9de41572010-04-28 19:15:22 +03001354static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001355 unsigned long addr, void *dest, unsigned size)
1356{
1357 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001358 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001359
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001360 if (mc->pos < mc->end)
1361 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001362
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001363 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001364
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001365 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1366 &ctxt->exception);
1367 if (rc != X86EMUL_CONTINUE)
1368 return rc;
1369
1370 mc->end += size;
1371
1372read_cached:
1373 memcpy(dest, mc->data + mc->pos, size);
1374 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001375 return X86EMUL_CONTINUE;
1376}
1377
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001378static int segmented_read(struct x86_emulate_ctxt *ctxt,
1379 struct segmented_address addr,
1380 void *data,
1381 unsigned size)
1382{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001383 int rc;
1384 ulong linear;
1385
Avi Kivity83b87952011-04-03 11:31:19 +03001386 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001387 if (rc != X86EMUL_CONTINUE)
1388 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001389 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001390}
1391
1392static int segmented_write(struct x86_emulate_ctxt *ctxt,
1393 struct segmented_address addr,
1394 const void *data,
1395 unsigned size)
1396{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001397 int rc;
1398 ulong linear;
1399
Avi Kivity83b87952011-04-03 11:31:19 +03001400 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001401 if (rc != X86EMUL_CONTINUE)
1402 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001403 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1404 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001405}
1406
1407static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1408 struct segmented_address addr,
1409 const void *orig_data, const void *data,
1410 unsigned size)
1411{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001412 int rc;
1413 ulong linear;
1414
Avi Kivity83b87952011-04-03 11:31:19 +03001415 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001416 if (rc != X86EMUL_CONTINUE)
1417 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001418 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1419 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001420}
1421
Gleb Natapov7b262e92010-03-18 15:20:27 +02001422static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001423 unsigned int size, unsigned short port,
1424 void *dest)
1425{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001426 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001427
1428 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001429 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001430 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001431 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001432 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001433 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1434 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001435 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1436 count);
1437 if (n == 0)
1438 n = 1;
1439 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001440 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001441 return 0;
1442 rc->end = n * size;
1443 }
1444
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001445 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1446 ctxt->dst.data = rc->data + rc->pos;
1447 ctxt->dst.type = OP_MEM_STR;
1448 ctxt->dst.count = (rc->end - rc->pos) / size;
1449 rc->pos = rc->end;
1450 } else {
1451 memcpy(dest, rc->data + rc->pos, size);
1452 rc->pos += size;
1453 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001454 return 1;
1455}
1456
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001457static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1458 u16 index, struct desc_struct *desc)
1459{
1460 struct desc_ptr dt;
1461 ulong addr;
1462
1463 ctxt->ops->get_idt(ctxt, &dt);
1464
1465 if (dt.size < index * 8 + 7)
1466 return emulate_gp(ctxt, index << 3 | 0x2);
1467
1468 addr = dt.address + index * 8;
1469 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1470 &ctxt->exception);
1471}
1472
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001473static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001474 u16 selector, struct desc_ptr *dt)
1475{
Mathias Krause0225fb52012-08-30 01:30:16 +02001476 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001477
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001478 if (selector & 1 << 2) {
1479 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001480 u16 sel;
1481
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001482 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001483 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001484 return;
1485
1486 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1487 dt->address = get_desc_base(&desc);
1488 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001489 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001490}
1491
1492/* allowed just for 8 bytes segments */
1493static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001494 u16 selector, struct desc_struct *desc,
1495 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001496{
1497 struct desc_ptr dt;
1498 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001499 ulong addr;
1500
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001501 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001502
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001503 if (dt.size < index * 8 + 7)
1504 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001505
Avi Kivitye9194642012-06-13 16:29:39 +03001506 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001507 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1508 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001509}
1510
1511/* allowed just for 8 bytes segments */
1512static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001513 u16 selector, struct desc_struct *desc)
1514{
1515 struct desc_ptr dt;
1516 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001517 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001518
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001519 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001520
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001521 if (dt.size < index * 8 + 7)
1522 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523
1524 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001525 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1526 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001527}
1528
Gleb Natapov5601d052011-03-07 14:55:06 +02001529/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001530static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001531 u16 selector, int seg)
1532{
Avi Kivity869be992012-06-13 16:30:53 +03001533 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001534 u8 dpl, rpl, cpl;
1535 unsigned err_vec = GP_VECTOR;
1536 u32 err_code = 0;
1537 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001538 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001539 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001540 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541
1542 memset(&seg_desc, 0, sizeof seg_desc);
1543
1544 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1545 || ctxt->mode == X86EMUL_MODE_REAL) {
1546 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001547 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001548 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001549 goto load;
1550 }
1551
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001552 rpl = selector & 3;
1553 cpl = ctxt->ops->cpl(ctxt);
1554
1555 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1556 if ((seg == VCPU_SREG_CS
1557 || (seg == VCPU_SREG_SS
1558 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1559 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001560 && null_selector)
1561 goto exception;
1562
1563 /* TR should be in GDT only */
1564 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1565 goto exception;
1566
1567 if (null_selector) /* for NULL selector skip all following checks */
1568 goto load;
1569
Avi Kivitye9194642012-06-13 16:29:39 +03001570 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571 if (ret != X86EMUL_CONTINUE)
1572 return ret;
1573
1574 err_code = selector & 0xfffc;
1575 err_vec = GP_VECTOR;
1576
Guo Chaofc058682012-06-28 15:19:51 +08001577 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001578 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1579 goto exception;
1580
1581 if (!seg_desc.p) {
1582 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1583 goto exception;
1584 }
1585
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001586 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001587
1588 switch (seg) {
1589 case VCPU_SREG_SS:
1590 /*
1591 * segment is not a writable data segment or segment
1592 * selector's RPL != CPL or segment selector's RPL != CPL
1593 */
1594 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1595 goto exception;
1596 break;
1597 case VCPU_SREG_CS:
1598 if (!(seg_desc.type & 8))
1599 goto exception;
1600
1601 if (seg_desc.type & 4) {
1602 /* conforming */
1603 if (dpl > cpl)
1604 goto exception;
1605 } else {
1606 /* nonconforming */
1607 if (rpl > cpl || dpl != cpl)
1608 goto exception;
1609 }
1610 /* CS(RPL) <- CPL */
1611 selector = (selector & 0xfffc) | cpl;
1612 break;
1613 case VCPU_SREG_TR:
1614 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1615 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001616 old_desc = seg_desc;
1617 seg_desc.type |= 2; /* busy */
1618 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1619 sizeof(seg_desc), &ctxt->exception);
1620 if (ret != X86EMUL_CONTINUE)
1621 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001622 break;
1623 case VCPU_SREG_LDTR:
1624 if (seg_desc.s || seg_desc.type != 2)
1625 goto exception;
1626 break;
1627 default: /* DS, ES, FS, or GS */
1628 /*
1629 * segment is not a data or readable code segment or
1630 * ((segment is a data or nonconforming code segment)
1631 * and (both RPL and CPL > DPL))
1632 */
1633 if ((seg_desc.type & 0xa) == 0x8 ||
1634 (((seg_desc.type & 0xc) != 0xc) &&
1635 (rpl > dpl && cpl > dpl)))
1636 goto exception;
1637 break;
1638 }
1639
1640 if (seg_desc.s) {
1641 /* mark segment as accessed */
1642 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001643 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001644 if (ret != X86EMUL_CONTINUE)
1645 return ret;
1646 }
1647load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001648 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001649 return X86EMUL_CONTINUE;
1650exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001651 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001652 return X86EMUL_PROPAGATE_FAULT;
1653}
1654
Wei Yongjun31be40b2010-08-17 09:17:30 +08001655static void write_register_operand(struct operand *op)
1656{
1657 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1658 switch (op->bytes) {
1659 case 1:
1660 *(u8 *)op->addr.reg = (u8)op->val;
1661 break;
1662 case 2:
1663 *(u16 *)op->addr.reg = (u16)op->val;
1664 break;
1665 case 4:
1666 *op->addr.reg = (u32)op->val;
1667 break; /* 64b: zero-extend */
1668 case 8:
1669 *op->addr.reg = op->val;
1670 break;
1671 }
1672}
1673
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001674static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001675{
1676 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001677
Avi Kivityb6744dc2013-01-04 16:18:50 +02001678 if (ctxt->d & NoWrite)
1679 return X86EMUL_CONTINUE;
1680
Avi Kivity9dac77f2011-06-01 15:34:25 +03001681 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001682 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001683 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001684 break;
1685 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001686 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001687 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001688 ctxt->dst.addr.mem,
1689 &ctxt->dst.orig_val,
1690 &ctxt->dst.val,
1691 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001692 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001693 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001694 ctxt->dst.addr.mem,
1695 &ctxt->dst.val,
1696 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001697 if (rc != X86EMUL_CONTINUE)
1698 return rc;
1699 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001700 case OP_MEM_STR:
1701 rc = segmented_write(ctxt,
1702 ctxt->dst.addr.mem,
1703 ctxt->dst.data,
1704 ctxt->dst.bytes * ctxt->dst.count);
1705 if (rc != X86EMUL_CONTINUE)
1706 return rc;
1707 break;
Avi Kivity12537912011-03-29 11:41:27 +02001708 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001709 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001710 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001711 case OP_MM:
1712 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1713 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001714 case OP_NONE:
1715 /* no writeback */
1716 break;
1717 default:
1718 break;
1719 }
1720 return X86EMUL_CONTINUE;
1721}
1722
Avi Kivity51ddff52012-06-12 20:19:40 +03001723static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001724{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001725 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001726
Avi Kivity5ad105e2012-08-19 14:34:31 +03001727 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001728 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001729 addr.seg = VCPU_SREG_SS;
1730
Avi Kivity51ddff52012-06-12 20:19:40 +03001731 return segmented_write(ctxt, addr, data, bytes);
1732}
1733
1734static int em_push(struct x86_emulate_ctxt *ctxt)
1735{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001736 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001737 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001738 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739}
1740
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001741static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001742 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001743{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001744 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001745 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001746
Avi Kivitydd856ef2012-08-27 23:46:17 +03001747 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001748 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001749 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001750 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001751 return rc;
1752
Avi Kivity5ad105e2012-08-19 14:34:31 +03001753 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001754 return rc;
1755}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001756
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001757static int em_pop(struct x86_emulate_ctxt *ctxt)
1758{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001759 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001760}
1761
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001762static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001763 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001764{
1765 int rc;
1766 unsigned long val, change_mask;
1767 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001768 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001769
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001770 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001771 if (rc != X86EMUL_CONTINUE)
1772 return rc;
1773
1774 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1775 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1776
1777 switch(ctxt->mode) {
1778 case X86EMUL_MODE_PROT64:
1779 case X86EMUL_MODE_PROT32:
1780 case X86EMUL_MODE_PROT16:
1781 if (cpl == 0)
1782 change_mask |= EFLG_IOPL;
1783 if (cpl <= iopl)
1784 change_mask |= EFLG_IF;
1785 break;
1786 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001787 if (iopl < 3)
1788 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001789 change_mask |= EFLG_IF;
1790 break;
1791 default: /* real mode */
1792 change_mask |= (EFLG_IOPL | EFLG_IF);
1793 break;
1794 }
1795
1796 *(unsigned long *)dest =
1797 (ctxt->eflags & ~change_mask) | (val & change_mask);
1798
1799 return rc;
1800}
1801
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001802static int em_popf(struct x86_emulate_ctxt *ctxt)
1803{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001804 ctxt->dst.type = OP_REG;
1805 ctxt->dst.addr.reg = &ctxt->eflags;
1806 ctxt->dst.bytes = ctxt->op_bytes;
1807 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001808}
1809
Avi Kivity612e89f2012-06-12 20:03:23 +03001810static int em_enter(struct x86_emulate_ctxt *ctxt)
1811{
1812 int rc;
1813 unsigned frame_size = ctxt->src.val;
1814 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001815 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001816
1817 if (nesting_level)
1818 return X86EMUL_UNHANDLEABLE;
1819
Avi Kivitydd856ef2012-08-27 23:46:17 +03001820 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1821 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001822 if (rc != X86EMUL_CONTINUE)
1823 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001824 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001825 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001826 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1827 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001828 stack_mask(ctxt));
1829 return X86EMUL_CONTINUE;
1830}
1831
Avi Kivityf47cfa32012-06-07 17:49:24 +03001832static int em_leave(struct x86_emulate_ctxt *ctxt)
1833{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001834 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001835 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001836 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001837}
1838
Avi Kivity1cd196e2011-09-13 10:45:51 +03001839static int em_push_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;
1842
Avi Kivity9dac77f2011-06-01 15:34:25 +03001843 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001844
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001845 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001846}
1847
Avi Kivity1cd196e2011-09-13 10:45:51 +03001848static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001849{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001850 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001851 unsigned long selector;
1852 int rc;
1853
Avi Kivity9dac77f2011-06-01 15:34:25 +03001854 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001855 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001856 return rc;
1857
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001858 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001859 return rc;
1860}
1861
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001862static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001863{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001864 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001865 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001866 int reg = VCPU_REGS_RAX;
1867
1868 while (reg <= VCPU_REGS_RDI) {
1869 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001870 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001871
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001872 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001873 if (rc != X86EMUL_CONTINUE)
1874 return rc;
1875
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001876 ++reg;
1877 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001878
Wei Yongjunc37eda12010-06-15 09:03:33 +08001879 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001880}
1881
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001882static int em_pushf(struct x86_emulate_ctxt *ctxt)
1883{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001884 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001885 return em_push(ctxt);
1886}
1887
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001888static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001889{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001890 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001891 int reg = VCPU_REGS_RDI;
1892
1893 while (reg >= VCPU_REGS_RAX) {
1894 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001895 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001896 --reg;
1897 }
1898
Avi Kivitydd856ef2012-08-27 23:46:17 +03001899 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001900 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001901 break;
1902 --reg;
1903 }
1904 return rc;
1905}
1906
Avi Kivitydd856ef2012-08-27 23:46:17 +03001907static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001908{
Mathias Krause0225fb52012-08-30 01:30:16 +02001909 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001910 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001911 struct desc_ptr dt;
1912 gva_t cs_addr;
1913 gva_t eip_addr;
1914 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001915
1916 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001917 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001918 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001919 if (rc != X86EMUL_CONTINUE)
1920 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001921
1922 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1923
Avi Kivity9dac77f2011-06-01 15:34:25 +03001924 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001925 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001926 if (rc != X86EMUL_CONTINUE)
1927 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001928
Avi Kivity9dac77f2011-06-01 15:34:25 +03001929 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001930 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001931 if (rc != X86EMUL_CONTINUE)
1932 return rc;
1933
Avi Kivity4bff1e862011-04-20 13:37:53 +03001934 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001935
1936 eip_addr = dt.address + (irq << 2);
1937 cs_addr = dt.address + (irq << 2) + 2;
1938
Avi Kivity0f65dd72011-04-20 13:37:53 +03001939 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001940 if (rc != X86EMUL_CONTINUE)
1941 return rc;
1942
Avi Kivity0f65dd72011-04-20 13:37:53 +03001943 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001944 if (rc != X86EMUL_CONTINUE)
1945 return rc;
1946
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001947 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001948 if (rc != X86EMUL_CONTINUE)
1949 return rc;
1950
Avi Kivity9dac77f2011-06-01 15:34:25 +03001951 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001952
1953 return rc;
1954}
1955
Avi Kivitydd856ef2012-08-27 23:46:17 +03001956int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1957{
1958 int rc;
1959
1960 invalidate_registers(ctxt);
1961 rc = __emulate_int_real(ctxt, irq);
1962 if (rc == X86EMUL_CONTINUE)
1963 writeback_registers(ctxt);
1964 return rc;
1965}
1966
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001967static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001968{
1969 switch(ctxt->mode) {
1970 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001971 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001972 case X86EMUL_MODE_VM86:
1973 case X86EMUL_MODE_PROT16:
1974 case X86EMUL_MODE_PROT32:
1975 case X86EMUL_MODE_PROT64:
1976 default:
1977 /* Protected mode interrupts unimplemented yet */
1978 return X86EMUL_UNHANDLEABLE;
1979 }
1980}
1981
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001982static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001983{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001984 int rc = X86EMUL_CONTINUE;
1985 unsigned long temp_eip = 0;
1986 unsigned long temp_eflags = 0;
1987 unsigned long cs = 0;
1988 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1989 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1990 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1991 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1992
1993 /* TODO: Add stack limit check */
1994
Avi Kivity9dac77f2011-06-01 15:34:25 +03001995 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001996
1997 if (rc != X86EMUL_CONTINUE)
1998 return rc;
1999
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002000 if (temp_eip & ~0xffff)
2001 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002002
Avi Kivity9dac77f2011-06-01 15:34:25 +03002003 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002004
2005 if (rc != X86EMUL_CONTINUE)
2006 return rc;
2007
Avi Kivity9dac77f2011-06-01 15:34:25 +03002008 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002009
2010 if (rc != X86EMUL_CONTINUE)
2011 return rc;
2012
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002013 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002014
2015 if (rc != X86EMUL_CONTINUE)
2016 return rc;
2017
Avi Kivity9dac77f2011-06-01 15:34:25 +03002018 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002019
2020
Avi Kivity9dac77f2011-06-01 15:34:25 +03002021 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002022 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002023 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002024 ctxt->eflags &= ~0xffff;
2025 ctxt->eflags |= temp_eflags;
2026 }
2027
2028 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2029 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2030
2031 return rc;
2032}
2033
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002034static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002035{
2036 switch(ctxt->mode) {
2037 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002038 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002039 case X86EMUL_MODE_VM86:
2040 case X86EMUL_MODE_PROT16:
2041 case X86EMUL_MODE_PROT32:
2042 case X86EMUL_MODE_PROT64:
2043 default:
2044 /* iret from protected mode unimplemented yet */
2045 return X86EMUL_UNHANDLEABLE;
2046 }
2047}
2048
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002049static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2050{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002051 int rc;
2052 unsigned short sel;
2053
Avi Kivity9dac77f2011-06-01 15:34:25 +03002054 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002055
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002056 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002057 if (rc != X86EMUL_CONTINUE)
2058 return rc;
2059
Avi Kivity9dac77f2011-06-01 15:34:25 +03002060 ctxt->_eip = 0;
2061 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002062 return X86EMUL_CONTINUE;
2063}
2064
Avi Kivity45a14672013-01-04 16:18:52 +02002065FASTOP1(not);
2066FASTOP1(neg);
Avi Kivity95413dc2013-01-19 19:51:53 +02002067FASTOP1(inc);
2068FASTOP1(dec);
Avi Kivity3329ece2011-09-13 10:45:39 +03002069
Avi Kivity007a3b52013-01-19 19:51:51 +02002070FASTOP2CL(rol);
2071FASTOP2CL(ror);
2072FASTOP2CL(rcl);
2073FASTOP2CL(rcr);
2074FASTOP2CL(shl);
2075FASTOP2CL(shr);
2076FASTOP2CL(sar);
2077
Avi Kivity11c363b2013-01-19 19:51:54 +02002078FASTOP2W(bsf);
2079FASTOP2W(bsr);
2080FASTOP2W(bt);
2081FASTOP2W(bts);
2082FASTOP2W(btr);
2083FASTOP2W(btc);
2084
Avi Kivity3329ece2011-09-13 10:45:39 +03002085static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2086{
2087 u8 ex = 0;
2088
2089 emulate_1op_rax_rdx(ctxt, "mul", ex);
2090 return X86EMUL_CONTINUE;
2091}
2092
2093static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2094{
2095 u8 ex = 0;
2096
2097 emulate_1op_rax_rdx(ctxt, "imul", ex);
2098 return X86EMUL_CONTINUE;
2099}
2100
2101static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002102{
Avi Kivity34d1f492010-08-26 11:59:01 +03002103 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002104
Avi Kivity3329ece2011-09-13 10:45:39 +03002105 emulate_1op_rax_rdx(ctxt, "div", de);
2106 if (de)
2107 return emulate_de(ctxt);
2108 return X86EMUL_CONTINUE;
2109}
2110
2111static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2112{
2113 u8 de = 0;
2114
2115 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002116 if (de)
2117 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002118 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002119}
2120
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002121static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002122{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002123 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124
Avi Kivity9dac77f2011-06-01 15:34:25 +03002125 switch (ctxt->modrm_reg) {
Mohammed Gamald19292e2008-09-08 21:47:19 +03002126 case 2: /* call near abs */ {
2127 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002128 old_eip = ctxt->_eip;
2129 ctxt->_eip = ctxt->src.val;
2130 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002131 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002132 break;
2133 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002134 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002135 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002136 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002137 case 5: /* jmp far */
2138 rc = em_jmp_far(ctxt);
2139 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002140 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002141 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002142 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002143 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002144 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002145}
2146
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002147static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002148{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002149 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002150
Avi Kivitydd856ef2012-08-27 23:46:17 +03002151 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2152 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2153 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2154 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002155 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002156 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002157 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2158 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002159
Laurent Vivier05f086f2007-09-24 11:10:55 +02002160 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002161 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002162 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002163}
2164
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002165static int em_ret(struct x86_emulate_ctxt *ctxt)
2166{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002167 ctxt->dst.type = OP_REG;
2168 ctxt->dst.addr.reg = &ctxt->_eip;
2169 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002170 return em_pop(ctxt);
2171}
2172
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002173static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002174{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002175 int rc;
2176 unsigned long cs;
2177
Avi Kivity9dac77f2011-06-01 15:34:25 +03002178 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002179 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002180 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002181 if (ctxt->op_bytes == 4)
2182 ctxt->_eip = (u32)ctxt->_eip;
2183 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002184 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002185 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002186 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002187 return rc;
2188}
2189
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002190static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2191{
2192 /* Save real source value, then compare EAX against destination. */
2193 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002194 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002195 emulate_2op_SrcV(ctxt, "cmp");
2196
2197 if (ctxt->eflags & EFLG_ZF) {
2198 /* Success: write back to memory. */
2199 ctxt->dst.val = ctxt->src.orig_val;
2200 } else {
2201 /* Failure: write the value we saw to EAX. */
2202 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002203 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002204 }
2205 return X86EMUL_CONTINUE;
2206}
2207
Avi Kivityd4b43252011-09-13 10:45:50 +03002208static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002209{
Avi Kivityd4b43252011-09-13 10:45:50 +03002210 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002211 unsigned short sel;
2212 int rc;
2213
Avi Kivity9dac77f2011-06-01 15:34:25 +03002214 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002215
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002216 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002217 if (rc != X86EMUL_CONTINUE)
2218 return rc;
2219
Avi Kivity9dac77f2011-06-01 15:34:25 +03002220 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002221 return rc;
2222}
2223
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002224static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002225setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002226 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002227{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002228 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002229 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002230 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002231 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002232 cs->type = 0x0b; /* Read, Execute, Accessed */
2233 cs->s = 1;
2234 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002235 cs->p = 1;
2236 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002237 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002238
Gleb Natapov79168fd2010-04-28 19:15:30 +03002239 set_desc_base(ss, 0); /* flat segment */
2240 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002241 ss->g = 1; /* 4kb granularity */
2242 ss->s = 1;
2243 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002244 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002245 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002246 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002247 ss->l = 0;
2248 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002249}
2250
Avi Kivity1a18a692012-02-01 12:23:21 +02002251static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2252{
2253 u32 eax, ebx, ecx, edx;
2254
2255 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002256 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2257 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002258 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2259 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2260}
2261
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002262static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2263{
Mathias Krause0225fb52012-08-30 01:30:16 +02002264 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002265 u32 eax, ebx, ecx, edx;
2266
2267 /*
2268 * syscall should always be enabled in longmode - so only become
2269 * vendor specific (cpuid) if other modes are active...
2270 */
2271 if (ctxt->mode == X86EMUL_MODE_PROT64)
2272 return true;
2273
2274 eax = 0x00000000;
2275 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002276 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2277 /*
2278 * Intel ("GenuineIntel")
2279 * remark: Intel CPUs only support "syscall" in 64bit
2280 * longmode. Also an 64bit guest with a
2281 * 32bit compat-app running will #UD !! While this
2282 * behaviour can be fixed (by emulating) into AMD
2283 * response - CPUs of AMD can't behave like Intel.
2284 */
2285 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2286 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2287 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2288 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002289
Avi Kivity0017f932012-06-07 14:10:16 +03002290 /* AMD ("AuthenticAMD") */
2291 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2292 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2293 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2294 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002295
Avi Kivity0017f932012-06-07 14:10:16 +03002296 /* AMD ("AMDisbetter!") */
2297 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2298 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2299 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2300 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002301
2302 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2303 return false;
2304}
2305
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002306static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002307{
Mathias Krause0225fb52012-08-30 01:30:16 +02002308 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002309 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002310 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002311 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002312 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002313
2314 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002315 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002316 ctxt->mode == X86EMUL_MODE_VM86)
2317 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002318
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002319 if (!(em_syscall_is_enabled(ctxt)))
2320 return emulate_ud(ctxt);
2321
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002322 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002323 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002324
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002325 if (!(efer & EFER_SCE))
2326 return emulate_ud(ctxt);
2327
Avi Kivity717746e2011-04-20 13:37:53 +03002328 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002329 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002330 cs_sel = (u16)(msr_data & 0xfffc);
2331 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002332
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002333 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002334 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002335 cs.l = 1;
2336 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002337 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2338 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339
Avi Kivitydd856ef2012-08-27 23:46:17 +03002340 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002341 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002342#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002343 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002344
Avi Kivity717746e2011-04-20 13:37:53 +03002345 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002346 ctxt->mode == X86EMUL_MODE_PROT64 ?
2347 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002348 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002349
Avi Kivity717746e2011-04-20 13:37:53 +03002350 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002351 ctxt->eflags &= ~(msr_data | EFLG_RF);
2352#endif
2353 } else {
2354 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002355 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002356 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002357
2358 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2359 }
2360
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002361 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002362}
2363
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002364static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002365{
Mathias Krause0225fb52012-08-30 01:30:16 +02002366 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002367 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002368 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002369 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002370 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002371
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002372 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002373 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002374 if (ctxt->mode == X86EMUL_MODE_REAL)
2375 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002376
Avi Kivity1a18a692012-02-01 12:23:21 +02002377 /*
2378 * Not recognized on AMD in compat mode (but is recognized in legacy
2379 * mode).
2380 */
2381 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2382 && !vendor_intel(ctxt))
2383 return emulate_ud(ctxt);
2384
Andre Przywara8c604352009-06-18 12:56:01 +02002385 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2386 * Therefore, we inject an #UD.
2387 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002388 if (ctxt->mode == X86EMUL_MODE_PROT64)
2389 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002390
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002391 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002392
Avi Kivity717746e2011-04-20 13:37:53 +03002393 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002394 switch (ctxt->mode) {
2395 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002396 if ((msr_data & 0xfffc) == 0x0)
2397 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002398 break;
2399 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002400 if (msr_data == 0x0)
2401 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002402 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002403 default:
2404 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002405 }
2406
2407 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002408 cs_sel = (u16)msr_data;
2409 cs_sel &= ~SELECTOR_RPL_MASK;
2410 ss_sel = cs_sel + 8;
2411 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002412 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002413 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002414 cs.l = 1;
2415 }
2416
Avi Kivity1aa36612011-04-27 13:20:30 +03002417 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2418 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002419
Avi Kivity717746e2011-04-20 13:37:53 +03002420 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002421 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002422
Avi Kivity717746e2011-04-20 13:37:53 +03002423 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002424 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002425
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002426 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002427}
2428
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002429static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002430{
Mathias Krause0225fb52012-08-30 01:30:16 +02002431 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002432 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002433 u64 msr_data;
2434 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002435 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002436
Gleb Natapova0044752010-02-10 14:21:31 +02002437 /* inject #GP if in real mode or Virtual 8086 mode */
2438 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002439 ctxt->mode == X86EMUL_MODE_VM86)
2440 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002441
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002442 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002443
Avi Kivity9dac77f2011-06-01 15:34:25 +03002444 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002445 usermode = X86EMUL_MODE_PROT64;
2446 else
2447 usermode = X86EMUL_MODE_PROT32;
2448
2449 cs.dpl = 3;
2450 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002451 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002452 switch (usermode) {
2453 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002454 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002455 if ((msr_data & 0xfffc) == 0x0)
2456 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002457 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002458 break;
2459 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002460 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002461 if (msr_data == 0x0)
2462 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002463 ss_sel = cs_sel + 8;
2464 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002465 cs.l = 1;
2466 break;
2467 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002468 cs_sel |= SELECTOR_RPL_MASK;
2469 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002470
Avi Kivity1aa36612011-04-27 13:20:30 +03002471 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2472 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002473
Avi Kivitydd856ef2012-08-27 23:46:17 +03002474 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2475 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002476
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002477 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002478}
2479
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002480static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002481{
2482 int iopl;
2483 if (ctxt->mode == X86EMUL_MODE_REAL)
2484 return false;
2485 if (ctxt->mode == X86EMUL_MODE_VM86)
2486 return true;
2487 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002488 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002489}
2490
2491static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002492 u16 port, u16 len)
2493{
Mathias Krause0225fb52012-08-30 01:30:16 +02002494 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002495 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002496 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002497 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002498 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002499 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002500 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002501
Avi Kivity1aa36612011-04-27 13:20:30 +03002502 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002503 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002504 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002505 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002506 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002507 base = get_desc_base(&tr_seg);
2508#ifdef CONFIG_X86_64
2509 base |= ((u64)base3) << 32;
2510#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002511 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002512 if (r != X86EMUL_CONTINUE)
2513 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002514 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002515 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002516 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002517 if (r != X86EMUL_CONTINUE)
2518 return false;
2519 if ((perm >> bit_idx) & mask)
2520 return false;
2521 return true;
2522}
2523
2524static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002525 u16 port, u16 len)
2526{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002527 if (ctxt->perm_ok)
2528 return true;
2529
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002530 if (emulator_bad_iopl(ctxt))
2531 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002532 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002533
2534 ctxt->perm_ok = true;
2535
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002536 return true;
2537}
2538
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002539static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002540 struct tss_segment_16 *tss)
2541{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002542 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002543 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002544 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2545 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2546 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2547 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2548 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2549 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2550 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2551 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002552
Avi Kivity1aa36612011-04-27 13:20:30 +03002553 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2554 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2555 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2556 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2557 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002558}
2559
2560static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002561 struct tss_segment_16 *tss)
2562{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002563 int ret;
2564
Avi Kivity9dac77f2011-06-01 15:34:25 +03002565 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002566 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002567 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2568 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2569 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2570 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2571 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2572 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2573 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2574 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575
2576 /*
2577 * SDM says that segment selectors are loaded before segment
2578 * descriptors
2579 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002580 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2581 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2582 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2583 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2584 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002585
2586 /*
Guo Chaofc058682012-06-28 15:19:51 +08002587 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002588 * it is handled in a context of new task
2589 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002590 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002591 if (ret != X86EMUL_CONTINUE)
2592 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002593 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002594 if (ret != X86EMUL_CONTINUE)
2595 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002596 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002597 if (ret != X86EMUL_CONTINUE)
2598 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002599 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 if (ret != X86EMUL_CONTINUE)
2601 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002602 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002603 if (ret != X86EMUL_CONTINUE)
2604 return ret;
2605
2606 return X86EMUL_CONTINUE;
2607}
2608
2609static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002610 u16 tss_selector, u16 old_tss_sel,
2611 ulong old_tss_base, struct desc_struct *new_desc)
2612{
Mathias Krause0225fb52012-08-30 01:30:16 +02002613 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002614 struct tss_segment_16 tss_seg;
2615 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002616 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002617
Avi Kivity0f65dd72011-04-20 13:37:53 +03002618 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002619 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002620 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002622 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002623
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002624 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625
Avi Kivity0f65dd72011-04-20 13:37:53 +03002626 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002627 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002628 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002629 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002630 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002631
Avi Kivity0f65dd72011-04-20 13:37:53 +03002632 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002633 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002634 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002635 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637
2638 if (old_tss_sel != 0xffff) {
2639 tss_seg.prev_task_link = old_tss_sel;
2640
Avi Kivity0f65dd72011-04-20 13:37:53 +03002641 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002642 &tss_seg.prev_task_link,
2643 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002644 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002645 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002646 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002647 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 }
2649
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002650 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002651}
2652
2653static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002654 struct tss_segment_32 *tss)
2655{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002656 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002657 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002658 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002659 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2660 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2661 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2662 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2663 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2664 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2665 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2666 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002667
Avi Kivity1aa36612011-04-27 13:20:30 +03002668 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2669 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2670 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2671 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2672 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2673 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2674 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675}
2676
2677static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002678 struct tss_segment_32 *tss)
2679{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002680 int ret;
2681
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002682 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002683 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002684 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002685 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002686
2687 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002688 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2689 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2690 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2691 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2692 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2693 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2694 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2695 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002696
2697 /*
2698 * SDM says that segment selectors are loaded before segment
2699 * descriptors
2700 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002701 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2702 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2703 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2704 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2705 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2706 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2707 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002708
2709 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002710 * If we're switching between Protected Mode and VM86, we need to make
2711 * sure to update the mode before loading the segment descriptors so
2712 * that the selectors are interpreted correctly.
2713 *
2714 * Need to get rflags to the vcpu struct immediately because it
2715 * influences the CPL which is checked at least when loading the segment
2716 * descriptors and when pushing an error code to the new kernel stack.
2717 *
2718 * TODO Introduce a separate ctxt->ops->set_cpl callback
2719 */
2720 if (ctxt->eflags & X86_EFLAGS_VM)
2721 ctxt->mode = X86EMUL_MODE_VM86;
2722 else
2723 ctxt->mode = X86EMUL_MODE_PROT32;
2724
2725 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2726
2727 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 * Now load segment descriptors. If fault happenes at this stage
2729 * it is handled in a context of new task
2730 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002731 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002732 if (ret != X86EMUL_CONTINUE)
2733 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002734 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002735 if (ret != X86EMUL_CONTINUE)
2736 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002737 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002738 if (ret != X86EMUL_CONTINUE)
2739 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002740 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002741 if (ret != X86EMUL_CONTINUE)
2742 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002743 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002744 if (ret != X86EMUL_CONTINUE)
2745 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002746 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747 if (ret != X86EMUL_CONTINUE)
2748 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002749 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002750 if (ret != X86EMUL_CONTINUE)
2751 return ret;
2752
2753 return X86EMUL_CONTINUE;
2754}
2755
2756static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002757 u16 tss_selector, u16 old_tss_sel,
2758 ulong old_tss_base, struct desc_struct *new_desc)
2759{
Mathias Krause0225fb52012-08-30 01:30:16 +02002760 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002761 struct tss_segment_32 tss_seg;
2762 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002763 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002764
Avi Kivity0f65dd72011-04-20 13:37:53 +03002765 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002766 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002767 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002769 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002770
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002771 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772
Avi Kivity0f65dd72011-04-20 13:37:53 +03002773 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002774 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002775 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002776 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002777 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002778
Avi Kivity0f65dd72011-04-20 13:37:53 +03002779 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002780 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002781 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002782 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784
2785 if (old_tss_sel != 0xffff) {
2786 tss_seg.prev_task_link = old_tss_sel;
2787
Avi Kivity0f65dd72011-04-20 13:37:53 +03002788 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002789 &tss_seg.prev_task_link,
2790 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002791 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002792 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002794 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795 }
2796
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002797 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798}
2799
2800static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002801 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002802 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002803{
Mathias Krause0225fb52012-08-30 01:30:16 +02002804 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002805 struct desc_struct curr_tss_desc, next_tss_desc;
2806 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002807 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002808 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002809 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002810 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002811 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002812
2813 /* FIXME: old_tss_base == ~0 ? */
2814
Avi Kivitye9194642012-06-13 16:29:39 +03002815 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002816 if (ret != X86EMUL_CONTINUE)
2817 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002818 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002819 if (ret != X86EMUL_CONTINUE)
2820 return ret;
2821
2822 /* FIXME: check that next_tss_desc is tss */
2823
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002824 /*
2825 * Check privileges. The three cases are task switch caused by...
2826 *
2827 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2828 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002829 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002830 */
2831 if (reason == TASK_SWITCH_GATE) {
2832 if (idt_index != -1) {
2833 /* Software interrupts */
2834 struct desc_struct task_gate_desc;
2835 int dpl;
2836
2837 ret = read_interrupt_descriptor(ctxt, idt_index,
2838 &task_gate_desc);
2839 if (ret != X86EMUL_CONTINUE)
2840 return ret;
2841
2842 dpl = task_gate_desc.dpl;
2843 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2844 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2845 }
2846 } else if (reason != TASK_SWITCH_IRET) {
2847 int dpl = next_tss_desc.dpl;
2848 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2849 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002850 }
2851
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002852
Gleb Natapovceffb452010-03-18 15:20:19 +02002853 desc_limit = desc_limit_scaled(&next_tss_desc);
2854 if (!next_tss_desc.p ||
2855 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2856 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002857 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002858 return X86EMUL_PROPAGATE_FAULT;
2859 }
2860
2861 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2862 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002863 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002864 }
2865
2866 if (reason == TASK_SWITCH_IRET)
2867 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2868
2869 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002870 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002871 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2872 old_tss_sel = 0xffff;
2873
2874 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002875 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002876 old_tss_base, &next_tss_desc);
2877 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002878 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002879 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002880 if (ret != X86EMUL_CONTINUE)
2881 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002882
2883 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2884 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2885
2886 if (reason != TASK_SWITCH_IRET) {
2887 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002888 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002889 }
2890
Avi Kivity717746e2011-04-20 13:37:53 +03002891 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002892 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002893
Jan Kiszkae269fb22010-04-14 15:51:09 +02002894 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002895 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2896 ctxt->lock_prefix = 0;
2897 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002898 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002899 }
2900
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002901 return ret;
2902}
2903
2904int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002905 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002906 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002907{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002908 int rc;
2909
Avi Kivitydd856ef2012-08-27 23:46:17 +03002910 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002911 ctxt->_eip = ctxt->eip;
2912 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002913
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002914 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002915 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002916
Avi Kivitydd856ef2012-08-27 23:46:17 +03002917 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002918 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002919 writeback_registers(ctxt);
2920 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002921
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002922 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002923}
2924
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002925static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2926 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002927{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002928 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002929
Avi Kivitydd856ef2012-08-27 23:46:17 +03002930 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2931 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002932}
2933
Avi Kivity7af04fc2010-08-18 14:16:35 +03002934static int em_das(struct x86_emulate_ctxt *ctxt)
2935{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002936 u8 al, old_al;
2937 bool af, cf, old_cf;
2938
2939 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002940 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002941
2942 old_al = al;
2943 old_cf = cf;
2944 cf = false;
2945 af = ctxt->eflags & X86_EFLAGS_AF;
2946 if ((al & 0x0f) > 9 || af) {
2947 al -= 6;
2948 cf = old_cf | (al >= 250);
2949 af = true;
2950 } else {
2951 af = false;
2952 }
2953 if (old_al > 0x99 || old_cf) {
2954 al -= 0x60;
2955 cf = true;
2956 }
2957
Avi Kivity9dac77f2011-06-01 15:34:25 +03002958 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002959 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002960 ctxt->src.type = OP_IMM;
2961 ctxt->src.val = 0;
2962 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002963 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002964 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2965 if (cf)
2966 ctxt->eflags |= X86_EFLAGS_CF;
2967 if (af)
2968 ctxt->eflags |= X86_EFLAGS_AF;
2969 return X86EMUL_CONTINUE;
2970}
2971
Gleb Natapov7f662272012-12-10 11:42:30 +02002972static int em_aad(struct x86_emulate_ctxt *ctxt)
2973{
2974 u8 al = ctxt->dst.val & 0xff;
2975 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2976
2977 al = (al + (ah * ctxt->src.val)) & 0xff;
2978
2979 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2980
2981 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2982
2983 if (!al)
2984 ctxt->eflags |= X86_EFLAGS_ZF;
2985 if (!(al & 1))
2986 ctxt->eflags |= X86_EFLAGS_PF;
2987 if (al & 0x80)
2988 ctxt->eflags |= X86_EFLAGS_SF;
2989
2990 return X86EMUL_CONTINUE;
2991}
2992
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002993static int em_call(struct x86_emulate_ctxt *ctxt)
2994{
2995 long rel = ctxt->src.val;
2996
2997 ctxt->src.val = (unsigned long)ctxt->_eip;
2998 jmp_rel(ctxt, rel);
2999 return em_push(ctxt);
3000}
3001
Avi Kivity0ef753b2010-08-18 14:51:45 +03003002static int em_call_far(struct x86_emulate_ctxt *ctxt)
3003{
Avi Kivity0ef753b2010-08-18 14:51:45 +03003004 u16 sel, old_cs;
3005 ulong old_eip;
3006 int rc;
3007
Avi Kivity1aa36612011-04-27 13:20:30 +03003008 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003009 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003010
Avi Kivity9dac77f2011-06-01 15:34:25 +03003011 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003012 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003013 return X86EMUL_CONTINUE;
3014
Avi Kivity9dac77f2011-06-01 15:34:25 +03003015 ctxt->_eip = 0;
3016 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003017
Avi Kivity9dac77f2011-06-01 15:34:25 +03003018 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003019 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003020 if (rc != X86EMUL_CONTINUE)
3021 return rc;
3022
Avi Kivity9dac77f2011-06-01 15:34:25 +03003023 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003024 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003025}
3026
Avi Kivity40ece7c2010-08-18 15:12:09 +03003027static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3028{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003029 int rc;
3030
Avi Kivity9dac77f2011-06-01 15:34:25 +03003031 ctxt->dst.type = OP_REG;
3032 ctxt->dst.addr.reg = &ctxt->_eip;
3033 ctxt->dst.bytes = ctxt->op_bytes;
3034 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003035 if (rc != X86EMUL_CONTINUE)
3036 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003037 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003038 return X86EMUL_CONTINUE;
3039}
3040
Avi Kivityfb864fb2013-01-04 16:18:54 +02003041FASTOP2(add);
3042FASTOP2(or);
3043FASTOP2(adc);
3044FASTOP2(sbb);
3045FASTOP2(and);
3046FASTOP2(sub);
3047FASTOP2(xor);
3048FASTOP2(cmp);
3049FASTOP2(test);
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003050
Avi Kivity0bdea062013-01-19 19:51:50 +02003051FASTOP3WCL(shld);
3052FASTOP3WCL(shrd);
3053
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003054static int em_xchg(struct x86_emulate_ctxt *ctxt)
3055{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003056 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003057 ctxt->src.val = ctxt->dst.val;
3058 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003059
3060 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003061 ctxt->dst.val = ctxt->src.orig_val;
3062 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003063 return X86EMUL_CONTINUE;
3064}
3065
Avi Kivity5c82aa22010-08-18 18:31:43 +03003066static int em_imul(struct x86_emulate_ctxt *ctxt)
3067{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003068 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03003069 return X86EMUL_CONTINUE;
3070}
3071
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003072static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3073{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003074 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03003075 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003076}
3077
Avi Kivity61429142010-08-19 15:13:00 +03003078static int em_cwd(struct x86_emulate_ctxt *ctxt)
3079{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003080 ctxt->dst.type = OP_REG;
3081 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003082 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003083 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003084
3085 return X86EMUL_CONTINUE;
3086}
3087
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03003088static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3089{
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03003090 u64 tsc = 0;
3091
Avi Kivity717746e2011-04-20 13:37:53 +03003092 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003093 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3094 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d3c42010-08-18 18:54:34 +03003095 return X86EMUL_CONTINUE;
3096}
3097
Avi Kivity222d21a2011-11-10 14:57:30 +02003098static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3099{
3100 u64 pmc;
3101
Avi Kivitydd856ef2012-08-27 23:46:17 +03003102 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003103 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003104 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3105 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003106 return X86EMUL_CONTINUE;
3107}
3108
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003109static int em_mov(struct x86_emulate_ctxt *ctxt)
3110{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003111 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003112 return X86EMUL_CONTINUE;
3113}
3114
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003115static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3116{
3117 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3118 return emulate_gp(ctxt, 0);
3119
3120 /* Disable writeback. */
3121 ctxt->dst.type = OP_NONE;
3122 return X86EMUL_CONTINUE;
3123}
3124
3125static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3126{
3127 unsigned long val;
3128
3129 if (ctxt->mode == X86EMUL_MODE_PROT64)
3130 val = ctxt->src.val & ~0ULL;
3131 else
3132 val = ctxt->src.val & ~0U;
3133
3134 /* #UD condition is already handled. */
3135 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3136 return emulate_gp(ctxt, 0);
3137
3138 /* Disable writeback. */
3139 ctxt->dst.type = OP_NONE;
3140 return X86EMUL_CONTINUE;
3141}
3142
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003143static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3144{
3145 u64 msr_data;
3146
Avi Kivitydd856ef2012-08-27 23:46:17 +03003147 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3148 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3149 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003150 return emulate_gp(ctxt, 0);
3151
3152 return X86EMUL_CONTINUE;
3153}
3154
3155static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3156{
3157 u64 msr_data;
3158
Avi Kivitydd856ef2012-08-27 23:46:17 +03003159 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003160 return emulate_gp(ctxt, 0);
3161
Avi Kivitydd856ef2012-08-27 23:46:17 +03003162 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3163 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003164 return X86EMUL_CONTINUE;
3165}
3166
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003167static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3168{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003169 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003170 return emulate_ud(ctxt);
3171
Avi Kivity9dac77f2011-06-01 15:34:25 +03003172 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003173 return X86EMUL_CONTINUE;
3174}
3175
3176static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3177{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003178 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003179
Avi Kivity9dac77f2011-06-01 15:34:25 +03003180 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003181 return emulate_ud(ctxt);
3182
Avi Kivity9dac77f2011-06-01 15:34:25 +03003183 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003184 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3185
3186 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003187 ctxt->dst.type = OP_NONE;
3188 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003189}
3190
Avi Kivitya14e5792012-06-13 12:28:33 +03003191static int em_lldt(struct x86_emulate_ctxt *ctxt)
3192{
3193 u16 sel = ctxt->src.val;
3194
3195 /* Disable writeback. */
3196 ctxt->dst.type = OP_NONE;
3197 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3198}
3199
Avi Kivity80890002012-06-13 16:33:29 +03003200static int em_ltr(struct x86_emulate_ctxt *ctxt)
3201{
3202 u16 sel = ctxt->src.val;
3203
3204 /* Disable writeback. */
3205 ctxt->dst.type = OP_NONE;
3206 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3207}
3208
Avi Kivity38503912011-03-31 18:48:09 +02003209static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3210{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003211 int rc;
3212 ulong linear;
3213
Avi Kivity9dac77f2011-06-01 15:34:25 +03003214 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003215 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003216 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003217 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003218 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003219 return X86EMUL_CONTINUE;
3220}
3221
Avi Kivity2d04a052011-04-20 15:32:49 +03003222static int em_clts(struct x86_emulate_ctxt *ctxt)
3223{
3224 ulong cr0;
3225
3226 cr0 = ctxt->ops->get_cr(ctxt, 0);
3227 cr0 &= ~X86_CR0_TS;
3228 ctxt->ops->set_cr(ctxt, 0, cr0);
3229 return X86EMUL_CONTINUE;
3230}
3231
Avi Kivity26d05cc2011-04-21 12:07:59 +03003232static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3233{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003234 int rc;
3235
Avi Kivity9dac77f2011-06-01 15:34:25 +03003236 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003237 return X86EMUL_UNHANDLEABLE;
3238
3239 rc = ctxt->ops->fix_hypercall(ctxt);
3240 if (rc != X86EMUL_CONTINUE)
3241 return rc;
3242
3243 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003244 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003245 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003246 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003247 return X86EMUL_CONTINUE;
3248}
3249
Avi Kivity96051572012-06-10 17:21:18 +03003250static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3251 void (*get)(struct x86_emulate_ctxt *ctxt,
3252 struct desc_ptr *ptr))
3253{
3254 struct desc_ptr desc_ptr;
3255
3256 if (ctxt->mode == X86EMUL_MODE_PROT64)
3257 ctxt->op_bytes = 8;
3258 get(ctxt, &desc_ptr);
3259 if (ctxt->op_bytes == 2) {
3260 ctxt->op_bytes = 4;
3261 desc_ptr.address &= 0x00ffffff;
3262 }
3263 /* Disable writeback. */
3264 ctxt->dst.type = OP_NONE;
3265 return segmented_write(ctxt, ctxt->dst.addr.mem,
3266 &desc_ptr, 2 + ctxt->op_bytes);
3267}
3268
3269static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3270{
3271 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3272}
3273
3274static int em_sidt(struct x86_emulate_ctxt *ctxt)
3275{
3276 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3277}
3278
Avi Kivity26d05cc2011-04-21 12:07:59 +03003279static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3280{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003281 struct desc_ptr desc_ptr;
3282 int rc;
3283
Avi Kivity510425f2012-06-07 17:04:36 +03003284 if (ctxt->mode == X86EMUL_MODE_PROT64)
3285 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003286 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003287 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003288 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003289 if (rc != X86EMUL_CONTINUE)
3290 return rc;
3291 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3292 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003293 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003294 return X86EMUL_CONTINUE;
3295}
3296
Avi Kivity5ef39c72011-04-21 12:21:50 +03003297static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003298{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003299 int rc;
3300
Avi Kivity5ef39c72011-04-21 12:21:50 +03003301 rc = ctxt->ops->fix_hypercall(ctxt);
3302
Avi Kivity26d05cc2011-04-21 12:07:59 +03003303 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003304 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003305 return rc;
3306}
3307
3308static int em_lidt(struct x86_emulate_ctxt *ctxt)
3309{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003310 struct desc_ptr desc_ptr;
3311 int rc;
3312
Avi Kivity510425f2012-06-07 17:04:36 +03003313 if (ctxt->mode == X86EMUL_MODE_PROT64)
3314 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003315 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003316 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003317 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003318 if (rc != X86EMUL_CONTINUE)
3319 return rc;
3320 ctxt->ops->set_idt(ctxt, &desc_ptr);
3321 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003322 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003323 return X86EMUL_CONTINUE;
3324}
3325
3326static int em_smsw(struct x86_emulate_ctxt *ctxt)
3327{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003328 ctxt->dst.bytes = 2;
3329 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003330 return X86EMUL_CONTINUE;
3331}
3332
3333static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3334{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003335 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003336 | (ctxt->src.val & 0x0f));
3337 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003338 return X86EMUL_CONTINUE;
3339}
3340
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003341static int em_loop(struct x86_emulate_ctxt *ctxt)
3342{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003343 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3344 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003345 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3346 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003347
3348 return X86EMUL_CONTINUE;
3349}
3350
3351static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3352{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003353 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003354 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003355
3356 return X86EMUL_CONTINUE;
3357}
3358
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003359static int em_in(struct x86_emulate_ctxt *ctxt)
3360{
3361 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3362 &ctxt->dst.val))
3363 return X86EMUL_IO_NEEDED;
3364
3365 return X86EMUL_CONTINUE;
3366}
3367
3368static int em_out(struct x86_emulate_ctxt *ctxt)
3369{
3370 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3371 &ctxt->src.val, 1);
3372 /* Disable writeback. */
3373 ctxt->dst.type = OP_NONE;
3374 return X86EMUL_CONTINUE;
3375}
3376
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003377static int em_cli(struct x86_emulate_ctxt *ctxt)
3378{
3379 if (emulator_bad_iopl(ctxt))
3380 return emulate_gp(ctxt, 0);
3381
3382 ctxt->eflags &= ~X86_EFLAGS_IF;
3383 return X86EMUL_CONTINUE;
3384}
3385
3386static int em_sti(struct x86_emulate_ctxt *ctxt)
3387{
3388 if (emulator_bad_iopl(ctxt))
3389 return emulate_gp(ctxt, 0);
3390
3391 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3392 ctxt->eflags |= X86_EFLAGS_IF;
3393 return X86EMUL_CONTINUE;
3394}
3395
Avi Kivity6d6eede2012-06-07 14:11:36 +03003396static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3397{
3398 u32 eax, ebx, ecx, edx;
3399
Avi Kivitydd856ef2012-08-27 23:46:17 +03003400 eax = reg_read(ctxt, VCPU_REGS_RAX);
3401 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003402 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003403 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3404 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3405 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3406 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003407 return X86EMUL_CONTINUE;
3408}
3409
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003410static int em_lahf(struct x86_emulate_ctxt *ctxt)
3411{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003412 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3413 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003414 return X86EMUL_CONTINUE;
3415}
3416
Avi Kivity92998362012-06-13 12:25:06 +03003417static int em_bswap(struct x86_emulate_ctxt *ctxt)
3418{
3419 switch (ctxt->op_bytes) {
3420#ifdef CONFIG_X86_64
3421 case 8:
3422 asm("bswap %0" : "+r"(ctxt->dst.val));
3423 break;
3424#endif
3425 default:
3426 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3427 break;
3428 }
3429 return X86EMUL_CONTINUE;
3430}
3431
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003432static bool valid_cr(int nr)
3433{
3434 switch (nr) {
3435 case 0:
3436 case 2 ... 4:
3437 case 8:
3438 return true;
3439 default:
3440 return false;
3441 }
3442}
3443
3444static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3445{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003446 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003447 return emulate_ud(ctxt);
3448
3449 return X86EMUL_CONTINUE;
3450}
3451
3452static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3453{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003454 u64 new_val = ctxt->src.val64;
3455 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003456 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003457
3458 static u64 cr_reserved_bits[] = {
3459 0xffffffff00000000ULL,
3460 0, 0, 0, /* CR3 checked later */
3461 CR4_RESERVED_BITS,
3462 0, 0, 0,
3463 CR8_RESERVED_BITS,
3464 };
3465
3466 if (!valid_cr(cr))
3467 return emulate_ud(ctxt);
3468
3469 if (new_val & cr_reserved_bits[cr])
3470 return emulate_gp(ctxt, 0);
3471
3472 switch (cr) {
3473 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003474 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003475 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3476 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3477 return emulate_gp(ctxt, 0);
3478
Avi Kivity717746e2011-04-20 13:37:53 +03003479 cr4 = ctxt->ops->get_cr(ctxt, 4);
3480 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003481
3482 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3483 !(cr4 & X86_CR4_PAE))
3484 return emulate_gp(ctxt, 0);
3485
3486 break;
3487 }
3488 case 3: {
3489 u64 rsvd = 0;
3490
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003491 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3492 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003493 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003494 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003495 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003496 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003497 rsvd = CR3_NONPAE_RESERVED_BITS;
3498
3499 if (new_val & rsvd)
3500 return emulate_gp(ctxt, 0);
3501
3502 break;
3503 }
3504 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003505 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003506
3507 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3508 return emulate_gp(ctxt, 0);
3509
3510 break;
3511 }
3512 }
3513
3514 return X86EMUL_CONTINUE;
3515}
3516
Joerg Roedel3b88e412011-04-04 12:39:29 +02003517static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3518{
3519 unsigned long dr7;
3520
Avi Kivity717746e2011-04-20 13:37:53 +03003521 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003522
3523 /* Check if DR7.Global_Enable is set */
3524 return dr7 & (1 << 13);
3525}
3526
3527static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3528{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003529 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003530 u64 cr4;
3531
3532 if (dr > 7)
3533 return emulate_ud(ctxt);
3534
Avi Kivity717746e2011-04-20 13:37:53 +03003535 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003536 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3537 return emulate_ud(ctxt);
3538
3539 if (check_dr7_gd(ctxt))
3540 return emulate_db(ctxt);
3541
3542 return X86EMUL_CONTINUE;
3543}
3544
3545static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3546{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003547 u64 new_val = ctxt->src.val64;
3548 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003549
3550 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3551 return emulate_gp(ctxt, 0);
3552
3553 return check_dr_read(ctxt);
3554}
3555
Joerg Roedel01de8b02011-04-04 12:39:31 +02003556static int check_svme(struct x86_emulate_ctxt *ctxt)
3557{
3558 u64 efer;
3559
Avi Kivity717746e2011-04-20 13:37:53 +03003560 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003561
3562 if (!(efer & EFER_SVME))
3563 return emulate_ud(ctxt);
3564
3565 return X86EMUL_CONTINUE;
3566}
3567
3568static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3569{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003570 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003571
3572 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003573 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003574 return emulate_gp(ctxt, 0);
3575
3576 return check_svme(ctxt);
3577}
3578
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003579static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3580{
Avi Kivity717746e2011-04-20 13:37:53 +03003581 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003582
Avi Kivity717746e2011-04-20 13:37:53 +03003583 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003584 return emulate_ud(ctxt);
3585
3586 return X86EMUL_CONTINUE;
3587}
3588
Joerg Roedel80612522011-04-04 12:39:33 +02003589static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3590{
Avi Kivity717746e2011-04-20 13:37:53 +03003591 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003592 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003593
Avi Kivity717746e2011-04-20 13:37:53 +03003594 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003595 (rcx > 3))
3596 return emulate_gp(ctxt, 0);
3597
3598 return X86EMUL_CONTINUE;
3599}
3600
Joerg Roedelf6511932011-04-04 12:39:35 +02003601static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3602{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003603 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3604 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003605 return emulate_gp(ctxt, 0);
3606
3607 return X86EMUL_CONTINUE;
3608}
3609
3610static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3611{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003612 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3613 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003614 return emulate_gp(ctxt, 0);
3615
3616 return X86EMUL_CONTINUE;
3617}
3618
Avi Kivity73fba5f2010-07-29 15:11:53 +03003619#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003620#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003621#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3622 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003623#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003624#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003625#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3626#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003627#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003628#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003629#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003630#define II(_f, _e, _i) \
3631 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003632#define IIP(_f, _e, _i, _p) \
3633 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3634 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003635#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003636
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003637#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003638#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003639#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003640#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003641#define I2bvIP(_f, _e, _i, _p) \
3642 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003643
Avi Kivityfb864fb2013-01-04 16:18:54 +02003644#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3645 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3646 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003647
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003648static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003649 DI(SrcNone | Priv, monitor),
3650 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003651 N, N, N, N, N, N,
3652};
3653
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003654static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003655 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3656 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3657 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3658 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3659 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3660 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3661 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3662 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003663};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003664
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003665static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003666 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003667 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003668 N, N, N, N, N, N,
3669};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003670
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003671static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003672 F(Lock, em_add),
3673 F(Lock | PageTable, em_or),
3674 F(Lock, em_adc),
3675 F(Lock, em_sbb),
3676 F(Lock | PageTable, em_and),
3677 F(Lock, em_sub),
3678 F(Lock, em_xor),
3679 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003680};
3681
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003682static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003683 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003684};
3685
Avi Kivity007a3b52013-01-19 19:51:51 +02003686static const struct opcode group2[] = {
3687 F(DstMem | ModRM, em_rol),
3688 F(DstMem | ModRM, em_ror),
3689 F(DstMem | ModRM, em_rcl),
3690 F(DstMem | ModRM, em_rcr),
3691 F(DstMem | ModRM, em_shl),
3692 F(DstMem | ModRM, em_shr),
3693 F(DstMem | ModRM, em_shl),
3694 F(DstMem | ModRM, em_sar),
3695};
3696
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003697static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003698 F(DstMem | SrcImm | NoWrite, em_test),
3699 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003700 F(DstMem | SrcNone | Lock, em_not),
3701 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003702 I(SrcMem, em_mul_ex),
3703 I(SrcMem, em_imul_ex),
3704 I(SrcMem, em_div_ex),
3705 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003706};
3707
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003708static const struct opcode group4[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003709 F(ByteOp | DstMem | SrcNone | Lock, em_inc),
3710 F(ByteOp | DstMem | SrcNone | Lock, em_dec),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003711 N, N, N, N, N, N,
3712};
3713
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003714static const struct opcode group5[] = {
Avi Kivity95413dc2013-01-19 19:51:53 +02003715 F(DstMem | SrcNone | Lock, em_inc),
3716 F(DstMem | SrcNone | Lock, em_dec),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003717 I(SrcMem | Stack, em_grp45),
3718 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3719 I(SrcMem | Stack, em_grp45),
3720 I(SrcMemFAddr | ImplicitOps, em_grp45),
3721 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003722};
3723
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003724static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003725 DI(Prot, sldt),
3726 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003727 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003728 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003729 N, N, N, N,
3730};
3731
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003732static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003733 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3734 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003735 II(SrcMem | Priv, em_lgdt, lgdt),
3736 II(SrcMem | Priv, em_lidt, lidt),
3737 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3738 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3739 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003740}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003741 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003742 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003743 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003744 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3745 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3746 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003747} };
3748
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003749static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003750 N, N, N, N,
Avi Kivity11c363b2013-01-19 19:51:54 +02003751 F(DstMem | SrcImmByte | NoWrite, em_bt),
3752 F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3753 F(DstMem | SrcImmByte | Lock, em_btr),
3754 F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003755};
3756
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003757static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003758 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003759}, {
3760 N, N, N, N, N, N, N, N,
3761} };
3762
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003763static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003764 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003765 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003766};
3767
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003768static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003769 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003770};
3771
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003772static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003773 I(0, em_mov), N, N, N,
3774};
3775
Gleb Natapov045a2822012-12-20 16:57:43 +02003776static const struct escape escape_d9 = { {
3777 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3778}, {
3779 /* 0xC0 - 0xC7 */
3780 N, N, N, N, N, N, N, N,
3781 /* 0xC8 - 0xCF */
3782 N, N, N, N, N, N, N, N,
3783 /* 0xD0 - 0xC7 */
3784 N, N, N, N, N, N, N, N,
3785 /* 0xD8 - 0xDF */
3786 N, N, N, N, N, N, N, N,
3787 /* 0xE0 - 0xE7 */
3788 N, N, N, N, N, N, N, N,
3789 /* 0xE8 - 0xEF */
3790 N, N, N, N, N, N, N, N,
3791 /* 0xF0 - 0xF7 */
3792 N, N, N, N, N, N, N, N,
3793 /* 0xF8 - 0xFF */
3794 N, N, N, N, N, N, N, N,
3795} };
3796
3797static const struct escape escape_db = { {
3798 N, N, N, N, N, N, N, N,
3799}, {
3800 /* 0xC0 - 0xC7 */
3801 N, N, N, N, N, N, N, N,
3802 /* 0xC8 - 0xCF */
3803 N, N, N, N, N, N, N, N,
3804 /* 0xD0 - 0xC7 */
3805 N, N, N, N, N, N, N, N,
3806 /* 0xD8 - 0xDF */
3807 N, N, N, N, N, N, N, N,
3808 /* 0xE0 - 0xE7 */
3809 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3810 /* 0xE8 - 0xEF */
3811 N, N, N, N, N, N, N, N,
3812 /* 0xF0 - 0xF7 */
3813 N, N, N, N, N, N, N, N,
3814 /* 0xF8 - 0xFF */
3815 N, N, N, N, N, N, N, N,
3816} };
3817
3818static const struct escape escape_dd = { {
3819 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3820}, {
3821 /* 0xC0 - 0xC7 */
3822 N, N, N, N, N, N, N, N,
3823 /* 0xC8 - 0xCF */
3824 N, N, N, N, N, N, N, N,
3825 /* 0xD0 - 0xC7 */
3826 N, N, N, N, N, N, N, N,
3827 /* 0xD8 - 0xDF */
3828 N, N, N, N, N, N, N, N,
3829 /* 0xE0 - 0xE7 */
3830 N, N, N, N, N, N, N, N,
3831 /* 0xE8 - 0xEF */
3832 N, N, N, N, N, N, N, N,
3833 /* 0xF0 - 0xF7 */
3834 N, N, N, N, N, N, N, N,
3835 /* 0xF8 - 0xFF */
3836 N, N, N, N, N, N, N, N,
3837} };
3838
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003839static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003840 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003841 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003842 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3843 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003844 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003845 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003846 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3847 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003848 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003849 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003850 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3851 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003852 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003853 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003854 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3855 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003856 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003857 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003858 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003859 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003860 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003861 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003862 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003863 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003864 /* 0x40 - 0x4F */
Avi Kivity95413dc2013-01-19 19:51:53 +02003865 X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003866 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003867 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003868 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003869 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003870 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003871 I(ImplicitOps | Stack | No64, em_pusha),
3872 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003873 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3874 N, N, N, N,
3875 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003876 I(SrcImm | Mov | Stack, em_push),
3877 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003878 I(SrcImmByte | Mov | Stack, em_push),
3879 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003880 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003881 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003882 /* 0x70 - 0x7F */
3883 X16(D(SrcImmByte)),
3884 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003885 G(ByteOp | DstMem | SrcImm, group1),
3886 G(DstMem | SrcImm, group1),
3887 G(ByteOp | DstMem | SrcImm | No64, group1),
3888 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003889 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003890 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003892 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003893 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003894 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003895 D(ModRM | SrcMem | NoAccess | DstReg),
3896 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3897 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003898 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003899 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003900 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003901 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003902 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003903 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003904 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003905 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003906 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003907 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003908 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003909 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003910 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003911 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003912 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3913 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003914 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003915 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003916 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003917 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003918 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003919 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003920 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003921 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003922 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003923 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3924 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003925 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003926 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003927 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3928 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003929 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003930 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003931 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003932 G(Src2One | ByteOp, group2), G(Src2One, group2),
3933 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Gleb Natapov7f662272012-12-10 11:42:30 +02003934 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003935 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003936 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003937 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003938 X3(I(SrcImmByte, em_loop)),
3939 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003940 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3941 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003942 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003943 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003944 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003945 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3946 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003947 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003948 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003949 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3950 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003951 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003952 D(ImplicitOps), D(ImplicitOps),
3953 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003954 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3955};
3956
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003957static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003958 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003959 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003960 N, I(ImplicitOps | VendorSpecific, em_syscall),
3961 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003962 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003963 N, D(ImplicitOps | ModRM), N, N,
3964 /* 0x10 - 0x1F */
3965 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3966 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003967 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003968 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003969 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
3970 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003971 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03003972 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
3973 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003974 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003975 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02003976 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003977 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02003978 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003979 I(ImplicitOps | VendorSpecific, em_sysenter),
3980 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02003981 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003982 N, N, N, N, N, N, N, N,
3983 /* 0x40 - 0x4F */
3984 X16(D(DstReg | SrcMem | ModRM | Mov)),
3985 /* 0x50 - 0x5F */
3986 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
3987 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003988 N, N, N, N,
3989 N, N, N, N,
3990 N, N, N, N,
3991 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003992 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02003993 N, N, N, N,
3994 N, N, N, N,
3995 N, N, N, N,
3996 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003997 /* 0x80 - 0x8F */
3998 X16(D(SrcImm)),
3999 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004000 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004001 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004002 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004003 II(ImplicitOps, em_cpuid, cpuid),
4004 F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004005 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4006 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004007 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004008 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004009 DI(ImplicitOps, rsm),
Avi Kivity11c363b2013-01-19 19:51:54 +02004010 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004011 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4012 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004013 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004014 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004015 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004016 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Avi Kivity11c363b2013-01-19 19:51:54 +02004017 F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004018 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4019 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004020 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004021 /* 0xB8 - 0xBF */
4022 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004023 G(BitOp, group8),
Avi Kivity11c363b2013-01-19 19:51:54 +02004024 F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
4025 F(DstReg | SrcMem | ModRM, em_bsf), F(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004026 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004027 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004028 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004029 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004030 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004031 /* 0xC8 - 0xCF */
4032 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004033 /* 0xD0 - 0xDF */
4034 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4035 /* 0xE0 - 0xEF */
4036 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4037 /* 0xF0 - 0xFF */
4038 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4039};
4040
4041#undef D
4042#undef N
4043#undef G
4044#undef GD
4045#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004046#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004047#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004048
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004049#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004050#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004051#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004052#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004053#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004054
Avi Kivity9dac77f2011-06-01 15:34:25 +03004055static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004056{
4057 unsigned size;
4058
Avi Kivity9dac77f2011-06-01 15:34:25 +03004059 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004060 if (size == 8)
4061 size = 4;
4062 return size;
4063}
4064
4065static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4066 unsigned size, bool sign_extension)
4067{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004068 int rc = X86EMUL_CONTINUE;
4069
4070 op->type = OP_IMM;
4071 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004072 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004073 /* NB. Immediates are sign-extended as necessary. */
4074 switch (op->bytes) {
4075 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004076 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004077 break;
4078 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004079 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004080 break;
4081 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004082 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004083 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004084 case 8:
4085 op->val = insn_fetch(s64, ctxt);
4086 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004087 }
4088 if (!sign_extension) {
4089 switch (op->bytes) {
4090 case 1:
4091 op->val &= 0xff;
4092 break;
4093 case 2:
4094 op->val &= 0xffff;
4095 break;
4096 case 4:
4097 op->val &= 0xffffffff;
4098 break;
4099 }
4100 }
4101done:
4102 return rc;
4103}
4104
Avi Kivitya99455492011-09-13 10:45:41 +03004105static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4106 unsigned d)
4107{
4108 int rc = X86EMUL_CONTINUE;
4109
4110 switch (d) {
4111 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004112 decode_register_operand(ctxt, op);
Avi Kivitya99455492011-09-13 10:45:41 +03004113 break;
4114 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004115 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya99455492011-09-13 10:45:41 +03004116 break;
4117 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004118 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004119 mem_common:
Avi Kivitya99455492011-09-13 10:45:41 +03004120 *op = ctxt->memop;
4121 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004122 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya99455492011-09-13 10:45:41 +03004123 fetch_bit_operand(ctxt);
4124 op->orig_val = op->val;
4125 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004126 case OpMem64:
4127 ctxt->memop.bytes = 8;
4128 goto mem_common;
Avi Kivitya99455492011-09-13 10:45:41 +03004129 case OpAcc:
4130 op->type = OP_REG;
4131 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004132 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya99455492011-09-13 10:45:41 +03004133 fetch_register_operand(op);
4134 op->orig_val = op->val;
4135 break;
4136 case OpDI:
4137 op->type = OP_MEM;
4138 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4139 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004140 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya99455492011-09-13 10:45:41 +03004141 op->addr.mem.seg = VCPU_SREG_ES;
4142 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004143 op->count = 1;
Avi Kivitya99455492011-09-13 10:45:41 +03004144 break;
4145 case OpDX:
4146 op->type = OP_REG;
4147 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004148 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya99455492011-09-13 10:45:41 +03004149 fetch_register_operand(op);
4150 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004151 case OpCL:
4152 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004153 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004154 break;
4155 case OpImmByte:
4156 rc = decode_imm(ctxt, op, 1, true);
4157 break;
4158 case OpOne:
4159 op->bytes = 1;
4160 op->val = 1;
4161 break;
4162 case OpImm:
4163 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4164 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004165 case OpImm64:
4166 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4167 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004168 case OpMem8:
4169 ctxt->memop.bytes = 1;
4170 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004171 case OpMem16:
4172 ctxt->memop.bytes = 2;
4173 goto mem_common;
4174 case OpMem32:
4175 ctxt->memop.bytes = 4;
4176 goto mem_common;
4177 case OpImmU16:
4178 rc = decode_imm(ctxt, op, 2, false);
4179 break;
4180 case OpImmU:
4181 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4182 break;
4183 case OpSI:
4184 op->type = OP_MEM;
4185 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4186 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004187 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004188 op->addr.mem.seg = seg_override(ctxt);
4189 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004190 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004191 break;
4192 case OpImmFAddr:
4193 op->type = OP_IMM;
4194 op->addr.mem.ea = ctxt->_eip;
4195 op->bytes = ctxt->op_bytes + 2;
4196 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4197 break;
4198 case OpMemFAddr:
4199 ctxt->memop.bytes = ctxt->op_bytes + 2;
4200 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004201 case OpES:
4202 op->val = VCPU_SREG_ES;
4203 break;
4204 case OpCS:
4205 op->val = VCPU_SREG_CS;
4206 break;
4207 case OpSS:
4208 op->val = VCPU_SREG_SS;
4209 break;
4210 case OpDS:
4211 op->val = VCPU_SREG_DS;
4212 break;
4213 case OpFS:
4214 op->val = VCPU_SREG_FS;
4215 break;
4216 case OpGS:
4217 op->val = VCPU_SREG_GS;
4218 break;
Avi Kivitya99455492011-09-13 10:45:41 +03004219 case OpImplicit:
4220 /* Special instructions do their own operand decoding. */
4221 default:
4222 op->type = OP_NONE; /* Disable writeback. */
4223 break;
4224 }
4225
4226done:
4227 return rc;
4228}
4229
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004230int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004231{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004232 int rc = X86EMUL_CONTINUE;
4233 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004234 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004235 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004236 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004237
Avi Kivityf09ed832011-09-13 10:45:40 +03004238 ctxt->memop.type = OP_NONE;
4239 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004240 ctxt->_eip = ctxt->eip;
4241 ctxt->fetch.start = ctxt->_eip;
4242 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004243 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004244 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004245
4246 switch (mode) {
4247 case X86EMUL_MODE_REAL:
4248 case X86EMUL_MODE_VM86:
4249 case X86EMUL_MODE_PROT16:
4250 def_op_bytes = def_ad_bytes = 2;
4251 break;
4252 case X86EMUL_MODE_PROT32:
4253 def_op_bytes = def_ad_bytes = 4;
4254 break;
4255#ifdef CONFIG_X86_64
4256 case X86EMUL_MODE_PROT64:
4257 def_op_bytes = 4;
4258 def_ad_bytes = 8;
4259 break;
4260#endif
4261 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004262 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004263 }
4264
Avi Kivity9dac77f2011-06-01 15:34:25 +03004265 ctxt->op_bytes = def_op_bytes;
4266 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267
4268 /* Legacy prefixes. */
4269 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004270 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004271 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004272 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004273 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004274 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275 break;
4276 case 0x67: /* address-size override */
4277 if (mode == X86EMUL_MODE_PROT64)
4278 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004279 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004280 else
4281 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004282 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004283 break;
4284 case 0x26: /* ES override */
4285 case 0x2e: /* CS override */
4286 case 0x36: /* SS override */
4287 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004288 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004289 break;
4290 case 0x64: /* FS override */
4291 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004292 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004293 break;
4294 case 0x40 ... 0x4f: /* REX */
4295 if (mode != X86EMUL_MODE_PROT64)
4296 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004297 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004298 continue;
4299 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004300 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004301 break;
4302 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004303 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004304 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004305 break;
4306 default:
4307 goto done_prefixes;
4308 }
4309
4310 /* Any legacy prefix after a REX prefix nullifies its effect. */
4311
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004313 }
4314
4315done_prefixes:
4316
4317 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004318 if (ctxt->rex_prefix & 8)
4319 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004320
4321 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004323 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004324 if (ctxt->b == 0x0f) {
4325 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004326 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004327 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004328 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004329 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004330
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004331 if (ctxt->d & ModRM)
4332 ctxt->modrm = insn_fetch(u8, ctxt);
4333
Avi Kivity9dac77f2011-06-01 15:34:25 +03004334 while (ctxt->d & GroupMask) {
4335 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004336 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004337 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004338 opcode = opcode.u.group[goffset];
4339 break;
4340 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004341 goffset = (ctxt->modrm >> 3) & 7;
4342 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004343 opcode = opcode.u.gdual->mod3[goffset];
4344 else
4345 opcode = opcode.u.gdual->mod012[goffset];
4346 break;
4347 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004349 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004350 break;
4351 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004353 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004354 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004355 switch (simd_prefix) {
4356 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4357 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4358 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4359 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4360 }
4361 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004362 case Escape:
4363 if (ctxt->modrm > 0xbf)
4364 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4365 else
4366 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4367 break;
Avi Kivity46561642011-04-24 14:09:59 +03004368 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004369 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004370 }
Avi Kivity46561642011-04-24 14:09:59 +03004371
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004372 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004373 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004374 }
4375
Avi Kivity9dac77f2011-06-01 15:34:25 +03004376 ctxt->execute = opcode.u.execute;
4377 ctxt->check_perm = opcode.check_perm;
4378 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004379
4380 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004381 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004382 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004383
Avi Kivity9dac77f2011-06-01 15:34:25 +03004384 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004385 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004386
Avi Kivity9dac77f2011-06-01 15:34:25 +03004387 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4388 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004389
Avi Kivity9dac77f2011-06-01 15:34:25 +03004390 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004391 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004392 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004393 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004394 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004395 }
4396
Avi Kivity9dac77f2011-06-01 15:34:25 +03004397 if (ctxt->d & Sse)
4398 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004399 else if (ctxt->d & Mmx)
4400 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004401
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004402 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004403 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004404 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004405 if (!ctxt->has_seg_override)
4406 set_seg_override(ctxt, ctxt->modrm_seg);
4407 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004408 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004409 if (rc != X86EMUL_CONTINUE)
4410 goto done;
4411
Avi Kivity9dac77f2011-06-01 15:34:25 +03004412 if (!ctxt->has_seg_override)
4413 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004414
Avi Kivityf09ed832011-09-13 10:45:40 +03004415 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004416
Avi Kivityf09ed832011-09-13 10:45:40 +03004417 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4418 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004419
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004420 /*
4421 * Decode and fetch the source operand: register, memory
4422 * or immediate.
4423 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004424 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004425 if (rc != X86EMUL_CONTINUE)
4426 goto done;
4427
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004428 /*
4429 * Decode and fetch the second source operand: register, memory
4430 * or immediate.
4431 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004432 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004433 if (rc != X86EMUL_CONTINUE)
4434 goto done;
4435
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004436 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya99455492011-09-13 10:45:41 +03004437 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004438
4439done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004440 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4441 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004442
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004443 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004444}
4445
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004446bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4447{
4448 return ctxt->d & PageTable;
4449}
4450
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004451static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4452{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004453 /* The second termination condition only applies for REPE
4454 * and REPNE. Test if the repeat string operation prefix is
4455 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4456 * corresponding termination condition according to:
4457 * - if REPE/REPZ and ZF = 0 then done
4458 * - if REPNE/REPNZ and ZF = 1 then done
4459 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004460 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4461 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4462 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004463 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004464 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004465 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4466 return true;
4467
4468 return false;
4469}
4470
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004471static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4472{
4473 bool fault = false;
4474
4475 ctxt->ops->get_fpu(ctxt);
4476 asm volatile("1: fwait \n\t"
4477 "2: \n\t"
4478 ".pushsection .fixup,\"ax\" \n\t"
4479 "3: \n\t"
4480 "movb $1, %[fault] \n\t"
4481 "jmp 2b \n\t"
4482 ".popsection \n\t"
4483 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004484 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004485 ctxt->ops->put_fpu(ctxt);
4486
4487 if (unlikely(fault))
4488 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4489
4490 return X86EMUL_CONTINUE;
4491}
4492
4493static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4494 struct operand *op)
4495{
4496 if (op->type == OP_MM)
4497 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4498}
4499
Avi Kivitye28bbd42013-01-04 16:18:48 +02004500static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4501{
4502 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4503 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4504 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4505 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4506 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4507 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4508 return X86EMUL_CONTINUE;
4509}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004510
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004511int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004512{
Mathias Krause0225fb52012-08-30 01:30:16 +02004513 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004514 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004515 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004516
Avi Kivity9dac77f2011-06-01 15:34:25 +03004517 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004518
Avi Kivity9dac77f2011-06-01 15:34:25 +03004519 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004520 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004521 goto done;
4522 }
4523
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004524 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004525 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004526 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004527 goto done;
4528 }
4529
Avi Kivity9dac77f2011-06-01 15:34:25 +03004530 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004531 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004532 goto done;
4533 }
4534
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004535 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4536 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004537 rc = emulate_ud(ctxt);
4538 goto done;
4539 }
4540
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004541 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004542 rc = emulate_nm(ctxt);
4543 goto done;
4544 }
4545
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004546 if (ctxt->d & Mmx) {
4547 rc = flush_pending_x87_faults(ctxt);
4548 if (rc != X86EMUL_CONTINUE)
4549 goto done;
4550 /*
4551 * Now that we know the fpu is exception safe, we can fetch
4552 * operands from it.
4553 */
4554 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4555 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4556 if (!(ctxt->d & Mov))
4557 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4558 }
4559
Avi Kivity9dac77f2011-06-01 15:34:25 +03004560 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4561 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004562 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004563 if (rc != X86EMUL_CONTINUE)
4564 goto done;
4565 }
4566
Gleb Natapove92805a2010-02-10 14:21:35 +02004567 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004568 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004569 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004570 goto done;
4571 }
4572
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004573 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004574 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004575 rc = emulate_ud(ctxt);
4576 goto done;
4577 }
4578
Joerg Roedeld09beab2011-04-04 12:39:25 +02004579 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004580 if (ctxt->check_perm) {
4581 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004582 if (rc != X86EMUL_CONTINUE)
4583 goto done;
4584 }
4585
Avi Kivity9dac77f2011-06-01 15:34:25 +03004586 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4587 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004588 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004589 if (rc != X86EMUL_CONTINUE)
4590 goto done;
4591 }
4592
Avi Kivity9dac77f2011-06-01 15:34:25 +03004593 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004594 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004595 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004596 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004597 goto done;
4598 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004599 }
4600
Avi Kivity9dac77f2011-06-01 15:34:25 +03004601 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4602 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4603 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004604 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004605 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004606 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004607 }
4608
Avi Kivity9dac77f2011-06-01 15:34:25 +03004609 if (ctxt->src2.type == OP_MEM) {
4610 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4611 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004612 if (rc != X86EMUL_CONTINUE)
4613 goto done;
4614 }
4615
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004617 goto special_insn;
4618
4619
Avi Kivity9dac77f2011-06-01 15:34:25 +03004620 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004621 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004622 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4623 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004624 if (rc != X86EMUL_CONTINUE)
4625 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004626 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004627 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004628
Avi Kivity018a98d2007-11-27 19:30:56 +02004629special_insn:
4630
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4632 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004633 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004634 if (rc != X86EMUL_CONTINUE)
4635 goto done;
4636 }
4637
Avi Kivity9dac77f2011-06-01 15:34:25 +03004638 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004639 if (ctxt->d & Fastop) {
4640 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4641 rc = fastop(ctxt, fop);
4642 if (rc != X86EMUL_CONTINUE)
4643 goto done;
4644 goto writeback;
4645 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004646 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004647 if (rc != X86EMUL_CONTINUE)
4648 goto done;
4649 goto writeback;
4650 }
4651
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004653 goto twobyte_insn;
4654
Avi Kivity9dac77f2011-06-01 15:34:25 +03004655 switch (ctxt->b) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004657 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004658 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004659 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004661 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004662 if (test_cc(ctxt->b, ctxt->eflags))
4663 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004664 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004665 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004666 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004667 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004668 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004669 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004670 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004671 rc = em_xchg(ctxt);
4672 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004673 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004674 switch (ctxt->op_bytes) {
4675 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4676 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4677 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004678 }
4679 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004680 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004681 rc = emulate_int(ctxt, 3);
4682 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004683 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004684 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004685 break;
4686 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004687 if (ctxt->eflags & EFLG_OF)
4688 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004689 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004690 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004691 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004692 jmp_rel(ctxt, ctxt->src.val);
4693 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004694 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004695 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004696 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004697 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004698 case 0xf5: /* cmc */
4699 /* complement carry flag from eflags reg */
4700 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004701 break;
4702 case 0xf8: /* clc */
4703 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004704 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004705 case 0xf9: /* stc */
4706 ctxt->eflags |= EFLG_CF;
4707 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004708 case 0xfc: /* cld */
4709 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004710 break;
4711 case 0xfd: /* std */
4712 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004713 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004714 default:
4715 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004717
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004718 if (rc != X86EMUL_CONTINUE)
4719 goto done;
4720
Avi Kivity018a98d2007-11-27 19:30:56 +02004721writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004722 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004723 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004724 goto done;
4725
Gleb Natapov5cd21912010-03-18 15:20:26 +02004726 /*
4727 * restore dst type in case the decoding will be reused
4728 * (happens for string instruction )
4729 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004730 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004731
Avi Kivity9dac77f2011-06-01 15:34:25 +03004732 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004733 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004734
Avi Kivity9dac77f2011-06-01 15:34:25 +03004735 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004736 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004737
Avi Kivity9dac77f2011-06-01 15:34:25 +03004738 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004739 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004740 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004741 if ((ctxt->d & SrcMask) == SrcSI)
4742 count = ctxt->src.count;
4743 else
4744 count = ctxt->dst.count;
4745 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4746 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004747
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004748 if (!string_insn_completed(ctxt)) {
4749 /*
4750 * Re-enter guest when pio read ahead buffer is empty
4751 * or, if it is not used, after each 1024 iteration.
4752 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004753 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004754 (r->end == 0 || r->end != r->pos)) {
4755 /*
4756 * Reset read cache. Usually happens before
4757 * decode, but since instruction is restarted
4758 * we have to do it here.
4759 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004760 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004761 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004762 return EMULATION_RESTART;
4763 }
4764 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004765 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004766 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004767
Avi Kivity9dac77f2011-06-01 15:34:25 +03004768 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004769
4770done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004771 if (rc == X86EMUL_PROPAGATE_FAULT)
4772 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004773 if (rc == X86EMUL_INTERCEPTED)
4774 return EMULATION_INTERCEPTED;
4775
Avi Kivitydd856ef2012-08-27 23:46:17 +03004776 if (rc == X86EMUL_CONTINUE)
4777 writeback_registers(ctxt);
4778
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004779 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780
4781twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004782 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004783 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004784 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004785 break;
4786 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004787 case 0x0d: /* GrpP (prefetch) */
4788 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004789 break;
4790 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004791 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004792 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004794 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004797 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4798 if (!test_cc(ctxt->b, ctxt->eflags))
4799 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004800 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004801 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004802 if (test_cc(ctxt->b, ctxt->eflags))
4803 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004804 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004805 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004806 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004807 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004808 case 0xae: /* clflush */
4809 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004810 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004811 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004812 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004813 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004814 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004816 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004817 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004818 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004819 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004820 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004821 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004822 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004823 ctxt->src.val = ctxt->dst.orig_val;
4824 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004825 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004826 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004827 ctxt->dst.bytes = ctxt->op_bytes;
4828 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4829 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004830 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004831 default:
4832 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004834
4835 if (rc != X86EMUL_CONTINUE)
4836 goto done;
4837
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 goto writeback;
4839
4840cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004841 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004842}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004843
4844void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4845{
4846 invalidate_registers(ctxt);
4847}
4848
4849void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4850{
4851 writeback_registers(ctxt);
4852}