blob: a94b1d76f799762a9569c6afe59ed958ae2474f3 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
Avi Kivity56e82312009-08-12 15:04:37 +03002 * emulate.c
Avi Kivity6aa8b732006-12-10 02:21:36 -08003 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * Linux coding style, mod r/m decoder, segment base fixes, real-mode
Rusty Russelldcc07662007-07-17 23:16:56 +10009 * privileged instructions:
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Copyright (C) 2006 Qumranet
Nicolas Kaiser9611c182010-10-06 14:23:22 +020012 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 *
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
21 */
22
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Avi Kivity56e82312009-08-12 15:04:37 +030026#include <asm/kvm_emulate.h>
Avi Kivityb7d491e2013-01-04 16:18:49 +020027#include <linux/stringify.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivity3eeb3282010-01-21 15:31:48 +020029#include "x86.h"
Gleb Natapov38ba30b2010-03-18 15:20:17 +020030#include "tss.h"
Andre Przywarae99f0502009-06-17 15:50:33 +020031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032/*
Avi Kivitya9945542011-09-13 10:45:41 +030033 * Operand types
34 */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030035#define OpNone 0ull
36#define OpImplicit 1ull /* No generic decode */
37#define OpReg 2ull /* Register */
38#define OpMem 3ull /* Memory */
39#define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
40#define OpDI 5ull /* ES:DI/EDI/RDI */
41#define OpMem64 6ull /* Memory, 64-bit */
42#define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
43#define OpDX 8ull /* DX register */
Avi Kivity4dd6a572011-09-13 10:45:43 +030044#define OpCL 9ull /* CL register (for shifts) */
45#define OpImmByte 10ull /* 8-bit sign extended immediate */
46#define OpOne 11ull /* Implied 1 */
Nadav Amit5e2c6882012-12-06 21:55:10 -020047#define OpImm 12ull /* Sign extended up to 32-bit immediate */
Avi Kivity0fe59122011-09-13 10:45:47 +030048#define OpMem16 13ull /* Memory operand (16-bit). */
49#define OpMem32 14ull /* Memory operand (32-bit). */
50#define OpImmU 15ull /* Immediate operand, zero extended */
51#define OpSI 16ull /* SI/ESI/RSI */
52#define OpImmFAddr 17ull /* Immediate far address */
53#define OpMemFAddr 18ull /* Far address in memory */
54#define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
Avi Kivityc191a7a2011-09-13 10:45:49 +030055#define OpES 20ull /* ES */
56#define OpCS 21ull /* CS */
57#define OpSS 22ull /* SS */
58#define OpDS 23ull /* DS */
59#define OpFS 24ull /* FS */
60#define OpGS 25ull /* GS */
Avi Kivity28867ce2012-01-16 15:08:44 +020061#define OpMem8 26ull /* 8-bit zero extended memory operand */
Nadav Amit5e2c6882012-12-06 21:55:10 -020062#define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
Avi Kivitya9945542011-09-13 10:45:41 +030063
Avi Kivity0fe59122011-09-13 10:45:47 +030064#define OpBits 5 /* Width of operand field */
Avi Kivityb1ea50b2011-09-13 10:45:42 +030065#define OpMask ((1ull << OpBits) - 1)
Avi Kivitya9945542011-09-13 10:45:41 +030066
67/*
Avi Kivity6aa8b732006-12-10 02:21:36 -080068 * Opcode effective-address decode tables.
69 * Note that we only emulate instructions that have at least one memory
70 * operand (excluding implicit stack references). We assume that stack
71 * references and instruction fetches will never occur in special memory
72 * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
73 * not be handled.
74 */
75
76/* Operand sizes: 8-bit operands or specified/overridden size. */
Avi Kivityab85b12b2010-07-29 15:11:49 +030077#define ByteOp (1<<0) /* 8-bit operands. */
Avi Kivity6aa8b732006-12-10 02:21:36 -080078/* Destination operand type. */
Avi Kivitya9945542011-09-13 10:45:41 +030079#define DstShift 1
80#define ImplicitOps (OpImplicit << DstShift)
81#define DstReg (OpReg << DstShift)
82#define DstMem (OpMem << DstShift)
83#define DstAcc (OpAcc << DstShift)
84#define DstDI (OpDI << DstShift)
85#define DstMem64 (OpMem64 << DstShift)
86#define DstImmUByte (OpImmUByte << DstShift)
87#define DstDX (OpDX << DstShift)
88#define DstMask (OpMask << DstShift)
Avi Kivity6aa8b732006-12-10 02:21:36 -080089/* Source operand type. */
Avi Kivity0fe59122011-09-13 10:45:47 +030090#define SrcShift 6
91#define SrcNone (OpNone << SrcShift)
92#define SrcReg (OpReg << SrcShift)
93#define SrcMem (OpMem << SrcShift)
94#define SrcMem16 (OpMem16 << SrcShift)
95#define SrcMem32 (OpMem32 << SrcShift)
96#define SrcImm (OpImm << SrcShift)
97#define SrcImmByte (OpImmByte << SrcShift)
98#define SrcOne (OpOne << SrcShift)
99#define SrcImmUByte (OpImmUByte << SrcShift)
100#define SrcImmU (OpImmU << SrcShift)
101#define SrcSI (OpSI << SrcShift)
102#define SrcImmFAddr (OpImmFAddr << SrcShift)
103#define SrcMemFAddr (OpMemFAddr << SrcShift)
104#define SrcAcc (OpAcc << SrcShift)
105#define SrcImmU16 (OpImmU16 << SrcShift)
Nadav Amit5e2c6882012-12-06 21:55:10 -0200106#define SrcImm64 (OpImm64 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300107#define SrcDX (OpDX << SrcShift)
Avi Kivity28867ce2012-01-16 15:08:44 +0200108#define SrcMem8 (OpMem8 << SrcShift)
Avi Kivity0fe59122011-09-13 10:45:47 +0300109#define SrcMask (OpMask << SrcShift)
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300110#define BitOp (1<<11)
111#define MemAbs (1<<12) /* Memory operand is absolute displacement */
112#define String (1<<13) /* String instruction (rep capable) */
113#define Stack (1<<14) /* Stack instruction (push/pop) */
114#define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
115#define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
116#define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
117#define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
118#define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
Gleb Natapov045a2822012-12-20 16:57:43 +0200119#define Escape (5<<15) /* Escape to coprocessor instruction */
Marcelo Tosatti221192b2011-05-30 15:23:14 -0300120#define Sse (1<<18) /* SSE Vector instruction */
Avi Kivity20c29ff2011-09-13 10:45:44 +0300121/* Generic ModRM decode. */
122#define ModRM (1<<19)
123/* Destination is only written; never read. */
124#define Mov (1<<20)
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300125/* Misc flags */
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +0200126#define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
Avi Kivityd8671622011-02-01 16:32:03 +0200127#define VendorSpecific (1<<22) /* Vendor specific instruction */
Avi Kivity5a506b12010-08-01 15:10:29 +0300128#define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
Avi Kivity7f9b4b72010-08-01 14:46:54 +0300129#define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
Avi Kivity047a4812010-07-26 14:37:47 +0300130#define Undefined (1<<25) /* No Such Instruction */
Gleb Natapovd380a5e2010-02-10 14:21:36 +0200131#define Lock (1<<26) /* lock prefix is allowed for the instruction */
Gleb Natapove92805a2010-02-10 14:21:35 +0200132#define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
Mohammed Gamald8769fe2009-08-23 14:24:25 +0300133#define No64 (1<<28)
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800134#define PageTable (1 << 29) /* instruction used to write page table */
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100135/* Source 2 operand type */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +0800136#define Src2Shift (30)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300137#define Src2None (OpNone << Src2Shift)
138#define Src2CL (OpCL << Src2Shift)
139#define Src2ImmByte (OpImmByte << Src2Shift)
140#define Src2One (OpOne << Src2Shift)
141#define Src2Imm (OpImm << Src2Shift)
Avi Kivityc191a7a2011-09-13 10:45:49 +0300142#define Src2ES (OpES << Src2Shift)
143#define Src2CS (OpCS << Src2Shift)
144#define Src2SS (OpSS << Src2Shift)
145#define Src2DS (OpDS << Src2Shift)
146#define Src2FS (OpFS << Src2Shift)
147#define Src2GS (OpGS << Src2Shift)
Avi Kivity4dd6a572011-09-13 10:45:43 +0300148#define Src2Mask (OpMask << Src2Shift)
Avi Kivitycbe2c9d2012-04-09 18:40:02 +0300149#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
Avi Kivity1c11b372012-04-09 18:39:59 +0300150#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
151#define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */
152#define Avx ((u64)1 << 43) /* Advanced Vector Extensions */
Avi Kivitye28bbd42013-01-04 16:18:48 +0200153#define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
Avi Kivityb6744dc2013-01-04 16:18:50 +0200154#define NoWrite ((u64)1 << 45) /* No writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
Avi Kivityd0e53322010-07-29 15:11:54 +0300156#define X2(x...) x, x
157#define X3(x...) X2(x), x
158#define X4(x...) X2(x), X2(x)
159#define X5(x...) X4(x), x
160#define X6(x...) X4(x), X2(x)
161#define X7(x...) X4(x), X3(x)
162#define X8(x...) X4(x), X4(x)
163#define X16(x...) X8(x), X8(x)
Avi Kivity83babbc2010-07-26 14:37:39 +0300164
Avi Kivitye28bbd42013-01-04 16:18:48 +0200165#define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
166#define FASTOP_SIZE 8
167
168/*
169 * fastop functions have a special calling convention:
170 *
171 * dst: [rdx]:rax (in/out)
172 * src: rbx (in/out)
173 * src2: rcx (in)
174 * flags: rflags (in/out)
175 *
176 * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
177 * different operand sizes can be reached by calculation, rather than a jump
178 * table (which would be bigger than the code).
179 *
180 * fastop functions are declared as taking a never-defined fastop parameter,
181 * so they can't be called from C directly.
182 */
183
184struct fastop;
185
Avi Kivityd65b1de2010-07-29 15:11:35 +0300186struct opcode {
Avi Kivityb1ea50b2011-09-13 10:45:42 +0300187 u64 flags : 56;
188 u64 intercept : 8;
Avi Kivity120df892010-07-29 15:11:39 +0300189 union {
Avi Kivityef65c882010-07-29 15:11:51 +0300190 int (*execute)(struct x86_emulate_ctxt *ctxt);
Mathias Krausefd0a0d82012-08-30 01:30:15 +0200191 const struct opcode *group;
192 const struct group_dual *gdual;
193 const struct gprefix *gprefix;
Gleb Natapov045a2822012-12-20 16:57:43 +0200194 const struct escape *esc;
Avi Kivitye28bbd42013-01-04 16:18:48 +0200195 void (*fastop)(struct fastop *fake);
Avi Kivity120df892010-07-29 15:11:39 +0300196 } u;
Joerg Roedeld09beab2011-04-04 12:39:25 +0200197 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Avi Kivity120df892010-07-29 15:11:39 +0300198};
199
200struct group_dual {
201 struct opcode mod012[8];
202 struct opcode mod3[8];
Avi Kivityd65b1de2010-07-29 15:11:35 +0300203};
204
Avi Kivity0d7cdee2011-03-29 11:34:38 +0200205struct gprefix {
206 struct opcode pfx_no;
207 struct opcode pfx_66;
208 struct opcode pfx_f2;
209 struct opcode pfx_f3;
210};
211
Gleb Natapov045a2822012-12-20 16:57:43 +0200212struct escape {
213 struct opcode op[8];
214 struct opcode high[64];
215};
216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217/* EFLAGS bit definitions. */
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200218#define EFLG_ID (1<<21)
219#define EFLG_VIP (1<<20)
220#define EFLG_VIF (1<<19)
221#define EFLG_AC (1<<18)
Andre Przywarab1d86142009-06-17 15:50:32 +0200222#define EFLG_VM (1<<17)
223#define EFLG_RF (1<<16)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200224#define EFLG_IOPL (3<<12)
225#define EFLG_NT (1<<14)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226#define EFLG_OF (1<<11)
227#define EFLG_DF (1<<10)
Andre Przywarab1d86142009-06-17 15:50:32 +0200228#define EFLG_IF (1<<9)
Gleb Natapovd4c6a152010-02-10 14:21:34 +0200229#define EFLG_TF (1<<8)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230#define EFLG_SF (1<<7)
231#define EFLG_ZF (1<<6)
232#define EFLG_AF (1<<4)
233#define EFLG_PF (1<<2)
234#define EFLG_CF (1<<0)
235
Mohammed Gamal62bd4302010-07-28 12:38:40 +0300236#define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
237#define EFLG_RESERVED_ONE_MASK 2
238
Avi Kivitydd856ef2012-08-27 23:46:17 +0300239static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
240{
241 if (!(ctxt->regs_valid & (1 << nr))) {
242 ctxt->regs_valid |= 1 << nr;
243 ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
244 }
245 return ctxt->_regs[nr];
246}
247
248static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
249{
250 ctxt->regs_valid |= 1 << nr;
251 ctxt->regs_dirty |= 1 << nr;
252 return &ctxt->_regs[nr];
253}
254
255static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
256{
257 reg_read(ctxt, nr);
258 return reg_write(ctxt, nr);
259}
260
261static void writeback_registers(struct x86_emulate_ctxt *ctxt)
262{
263 unsigned reg;
264
265 for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
266 ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
267}
268
269static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
270{
271 ctxt->regs_dirty = 0;
272 ctxt->regs_valid = 0;
273}
274
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275/*
276 * Instruction emulation:
277 * Most instructions are emulated directly via a fragment of inline assembly
278 * code. This allows us to save/restore EFLAGS and thus very easily pick up
279 * any modified flags.
280 */
281
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800282#if defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283#define _LO32 "k" /* force 32-bit operand */
284#define _STK "%%rsp" /* stack pointer */
285#elif defined(__i386__)
286#define _LO32 "" /* force 32-bit operand */
287#define _STK "%%esp" /* stack pointer */
288#endif
289
290/*
291 * These EFLAGS bits are restored from saved value during emulation, and
292 * any changes are written back to the saved value after emulation.
293 */
294#define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
295
296/* Before executing instruction: restore necessary bits in EFLAGS. */
Avi Kivitye934c9c2007-12-06 16:15:02 +0200297#define _PRE_EFLAGS(_sav, _msk, _tmp) \
298 /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
299 "movl %"_sav",%"_LO32 _tmp"; " \
300 "push %"_tmp"; " \
301 "push %"_tmp"; " \
302 "movl %"_msk",%"_LO32 _tmp"; " \
303 "andl %"_LO32 _tmp",("_STK"); " \
304 "pushf; " \
305 "notl %"_LO32 _tmp"; " \
306 "andl %"_LO32 _tmp",("_STK"); " \
307 "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
308 "pop %"_tmp"; " \
309 "orl %"_LO32 _tmp",("_STK"); " \
310 "popf; " \
311 "pop %"_sav"; "
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312
313/* After executing instruction: write-back necessary bits in EFLAGS. */
314#define _POST_EFLAGS(_sav, _msk, _tmp) \
315 /* _sav |= EFLAGS & _msk; */ \
316 "pushf; " \
317 "pop %"_tmp"; " \
318 "andl %"_msk",%"_LO32 _tmp"; " \
319 "orl %"_LO32 _tmp",%"_sav"; "
320
Avi Kivitydda96d82008-11-26 15:14:10 +0200321#ifdef CONFIG_X86_64
322#define ON64(x) x
323#else
324#define ON64(x)
325#endif
326
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300327#define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200328 do { \
329 __asm__ __volatile__ ( \
330 _PRE_EFLAGS("0", "4", "2") \
331 _op _suffix " %"_x"3,%1; " \
332 _POST_EFLAGS("0", "4", "2") \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300333 : "=m" ((ctxt)->eflags), \
334 "+q" (*(_dsttype*)&(ctxt)->dst.val), \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200335 "=&r" (_tmp) \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300336 : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \
Avi Kivityf3fd92f2008-11-29 20:38:12 +0200337 } while (0)
Avi Kivity6b7ad612008-11-26 15:30:45 +0200338
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340/* Raw emulation: instruction has two explicit operands. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300341#define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200342 do { \
343 unsigned long _tmp; \
344 \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300345 switch ((ctxt)->dst.bytes) { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200346 case 2: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300347 ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200348 break; \
349 case 4: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300350 ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200351 break; \
352 case 8: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300353 ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200354 break; \
355 } \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 } while (0)
357
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300358#define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359 do { \
Avi Kivity6b7ad612008-11-26 15:30:45 +0200360 unsigned long _tmp; \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300361 switch ((ctxt)->dst.bytes) { \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 case 1: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300363 ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364 break; \
365 default: \
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300366 __emulate_2op_nobyte(ctxt, _op, \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 _wx, _wy, _lx, _ly, _qx, _qy); \
368 break; \
369 } \
370 } while (0)
371
372/* Source operand is byte-sized and may be restricted to just %cl. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300373#define emulate_2op_SrcB(ctxt, _op) \
374 __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375
376/* Source operand is byte, word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300377#define emulate_2op_SrcV(ctxt, _op) \
378 __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379
380/* Source operand is word, long or quad sized. */
Avi Kivitya31b9ce2011-09-07 16:41:35 +0300381#define emulate_2op_SrcV_nobyte(ctxt, _op) \
382 __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r")
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100384/* Instruction has three operands and one operand is stored in ECX register */
Avi Kivity29053a62011-09-07 16:41:37 +0300385#define __emulate_2op_cl(ctxt, _op, _suffix, _type) \
Avi Kivity72952612011-04-20 13:12:27 +0300386 do { \
387 unsigned long _tmp; \
Avi Kivity761441b2011-09-07 16:41:36 +0300388 _type _clv = (ctxt)->src2.val; \
389 _type _srcv = (ctxt)->src.val; \
390 _type _dstv = (ctxt)->dst.val; \
Avi Kivity72952612011-04-20 13:12:27 +0300391 \
392 __asm__ __volatile__ ( \
393 _PRE_EFLAGS("0", "5", "2") \
394 _op _suffix " %4,%1 \n" \
395 _POST_EFLAGS("0", "5", "2") \
Avi Kivity761441b2011-09-07 16:41:36 +0300396 : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \
Avi Kivity72952612011-04-20 13:12:27 +0300397 : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
398 ); \
399 \
Avi Kivity761441b2011-09-07 16:41:36 +0300400 (ctxt)->src2.val = (unsigned long) _clv; \
401 (ctxt)->src2.val = (unsigned long) _srcv; \
402 (ctxt)->dst.val = (unsigned long) _dstv; \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100403 } while (0)
404
Avi Kivity761441b2011-09-07 16:41:36 +0300405#define emulate_2op_cl(ctxt, _op) \
Avi Kivity72952612011-04-20 13:12:27 +0300406 do { \
Avi Kivity761441b2011-09-07 16:41:36 +0300407 switch ((ctxt)->dst.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300408 case 2: \
Avi Kivity29053a62011-09-07 16:41:37 +0300409 __emulate_2op_cl(ctxt, _op, "w", u16); \
Avi Kivity72952612011-04-20 13:12:27 +0300410 break; \
411 case 4: \
Avi Kivity29053a62011-09-07 16:41:37 +0300412 __emulate_2op_cl(ctxt, _op, "l", u32); \
Avi Kivity72952612011-04-20 13:12:27 +0300413 break; \
414 case 8: \
Avi Kivity29053a62011-09-07 16:41:37 +0300415 ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \
Avi Kivity72952612011-04-20 13:12:27 +0300416 break; \
417 } \
Guillaume Thouvenind1752262008-12-04 14:29:00 +0100418 } while (0)
419
Avi Kivityd1eef452011-09-07 16:41:38 +0300420#define __emulate_1op(ctxt, _op, _suffix) \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421 do { \
422 unsigned long _tmp; \
423 \
Avi Kivitydda96d82008-11-26 15:14:10 +0200424 __asm__ __volatile__ ( \
425 _PRE_EFLAGS("0", "3", "2") \
426 _op _suffix " %1; " \
427 _POST_EFLAGS("0", "3", "2") \
Avi Kivityd1eef452011-09-07 16:41:38 +0300428 : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \
Avi Kivitydda96d82008-11-26 15:14:10 +0200429 "=&r" (_tmp) \
430 : "i" (EFLAGS_MASK)); \
431 } while (0)
432
433/* Instruction has only one explicit operand (no source operand). */
Avi Kivityd1eef452011-09-07 16:41:38 +0300434#define emulate_1op(ctxt, _op) \
Avi Kivitydda96d82008-11-26 15:14:10 +0200435 do { \
Avi Kivityd1eef452011-09-07 16:41:38 +0300436 switch ((ctxt)->dst.bytes) { \
437 case 1: __emulate_1op(ctxt, _op, "b"); break; \
438 case 2: __emulate_1op(ctxt, _op, "w"); break; \
439 case 4: __emulate_1op(ctxt, _op, "l"); break; \
440 case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 } \
442 } while (0)
443
Avi Kivityb7d491e2013-01-04 16:18:49 +0200444#define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t"
445#define FOP_RET "ret \n\t"
446
447#define FOP_START(op) \
448 extern void em_##op(struct fastop *fake); \
449 asm(".pushsection .text, \"ax\" \n\t" \
450 ".global em_" #op " \n\t" \
451 FOP_ALIGN \
452 "em_" #op ": \n\t"
453
454#define FOP_END \
455 ".popsection")
456
Avi Kivity0bdea062013-01-19 19:51:50 +0200457#define FOPNOP() FOP_ALIGN FOP_RET
458
Avi Kivityb7d491e2013-01-04 16:18:49 +0200459#define FOP1E(op, dst) \
460 FOP_ALIGN #op " %" #dst " \n\t" FOP_RET
461
462#define FASTOP1(op) \
463 FOP_START(op) \
464 FOP1E(op##b, al) \
465 FOP1E(op##w, ax) \
466 FOP1E(op##l, eax) \
467 ON64(FOP1E(op##q, rax)) \
468 FOP_END
469
Avi Kivityf7857f32013-01-04 16:18:53 +0200470#define FOP2E(op, dst, src) \
471 FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET
472
473#define FASTOP2(op) \
474 FOP_START(op) \
475 FOP2E(op##b, al, bl) \
476 FOP2E(op##w, ax, bx) \
477 FOP2E(op##l, eax, ebx) \
478 ON64(FOP2E(op##q, rax, rbx)) \
479 FOP_END
480
Avi Kivity007a3b52013-01-19 19:51:51 +0200481/* 2 operand, src is CL */
482#define FASTOP2CL(op) \
483 FOP_START(op) \
484 FOP2E(op##b, al, cl) \
485 FOP2E(op##w, ax, cl) \
486 FOP2E(op##l, eax, cl) \
487 ON64(FOP2E(op##q, rax, cl)) \
488 FOP_END
489
Avi Kivity0bdea062013-01-19 19:51:50 +0200490#define FOP3E(op, dst, src, src2) \
491 FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
492
493/* 3-operand, word-only, src2=cl */
494#define FASTOP3WCL(op) \
495 FOP_START(op) \
496 FOPNOP() \
497 FOP3E(op##w, ax, bx, cl) \
498 FOP3E(op##l, eax, ebx, cl) \
499 ON64(FOP3E(op##q, rax, rbx, cl)) \
500 FOP_END
501
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300502#define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \
Avi Kivityf6b35972010-08-26 11:59:00 +0300503 do { \
504 unsigned long _tmp; \
Avi Kivitydd856ef2012-08-27 23:46:17 +0300505 ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \
506 ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300507 \
508 __asm__ __volatile__ ( \
509 _PRE_EFLAGS("0", "5", "1") \
510 "1: \n\t" \
511 _op _suffix " %6; " \
512 "2: \n\t" \
513 _POST_EFLAGS("0", "5", "1") \
514 ".pushsection .fixup,\"ax\" \n\t" \
515 "3: movb $1, %4 \n\t" \
516 "jmp 2b \n\t" \
517 ".popsection \n\t" \
518 _ASM_EXTABLE(1b, 3b) \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300519 : "=m" ((ctxt)->eflags), "=&r" (_tmp), \
520 "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \
H. Peter Anvincb7cb282012-11-21 14:41:21 -0800521 : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300522 } while (0)
523
Mohammed Gamal3f9f53b2010-08-08 21:11:37 +0300524/* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300525#define emulate_1op_rax_rdx(ctxt, _op, _ex) \
Avi Kivity72952612011-04-20 13:12:27 +0300526 do { \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300527 switch((ctxt)->src.bytes) { \
Avi Kivity72952612011-04-20 13:12:27 +0300528 case 1: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300529 __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300530 break; \
531 case 2: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300532 __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \
Avi Kivity72952612011-04-20 13:12:27 +0300533 break; \
534 case 4: \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300535 __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300536 break; \
537 case 8: ON64( \
Avi Kivitye8f2b1d2011-09-07 16:41:40 +0300538 __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \
Avi Kivityf6b35972010-08-26 11:59:00 +0300539 break; \
540 } \
541 } while (0)
542
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200543static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
544 enum x86_intercept intercept,
545 enum x86_intercept_stage stage)
546{
547 struct x86_instruction_info info = {
548 .intercept = intercept,
Avi Kivity9dac77f2011-06-01 15:34:25 +0300549 .rep_prefix = ctxt->rep_prefix,
550 .modrm_mod = ctxt->modrm_mod,
551 .modrm_reg = ctxt->modrm_reg,
552 .modrm_rm = ctxt->modrm_rm,
553 .src_val = ctxt->src.val64,
554 .src_bytes = ctxt->src.bytes,
555 .dst_bytes = ctxt->dst.bytes,
556 .ad_bytes = ctxt->ad_bytes,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200557 .next_rip = ctxt->eip,
558 };
559
Avi Kivity29535382011-04-20 13:37:53 +0300560 return ctxt->ops->intercept(ctxt, &info, stage);
Joerg Roedel8a76d7f2011-04-04 12:39:27 +0200561}
562
Avi Kivityf47cfa32012-06-07 17:49:24 +0300563static void assign_masked(ulong *dest, ulong src, ulong mask)
564{
565 *dest = (*dest & ~mask) | (src & mask);
566}
567
Avi Kivity9dac77f2011-06-01 15:34:25 +0300568static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800569{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300570 return (1UL << (ctxt->ad_bytes << 3)) - 1;
Harvey Harrisonddcb2882008-02-18 11:12:48 -0800571}
572
Avi Kivityf47cfa32012-06-07 17:49:24 +0300573static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
574{
575 u16 sel;
576 struct desc_struct ss;
577
578 if (ctxt->mode == X86EMUL_MODE_PROT64)
579 return ~0UL;
580 ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
581 return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
582}
583
Avi Kivity612e89f2012-06-12 20:03:23 +0300584static int stack_size(struct x86_emulate_ctxt *ctxt)
585{
586 return (__fls(stack_mask(ctxt)) + 1) >> 3;
587}
588
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589/* Access/update address held in a register, based on addressing mode. */
Harvey Harrisone4706772008-02-19 07:40:38 -0800590static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300591address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800592{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300593 if (ctxt->ad_bytes == sizeof(unsigned long))
Harvey Harrisone4706772008-02-19 07:40:38 -0800594 return reg;
595 else
Avi Kivity9dac77f2011-06-01 15:34:25 +0300596 return reg & ad_mask(ctxt);
Harvey Harrisone4706772008-02-19 07:40:38 -0800597}
598
599static inline unsigned long
Avi Kivity9dac77f2011-06-01 15:34:25 +0300600register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)
Harvey Harrisone4706772008-02-19 07:40:38 -0800601{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300602 return address_mask(ctxt, reg);
Harvey Harrisone4706772008-02-19 07:40:38 -0800603}
604
Avi Kivity5ad105e2012-08-19 14:34:31 +0300605static void masked_increment(ulong *reg, ulong mask, int inc)
606{
607 assign_masked(reg, *reg + inc, mask);
608}
609
Harvey Harrison7a9572752008-02-19 07:40:41 -0800610static inline void
Avi Kivity9dac77f2011-06-01 15:34:25 +0300611register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800612{
Avi Kivity5ad105e2012-08-19 14:34:31 +0300613 ulong mask;
614
Avi Kivity9dac77f2011-06-01 15:34:25 +0300615 if (ctxt->ad_bytes == sizeof(unsigned long))
Avi Kivity5ad105e2012-08-19 14:34:31 +0300616 mask = ~0UL;
Harvey Harrison7a9572752008-02-19 07:40:41 -0800617 else
Avi Kivity5ad105e2012-08-19 14:34:31 +0300618 mask = ad_mask(ctxt);
619 masked_increment(reg, mask, inc);
620}
621
622static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
623{
Avi Kivitydd856ef2012-08-27 23:46:17 +0300624 masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800625}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626
Avi Kivity9dac77f2011-06-01 15:34:25 +0300627static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
Harvey Harrison7a9572752008-02-19 07:40:41 -0800628{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300629 register_address_increment(ctxt, &ctxt->_eip, rel);
Harvey Harrison7a9572752008-02-19 07:40:41 -0800630}
Nitin A Kamble098c9372007-08-19 11:00:36 +0300631
Avi Kivity56697682011-04-03 14:08:51 +0300632static u32 desc_limit_scaled(struct desc_struct *desc)
633{
634 u32 limit = get_desc_limit(desc);
635
636 return desc->g ? (limit << 12) | 0xfff : limit;
637}
638
Avi Kivity9dac77f2011-06-01 15:34:25 +0300639static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300640{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300641 ctxt->has_seg_override = true;
642 ctxt->seg_override = seg;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300643}
644
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900645static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300646{
647 if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
648 return 0;
649
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900650 return ctxt->ops->get_cached_segment_base(ctxt, seg);
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300651}
652
Avi Kivity9dac77f2011-06-01 15:34:25 +0300653static unsigned seg_override(struct x86_emulate_ctxt *ctxt)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300654{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300655 if (!ctxt->has_seg_override)
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300656 return 0;
657
Avi Kivity9dac77f2011-06-01 15:34:25 +0300658 return ctxt->seg_override;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300659}
660
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200661static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
662 u32 error, bool valid)
Gleb Natapov54b84862010-04-28 19:15:44 +0300663{
Avi Kivityda9cb572010-11-22 17:53:21 +0200664 ctxt->exception.vector = vec;
665 ctxt->exception.error_code = error;
666 ctxt->exception.error_code_valid = valid;
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200667 return X86EMUL_PROPAGATE_FAULT;
Gleb Natapov54b84862010-04-28 19:15:44 +0300668}
669
Joerg Roedel3b88e412011-04-04 12:39:29 +0200670static int emulate_db(struct x86_emulate_ctxt *ctxt)
671{
672 return emulate_exception(ctxt, DB_VECTOR, 0, false);
673}
674
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200675static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300676{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200677 return emulate_exception(ctxt, GP_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300678}
679
Avi Kivity618ff152011-04-03 12:32:09 +0300680static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
681{
682 return emulate_exception(ctxt, SS_VECTOR, err, true);
683}
684
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200685static int emulate_ud(struct x86_emulate_ctxt *ctxt)
Gleb Natapov54b84862010-04-28 19:15:44 +0300686{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200687 return emulate_exception(ctxt, UD_VECTOR, 0, false);
Gleb Natapov54b84862010-04-28 19:15:44 +0300688}
689
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200690static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
Gleb Natapov54b84862010-04-28 19:15:44 +0300691{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200692 return emulate_exception(ctxt, TS_VECTOR, err, true);
Gleb Natapov54b84862010-04-28 19:15:44 +0300693}
694
Avi Kivity34d1f492010-08-26 11:59:01 +0300695static int emulate_de(struct x86_emulate_ctxt *ctxt)
696{
Avi Kivity35d3d4a2010-11-22 17:53:25 +0200697 return emulate_exception(ctxt, DE_VECTOR, 0, false);
Avi Kivity34d1f492010-08-26 11:59:01 +0300698}
699
Avi Kivity12537912011-03-29 11:41:27 +0200700static int emulate_nm(struct x86_emulate_ctxt *ctxt)
701{
702 return emulate_exception(ctxt, NM_VECTOR, 0, false);
703}
704
Avi Kivity1aa36612011-04-27 13:20:30 +0300705static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
706{
707 u16 selector;
708 struct desc_struct desc;
709
710 ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
711 return selector;
712}
713
714static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
715 unsigned seg)
716{
717 u16 dummy;
718 u32 base3;
719 struct desc_struct desc;
720
721 ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
722 ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
723}
724
Avi Kivity1c11b372012-04-09 18:39:59 +0300725/*
726 * x86 defines three classes of vector instructions: explicitly
727 * aligned, explicitly unaligned, and the rest, which change behaviour
728 * depending on whether they're AVX encoded or not.
729 *
730 * Also included is CMPXCHG16B which is not a vector instruction, yet it is
731 * subject to the same check.
732 */
733static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
734{
735 if (likely(size < 16))
736 return false;
737
738 if (ctxt->d & Aligned)
739 return true;
740 else if (ctxt->d & Unaligned)
741 return false;
742 else if (ctxt->d & Avx)
743 return false;
744 else
745 return true;
746}
747
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400748static int __linearize(struct x86_emulate_ctxt *ctxt,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300749 struct segmented_address addr,
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400750 unsigned size, bool write, bool fetch,
Avi Kivity52fd8b42011-04-03 12:33:12 +0300751 ulong *linear)
752{
Avi Kivity618ff152011-04-03 12:32:09 +0300753 struct desc_struct desc;
754 bool usable;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300755 ulong la;
Avi Kivity618ff152011-04-03 12:32:09 +0300756 u32 lim;
Avi Kivity1aa36612011-04-27 13:20:30 +0300757 u16 sel;
Gleb Natapov3a78a4f2012-12-20 16:57:42 +0200758 unsigned cpl;
Avi Kivity52fd8b42011-04-03 12:33:12 +0300759
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +0900760 la = seg_base(ctxt, addr.seg) + addr.ea;
Avi Kivity618ff152011-04-03 12:32:09 +0300761 switch (ctxt->mode) {
Avi Kivity618ff152011-04-03 12:32:09 +0300762 case X86EMUL_MODE_PROT64:
763 if (((signed long)la << 16) >> 16 != la)
764 return emulate_gp(ctxt, 0);
765 break;
766 default:
Avi Kivity1aa36612011-04-27 13:20:30 +0300767 usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
768 addr.seg);
Avi Kivity618ff152011-04-03 12:32:09 +0300769 if (!usable)
770 goto bad;
Gleb Natapov58b78252012-12-11 15:14:12 +0200771 /* code segment in protected mode or read-only data segment */
772 if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
773 || !(desc.type & 2)) && write)
Avi Kivity618ff152011-04-03 12:32:09 +0300774 goto bad;
775 /* unreadable code segment */
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400776 if (!fetch && (desc.type & 8) && !(desc.type & 2))
Avi Kivity618ff152011-04-03 12:32:09 +0300777 goto bad;
778 lim = desc_limit_scaled(&desc);
779 if ((desc.type & 8) || !(desc.type & 4)) {
780 /* expand-up segment */
781 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
782 goto bad;
783 } else {
Guo Chaofc058682012-06-28 15:19:51 +0800784 /* expand-down segment */
Avi Kivity618ff152011-04-03 12:32:09 +0300785 if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
786 goto bad;
787 lim = desc.d ? 0xffffffff : 0xffff;
788 if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
789 goto bad;
790 }
Avi Kivity717746e2011-04-20 13:37:53 +0300791 cpl = ctxt->ops->cpl(ctxt);
Avi Kivity618ff152011-04-03 12:32:09 +0300792 if (!(desc.type & 8)) {
793 /* data segment */
794 if (cpl > desc.dpl)
795 goto bad;
796 } else if ((desc.type & 8) && !(desc.type & 4)) {
797 /* nonconforming code segment */
798 if (cpl != desc.dpl)
799 goto bad;
800 } else if ((desc.type & 8) && (desc.type & 4)) {
801 /* conforming code segment */
802 if (cpl < desc.dpl)
803 goto bad;
804 }
805 break;
806 }
Avi Kivity9dac77f2011-06-01 15:34:25 +0300807 if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
Avi Kivity52fd8b42011-04-03 12:33:12 +0300808 la &= (u32)-1;
Avi Kivity1c11b372012-04-09 18:39:59 +0300809 if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
810 return emulate_gp(ctxt, 0);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300811 *linear = la;
812 return X86EMUL_CONTINUE;
Avi Kivity618ff152011-04-03 12:32:09 +0300813bad:
814 if (addr.seg == VCPU_SREG_SS)
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300815 return emulate_ss(ctxt, sel);
Avi Kivity618ff152011-04-03 12:32:09 +0300816 else
Avi Kivity0afbe2f2012-08-21 17:07:06 +0300817 return emulate_gp(ctxt, sel);
Avi Kivity52fd8b42011-04-03 12:33:12 +0300818}
819
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400820static int linearize(struct x86_emulate_ctxt *ctxt,
821 struct segmented_address addr,
822 unsigned size, bool write,
823 ulong *linear)
824{
825 return __linearize(ctxt, addr, size, write, false, linear);
826}
827
828
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200829static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
830 struct segmented_address addr,
831 void *data,
832 unsigned size)
833{
Avi Kivity9fa088f2011-03-31 18:54:30 +0200834 int rc;
835 ulong linear;
836
Avi Kivity83b87952011-04-03 11:31:19 +0300837 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +0200838 if (rc != X86EMUL_CONTINUE)
839 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +0300840 return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200841}
842
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900843/*
844 * Fetch the next byte of the instruction being emulated which is pointed to
845 * by ctxt->_eip, then increment ctxt->_eip.
846 *
847 * Also prefetch the remaining bytes of the instruction without crossing page
848 * boundary if they are not in fetch_cache yet.
849 */
850static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest)
Avi Kivity62266862007-11-20 13:15:52 +0200851{
Avi Kivity9dac77f2011-06-01 15:34:25 +0300852 struct fetch_cache *fc = &ctxt->fetch;
Avi Kivity62266862007-11-20 13:15:52 +0200853 int rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300854 int size, cur_size;
Avi Kivity62266862007-11-20 13:15:52 +0200855
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900856 if (ctxt->_eip == fc->end) {
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400857 unsigned long linear;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900858 struct segmented_address addr = { .seg = VCPU_SREG_CS,
859 .ea = ctxt->_eip };
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300860 cur_size = fc->end - fc->start;
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900861 size = min(15UL - cur_size,
862 PAGE_SIZE - offset_in_page(ctxt->_eip));
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400863 rc = __linearize(ctxt, addr, size, false, true, &linear);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900864 if (unlikely(rc != X86EMUL_CONTINUE))
Nelson Elhage3d9b9382011-04-18 12:05:53 -0400865 return rc;
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +0900866 rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
867 size, &ctxt->exception);
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900868 if (unlikely(rc != X86EMUL_CONTINUE))
Avi Kivity62266862007-11-20 13:15:52 +0200869 return rc;
Avi Kivity2fb53ad2010-04-11 13:05:15 +0300870 fc->end += size;
Avi Kivity62266862007-11-20 13:15:52 +0200871 }
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900872 *dest = fc->data[ctxt->_eip - fc->start];
873 ctxt->_eip++;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900874 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200875}
876
877static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900878 void *dest, unsigned size)
Avi Kivity62266862007-11-20 13:15:52 +0200879{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900880 int rc;
Avi Kivity62266862007-11-20 13:15:52 +0200881
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200882 /* x86 instructions are limited to 15 bytes. */
Takuya Yoshikawa7d88bb42011-07-30 18:02:29 +0900883 if (unlikely(ctxt->_eip + size - ctxt->eip > 15))
Avi Kivityeb3c79e2009-11-24 15:20:15 +0200884 return X86EMUL_UNHANDLEABLE;
Avi Kivity62266862007-11-20 13:15:52 +0200885 while (size--) {
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900886 rc = do_insn_fetch_byte(ctxt, dest++);
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900887 if (rc != X86EMUL_CONTINUE)
Avi Kivity62266862007-11-20 13:15:52 +0200888 return rc;
889 }
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +0900890 return X86EMUL_CONTINUE;
Avi Kivity62266862007-11-20 13:15:52 +0200891}
892
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900893/* Fetch next part of the instruction being emulated. */
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900894#define insn_fetch(_type, _ctxt) \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900895({ unsigned long _x; \
Takuya Yoshikawae85a1082011-07-30 18:01:26 +0900896 rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900897 if (rc != X86EMUL_CONTINUE) \
898 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900899 (_type)_x; \
900})
901
Takuya Yoshikawa807941b2011-07-30 18:00:17 +0900902#define insn_fetch_arr(_arr, _size, _ctxt) \
903({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900904 if (rc != X86EMUL_CONTINUE) \
905 goto done; \
Takuya Yoshikawa67cbc902011-05-15 00:54:58 +0900906})
907
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000908/*
909 * Given the 'reg' portion of a ModRM byte, and a register block, return a
910 * pointer into the block that addresses the relevant register.
911 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
912 */
Avi Kivitydd856ef2012-08-27 23:46:17 +0300913static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
Rusty Russell1e3c5cb2007-07-17 23:16:11 +1000914 int highbyte_regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915{
916 void *p;
917
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
Avi Kivitydd856ef2012-08-27 23:46:17 +0300919 p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
920 else
921 p = reg_rmw(ctxt, modrm_reg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 return p;
923}
924
925static int read_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivity90de84f2010-11-17 15:28:21 +0200926 struct segmented_address addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 u16 *size, unsigned long *address, int op_bytes)
928{
929 int rc;
930
931 if (op_bytes == 2)
932 op_bytes = 3;
933 *address = 0;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200934 rc = segmented_read_std(ctxt, addr, size, 2);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +0900935 if (rc != X86EMUL_CONTINUE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 return rc;
Avi Kivity30b31ab2010-11-17 15:28:22 +0200937 addr.ea += 2;
Avi Kivity3ca3ac42011-03-31 16:52:26 +0200938 rc = segmented_read_std(ctxt, addr, address, op_bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 return rc;
940}
941
Nitin A Kamblebbe9abb2007-09-15 10:23:07 +0300942static int test_cc(unsigned int condition, unsigned int flags)
943{
944 int rc = 0;
945
946 switch ((condition & 15) >> 1) {
947 case 0: /* o */
948 rc |= (flags & EFLG_OF);
949 break;
950 case 1: /* b/c/nae */
951 rc |= (flags & EFLG_CF);
952 break;
953 case 2: /* z/e */
954 rc |= (flags & EFLG_ZF);
955 break;
956 case 3: /* be/na */
957 rc |= (flags & (EFLG_CF|EFLG_ZF));
958 break;
959 case 4: /* s */
960 rc |= (flags & EFLG_SF);
961 break;
962 case 5: /* p/pe */
963 rc |= (flags & EFLG_PF);
964 break;
965 case 7: /* le/ng */
966 rc |= (flags & EFLG_ZF);
967 /* fall through */
968 case 6: /* l/nge */
969 rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
970 break;
971 }
972
973 /* Odd condition identifiers (lsb == 1) have inverted sense. */
974 return (!!rc ^ (condition & 1));
975}
976
Avi Kivity91ff3cb2010-08-01 12:53:09 +0300977static void fetch_register_operand(struct operand *op)
978{
979 switch (op->bytes) {
980 case 1:
981 op->val = *(u8 *)op->addr.reg;
982 break;
983 case 2:
984 op->val = *(u16 *)op->addr.reg;
985 break;
986 case 4:
987 op->val = *(u32 *)op->addr.reg;
988 break;
989 case 8:
990 op->val = *(u64 *)op->addr.reg;
991 break;
992 }
993}
994
Avi Kivity12537912011-03-29 11:41:27 +0200995static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
996{
997 ctxt->ops->get_fpu(ctxt);
998 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +0200999 case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
1000 case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
1001 case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
1002 case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
1003 case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
1004 case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
1005 case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
1006 case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001007#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001008 case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
1009 case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
1010 case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
1011 case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
1012 case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
1013 case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
1014 case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
1015 case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001016#endif
1017 default: BUG();
1018 }
1019 ctxt->ops->put_fpu(ctxt);
1020}
1021
1022static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
1023 int reg)
1024{
1025 ctxt->ops->get_fpu(ctxt);
1026 switch (reg) {
Mathias Krause89a87c62012-08-30 01:30:14 +02001027 case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
1028 case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
1029 case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
1030 case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
1031 case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
1032 case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
1033 case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
1034 case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001035#ifdef CONFIG_X86_64
Mathias Krause89a87c62012-08-30 01:30:14 +02001036 case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
1037 case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
1038 case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
1039 case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
1040 case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
1041 case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
1042 case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
1043 case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
Avi Kivity12537912011-03-29 11:41:27 +02001044#endif
1045 default: BUG();
1046 }
1047 ctxt->ops->put_fpu(ctxt);
1048}
1049
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001050static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1051{
1052 ctxt->ops->get_fpu(ctxt);
1053 switch (reg) {
1054 case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
1055 case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
1056 case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
1057 case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
1058 case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
1059 case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
1060 case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
1061 case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
1062 default: BUG();
1063 }
1064 ctxt->ops->put_fpu(ctxt);
1065}
1066
1067static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
1068{
1069 ctxt->ops->get_fpu(ctxt);
1070 switch (reg) {
1071 case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
1072 case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
1073 case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
1074 case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
1075 case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
1076 case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
1077 case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
1078 case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
1079 default: BUG();
1080 }
1081 ctxt->ops->put_fpu(ctxt);
1082}
1083
Gleb Natapov045a2822012-12-20 16:57:43 +02001084static int em_fninit(struct x86_emulate_ctxt *ctxt)
1085{
1086 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1087 return emulate_nm(ctxt);
1088
1089 ctxt->ops->get_fpu(ctxt);
1090 asm volatile("fninit");
1091 ctxt->ops->put_fpu(ctxt);
1092 return X86EMUL_CONTINUE;
1093}
1094
1095static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
1096{
1097 u16 fcw;
1098
1099 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1100 return emulate_nm(ctxt);
1101
1102 ctxt->ops->get_fpu(ctxt);
1103 asm volatile("fnstcw %0": "+m"(fcw));
1104 ctxt->ops->put_fpu(ctxt);
1105
1106 /* force 2 byte destination */
1107 ctxt->dst.bytes = 2;
1108 ctxt->dst.val = fcw;
1109
1110 return X86EMUL_CONTINUE;
1111}
1112
1113static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
1114{
1115 u16 fsw;
1116
1117 if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
1118 return emulate_nm(ctxt);
1119
1120 ctxt->ops->get_fpu(ctxt);
1121 asm volatile("fnstsw %0": "+m"(fsw));
1122 ctxt->ops->put_fpu(ctxt);
1123
1124 /* force 2 byte destination */
1125 ctxt->dst.bytes = 2;
1126 ctxt->dst.val = fsw;
1127
1128 return X86EMUL_CONTINUE;
1129}
1130
Avi Kivity12537912011-03-29 11:41:27 +02001131static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001132 struct operand *op)
Avi Kivity3c118e22007-10-31 10:27:04 +02001133{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001134 unsigned reg = ctxt->modrm_reg;
1135 int highbyte_regs = ctxt->rex_prefix == 0;
Avi Kivity33615aa2007-10-31 11:15:56 +02001136
Avi Kivity9dac77f2011-06-01 15:34:25 +03001137 if (!(ctxt->d & ModRM))
1138 reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
Avi Kivity12537912011-03-29 11:41:27 +02001139
Avi Kivity9dac77f2011-06-01 15:34:25 +03001140 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001141 op->type = OP_XMM;
1142 op->bytes = 16;
1143 op->addr.xmm = reg;
1144 read_sse_reg(ctxt, &op->vec_val, reg);
1145 return;
1146 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001147 if (ctxt->d & Mmx) {
1148 reg &= 7;
1149 op->type = OP_MM;
1150 op->bytes = 8;
1151 op->addr.mm = reg;
1152 return;
1153 }
Avi Kivity12537912011-03-29 11:41:27 +02001154
Avi Kivity3c118e22007-10-31 10:27:04 +02001155 op->type = OP_REG;
Avi Kivity2adb5ad2012-01-16 15:08:45 +02001156 if (ctxt->d & ByteOp) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001157 op->addr.reg = decode_register(ctxt, reg, highbyte_regs);
Avi Kivity3c118e22007-10-31 10:27:04 +02001158 op->bytes = 1;
1159 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001160 op->addr.reg = decode_register(ctxt, reg, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001161 op->bytes = ctxt->op_bytes;
Avi Kivity3c118e22007-10-31 10:27:04 +02001162 }
Avi Kivity91ff3cb2010-08-01 12:53:09 +03001163 fetch_register_operand(op);
Avi Kivity3c118e22007-10-31 10:27:04 +02001164 op->orig_val = op->val;
1165}
1166
Avi Kivitya6e34072012-06-10 17:15:39 +03001167static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
1168{
1169 if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
1170 ctxt->modrm_seg = VCPU_SREG_SS;
1171}
1172
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001173static int decode_modrm(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001174 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001175{
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001176 u8 sib;
Avi Kivityf5b4edc2008-06-15 22:09:11 -07001177 int index_reg = 0, base_reg = 0, scale;
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001178 int rc = X86EMUL_CONTINUE;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001179 ulong modrm_ea = 0;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001180
Avi Kivity9dac77f2011-06-01 15:34:25 +03001181 if (ctxt->rex_prefix) {
1182 ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */
1183 index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */
1184 ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001185 }
1186
Avi Kivity9dac77f2011-06-01 15:34:25 +03001187 ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6;
1188 ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
1189 ctxt->modrm_rm |= (ctxt->modrm & 0x07);
1190 ctxt->modrm_seg = VCPU_SREG_DS;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001191
Avi Kivity9dac77f2011-06-01 15:34:25 +03001192 if (ctxt->modrm_mod == 3) {
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001193 op->type = OP_REG;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001194 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001195 op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp);
Avi Kivity9dac77f2011-06-01 15:34:25 +03001196 if (ctxt->d & Sse) {
Avi Kivity12537912011-03-29 11:41:27 +02001197 op->type = OP_XMM;
1198 op->bytes = 16;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001199 op->addr.xmm = ctxt->modrm_rm;
1200 read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
Avi Kivity12537912011-03-29 11:41:27 +02001201 return rc;
1202 }
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001203 if (ctxt->d & Mmx) {
1204 op->type = OP_MM;
1205 op->bytes = 8;
1206 op->addr.xmm = ctxt->modrm_rm & 7;
1207 return rc;
1208 }
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001209 fetch_register_operand(op);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001210 return rc;
1211 }
1212
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001213 op->type = OP_MEM;
1214
Avi Kivity9dac77f2011-06-01 15:34:25 +03001215 if (ctxt->ad_bytes == 2) {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001216 unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
1217 unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
1218 unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
1219 unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001220
1221 /* 16-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001222 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001223 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001224 if (ctxt->modrm_rm == 6)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001225 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001226 break;
1227 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001228 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001229 break;
1230 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001231 modrm_ea += insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001232 break;
1233 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001234 switch (ctxt->modrm_rm) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001235 case 0:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001236 modrm_ea += bx + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001237 break;
1238 case 1:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001239 modrm_ea += bx + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001240 break;
1241 case 2:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001242 modrm_ea += bp + si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001243 break;
1244 case 3:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001245 modrm_ea += bp + di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001246 break;
1247 case 4:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001248 modrm_ea += si;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001249 break;
1250 case 5:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001251 modrm_ea += di;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001252 break;
1253 case 6:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001254 if (ctxt->modrm_mod != 0)
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001255 modrm_ea += bp;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001256 break;
1257 case 7:
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001258 modrm_ea += bx;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001259 break;
1260 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001261 if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
1262 (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
1263 ctxt->modrm_seg = VCPU_SREG_SS;
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001264 modrm_ea = (u16)modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001265 } else {
1266 /* 32/64-bit ModR/M decode. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001267 if ((ctxt->modrm_rm & 7) == 4) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001268 sib = insn_fetch(u8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001269 index_reg |= (sib >> 3) & 7;
1270 base_reg |= sib & 7;
1271 scale = sib >> 6;
1272
Avi Kivity9dac77f2011-06-01 15:34:25 +03001273 if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001274 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivitya6e34072012-06-10 17:15:39 +03001275 else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03001276 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001277 adjust_modrm_seg(ctxt, base_reg);
1278 }
Avi Kivitydc71d0f2008-06-15 21:23:17 -07001279 if (index_reg != 4)
Avi Kivitydd856ef2012-08-27 23:46:17 +03001280 modrm_ea += reg_read(ctxt, index_reg) << scale;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001281 } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
Avi Kivity84411d82008-06-15 21:53:26 -07001282 if (ctxt->mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03001283 ctxt->rip_relative = 1;
Avi Kivitya6e34072012-06-10 17:15:39 +03001284 } else {
1285 base_reg = ctxt->modrm_rm;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001286 modrm_ea += reg_read(ctxt, base_reg);
Avi Kivitya6e34072012-06-10 17:15:39 +03001287 adjust_modrm_seg(ctxt, base_reg);
1288 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03001289 switch (ctxt->modrm_mod) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001290 case 0:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001291 if (ctxt->modrm_rm == 5)
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001292 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001293 break;
1294 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001295 modrm_ea += insn_fetch(s8, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001296 break;
1297 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001298 modrm_ea += insn_fetch(s32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001299 break;
1300 }
1301 }
Avi Kivity90de84f2010-11-17 15:28:21 +02001302 op->addr.mem.ea = modrm_ea;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001303done:
1304 return rc;
1305}
1306
1307static int decode_abs(struct x86_emulate_ctxt *ctxt,
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001308 struct operand *op)
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001309{
Takuya Yoshikawa3e2815e2010-02-12 15:53:59 +09001310 int rc = X86EMUL_CONTINUE;
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001311
Avi Kivity2dbd0dd2010-08-01 15:40:19 +03001312 op->type = OP_MEM;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001313 switch (ctxt->ad_bytes) {
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001314 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001315 op->addr.mem.ea = insn_fetch(u16, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001316 break;
1317 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001318 op->addr.mem.ea = insn_fetch(u32, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001319 break;
1320 case 8:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09001321 op->addr.mem.ea = insn_fetch(u64, ctxt);
Avi Kivity1c73ef6652007-11-01 06:31:28 +02001322 break;
1323 }
1324done:
1325 return rc;
1326}
1327
Avi Kivity9dac77f2011-06-01 15:34:25 +03001328static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
Wei Yongjun35c843c2010-08-09 11:34:56 +08001329{
Sheng Yang7129eec2010-09-28 16:33:32 +08001330 long sv = 0, mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001331
Avi Kivity9dac77f2011-06-01 15:34:25 +03001332 if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
1333 mask = ~(ctxt->dst.bytes * 8 - 1);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001334
Avi Kivity9dac77f2011-06-01 15:34:25 +03001335 if (ctxt->src.bytes == 2)
1336 sv = (s16)ctxt->src.val & (s16)mask;
1337 else if (ctxt->src.bytes == 4)
1338 sv = (s32)ctxt->src.val & (s32)mask;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001339
Avi Kivity9dac77f2011-06-01 15:34:25 +03001340 ctxt->dst.addr.mem.ea += (sv >> 3);
Wei Yongjun35c843c2010-08-09 11:34:56 +08001341 }
Wei Yongjunba7ff2b2010-08-09 11:39:14 +08001342
1343 /* only subword offset */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001344 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
Wei Yongjun35c843c2010-08-09 11:34:56 +08001345}
1346
Gleb Natapov9de41572010-04-28 19:15:22 +03001347static int read_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov9de41572010-04-28 19:15:22 +03001348 unsigned long addr, void *dest, unsigned size)
1349{
1350 int rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001351 struct read_cache *mc = &ctxt->mem_read;
Gleb Natapov9de41572010-04-28 19:15:22 +03001352
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001353 if (mc->pos < mc->end)
1354 goto read_cached;
Gleb Natapov9de41572010-04-28 19:15:22 +03001355
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001356 WARN_ON((mc->end + size) >= sizeof(mc->data));
Gleb Natapov9de41572010-04-28 19:15:22 +03001357
Xiao Guangrongf23b0702012-07-26 13:12:22 +08001358 rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
1359 &ctxt->exception);
1360 if (rc != X86EMUL_CONTINUE)
1361 return rc;
1362
1363 mc->end += size;
1364
1365read_cached:
1366 memcpy(dest, mc->data + mc->pos, size);
1367 mc->pos += size;
Gleb Natapov9de41572010-04-28 19:15:22 +03001368 return X86EMUL_CONTINUE;
1369}
1370
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001371static int segmented_read(struct x86_emulate_ctxt *ctxt,
1372 struct segmented_address addr,
1373 void *data,
1374 unsigned size)
1375{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001376 int rc;
1377 ulong linear;
1378
Avi Kivity83b87952011-04-03 11:31:19 +03001379 rc = linearize(ctxt, addr, size, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001380 if (rc != X86EMUL_CONTINUE)
1381 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001382 return read_emulated(ctxt, linear, data, size);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001383}
1384
1385static int segmented_write(struct x86_emulate_ctxt *ctxt,
1386 struct segmented_address addr,
1387 const void *data,
1388 unsigned size)
1389{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001390 int rc;
1391 ulong linear;
1392
Avi Kivity83b87952011-04-03 11:31:19 +03001393 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001394 if (rc != X86EMUL_CONTINUE)
1395 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001396 return ctxt->ops->write_emulated(ctxt, linear, data, size,
1397 &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001398}
1399
1400static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
1401 struct segmented_address addr,
1402 const void *orig_data, const void *data,
1403 unsigned size)
1404{
Avi Kivity9fa088f2011-03-31 18:54:30 +02001405 int rc;
1406 ulong linear;
1407
Avi Kivity83b87952011-04-03 11:31:19 +03001408 rc = linearize(ctxt, addr, size, true, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02001409 if (rc != X86EMUL_CONTINUE)
1410 return rc;
Avi Kivity0f65dd72011-04-20 13:37:53 +03001411 return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
1412 size, &ctxt->exception);
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001413}
1414
Gleb Natapov7b262e92010-03-18 15:20:27 +02001415static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
Gleb Natapov7b262e92010-03-18 15:20:27 +02001416 unsigned int size, unsigned short port,
1417 void *dest)
1418{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001419 struct read_cache *rc = &ctxt->io_read;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001420
1421 if (rc->pos == rc->end) { /* refill pio read ahead */
Gleb Natapov7b262e92010-03-18 15:20:27 +02001422 unsigned int in_page, n;
Avi Kivity9dac77f2011-06-01 15:34:25 +03001423 unsigned int count = ctxt->rep_prefix ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001424 address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
Gleb Natapov7b262e92010-03-18 15:20:27 +02001425 in_page = (ctxt->eflags & EFLG_DF) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001426 offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
1427 PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
Gleb Natapov7b262e92010-03-18 15:20:27 +02001428 n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
1429 count);
1430 if (n == 0)
1431 n = 1;
1432 rc->pos = rc->end = 0;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001433 if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
Gleb Natapov7b262e92010-03-18 15:20:27 +02001434 return 0;
1435 rc->end = n * size;
1436 }
1437
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001438 if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) {
1439 ctxt->dst.data = rc->data + rc->pos;
1440 ctxt->dst.type = OP_MEM_STR;
1441 ctxt->dst.count = (rc->end - rc->pos) / size;
1442 rc->pos = rc->end;
1443 } else {
1444 memcpy(dest, rc->data + rc->pos, size);
1445 rc->pos += size;
1446 }
Gleb Natapov7b262e92010-03-18 15:20:27 +02001447 return 1;
1448}
1449
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01001450static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
1451 u16 index, struct desc_struct *desc)
1452{
1453 struct desc_ptr dt;
1454 ulong addr;
1455
1456 ctxt->ops->get_idt(ctxt, &dt);
1457
1458 if (dt.size < index * 8 + 7)
1459 return emulate_gp(ctxt, index << 3 | 0x2);
1460
1461 addr = dt.address + index * 8;
1462 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1463 &ctxt->exception);
1464}
1465
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001466static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001467 u16 selector, struct desc_ptr *dt)
1468{
Mathias Krause0225fb52012-08-30 01:30:16 +02001469 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001470
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001471 if (selector & 1 << 2) {
1472 struct desc_struct desc;
Avi Kivity1aa36612011-04-27 13:20:30 +03001473 u16 sel;
1474
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001475 memset (dt, 0, sizeof *dt);
Avi Kivity1aa36612011-04-27 13:20:30 +03001476 if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001477 return;
1478
1479 dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
1480 dt->address = get_desc_base(&desc);
1481 } else
Avi Kivity4bff1e862011-04-20 13:37:53 +03001482 ops->get_gdt(ctxt, dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001483}
1484
1485/* allowed just for 8 bytes segments */
1486static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Avi Kivitye9194642012-06-13 16:29:39 +03001487 u16 selector, struct desc_struct *desc,
1488 ulong *desc_addr_p)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001489{
1490 struct desc_ptr dt;
1491 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001492 ulong addr;
1493
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001494 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001495
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001496 if (dt.size < index * 8 + 7)
1497 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001498
Avi Kivitye9194642012-06-13 16:29:39 +03001499 *desc_addr_p = addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001500 return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
1501 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001502}
1503
1504/* allowed just for 8 bytes segments */
1505static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001506 u16 selector, struct desc_struct *desc)
1507{
1508 struct desc_ptr dt;
1509 u16 index = selector >> 3;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001510 ulong addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001511
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001512 get_descriptor_table_ptr(ctxt, selector, &dt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001513
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001514 if (dt.size < index * 8 + 7)
1515 return emulate_gp(ctxt, selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001516
1517 addr = dt.address + index * 8;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001518 return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
1519 &ctxt->exception);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001520}
1521
Gleb Natapov5601d052011-03-07 14:55:06 +02001522/* Does not support long mode */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001523static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001524 u16 selector, int seg)
1525{
Avi Kivity869be992012-06-13 16:30:53 +03001526 struct desc_struct seg_desc, old_desc;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001527 u8 dpl, rpl, cpl;
1528 unsigned err_vec = GP_VECTOR;
1529 u32 err_code = 0;
1530 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
Avi Kivitye9194642012-06-13 16:29:39 +03001531 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001532 int ret;
Avi Kivity03ebebe2012-08-21 17:07:04 +03001533 u16 dummy;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001534
1535 memset(&seg_desc, 0, sizeof seg_desc);
1536
1537 if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
1538 || ctxt->mode == X86EMUL_MODE_REAL) {
1539 /* set real mode segment descriptor */
Avi Kivity03ebebe2012-08-21 17:07:04 +03001540 ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001541 set_desc_base(&seg_desc, selector << 4);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001542 goto load;
1543 }
1544
Avi Kivity79d5b4c2012-06-07 17:03:42 +03001545 rpl = selector & 3;
1546 cpl = ctxt->ops->cpl(ctxt);
1547
1548 /* NULL selector is not valid for TR, CS and SS (except for long mode) */
1549 if ((seg == VCPU_SREG_CS
1550 || (seg == VCPU_SREG_SS
1551 && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl))
1552 || seg == VCPU_SREG_TR)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001553 && null_selector)
1554 goto exception;
1555
1556 /* TR should be in GDT only */
1557 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
1558 goto exception;
1559
1560 if (null_selector) /* for NULL selector skip all following checks */
1561 goto load;
1562
Avi Kivitye9194642012-06-13 16:29:39 +03001563 ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001564 if (ret != X86EMUL_CONTINUE)
1565 return ret;
1566
1567 err_code = selector & 0xfffc;
1568 err_vec = GP_VECTOR;
1569
Guo Chaofc058682012-06-28 15:19:51 +08001570 /* can't load system descriptor into segment selector */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001571 if (seg <= VCPU_SREG_GS && !seg_desc.s)
1572 goto exception;
1573
1574 if (!seg_desc.p) {
1575 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
1576 goto exception;
1577 }
1578
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001579 dpl = seg_desc.dpl;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001580
1581 switch (seg) {
1582 case VCPU_SREG_SS:
1583 /*
1584 * segment is not a writable data segment or segment
1585 * selector's RPL != CPL or segment selector's RPL != CPL
1586 */
1587 if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
1588 goto exception;
1589 break;
1590 case VCPU_SREG_CS:
1591 if (!(seg_desc.type & 8))
1592 goto exception;
1593
1594 if (seg_desc.type & 4) {
1595 /* conforming */
1596 if (dpl > cpl)
1597 goto exception;
1598 } else {
1599 /* nonconforming */
1600 if (rpl > cpl || dpl != cpl)
1601 goto exception;
1602 }
1603 /* CS(RPL) <- CPL */
1604 selector = (selector & 0xfffc) | cpl;
1605 break;
1606 case VCPU_SREG_TR:
1607 if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
1608 goto exception;
Avi Kivity869be992012-06-13 16:30:53 +03001609 old_desc = seg_desc;
1610 seg_desc.type |= 2; /* busy */
1611 ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
1612 sizeof(seg_desc), &ctxt->exception);
1613 if (ret != X86EMUL_CONTINUE)
1614 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001615 break;
1616 case VCPU_SREG_LDTR:
1617 if (seg_desc.s || seg_desc.type != 2)
1618 goto exception;
1619 break;
1620 default: /* DS, ES, FS, or GS */
1621 /*
1622 * segment is not a data or readable code segment or
1623 * ((segment is a data or nonconforming code segment)
1624 * and (both RPL and CPL > DPL))
1625 */
1626 if ((seg_desc.type & 0xa) == 0x8 ||
1627 (((seg_desc.type & 0xc) != 0xc) &&
1628 (rpl > dpl && cpl > dpl)))
1629 goto exception;
1630 break;
1631 }
1632
1633 if (seg_desc.s) {
1634 /* mark segment as accessed */
1635 seg_desc.type |= 1;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001636 ret = write_segment_descriptor(ctxt, selector, &seg_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001637 if (ret != X86EMUL_CONTINUE)
1638 return ret;
1639 }
1640load:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001641 ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001642 return X86EMUL_CONTINUE;
1643exception:
Gleb Natapov54b84862010-04-28 19:15:44 +03001644 emulate_exception(ctxt, err_vec, err_code, true);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02001645 return X86EMUL_PROPAGATE_FAULT;
1646}
1647
Wei Yongjun31be40b2010-08-17 09:17:30 +08001648static void write_register_operand(struct operand *op)
1649{
1650 /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
1651 switch (op->bytes) {
1652 case 1:
1653 *(u8 *)op->addr.reg = (u8)op->val;
1654 break;
1655 case 2:
1656 *(u16 *)op->addr.reg = (u16)op->val;
1657 break;
1658 case 4:
1659 *op->addr.reg = (u32)op->val;
1660 break; /* 64b: zero-extend */
1661 case 8:
1662 *op->addr.reg = op->val;
1663 break;
1664 }
1665}
1666
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09001667static int writeback(struct x86_emulate_ctxt *ctxt)
Wei Yongjunc37eda12010-06-15 09:03:33 +08001668{
1669 int rc;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001670
Avi Kivityb6744dc2013-01-04 16:18:50 +02001671 if (ctxt->d & NoWrite)
1672 return X86EMUL_CONTINUE;
1673
Avi Kivity9dac77f2011-06-01 15:34:25 +03001674 switch (ctxt->dst.type) {
Wei Yongjunc37eda12010-06-15 09:03:33 +08001675 case OP_REG:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001676 write_register_operand(&ctxt->dst);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001677 break;
1678 case OP_MEM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001679 if (ctxt->lock_prefix)
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001680 rc = segmented_cmpxchg(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001681 ctxt->dst.addr.mem,
1682 &ctxt->dst.orig_val,
1683 &ctxt->dst.val,
1684 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001685 else
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001686 rc = segmented_write(ctxt,
Avi Kivity9dac77f2011-06-01 15:34:25 +03001687 ctxt->dst.addr.mem,
1688 &ctxt->dst.val,
1689 ctxt->dst.bytes);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001690 if (rc != X86EMUL_CONTINUE)
1691 return rc;
1692 break;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03001693 case OP_MEM_STR:
1694 rc = segmented_write(ctxt,
1695 ctxt->dst.addr.mem,
1696 ctxt->dst.data,
1697 ctxt->dst.bytes * ctxt->dst.count);
1698 if (rc != X86EMUL_CONTINUE)
1699 return rc;
1700 break;
Avi Kivity12537912011-03-29 11:41:27 +02001701 case OP_XMM:
Avi Kivity9dac77f2011-06-01 15:34:25 +03001702 write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm);
Avi Kivity12537912011-03-29 11:41:27 +02001703 break;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03001704 case OP_MM:
1705 write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm);
1706 break;
Wei Yongjunc37eda12010-06-15 09:03:33 +08001707 case OP_NONE:
1708 /* no writeback */
1709 break;
1710 default:
1711 break;
1712 }
1713 return X86EMUL_CONTINUE;
1714}
1715
Avi Kivity51ddff52012-06-12 20:19:40 +03001716static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001717{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001718 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001719
Avi Kivity5ad105e2012-08-19 14:34:31 +03001720 rsp_increment(ctxt, -bytes);
Avi Kivitydd856ef2012-08-27 23:46:17 +03001721 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001722 addr.seg = VCPU_SREG_SS;
1723
Avi Kivity51ddff52012-06-12 20:19:40 +03001724 return segmented_write(ctxt, addr, data, bytes);
1725}
1726
1727static int em_push(struct x86_emulate_ctxt *ctxt)
1728{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09001729 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001730 ctxt->dst.type = OP_NONE;
Avi Kivity51ddff52012-06-12 20:19:40 +03001731 return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001732}
1733
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001734static int emulate_pop(struct x86_emulate_ctxt *ctxt,
Avi Kivity350f69d2009-01-05 11:12:40 +02001735 void *dest, int len)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001736{
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001737 int rc;
Avi Kivity90de84f2010-11-17 15:28:21 +02001738 struct segmented_address addr;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001739
Avi Kivitydd856ef2012-08-27 23:46:17 +03001740 addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
Avi Kivity90de84f2010-11-17 15:28:21 +02001741 addr.seg = VCPU_SREG_SS;
Avi Kivity3ca3ac42011-03-31 16:52:26 +02001742 rc = segmented_read(ctxt, addr, dest, len);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09001743 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001744 return rc;
1745
Avi Kivity5ad105e2012-08-19 14:34:31 +03001746 rsp_increment(ctxt, len);
Avi Kivityfaa5a3a2008-11-27 17:36:41 +02001747 return rc;
1748}
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02001749
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001750static int em_pop(struct x86_emulate_ctxt *ctxt)
1751{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001752 return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09001753}
1754
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001755static int emulate_popf(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001756 void *dest, int len)
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001757{
1758 int rc;
1759 unsigned long val, change_mask;
1760 int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001761 int cpl = ctxt->ops->cpl(ctxt);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001762
Takuya Yoshikawa3b9be3b2011-05-02 02:27:55 +09001763 rc = emulate_pop(ctxt, &val, len);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001764 if (rc != X86EMUL_CONTINUE)
1765 return rc;
1766
1767 change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
1768 | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
1769
1770 switch(ctxt->mode) {
1771 case X86EMUL_MODE_PROT64:
1772 case X86EMUL_MODE_PROT32:
1773 case X86EMUL_MODE_PROT16:
1774 if (cpl == 0)
1775 change_mask |= EFLG_IOPL;
1776 if (cpl <= iopl)
1777 change_mask |= EFLG_IF;
1778 break;
1779 case X86EMUL_MODE_VM86:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001780 if (iopl < 3)
1781 return emulate_gp(ctxt, 0);
Gleb Natapovd4c6a152010-02-10 14:21:34 +02001782 change_mask |= EFLG_IF;
1783 break;
1784 default: /* real mode */
1785 change_mask |= (EFLG_IOPL | EFLG_IF);
1786 break;
1787 }
1788
1789 *(unsigned long *)dest =
1790 (ctxt->eflags & ~change_mask) | (val & change_mask);
1791
1792 return rc;
1793}
1794
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001795static int em_popf(struct x86_emulate_ctxt *ctxt)
1796{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001797 ctxt->dst.type = OP_REG;
1798 ctxt->dst.addr.reg = &ctxt->eflags;
1799 ctxt->dst.bytes = ctxt->op_bytes;
1800 return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001801}
1802
Avi Kivity612e89f2012-06-12 20:03:23 +03001803static int em_enter(struct x86_emulate_ctxt *ctxt)
1804{
1805 int rc;
1806 unsigned frame_size = ctxt->src.val;
1807 unsigned nesting_level = ctxt->src2.val & 31;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001808 ulong rbp;
Avi Kivity612e89f2012-06-12 20:03:23 +03001809
1810 if (nesting_level)
1811 return X86EMUL_UNHANDLEABLE;
1812
Avi Kivitydd856ef2012-08-27 23:46:17 +03001813 rbp = reg_read(ctxt, VCPU_REGS_RBP);
1814 rc = push(ctxt, &rbp, stack_size(ctxt));
Avi Kivity612e89f2012-06-12 20:03:23 +03001815 if (rc != X86EMUL_CONTINUE)
1816 return rc;
Avi Kivitydd856ef2012-08-27 23:46:17 +03001817 assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
Avi Kivity612e89f2012-06-12 20:03:23 +03001818 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001819 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
1820 reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
Avi Kivity612e89f2012-06-12 20:03:23 +03001821 stack_mask(ctxt));
1822 return X86EMUL_CONTINUE;
1823}
1824
Avi Kivityf47cfa32012-06-07 17:49:24 +03001825static int em_leave(struct x86_emulate_ctxt *ctxt)
1826{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001827 assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
Avi Kivityf47cfa32012-06-07 17:49:24 +03001828 stack_mask(ctxt));
Avi Kivitydd856ef2012-08-27 23:46:17 +03001829 return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
Avi Kivityf47cfa32012-06-07 17:49:24 +03001830}
1831
Avi Kivity1cd196e2011-09-13 10:45:51 +03001832static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001833{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001834 int seg = ctxt->src2.val;
1835
Avi Kivity9dac77f2011-06-01 15:34:25 +03001836 ctxt->src.val = get_segment_selector(ctxt, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001837
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001838 return em_push(ctxt);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001839}
1840
Avi Kivity1cd196e2011-09-13 10:45:51 +03001841static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001842{
Avi Kivity1cd196e2011-09-13 10:45:51 +03001843 int seg = ctxt->src2.val;
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001844 unsigned long selector;
1845 int rc;
1846
Avi Kivity9dac77f2011-06-01 15:34:25 +03001847 rc = emulate_pop(ctxt, &selector, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001848 if (rc != X86EMUL_CONTINUE)
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001849 return rc;
1850
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001851 rc = load_segment_descriptor(ctxt, (u16)selector, seg);
Mohammed Gamal0934ac92009-08-23 14:24:24 +03001852 return rc;
1853}
1854
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001855static int em_pusha(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001856{
Avi Kivitydd856ef2012-08-27 23:46:17 +03001857 unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001858 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001859 int reg = VCPU_REGS_RAX;
1860
1861 while (reg <= VCPU_REGS_RDI) {
1862 (reg == VCPU_REGS_RSP) ?
Avi Kivitydd856ef2012-08-27 23:46:17 +03001863 (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001864
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001865 rc = em_push(ctxt);
Wei Yongjunc37eda12010-06-15 09:03:33 +08001866 if (rc != X86EMUL_CONTINUE)
1867 return rc;
1868
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001869 ++reg;
1870 }
Wei Yongjunc37eda12010-06-15 09:03:33 +08001871
Wei Yongjunc37eda12010-06-15 09:03:33 +08001872 return rc;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001873}
1874
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001875static int em_pushf(struct x86_emulate_ctxt *ctxt)
1876{
Avi Kivity9dac77f2011-06-01 15:34:25 +03001877 ctxt->src.val = (unsigned long)ctxt->eflags;
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09001878 return em_push(ctxt);
1879}
1880
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09001881static int em_popa(struct x86_emulate_ctxt *ctxt)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001882{
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001883 int rc = X86EMUL_CONTINUE;
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001884 int reg = VCPU_REGS_RDI;
1885
1886 while (reg >= VCPU_REGS_RAX) {
1887 if (reg == VCPU_REGS_RSP) {
Avi Kivity5ad105e2012-08-19 14:34:31 +03001888 rsp_increment(ctxt, ctxt->op_bytes);
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001889 --reg;
1890 }
1891
Avi Kivitydd856ef2012-08-27 23:46:17 +03001892 rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09001893 if (rc != X86EMUL_CONTINUE)
Mohammed Gamalabcf14b2009-09-01 15:28:11 +02001894 break;
1895 --reg;
1896 }
1897 return rc;
1898}
1899
Avi Kivitydd856ef2012-08-27 23:46:17 +03001900static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001901{
Mathias Krause0225fb52012-08-30 01:30:16 +02001902 const struct x86_emulate_ops *ops = ctxt->ops;
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001903 int rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001904 struct desc_ptr dt;
1905 gva_t cs_addr;
1906 gva_t eip_addr;
1907 u16 cs, eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001908
1909 /* TODO: Add limit checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03001910 ctxt->src.val = ctxt->eflags;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001911 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001912 if (rc != X86EMUL_CONTINUE)
1913 return rc;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001914
1915 ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
1916
Avi Kivity9dac77f2011-06-01 15:34:25 +03001917 ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
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
Avi Kivity9dac77f2011-06-01 15:34:25 +03001922 ctxt->src.val = ctxt->_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09001923 rc = em_push(ctxt);
Avi Kivity5c56e1c2010-08-17 11:17:51 +03001924 if (rc != X86EMUL_CONTINUE)
1925 return rc;
1926
Avi Kivity4bff1e862011-04-20 13:37:53 +03001927 ops->get_idt(ctxt, &dt);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001928
1929 eip_addr = dt.address + (irq << 2);
1930 cs_addr = dt.address + (irq << 2) + 2;
1931
Avi Kivity0f65dd72011-04-20 13:37:53 +03001932 rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001933 if (rc != X86EMUL_CONTINUE)
1934 return rc;
1935
Avi Kivity0f65dd72011-04-20 13:37:53 +03001936 rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001937 if (rc != X86EMUL_CONTINUE)
1938 return rc;
1939
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001940 rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001941 if (rc != X86EMUL_CONTINUE)
1942 return rc;
1943
Avi Kivity9dac77f2011-06-01 15:34:25 +03001944 ctxt->_eip = eip;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001945
1946 return rc;
1947}
1948
Avi Kivitydd856ef2012-08-27 23:46:17 +03001949int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
1950{
1951 int rc;
1952
1953 invalidate_registers(ctxt);
1954 rc = __emulate_int_real(ctxt, irq);
1955 if (rc == X86EMUL_CONTINUE)
1956 writeback_registers(ctxt);
1957 return rc;
1958}
1959
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001960static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001961{
1962 switch(ctxt->mode) {
1963 case X86EMUL_MODE_REAL:
Avi Kivitydd856ef2012-08-27 23:46:17 +03001964 return __emulate_int_real(ctxt, irq);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03001965 case X86EMUL_MODE_VM86:
1966 case X86EMUL_MODE_PROT16:
1967 case X86EMUL_MODE_PROT32:
1968 case X86EMUL_MODE_PROT64:
1969 default:
1970 /* Protected mode interrupts unimplemented yet */
1971 return X86EMUL_UNHANDLEABLE;
1972 }
1973}
1974
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09001975static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001976{
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001977 int rc = X86EMUL_CONTINUE;
1978 unsigned long temp_eip = 0;
1979 unsigned long temp_eflags = 0;
1980 unsigned long cs = 0;
1981 unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
1982 EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
1983 EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
1984 unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
1985
1986 /* TODO: Add stack limit check */
1987
Avi Kivity9dac77f2011-06-01 15:34:25 +03001988 rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001989
1990 if (rc != X86EMUL_CONTINUE)
1991 return rc;
1992
Avi Kivity35d3d4a2010-11-22 17:53:25 +02001993 if (temp_eip & ~0xffff)
1994 return emulate_gp(ctxt, 0);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001995
Avi Kivity9dac77f2011-06-01 15:34:25 +03001996 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03001997
1998 if (rc != X86EMUL_CONTINUE)
1999 return rc;
2000
Avi Kivity9dac77f2011-06-01 15:34:25 +03002001 rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002002
2003 if (rc != X86EMUL_CONTINUE)
2004 return rc;
2005
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002006 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002007
2008 if (rc != X86EMUL_CONTINUE)
2009 return rc;
2010
Avi Kivity9dac77f2011-06-01 15:34:25 +03002011 ctxt->_eip = temp_eip;
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002012
2013
Avi Kivity9dac77f2011-06-01 15:34:25 +03002014 if (ctxt->op_bytes == 4)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002015 ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
Avi Kivity9dac77f2011-06-01 15:34:25 +03002016 else if (ctxt->op_bytes == 2) {
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002017 ctxt->eflags &= ~0xffff;
2018 ctxt->eflags |= temp_eflags;
2019 }
2020
2021 ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
2022 ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
2023
2024 return rc;
2025}
2026
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002027static int em_iret(struct x86_emulate_ctxt *ctxt)
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002028{
2029 switch(ctxt->mode) {
2030 case X86EMUL_MODE_REAL:
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002031 return emulate_iret_real(ctxt);
Mohammed Gamal62bd4302010-07-28 12:38:40 +03002032 case X86EMUL_MODE_VM86:
2033 case X86EMUL_MODE_PROT16:
2034 case X86EMUL_MODE_PROT32:
2035 case X86EMUL_MODE_PROT64:
2036 default:
2037 /* iret from protected mode unimplemented yet */
2038 return X86EMUL_UNHANDLEABLE;
2039 }
2040}
2041
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002042static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
2043{
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002044 int rc;
2045 unsigned short sel;
2046
Avi Kivity9dac77f2011-06-01 15:34:25 +03002047 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002048
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002049 rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002050 if (rc != X86EMUL_CONTINUE)
2051 return rc;
2052
Avi Kivity9dac77f2011-06-01 15:34:25 +03002053 ctxt->_eip = 0;
2054 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002055 return X86EMUL_CONTINUE;
2056}
2057
Avi Kivity45a14672013-01-04 16:18:52 +02002058FASTOP1(not);
2059FASTOP1(neg);
Avi Kivity3329ece2011-09-13 10:45:39 +03002060
Avi Kivity007a3b52013-01-19 19:51:51 +02002061FASTOP2CL(rol);
2062FASTOP2CL(ror);
2063FASTOP2CL(rcl);
2064FASTOP2CL(rcr);
2065FASTOP2CL(shl);
2066FASTOP2CL(shr);
2067FASTOP2CL(sar);
2068
Avi Kivity3329ece2011-09-13 10:45:39 +03002069static int em_mul_ex(struct x86_emulate_ctxt *ctxt)
2070{
2071 u8 ex = 0;
2072
2073 emulate_1op_rax_rdx(ctxt, "mul", ex);
2074 return X86EMUL_CONTINUE;
2075}
2076
2077static int em_imul_ex(struct x86_emulate_ctxt *ctxt)
2078{
2079 u8 ex = 0;
2080
2081 emulate_1op_rax_rdx(ctxt, "imul", ex);
2082 return X86EMUL_CONTINUE;
2083}
2084
2085static int em_div_ex(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002086{
Avi Kivity34d1f492010-08-26 11:59:01 +03002087 u8 de = 0;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002088
Avi Kivity3329ece2011-09-13 10:45:39 +03002089 emulate_1op_rax_rdx(ctxt, "div", de);
2090 if (de)
2091 return emulate_de(ctxt);
2092 return X86EMUL_CONTINUE;
2093}
2094
2095static int em_idiv_ex(struct x86_emulate_ctxt *ctxt)
2096{
2097 u8 de = 0;
2098
2099 emulate_1op_rax_rdx(ctxt, "idiv", de);
Avi Kivity34d1f492010-08-26 11:59:01 +03002100 if (de)
2101 return emulate_de(ctxt);
Mohammed Gamal8c5eee32010-08-08 21:11:38 +03002102 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002103}
2104
Takuya Yoshikawa51187682011-05-02 02:29:17 +09002105static int em_grp45(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002106{
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002107 int rc = X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002108
Avi Kivity9dac77f2011-06-01 15:34:25 +03002109 switch (ctxt->modrm_reg) {
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002110 case 0: /* inc */
Avi Kivityd1eef452011-09-07 16:41:38 +03002111 emulate_1op(ctxt, "inc");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002112 break;
2113 case 1: /* dec */
Avi Kivityd1eef452011-09-07 16:41:38 +03002114 emulate_1op(ctxt, "dec");
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002115 break;
Mohammed Gamald19292e2008-09-08 21:47:19 +03002116 case 2: /* call near abs */ {
2117 long int old_eip;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002118 old_eip = ctxt->_eip;
2119 ctxt->_eip = ctxt->src.val;
2120 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002121 rc = em_push(ctxt);
Mohammed Gamald19292e2008-09-08 21:47:19 +03002122 break;
2123 }
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002124 case 4: /* jmp abs */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002125 ctxt->_eip = ctxt->src.val;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002126 break;
Takuya Yoshikawad2f62762011-05-02 02:30:48 +09002127 case 5: /* jmp far */
2128 rc = em_jmp_far(ctxt);
2129 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002130 case 6: /* push */
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002131 rc = em_push(ctxt);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002132 break;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002133 }
Takuya Yoshikawa4179bb02011-04-13 00:29:09 +09002134 return rc;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002135}
2136
Takuya Yoshikawae0dac402011-12-06 18:07:27 +09002137static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002138{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002139 u64 old = ctxt->dst.orig_val64;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002140
Avi Kivitydd856ef2012-08-27 23:46:17 +03002141 if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
2142 ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
2143 *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
2144 *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
Laurent Vivier05f086f2007-09-24 11:10:55 +02002145 ctxt->eflags &= ~EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002146 } else {
Avi Kivitydd856ef2012-08-27 23:46:17 +03002147 ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
2148 (u32) reg_read(ctxt, VCPU_REGS_RBX);
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002149
Laurent Vivier05f086f2007-09-24 11:10:55 +02002150 ctxt->eflags |= EFLG_ZF;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002151 }
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002152 return X86EMUL_CONTINUE;
Laurent Vivier8cdbd2c2007-09-24 11:10:54 +02002153}
2154
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002155static int em_ret(struct x86_emulate_ctxt *ctxt)
2156{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002157 ctxt->dst.type = OP_REG;
2158 ctxt->dst.addr.reg = &ctxt->_eip;
2159 ctxt->dst.bytes = ctxt->op_bytes;
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09002160 return em_pop(ctxt);
2161}
2162
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002163static int em_ret_far(struct x86_emulate_ctxt *ctxt)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002164{
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002165 int rc;
2166 unsigned long cs;
2167
Avi Kivity9dac77f2011-06-01 15:34:25 +03002168 rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002169 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002170 return rc;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002171 if (ctxt->op_bytes == 4)
2172 ctxt->_eip = (u32)ctxt->_eip;
2173 rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09002174 if (rc != X86EMUL_CONTINUE)
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002175 return rc;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002176 rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
Avi Kivitya77ab5e2009-01-05 13:27:34 +02002177 return rc;
2178}
2179
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002180static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
2181{
2182 /* Save real source value, then compare EAX against destination. */
2183 ctxt->src.orig_val = ctxt->src.val;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002184 ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002185 emulate_2op_SrcV(ctxt, "cmp");
2186
2187 if (ctxt->eflags & EFLG_ZF) {
2188 /* Success: write back to memory. */
2189 ctxt->dst.val = ctxt->src.orig_val;
2190 } else {
2191 /* Failure: write the value we saw to EAX. */
2192 ctxt->dst.type = OP_REG;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002193 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09002194 }
2195 return X86EMUL_CONTINUE;
2196}
2197
Avi Kivityd4b43252011-09-13 10:45:50 +03002198static int em_lseg(struct x86_emulate_ctxt *ctxt)
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002199{
Avi Kivityd4b43252011-09-13 10:45:50 +03002200 int seg = ctxt->src2.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002201 unsigned short sel;
2202 int rc;
2203
Avi Kivity9dac77f2011-06-01 15:34:25 +03002204 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002205
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002206 rc = load_segment_descriptor(ctxt, sel, seg);
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002207 if (rc != X86EMUL_CONTINUE)
2208 return rc;
2209
Avi Kivity9dac77f2011-06-01 15:34:25 +03002210 ctxt->dst.val = ctxt->src.val;
Wei Yongjun09b5f4d2010-08-23 14:56:54 +08002211 return rc;
2212}
2213
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002214static void
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002215setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002216 struct desc_struct *cs, struct desc_struct *ss)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002217{
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002218 cs->l = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002219 set_desc_base(cs, 0); /* flat segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002220 cs->g = 1; /* 4kb granularity */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002221 set_desc_limit(cs, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002222 cs->type = 0x0b; /* Read, Execute, Accessed */
2223 cs->s = 1;
2224 cs->dpl = 0; /* will be adjusted later */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002225 cs->p = 1;
2226 cs->d = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002227 cs->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002228
Gleb Natapov79168fd2010-04-28 19:15:30 +03002229 set_desc_base(ss, 0); /* flat segment */
2230 set_desc_limit(ss, 0xfffff); /* 4GB limit */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002231 ss->g = 1; /* 4kb granularity */
2232 ss->s = 1;
2233 ss->type = 0x03; /* Read/Write, Accessed */
Gleb Natapov79168fd2010-04-28 19:15:30 +03002234 ss->d = 1; /* 32bit stack segment */
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002235 ss->dpl = 0;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002236 ss->p = 1;
Gleb Natapov99245b52012-07-25 15:49:42 +03002237 ss->l = 0;
2238 ss->avl = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002239}
2240
Avi Kivity1a18a692012-02-01 12:23:21 +02002241static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
2242{
2243 u32 eax, ebx, ecx, edx;
2244
2245 eax = ecx = 0;
Avi Kivity0017f932012-06-07 14:10:16 +03002246 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2247 return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
Avi Kivity1a18a692012-02-01 12:23:21 +02002248 && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
2249 && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
2250}
2251
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002252static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
2253{
Mathias Krause0225fb52012-08-30 01:30:16 +02002254 const struct x86_emulate_ops *ops = ctxt->ops;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002255 u32 eax, ebx, ecx, edx;
2256
2257 /*
2258 * syscall should always be enabled in longmode - so only become
2259 * vendor specific (cpuid) if other modes are active...
2260 */
2261 if (ctxt->mode == X86EMUL_MODE_PROT64)
2262 return true;
2263
2264 eax = 0x00000000;
2265 ecx = 0x00000000;
Avi Kivity0017f932012-06-07 14:10:16 +03002266 ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
2267 /*
2268 * Intel ("GenuineIntel")
2269 * remark: Intel CPUs only support "syscall" in 64bit
2270 * longmode. Also an 64bit guest with a
2271 * 32bit compat-app running will #UD !! While this
2272 * behaviour can be fixed (by emulating) into AMD
2273 * response - CPUs of AMD can't behave like Intel.
2274 */
2275 if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
2276 ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
2277 edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
2278 return false;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002279
Avi Kivity0017f932012-06-07 14:10:16 +03002280 /* AMD ("AuthenticAMD") */
2281 if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
2282 ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
2283 edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
2284 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002285
Avi Kivity0017f932012-06-07 14:10:16 +03002286 /* AMD ("AMDisbetter!") */
2287 if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
2288 ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
2289 edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
2290 return true;
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002291
2292 /* default: (not Intel, not AMD), apply Intel's stricter rules... */
2293 return false;
2294}
2295
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002296static int em_syscall(struct x86_emulate_ctxt *ctxt)
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002297{
Mathias Krause0225fb52012-08-30 01:30:16 +02002298 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002299 struct desc_struct cs, ss;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002300 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002301 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002302 u64 efer = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002303
2304 /* syscall is not available in real mode */
Gleb Natapov2e901c42010-03-18 15:20:12 +02002305 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002306 ctxt->mode == X86EMUL_MODE_VM86)
2307 return emulate_ud(ctxt);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002308
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002309 if (!(em_syscall_is_enabled(ctxt)))
2310 return emulate_ud(ctxt);
2311
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002312 ops->get_msr(ctxt, MSR_EFER, &efer);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002313 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002314
Stephan Bärwolfc2226fc2012-01-12 16:43:04 +01002315 if (!(efer & EFER_SCE))
2316 return emulate_ud(ctxt);
2317
Avi Kivity717746e2011-04-20 13:37:53 +03002318 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002319 msr_data >>= 32;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002320 cs_sel = (u16)(msr_data & 0xfffc);
2321 ss_sel = (u16)(msr_data + 8);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002322
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002323 if (efer & EFER_LMA) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002324 cs.d = 0;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002325 cs.l = 1;
2326 }
Avi Kivity1aa36612011-04-27 13:20:30 +03002327 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2328 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002329
Avi Kivitydd856ef2012-08-27 23:46:17 +03002330 *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002331 if (efer & EFER_LMA) {
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002332#ifdef CONFIG_X86_64
Avi Kivitydd856ef2012-08-27 23:46:17 +03002333 *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002334
Avi Kivity717746e2011-04-20 13:37:53 +03002335 ops->get_msr(ctxt,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03002336 ctxt->mode == X86EMUL_MODE_PROT64 ?
2337 MSR_LSTAR : MSR_CSTAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002338 ctxt->_eip = msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002339
Avi Kivity717746e2011-04-20 13:37:53 +03002340 ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002341 ctxt->eflags &= ~(msr_data | EFLG_RF);
2342#endif
2343 } else {
2344 /* legacy mode */
Avi Kivity717746e2011-04-20 13:37:53 +03002345 ops->get_msr(ctxt, MSR_STAR, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002346 ctxt->_eip = (u32)msr_data;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002347
2348 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
2349 }
2350
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002351 return X86EMUL_CONTINUE;
Andre Przywarae66bb2c2009-06-18 12:56:00 +02002352}
2353
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002354static int em_sysenter(struct x86_emulate_ctxt *ctxt)
Andre Przywara8c604352009-06-18 12:56:01 +02002355{
Mathias Krause0225fb52012-08-30 01:30:16 +02002356 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002357 struct desc_struct cs, ss;
Andre Przywara8c604352009-06-18 12:56:01 +02002358 u64 msr_data;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002359 u16 cs_sel, ss_sel;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002360 u64 efer = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002361
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002362 ops->get_msr(ctxt, MSR_EFER, &efer);
Gleb Natapova0044752010-02-10 14:21:31 +02002363 /* inject #GP if in real mode */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002364 if (ctxt->mode == X86EMUL_MODE_REAL)
2365 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002366
Avi Kivity1a18a692012-02-01 12:23:21 +02002367 /*
2368 * Not recognized on AMD in compat mode (but is recognized in legacy
2369 * mode).
2370 */
2371 if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
2372 && !vendor_intel(ctxt))
2373 return emulate_ud(ctxt);
2374
Andre Przywara8c604352009-06-18 12:56:01 +02002375 /* XXX sysenter/sysexit have not been tested in 64bit mode.
2376 * Therefore, we inject an #UD.
2377 */
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002378 if (ctxt->mode == X86EMUL_MODE_PROT64)
2379 return emulate_ud(ctxt);
Andre Przywara8c604352009-06-18 12:56:01 +02002380
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002381 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara8c604352009-06-18 12:56:01 +02002382
Avi Kivity717746e2011-04-20 13:37:53 +03002383 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara8c604352009-06-18 12:56:01 +02002384 switch (ctxt->mode) {
2385 case X86EMUL_MODE_PROT32:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002386 if ((msr_data & 0xfffc) == 0x0)
2387 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002388 break;
2389 case X86EMUL_MODE_PROT64:
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002390 if (msr_data == 0x0)
2391 return emulate_gp(ctxt, 0);
Andre Przywara8c604352009-06-18 12:56:01 +02002392 break;
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03002393 default:
2394 break;
Andre Przywara8c604352009-06-18 12:56:01 +02002395 }
2396
2397 ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002398 cs_sel = (u16)msr_data;
2399 cs_sel &= ~SELECTOR_RPL_MASK;
2400 ss_sel = cs_sel + 8;
2401 ss_sel &= ~SELECTOR_RPL_MASK;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03002402 if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
Gleb Natapov79168fd2010-04-28 19:15:30 +03002403 cs.d = 0;
Andre Przywara8c604352009-06-18 12:56:01 +02002404 cs.l = 1;
2405 }
2406
Avi Kivity1aa36612011-04-27 13:20:30 +03002407 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2408 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara8c604352009-06-18 12:56:01 +02002409
Avi Kivity717746e2011-04-20 13:37:53 +03002410 ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002411 ctxt->_eip = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002412
Avi Kivity717746e2011-04-20 13:37:53 +03002413 ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
Avi Kivitydd856ef2012-08-27 23:46:17 +03002414 *reg_write(ctxt, VCPU_REGS_RSP) = msr_data;
Andre Przywara8c604352009-06-18 12:56:01 +02002415
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002416 return X86EMUL_CONTINUE;
Andre Przywara8c604352009-06-18 12:56:01 +02002417}
2418
Takuya Yoshikawae01991e2011-05-29 21:55:10 +09002419static int em_sysexit(struct x86_emulate_ctxt *ctxt)
Andre Przywara4668f052009-06-18 12:56:02 +02002420{
Mathias Krause0225fb52012-08-30 01:30:16 +02002421 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002422 struct desc_struct cs, ss;
Andre Przywara4668f052009-06-18 12:56:02 +02002423 u64 msr_data;
2424 int usermode;
Xiao Guangrong1249b962011-05-15 23:25:10 +08002425 u16 cs_sel = 0, ss_sel = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002426
Gleb Natapova0044752010-02-10 14:21:31 +02002427 /* inject #GP if in real mode or Virtual 8086 mode */
2428 if (ctxt->mode == X86EMUL_MODE_REAL ||
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002429 ctxt->mode == X86EMUL_MODE_VM86)
2430 return emulate_gp(ctxt, 0);
Andre Przywara4668f052009-06-18 12:56:02 +02002431
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002432 setup_syscalls_segments(ctxt, &cs, &ss);
Andre Przywara4668f052009-06-18 12:56:02 +02002433
Avi Kivity9dac77f2011-06-01 15:34:25 +03002434 if ((ctxt->rex_prefix & 0x8) != 0x0)
Andre Przywara4668f052009-06-18 12:56:02 +02002435 usermode = X86EMUL_MODE_PROT64;
2436 else
2437 usermode = X86EMUL_MODE_PROT32;
2438
2439 cs.dpl = 3;
2440 ss.dpl = 3;
Avi Kivity717746e2011-04-20 13:37:53 +03002441 ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
Andre Przywara4668f052009-06-18 12:56:02 +02002442 switch (usermode) {
2443 case X86EMUL_MODE_PROT32:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002444 cs_sel = (u16)(msr_data + 16);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002445 if ((msr_data & 0xfffc) == 0x0)
2446 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002447 ss_sel = (u16)(msr_data + 24);
Andre Przywara4668f052009-06-18 12:56:02 +02002448 break;
2449 case X86EMUL_MODE_PROT64:
Gleb Natapov79168fd2010-04-28 19:15:30 +03002450 cs_sel = (u16)(msr_data + 32);
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002451 if (msr_data == 0x0)
2452 return emulate_gp(ctxt, 0);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002453 ss_sel = cs_sel + 8;
2454 cs.d = 0;
Andre Przywara4668f052009-06-18 12:56:02 +02002455 cs.l = 1;
2456 break;
2457 }
Gleb Natapov79168fd2010-04-28 19:15:30 +03002458 cs_sel |= SELECTOR_RPL_MASK;
2459 ss_sel |= SELECTOR_RPL_MASK;
Andre Przywara4668f052009-06-18 12:56:02 +02002460
Avi Kivity1aa36612011-04-27 13:20:30 +03002461 ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
2462 ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
Andre Przywara4668f052009-06-18 12:56:02 +02002463
Avi Kivitydd856ef2012-08-27 23:46:17 +03002464 ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX);
2465 *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX);
Andre Przywara4668f052009-06-18 12:56:02 +02002466
Takuya Yoshikawae54cfa92010-02-18 12:15:02 +02002467 return X86EMUL_CONTINUE;
Andre Przywara4668f052009-06-18 12:56:02 +02002468}
2469
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002470static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002471{
2472 int iopl;
2473 if (ctxt->mode == X86EMUL_MODE_REAL)
2474 return false;
2475 if (ctxt->mode == X86EMUL_MODE_VM86)
2476 return true;
2477 iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002478 return ctxt->ops->cpl(ctxt) > iopl;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002479}
2480
2481static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002482 u16 port, u16 len)
2483{
Mathias Krause0225fb52012-08-30 01:30:16 +02002484 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002485 struct desc_struct tr_seg;
Gleb Natapov5601d052011-03-07 14:55:06 +02002486 u32 base3;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002487 int r;
Avi Kivity1aa36612011-04-27 13:20:30 +03002488 u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002489 unsigned mask = (1 << len) - 1;
Gleb Natapov5601d052011-03-07 14:55:06 +02002490 unsigned long base;
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002491
Avi Kivity1aa36612011-04-27 13:20:30 +03002492 ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
Gleb Natapov79168fd2010-04-28 19:15:30 +03002493 if (!tr_seg.p)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002494 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002495 if (desc_limit_scaled(&tr_seg) < 103)
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002496 return false;
Gleb Natapov5601d052011-03-07 14:55:06 +02002497 base = get_desc_base(&tr_seg);
2498#ifdef CONFIG_X86_64
2499 base |= ((u64)base3) << 32;
2500#endif
Avi Kivity0f65dd72011-04-20 13:37:53 +03002501 r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002502 if (r != X86EMUL_CONTINUE)
2503 return false;
Gleb Natapov79168fd2010-04-28 19:15:30 +03002504 if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002505 return false;
Avi Kivity0f65dd72011-04-20 13:37:53 +03002506 r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002507 if (r != X86EMUL_CONTINUE)
2508 return false;
2509 if ((perm >> bit_idx) & mask)
2510 return false;
2511 return true;
2512}
2513
2514static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002515 u16 port, u16 len)
2516{
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002517 if (ctxt->perm_ok)
2518 return true;
2519
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002520 if (emulator_bad_iopl(ctxt))
2521 if (!emulator_io_port_access_allowed(ctxt, port, len))
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002522 return false;
Gleb Natapov4fc40f02010-08-02 12:47:51 +03002523
2524 ctxt->perm_ok = true;
2525
Gleb Natapovf850e2e2010-02-10 14:21:33 +02002526 return true;
2527}
2528
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002529static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002530 struct tss_segment_16 *tss)
2531{
Avi Kivity9dac77f2011-06-01 15:34:25 +03002532 tss->ip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002533 tss->flag = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002534 tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
2535 tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
2536 tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
2537 tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
2538 tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
2539 tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
2540 tss->si = reg_read(ctxt, VCPU_REGS_RSI);
2541 tss->di = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002542
Avi Kivity1aa36612011-04-27 13:20:30 +03002543 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2544 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2545 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2546 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2547 tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002548}
2549
2550static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002551 struct tss_segment_16 *tss)
2552{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002553 int ret;
2554
Avi Kivity9dac77f2011-06-01 15:34:25 +03002555 ctxt->_eip = tss->ip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002556 ctxt->eflags = tss->flag | 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002557 *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
2558 *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
2559 *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
2560 *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
2561 *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
2562 *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
2563 *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
2564 *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002565
2566 /*
2567 * SDM says that segment selectors are loaded before segment
2568 * descriptors
2569 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002570 set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
2571 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2572 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2573 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2574 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002575
2576 /*
Guo Chaofc058682012-06-28 15:19:51 +08002577 * Now load segment descriptors. If fault happens at this stage
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002578 * it is handled in a context of new task
2579 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002580 ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002581 if (ret != X86EMUL_CONTINUE)
2582 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002583 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002584 if (ret != X86EMUL_CONTINUE)
2585 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002586 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002587 if (ret != X86EMUL_CONTINUE)
2588 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002589 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002590 if (ret != X86EMUL_CONTINUE)
2591 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002592 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002593 if (ret != X86EMUL_CONTINUE)
2594 return ret;
2595
2596 return X86EMUL_CONTINUE;
2597}
2598
2599static int task_switch_16(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002600 u16 tss_selector, u16 old_tss_sel,
2601 ulong old_tss_base, struct desc_struct *new_desc)
2602{
Mathias Krause0225fb52012-08-30 01:30:16 +02002603 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002604 struct tss_segment_16 tss_seg;
2605 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002606 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002607
Avi Kivity0f65dd72011-04-20 13:37:53 +03002608 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002609 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002610 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002611 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002612 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002613
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002614 save_state_to_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002615
Avi Kivity0f65dd72011-04-20 13:37:53 +03002616 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002617 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002618 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002619 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002620 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002621
Avi Kivity0f65dd72011-04-20 13:37:53 +03002622 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002623 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002624 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002625 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002626 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002627
2628 if (old_tss_sel != 0xffff) {
2629 tss_seg.prev_task_link = old_tss_sel;
2630
Avi Kivity0f65dd72011-04-20 13:37:53 +03002631 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002632 &tss_seg.prev_task_link,
2633 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002634 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002635 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002636 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002637 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002638 }
2639
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002640 return load_state_from_tss16(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002641}
2642
2643static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002644 struct tss_segment_32 *tss)
2645{
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002646 tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002647 tss->eip = ctxt->_eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002648 tss->eflags = ctxt->eflags;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002649 tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
2650 tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
2651 tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
2652 tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
2653 tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
2654 tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
2655 tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
2656 tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002657
Avi Kivity1aa36612011-04-27 13:20:30 +03002658 tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
2659 tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
2660 tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
2661 tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
2662 tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
2663 tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
2664 tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002665}
2666
2667static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002668 struct tss_segment_32 *tss)
2669{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002670 int ret;
2671
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002672 if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
Avi Kivity35d3d4a2010-11-22 17:53:25 +02002673 return emulate_gp(ctxt, 0);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002674 ctxt->_eip = tss->eip;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002675 ctxt->eflags = tss->eflags | 2;
Kevin Wolf4cee4792012-02-08 14:34:41 +01002676
2677 /* General purpose registers */
Avi Kivitydd856ef2012-08-27 23:46:17 +03002678 *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
2679 *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
2680 *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
2681 *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
2682 *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
2683 *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
2684 *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
2685 *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002686
2687 /*
2688 * SDM says that segment selectors are loaded before segment
2689 * descriptors
2690 */
Avi Kivity1aa36612011-04-27 13:20:30 +03002691 set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
2692 set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
2693 set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
2694 set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
2695 set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
2696 set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
2697 set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002698
2699 /*
Kevin Wolf4cee4792012-02-08 14:34:41 +01002700 * If we're switching between Protected Mode and VM86, we need to make
2701 * sure to update the mode before loading the segment descriptors so
2702 * that the selectors are interpreted correctly.
2703 *
2704 * Need to get rflags to the vcpu struct immediately because it
2705 * influences the CPL which is checked at least when loading the segment
2706 * descriptors and when pushing an error code to the new kernel stack.
2707 *
2708 * TODO Introduce a separate ctxt->ops->set_cpl callback
2709 */
2710 if (ctxt->eflags & X86_EFLAGS_VM)
2711 ctxt->mode = X86EMUL_MODE_VM86;
2712 else
2713 ctxt->mode = X86EMUL_MODE_PROT32;
2714
2715 ctxt->ops->set_rflags(ctxt, ctxt->eflags);
2716
2717 /*
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002718 * Now load segment descriptors. If fault happenes at this stage
2719 * it is handled in a context of new task
2720 */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002721 ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002722 if (ret != X86EMUL_CONTINUE)
2723 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002724 ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002725 if (ret != X86EMUL_CONTINUE)
2726 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002727 ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002728 if (ret != X86EMUL_CONTINUE)
2729 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002730 ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002731 if (ret != X86EMUL_CONTINUE)
2732 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002733 ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002734 if (ret != X86EMUL_CONTINUE)
2735 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002736 ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002737 if (ret != X86EMUL_CONTINUE)
2738 return ret;
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002739 ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002740 if (ret != X86EMUL_CONTINUE)
2741 return ret;
2742
2743 return X86EMUL_CONTINUE;
2744}
2745
2746static int task_switch_32(struct x86_emulate_ctxt *ctxt,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002747 u16 tss_selector, u16 old_tss_sel,
2748 ulong old_tss_base, struct desc_struct *new_desc)
2749{
Mathias Krause0225fb52012-08-30 01:30:16 +02002750 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002751 struct tss_segment_32 tss_seg;
2752 int ret;
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002753 u32 new_tss_base = get_desc_base(new_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002754
Avi Kivity0f65dd72011-04-20 13:37:53 +03002755 ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002756 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002757 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002758 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002759 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002760
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002761 save_state_to_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002762
Avi Kivity0f65dd72011-04-20 13:37:53 +03002763 ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002764 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002765 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002766 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002767 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002768
Avi Kivity0f65dd72011-04-20 13:37:53 +03002769 ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
Avi Kivitybcc55cb2010-11-22 17:53:22 +02002770 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002771 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002772 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002773 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002774
2775 if (old_tss_sel != 0xffff) {
2776 tss_seg.prev_task_link = old_tss_sel;
2777
Avi Kivity0f65dd72011-04-20 13:37:53 +03002778 ret = ops->write_std(ctxt, new_tss_base,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002779 &tss_seg.prev_task_link,
2780 sizeof tss_seg.prev_task_link,
Avi Kivity0f65dd72011-04-20 13:37:53 +03002781 &ctxt->exception);
Avi Kivitydb297e32010-11-22 17:53:24 +02002782 if (ret != X86EMUL_CONTINUE)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002783 /* FIXME: need to provide precise fault address */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002784 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002785 }
2786
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002787 return load_state_from_tss32(ctxt, &tss_seg);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002788}
2789
2790static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002791 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002792 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002793{
Mathias Krause0225fb52012-08-30 01:30:16 +02002794 const struct x86_emulate_ops *ops = ctxt->ops;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002795 struct desc_struct curr_tss_desc, next_tss_desc;
2796 int ret;
Avi Kivity1aa36612011-04-27 13:20:30 +03002797 u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002798 ulong old_tss_base =
Avi Kivity4bff1e862011-04-20 13:37:53 +03002799 ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
Gleb Natapovceffb452010-03-18 15:20:19 +02002800 u32 desc_limit;
Avi Kivitye9194642012-06-13 16:29:39 +03002801 ulong desc_addr;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002802
2803 /* FIXME: old_tss_base == ~0 ? */
2804
Avi Kivitye9194642012-06-13 16:29:39 +03002805 ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002806 if (ret != X86EMUL_CONTINUE)
2807 return ret;
Avi Kivitye9194642012-06-13 16:29:39 +03002808 ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002809 if (ret != X86EMUL_CONTINUE)
2810 return ret;
2811
2812 /* FIXME: check that next_tss_desc is tss */
2813
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002814 /*
2815 * Check privileges. The three cases are task switch caused by...
2816 *
2817 * 1. jmp/call/int to task gate: Check against DPL of the task gate
2818 * 2. Exception/IRQ/iret: No check is performed
Guo Chaofc058682012-06-28 15:19:51 +08002819 * 3. jmp/call to TSS: Check against DPL of the TSS
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002820 */
2821 if (reason == TASK_SWITCH_GATE) {
2822 if (idt_index != -1) {
2823 /* Software interrupts */
2824 struct desc_struct task_gate_desc;
2825 int dpl;
2826
2827 ret = read_interrupt_descriptor(ctxt, idt_index,
2828 &task_gate_desc);
2829 if (ret != X86EMUL_CONTINUE)
2830 return ret;
2831
2832 dpl = task_gate_desc.dpl;
2833 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2834 return emulate_gp(ctxt, (idt_index << 3) | 0x2);
2835 }
2836 } else if (reason != TASK_SWITCH_IRET) {
2837 int dpl = next_tss_desc.dpl;
2838 if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
2839 return emulate_gp(ctxt, tss_selector);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002840 }
2841
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002842
Gleb Natapovceffb452010-03-18 15:20:19 +02002843 desc_limit = desc_limit_scaled(&next_tss_desc);
2844 if (!next_tss_desc.p ||
2845 ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
2846 desc_limit < 0x2b)) {
Gleb Natapov54b84862010-04-28 19:15:44 +03002847 emulate_ts(ctxt, tss_selector & 0xfffc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002848 return X86EMUL_PROPAGATE_FAULT;
2849 }
2850
2851 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
2852 curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002853 write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002854 }
2855
2856 if (reason == TASK_SWITCH_IRET)
2857 ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
2858
2859 /* set back link to prev task only if NT bit is set in eflags
Guo Chaofc058682012-06-28 15:19:51 +08002860 note that old_tss_sel is not used after this point */
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002861 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
2862 old_tss_sel = 0xffff;
2863
2864 if (next_tss_desc.type & 8)
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002865 ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002866 old_tss_base, &next_tss_desc);
2867 else
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002868 ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002869 old_tss_base, &next_tss_desc);
Jan Kiszka0760d442010-04-14 15:50:57 +02002870 if (ret != X86EMUL_CONTINUE)
2871 return ret;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002872
2873 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
2874 ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
2875
2876 if (reason != TASK_SWITCH_IRET) {
2877 next_tss_desc.type |= (1 << 1); /* set busy flag */
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09002878 write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002879 }
2880
Avi Kivity717746e2011-04-20 13:37:53 +03002881 ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
Avi Kivity1aa36612011-04-27 13:20:30 +03002882 ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002883
Jan Kiszkae269fb22010-04-14 15:51:09 +02002884 if (has_error_code) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002885 ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
2886 ctxt->lock_prefix = 0;
2887 ctxt->src.val = (unsigned long) error_code;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09002888 ret = em_push(ctxt);
Jan Kiszkae269fb22010-04-14 15:51:09 +02002889 }
2890
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002891 return ret;
2892}
2893
2894int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002895 u16 tss_selector, int idt_index, int reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002896 bool has_error_code, u32 error_code)
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002897{
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002898 int rc;
2899
Avi Kivitydd856ef2012-08-27 23:46:17 +03002900 invalidate_registers(ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002901 ctxt->_eip = ctxt->eip;
2902 ctxt->dst.type = OP_NONE;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002903
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002904 rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
Jan Kiszkae269fb22010-04-14 15:51:09 +02002905 has_error_code, error_code);
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002906
Avi Kivitydd856ef2012-08-27 23:46:17 +03002907 if (rc == X86EMUL_CONTINUE) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03002908 ctxt->eip = ctxt->_eip;
Avi Kivitydd856ef2012-08-27 23:46:17 +03002909 writeback_registers(ctxt);
2910 }
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002911
Gleb Natapova0c0ab22011-03-28 16:57:49 +02002912 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Gleb Natapov38ba30b2010-03-18 15:20:17 +02002913}
2914
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03002915static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
2916 struct operand *op)
Gleb Natapova682e352010-03-18 15:20:21 +02002917{
Gleb Natapovb3356bf2012-09-03 15:24:29 +03002918 int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count;
Gleb Natapova682e352010-03-18 15:20:21 +02002919
Avi Kivitydd856ef2012-08-27 23:46:17 +03002920 register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes);
2921 op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg));
Gleb Natapova682e352010-03-18 15:20:21 +02002922}
2923
Avi Kivity7af04fc2010-08-18 14:16:35 +03002924static int em_das(struct x86_emulate_ctxt *ctxt)
2925{
Avi Kivity7af04fc2010-08-18 14:16:35 +03002926 u8 al, old_al;
2927 bool af, cf, old_cf;
2928
2929 cf = ctxt->eflags & X86_EFLAGS_CF;
Avi Kivity9dac77f2011-06-01 15:34:25 +03002930 al = ctxt->dst.val;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002931
2932 old_al = al;
2933 old_cf = cf;
2934 cf = false;
2935 af = ctxt->eflags & X86_EFLAGS_AF;
2936 if ((al & 0x0f) > 9 || af) {
2937 al -= 6;
2938 cf = old_cf | (al >= 250);
2939 af = true;
2940 } else {
2941 af = false;
2942 }
2943 if (old_al > 0x99 || old_cf) {
2944 al -= 0x60;
2945 cf = true;
2946 }
2947
Avi Kivity9dac77f2011-06-01 15:34:25 +03002948 ctxt->dst.val = al;
Avi Kivity7af04fc2010-08-18 14:16:35 +03002949 /* Set PF, ZF, SF */
Avi Kivity9dac77f2011-06-01 15:34:25 +03002950 ctxt->src.type = OP_IMM;
2951 ctxt->src.val = 0;
2952 ctxt->src.bytes = 1;
Avi Kivitya31b9ce2011-09-07 16:41:35 +03002953 emulate_2op_SrcV(ctxt, "or");
Avi Kivity7af04fc2010-08-18 14:16:35 +03002954 ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
2955 if (cf)
2956 ctxt->eflags |= X86_EFLAGS_CF;
2957 if (af)
2958 ctxt->eflags |= X86_EFLAGS_AF;
2959 return X86EMUL_CONTINUE;
2960}
2961
Gleb Natapov7f662272012-12-10 11:42:30 +02002962static int em_aad(struct x86_emulate_ctxt *ctxt)
2963{
2964 u8 al = ctxt->dst.val & 0xff;
2965 u8 ah = (ctxt->dst.val >> 8) & 0xff;
2966
2967 al = (al + (ah * ctxt->src.val)) & 0xff;
2968
2969 ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
2970
2971 ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF);
2972
2973 if (!al)
2974 ctxt->eflags |= X86_EFLAGS_ZF;
2975 if (!(al & 1))
2976 ctxt->eflags |= X86_EFLAGS_PF;
2977 if (al & 0x80)
2978 ctxt->eflags |= X86_EFLAGS_SF;
2979
2980 return X86EMUL_CONTINUE;
2981}
2982
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09002983static int em_call(struct x86_emulate_ctxt *ctxt)
2984{
2985 long rel = ctxt->src.val;
2986
2987 ctxt->src.val = (unsigned long)ctxt->_eip;
2988 jmp_rel(ctxt, rel);
2989 return em_push(ctxt);
2990}
2991
Avi Kivity0ef753b2010-08-18 14:51:45 +03002992static int em_call_far(struct x86_emulate_ctxt *ctxt)
2993{
Avi Kivity0ef753b2010-08-18 14:51:45 +03002994 u16 sel, old_cs;
2995 ulong old_eip;
2996 int rc;
2997
Avi Kivity1aa36612011-04-27 13:20:30 +03002998 old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
Avi Kivity9dac77f2011-06-01 15:34:25 +03002999 old_eip = ctxt->_eip;
Avi Kivity0ef753b2010-08-18 14:51:45 +03003000
Avi Kivity9dac77f2011-06-01 15:34:25 +03003001 memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09003002 if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
Avi Kivity0ef753b2010-08-18 14:51:45 +03003003 return X86EMUL_CONTINUE;
3004
Avi Kivity9dac77f2011-06-01 15:34:25 +03003005 ctxt->_eip = 0;
3006 memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003007
Avi Kivity9dac77f2011-06-01 15:34:25 +03003008 ctxt->src.val = old_cs;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003009 rc = em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003010 if (rc != X86EMUL_CONTINUE)
3011 return rc;
3012
Avi Kivity9dac77f2011-06-01 15:34:25 +03003013 ctxt->src.val = old_eip;
Takuya Yoshikawa4487b3b2011-04-13 00:31:23 +09003014 return em_push(ctxt);
Avi Kivity0ef753b2010-08-18 14:51:45 +03003015}
3016
Avi Kivity40ece7c2010-08-18 15:12:09 +03003017static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
3018{
Avi Kivity40ece7c2010-08-18 15:12:09 +03003019 int rc;
3020
Avi Kivity9dac77f2011-06-01 15:34:25 +03003021 ctxt->dst.type = OP_REG;
3022 ctxt->dst.addr.reg = &ctxt->_eip;
3023 ctxt->dst.bytes = ctxt->op_bytes;
3024 rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003025 if (rc != X86EMUL_CONTINUE)
3026 return rc;
Avi Kivity5ad105e2012-08-19 14:34:31 +03003027 rsp_increment(ctxt, ctxt->src.val);
Avi Kivity40ece7c2010-08-18 15:12:09 +03003028 return X86EMUL_CONTINUE;
3029}
3030
Avi Kivityfb864fb2013-01-04 16:18:54 +02003031FASTOP2(add);
3032FASTOP2(or);
3033FASTOP2(adc);
3034FASTOP2(sbb);
3035FASTOP2(and);
3036FASTOP2(sub);
3037FASTOP2(xor);
3038FASTOP2(cmp);
3039FASTOP2(test);
Takuya Yoshikawa9f21ca52011-05-29 21:57:53 +09003040
Avi Kivity0bdea062013-01-19 19:51:50 +02003041FASTOP3WCL(shld);
3042FASTOP3WCL(shrd);
3043
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003044static int em_xchg(struct x86_emulate_ctxt *ctxt)
3045{
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003046 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003047 ctxt->src.val = ctxt->dst.val;
3048 write_register_operand(&ctxt->src);
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003049
3050 /* Write back the memory destination with implicit LOCK prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003051 ctxt->dst.val = ctxt->src.orig_val;
3052 ctxt->lock_prefix = 1;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09003053 return X86EMUL_CONTINUE;
3054}
3055
Avi Kivity5c82aa22010-08-18 18:31:43 +03003056static int em_imul(struct x86_emulate_ctxt *ctxt)
3057{
Avi Kivitya31b9ce2011-09-07 16:41:35 +03003058 emulate_2op_SrcV_nobyte(ctxt, "imul");
Avi Kivity5c82aa22010-08-18 18:31:43 +03003059 return X86EMUL_CONTINUE;
3060}
3061
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003062static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
3063{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003064 ctxt->dst.val = ctxt->src2.val;
Avi Kivity5c82aa22010-08-18 18:31:43 +03003065 return em_imul(ctxt);
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003066}
3067
Avi Kivity61429142010-08-19 15:13:00 +03003068static int em_cwd(struct x86_emulate_ctxt *ctxt)
3069{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003070 ctxt->dst.type = OP_REG;
3071 ctxt->dst.bytes = ctxt->src.bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03003072 ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivity9dac77f2011-06-01 15:34:25 +03003073 ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
Avi Kivity61429142010-08-19 15:13:00 +03003074
3075 return X86EMUL_CONTINUE;
3076}
3077
Avi Kivity48bb5d32010-08-18 18:54:34 +03003078static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
3079{
Avi Kivity48bb5d32010-08-18 18:54:34 +03003080 u64 tsc = 0;
3081
Avi Kivity717746e2011-04-20 13:37:53 +03003082 ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003083 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
3084 *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
Avi Kivity48bb5d32010-08-18 18:54:34 +03003085 return X86EMUL_CONTINUE;
3086}
3087
Avi Kivity222d21a2011-11-10 14:57:30 +02003088static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
3089{
3090 u64 pmc;
3091
Avi Kivitydd856ef2012-08-27 23:46:17 +03003092 if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
Avi Kivity222d21a2011-11-10 14:57:30 +02003093 return emulate_gp(ctxt, 0);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003094 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
3095 *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
Avi Kivity222d21a2011-11-10 14:57:30 +02003096 return X86EMUL_CONTINUE;
3097}
3098
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003099static int em_mov(struct x86_emulate_ctxt *ctxt)
3100{
Stefan Hajnoczi49597d82012-04-09 18:40:00 +03003101 memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003102 return X86EMUL_CONTINUE;
3103}
3104
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09003105static int em_cr_write(struct x86_emulate_ctxt *ctxt)
3106{
3107 if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3108 return emulate_gp(ctxt, 0);
3109
3110 /* Disable writeback. */
3111 ctxt->dst.type = OP_NONE;
3112 return X86EMUL_CONTINUE;
3113}
3114
3115static int em_dr_write(struct x86_emulate_ctxt *ctxt)
3116{
3117 unsigned long val;
3118
3119 if (ctxt->mode == X86EMUL_MODE_PROT64)
3120 val = ctxt->src.val & ~0ULL;
3121 else
3122 val = ctxt->src.val & ~0U;
3123
3124 /* #UD condition is already handled. */
3125 if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
3126 return emulate_gp(ctxt, 0);
3127
3128 /* Disable writeback. */
3129 ctxt->dst.type = OP_NONE;
3130 return X86EMUL_CONTINUE;
3131}
3132
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003133static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
3134{
3135 u64 msr_data;
3136
Avi Kivitydd856ef2012-08-27 23:46:17 +03003137 msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
3138 | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
3139 if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003140 return emulate_gp(ctxt, 0);
3141
3142 return X86EMUL_CONTINUE;
3143}
3144
3145static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
3146{
3147 u64 msr_data;
3148
Avi Kivitydd856ef2012-08-27 23:46:17 +03003149 if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003150 return emulate_gp(ctxt, 0);
3151
Avi Kivitydd856ef2012-08-27 23:46:17 +03003152 *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
3153 *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09003154 return X86EMUL_CONTINUE;
3155}
3156
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003157static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
3158{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003159 if (ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003160 return emulate_ud(ctxt);
3161
Avi Kivity9dac77f2011-06-01 15:34:25 +03003162 ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003163 return X86EMUL_CONTINUE;
3164}
3165
3166static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
3167{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003168 u16 sel = ctxt->src.val;
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003169
Avi Kivity9dac77f2011-06-01 15:34:25 +03003170 if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003171 return emulate_ud(ctxt);
3172
Avi Kivity9dac77f2011-06-01 15:34:25 +03003173 if (ctxt->modrm_reg == VCPU_SREG_SS)
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003174 ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
3175
3176 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003177 ctxt->dst.type = OP_NONE;
3178 return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003179}
3180
Avi Kivitya14e5792012-06-13 12:28:33 +03003181static int em_lldt(struct x86_emulate_ctxt *ctxt)
3182{
3183 u16 sel = ctxt->src.val;
3184
3185 /* Disable writeback. */
3186 ctxt->dst.type = OP_NONE;
3187 return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
3188}
3189
Avi Kivity80890002012-06-13 16:33:29 +03003190static int em_ltr(struct x86_emulate_ctxt *ctxt)
3191{
3192 u16 sel = ctxt->src.val;
3193
3194 /* Disable writeback. */
3195 ctxt->dst.type = OP_NONE;
3196 return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
3197}
3198
Avi Kivity38503912011-03-31 18:48:09 +02003199static int em_invlpg(struct x86_emulate_ctxt *ctxt)
3200{
Avi Kivity9fa088f2011-03-31 18:54:30 +02003201 int rc;
3202 ulong linear;
3203
Avi Kivity9dac77f2011-06-01 15:34:25 +03003204 rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
Avi Kivity9fa088f2011-03-31 18:54:30 +02003205 if (rc == X86EMUL_CONTINUE)
Avi Kivity3cb16fe2011-04-20 15:38:44 +03003206 ctxt->ops->invlpg(ctxt, linear);
Avi Kivity38503912011-03-31 18:48:09 +02003207 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003208 ctxt->dst.type = OP_NONE;
Avi Kivity38503912011-03-31 18:48:09 +02003209 return X86EMUL_CONTINUE;
3210}
3211
Avi Kivity2d04a052011-04-20 15:32:49 +03003212static int em_clts(struct x86_emulate_ctxt *ctxt)
3213{
3214 ulong cr0;
3215
3216 cr0 = ctxt->ops->get_cr(ctxt, 0);
3217 cr0 &= ~X86_CR0_TS;
3218 ctxt->ops->set_cr(ctxt, 0, cr0);
3219 return X86EMUL_CONTINUE;
3220}
3221
Avi Kivity26d05cc2011-04-21 12:07:59 +03003222static int em_vmcall(struct x86_emulate_ctxt *ctxt)
3223{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003224 int rc;
3225
Avi Kivity9dac77f2011-06-01 15:34:25 +03003226 if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003227 return X86EMUL_UNHANDLEABLE;
3228
3229 rc = ctxt->ops->fix_hypercall(ctxt);
3230 if (rc != X86EMUL_CONTINUE)
3231 return rc;
3232
3233 /* Let the processor re-execute the fixed hypercall */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003234 ctxt->_eip = ctxt->eip;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003235 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003236 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003237 return X86EMUL_CONTINUE;
3238}
3239
Avi Kivity96051572012-06-10 17:21:18 +03003240static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
3241 void (*get)(struct x86_emulate_ctxt *ctxt,
3242 struct desc_ptr *ptr))
3243{
3244 struct desc_ptr desc_ptr;
3245
3246 if (ctxt->mode == X86EMUL_MODE_PROT64)
3247 ctxt->op_bytes = 8;
3248 get(ctxt, &desc_ptr);
3249 if (ctxt->op_bytes == 2) {
3250 ctxt->op_bytes = 4;
3251 desc_ptr.address &= 0x00ffffff;
3252 }
3253 /* Disable writeback. */
3254 ctxt->dst.type = OP_NONE;
3255 return segmented_write(ctxt, ctxt->dst.addr.mem,
3256 &desc_ptr, 2 + ctxt->op_bytes);
3257}
3258
3259static int em_sgdt(struct x86_emulate_ctxt *ctxt)
3260{
3261 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
3262}
3263
3264static int em_sidt(struct x86_emulate_ctxt *ctxt)
3265{
3266 return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
3267}
3268
Avi Kivity26d05cc2011-04-21 12:07:59 +03003269static int em_lgdt(struct x86_emulate_ctxt *ctxt)
3270{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003271 struct desc_ptr desc_ptr;
3272 int rc;
3273
Avi Kivity510425f2012-06-07 17:04:36 +03003274 if (ctxt->mode == X86EMUL_MODE_PROT64)
3275 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003276 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Avi Kivity26d05cc2011-04-21 12:07:59 +03003277 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003278 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003279 if (rc != X86EMUL_CONTINUE)
3280 return rc;
3281 ctxt->ops->set_gdt(ctxt, &desc_ptr);
3282 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003283 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003284 return X86EMUL_CONTINUE;
3285}
3286
Avi Kivity5ef39c72011-04-21 12:21:50 +03003287static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
Avi Kivity26d05cc2011-04-21 12:07:59 +03003288{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003289 int rc;
3290
Avi Kivity5ef39c72011-04-21 12:21:50 +03003291 rc = ctxt->ops->fix_hypercall(ctxt);
3292
Avi Kivity26d05cc2011-04-21 12:07:59 +03003293 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003294 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003295 return rc;
3296}
3297
3298static int em_lidt(struct x86_emulate_ctxt *ctxt)
3299{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003300 struct desc_ptr desc_ptr;
3301 int rc;
3302
Avi Kivity510425f2012-06-07 17:04:36 +03003303 if (ctxt->mode == X86EMUL_MODE_PROT64)
3304 ctxt->op_bytes = 8;
Avi Kivity9dac77f2011-06-01 15:34:25 +03003305 rc = read_descriptor(ctxt, ctxt->src.addr.mem,
Takuya Yoshikawa509cf9f2011-05-02 02:25:07 +09003306 &desc_ptr.size, &desc_ptr.address,
Avi Kivity9dac77f2011-06-01 15:34:25 +03003307 ctxt->op_bytes);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003308 if (rc != X86EMUL_CONTINUE)
3309 return rc;
3310 ctxt->ops->set_idt(ctxt, &desc_ptr);
3311 /* Disable writeback. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03003312 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003313 return X86EMUL_CONTINUE;
3314}
3315
3316static int em_smsw(struct x86_emulate_ctxt *ctxt)
3317{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003318 ctxt->dst.bytes = 2;
3319 ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
Avi Kivity26d05cc2011-04-21 12:07:59 +03003320 return X86EMUL_CONTINUE;
3321}
3322
3323static int em_lmsw(struct x86_emulate_ctxt *ctxt)
3324{
Avi Kivity26d05cc2011-04-21 12:07:59 +03003325 ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003326 | (ctxt->src.val & 0x0f));
3327 ctxt->dst.type = OP_NONE;
Avi Kivity26d05cc2011-04-21 12:07:59 +03003328 return X86EMUL_CONTINUE;
3329}
3330
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003331static int em_loop(struct x86_emulate_ctxt *ctxt)
3332{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003333 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1);
3334 if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
Avi Kivity9dac77f2011-06-01 15:34:25 +03003335 (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
3336 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003337
3338 return X86EMUL_CONTINUE;
3339}
3340
3341static int em_jcxz(struct x86_emulate_ctxt *ctxt)
3342{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003343 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03003344 jmp_rel(ctxt, ctxt->src.val);
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003345
3346 return X86EMUL_CONTINUE;
3347}
3348
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003349static int em_in(struct x86_emulate_ctxt *ctxt)
3350{
3351 if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
3352 &ctxt->dst.val))
3353 return X86EMUL_IO_NEEDED;
3354
3355 return X86EMUL_CONTINUE;
3356}
3357
3358static int em_out(struct x86_emulate_ctxt *ctxt)
3359{
3360 ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
3361 &ctxt->src.val, 1);
3362 /* Disable writeback. */
3363 ctxt->dst.type = OP_NONE;
3364 return X86EMUL_CONTINUE;
3365}
3366
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003367static int em_cli(struct x86_emulate_ctxt *ctxt)
3368{
3369 if (emulator_bad_iopl(ctxt))
3370 return emulate_gp(ctxt, 0);
3371
3372 ctxt->eflags &= ~X86_EFLAGS_IF;
3373 return X86EMUL_CONTINUE;
3374}
3375
3376static int em_sti(struct x86_emulate_ctxt *ctxt)
3377{
3378 if (emulator_bad_iopl(ctxt))
3379 return emulate_gp(ctxt, 0);
3380
3381 ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
3382 ctxt->eflags |= X86_EFLAGS_IF;
3383 return X86EMUL_CONTINUE;
3384}
3385
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09003386static int em_bt(struct x86_emulate_ctxt *ctxt)
3387{
3388 /* Disable writeback. */
3389 ctxt->dst.type = OP_NONE;
3390 /* only subword offset */
3391 ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
3392
3393 emulate_2op_SrcV_nobyte(ctxt, "bt");
3394 return X86EMUL_CONTINUE;
3395}
3396
3397static int em_bts(struct x86_emulate_ctxt *ctxt)
3398{
3399 emulate_2op_SrcV_nobyte(ctxt, "bts");
3400 return X86EMUL_CONTINUE;
3401}
3402
3403static int em_btr(struct x86_emulate_ctxt *ctxt)
3404{
3405 emulate_2op_SrcV_nobyte(ctxt, "btr");
3406 return X86EMUL_CONTINUE;
3407}
3408
3409static int em_btc(struct x86_emulate_ctxt *ctxt)
3410{
3411 emulate_2op_SrcV_nobyte(ctxt, "btc");
3412 return X86EMUL_CONTINUE;
3413}
3414
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003415static int em_bsf(struct x86_emulate_ctxt *ctxt)
3416{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003417 emulate_2op_SrcV_nobyte(ctxt, "bsf");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003418 return X86EMUL_CONTINUE;
3419}
3420
3421static int em_bsr(struct x86_emulate_ctxt *ctxt)
3422{
Joerg Roedeld54e4232012-05-07 12:12:25 +02003423 emulate_2op_SrcV_nobyte(ctxt, "bsr");
Takuya Yoshikawaff227392011-11-22 15:21:33 +09003424 return X86EMUL_CONTINUE;
3425}
3426
Avi Kivity6d6eede2012-06-07 14:11:36 +03003427static int em_cpuid(struct x86_emulate_ctxt *ctxt)
3428{
3429 u32 eax, ebx, ecx, edx;
3430
Avi Kivitydd856ef2012-08-27 23:46:17 +03003431 eax = reg_read(ctxt, VCPU_REGS_RAX);
3432 ecx = reg_read(ctxt, VCPU_REGS_RCX);
Avi Kivity6d6eede2012-06-07 14:11:36 +03003433 ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003434 *reg_write(ctxt, VCPU_REGS_RAX) = eax;
3435 *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
3436 *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
3437 *reg_write(ctxt, VCPU_REGS_RDX) = edx;
Avi Kivity6d6eede2012-06-07 14:11:36 +03003438 return X86EMUL_CONTINUE;
3439}
3440
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003441static int em_lahf(struct x86_emulate_ctxt *ctxt)
3442{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003443 *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
3444 *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003445 return X86EMUL_CONTINUE;
3446}
3447
Avi Kivity92998362012-06-13 12:25:06 +03003448static int em_bswap(struct x86_emulate_ctxt *ctxt)
3449{
3450 switch (ctxt->op_bytes) {
3451#ifdef CONFIG_X86_64
3452 case 8:
3453 asm("bswap %0" : "+r"(ctxt->dst.val));
3454 break;
3455#endif
3456 default:
3457 asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
3458 break;
3459 }
3460 return X86EMUL_CONTINUE;
3461}
3462
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003463static bool valid_cr(int nr)
3464{
3465 switch (nr) {
3466 case 0:
3467 case 2 ... 4:
3468 case 8:
3469 return true;
3470 default:
3471 return false;
3472 }
3473}
3474
3475static int check_cr_read(struct x86_emulate_ctxt *ctxt)
3476{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003477 if (!valid_cr(ctxt->modrm_reg))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003478 return emulate_ud(ctxt);
3479
3480 return X86EMUL_CONTINUE;
3481}
3482
3483static int check_cr_write(struct x86_emulate_ctxt *ctxt)
3484{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003485 u64 new_val = ctxt->src.val64;
3486 int cr = ctxt->modrm_reg;
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003487 u64 efer = 0;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003488
3489 static u64 cr_reserved_bits[] = {
3490 0xffffffff00000000ULL,
3491 0, 0, 0, /* CR3 checked later */
3492 CR4_RESERVED_BITS,
3493 0, 0, 0,
3494 CR8_RESERVED_BITS,
3495 };
3496
3497 if (!valid_cr(cr))
3498 return emulate_ud(ctxt);
3499
3500 if (new_val & cr_reserved_bits[cr])
3501 return emulate_gp(ctxt, 0);
3502
3503 switch (cr) {
3504 case 0: {
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003505 u64 cr4;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003506 if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
3507 ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
3508 return emulate_gp(ctxt, 0);
3509
Avi Kivity717746e2011-04-20 13:37:53 +03003510 cr4 = ctxt->ops->get_cr(ctxt, 4);
3511 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003512
3513 if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
3514 !(cr4 & X86_CR4_PAE))
3515 return emulate_gp(ctxt, 0);
3516
3517 break;
3518 }
3519 case 3: {
3520 u64 rsvd = 0;
3521
Avi Kivityc2ad2bb2011-04-20 15:21:35 +03003522 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
3523 if (efer & EFER_LMA)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003524 rsvd = CR3_L_MODE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003525 else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003526 rsvd = CR3_PAE_RESERVED_BITS;
Avi Kivityfd72c412011-04-20 15:24:32 +03003527 else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003528 rsvd = CR3_NONPAE_RESERVED_BITS;
3529
3530 if (new_val & rsvd)
3531 return emulate_gp(ctxt, 0);
3532
3533 break;
3534 }
3535 case 4: {
Avi Kivity717746e2011-04-20 13:37:53 +03003536 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003537
3538 if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
3539 return emulate_gp(ctxt, 0);
3540
3541 break;
3542 }
3543 }
3544
3545 return X86EMUL_CONTINUE;
3546}
3547
Joerg Roedel3b88e412011-04-04 12:39:29 +02003548static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
3549{
3550 unsigned long dr7;
3551
Avi Kivity717746e2011-04-20 13:37:53 +03003552 ctxt->ops->get_dr(ctxt, 7, &dr7);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003553
3554 /* Check if DR7.Global_Enable is set */
3555 return dr7 & (1 << 13);
3556}
3557
3558static int check_dr_read(struct x86_emulate_ctxt *ctxt)
3559{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003560 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003561 u64 cr4;
3562
3563 if (dr > 7)
3564 return emulate_ud(ctxt);
3565
Avi Kivity717746e2011-04-20 13:37:53 +03003566 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedel3b88e412011-04-04 12:39:29 +02003567 if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
3568 return emulate_ud(ctxt);
3569
3570 if (check_dr7_gd(ctxt))
3571 return emulate_db(ctxt);
3572
3573 return X86EMUL_CONTINUE;
3574}
3575
3576static int check_dr_write(struct x86_emulate_ctxt *ctxt)
3577{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003578 u64 new_val = ctxt->src.val64;
3579 int dr = ctxt->modrm_reg;
Joerg Roedel3b88e412011-04-04 12:39:29 +02003580
3581 if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
3582 return emulate_gp(ctxt, 0);
3583
3584 return check_dr_read(ctxt);
3585}
3586
Joerg Roedel01de8b02011-04-04 12:39:31 +02003587static int check_svme(struct x86_emulate_ctxt *ctxt)
3588{
3589 u64 efer;
3590
Avi Kivity717746e2011-04-20 13:37:53 +03003591 ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003592
3593 if (!(efer & EFER_SVME))
3594 return emulate_ud(ctxt);
3595
3596 return X86EMUL_CONTINUE;
3597}
3598
3599static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
3600{
Avi Kivitydd856ef2012-08-27 23:46:17 +03003601 u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
Joerg Roedel01de8b02011-04-04 12:39:31 +02003602
3603 /* Valid physical address? */
Randy Dunlapd4224442011-04-21 09:09:22 -07003604 if (rax & 0xffff000000000000ULL)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003605 return emulate_gp(ctxt, 0);
3606
3607 return check_svme(ctxt);
3608}
3609
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003610static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
3611{
Avi Kivity717746e2011-04-20 13:37:53 +03003612 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003613
Avi Kivity717746e2011-04-20 13:37:53 +03003614 if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003615 return emulate_ud(ctxt);
3616
3617 return X86EMUL_CONTINUE;
3618}
3619
Joerg Roedel80612522011-04-04 12:39:33 +02003620static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
3621{
Avi Kivity717746e2011-04-20 13:37:53 +03003622 u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
Avi Kivitydd856ef2012-08-27 23:46:17 +03003623 u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
Joerg Roedel80612522011-04-04 12:39:33 +02003624
Avi Kivity717746e2011-04-20 13:37:53 +03003625 if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
Joerg Roedel80612522011-04-04 12:39:33 +02003626 (rcx > 3))
3627 return emulate_gp(ctxt, 0);
3628
3629 return X86EMUL_CONTINUE;
3630}
3631
Joerg Roedelf6511932011-04-04 12:39:35 +02003632static int check_perm_in(struct x86_emulate_ctxt *ctxt)
3633{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003634 ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
3635 if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003636 return emulate_gp(ctxt, 0);
3637
3638 return X86EMUL_CONTINUE;
3639}
3640
3641static int check_perm_out(struct x86_emulate_ctxt *ctxt)
3642{
Avi Kivity9dac77f2011-06-01 15:34:25 +03003643 ctxt->src.bytes = min(ctxt->src.bytes, 4u);
3644 if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
Joerg Roedelf6511932011-04-04 12:39:35 +02003645 return emulate_gp(ctxt, 0);
3646
3647 return X86EMUL_CONTINUE;
3648}
3649
Avi Kivity73fba5f2010-07-29 15:11:53 +03003650#define D(_y) { .flags = (_y) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003651#define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003652#define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
3653 .check_perm = (_p) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003654#define N D(0)
Joerg Roedel01de8b02011-04-04 12:39:31 +02003655#define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003656#define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
3657#define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
Gleb Natapov045a2822012-12-20 16:57:43 +02003658#define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003659#define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
Avi Kivitye28bbd42013-01-04 16:18:48 +02003660#define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
Avi Kivityc4f035c2011-04-04 12:39:22 +02003661#define II(_f, _e, _i) \
3662 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
Joerg Roedeld09beab2011-04-04 12:39:25 +02003663#define IIP(_f, _e, _i, _p) \
3664 { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
3665 .check_perm = (_p) }
Avi Kivityaa97bb42010-01-20 18:09:23 +02003666#define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
Avi Kivity73fba5f2010-07-29 15:11:53 +03003667
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003668#define D2bv(_f) D((_f) | ByteOp), D(_f)
Joerg Roedelf6511932011-04-04 12:39:35 +02003669#define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003670#define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
Avi Kivityf7857f32013-01-04 16:18:53 +02003671#define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003672#define I2bvIP(_f, _e, _i, _p) \
3673 IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
Avi Kivity8d8f4e92010-08-26 11:56:06 +03003674
Avi Kivityfb864fb2013-01-04 16:18:54 +02003675#define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
3676 F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
3677 F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
Avi Kivity6230f7f2010-08-26 18:34:55 +03003678
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003679static const struct opcode group7_rm1[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003680 DI(SrcNone | Priv, monitor),
3681 DI(SrcNone | Priv, mwait),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003682 N, N, N, N, N, N,
3683};
3684
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003685static const struct opcode group7_rm3[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003686 DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
3687 II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall),
3688 DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
3689 DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
3690 DIP(SrcNone | Prot | Priv, stgi, check_svme),
3691 DIP(SrcNone | Prot | Priv, clgi, check_svme),
3692 DIP(SrcNone | Prot | Priv, skinit, check_svme),
3693 DIP(SrcNone | Prot | Priv, invlpga, check_svme),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003694};
Avi Kivity6230f7f2010-08-26 18:34:55 +03003695
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003696static const struct opcode group7_rm7[] = {
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003697 N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003698 DIP(SrcNone, rdtscp, check_rdtsc),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003699 N, N, N, N, N, N,
3700};
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09003701
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003702static const struct opcode group1[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003703 F(Lock, em_add),
3704 F(Lock | PageTable, em_or),
3705 F(Lock, em_adc),
3706 F(Lock, em_sbb),
3707 F(Lock | PageTable, em_and),
3708 F(Lock, em_sub),
3709 F(Lock, em_xor),
3710 F(NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003711};
3712
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003713static const struct opcode group1A[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003714 I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003715};
3716
Avi Kivity007a3b52013-01-19 19:51:51 +02003717static const struct opcode group2[] = {
3718 F(DstMem | ModRM, em_rol),
3719 F(DstMem | ModRM, em_ror),
3720 F(DstMem | ModRM, em_rcl),
3721 F(DstMem | ModRM, em_rcr),
3722 F(DstMem | ModRM, em_shl),
3723 F(DstMem | ModRM, em_shr),
3724 F(DstMem | ModRM, em_shl),
3725 F(DstMem | ModRM, em_sar),
3726};
3727
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003728static const struct opcode group3[] = {
Avi Kivityfb864fb2013-01-04 16:18:54 +02003729 F(DstMem | SrcImm | NoWrite, em_test),
3730 F(DstMem | SrcImm | NoWrite, em_test),
Avi Kivity45a14672013-01-04 16:18:52 +02003731 F(DstMem | SrcNone | Lock, em_not),
3732 F(DstMem | SrcNone | Lock, em_neg),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003733 I(SrcMem, em_mul_ex),
3734 I(SrcMem, em_imul_ex),
3735 I(SrcMem, em_div_ex),
3736 I(SrcMem, em_idiv_ex),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003737};
3738
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003739static const struct opcode group4[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003740 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
3741 I(ByteOp | DstMem | SrcNone | Lock, em_grp45),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003742 N, N, N, N, N, N,
3743};
3744
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003745static const struct opcode group5[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003746 I(DstMem | SrcNone | Lock, em_grp45),
3747 I(DstMem | SrcNone | Lock, em_grp45),
3748 I(SrcMem | Stack, em_grp45),
3749 I(SrcMemFAddr | ImplicitOps | Stack, em_call_far),
3750 I(SrcMem | Stack, em_grp45),
3751 I(SrcMemFAddr | ImplicitOps, em_grp45),
3752 I(SrcMem | Stack, em_grp45), N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003753};
3754
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003755static const struct opcode group6[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003756 DI(Prot, sldt),
3757 DI(Prot, str),
Avi Kivitya14e5792012-06-13 12:28:33 +03003758 II(Prot | Priv | SrcMem16, em_lldt, lldt),
Avi Kivity80890002012-06-13 16:33:29 +03003759 II(Prot | Priv | SrcMem16, em_ltr, ltr),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003760 N, N, N, N,
3761};
3762
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003763static const struct group_dual group7 = { {
Avi Kivity96051572012-06-10 17:21:18 +03003764 II(Mov | DstMem | Priv, em_sgdt, sgdt),
3765 II(Mov | DstMem | Priv, em_sidt, sidt),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003766 II(SrcMem | Priv, em_lgdt, lgdt),
3767 II(SrcMem | Priv, em_lidt, lidt),
3768 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3769 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3770 II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003771}, {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003772 I(SrcNone | Priv | VendorSpecific, em_vmcall),
Avi Kivity5ef39c72011-04-21 12:21:50 +03003773 EXT(0, group7_rm1),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003774 N, EXT(0, group7_rm3),
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003775 II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
3776 II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
3777 EXT(0, group7_rm7),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003778} };
3779
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003780static const struct opcode group8[] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003781 N, N, N, N,
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003782 I(DstMem | SrcImmByte, em_bt),
3783 I(DstMem | SrcImmByte | Lock | PageTable, em_bts),
3784 I(DstMem | SrcImmByte | Lock, em_btr),
3785 I(DstMem | SrcImmByte | Lock | PageTable, em_btc),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003786};
3787
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003788static const struct group_dual group9 = { {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003789 N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003790}, {
3791 N, N, N, N, N, N, N, N,
3792} };
3793
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003794static const struct opcode group11[] = {
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003795 I(DstMem | SrcImm | Mov | PageTable, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003796 X7(D(Undefined)),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003797};
3798
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003799static const struct gprefix pfx_0f_6f_0f_7f = {
Avi Kivitye5971752012-04-09 18:40:03 +03003800 I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
Avi Kivityaa97bb42010-01-20 18:09:23 +02003801};
3802
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003803static const struct gprefix pfx_vmovntpx = {
Avi Kivity3e114eb2012-04-09 18:40:01 +03003804 I(0, em_mov), N, N, N,
3805};
3806
Gleb Natapov045a2822012-12-20 16:57:43 +02003807static const struct escape escape_d9 = { {
3808 N, N, N, N, N, N, N, I(DstMem, em_fnstcw),
3809}, {
3810 /* 0xC0 - 0xC7 */
3811 N, N, N, N, N, N, N, N,
3812 /* 0xC8 - 0xCF */
3813 N, N, N, N, N, N, N, N,
3814 /* 0xD0 - 0xC7 */
3815 N, N, N, N, N, N, N, N,
3816 /* 0xD8 - 0xDF */
3817 N, N, N, N, N, N, N, N,
3818 /* 0xE0 - 0xE7 */
3819 N, N, N, N, N, N, N, N,
3820 /* 0xE8 - 0xEF */
3821 N, N, N, N, N, N, N, N,
3822 /* 0xF0 - 0xF7 */
3823 N, N, N, N, N, N, N, N,
3824 /* 0xF8 - 0xFF */
3825 N, N, N, N, N, N, N, N,
3826} };
3827
3828static const struct escape escape_db = { {
3829 N, N, N, N, N, N, N, N,
3830}, {
3831 /* 0xC0 - 0xC7 */
3832 N, N, N, N, N, N, N, N,
3833 /* 0xC8 - 0xCF */
3834 N, N, N, N, N, N, N, N,
3835 /* 0xD0 - 0xC7 */
3836 N, N, N, N, N, N, N, N,
3837 /* 0xD8 - 0xDF */
3838 N, N, N, N, N, N, N, N,
3839 /* 0xE0 - 0xE7 */
3840 N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
3841 /* 0xE8 - 0xEF */
3842 N, N, N, N, N, N, N, N,
3843 /* 0xF0 - 0xF7 */
3844 N, N, N, N, N, N, N, N,
3845 /* 0xF8 - 0xFF */
3846 N, N, N, N, N, N, N, N,
3847} };
3848
3849static const struct escape escape_dd = { {
3850 N, N, N, N, N, N, N, I(DstMem, em_fnstsw),
3851}, {
3852 /* 0xC0 - 0xC7 */
3853 N, N, N, N, N, N, N, N,
3854 /* 0xC8 - 0xCF */
3855 N, N, N, N, N, N, N, N,
3856 /* 0xD0 - 0xC7 */
3857 N, N, N, N, N, N, N, N,
3858 /* 0xD8 - 0xDF */
3859 N, N, N, N, N, N, N, N,
3860 /* 0xE0 - 0xE7 */
3861 N, N, N, N, N, N, N, N,
3862 /* 0xE8 - 0xEF */
3863 N, N, N, N, N, N, N, N,
3864 /* 0xF0 - 0xF7 */
3865 N, N, N, N, N, N, N, N,
3866 /* 0xF8 - 0xFF */
3867 N, N, N, N, N, N, N, N,
3868} };
3869
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003870static const struct opcode opcode_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003871 /* 0x00 - 0x07 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003872 F6ALU(Lock, em_add),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003873 I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
3874 I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003875 /* 0x08 - 0x0F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003876 F6ALU(Lock | PageTable, em_or),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003877 I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
3878 N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003879 /* 0x10 - 0x17 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003880 F6ALU(Lock, em_adc),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003881 I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
3882 I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003883 /* 0x18 - 0x1F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003884 F6ALU(Lock, em_sbb),
Avi Kivity1cd196e2011-09-13 10:45:51 +03003885 I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
3886 I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003887 /* 0x20 - 0x27 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003888 F6ALU(Lock | PageTable, em_and), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003889 /* 0x28 - 0x2F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003890 F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003891 /* 0x30 - 0x37 */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003892 F6ALU(Lock, em_xor), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003893 /* 0x38 - 0x3F */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003894 F6ALU(NoWrite, em_cmp), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003895 /* 0x40 - 0x4F */
3896 X16(D(DstReg)),
3897 /* 0x50 - 0x57 */
Avi Kivity63540382010-07-29 15:11:55 +03003898 X8(I(SrcReg | Stack, em_push)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003899 /* 0x58 - 0x5F */
Takuya Yoshikawac54fe502011-04-23 18:49:40 +09003900 X8(I(DstReg | Stack, em_pop)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003901 /* 0x60 - 0x67 */
Takuya Yoshikawab96a7fa2011-04-23 18:51:07 +09003902 I(ImplicitOps | Stack | No64, em_pusha),
3903 I(ImplicitOps | Stack | No64, em_popa),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003904 N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
3905 N, N, N, N,
3906 /* 0x68 - 0x6F */
Avi Kivityd46164d2010-08-18 19:29:33 +03003907 I(SrcImm | Mov | Stack, em_push),
3908 I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
Avi Kivityf3a1b9f2010-08-18 18:25:25 +03003909 I(SrcImmByte | Mov | Stack, em_push),
3910 I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
Gleb Natapovb3356bf2012-09-03 15:24:29 +03003911 I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
Takuya Yoshikawa2b5e97e2011-11-23 12:27:39 +09003912 I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
Avi Kivity73fba5f2010-07-29 15:11:53 +03003913 /* 0x70 - 0x7F */
3914 X16(D(SrcImmByte)),
3915 /* 0x80 - 0x87 */
Takuya Yoshikawa1c2545b2012-04-30 17:46:31 +09003916 G(ByteOp | DstMem | SrcImm, group1),
3917 G(DstMem | SrcImm, group1),
3918 G(ByteOp | DstMem | SrcImm | No64, group1),
3919 G(DstMem | SrcImmByte, group1),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003920 F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003921 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003922 /* 0x88 - 0x8F */
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003923 I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003924 I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003925 I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
Takuya Yoshikawa1bd5f462011-05-29 22:01:33 +09003926 D(ModRM | SrcMem | NoAccess | DstReg),
3927 I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
3928 G(0, group1A),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003929 /* 0x90 - 0x97 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003930 DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003931 /* 0x98 - 0x9F */
Avi Kivity61429142010-08-19 15:13:00 +03003932 D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
Wei Yongjuncc4feed2010-08-25 14:10:53 +08003933 I(SrcImmFAddr | No64, em_call_far), N,
Takuya Yoshikawa62aaa2f2011-04-23 18:52:56 +09003934 II(ImplicitOps | Stack, em_pushf, pushf),
Avi Kivity2dd7caa2012-06-11 13:09:07 +03003935 II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003936 /* 0xA0 - 0xA7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003937 I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08003938 I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003939 I2bv(SrcSI | DstDI | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003940 F2bv(SrcSI | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003941 /* 0xA8 - 0xAF */
Avi Kivityfb864fb2013-01-04 16:18:54 +02003942 F2bv(DstAcc | SrcImm | NoWrite, em_test),
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003943 I2bv(SrcAcc | DstDI | Mov | String, em_mov),
3944 I2bv(SrcSI | DstAcc | Mov | String, em_mov),
Avi Kivityfb864fb2013-01-04 16:18:54 +02003945 F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003946 /* 0xB0 - 0xB7 */
Avi Kivityb9eac5f2010-08-03 14:46:56 +03003947 X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003948 /* 0xB8 - 0xBF */
Nadav Amit5e2c6882012-12-06 21:55:10 -02003949 X8(I(DstReg | SrcImm64 | Mov, em_mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003950 /* 0xC0 - 0xC7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003951 G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
Avi Kivity40ece7c2010-08-18 15:12:09 +03003952 I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
Takuya Yoshikawaebda02c2011-05-29 22:00:22 +09003953 I(ImplicitOps | Stack, em_ret),
Avi Kivityd4b43252011-09-13 10:45:50 +03003954 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
3955 I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
Avi Kivitya4d4a7c2010-08-03 15:05:46 +03003956 G(ByteOp, group11), G(0, group11),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003957 /* 0xC8 - 0xCF */
Avi Kivity612e89f2012-06-12 20:03:23 +03003958 I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
3959 N, I(ImplicitOps | Stack, em_ret_far),
Avi Kivity3c6e2762011-04-04 12:39:23 +02003960 D(ImplicitOps), DI(SrcImmByte, intn),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003961 D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003962 /* 0xD0 - 0xD7 */
Avi Kivity007a3b52013-01-19 19:51:51 +02003963 G(Src2One | ByteOp, group2), G(Src2One, group2),
3964 G(Src2CL | ByteOp, group2), G(Src2CL, group2),
Gleb Natapov7f662272012-12-10 11:42:30 +02003965 N, I(DstAcc | SrcImmByte | No64, em_aad), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003966 /* 0xD8 - 0xDF */
Gleb Natapov045a2822012-12-20 16:57:43 +02003967 N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003968 /* 0xE0 - 0xE7 */
Takuya Yoshikawad06e03a2011-05-29 22:04:08 +09003969 X3(I(SrcImmByte, em_loop)),
3970 I(SrcImmByte, em_jcxz),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003971 I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
3972 I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003973 /* 0xE8 - 0xEF */
Takuya Yoshikawad4ddafc2011-11-22 15:18:35 +09003974 I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003975 I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09003976 I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
3977 I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003978 /* 0xF0 - 0xF7 */
Joerg Roedelbf608f82011-04-04 12:39:34 +02003979 N, DI(ImplicitOps, icebp), N, N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003980 DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
3981 G(ByteOp, group3), G(0, group3),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003982 /* 0xF8 - 0xFF */
Takuya Yoshikawaf411e6c2011-05-29 22:05:15 +09003983 D(ImplicitOps), D(ImplicitOps),
3984 I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
Avi Kivity73fba5f2010-07-29 15:11:53 +03003985 D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
3986};
3987
Mathias Krausefd0a0d82012-08-30 01:30:15 +02003988static const struct opcode twobyte_table[256] = {
Avi Kivity73fba5f2010-07-29 15:11:53 +03003989 /* 0x00 - 0x0F */
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003990 G(0, group6), GD(0, &group7), N, N,
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09003991 N, I(ImplicitOps | VendorSpecific, em_syscall),
3992 II(ImplicitOps | Priv, em_clts, clts), N,
Avi Kivity3c6e2762011-04-04 12:39:23 +02003993 DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03003994 N, D(ImplicitOps | ModRM), N, N,
3995 /* 0x10 - 0x1F */
3996 N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
3997 /* 0x20 - 0x2F */
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003998 DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003999 DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
Takuya Yoshikawabc00f8d2011-11-22 15:19:19 +09004000 IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
4001 IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004002 N, N, N, N,
Avi Kivity3e114eb2012-04-09 18:40:01 +03004003 N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
4004 N, N, N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004005 /* 0x30 - 0x3F */
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004006 II(ImplicitOps | Priv, em_wrmsr, wrmsr),
Joerg Roedel80612522011-04-04 12:39:33 +02004007 IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Takuya Yoshikawae1e210b2011-11-22 15:20:03 +09004008 II(ImplicitOps | Priv, em_rdmsr, rdmsr),
Avi Kivity222d21a2011-11-10 14:57:30 +02004009 IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004010 I(ImplicitOps | VendorSpecific, em_sysenter),
4011 I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
Avi Kivityd8671622011-02-01 16:32:03 +02004012 N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004013 N, N, N, N, N, N, N, N,
4014 /* 0x40 - 0x4F */
4015 X16(D(DstReg | SrcMem | ModRM | Mov)),
4016 /* 0x50 - 0x5F */
4017 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4018 /* 0x60 - 0x6F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004019 N, N, N, N,
4020 N, N, N, N,
4021 N, N, N, N,
4022 N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004023 /* 0x70 - 0x7F */
Avi Kivityaa97bb42010-01-20 18:09:23 +02004024 N, N, N, N,
4025 N, N, N, N,
4026 N, N, N, N,
4027 N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004028 /* 0x80 - 0x8F */
4029 X16(D(SrcImm)),
4030 /* 0x90 - 0x9F */
Wei Yongjunee45b582010-08-06 17:10:07 +08004031 X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004032 /* 0xA0 - 0xA7 */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004033 I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
Avi Kivity6d6eede2012-06-07 14:11:36 +03004034 II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt),
Avi Kivity0bdea062013-01-19 19:51:50 +02004035 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
4036 F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
Avi Kivity73fba5f2010-07-29 15:11:53 +03004037 /* 0xA8 - 0xAF */
Avi Kivity1cd196e2011-09-13 10:45:51 +03004038 I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
Xiao Guangrongd5ae7ce2011-09-22 16:53:46 +08004039 DI(ImplicitOps, rsm),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004040 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
Avi Kivity0bdea062013-01-19 19:51:50 +02004041 F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
4042 F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
Avi Kivity5c82aa22010-08-18 18:31:43 +03004043 D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004044 /* 0xB0 - 0xB7 */
Takuya Yoshikawae940b5c2011-11-22 15:20:47 +09004045 I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg),
Avi Kivityd4b43252011-09-13 10:45:50 +03004046 I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004047 I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
Avi Kivityd4b43252011-09-13 10:45:50 +03004048 I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
4049 I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004050 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004051 /* 0xB8 - 0xBF */
4052 N, N,
Takuya Yoshikawace7faab2011-11-22 15:17:48 +09004053 G(BitOp, group8),
4054 I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
Takuya Yoshikawaff227392011-11-22 15:21:33 +09004055 I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr),
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004056 D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
Avi Kivity92998362012-06-13 12:25:06 +03004057 /* 0xC0 - 0xC7 */
Avi Kivity739ae402010-08-26 11:56:13 +03004058 D2bv(DstMem | SrcReg | ModRM | Lock),
Wei Yongjun92f738a2010-08-17 09:19:34 +08004059 N, D(DstMem | SrcReg | ModRM | Mov),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004060 N, N, N, GD(0, &group9),
Avi Kivity92998362012-06-13 12:25:06 +03004061 /* 0xC8 - 0xCF */
4062 X8(I(DstReg, em_bswap)),
Avi Kivity73fba5f2010-07-29 15:11:53 +03004063 /* 0xD0 - 0xDF */
4064 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4065 /* 0xE0 - 0xEF */
4066 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
4067 /* 0xF0 - 0xFF */
4068 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
4069};
4070
4071#undef D
4072#undef N
4073#undef G
4074#undef GD
4075#undef I
Avi Kivityaa97bb42010-01-20 18:09:23 +02004076#undef GP
Joerg Roedel01de8b02011-04-04 12:39:31 +02004077#undef EXT
Avi Kivity73fba5f2010-07-29 15:11:53 +03004078
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004079#undef D2bv
Joerg Roedelf6511932011-04-04 12:39:35 +02004080#undef D2bvIP
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004081#undef I2bv
Takuya Yoshikawad7841a42011-11-22 15:16:54 +09004082#undef I2bvIP
Takuya Yoshikawad67fc272011-04-23 18:48:02 +09004083#undef I6ALU
Avi Kivity8d8f4e92010-08-26 11:56:06 +03004084
Avi Kivity9dac77f2011-06-01 15:34:25 +03004085static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
Avi Kivity39f21ee2010-08-18 19:20:21 +03004086{
4087 unsigned size;
4088
Avi Kivity9dac77f2011-06-01 15:34:25 +03004089 size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004090 if (size == 8)
4091 size = 4;
4092 return size;
4093}
4094
4095static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
4096 unsigned size, bool sign_extension)
4097{
Avi Kivity39f21ee2010-08-18 19:20:21 +03004098 int rc = X86EMUL_CONTINUE;
4099
4100 op->type = OP_IMM;
4101 op->bytes = size;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004102 op->addr.mem.ea = ctxt->_eip;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004103 /* NB. Immediates are sign-extended as necessary. */
4104 switch (op->bytes) {
4105 case 1:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004106 op->val = insn_fetch(s8, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004107 break;
4108 case 2:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004109 op->val = insn_fetch(s16, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004110 break;
4111 case 4:
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004112 op->val = insn_fetch(s32, ctxt);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004113 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004114 case 8:
4115 op->val = insn_fetch(s64, ctxt);
4116 break;
Avi Kivity39f21ee2010-08-18 19:20:21 +03004117 }
4118 if (!sign_extension) {
4119 switch (op->bytes) {
4120 case 1:
4121 op->val &= 0xff;
4122 break;
4123 case 2:
4124 op->val &= 0xffff;
4125 break;
4126 case 4:
4127 op->val &= 0xffffffff;
4128 break;
4129 }
4130 }
4131done:
4132 return rc;
4133}
4134
Avi Kivitya9945542011-09-13 10:45:41 +03004135static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
4136 unsigned d)
4137{
4138 int rc = X86EMUL_CONTINUE;
4139
4140 switch (d) {
4141 case OpReg:
Avi Kivity2adb5ad2012-01-16 15:08:45 +02004142 decode_register_operand(ctxt, op);
Avi Kivitya9945542011-09-13 10:45:41 +03004143 break;
4144 case OpImmUByte:
Avi Kivity608aabe2011-09-13 10:45:45 +03004145 rc = decode_imm(ctxt, op, 1, false);
Avi Kivitya9945542011-09-13 10:45:41 +03004146 break;
4147 case OpMem:
Avi Kivity41ddf972011-09-13 10:45:48 +03004148 ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivity0fe59122011-09-13 10:45:47 +03004149 mem_common:
Avi Kivitya9945542011-09-13 10:45:41 +03004150 *op = ctxt->memop;
4151 ctxt->memopp = op;
Avi Kivity0fe59122011-09-13 10:45:47 +03004152 if ((ctxt->d & BitOp) && op == &ctxt->dst)
Avi Kivitya9945542011-09-13 10:45:41 +03004153 fetch_bit_operand(ctxt);
4154 op->orig_val = op->val;
4155 break;
Avi Kivity41ddf972011-09-13 10:45:48 +03004156 case OpMem64:
4157 ctxt->memop.bytes = 8;
4158 goto mem_common;
Avi Kivitya9945542011-09-13 10:45:41 +03004159 case OpAcc:
4160 op->type = OP_REG;
4161 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004162 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
Avi Kivitya9945542011-09-13 10:45:41 +03004163 fetch_register_operand(op);
4164 op->orig_val = op->val;
4165 break;
4166 case OpDI:
4167 op->type = OP_MEM;
4168 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4169 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004170 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI));
Avi Kivitya9945542011-09-13 10:45:41 +03004171 op->addr.mem.seg = VCPU_SREG_ES;
4172 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004173 op->count = 1;
Avi Kivitya9945542011-09-13 10:45:41 +03004174 break;
4175 case OpDX:
4176 op->type = OP_REG;
4177 op->bytes = 2;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004178 op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
Avi Kivitya9945542011-09-13 10:45:41 +03004179 fetch_register_operand(op);
4180 break;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004181 case OpCL:
4182 op->bytes = 1;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004183 op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
Avi Kivity4dd6a572011-09-13 10:45:43 +03004184 break;
4185 case OpImmByte:
4186 rc = decode_imm(ctxt, op, 1, true);
4187 break;
4188 case OpOne:
4189 op->bytes = 1;
4190 op->val = 1;
4191 break;
4192 case OpImm:
4193 rc = decode_imm(ctxt, op, imm_size(ctxt), true);
4194 break;
Nadav Amit5e2c6882012-12-06 21:55:10 -02004195 case OpImm64:
4196 rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
4197 break;
Avi Kivity28867ce2012-01-16 15:08:44 +02004198 case OpMem8:
4199 ctxt->memop.bytes = 1;
4200 goto mem_common;
Avi Kivity0fe59122011-09-13 10:45:47 +03004201 case OpMem16:
4202 ctxt->memop.bytes = 2;
4203 goto mem_common;
4204 case OpMem32:
4205 ctxt->memop.bytes = 4;
4206 goto mem_common;
4207 case OpImmU16:
4208 rc = decode_imm(ctxt, op, 2, false);
4209 break;
4210 case OpImmU:
4211 rc = decode_imm(ctxt, op, imm_size(ctxt), false);
4212 break;
4213 case OpSI:
4214 op->type = OP_MEM;
4215 op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
4216 op->addr.mem.ea =
Avi Kivitydd856ef2012-08-27 23:46:17 +03004217 register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI));
Avi Kivity0fe59122011-09-13 10:45:47 +03004218 op->addr.mem.seg = seg_override(ctxt);
4219 op->val = 0;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004220 op->count = 1;
Avi Kivity0fe59122011-09-13 10:45:47 +03004221 break;
4222 case OpImmFAddr:
4223 op->type = OP_IMM;
4224 op->addr.mem.ea = ctxt->_eip;
4225 op->bytes = ctxt->op_bytes + 2;
4226 insn_fetch_arr(op->valptr, op->bytes, ctxt);
4227 break;
4228 case OpMemFAddr:
4229 ctxt->memop.bytes = ctxt->op_bytes + 2;
4230 goto mem_common;
Avi Kivityc191a7a2011-09-13 10:45:49 +03004231 case OpES:
4232 op->val = VCPU_SREG_ES;
4233 break;
4234 case OpCS:
4235 op->val = VCPU_SREG_CS;
4236 break;
4237 case OpSS:
4238 op->val = VCPU_SREG_SS;
4239 break;
4240 case OpDS:
4241 op->val = VCPU_SREG_DS;
4242 break;
4243 case OpFS:
4244 op->val = VCPU_SREG_FS;
4245 break;
4246 case OpGS:
4247 op->val = VCPU_SREG_GS;
4248 break;
Avi Kivitya9945542011-09-13 10:45:41 +03004249 case OpImplicit:
4250 /* Special instructions do their own operand decoding. */
4251 default:
4252 op->type = OP_NONE; /* Disable writeback. */
4253 break;
4254 }
4255
4256done:
4257 return rc;
4258}
4259
Takuya Yoshikawaef5d75c2011-05-15 00:57:43 +09004260int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004261{
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004262 int rc = X86EMUL_CONTINUE;
4263 int mode = ctxt->mode;
Avi Kivity46561642011-04-24 14:09:59 +03004264 int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004265 bool op_prefix = false;
Avi Kivity46561642011-04-24 14:09:59 +03004266 struct opcode opcode;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004267
Avi Kivityf09ed832011-09-13 10:45:40 +03004268 ctxt->memop.type = OP_NONE;
4269 ctxt->memopp = NULL;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004270 ctxt->_eip = ctxt->eip;
4271 ctxt->fetch.start = ctxt->_eip;
4272 ctxt->fetch.end = ctxt->fetch.start + insn_len;
Andre Przywaradc25e892010-12-21 11:12:07 +01004273 if (insn_len > 0)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004274 memcpy(ctxt->fetch.data, insn, insn_len);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004275
4276 switch (mode) {
4277 case X86EMUL_MODE_REAL:
4278 case X86EMUL_MODE_VM86:
4279 case X86EMUL_MODE_PROT16:
4280 def_op_bytes = def_ad_bytes = 2;
4281 break;
4282 case X86EMUL_MODE_PROT32:
4283 def_op_bytes = def_ad_bytes = 4;
4284 break;
4285#ifdef CONFIG_X86_64
4286 case X86EMUL_MODE_PROT64:
4287 def_op_bytes = 4;
4288 def_ad_bytes = 8;
4289 break;
4290#endif
4291 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004292 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004293 }
4294
Avi Kivity9dac77f2011-06-01 15:34:25 +03004295 ctxt->op_bytes = def_op_bytes;
4296 ctxt->ad_bytes = def_ad_bytes;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004297
4298 /* Legacy prefixes. */
4299 for (;;) {
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004300 switch (ctxt->b = insn_fetch(u8, ctxt)) {
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004301 case 0x66: /* operand-size override */
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004302 op_prefix = true;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004303 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004304 ctxt->op_bytes = def_op_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004305 break;
4306 case 0x67: /* address-size override */
4307 if (mode == X86EMUL_MODE_PROT64)
4308 /* switch between 4/8 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004309 ctxt->ad_bytes = def_ad_bytes ^ 12;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004310 else
4311 /* switch between 2/4 bytes */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004312 ctxt->ad_bytes = def_ad_bytes ^ 6;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004313 break;
4314 case 0x26: /* ES override */
4315 case 0x2e: /* CS override */
4316 case 0x36: /* SS override */
4317 case 0x3e: /* DS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004318 set_seg_override(ctxt, (ctxt->b >> 3) & 3);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004319 break;
4320 case 0x64: /* FS override */
4321 case 0x65: /* GS override */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004322 set_seg_override(ctxt, ctxt->b & 7);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004323 break;
4324 case 0x40 ... 0x4f: /* REX */
4325 if (mode != X86EMUL_MODE_PROT64)
4326 goto done_prefixes;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004327 ctxt->rex_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004328 continue;
4329 case 0xf0: /* LOCK */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004330 ctxt->lock_prefix = 1;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004331 break;
4332 case 0xf2: /* REPNE/REPNZ */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004333 case 0xf3: /* REP/REPE/REPZ */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004334 ctxt->rep_prefix = ctxt->b;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004335 break;
4336 default:
4337 goto done_prefixes;
4338 }
4339
4340 /* Any legacy prefix after a REX prefix nullifies its effect. */
4341
Avi Kivity9dac77f2011-06-01 15:34:25 +03004342 ctxt->rex_prefix = 0;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004343 }
4344
4345done_prefixes:
4346
4347 /* REX prefix. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004348 if (ctxt->rex_prefix & 8)
4349 ctxt->op_bytes = 8; /* REX.W */
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004350
4351 /* Opcode byte(s). */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004352 opcode = opcode_table[ctxt->b];
Wei Yongjund3ad6242010-08-05 16:34:39 +08004353 /* Two-byte opcode? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004354 if (ctxt->b == 0x0f) {
4355 ctxt->twobyte = 1;
Takuya Yoshikawae85a1082011-07-30 18:01:26 +09004356 ctxt->b = insn_fetch(u8, ctxt);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004357 opcode = twobyte_table[ctxt->b];
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004358 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004359 ctxt->d = opcode.flags;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004360
Takuya Yoshikawa9f4260e2012-04-30 17:48:25 +09004361 if (ctxt->d & ModRM)
4362 ctxt->modrm = insn_fetch(u8, ctxt);
4363
Avi Kivity9dac77f2011-06-01 15:34:25 +03004364 while (ctxt->d & GroupMask) {
4365 switch (ctxt->d & GroupMask) {
Avi Kivity46561642011-04-24 14:09:59 +03004366 case Group:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004367 goffset = (ctxt->modrm >> 3) & 7;
Avi Kivity46561642011-04-24 14:09:59 +03004368 opcode = opcode.u.group[goffset];
4369 break;
4370 case GroupDual:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004371 goffset = (ctxt->modrm >> 3) & 7;
4372 if ((ctxt->modrm >> 6) == 3)
Avi Kivity46561642011-04-24 14:09:59 +03004373 opcode = opcode.u.gdual->mod3[goffset];
4374 else
4375 opcode = opcode.u.gdual->mod012[goffset];
4376 break;
4377 case RMExt:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004378 goffset = ctxt->modrm & 7;
Joerg Roedel01de8b02011-04-04 12:39:31 +02004379 opcode = opcode.u.group[goffset];
Avi Kivity46561642011-04-24 14:09:59 +03004380 break;
4381 case Prefix:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004382 if (ctxt->rep_prefix && op_prefix)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004383 return EMULATION_FAILED;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004384 simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
Avi Kivity46561642011-04-24 14:09:59 +03004385 switch (simd_prefix) {
4386 case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
4387 case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
4388 case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
4389 case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
4390 }
4391 break;
Gleb Natapov045a2822012-12-20 16:57:43 +02004392 case Escape:
4393 if (ctxt->modrm > 0xbf)
4394 opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
4395 else
4396 opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
4397 break;
Avi Kivity46561642011-04-24 14:09:59 +03004398 default:
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004399 return EMULATION_FAILED;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004400 }
Avi Kivity46561642011-04-24 14:09:59 +03004401
Avi Kivityb1ea50b2011-09-13 10:45:42 +03004402 ctxt->d &= ~(u64)GroupMask;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004403 ctxt->d |= opcode.flags;
Avi Kivity0d7cdee2011-03-29 11:34:38 +02004404 }
4405
Avi Kivity9dac77f2011-06-01 15:34:25 +03004406 ctxt->execute = opcode.u.execute;
4407 ctxt->check_perm = opcode.check_perm;
4408 ctxt->intercept = opcode.intercept;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004409
4410 /* Unrecognised? */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004411 if (ctxt->d == 0 || (ctxt->d & Undefined))
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004412 return EMULATION_FAILED;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004413
Avi Kivity9dac77f2011-06-01 15:34:25 +03004414 if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004415 return EMULATION_FAILED;
Avi Kivityd8671622011-02-01 16:32:03 +02004416
Avi Kivity9dac77f2011-06-01 15:34:25 +03004417 if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack))
4418 ctxt->op_bytes = 8;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004419
Avi Kivity9dac77f2011-06-01 15:34:25 +03004420 if (ctxt->d & Op3264) {
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004421 if (mode == X86EMUL_MODE_PROT64)
Avi Kivity9dac77f2011-06-01 15:34:25 +03004422 ctxt->op_bytes = 8;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004423 else
Avi Kivity9dac77f2011-06-01 15:34:25 +03004424 ctxt->op_bytes = 4;
Avi Kivity7f9b4b72010-08-01 14:46:54 +03004425 }
4426
Avi Kivity9dac77f2011-06-01 15:34:25 +03004427 if (ctxt->d & Sse)
4428 ctxt->op_bytes = 16;
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004429 else if (ctxt->d & Mmx)
4430 ctxt->op_bytes = 8;
Avi Kivity12537912011-03-29 11:41:27 +02004431
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004432 /* ModRM and SIB bytes. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004433 if (ctxt->d & ModRM) {
Avi Kivityf09ed832011-09-13 10:45:40 +03004434 rc = decode_modrm(ctxt, &ctxt->memop);
Avi Kivity9dac77f2011-06-01 15:34:25 +03004435 if (!ctxt->has_seg_override)
4436 set_seg_override(ctxt, ctxt->modrm_seg);
4437 } else if (ctxt->d & MemAbs)
Avi Kivityf09ed832011-09-13 10:45:40 +03004438 rc = decode_abs(ctxt, &ctxt->memop);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004439 if (rc != X86EMUL_CONTINUE)
4440 goto done;
4441
Avi Kivity9dac77f2011-06-01 15:34:25 +03004442 if (!ctxt->has_seg_override)
4443 set_seg_override(ctxt, VCPU_SREG_DS);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004444
Avi Kivityf09ed832011-09-13 10:45:40 +03004445 ctxt->memop.addr.mem.seg = seg_override(ctxt);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004446
Avi Kivityf09ed832011-09-13 10:45:40 +03004447 if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8)
4448 ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004449
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004450 /*
4451 * Decode and fetch the source operand: register, memory
4452 * or immediate.
4453 */
Avi Kivity0fe59122011-09-13 10:45:47 +03004454 rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004455 if (rc != X86EMUL_CONTINUE)
4456 goto done;
4457
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004458 /*
4459 * Decode and fetch the second source operand: register, memory
4460 * or immediate.
4461 */
Avi Kivity4dd6a572011-09-13 10:45:43 +03004462 rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
Avi Kivity39f21ee2010-08-18 19:20:21 +03004463 if (rc != X86EMUL_CONTINUE)
4464 goto done;
4465
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004466 /* Decode and fetch the destination operand: register or memory. */
Avi Kivitya9945542011-09-13 10:45:41 +03004467 rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004468
4469done:
Avi Kivityf09ed832011-09-13 10:45:40 +03004470 if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative)
4471 ctxt->memopp->addr.mem.ea += ctxt->_eip;
Avi Kivitycb16c342011-06-19 19:21:11 +03004472
Takuya Yoshikawa1d2887e2011-07-30 18:03:34 +09004473 return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivitydde7e6d122010-07-29 15:11:52 +03004474}
4475
Xiao Guangrong1cb3f3a2011-09-22 17:02:48 +08004476bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
4477{
4478 return ctxt->d & PageTable;
4479}
4480
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004481static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
4482{
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004483 /* The second termination condition only applies for REPE
4484 * and REPNE. Test if the repeat string operation prefix is
4485 * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
4486 * corresponding termination condition according to:
4487 * - if REPE/REPZ and ZF = 0 then done
4488 * - if REPNE/REPNZ and ZF = 1 then done
4489 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004490 if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
4491 (ctxt->b == 0xae) || (ctxt->b == 0xaf))
4492 && (((ctxt->rep_prefix == REPE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004493 ((ctxt->eflags & EFLG_ZF) == 0))
Avi Kivity9dac77f2011-06-01 15:34:25 +03004494 || ((ctxt->rep_prefix == REPNE_PREFIX) &&
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004495 ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
4496 return true;
4497
4498 return false;
4499}
4500
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004501static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
4502{
4503 bool fault = false;
4504
4505 ctxt->ops->get_fpu(ctxt);
4506 asm volatile("1: fwait \n\t"
4507 "2: \n\t"
4508 ".pushsection .fixup,\"ax\" \n\t"
4509 "3: \n\t"
4510 "movb $1, %[fault] \n\t"
4511 "jmp 2b \n\t"
4512 ".popsection \n\t"
4513 _ASM_EXTABLE(1b, 3b)
Avi Kivity38e8a2d2012-04-22 15:12:50 +03004514 : [fault]"+qm"(fault));
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004515 ctxt->ops->put_fpu(ctxt);
4516
4517 if (unlikely(fault))
4518 return emulate_exception(ctxt, MF_VECTOR, 0, false);
4519
4520 return X86EMUL_CONTINUE;
4521}
4522
4523static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
4524 struct operand *op)
4525{
4526 if (op->type == OP_MM)
4527 read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
4528}
4529
Avi Kivitye28bbd42013-01-04 16:18:48 +02004530static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
4531{
4532 ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
4533 fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
4534 asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n"
4535 : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags)
4536 : "c"(ctxt->src2.val), [fastop]"S"(fop));
4537 ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
4538 return X86EMUL_CONTINUE;
4539}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004540
Takuya Yoshikawa7b105ca2011-05-15 01:00:52 +09004541int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004542{
Mathias Krause0225fb52012-08-30 01:30:16 +02004543 const struct x86_emulate_ops *ops = ctxt->ops;
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004544 int rc = X86EMUL_CONTINUE;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004545 int saved_dst_type = ctxt->dst.type;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004546
Avi Kivity9dac77f2011-06-01 15:34:25 +03004547 ctxt->mem_read.pos = 0;
Glauber Costa310b5d32009-05-12 16:21:06 -04004548
Avi Kivity9dac77f2011-06-01 15:34:25 +03004549 if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004550 rc = emulate_ud(ctxt);
Gleb Natapov11616242010-02-11 14:43:14 +02004551 goto done;
4552 }
4553
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004554 /* LOCK prefix is allowed only with some instructions */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004555 if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004556 rc = emulate_ud(ctxt);
Gleb Natapovd380a5e2010-02-10 14:21:36 +02004557 goto done;
4558 }
4559
Avi Kivity9dac77f2011-06-01 15:34:25 +03004560 if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004561 rc = emulate_ud(ctxt);
Avi Kivity081bca02010-08-26 11:06:15 +03004562 goto done;
4563 }
4564
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004565 if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
4566 || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
Avi Kivity12537912011-03-29 11:41:27 +02004567 rc = emulate_ud(ctxt);
4568 goto done;
4569 }
4570
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004571 if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
Avi Kivity12537912011-03-29 11:41:27 +02004572 rc = emulate_nm(ctxt);
4573 goto done;
4574 }
4575
Avi Kivitycbe2c9d2012-04-09 18:40:02 +03004576 if (ctxt->d & Mmx) {
4577 rc = flush_pending_x87_faults(ctxt);
4578 if (rc != X86EMUL_CONTINUE)
4579 goto done;
4580 /*
4581 * Now that we know the fpu is exception safe, we can fetch
4582 * operands from it.
4583 */
4584 fetch_possible_mmx_operand(ctxt, &ctxt->src);
4585 fetch_possible_mmx_operand(ctxt, &ctxt->src2);
4586 if (!(ctxt->d & Mov))
4587 fetch_possible_mmx_operand(ctxt, &ctxt->dst);
4588 }
4589
Avi Kivity9dac77f2011-06-01 15:34:25 +03004590 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4591 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004592 X86_ICPT_PRE_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004593 if (rc != X86EMUL_CONTINUE)
4594 goto done;
4595 }
4596
Gleb Natapove92805a2010-02-10 14:21:35 +02004597 /* Privileged instruction can be executed only in CPL=0 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004598 if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
Avi Kivity35d3d4a2010-11-22 17:53:25 +02004599 rc = emulate_gp(ctxt, 0);
Gleb Natapove92805a2010-02-10 14:21:35 +02004600 goto done;
4601 }
4602
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004603 /* Instruction can only be executed in protected mode */
Gleb Natapov9d1b39a2012-09-03 15:24:27 +03004604 if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
Joerg Roedel8ea7d6a2011-04-04 12:39:26 +02004605 rc = emulate_ud(ctxt);
4606 goto done;
4607 }
4608
Joerg Roedeld09beab2011-04-04 12:39:25 +02004609 /* Do instruction specific permission checks */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004610 if (ctxt->check_perm) {
4611 rc = ctxt->check_perm(ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +02004612 if (rc != X86EMUL_CONTINUE)
4613 goto done;
4614 }
4615
Avi Kivity9dac77f2011-06-01 15:34:25 +03004616 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4617 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004618 X86_ICPT_POST_EXCEPT);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004619 if (rc != X86EMUL_CONTINUE)
4620 goto done;
4621 }
4622
Avi Kivity9dac77f2011-06-01 15:34:25 +03004623 if (ctxt->rep_prefix && (ctxt->d & String)) {
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004624 /* All REP prefixes have the same first termination condition */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004625 if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
Avi Kivity9dac77f2011-06-01 15:34:25 +03004626 ctxt->eip = ctxt->_eip;
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004627 goto done;
4628 }
Avi Kivityb9fa9d62007-11-27 19:05:37 +02004629 }
4630
Avi Kivity9dac77f2011-06-01 15:34:25 +03004631 if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
4632 rc = segmented_read(ctxt, ctxt->src.addr.mem,
4633 ctxt->src.valptr, ctxt->src.bytes);
Takuya Yoshikawab60d5132010-01-20 16:47:21 +09004634 if (rc != X86EMUL_CONTINUE)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004635 goto done;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004636 ctxt->src.orig_val64 = ctxt->src.val64;
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004637 }
4638
Avi Kivity9dac77f2011-06-01 15:34:25 +03004639 if (ctxt->src2.type == OP_MEM) {
4640 rc = segmented_read(ctxt, ctxt->src2.addr.mem,
4641 &ctxt->src2.val, ctxt->src2.bytes);
Gleb Natapove35b7b92010-02-25 16:36:42 +02004642 if (rc != X86EMUL_CONTINUE)
4643 goto done;
4644 }
4645
Avi Kivity9dac77f2011-06-01 15:34:25 +03004646 if ((ctxt->d & DstMask) == ImplicitOps)
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004647 goto special_insn;
4648
4649
Avi Kivity9dac77f2011-06-01 15:34:25 +03004650 if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004651 /* optimisation - avoid slow emulated read if Mov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004652 rc = segmented_read(ctxt, ctxt->dst.addr.mem,
4653 &ctxt->dst.val, ctxt->dst.bytes);
Gleb Natapov69f55cb2010-03-18 15:20:20 +02004654 if (rc != X86EMUL_CONTINUE)
4655 goto done;
Avi Kivity038e51d2007-01-22 20:40:40 -08004656 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004657 ctxt->dst.orig_val = ctxt->dst.val;
Avi Kivity038e51d2007-01-22 20:40:40 -08004658
Avi Kivity018a98d2007-11-27 19:30:56 +02004659special_insn:
4660
Avi Kivity9dac77f2011-06-01 15:34:25 +03004661 if (unlikely(ctxt->guest_mode) && ctxt->intercept) {
4662 rc = emulator_check_intercept(ctxt, ctxt->intercept,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004663 X86_ICPT_POST_MEMACCESS);
Avi Kivityc4f035c2011-04-04 12:39:22 +02004664 if (rc != X86EMUL_CONTINUE)
4665 goto done;
4666 }
4667
Avi Kivity9dac77f2011-06-01 15:34:25 +03004668 if (ctxt->execute) {
Avi Kivitye28bbd42013-01-04 16:18:48 +02004669 if (ctxt->d & Fastop) {
4670 void (*fop)(struct fastop *) = (void *)ctxt->execute;
4671 rc = fastop(ctxt, fop);
4672 if (rc != X86EMUL_CONTINUE)
4673 goto done;
4674 goto writeback;
4675 }
Avi Kivity9dac77f2011-06-01 15:34:25 +03004676 rc = ctxt->execute(ctxt);
Avi Kivityef65c882010-07-29 15:11:51 +03004677 if (rc != X86EMUL_CONTINUE)
4678 goto done;
4679 goto writeback;
4680 }
4681
Avi Kivity9dac77f2011-06-01 15:34:25 +03004682 if (ctxt->twobyte)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683 goto twobyte_insn;
4684
Avi Kivity9dac77f2011-06-01 15:34:25 +03004685 switch (ctxt->b) {
Avi Kivity33615aa2007-10-31 11:15:56 +02004686 case 0x40 ... 0x47: /* inc r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004687 emulate_1op(ctxt, "inc");
Avi Kivity33615aa2007-10-31 11:15:56 +02004688 break;
4689 case 0x48 ... 0x4f: /* dec r16/r32 */
Avi Kivityd1eef452011-09-07 16:41:38 +03004690 emulate_1op(ctxt, "dec");
Avi Kivity33615aa2007-10-31 11:15:56 +02004691 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692 case 0x63: /* movsxd */
Laurent Vivier8b4caf62007-09-18 11:27:19 +02004693 if (ctxt->mode != X86EMUL_MODE_PROT64)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694 goto cannot_emulate;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004695 ctxt->dst.val = (s32) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004697 case 0x70 ... 0x7f: /* jcc (short) */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004698 if (test_cc(ctxt->b, ctxt->eflags))
4699 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004700 break;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004701 case 0x8d: /* lea r16/r32, m */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004702 ctxt->dst.val = ctxt->src.addr.mem.ea;
Nitin A Kamble7e0b54b2007-09-15 10:35:36 +03004703 break;
Avi Kivity3d9e77d2010-08-01 12:41:59 +03004704 case 0x90 ... 0x97: /* nop / xchg reg, rax */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004705 if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
Mohammed Gamal34698d82010-08-04 14:41:04 +03004706 break;
Takuya Yoshikawae4f973a2011-05-29 21:59:09 +09004707 rc = em_xchg(ctxt);
4708 break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004709 case 0x98: /* cbw/cwde/cdqe */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004710 switch (ctxt->op_bytes) {
4711 case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
4712 case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
4713 case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
Wei Yongjune8b6fa72010-08-18 16:43:13 +08004714 }
4715 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004716 case 0xcc: /* int3 */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004717 rc = emulate_int(ctxt, 3);
4718 break;
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004719 case 0xcd: /* int n */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004720 rc = emulate_int(ctxt, ctxt->src.val);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004721 break;
4722 case 0xce: /* into */
Takuya Yoshikawa5c5df762011-05-29 22:02:55 +09004723 if (ctxt->eflags & EFLG_OF)
4724 rc = emulate_int(ctxt, 4);
Mohammed Gamal6e154e52010-08-04 14:38:06 +03004725 break;
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004726 case 0xe9: /* jmp rel */
Takuya Yoshikawadb5b0762011-05-29 21:56:26 +09004727 case 0xeb: /* jmp rel short */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004728 jmp_rel(ctxt, ctxt->src.val);
4729 ctxt->dst.type = OP_NONE; /* Disable writeback. */
Nitin A Kamble1a52e052007-09-18 16:34:25 -07004730 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004731 case 0xf4: /* hlt */
Avi Kivity6c3287f2011-04-20 15:43:05 +03004732 ctxt->ops->halt(ctxt);
Mohammed Gamal19fdfa02008-07-06 16:51:26 +03004733 break;
Avi Kivity111de5d2007-11-27 19:14:21 +02004734 case 0xf5: /* cmc */
4735 /* complement carry flag from eflags reg */
4736 ctxt->eflags ^= EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004737 break;
4738 case 0xf8: /* clc */
4739 ctxt->eflags &= ~EFLG_CF;
Avi Kivity111de5d2007-11-27 19:14:21 +02004740 break;
Mohammed Gamal8744aa92010-08-05 15:42:49 +03004741 case 0xf9: /* stc */
4742 ctxt->eflags |= EFLG_CF;
4743 break;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004744 case 0xfc: /* cld */
4745 ctxt->eflags &= ~EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004746 break;
4747 case 0xfd: /* std */
4748 ctxt->eflags |= EFLG_DF;
Mohammed Gamalfb4616f2008-09-01 04:52:24 +03004749 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004750 default:
4751 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752 }
Avi Kivity018a98d2007-11-27 19:30:56 +02004753
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004754 if (rc != X86EMUL_CONTINUE)
4755 goto done;
4756
Avi Kivity018a98d2007-11-27 19:30:56 +02004757writeback:
Takuya Yoshikawaadddcec2011-05-02 02:26:23 +09004758 rc = writeback(ctxt);
Takuya Yoshikawa1b30eaa2010-02-12 15:57:56 +09004759 if (rc != X86EMUL_CONTINUE)
Avi Kivity018a98d2007-11-27 19:30:56 +02004760 goto done;
4761
Gleb Natapov5cd21912010-03-18 15:20:26 +02004762 /*
4763 * restore dst type in case the decoding will be reused
4764 * (happens for string instruction )
4765 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004766 ctxt->dst.type = saved_dst_type;
Gleb Natapov5cd21912010-03-18 15:20:26 +02004767
Avi Kivity9dac77f2011-06-01 15:34:25 +03004768 if ((ctxt->d & SrcMask) == SrcSI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004769 string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
Gleb Natapova682e352010-03-18 15:20:21 +02004770
Avi Kivity9dac77f2011-06-01 15:34:25 +03004771 if ((ctxt->d & DstMask) == DstDI)
Gleb Natapovf3bd64c2012-09-03 15:24:28 +03004772 string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
Gleb Natapovd9271122010-03-18 15:20:22 +02004773
Avi Kivity9dac77f2011-06-01 15:34:25 +03004774 if (ctxt->rep_prefix && (ctxt->d & String)) {
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004775 unsigned int count;
Avi Kivity9dac77f2011-06-01 15:34:25 +03004776 struct read_cache *r = &ctxt->io_read;
Gleb Natapovb3356bf2012-09-03 15:24:29 +03004777 if ((ctxt->d & SrcMask) == SrcSI)
4778 count = ctxt->src.count;
4779 else
4780 count = ctxt->dst.count;
4781 register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX),
4782 -count);
Gleb Natapov3e2f65d2010-08-25 12:47:42 +03004783
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004784 if (!string_insn_completed(ctxt)) {
4785 /*
4786 * Re-enter guest when pio read ahead buffer is empty
4787 * or, if it is not used, after each 1024 iteration.
4788 */
Avi Kivitydd856ef2012-08-27 23:46:17 +03004789 if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004790 (r->end == 0 || r->end != r->pos)) {
4791 /*
4792 * Reset read cache. Usually happens before
4793 * decode, but since instruction is restarted
4794 * we have to do it here.
4795 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004796 ctxt->mem_read.end = 0;
Avi Kivitydd856ef2012-08-27 23:46:17 +03004797 writeback_registers(ctxt);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004798 return EMULATION_RESTART;
4799 }
4800 goto done; /* skip rip writeback */
Avi Kivity0fa6ccb2010-08-17 11:22:17 +03004801 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004802 }
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004803
Avi Kivity9dac77f2011-06-01 15:34:25 +03004804 ctxt->eip = ctxt->_eip;
Avi Kivity018a98d2007-11-27 19:30:56 +02004805
4806done:
Avi Kivityda9cb572010-11-22 17:53:21 +02004807 if (rc == X86EMUL_PROPAGATE_FAULT)
4808 ctxt->have_exception = true;
Joerg Roedel775fde82011-04-04 12:39:24 +02004809 if (rc == X86EMUL_INTERCEPTED)
4810 return EMULATION_INTERCEPTED;
4811
Avi Kivitydd856ef2012-08-27 23:46:17 +03004812 if (rc == X86EMUL_CONTINUE)
4813 writeback_registers(ctxt);
4814
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +03004815 return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004816
4817twobyte_insn:
Avi Kivity9dac77f2011-06-01 15:34:25 +03004818 switch (ctxt->b) {
Avi Kivity018a98d2007-11-27 19:30:56 +02004819 case 0x09: /* wbinvd */
Clemens Nosscfb22372011-04-21 21:16:05 +02004820 (ctxt->ops->wbinvd)(ctxt);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004821 break;
4822 case 0x08: /* invd */
Avi Kivity018a98d2007-11-27 19:30:56 +02004823 case 0x0d: /* GrpP (prefetch) */
4824 case 0x18: /* Grp16 (prefetch/nop) */
Avi Kivity018a98d2007-11-27 19:30:56 +02004825 break;
4826 case 0x20: /* mov cr, reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004827 ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
Avi Kivity018a98d2007-11-27 19:30:56 +02004828 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004829 case 0x21: /* mov from dr to reg */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004830 ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004831 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004832 case 0x40 ... 0x4f: /* cmov */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004833 ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val;
4834 if (!test_cc(ctxt->b, ctxt->eflags))
4835 ctxt->dst.type = OP_NONE; /* no writeback */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004836 break;
Gleb Natapovb2833e32009-04-12 13:36:30 +03004837 case 0x80 ... 0x8f: /* jnz rel, etc*/
Avi Kivity9dac77f2011-06-01 15:34:25 +03004838 if (test_cc(ctxt->b, ctxt->eflags))
4839 jmp_rel(ctxt, ctxt->src.val);
Avi Kivity018a98d2007-11-27 19:30:56 +02004840 break;
Wei Yongjunee45b582010-08-06 17:10:07 +08004841 case 0x90 ... 0x9f: /* setcc r/m8 */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004842 ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
Wei Yongjunee45b582010-08-06 17:10:07 +08004843 break;
Glauber Costa2a7c5b82008-07-10 17:08:15 -03004844 case 0xae: /* clflush */
4845 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846 case 0xb6 ... 0xb7: /* movzx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004847 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004848 ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
Avi Kivity9dac77f2011-06-01 15:34:25 +03004849 : (u16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851 case 0xbe ... 0xbf: /* movsx */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004852 ctxt->dst.bytes = ctxt->op_bytes;
Avi Kivity361cad22012-06-11 19:40:15 +03004853 ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
Avi Kivity9dac77f2011-06-01 15:34:25 +03004854 (s16) ctxt->src.val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004855 break;
Wei Yongjun92f738a2010-08-17 09:19:34 +08004856 case 0xc0 ... 0xc1: /* xadd */
Avi Kivitya31b9ce2011-09-07 16:41:35 +03004857 emulate_2op_SrcV(ctxt, "add");
Wei Yongjun92f738a2010-08-17 09:19:34 +08004858 /* Write back the register source. */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004859 ctxt->src.val = ctxt->dst.orig_val;
4860 write_register_operand(&ctxt->src);
Wei Yongjun92f738a2010-08-17 09:19:34 +08004861 break;
Sheng Yanga012e652007-10-15 14:24:20 +08004862 case 0xc3: /* movnti */
Avi Kivity9dac77f2011-06-01 15:34:25 +03004863 ctxt->dst.bytes = ctxt->op_bytes;
4864 ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val :
4865 (u64) ctxt->src.val;
Sheng Yanga012e652007-10-15 14:24:20 +08004866 break;
Avi Kivity91269b82010-07-25 14:51:16 +03004867 default:
4868 goto cannot_emulate;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 }
Avi Kivity7d9ddae2010-08-30 17:12:28 +03004870
4871 if (rc != X86EMUL_CONTINUE)
4872 goto done;
4873
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 goto writeback;
4875
4876cannot_emulate:
Gleb Natapova0c0ab22011-03-28 16:57:49 +02004877 return EMULATION_FAILED;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878}
Avi Kivitydd856ef2012-08-27 23:46:17 +03004879
4880void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
4881{
4882 invalidate_registers(ctxt);
4883}
4884
4885void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
4886{
4887 writeback_registers(ctxt);
4888}